libustcomm: add buffering mechanism to receive function
[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"
6af64c43 36#include "usterr.h"
3796af9b
PMF
37#include "ustcomm.h"
38
3a7b90de
PMF
39/* return value: 0 = subbuffer is finished, it won't produce data anymore
40 * 1 = got subbuffer successfully
41 * <0 = error
42 */
3796af9b 43
8cefc145
PMF
44#define GET_SUBBUF_OK 1
45#define GET_SUBBUF_DONE 0
46#define GET_SUBBUF_DIED 2
47
a3cdd4a7
PMF
48#define PUT_SUBBUF_OK 1
49#define PUT_SUBBUF_DIED 0
50#define PUT_SUBBUF_PUSHED 2
c970a26f 51#define PUT_SUBBUF_DONE 3
a3cdd4a7 52
c97d4437 53char *sock_path=NULL;
cd226f25 54char *trace_path=NULL;
bce2937a 55int daemon_mode = 0;
2730a7d6 56char *pidfile = NULL;
cd226f25 57
3158b808
PMF
58/* Number of active buffers and the mutex to protect it. */
59int active_buffers = 0;
60pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER;
61/* Whether a request to end the program was received. */
62sig_atomic_t terminate_req = 0;
63
688760ef
PMF
64int get_subbuffer(struct buffer_info *buf)
65{
ab805ccd
PMF
66 char *send_msg=NULL;
67 char *received_msg=NULL;
68 char *rep_code=NULL;
688760ef
PMF
69 int retval;
70 int result;
71
72 asprintf(&send_msg, "get_subbuffer %s", buf->name);
3bb56863 73 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
dc2f5ffa 74 if((result == -1 && errno == EPIPE) || result == 0) {
409e2abe
PMF
75 DBG("app died while being traced");
76 retval = GET_SUBBUF_DIED;
ab805ccd 77 goto end;
a3cdd4a7
PMF
78 }
79 else if(result < 0) {
3bb56863 80 ERR("get_subbuffer: ustcomm_send_request failed");
ab805ccd
PMF
81 retval = -1;
82 goto end;
688760ef 83 }
688760ef
PMF
84
85 result = sscanf(received_msg, "%as %ld", &rep_code, &buf->consumed_old);
3a7b90de 86 if(result != 2 && result != 1) {
688760ef 87 ERR("unable to parse response to get_subbuffer");
ab805ccd
PMF
88 retval = -1;
89 goto end_rep;
688760ef 90 }
3a7b90de
PMF
91
92 DBG("received msg is %s", received_msg);
688760ef
PMF
93
94 if(!strcmp(rep_code, "OK")) {
95 DBG("got subbuffer %s", buf->name);
8cefc145 96 retval = GET_SUBBUF_OK;
688760ef 97 }
3a7b90de 98 else if(nth_token_is(received_msg, "END", 0) == 1) {
ab805ccd
PMF
99 retval = GET_SUBBUF_DONE;
100 goto end_rep;
3a7b90de 101 }
c970a26f
PMF
102 else if(!strcmp(received_msg, "NOTFOUND")) {
103 WARN("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf->name);
104 retval = GET_SUBBUF_DONE;
105 goto end_rep;
106 }
688760ef 107 else {
3a7b90de
PMF
108 DBG("error getting subbuffer %s", buf->name);
109 retval = -1;
688760ef
PMF
110 }
111
3a7b90de 112 /* FIMXE: free correctly the stuff */
ab805ccd
PMF
113end_rep:
114 if(rep_code)
115 free(rep_code);
116end:
117 if(send_msg)
118 free(send_msg);
119 if(received_msg)
120 free(received_msg);
121
688760ef
PMF
122 return retval;
123}
124
125int put_subbuffer(struct buffer_info *buf)
126{
ab805ccd
PMF
127 char *send_msg=NULL;
128 char *received_msg=NULL;
129 char *rep_code=NULL;
688760ef
PMF
130 int retval;
131 int result;
132
133 asprintf(&send_msg, "put_subbuffer %s %ld", buf->name, buf->consumed_old);
3bb56863 134 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
ab805ccd
PMF
135 if(result < 0 && errno == ECONNRESET) {
136 retval = PUT_SUBBUF_DIED;
137 goto end;
138 }
c970a26f 139 else if(result < 0) {
688760ef 140 ERR("put_subbuffer: send_message failed");
ab805ccd
PMF
141 retval = -1;
142 goto end;
688760ef 143 }
c970a26f
PMF
144 else if(result == 0) {
145 /* Program seems finished. However this might not be
146 * the last subbuffer that has to be collected.
147 */
148 retval = PUT_SUBBUF_DIED;
149 goto end;
150 }
688760ef
PMF
151
152 result = sscanf(received_msg, "%as", &rep_code);
153 if(result != 1) {
154 ERR("unable to parse response to put_subbuffer");
ab805ccd
PMF
155 retval = -1;
156 goto end_rep;
688760ef 157 }
688760ef
PMF
158
159 if(!strcmp(rep_code, "OK")) {
160 DBG("subbuffer put %s", buf->name);
a3cdd4a7 161 retval = PUT_SUBBUF_OK;
688760ef 162 }
2ddb81a8
PMF
163 else if(!strcmp(received_msg, "NOTFOUND")) {
164 WARN("For buffer %s, the trace was not found. This likely means it was destroyed by the user.", buf->name);
165 /* However, maybe this was not the last subbuffer. So
166 * we return the program died.
167 */
168 retval = PUT_SUBBUF_DIED;
169 goto end_rep;
170 }
688760ef 171 else {
a3cdd4a7 172 DBG("put_subbuffer: received error, we were pushed");
ab805ccd
PMF
173 retval = PUT_SUBBUF_PUSHED;
174 goto end_rep;
688760ef
PMF
175 }
176
ab805ccd
PMF
177end_rep:
178 if(rep_code)
179 free(rep_code);
180
181end:
182 if(send_msg)
183 free(send_msg);
184 if(received_msg)
185 free(received_msg);
186
688760ef
PMF
187 return retval;
188}
189
3158b808
PMF
190void decrement_active_buffers(void *arg)
191{
192 pthread_mutex_lock(&active_buffers_mutex);
193 active_buffers--;
194 pthread_mutex_unlock(&active_buffers_mutex);
195}
196
72ebd39a
PMF
197int create_dir_if_needed(char *dir)
198{
199 int result;
200 result = mkdir(dir, 0777);
201 if(result == -1) {
202 if(errno != EEXIST) {
4d70f833 203 PERROR("mkdir");
72ebd39a
PMF
204 return -1;
205 }
206 }
207
208 return 0;
209}
210
cd226f25
PMF
211int is_directory(const char *dir)
212{
213 int result;
214 struct stat st;
215
216 result = stat(dir, &st);
217 if(result == -1) {
218 PERROR("stat");
219 return 0;
220 }
221
222 if(!S_ISDIR(st.st_mode)) {
223 return 0;
224 }
225
226 return 1;
227}
228
f99c0b5c 229struct buffer_info *connect_buffer(pid_t pid, const char *bufname)
3a7b90de
PMF
230{
231 struct buffer_info *buf;
232 char *send_msg;
233 char *received_msg;
234 int result;
235 char *tmp;
236 int fd;
a3cdd4a7 237 struct shmid_ds shmds;
3a7b90de
PMF
238
239 buf = (struct buffer_info *) malloc(sizeof(struct buffer_info));
240 if(buf == NULL) {
241 ERR("add_buffer: insufficient memory");
f99c0b5c 242 return NULL;
3a7b90de
PMF
243 }
244
245 buf->name = bufname;
246 buf->pid = pid;
247
4e2a8808
PMF
248 /* connect to app */
249 result = ustcomm_connect_app(buf->pid, &buf->conn);
250 if(result) {
a3cdd4a7 251 WARN("unable to connect to process, it probably died before we were able to connect");
f99c0b5c 252 return NULL;
4e2a8808
PMF
253 }
254
ed1317e7
PMF
255 /* get pidunique */
256 asprintf(&send_msg, "get_pidunique");
257 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
258 free(send_msg);
259 if(result == -1) {
260 ERR("problem in ustcomm_send_request(get_pidunique)");
f99c0b5c 261 return NULL;
ed1317e7
PMF
262 }
263
264 result = sscanf(received_msg, "%lld", &buf->pidunique);
265 if(result != 1) {
266 ERR("unable to parse response to get_pidunique");
f99c0b5c 267 return NULL;
ed1317e7
PMF
268 }
269 free(received_msg);
270 DBG("got pidunique %lld", buf->pidunique);
271
3a7b90de
PMF
272 /* get shmid */
273 asprintf(&send_msg, "get_shmid %s", buf->name);
a3cdd4a7 274 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
3a7b90de 275 free(send_msg);
a3cdd4a7
PMF
276 if(result == -1) {
277 ERR("problem in ustcomm_send_request(get_shmid)");
f99c0b5c 278 return NULL;
a3cdd4a7 279 }
3a7b90de 280
8cefc145
PMF
281 result = sscanf(received_msg, "%d %d", &buf->shmid, &buf->bufstruct_shmid);
282 if(result != 2) {
204141ee 283 ERR("unable to parse response to get_shmid (\"%s\")", received_msg);
f99c0b5c 284 return NULL;
3a7b90de
PMF
285 }
286 free(received_msg);
8cefc145 287 DBG("got shmids %d %d", buf->shmid, buf->bufstruct_shmid);
3a7b90de
PMF
288
289 /* get n_subbufs */
290 asprintf(&send_msg, "get_n_subbufs %s", buf->name);
a3cdd4a7 291 result = ustcomm_send_request(&buf->conn, send_msg, &received_msg);
3a7b90de 292 free(send_msg);
a3cdd4a7
PMF
293 if(result == -1) {
294 ERR("problem in ustcomm_send_request(g_n_subbufs)");
f99c0b5c 295 return NULL;
a3cdd4a7 296 }
3a7b90de
PMF
297
298 result = sscanf(received_msg, "%d", &buf->n_subbufs);
299 if(result != 1) {
300 ERR("unable to parse response to get_n_subbufs");
f99c0b5c 301 return NULL;
3a7b90de
PMF
302 }
303 free(received_msg);
304 DBG("got n_subbufs %d", buf->n_subbufs);
305
306 /* get subbuf size */
307 asprintf(&send_msg, "get_subbuf_size %s", buf->name);
4e2a8808 308 ustcomm_send_request(&buf->conn, send_msg, &received_msg);
3a7b90de
PMF
309 free(send_msg);
310
311 result = sscanf(received_msg, "%d", &buf->subbuf_size);
312 if(result != 1) {
313 ERR("unable to parse response to get_subbuf_size");
f99c0b5c 314 return NULL;
3a7b90de
PMF
315 }
316 free(received_msg);
317 DBG("got subbuf_size %d", buf->subbuf_size);
318
319 /* attach memory */
320 buf->mem = shmat(buf->shmid, NULL, 0);
321 if(buf->mem == (void *) 0) {
4d70f833 322 PERROR("shmat");
f99c0b5c 323 return NULL;
3a7b90de 324 }
8cefc145
PMF
325 DBG("successfully attached buffer memory");
326
327 buf->bufstruct_mem = shmat(buf->bufstruct_shmid, NULL, 0);
328 if(buf->bufstruct_mem == (void *) 0) {
4d70f833 329 PERROR("shmat");
f99c0b5c 330 return NULL;
8cefc145
PMF
331 }
332 DBG("successfully attached buffer bufstruct memory");
3a7b90de 333
a3cdd4a7
PMF
334 /* obtain info on the memory segment */
335 result = shmctl(buf->shmid, IPC_STAT, &shmds);
336 if(result == -1) {
4d70f833 337 PERROR("shmctl");
f99c0b5c 338 return NULL;
a3cdd4a7
PMF
339 }
340 buf->memlen = shmds.shm_segsz;
341
3a7b90de 342 /* open file for output */
cd226f25
PMF
343 if(!trace_path) {
344 /* Only create the directory if using the default path, because
345 * of the risk of typo when using trace path override. We don't
346 * want to risk creating plenty of useless directories in that case.
347 */
348 result = create_dir_if_needed(USTD_DEFAULT_TRACE_PATH);
349 if(result == -1) {
350 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH);
f99c0b5c 351 return NULL;
cd226f25
PMF
352 }
353
354 trace_path = USTD_DEFAULT_TRACE_PATH;
72ebd39a
PMF
355 }
356
ed1317e7 357 asprintf(&tmp, "%s/%u_%lld", trace_path, buf->pid, buf->pidunique);
72ebd39a
PMF
358 result = create_dir_if_needed(tmp);
359 if(result == -1) {
360 ERR("could not create directory %s", tmp);
361 free(tmp);
f99c0b5c 362 return NULL;
72ebd39a
PMF
363 }
364 free(tmp);
365
204141ee 366 asprintf(&tmp, "%s/%u_%lld/%s", trace_path, buf->pid, buf->pidunique, buf->name);
ed1317e7 367 result = fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 00600);
3a7b90de
PMF
368 if(result == -1) {
369 PERROR("open");
6cb88bc0 370 ERR("failed opening trace file %s", tmp);
f99c0b5c 371 return NULL;
3a7b90de
PMF
372 }
373 buf->file_fd = fd;
374 free(tmp);
375
3158b808
PMF
376 pthread_mutex_lock(&active_buffers_mutex);
377 active_buffers++;
378 pthread_mutex_unlock(&active_buffers_mutex);
379
f99c0b5c
PMF
380 return buf;
381}
382
02af3e60
PMF
383int write_current_subbuffer(struct buffer_info *buf)
384{
385 int result;
386
387 void *subbuf_mem = buf->mem + (buf->consumed_old & (buf->n_subbufs * buf->subbuf_size-1));
388
389 size_t cur_sb_size = subbuffer_data_size(subbuf_mem);
390
391 result = patient_write(buf->file_fd, subbuf_mem, cur_sb_size);
392 if(result == -1) {
393 PERROR("write");
394 /* FIXME: maybe drop this trace */
395 return 0;
396 }
397
398 return 0;
399}
400
f99c0b5c
PMF
401int consumer_loop(struct buffer_info *buf)
402{
403 int result;
404
405 pthread_cleanup_push(decrement_active_buffers, NULL);
406
407 for(;;) {
408 /* get the subbuffer */
409 result = get_subbuffer(buf);
410 if(result == -1) {
411 ERR("error getting subbuffer");
412 continue;
413 }
414 else if(result == GET_SUBBUF_DONE) {
415 /* this is done */
416 break;
417 }
418 else if(result == GET_SUBBUF_DIED) {
419 finish_consuming_dead_subbuffer(buf);
420 break;
421 }
422
423 /* write data to file */
02af3e60
PMF
424 write_current_subbuffer(buf);
425 /* FIXME: handle return value? */
f99c0b5c
PMF
426
427 /* put the subbuffer */
428 /* FIXME: we actually should unput the buffer before consuming... */
429 result = put_subbuffer(buf);
430 if(result == -1) {
431 ERR("unknown error putting subbuffer (channel=%s)", buf->name);
432 break;
433 }
434 else if(result == PUT_SUBBUF_PUSHED) {
435 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf->name);
436 break;
437 }
438 else if(result == PUT_SUBBUF_DIED) {
439 WARN("application died while putting subbuffer");
440 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
441 finish_consuming_dead_subbuffer(buf);
442 break;
443 }
c970a26f
PMF
444 else if(result == PUT_SUBBUF_DONE) {
445 /* Done with this subbuffer */
446 /* FIXME: add a case where this branch is used? Upon
447 * normal trace termination, at put_subbuf time, a
448 * special last-subbuffer code could be returned by
449 * the listener.
450 */
451 break;
452 }
f99c0b5c
PMF
453 else if(result == PUT_SUBBUF_OK) {
454 }
455 }
456
457 DBG("thread for buffer %s is stopping", buf->name);
458
459 /* FIXME: destroy, unalloc... */
460
461 pthread_cleanup_pop(1);
462
463 return 0;
464}
465
466void free_buffer(struct buffer_info *buf)
467{
468}
469
470struct consumer_thread_args {
471 pid_t pid;
472 const char *bufname;
473};
474
475void *consumer_thread(void *arg)
476{
477 struct buffer_info *buf = (struct buffer_info *) arg;
478 struct consumer_thread_args *args = (struct consumer_thread_args *) arg;
479
480 DBG("GOT ARGS: pid %d bufname %s", args->pid, args->bufname);
481
482 buf = connect_buffer(args->pid, args->bufname);
483 if(buf == NULL) {
484 ERR("failed to connect to buffer");
485 goto end;
486 }
487
488 consumer_loop(buf);
489
490 free_buffer(buf);
491
492 end:
493 /* bufname is free'd in free_buffer() */
494 free(args);
495 return NULL;
496}
497
498int start_consuming_buffer(pid_t pid, const char *bufname)
499{
500 pthread_t thr;
501 struct consumer_thread_args *args;
502
503 DBG("beginning of start_consuming_buffer: args: pid %d bufname %s", pid, bufname);
504
505 args = (struct consumer_thread_args *) malloc(sizeof(struct consumer_thread_args));
506
507 args->pid = pid;
508 args->bufname = strdup(bufname);
509 DBG("beginning2 of start_consuming_buffer: args: pid %d bufname %s", args->pid, args->bufname);
510
511 pthread_create(&thr, NULL, consumer_thread, args);
512 DBG("end of start_consuming_buffer: args: pid %d bufname %s", args->pid, args->bufname);
3a7b90de
PMF
513
514 return 0;
515}
516
cd226f25
PMF
517void usage(void)
518{
519 fprintf(stderr, "Usage:\nustd OPTIONS\n\nOptions:\n"
520 "\t-h\t\tDisplay this usage.\n"
521 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
bce2937a
PMF
522 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n"
523 "\t-d\t\tStart as a daemon.\n"
524 "\t--pidfile FILE\tWrite the PID in this file (when using -d).\n");
cd226f25
PMF
525}
526
527int parse_args(int argc, char **argv)
528{
529 int c;
530
531 while (1) {
532 int option_index = 0;
533 static struct option long_options[] = {
bce2937a 534 {"pidfile", 1, 0, 'p'},
cd226f25
PMF
535 {"help", 0, 0, 'h'},
536 {"version", 0, 0, 'V'},
537 {0, 0, 0, 0}
538 };
539
bce2937a 540 c = getopt_long(argc, argv, "hs:o:d", long_options, &option_index);
cd226f25
PMF
541 if (c == -1)
542 break;
543
544 switch (c) {
545 case 0:
546 printf("option %s", long_options[option_index].name);
547 if (optarg)
548 printf(" with arg %s", optarg);
549 printf("\n");
550 break;
551 case 's':
552 sock_path = optarg;
553 break;
554 case 'o':
555 trace_path = optarg;
556 if(!is_directory(trace_path)) {
557 ERR("Not a valid directory. (%s)", trace_path);
558 return -1;
559 }
560 break;
bce2937a
PMF
561 case 'd':
562 daemon_mode = 1;
563 break;
2730a7d6
PMF
564 case 'p':
565 pidfile = strdup(optarg);
566 break;
cd226f25
PMF
567 case 'h':
568 usage();
569 exit(0);
570 case 'V':
571 printf("Version 0.0\n");
572 break;
573
574 default:
575 /* unknown option or other error; error is
576 printed by getopt, just return */
577 return -1;
578 }
579 }
580
581 return 0;
582}
583
3158b808
PMF
584void sigterm_handler(int sig)
585{
586 terminate_req = 1;
587}
588
2b3c64a4
PMF
589static int write_pidfile(const char *file_name, pid_t pid)
590{
591 FILE *pidfp;
592
5d72e651 593 pidfp = fopen(file_name, "w");
2b3c64a4
PMF
594 if(!pidfp) {
595 PERROR("fopen (%s)", pidfile);
596 WARN("killing child process");
597 return -1;
598 }
599
600 fprintf(pidfp, "%d\n", pid);
601
602 fclose(pidfp);
603
604 return 0;
605}
606
bce2937a 607int start_ustd(int fd)
3796af9b
PMF
608{
609 struct ustcomm_ustd ustd;
610 int result;
a3cdd4a7 611 sigset_t sigset;
3158b808
PMF
612 struct sigaction sa;
613
614 result = sigemptyset(&sigset);
615 if(result == -1) {
4d70f833 616 PERROR("sigemptyset");
3158b808
PMF
617 return 1;
618 }
619 sa.sa_handler = sigterm_handler;
620 sa.sa_mask = sigset;
621 sa.sa_flags = SA_RESTART;
622 result = sigaction(SIGTERM, &sa, NULL);
623 if(result == -1) {
624 PERROR("sigaction");
625 return 1;
626 }
3796af9b 627
cd226f25 628 result = ustcomm_init_ustd(&ustd, sock_path);
3796af9b
PMF
629 if(result == -1) {
630 ERR("failed to initialize socket");
631 return 1;
632 }
633
3158b808 634 /* setup handler for SIGPIPE */
a3cdd4a7
PMF
635 result = sigemptyset(&sigset);
636 if(result == -1) {
4d70f833 637 PERROR("sigemptyset");
a3cdd4a7
PMF
638 return 1;
639 }
640 result = sigaddset(&sigset, SIGPIPE);
641 if(result == -1) {
4d70f833 642 PERROR("sigaddset");
a3cdd4a7
PMF
643 return 1;
644 }
645 result = sigprocmask(SIG_BLOCK, &sigset, NULL);
646 if(result == -1) {
4d70f833 647 PERROR("sigprocmask");
a3cdd4a7
PMF
648 return 1;
649 }
650
2b3c64a4
PMF
651 /* Write pidfile */
652 if(pidfile) {
653 result = write_pidfile(pidfile, getpid());
654 if(result == -1) {
655 ERR("failed to write pidfile");
656 return 1;
657 }
658 }
659
bce2937a
PMF
660 /* Notify parent that we are successfully started. */
661 if(fd != -1) {
662 /* write any one character */
663 result = write(fd, "!", 1);
664 if(result == -1) {
665 PERROR("write");
666 return -1;
667 }
668 if(result != 1) {
669 ERR("Problem sending confirmation of daemon start to parent");
670 return -1;
671 }
672 result = close(fd);
673 if(result == -1) {
674 PERROR("close");
675 }
676 }
677
688760ef 678 /* app loop */
3796af9b
PMF
679 for(;;) {
680 char *recvbuf;
681
3a7b90de 682 /* check for requests on our public socket */
688760ef
PMF
683 result = ustcomm_ustd_recv_message(&ustd, &recvbuf, NULL, 100);
684 if(result == -1) {
685 ERR("error in ustcomm_ustd_recv_message");
f99c0b5c 686 goto loop_end;
688760ef
PMF
687 }
688 if(result > 0) {
689 if(!strncmp(recvbuf, "collect", 7)) {
690 pid_t pid;
691 char *bufname;
692 int result;
3796af9b 693
688760ef
PMF
694 result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname);
695 if(result != 2) {
f99c0b5c
PMF
696 ERR("parsing error: %s", recvbuf);
697 goto free_bufname;
688760ef 698 }
3796af9b 699
f99c0b5c 700 result = start_consuming_buffer(pid, bufname);
688760ef
PMF
701 if(result < 0) {
702 ERR("error in add_buffer");
f99c0b5c 703 goto free_bufname;
688760ef 704 }
f99c0b5c
PMF
705
706 free_bufname:
707 free(bufname);
3796af9b
PMF
708 }
709
688760ef 710 free(recvbuf);
3796af9b 711 }
3158b808 712
f99c0b5c
PMF
713 loop_end:
714
3158b808
PMF
715 if(terminate_req) {
716 pthread_mutex_lock(&active_buffers_mutex);
717 if(active_buffers == 0) {
718 pthread_mutex_unlock(&active_buffers_mutex);
719 break;
720 }
721 pthread_mutex_unlock(&active_buffers_mutex);
722 }
3796af9b
PMF
723 }
724
725 return 0;
726}
bce2937a
PMF
727
728int start_ustd_daemon()
729{
730 int result;
731 int fd[2];
2730a7d6 732 pid_t child_pid;
bce2937a
PMF
733
734 result = pipe(fd);
735
2730a7d6 736 result = child_pid = fork();
bce2937a
PMF
737 if(result == -1) {
738 PERROR("fork");
739 return -1;
740 }
741 else if(result == 0) {
742 return start_ustd(fd[1]);
743 }
744 else {
745 char buf;
2730a7d6 746
bce2937a
PMF
747 result = read(fd[0], &buf, 1);
748 if(result == -1) {
749 PERROR("read");
750 return -1;
751 }
752 if(result != 1) {
753 ERR("did not receive valid confirmation that the daemon is started");
754 return -1;
755 }
756
757 result = close(fd[0]);
758 if(result == -1) {
759 PERROR("close");
760 }
761
762 DBG("The daemon is now successfully started");
763 }
764
765 /* Wait for confirmation that the server is ready. */
766
767
768 return 0;
769}
770
771int main(int argc, char **argv)
772{
773 int result;
774
775 result = parse_args(argc, argv);
776 if(result == -1) {
777 exit(1);
778 }
779
780 if(daemon_mode) {
781 result = start_ustd_daemon();
782 }
783 else {
784 result = start_ustd(-1);
785 }
786
787 return result;
788}
This page took 0.060477 seconds and 4 git commands to generate.