ustd: improve handling of remote app termination
[ust.git] / ustd / ustd.c
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
18 #define _GNU_SOURCE
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/shm.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <pthread.h>
26 #include <signal.h>
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include <getopt.h>
34
35 #include "ustd.h"
36 #include "localerr.h"
37 #include "ustcomm.h"
38 #include "share.h"
39
40 /* return value: 0 = subbuffer is finished, it won't produce data anymore
41 * 1 = got subbuffer successfully
42 * <0 = error
43 */
44
45 #define GET_SUBBUF_OK 1
46 #define GET_SUBBUF_DONE 0
47 #define GET_SUBBUF_DIED 2
48
49 #define PUT_SUBBUF_OK 1
50 #define PUT_SUBBUF_DIED 0
51 #define PUT_SUBBUF_PUSHED 2
52
53 char *sock_path=NULL;
54 char *trace_path=NULL;
55
56 /* Number of active buffers and the mutex to protect it. */
57 int active_buffers = 0;
58 pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER;
59 /* Whether a request to end the program was received. */
60 sig_atomic_t terminate_req = 0;
61
62 int get_subbuffer(struct buffer_info *buf)
63 {
64 char *send_msg=NULL;
65 char *received_msg=NULL;
66 char *rep_code=NULL;
67 int retval;
68 int result;
69
70 asprintf(&send_msg, "get_subbuffer %s", buf->name);
71 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
72 if(result == -1 && errno == EPIPE || result == 0) {
73 DBG("app died while being traced");
74 retval = GET_SUBBUF_DIED;
75 goto end;
76 }
77 else if(result < 0) {
78 ERR("get_subbuffer: ustcomm_send_request failed");
79 retval = -1;
80 goto end;
81 }
82
83 result = sscanf(received_msg, "%as %ld", &rep_code, &buf->consumed_old);
84 if(result != 2 && result != 1) {
85 ERR("unable to parse response to get_subbuffer");
86 retval = -1;
87 goto end_rep;
88 }
89
90 DBG("received msg is %s", received_msg);
91
92 if(!strcmp(rep_code, "OK")) {
93 DBG("got subbuffer %s", buf->name);
94 retval = GET_SUBBUF_OK;
95 }
96 else if(nth_token_is(received_msg, "END", 0) == 1) {
97 retval = GET_SUBBUF_DONE;
98 goto end_rep;
99 }
100 else {
101 DBG("error getting subbuffer %s", buf->name);
102 retval = -1;
103 }
104
105 /* FIMXE: free correctly the stuff */
106 end_rep:
107 if(rep_code)
108 free(rep_code);
109 end:
110 if(send_msg)
111 free(send_msg);
112 if(received_msg)
113 free(received_msg);
114
115 return retval;
116 }
117
118 int put_subbuffer(struct buffer_info *buf)
119 {
120 char *send_msg=NULL;
121 char *received_msg=NULL;
122 char *rep_code=NULL;
123 int retval;
124 int result;
125
126 asprintf(&send_msg, "put_subbuffer %s %ld", buf->name, buf->consumed_old);
127 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
128 if(result < 0 && errno == ECONNRESET) {
129 retval = PUT_SUBBUF_DIED;
130 goto end;
131 }
132 if(result < 0) {
133 ERR("put_subbuffer: send_message failed");
134 retval = -1;
135 goto end;
136 }
137
138 result = sscanf(received_msg, "%as", &rep_code);
139 if(result != 1) {
140 ERR("unable to parse response to put_subbuffer");
141 retval = -1;
142 goto end_rep;
143 }
144
145 if(!strcmp(rep_code, "OK")) {
146 DBG("subbuffer put %s", buf->name);
147 retval = PUT_SUBBUF_OK;
148 }
149 else {
150 DBG("put_subbuffer: received error, we were pushed");
151 retval = PUT_SUBBUF_PUSHED;
152 goto end_rep;
153 }
154
155 end_rep:
156 if(rep_code)
157 free(rep_code);
158
159 end:
160 if(send_msg)
161 free(send_msg);
162 if(received_msg)
163 free(received_msg);
164
165 return retval;
166 }
167
168 void decrement_active_buffers(void *arg)
169 {
170 pthread_mutex_lock(&active_buffers_mutex);
171 active_buffers--;
172 pthread_mutex_unlock(&active_buffers_mutex);
173 }
174
175 void *consumer_thread(void *arg)
176 {
177 struct buffer_info *buf = (struct buffer_info *) arg;
178 int result;
179
180 pthread_cleanup_push(decrement_active_buffers, NULL);
181
182 for(;;) {
183 /* get the subbuffer */
184 result = get_subbuffer(buf);
185 if(result == -1) {
186 ERR("error getting subbuffer");
187 continue;
188 }
189 else if(result == GET_SUBBUF_DONE) {
190 /* this is done */
191 break;
192 }
193 else if(result == GET_SUBBUF_DIED) {
194 finish_consuming_dead_subbuffer(buf);
195 break;
196 }
197
198 /* write data to file */
199 result = patient_write(buf->file_fd, buf->mem + (buf->consumed_old & (buf->n_subbufs * buf->subbuf_size-1)), buf->subbuf_size);
200 if(result == -1) {
201 PERROR("write");
202 /* FIXME: maybe drop this trace */
203 }
204
205 /* put the subbuffer */
206 result = put_subbuffer(buf);
207 if(result == -1) {
208 ERR("unknown error putting subbuffer (channel=%s)", buf->name);
209 break;
210 }
211 else if(result == PUT_SUBBUF_PUSHED) {
212 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf->name);
213 break;
214 }
215 else if(result == PUT_SUBBUF_DIED) {
216 WARN("application died while putting subbuffer");
217 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
218 finish_consuming_dead_subbuffer(buf);
219 break;
220 }
221 else if(result == PUT_SUBBUF_OK) {
222 }
223 }
224
225 DBG("thread for buffer %s is stopping", buf->name);
226
227 /* FIXME: destroy, unalloc... */
228
229 pthread_cleanup_pop(1);
230
231 return NULL;
232 }
233
234 int create_dir_if_needed(char *dir)
235 {
236 int result;
237 result = mkdir(dir, 0777);
238 if(result == -1) {
239 if(errno != EEXIST) {
240 PERROR("mkdir");
241 return -1;
242 }
243 }
244
245 return 0;
246 }
247
248 int is_directory(const char *dir)
249 {
250 int result;
251 struct stat st;
252
253 result = stat(dir, &st);
254 if(result == -1) {
255 PERROR("stat");
256 return 0;
257 }
258
259 if(!S_ISDIR(st.st_mode)) {
260 return 0;
261 }
262
263 return 1;
264 }
265
266 int add_buffer(pid_t pid, char *bufname)
267 {
268 struct buffer_info *buf;
269 char *send_msg;
270 char *received_msg;
271 int result;
272 char *tmp;
273 int fd;
274 pthread_t thr;
275 struct shmid_ds shmds;
276
277 buf = (struct buffer_info *) malloc(sizeof(struct buffer_info));
278 if(buf == NULL) {
279 ERR("add_buffer: insufficient memory");
280 return -1;
281 }
282
283 buf->name = bufname;
284 buf->pid = pid;
285
286 /* connect to app */
287 result = ustcomm_connect_app(buf->pid, &buf->conn);
288 if(result) {
289 WARN("unable to connect to process, it probably died before we were able to connect");
290 return -1;
291 }
292
293 /* get pidunique */
294 asprintf(&send_msg, "get_pidunique");
295 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
296 free(send_msg);
297 if(result == -1) {
298 ERR("problem in ustcomm_send_request(get_pidunique)");
299 return -1;
300 }
301
302 result = sscanf(received_msg, "%lld", &buf->pidunique);
303 if(result != 1) {
304 ERR("unable to parse response to get_pidunique");
305 return -1;
306 }
307 free(received_msg);
308 DBG("got pidunique %lld", buf->pidunique);
309
310 /* get shmid */
311 asprintf(&send_msg, "get_shmid %s", buf->name);
312 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
313 free(send_msg);
314 if(result == -1) {
315 ERR("problem in ustcomm_send_request(get_shmid)");
316 return -1;
317 }
318
319 result = sscanf(received_msg, "%d %d", &buf->shmid, &buf->bufstruct_shmid);
320 if(result != 2) {
321 ERR("unable to parse response to get_shmid");
322 return -1;
323 }
324 free(received_msg);
325 DBG("got shmids %d %d", buf->shmid, buf->bufstruct_shmid);
326
327 /* get n_subbufs */
328 asprintf(&send_msg, "get_n_subbufs %s", buf->name);
329 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
330 free(send_msg);
331 if(result == -1) {
332 ERR("problem in ustcomm_send_request(g_n_subbufs)");
333 return -1;
334 }
335
336 result = sscanf(received_msg, "%d", &buf->n_subbufs);
337 if(result != 1) {
338 ERR("unable to parse response to get_n_subbufs");
339 return -1;
340 }
341 free(received_msg);
342 DBG("got n_subbufs %d", buf->n_subbufs);
343
344 /* get subbuf size */
345 asprintf(&send_msg, "get_subbuf_size %s", buf->name);
346 ustcomm_send_request(&buf->conn, send_msg, &received_msg);
347 free(send_msg);
348
349 result = sscanf(received_msg, "%d", &buf->subbuf_size);
350 if(result != 1) {
351 ERR("unable to parse response to get_subbuf_size");
352 return -1;
353 }
354 free(received_msg);
355 DBG("got subbuf_size %d", buf->subbuf_size);
356
357 /* attach memory */
358 buf->mem = shmat(buf->shmid, NULL, 0);
359 if(buf->mem == (void *) 0) {
360 PERROR("shmat");
361 return -1;
362 }
363 DBG("successfully attached buffer memory");
364
365 buf->bufstruct_mem = shmat(buf->bufstruct_shmid, NULL, 0);
366 if(buf->bufstruct_mem == (void *) 0) {
367 PERROR("shmat");
368 return -1;
369 }
370 DBG("successfully attached buffer bufstruct memory");
371
372 /* obtain info on the memory segment */
373 result = shmctl(buf->shmid, IPC_STAT, &shmds);
374 if(result == -1) {
375 PERROR("shmctl");
376 return -1;
377 }
378 buf->memlen = shmds.shm_segsz;
379
380 /* open file for output */
381 if(!trace_path) {
382 /* Only create the directory if using the default path, because
383 * of the risk of typo when using trace path override. We don't
384 * want to risk creating plenty of useless directories in that case.
385 */
386 result = create_dir_if_needed(USTD_DEFAULT_TRACE_PATH);
387 if(result == -1) {
388 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH);
389 return -1;
390 }
391
392 trace_path = USTD_DEFAULT_TRACE_PATH;
393 }
394
395 asprintf(&tmp, "%s/%u_%lld", trace_path, buf->pid, buf->pidunique);
396 result = create_dir_if_needed(tmp);
397 if(result == -1) {
398 ERR("could not create directory %s", tmp);
399 free(tmp);
400 return -1;
401 }
402 free(tmp);
403
404 asprintf(&tmp, "%s/%u_%lld/%s_0", trace_path, buf->pid, buf->pidunique, buf->name);
405 result = fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 00600);
406 if(result == -1) {
407 PERROR("open");
408 ERR("failed opening trace file %s", tmp);
409 return -1;
410 }
411 buf->file_fd = fd;
412 free(tmp);
413
414 pthread_mutex_lock(&active_buffers_mutex);
415 active_buffers++;
416 pthread_mutex_unlock(&active_buffers_mutex);
417
418 pthread_create(&thr, NULL, consumer_thread, buf);
419
420 return 0;
421 }
422
423 void usage(void)
424 {
425 fprintf(stderr, "Usage:\nustd OPTIONS\n\nOptions:\n"
426 "\t-h\t\tDisplay this usage.\n"
427 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
428 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
429 }
430
431 int parse_args(int argc, char **argv)
432 {
433 int c;
434
435 while (1) {
436 int option_index = 0;
437 static struct option long_options[] = {
438 {"help", 0, 0, 'h'},
439 {"version", 0, 0, 'V'},
440 {0, 0, 0, 0}
441 };
442
443 c = getopt_long(argc, argv, "hs:o:", long_options, &option_index);
444 if (c == -1)
445 break;
446
447 switch (c) {
448 case 0:
449 printf("option %s", long_options[option_index].name);
450 if (optarg)
451 printf(" with arg %s", optarg);
452 printf("\n");
453 break;
454 case 's':
455 sock_path = optarg;
456 break;
457 case 'o':
458 trace_path = optarg;
459 if(!is_directory(trace_path)) {
460 ERR("Not a valid directory. (%s)", trace_path);
461 return -1;
462 }
463 break;
464 case 'h':
465 usage();
466 exit(0);
467 case 'V':
468 printf("Version 0.0\n");
469 break;
470
471 default:
472 /* unknown option or other error; error is
473 printed by getopt, just return */
474 return -1;
475 }
476 }
477
478 return 0;
479 }
480
481 void sigterm_handler(int sig)
482 {
483 terminate_req = 1;
484 }
485
486 int main(int argc, char **argv)
487 {
488 struct ustcomm_ustd ustd;
489 int result;
490 sigset_t sigset;
491 struct sigaction sa;
492
493 result = sigemptyset(&sigset);
494 if(result == -1) {
495 PERROR("sigemptyset");
496 return 1;
497 }
498 sa.sa_handler = sigterm_handler;
499 sa.sa_mask = sigset;
500 sa.sa_flags = SA_RESTART;
501 result = sigaction(SIGTERM, &sa, NULL);
502 if(result == -1) {
503 PERROR("sigaction");
504 return 1;
505 }
506
507 result = parse_args(argc, argv);
508 if(result == -1) {
509 exit(1);
510 }
511
512 result = ustcomm_init_ustd(&ustd, sock_path);
513 if(result == -1) {
514 ERR("failed to initialize socket");
515 return 1;
516 }
517
518 /* setup handler for SIGPIPE */
519 result = sigemptyset(&sigset);
520 if(result == -1) {
521 PERROR("sigemptyset");
522 return 1;
523 }
524 result = sigaddset(&sigset, SIGPIPE);
525 if(result == -1) {
526 PERROR("sigaddset");
527 return 1;
528 }
529 result = sigprocmask(SIG_BLOCK, &sigset, NULL);
530 if(result == -1) {
531 PERROR("sigprocmask");
532 return 1;
533 }
534
535 /* app loop */
536 for(;;) {
537 char *recvbuf;
538
539 /* check for requests on our public socket */
540 result = ustcomm_ustd_recv_message(&ustd, &recvbuf, NULL, 100);
541 if(result == -1) {
542 ERR("error in ustcomm_ustd_recv_message");
543 continue;
544 }
545 if(result > 0) {
546 if(!strncmp(recvbuf, "collect", 7)) {
547 pid_t pid;
548 char *bufname;
549 int result;
550
551 result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname);
552 if(result != 2) {
553 fprintf(stderr, "parsing error: %s\n", recvbuf);
554 }
555
556 result = add_buffer(pid, bufname);
557 if(result < 0) {
558 ERR("error in add_buffer");
559 continue;
560 }
561 }
562
563 free(recvbuf);
564 }
565
566 if(terminate_req) {
567 pthread_mutex_lock(&active_buffers_mutex);
568 if(active_buffers == 0) {
569 pthread_mutex_unlock(&active_buffers_mutex);
570 break;
571 }
572 pthread_mutex_unlock(&active_buffers_mutex);
573 }
574 }
575
576 return 0;
577 }
This page took 0.039829 seconds and 5 git commands to generate.