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