ustd: improve handling of remote app termination
[ust.git] / ustd / ustd.c
CommitLineData
c39c72ee 1/* Copyright (C) 2009 Pierre-Marc Fournier
1f8b0dff 2 *
c39c72ee
PMF
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.
1f8b0dff 7 *
c39c72ee 8 * This library is distributed in the hope that it will be useful,
1f8b0dff 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c39c72ee
PMF
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
1f8b0dff 12 *
c39c72ee
PMF
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
1f8b0dff
PMF
16 */
17
3796af9b
PMF
18#define _GNU_SOURCE
19
20#include <sys/types.h>
cd226f25 21#include <sys/stat.h>
3796af9b 22#include <sys/shm.h>
688760ef
PMF
23#include <fcntl.h>
24#include <unistd.h>
3a7b90de 25#include <pthread.h>
a3cdd4a7 26#include <signal.h>
3796af9b
PMF
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
a3cdd4a7
PMF
31#include <errno.h>
32#include <assert.h>
cd226f25 33#include <getopt.h>
3796af9b 34
0b0cd937 35#include "ustd.h"
3796af9b
PMF
36#include "localerr.h"
37#include "ustcomm.h"
8bf5ab2d 38#include "share.h"
3796af9b 39
3a7b90de
PMF
40/* return value: 0 = subbuffer is finished, it won't produce data anymore
41 * 1 = got subbuffer successfully
42 * <0 = error
43 */
3796af9b 44
8cefc145
PMF
45#define GET_SUBBUF_OK 1
46#define GET_SUBBUF_DONE 0
47#define GET_SUBBUF_DIED 2
48
a3cdd4a7
PMF
49#define PUT_SUBBUF_OK 1
50#define PUT_SUBBUF_DIED 0
51#define PUT_SUBBUF_PUSHED 2
52
c97d4437 53char *sock_path=NULL;
cd226f25
PMF
54char *trace_path=NULL;
55
3158b808
PMF
56/* Number of active buffers and the mutex to protect it. */
57int active_buffers = 0;
58pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER;
59/* Whether a request to end the program was received. */
60sig_atomic_t terminate_req = 0;
61
688760ef
PMF
62int get_subbuffer(struct buffer_info *buf)
63{
ab805ccd
PMF
64 char *send_msg=NULL;
65 char *received_msg=NULL;
66 char *rep_code=NULL;
688760ef
PMF
67 int retval;
68 int result;
69
70 asprintf(&send_msg, "get_subbuffer %s", buf->name);
3bb56863 71 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
409e2abe
PMF
72 if(result == -1 && errno == EPIPE || result == 0) {
73 DBG("app died while being traced");
74 retval = GET_SUBBUF_DIED;
ab805ccd 75 goto end;
a3cdd4a7
PMF
76 }
77 else if(result < 0) {
3bb56863 78 ERR("get_subbuffer: ustcomm_send_request failed");
ab805ccd
PMF
79 retval = -1;
80 goto end;
688760ef 81 }
688760ef
PMF
82
83 result = sscanf(received_msg, "%as %ld", &rep_code, &buf->consumed_old);
3a7b90de 84 if(result != 2 && result != 1) {
688760ef 85 ERR("unable to parse response to get_subbuffer");
ab805ccd
PMF
86 retval = -1;
87 goto end_rep;
688760ef 88 }
3a7b90de
PMF
89
90 DBG("received msg is %s", received_msg);
688760ef
PMF
91
92 if(!strcmp(rep_code, "OK")) {
93 DBG("got subbuffer %s", buf->name);
8cefc145 94 retval = GET_SUBBUF_OK;
688760ef 95 }
3a7b90de 96 else if(nth_token_is(received_msg, "END", 0) == 1) {
ab805ccd
PMF
97 retval = GET_SUBBUF_DONE;
98 goto end_rep;
3a7b90de 99 }
688760ef 100 else {
3a7b90de
PMF
101 DBG("error getting subbuffer %s", buf->name);
102 retval = -1;
688760ef
PMF
103 }
104
3a7b90de 105 /* FIMXE: free correctly the stuff */
ab805ccd
PMF
106end_rep:
107 if(rep_code)
108 free(rep_code);
109end:
110 if(send_msg)
111 free(send_msg);
112 if(received_msg)
113 free(received_msg);
114
688760ef
PMF
115 return retval;
116}
117
118int put_subbuffer(struct buffer_info *buf)
119{
ab805ccd
PMF
120 char *send_msg=NULL;
121 char *received_msg=NULL;
122 char *rep_code=NULL;
688760ef
PMF
123 int retval;
124 int result;
125
126 asprintf(&send_msg, "put_subbuffer %s %ld", buf->name, buf->consumed_old);
3bb56863 127 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
ab805ccd
PMF
128 if(result < 0 && errno == ECONNRESET) {
129 retval = PUT_SUBBUF_DIED;
130 goto end;
131 }
688760ef
PMF
132 if(result < 0) {
133 ERR("put_subbuffer: send_message failed");
ab805ccd
PMF
134 retval = -1;
135 goto end;
688760ef 136 }
688760ef
PMF
137
138 result = sscanf(received_msg, "%as", &rep_code);
139 if(result != 1) {
140 ERR("unable to parse response to put_subbuffer");
ab805ccd
PMF
141 retval = -1;
142 goto end_rep;
688760ef 143 }
688760ef
PMF
144
145 if(!strcmp(rep_code, "OK")) {
146 DBG("subbuffer put %s", buf->name);
a3cdd4a7 147 retval = PUT_SUBBUF_OK;
688760ef
PMF
148 }
149 else {
a3cdd4a7 150 DBG("put_subbuffer: received error, we were pushed");
ab805ccd
PMF
151 retval = PUT_SUBBUF_PUSHED;
152 goto end_rep;
688760ef
PMF
153 }
154
ab805ccd
PMF
155end_rep:
156 if(rep_code)
157 free(rep_code);
158
159end:
160 if(send_msg)
161 free(send_msg);
162 if(received_msg)
163 free(received_msg);
164
688760ef
PMF
165 return retval;
166}
167
3158b808
PMF
168void 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
3a7b90de
PMF
175void *consumer_thread(void *arg)
176{
177 struct buffer_info *buf = (struct buffer_info *) arg;
178 int result;
179
3158b808
PMF
180 pthread_cleanup_push(decrement_active_buffers, NULL);
181
3a7b90de 182 for(;;) {
8cefc145 183 /* get the subbuffer */
0b0cd937
PMF
184 result = get_subbuffer(buf);
185 if(result == -1) {
186 ERR("error getting subbuffer");
187 continue;
3a7b90de 188 }
0b0cd937
PMF
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;
3a7b90de
PMF
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
8cefc145 205 /* put the subbuffer */
0b0cd937
PMF
206 result = put_subbuffer(buf);
207 if(result == -1) {
a3cdd4a7
PMF
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);
0b0cd937 213 break;
3a7b90de 214 }
a3cdd4a7
PMF
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);
9f654956 219 break;
a3cdd4a7
PMF
220 }
221 else if(result == PUT_SUBBUF_OK) {
222 }
3a7b90de
PMF
223 }
224
225 DBG("thread for buffer %s is stopping", buf->name);
226
8cefc145
PMF
227 /* FIXME: destroy, unalloc... */
228
3158b808
PMF
229 pthread_cleanup_pop(1);
230
3a7b90de
PMF
231 return NULL;
232}
233
72ebd39a
PMF
234int create_dir_if_needed(char *dir)
235{
236 int result;
237 result = mkdir(dir, 0777);
238 if(result == -1) {
239 if(errno != EEXIST) {
4d70f833 240 PERROR("mkdir");
72ebd39a
PMF
241 return -1;
242 }
243 }
244
245 return 0;
246}
247
cd226f25
PMF
248int 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
3a7b90de
PMF
266int 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;
a3cdd4a7 275 struct shmid_ds shmds;
3a7b90de
PMF
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
4e2a8808
PMF
286 /* connect to app */
287 result = ustcomm_connect_app(buf->pid, &buf->conn);
288 if(result) {
a3cdd4a7 289 WARN("unable to connect to process, it probably died before we were able to connect");
4e2a8808
PMF
290 return -1;
291 }
292
ed1317e7
PMF
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
3a7b90de
PMF
310 /* get shmid */
311 asprintf(&send_msg, "get_shmid %s", buf->name);
a3cdd4a7 312 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
3a7b90de 313 free(send_msg);
a3cdd4a7
PMF
314 if(result == -1) {
315 ERR("problem in ustcomm_send_request(get_shmid)");
316 return -1;
317 }
3a7b90de 318
8cefc145
PMF
319 result = sscanf(received_msg, "%d %d", &buf->shmid, &buf->bufstruct_shmid);
320 if(result != 2) {
3a7b90de
PMF
321 ERR("unable to parse response to get_shmid");
322 return -1;
323 }
324 free(received_msg);
8cefc145 325 DBG("got shmids %d %d", buf->shmid, buf->bufstruct_shmid);
3a7b90de
PMF
326
327 /* get n_subbufs */
328 asprintf(&send_msg, "get_n_subbufs %s", buf->name);
a3cdd4a7 329 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
3a7b90de 330 free(send_msg);
a3cdd4a7
PMF
331 if(result == -1) {
332 ERR("problem in ustcomm_send_request(g_n_subbufs)");
333 return -1;
334 }
3a7b90de
PMF
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);
4e2a8808 346 ustcomm_send_request(&buf->conn, send_msg, &received_msg);
3a7b90de
PMF
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) {
4d70f833 360 PERROR("shmat");
3a7b90de
PMF
361 return -1;
362 }
8cefc145
PMF
363 DBG("successfully attached buffer memory");
364
365 buf->bufstruct_mem = shmat(buf->bufstruct_shmid, NULL, 0);
366 if(buf->bufstruct_mem == (void *) 0) {
4d70f833 367 PERROR("shmat");
8cefc145
PMF
368 return -1;
369 }
370 DBG("successfully attached buffer bufstruct memory");
3a7b90de 371
a3cdd4a7
PMF
372 /* obtain info on the memory segment */
373 result = shmctl(buf->shmid, IPC_STAT, &shmds);
374 if(result == -1) {
4d70f833 375 PERROR("shmctl");
a3cdd4a7
PMF
376 return -1;
377 }
378 buf->memlen = shmds.shm_segsz;
379
3a7b90de 380 /* open file for output */
cd226f25
PMF
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;
72ebd39a
PMF
393 }
394
ed1317e7 395 asprintf(&tmp, "%s/%u_%lld", trace_path, buf->pid, buf->pidunique);
72ebd39a
PMF
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
ed1317e7
PMF
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);
3a7b90de
PMF
406 if(result == -1) {
407 PERROR("open");
6cb88bc0 408 ERR("failed opening trace file %s", tmp);
3a7b90de
PMF
409 return -1;
410 }
411 buf->file_fd = fd;
412 free(tmp);
413
3158b808
PMF
414 pthread_mutex_lock(&active_buffers_mutex);
415 active_buffers++;
416 pthread_mutex_unlock(&active_buffers_mutex);
417
3a7b90de
PMF
418 pthread_create(&thr, NULL, consumer_thread, buf);
419
420 return 0;
421}
422
cd226f25
PMF
423void 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
431int 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
3158b808
PMF
481void sigterm_handler(int sig)
482{
483 terminate_req = 1;
484}
485
3796af9b
PMF
486int main(int argc, char **argv)
487{
488 struct ustcomm_ustd ustd;
489 int result;
a3cdd4a7 490 sigset_t sigset;
3158b808
PMF
491 struct sigaction sa;
492
493 result = sigemptyset(&sigset);
494 if(result == -1) {
4d70f833 495 PERROR("sigemptyset");
3158b808
PMF
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 }
3796af9b 506
cd226f25
PMF
507 result = parse_args(argc, argv);
508 if(result == -1) {
509 exit(1);
510 }
511
512 result = ustcomm_init_ustd(&ustd, sock_path);
3796af9b
PMF
513 if(result == -1) {
514 ERR("failed to initialize socket");
515 return 1;
516 }
517
3158b808 518 /* setup handler for SIGPIPE */
a3cdd4a7
PMF
519 result = sigemptyset(&sigset);
520 if(result == -1) {
4d70f833 521 PERROR("sigemptyset");
a3cdd4a7
PMF
522 return 1;
523 }
524 result = sigaddset(&sigset, SIGPIPE);
525 if(result == -1) {
4d70f833 526 PERROR("sigaddset");
a3cdd4a7
PMF
527 return 1;
528 }
529 result = sigprocmask(SIG_BLOCK, &sigset, NULL);
530 if(result == -1) {
4d70f833 531 PERROR("sigprocmask");
a3cdd4a7
PMF
532 return 1;
533 }
534
688760ef 535 /* app loop */
3796af9b
PMF
536 for(;;) {
537 char *recvbuf;
538
3a7b90de 539 /* check for requests on our public socket */
688760ef
PMF
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;
3796af9b 550
688760ef
PMF
551 result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname);
552 if(result != 2) {
553 fprintf(stderr, "parsing error: %s\n", recvbuf);
554 }
3796af9b 555
688760ef
PMF
556 result = add_buffer(pid, bufname);
557 if(result < 0) {
558 ERR("error in add_buffer");
559 continue;
560 }
3796af9b
PMF
561 }
562
688760ef 563 free(recvbuf);
3796af9b 564 }
3158b808
PMF
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 }
3796af9b
PMF
574 }
575
576 return 0;
577}
This page took 0.049543 seconds and 4 git commands to generate.