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