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