Commit | Line | Data |
---|---|---|
c39c72ee PMF |
1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
93e5ce29 PMF |
18 | /* API used by UST components to communicate with each other via sockets. */ |
19 | ||
d0b5f2b9 | 20 | #define _GNU_SOURCE |
f446d1cb | 21 | #include <dirent.h> |
f9e5ce61 PMF |
22 | #include <sys/types.h> |
23 | #include <signal.h> | |
24 | #include <errno.h> | |
0f79e1ef | 25 | #include <limits.h> |
f9e5ce61 PMF |
26 | #include <sys/socket.h> |
27 | #include <sys/un.h> | |
d0b5f2b9 | 28 | #include <unistd.h> |
aca1ad90 | 29 | #include <poll.h> |
4723ca09 | 30 | #include <sys/epoll.h> |
803a4f58 | 31 | #include <sys/stat.h> |
f9e5ce61 PMF |
32 | |
33 | #include <stdio.h> | |
34 | #include <stdlib.h> | |
d0b5f2b9 PMF |
35 | #include <string.h> |
36 | ||
37 | #include "ustcomm.h" | |
6af64c43 | 38 | #include "usterr.h" |
2dae156b | 39 | #include "share.h" |
f9e5ce61 | 40 | |
d6d27063 PMF |
41 | static int mkdir_p(const char *path, mode_t mode) |
42 | { | |
c555b133 | 43 | const char *path_p; |
d6d27063 PMF |
44 | char *tmp; |
45 | ||
46 | int retval = 0; | |
47 | int result; | |
18baca84 | 48 | mode_t old_umask; |
d6d27063 | 49 | |
7032c7d3 | 50 | tmp = zmalloc(strlen(path) + 1); |
d6d27063 PMF |
51 | if (tmp == NULL) |
52 | return -1; | |
53 | ||
54 | /* skip first / */ | |
55 | path_p = path+1; | |
56 | ||
18baca84 | 57 | old_umask = umask(0); |
d6d27063 PMF |
58 | for(;;) { |
59 | while (*path_p != '/') { | |
60 | if(*path_p == 0) | |
61 | break; | |
62 | ++path_p; | |
63 | } | |
64 | if (*path_p == '/') { | |
65 | strncpy(tmp, path, path_p - path); | |
66 | tmp[path_p-path] = '\0'; | |
67 | if (tmp[path_p - path - 1] != '/') { | |
68 | result = mkdir(tmp, mode); | |
69 | if(result == -1) { | |
70 | if (!(errno == EEXIST || errno == EACCES || errno == EROFS)) { | |
71 | /* Then this is a real error */ | |
72 | retval = -1; | |
73 | break; | |
74 | } | |
75 | } | |
76 | } | |
77 | /* pass / */ | |
78 | path_p++; | |
79 | } else { | |
80 | /* last component */ | |
81 | result = mkdir(path, mode); | |
82 | if (result == -1) | |
83 | retval = -1; | |
84 | break; | |
85 | } | |
86 | } | |
87 | ||
88 | free(tmp); | |
18baca84 | 89 | umask(old_umask); |
d6d27063 PMF |
90 | return retval; |
91 | } | |
92 | ||
4723ca09 NC |
93 | static struct sockaddr_un * create_sock_addr(const char *name, |
94 | size_t *sock_addr_size) | |
f9e5ce61 | 95 | { |
4723ca09 NC |
96 | struct sockaddr_un * addr; |
97 | size_t alloc_size; | |
f9e5ce61 | 98 | |
4723ca09 NC |
99 | alloc_size = (size_t) (((struct sockaddr_un *) 0)->sun_path) + |
100 | strlen(name) + 1; | |
5932431b | 101 | |
4723ca09 NC |
102 | addr = malloc(alloc_size); |
103 | if (addr < 0) { | |
104 | ERR("allocating addr failed"); | |
105 | return NULL; | |
106 | } | |
ab33e65c | 107 | |
4723ca09 NC |
108 | addr->sun_family = AF_UNIX; |
109 | strcpy(addr->sun_path, name); | |
110 | ||
111 | *sock_addr_size = alloc_size; | |
112 | ||
113 | return addr; | |
114 | } | |
2dae156b | 115 | |
4723ca09 | 116 | struct ustcomm_sock * ustcomm_init_sock(int fd, int epoll_fd, |
0222e121 | 117 | struct cds_list_head *list) |
811e4b93 | 118 | { |
4723ca09 NC |
119 | struct epoll_event ev; |
120 | struct ustcomm_sock *sock; | |
811e4b93 | 121 | |
4723ca09 NC |
122 | sock = malloc(sizeof(struct ustcomm_sock)); |
123 | if (!sock) { | |
124 | perror("malloc: couldn't allocate ustcomm_sock"); | |
125 | return NULL; | |
811e4b93 | 126 | } |
4723ca09 NC |
127 | |
128 | ev.events = EPOLLIN; | |
129 | ev.data.ptr = sock; | |
130 | sock->fd = fd; | |
131 | ||
132 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock->fd, &ev) == -1) { | |
133 | perror("epoll_ctl: failed to add socket\n"); | |
134 | free(sock); | |
135 | return NULL; | |
688760ef | 136 | } |
811e4b93 | 137 | |
4723ca09 NC |
138 | sock->epoll_fd = epoll_fd; |
139 | if (list) { | |
0222e121 | 140 | cds_list_add(&sock->list, list); |
4723ca09 | 141 | } else { |
0222e121 | 142 | CDS_INIT_LIST_HEAD(&sock->list); |
4723ca09 NC |
143 | } |
144 | ||
145 | return sock; | |
811e4b93 PMF |
146 | } |
147 | ||
4723ca09 NC |
148 | void ustcomm_del_sock(struct ustcomm_sock *sock, int keep_in_epoll) |
149 | { | |
0222e121 | 150 | cds_list_del(&sock->list); |
4723ca09 NC |
151 | if (!keep_in_epoll) { |
152 | if (epoll_ctl(sock->epoll_fd, EPOLL_CTL_DEL, sock->fd, NULL) == -1) { | |
153 | PERROR("epoll_ctl: failed to delete socket"); | |
154 | } | |
155 | } | |
156 | close(sock->fd); | |
157 | free(sock); | |
158 | } | |
b0540e11 | 159 | |
4723ca09 NC |
160 | struct ustcomm_sock * ustcomm_init_named_socket(const char *name, |
161 | int epoll_fd) | |
b0540e11 | 162 | { |
b0540e11 | 163 | int result; |
4723ca09 NC |
164 | int fd; |
165 | size_t sock_addr_size; | |
166 | struct sockaddr_un * addr; | |
167 | struct ustcomm_sock *sock; | |
c97d4437 | 168 | |
4723ca09 NC |
169 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
170 | if(fd == -1) { | |
171 | PERROR("socket"); | |
172 | return NULL; | |
c97d4437 | 173 | } |
b0540e11 | 174 | |
4723ca09 NC |
175 | addr = create_sock_addr(name, &sock_addr_size); |
176 | if (addr == NULL) { | |
177 | ERR("allocating addr, UST thread bailing"); | |
178 | goto close_sock; | |
b0540e11 PMF |
179 | } |
180 | ||
4723ca09 NC |
181 | result = access(name, F_OK); |
182 | if(result == 0) { | |
183 | /* file exists */ | |
184 | result = unlink(name); | |
185 | if(result == -1) { | |
186 | PERROR("unlink of socket file"); | |
187 | goto free_addr; | |
188 | } | |
189 | DBG("socket already exists; overwriting"); | |
08b8805e | 190 | } |
b0540e11 | 191 | |
4723ca09 | 192 | result = bind(fd, (struct sockaddr *)addr, sock_addr_size); |
08230db7 | 193 | if(result == -1) { |
4723ca09 NC |
194 | PERROR("bind"); |
195 | goto free_addr; | |
08230db7 PMF |
196 | } |
197 | ||
4723ca09 | 198 | result = listen(fd, 1); |
08230db7 | 199 | if(result == -1) { |
4723ca09 NC |
200 | PERROR("listen"); |
201 | goto free_addr; | |
08230db7 PMF |
202 | } |
203 | ||
4723ca09 NC |
204 | sock = ustcomm_init_sock(fd, epoll_fd, |
205 | NULL); | |
206 | if (!sock) { | |
207 | ERR("failed to create ustcomm_sock"); | |
208 | goto free_addr; | |
209 | } | |
b0540e11 | 210 | |
4723ca09 | 211 | free(addr); |
b0540e11 | 212 | |
4723ca09 | 213 | return sock; |
811e4b93 | 214 | |
4723ca09 NC |
215 | free_addr: |
216 | free(addr); | |
217 | close_sock: | |
218 | close(fd); | |
2dae156b | 219 | |
4723ca09 NC |
220 | return NULL; |
221 | } | |
222 | ||
223 | void ustcomm_del_named_sock(struct ustcomm_sock *sock, | |
224 | int keep_socket_file) | |
d0b5f2b9 | 225 | { |
4723ca09 NC |
226 | int result, fd; |
227 | struct stat st; | |
228 | struct sockaddr dummy; | |
229 | struct sockaddr_un *sockaddr = NULL; | |
230 | int alloc_size; | |
d0b5f2b9 | 231 | |
4723ca09 | 232 | fd = sock->fd; |
b02e31e5 | 233 | |
4723ca09 | 234 | if(!keep_socket_file) { |
5932431b | 235 | |
4723ca09 NC |
236 | /* Get the socket name */ |
237 | alloc_size = sizeof(dummy); | |
238 | if (getsockname(fd, &dummy, (socklen_t *)&alloc_size) < 0) { | |
239 | PERROR("getsockname failed"); | |
240 | goto del_sock; | |
2dae156b | 241 | } |
b0540e11 | 242 | |
4723ca09 NC |
243 | sockaddr = zmalloc(alloc_size); |
244 | if (!sockaddr) { | |
245 | ERR("failed to allocate sockaddr"); | |
246 | goto del_sock; | |
5932431b PMF |
247 | } |
248 | ||
4723ca09 NC |
249 | if (getsockname(fd, sockaddr, (socklen_t *)&alloc_size) < 0) { |
250 | PERROR("getsockname failed"); | |
251 | goto free_sockaddr; | |
5932431b PMF |
252 | } |
253 | ||
4723ca09 NC |
254 | /* Destroy socket */ |
255 | result = stat(sockaddr->sun_path, &st); | |
256 | if(result < 0) { | |
257 | PERROR("stat (%s)", sockaddr->sun_path); | |
258 | goto free_sockaddr; | |
2dae156b | 259 | } |
4723ca09 NC |
260 | |
261 | /* Paranoid check before deleting. */ | |
262 | result = S_ISSOCK(st.st_mode); | |
263 | if(!result) { | |
264 | ERR("The socket we are about to delete is not a socket."); | |
265 | goto free_sockaddr; | |
2dae156b PMF |
266 | } |
267 | ||
4723ca09 NC |
268 | result = unlink(sockaddr->sun_path); |
269 | if(result < 0) { | |
270 | PERROR("unlink"); | |
271 | } | |
2dae156b | 272 | } |
b0540e11 | 273 | |
4723ca09 NC |
274 | free_sockaddr: |
275 | free(sockaddr); | |
2dae156b | 276 | |
4723ca09 NC |
277 | del_sock: |
278 | ustcomm_del_sock(sock, keep_socket_file); | |
d0b5f2b9 PMF |
279 | } |
280 | ||
72098143 NC |
281 | int ustcomm_recv_alloc(int sock, |
282 | struct ustcomm_header *header, | |
283 | char **data) { | |
284 | int result; | |
285 | struct ustcomm_header peek_header; | |
286 | struct iovec iov[2]; | |
287 | struct msghdr msg; | |
5932431b | 288 | |
72098143 NC |
289 | /* Just to make the caller fail hard */ |
290 | *data = NULL; | |
811e4b93 | 291 | |
72098143 NC |
292 | result = recv(sock, &peek_header, sizeof(peek_header), |
293 | MSG_PEEK | MSG_WAITALL); | |
294 | if (result <= 0) { | |
295 | if(errno == ECONNRESET) { | |
296 | return 0; | |
297 | } else if (errno == EINTR) { | |
298 | return -1; | |
299 | } else if (result < 0) { | |
300 | PERROR("recv"); | |
301 | return -1; | |
302 | } | |
303 | return 0; | |
811e4b93 PMF |
304 | } |
305 | ||
72098143 | 306 | memset(&msg, 0, sizeof(msg)); |
99b72dc0 | 307 | |
72098143 NC |
308 | iov[0].iov_base = (char *)header; |
309 | iov[0].iov_len = sizeof(struct ustcomm_header); | |
310 | ||
311 | msg.msg_iov = iov; | |
312 | msg.msg_iovlen = 1; | |
313 | ||
314 | if (peek_header.size) { | |
315 | *data = zmalloc(peek_header.size); | |
316 | if (!*data) { | |
317 | return -ENOMEM; | |
318 | } | |
319 | ||
320 | iov[1].iov_base = *data; | |
321 | iov[1].iov_len = peek_header.size; | |
322 | ||
323 | msg.msg_iovlen++; | |
4723ca09 | 324 | } |
99b72dc0 | 325 | |
72098143 | 326 | result = recvmsg(sock, &msg, MSG_WAITALL); |
4723ca09 | 327 | if (result < 0) { |
72098143 NC |
328 | free(*data); |
329 | PERROR("recvmsg failed"); | |
99b72dc0 PMF |
330 | } |
331 | ||
72098143 | 332 | return result; |
99b72dc0 PMF |
333 | } |
334 | ||
4723ca09 NC |
335 | /* returns 1 to indicate a message was received |
336 | * returns 0 to indicate no message was received (end of stream) | |
688760ef PMF |
337 | * returns -1 to indicate an error |
338 | */ | |
4723ca09 NC |
339 | int ustcomm_recv_fd(int sock, |
340 | struct ustcomm_header *header, | |
72098143 | 341 | char *data, int *fd) |
b0540e11 | 342 | { |
aca1ad90 | 343 | int result; |
4723ca09 NC |
344 | struct ustcomm_header peek_header; |
345 | struct iovec iov[2]; | |
346 | struct msghdr msg; | |
347 | struct cmsghdr *cmsg; | |
348 | char buf[CMSG_SPACE(sizeof(int))]; | |
349 | ||
350 | result = recv(sock, &peek_header, sizeof(peek_header), | |
351 | MSG_PEEK | MSG_WAITALL); | |
352 | if (result <= 0) { | |
353 | if(errno == ECONNRESET) { | |
354 | return 0; | |
355 | } else if (errno == EINTR) { | |
356 | return -1; | |
357 | } else if (result < 0) { | |
358 | PERROR("recv"); | |
aca1ad90 PMF |
359 | return -1; |
360 | } | |
4723ca09 NC |
361 | return 0; |
362 | } | |
aca1ad90 | 363 | |
4723ca09 | 364 | memset(&msg, 0, sizeof(msg)); |
5932431b | 365 | |
4723ca09 NC |
366 | iov[0].iov_base = (char *)header; |
367 | iov[0].iov_len = sizeof(struct ustcomm_header); | |
aca1ad90 | 368 | |
4723ca09 NC |
369 | msg.msg_iov = iov; |
370 | msg.msg_iovlen = 1; | |
aca1ad90 | 371 | |
72098143 NC |
372 | if (peek_header.size && data) { |
373 | if (peek_header.size < 0 || | |
374 | peek_header.size > USTCOMM_DATA_SIZE) { | |
fbae86d6 | 375 | ERR("big peek header! %ld", peek_header.size); |
72098143 | 376 | return 0; |
2a79ceeb | 377 | } |
688760ef | 378 | |
72098143 | 379 | iov[1].iov_base = data; |
4723ca09 | 380 | iov[1].iov_len = peek_header.size; |
aca1ad90 | 381 | |
4723ca09 NC |
382 | msg.msg_iovlen++; |
383 | } | |
aca1ad90 | 384 | |
4723ca09 NC |
385 | if (fd && peek_header.fd_included) { |
386 | msg.msg_control = buf; | |
387 | msg.msg_controllen = sizeof(buf); | |
388 | } | |
aca1ad90 | 389 | |
72098143 | 390 | result = recvmsg(sock, &msg, MSG_WAITALL); |
4723ca09 | 391 | if (result <= 0) { |
72098143 NC |
392 | if (result < 0) { |
393 | PERROR("recvmsg failed"); | |
aca1ad90 | 394 | } |
72098143 | 395 | return result; |
4723ca09 NC |
396 | } |
397 | ||
398 | if (fd && peek_header.fd_included) { | |
399 | cmsg = CMSG_FIRSTHDR(&msg); | |
400 | result = 0; | |
401 | while (cmsg != NULL) { | |
402 | if (cmsg->cmsg_level == SOL_SOCKET | |
403 | && cmsg->cmsg_type == SCM_RIGHTS) { | |
404 | *fd = *(int *) CMSG_DATA(cmsg); | |
405 | result = 1; | |
406 | break; | |
aca1ad90 | 407 | } |
4723ca09 NC |
408 | cmsg = CMSG_NXTHDR(&msg, cmsg); |
409 | } | |
410 | if (!result) { | |
411 | ERR("Failed to receive file descriptor\n"); | |
aca1ad90 | 412 | } |
aca1ad90 PMF |
413 | } |
414 | ||
4723ca09 | 415 | return 1; |
b0540e11 PMF |
416 | } |
417 | ||
4723ca09 NC |
418 | int ustcomm_recv(int sock, |
419 | struct ustcomm_header *header, | |
72098143 | 420 | char *data) |
811e4b93 | 421 | { |
4723ca09 | 422 | return ustcomm_recv_fd(sock, header, data, NULL); |
811e4b93 PMF |
423 | } |
424 | ||
4723ca09 | 425 | |
4723ca09 NC |
426 | int ustcomm_send_fd(int sock, |
427 | const struct ustcomm_header *header, | |
428 | const char *data, | |
429 | int *fd) | |
46ef48cd | 430 | { |
4723ca09 NC |
431 | struct iovec iov[2]; |
432 | struct msghdr msg; | |
433 | int result; | |
434 | struct cmsghdr *cmsg; | |
435 | char buf[CMSG_SPACE(sizeof(int))]; | |
436 | ||
437 | memset(&msg, 0, sizeof(msg)); | |
438 | ||
439 | iov[0].iov_base = (char *)header; | |
440 | iov[0].iov_len = sizeof(struct ustcomm_header); | |
441 | ||
442 | msg.msg_iov = iov; | |
443 | msg.msg_iovlen = 1; | |
444 | ||
72098143 | 445 | if (header->size && data) { |
4723ca09 NC |
446 | iov[1].iov_base = (char *)data; |
447 | iov[1].iov_len = header->size; | |
448 | ||
449 | msg.msg_iovlen++; | |
46ef48cd | 450 | |
46ef48cd PMF |
451 | } |
452 | ||
4723ca09 NC |
453 | if (fd && header->fd_included) { |
454 | msg.msg_control = buf; | |
455 | msg.msg_controllen = sizeof(buf); | |
456 | cmsg = CMSG_FIRSTHDR(&msg); | |
457 | cmsg->cmsg_level = SOL_SOCKET; | |
458 | cmsg->cmsg_type = SCM_RIGHTS; | |
459 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
460 | *(int *) CMSG_DATA(cmsg) = *fd; | |
461 | msg.msg_controllen = cmsg->cmsg_len; | |
462 | } | |
463 | ||
464 | result = sendmsg(sock, &msg, MSG_NOSIGNAL); | |
465 | if (result < 0 && errno != EPIPE) { | |
466 | PERROR("sendmsg failed"); | |
467 | } | |
468 | return result; | |
46ef48cd PMF |
469 | } |
470 | ||
4723ca09 NC |
471 | int ustcomm_send(int sock, |
472 | const struct ustcomm_header *header, | |
473 | const char *data) | |
d0b5f2b9 | 474 | { |
4723ca09 NC |
475 | return ustcomm_send_fd(sock, header, data, NULL); |
476 | } | |
d0b5f2b9 | 477 | |
72098143 NC |
478 | int ustcomm_req(int sock, |
479 | const struct ustcomm_header *req_header, | |
480 | const char *req_data, | |
481 | struct ustcomm_header *res_header, | |
482 | char *res_data) | |
4723ca09 NC |
483 | { |
484 | int result; | |
aca1ad90 | 485 | |
72098143 | 486 | result = ustcomm_send(sock, req_header, req_data); |
4723ca09 NC |
487 | if ( result <= 0) { |
488 | return result; | |
b0540e11 | 489 | } |
d0b5f2b9 | 490 | |
72098143 | 491 | return ustcomm_recv(sock, res_header, res_data); |
4e2a8808 PMF |
492 | } |
493 | ||
2a79ceeb PMF |
494 | /* Return value: |
495 | * 0: success | |
496 | * -1: error | |
497 | */ | |
498 | ||
4723ca09 | 499 | int ustcomm_connect_path(const char *name, int *connection_fd) |
4e2a8808 | 500 | { |
4723ca09 NC |
501 | int result, fd; |
502 | size_t sock_addr_size; | |
503 | struct sockaddr_un *addr; | |
4e2a8808 | 504 | |
4723ca09 NC |
505 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
506 | if(fd == -1) { | |
4e2a8808 PMF |
507 | PERROR("socket"); |
508 | return -1; | |
509 | } | |
510 | ||
4723ca09 NC |
511 | addr = create_sock_addr(name, &sock_addr_size); |
512 | if (addr == NULL) { | |
513 | ERR("allocating addr failed"); | |
514 | goto close_sock; | |
52c51a47 | 515 | } |
4e2a8808 | 516 | |
4723ca09 | 517 | result = connect(fd, (struct sockaddr *)addr, sock_addr_size); |
4e2a8808 | 518 | if(result == -1) { |
4723ca09 NC |
519 | PERROR("connect (path=%s)", name); |
520 | goto free_sock_addr; | |
4e2a8808 PMF |
521 | } |
522 | ||
4723ca09 NC |
523 | *connection_fd = fd; |
524 | ||
525 | free(addr); | |
4e2a8808 PMF |
526 | |
527 | return 0; | |
4e2a8808 | 528 | |
4723ca09 NC |
529 | free_sock_addr: |
530 | free(addr); | |
531 | close_sock: | |
532 | close(fd); | |
533 | ||
534 | return -1; | |
4e2a8808 PMF |
535 | } |
536 | ||
dbd75de7 NC |
537 | /* Returns the current users socket directory, must be freed */ |
538 | char *ustcomm_user_sock_dir(void) | |
539 | { | |
540 | int result; | |
541 | char *sock_dir = NULL; | |
542 | ||
543 | result = asprintf(&sock_dir, "%s%s", USER_SOCK_DIR, | |
544 | cuserid(NULL)); | |
545 | if (result < 0) { | |
546 | ERR("string overflow allocating directory name"); | |
547 | return NULL; | |
548 | } | |
549 | ||
550 | return sock_dir; | |
551 | } | |
4723ca09 | 552 | |
0f79e1ef NC |
553 | static int time_and_pid_from_socket_name(char *sock_name, unsigned long *time, |
554 | pid_t *pid) | |
555 | { | |
556 | char *saveptr, *pid_m_time_str; | |
557 | char *sock_basename = strdup(basename(sock_name)); | |
558 | ||
559 | if (!sock_basename) { | |
560 | return -1; | |
561 | } | |
562 | ||
563 | /* This is the pid */ | |
564 | pid_m_time_str = strtok_r(sock_basename, ".", &saveptr); | |
565 | if (!pid_m_time_str) { | |
566 | goto out_err; | |
567 | } | |
568 | ||
569 | errno = 0; | |
570 | *pid = (pid_t)strtoul(pid_m_time_str, NULL, 10); | |
571 | if (errno) { | |
572 | goto out_err; | |
573 | } | |
574 | ||
575 | /* This should be the time-stamp */ | |
576 | pid_m_time_str = strtok_r(NULL, ".", &saveptr); | |
577 | if (!pid_m_time_str) { | |
578 | goto out_err; | |
579 | } | |
580 | ||
581 | errno = 0; | |
582 | *time = strtoul(pid_m_time_str, NULL, 10); | |
583 | if (errno) { | |
584 | goto out_err; | |
585 | } | |
586 | ||
587 | return 0; | |
588 | ||
589 | out_err: | |
590 | free(sock_basename); | |
591 | return -1; | |
592 | } | |
593 | ||
594 | time_t ustcomm_pid_st_mtime(pid_t pid) | |
595 | { | |
596 | struct stat proc_stat; | |
597 | char proc_name[PATH_MAX]; | |
598 | ||
599 | if (snprintf(proc_name, PATH_MAX - 1, "/proc/%ld", (long) pid) < 0) { | |
600 | return 0; | |
601 | } | |
602 | ||
603 | if (stat(proc_name, &proc_stat)) { | |
604 | return 0; | |
605 | } | |
606 | ||
607 | return proc_stat.st_mtime; | |
608 | } | |
609 | ||
610 | int ustcomm_is_socket_live(char *sock_name, pid_t *read_pid) | |
611 | { | |
612 | time_t time_from_pid; | |
613 | unsigned long time_from_sock; | |
614 | pid_t pid; | |
615 | ||
616 | if (time_and_pid_from_socket_name(sock_name, &time_from_sock, &pid)) { | |
617 | return 0; | |
618 | } | |
619 | ||
620 | if (read_pid) { | |
621 | *read_pid = pid; | |
622 | } | |
623 | ||
624 | time_from_pid = ustcomm_pid_st_mtime(pid); | |
625 | if (!time_from_pid) { | |
626 | return 0; | |
627 | } | |
628 | ||
629 | if ((unsigned long) time_from_pid == time_from_sock) { | |
630 | return 1; | |
631 | } | |
632 | ||
633 | return 0; | |
634 | } | |
635 | ||
636 | #define MAX_SOCK_PATH_BASE_LEN 100 | |
637 | ||
638 | static int ustcomm_get_sock_name(char *dir_name, pid_t pid, char *sock_name) | |
639 | { | |
640 | struct dirent *dirent; | |
641 | char sock_path_base[MAX_SOCK_PATH_BASE_LEN]; | |
642 | int len; | |
643 | DIR *dir = opendir(dir_name); | |
644 | ||
645 | snprintf(sock_path_base, MAX_SOCK_PATH_BASE_LEN - 1, | |
646 | "%ld.", (long) pid); | |
647 | len = strlen(sock_path_base); | |
648 | ||
649 | while ((dirent = readdir(dir))) { | |
650 | if (!strcmp(dirent->d_name, ".") || | |
651 | !strcmp(dirent->d_name, "..") || | |
652 | !strcmp(dirent->d_name, "ust-consumer") || | |
653 | dirent->d_type == DT_DIR || | |
654 | strncmp(dirent->d_name, sock_path_base, len)) { | |
655 | continue; | |
656 | } | |
657 | ||
658 | if (ustcomm_is_socket_live(dirent->d_name, NULL)) { | |
659 | if (snprintf(sock_name, PATH_MAX - 1, "%s/%s", | |
660 | dir_name, dirent->d_name) < 0) { | |
661 | PERROR("path longer than PATH_MAX?"); | |
662 | goto out_err; | |
663 | } | |
664 | closedir(dir); | |
665 | return 0; | |
666 | } | |
667 | } | |
668 | ||
669 | out_err: | |
670 | closedir(dir); | |
671 | return -1; | |
672 | } | |
673 | ||
2a79ceeb PMF |
674 | /* Open a connection to a traceable app. |
675 | * | |
676 | * Return value: | |
677 | * 0: success | |
678 | * -1: error | |
679 | */ | |
680 | ||
f446d1cb | 681 | static int connect_app_non_root(pid_t pid, int *app_fd) |
4e2a8808 PMF |
682 | { |
683 | int result; | |
4723ca09 | 684 | int retval = 0; |
0f79e1ef NC |
685 | char *dir_name; |
686 | char sock_name[PATH_MAX]; | |
dbd75de7 NC |
687 | |
688 | dir_name = ustcomm_user_sock_dir(); | |
689 | if (!dir_name) | |
690 | return -ENOMEM; | |
4e2a8808 | 691 | |
0f79e1ef NC |
692 | if (ustcomm_get_sock_name(dir_name, pid, sock_name)) { |
693 | retval = -ENOENT; | |
dbd75de7 | 694 | goto free_dir_name; |
4e2a8808 PMF |
695 | } |
696 | ||
dbd75de7 | 697 | result = ustcomm_connect_path(sock_name, app_fd); |
4723ca09 NC |
698 | if (result < 0) { |
699 | ERR("failed to connect to app"); | |
700 | retval = -1; | |
0f79e1ef | 701 | goto free_dir_name; |
4723ca09 | 702 | } |
2a79ceeb | 703 | |
dbd75de7 NC |
704 | free_dir_name: |
705 | free(dir_name); | |
2a79ceeb | 706 | |
4723ca09 | 707 | return retval; |
2a79ceeb PMF |
708 | } |
709 | ||
f446d1cb NC |
710 | |
711 | ||
712 | static int connect_app_root(pid_t pid, int *app_fd) | |
713 | { | |
714 | DIR *tmp_dir; | |
715 | struct dirent *dirent; | |
0f79e1ef NC |
716 | char dir_name[PATH_MAX], sock_name[PATH_MAX]; |
717 | int result = -1; | |
f446d1cb NC |
718 | |
719 | tmp_dir = opendir(USER_TMP_DIR); | |
720 | if (!tmp_dir) { | |
721 | return -1; | |
722 | } | |
723 | ||
724 | while ((dirent = readdir(tmp_dir))) { | |
725 | if (!strncmp(dirent->d_name, USER_SOCK_DIR_BASE, | |
726 | strlen(USER_SOCK_DIR_BASE))) { | |
727 | ||
0f79e1ef NC |
728 | if (snprintf(dir_name, PATH_MAX - 1, "%s/%s", USER_TMP_DIR, |
729 | dirent->d_name) < 0) { | |
730 | continue; | |
f446d1cb NC |
731 | } |
732 | ||
0f79e1ef NC |
733 | if (ustcomm_get_sock_name(dir_name, pid, sock_name)) { |
734 | continue; | |
735 | } | |
f446d1cb | 736 | |
0f79e1ef | 737 | result = ustcomm_connect_path(sock_name, app_fd); |
f446d1cb NC |
738 | |
739 | if (result == 0) { | |
740 | goto close_tmp_dir; | |
741 | } | |
742 | } | |
743 | } | |
744 | ||
745 | close_tmp_dir: | |
746 | closedir(tmp_dir); | |
747 | ||
748 | return result; | |
749 | } | |
750 | ||
751 | int ustcomm_connect_app(pid_t pid, int *app_fd) | |
752 | { | |
753 | *app_fd = 0; | |
754 | ||
755 | if (geteuid()) { | |
756 | return connect_app_non_root(pid, app_fd); | |
757 | } else { | |
758 | return connect_app_root(pid, app_fd); | |
759 | } | |
760 | ||
761 | } | |
762 | ||
304f67a5 | 763 | int ensure_dir_exists(const char *dir, mode_t mode) |
dce0b474 PMF |
764 | { |
765 | struct stat st; | |
766 | int result; | |
767 | ||
304f67a5 | 768 | if (!strcmp(dir, "")) |
dce0b474 PMF |
769 | return -1; |
770 | ||
771 | result = stat(dir, &st); | |
304f67a5 | 772 | if (result < 0 && errno != ENOENT) { |
dce0b474 | 773 | return -1; |
304f67a5 | 774 | } else if (result < 0) { |
dce0b474 | 775 | /* ENOENT */ |
dce0b474 PMF |
776 | int result; |
777 | ||
304f67a5 | 778 | result = mkdir_p(dir, mode); |
dce0b474 | 779 | if(result != 0) { |
d6d27063 | 780 | ERR("executing in recursive creation of directory %s", dir); |
dce0b474 PMF |
781 | return -1; |
782 | } | |
304f67a5 NC |
783 | } else { |
784 | if (st.st_mode != mode) { | |
785 | result = chmod(dir, mode); | |
786 | if (result < 0) { | |
787 | ERR("couldn't set directory mode on %s", dir); | |
788 | return -1; | |
789 | } | |
790 | } | |
dce0b474 PMF |
791 | } |
792 | ||
793 | return 0; | |
794 | } | |
795 | ||
72098143 NC |
796 | char * ustcomm_print_data(char *data_field, int field_size, |
797 | int *offset, const char *format, ...) | |
798 | { | |
799 | va_list args; | |
800 | int count, limit; | |
801 | char *ptr = USTCOMM_POISON_PTR; | |
802 | ||
803 | limit = field_size - *offset; | |
804 | va_start(args, format); | |
805 | count = vsnprintf(&data_field[*offset], limit, format, args); | |
806 | va_end(args); | |
807 | ||
808 | if (count < limit && count > -1) { | |
809 | ptr = NULL + *offset; | |
810 | *offset = *offset + count + 1; | |
811 | } | |
812 | ||
813 | return ptr; | |
814 | } | |
815 | ||
816 | char * ustcomm_restore_ptr(char *ptr, char *data_field, int data_field_size) | |
817 | { | |
818 | if ((unsigned long)ptr > data_field_size || | |
819 | ptr == USTCOMM_POISON_PTR) { | |
820 | return NULL; | |
821 | } | |
08230db7 | 822 | |
72098143 NC |
823 | return data_field + (long)ptr; |
824 | } | |
2a79ceeb | 825 | |
28c1bb40 NC |
826 | int ustcomm_pack_single_field(struct ustcomm_header *header, |
827 | struct ustcomm_single_field *single_field, | |
828 | const char *string) | |
10f2b724 NC |
829 | { |
830 | int offset = 0; | |
831 | ||
28c1bb40 NC |
832 | single_field->field = ustcomm_print_data(single_field->data, |
833 | sizeof(single_field->data), | |
834 | &offset, | |
835 | string); | |
10f2b724 | 836 | |
28c1bb40 | 837 | if (single_field->field == USTCOMM_POISON_PTR) { |
10f2b724 NC |
838 | return -ENOMEM; |
839 | } | |
840 | ||
28c1bb40 | 841 | header->size = COMPUTE_MSG_SIZE(single_field, offset); |
10f2b724 NC |
842 | |
843 | return 0; | |
844 | } | |
845 | ||
28c1bb40 | 846 | int ustcomm_unpack_single_field(struct ustcomm_single_field *single_field) |
10f2b724 | 847 | { |
28c1bb40 NC |
848 | single_field->field = ustcomm_restore_ptr(single_field->field, |
849 | single_field->data, | |
850 | sizeof(single_field->data)); | |
851 | if (!single_field->field) { | |
10f2b724 NC |
852 | return -EINVAL; |
853 | } | |
854 | ||
855 | return 0; | |
856 | } | |
72098143 NC |
857 | |
858 | int ustcomm_pack_channel_info(struct ustcomm_header *header, | |
859 | struct ustcomm_channel_info *ch_inf, | |
d89b8191 | 860 | const char *trace, |
72098143 | 861 | const char *channel) |
b02e31e5 | 862 | { |
72098143 NC |
863 | int offset = 0; |
864 | ||
d89b8191 NC |
865 | ch_inf->trace = ustcomm_print_data(ch_inf->data, |
866 | sizeof(ch_inf->data), | |
867 | &offset, | |
868 | trace); | |
869 | ||
870 | if (ch_inf->trace == USTCOMM_POISON_PTR) { | |
871 | return -ENOMEM; | |
872 | } | |
873 | ||
72098143 NC |
874 | ch_inf->channel = ustcomm_print_data(ch_inf->data, |
875 | sizeof(ch_inf->data), | |
876 | &offset, | |
877 | channel); | |
b02e31e5 | 878 | |
72098143 NC |
879 | if (ch_inf->channel == USTCOMM_POISON_PTR) { |
880 | return -ENOMEM; | |
b02e31e5 PMF |
881 | } |
882 | ||
72098143 NC |
883 | header->size = COMPUTE_MSG_SIZE(ch_inf, offset); |
884 | ||
885 | return 0; | |
b02e31e5 PMF |
886 | } |
887 | ||
72098143 NC |
888 | |
889 | int ustcomm_unpack_channel_info(struct ustcomm_channel_info *ch_inf) | |
b02e31e5 | 890 | { |
d89b8191 NC |
891 | ch_inf->trace = ustcomm_restore_ptr(ch_inf->trace, |
892 | ch_inf->data, | |
893 | sizeof(ch_inf->data)); | |
894 | if (!ch_inf->trace) { | |
895 | return -EINVAL; | |
896 | } | |
897 | ||
72098143 NC |
898 | ch_inf->channel = ustcomm_restore_ptr(ch_inf->channel, |
899 | ch_inf->data, | |
900 | sizeof(ch_inf->data)); | |
901 | if (!ch_inf->channel) { | |
902 | return -EINVAL; | |
903 | } | |
b02e31e5 | 904 | |
72098143 NC |
905 | return 0; |
906 | } | |
907 | ||
908 | int ustcomm_pack_buffer_info(struct ustcomm_header *header, | |
909 | struct ustcomm_buffer_info *buf_inf, | |
d89b8191 | 910 | const char *trace, |
72098143 NC |
911 | const char *channel, |
912 | int channel_cpu) | |
913 | { | |
914 | int offset = 0; | |
915 | ||
d89b8191 NC |
916 | buf_inf->trace = ustcomm_print_data(buf_inf->data, |
917 | sizeof(buf_inf->data), | |
918 | &offset, | |
919 | trace); | |
920 | ||
921 | if (buf_inf->trace == USTCOMM_POISON_PTR) { | |
922 | return -ENOMEM; | |
923 | } | |
924 | ||
72098143 NC |
925 | buf_inf->channel = ustcomm_print_data(buf_inf->data, |
926 | sizeof(buf_inf->data), | |
927 | &offset, | |
928 | channel); | |
929 | ||
930 | if (buf_inf->channel == USTCOMM_POISON_PTR) { | |
931 | return -ENOMEM; | |
b02e31e5 PMF |
932 | } |
933 | ||
72098143 NC |
934 | buf_inf->ch_cpu = channel_cpu; |
935 | ||
936 | header->size = COMPUTE_MSG_SIZE(buf_inf, offset); | |
937 | ||
938 | return 0; | |
b02e31e5 PMF |
939 | } |
940 | ||
72098143 NC |
941 | |
942 | int ustcomm_unpack_buffer_info(struct ustcomm_buffer_info *buf_inf) | |
b02e31e5 | 943 | { |
d89b8191 NC |
944 | buf_inf->trace = ustcomm_restore_ptr(buf_inf->trace, |
945 | buf_inf->data, | |
946 | sizeof(buf_inf->data)); | |
947 | if (!buf_inf->trace) { | |
948 | return -EINVAL; | |
949 | } | |
950 | ||
72098143 NC |
951 | buf_inf->channel = ustcomm_restore_ptr(buf_inf->channel, |
952 | buf_inf->data, | |
953 | sizeof(buf_inf->data)); | |
954 | if (!buf_inf->channel) { | |
955 | return -EINVAL; | |
956 | } | |
b02e31e5 | 957 | |
72098143 NC |
958 | return 0; |
959 | } | |
960 | ||
b521931e MD |
961 | int ustcomm_pack_ust_marker_info(struct ustcomm_header *header, |
962 | struct ustcomm_ust_marker_info *ust_marker_inf, | |
d89b8191 | 963 | const char *trace, |
72098143 | 964 | const char *channel, |
b521931e | 965 | const char *ust_marker) |
72098143 NC |
966 | { |
967 | int offset = 0; | |
b02e31e5 | 968 | |
b521931e MD |
969 | ust_marker_inf->trace = ustcomm_print_data(ust_marker_inf->data, |
970 | sizeof(ust_marker_inf->data), | |
d89b8191 NC |
971 | &offset, |
972 | trace); | |
973 | ||
b521931e | 974 | if (ust_marker_inf->trace == USTCOMM_POISON_PTR) { |
d89b8191 NC |
975 | return -ENOMEM; |
976 | } | |
977 | ||
978 | ||
b521931e MD |
979 | ust_marker_inf->channel = ustcomm_print_data(ust_marker_inf->data, |
980 | sizeof(ust_marker_inf->data), | |
72098143 NC |
981 | &offset, |
982 | channel); | |
b02e31e5 | 983 | |
b521931e | 984 | if (ust_marker_inf->channel == USTCOMM_POISON_PTR) { |
72098143 NC |
985 | return -ENOMEM; |
986 | } | |
987 | ||
988 | ||
b521931e MD |
989 | ust_marker_inf->ust_marker = ustcomm_print_data(ust_marker_inf->data, |
990 | sizeof(ust_marker_inf->data), | |
72098143 | 991 | &offset, |
b521931e | 992 | ust_marker); |
b02e31e5 | 993 | |
b521931e | 994 | if (ust_marker_inf->ust_marker == USTCOMM_POISON_PTR) { |
72098143 | 995 | return -ENOMEM; |
b02e31e5 PMF |
996 | } |
997 | ||
b521931e | 998 | header->size = COMPUTE_MSG_SIZE(ust_marker_inf, offset); |
b02e31e5 | 999 | |
72098143 NC |
1000 | return 0; |
1001 | } | |
b02e31e5 | 1002 | |
b521931e | 1003 | int ustcomm_unpack_ust_marker_info(struct ustcomm_ust_marker_info *ust_marker_inf) |
72098143 | 1004 | { |
b521931e MD |
1005 | ust_marker_inf->trace = ustcomm_restore_ptr(ust_marker_inf->trace, |
1006 | ust_marker_inf->data, | |
1007 | sizeof(ust_marker_inf->data)); | |
1008 | if (!ust_marker_inf->trace) { | |
d89b8191 NC |
1009 | return -EINVAL; |
1010 | } | |
1011 | ||
b521931e MD |
1012 | ust_marker_inf->channel = ustcomm_restore_ptr(ust_marker_inf->channel, |
1013 | ust_marker_inf->data, | |
1014 | sizeof(ust_marker_inf->data)); | |
1015 | if (!ust_marker_inf->channel) { | |
72098143 NC |
1016 | return -EINVAL; |
1017 | } | |
1018 | ||
b521931e MD |
1019 | ust_marker_inf->ust_marker = ustcomm_restore_ptr(ust_marker_inf->ust_marker, |
1020 | ust_marker_inf->data, | |
1021 | sizeof(ust_marker_inf->data)); | |
1022 | if (!ust_marker_inf->ust_marker) { | |
72098143 NC |
1023 | return -EINVAL; |
1024 | } | |
1025 | ||
1026 | return 0; | |
b02e31e5 PMF |
1027 | } |
1028 |