Commit | Line | Data |
---|---|---|
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 | 53 | char *sock_path=NULL; |
cd226f25 | 54 | char *trace_path=NULL; |
bce2937a | 55 | int daemon_mode = 0; |
2730a7d6 | 56 | char *pidfile = NULL; |
cd226f25 | 57 | |
3158b808 PMF |
58 | /* Number of active buffers and the mutex to protect it. */ |
59 | int active_buffers = 0; | |
60 | pthread_mutex_t active_buffers_mutex = PTHREAD_MUTEX_INITIALIZER; | |
61 | /* Whether a request to end the program was received. */ | |
62 | sig_atomic_t terminate_req = 0; | |
63 | ||
688760ef PMF |
64 | int 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 |
108 | end_rep: |
109 | if(rep_code) | |
110 | free(rep_code); | |
111 | end: | |
112 | if(send_msg) | |
113 | free(send_msg); | |
114 | if(received_msg) | |
115 | free(received_msg); | |
116 | ||
688760ef PMF |
117 | return retval; |
118 | } | |
119 | ||
120 | int 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 |
157 | end_rep: |
158 | if(rep_code) | |
159 | free(rep_code); | |
160 | ||
161 | end: | |
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 |
170 | void 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 |
177 | int 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 |
191 | int 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 | 209 | struct 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 | ||
363 | int 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 | ||
422 | void free_buffer(struct buffer_info *buf) | |
423 | { | |
424 | } | |
425 | ||
426 | struct consumer_thread_args { | |
427 | pid_t pid; | |
428 | const char *bufname; | |
429 | }; | |
430 | ||
431 | void *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 | ||
454 | int 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 |
473 | void 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 | ||
483 | int 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 |
540 | void sigterm_handler(int sig) |
541 | { | |
542 | terminate_req = 1; | |
543 | } | |
544 | ||
2b3c64a4 PMF |
545 | static int write_pidfile(const char *file_name, pid_t pid) |
546 | { | |
547 | FILE *pidfp; | |
548 | ||
549 | pidfp = fopen(file_name, "w+"); | |
550 | if(!pidfp) { | |
551 | PERROR("fopen (%s)", pidfile); | |
552 | WARN("killing child process"); | |
553 | return -1; | |
554 | } | |
555 | ||
556 | fprintf(pidfp, "%d\n", pid); | |
557 | ||
558 | fclose(pidfp); | |
559 | ||
560 | return 0; | |
561 | } | |
562 | ||
bce2937a | 563 | int start_ustd(int fd) |
3796af9b PMF |
564 | { |
565 | struct ustcomm_ustd ustd; | |
566 | int result; | |
a3cdd4a7 | 567 | sigset_t sigset; |
3158b808 PMF |
568 | struct sigaction sa; |
569 | ||
570 | result = sigemptyset(&sigset); | |
571 | if(result == -1) { | |
4d70f833 | 572 | PERROR("sigemptyset"); |
3158b808 PMF |
573 | return 1; |
574 | } | |
575 | sa.sa_handler = sigterm_handler; | |
576 | sa.sa_mask = sigset; | |
577 | sa.sa_flags = SA_RESTART; | |
578 | result = sigaction(SIGTERM, &sa, NULL); | |
579 | if(result == -1) { | |
580 | PERROR("sigaction"); | |
581 | return 1; | |
582 | } | |
3796af9b | 583 | |
cd226f25 | 584 | result = ustcomm_init_ustd(&ustd, sock_path); |
3796af9b PMF |
585 | if(result == -1) { |
586 | ERR("failed to initialize socket"); | |
587 | return 1; | |
588 | } | |
589 | ||
3158b808 | 590 | /* setup handler for SIGPIPE */ |
a3cdd4a7 PMF |
591 | result = sigemptyset(&sigset); |
592 | if(result == -1) { | |
4d70f833 | 593 | PERROR("sigemptyset"); |
a3cdd4a7 PMF |
594 | return 1; |
595 | } | |
596 | result = sigaddset(&sigset, SIGPIPE); | |
597 | if(result == -1) { | |
4d70f833 | 598 | PERROR("sigaddset"); |
a3cdd4a7 PMF |
599 | return 1; |
600 | } | |
601 | result = sigprocmask(SIG_BLOCK, &sigset, NULL); | |
602 | if(result == -1) { | |
4d70f833 | 603 | PERROR("sigprocmask"); |
a3cdd4a7 PMF |
604 | return 1; |
605 | } | |
606 | ||
2b3c64a4 PMF |
607 | /* Write pidfile */ |
608 | if(pidfile) { | |
609 | result = write_pidfile(pidfile, getpid()); | |
610 | if(result == -1) { | |
611 | ERR("failed to write pidfile"); | |
612 | return 1; | |
613 | } | |
614 | } | |
615 | ||
bce2937a PMF |
616 | /* Notify parent that we are successfully started. */ |
617 | if(fd != -1) { | |
618 | /* write any one character */ | |
619 | result = write(fd, "!", 1); | |
620 | if(result == -1) { | |
621 | PERROR("write"); | |
622 | return -1; | |
623 | } | |
624 | if(result != 1) { | |
625 | ERR("Problem sending confirmation of daemon start to parent"); | |
626 | return -1; | |
627 | } | |
628 | result = close(fd); | |
629 | if(result == -1) { | |
630 | PERROR("close"); | |
631 | } | |
632 | } | |
633 | ||
688760ef | 634 | /* app loop */ |
3796af9b PMF |
635 | for(;;) { |
636 | char *recvbuf; | |
637 | ||
3a7b90de | 638 | /* check for requests on our public socket */ |
688760ef PMF |
639 | result = ustcomm_ustd_recv_message(&ustd, &recvbuf, NULL, 100); |
640 | if(result == -1) { | |
641 | ERR("error in ustcomm_ustd_recv_message"); | |
f99c0b5c | 642 | goto loop_end; |
688760ef PMF |
643 | } |
644 | if(result > 0) { | |
645 | if(!strncmp(recvbuf, "collect", 7)) { | |
646 | pid_t pid; | |
647 | char *bufname; | |
648 | int result; | |
3796af9b | 649 | |
688760ef PMF |
650 | result = sscanf(recvbuf, "%*s %d %50as", &pid, &bufname); |
651 | if(result != 2) { | |
f99c0b5c PMF |
652 | ERR("parsing error: %s", recvbuf); |
653 | goto free_bufname; | |
688760ef | 654 | } |
3796af9b | 655 | |
f99c0b5c | 656 | result = start_consuming_buffer(pid, bufname); |
688760ef PMF |
657 | if(result < 0) { |
658 | ERR("error in add_buffer"); | |
f99c0b5c | 659 | goto free_bufname; |
688760ef | 660 | } |
f99c0b5c PMF |
661 | |
662 | free_bufname: | |
663 | free(bufname); | |
3796af9b PMF |
664 | } |
665 | ||
688760ef | 666 | free(recvbuf); |
3796af9b | 667 | } |
3158b808 | 668 | |
f99c0b5c PMF |
669 | loop_end: |
670 | ||
3158b808 PMF |
671 | if(terminate_req) { |
672 | pthread_mutex_lock(&active_buffers_mutex); | |
673 | if(active_buffers == 0) { | |
674 | pthread_mutex_unlock(&active_buffers_mutex); | |
675 | break; | |
676 | } | |
677 | pthread_mutex_unlock(&active_buffers_mutex); | |
678 | } | |
3796af9b PMF |
679 | } |
680 | ||
681 | return 0; | |
682 | } | |
bce2937a PMF |
683 | |
684 | int start_ustd_daemon() | |
685 | { | |
686 | int result; | |
687 | int fd[2]; | |
2730a7d6 | 688 | pid_t child_pid; |
bce2937a PMF |
689 | |
690 | result = pipe(fd); | |
691 | ||
2730a7d6 | 692 | result = child_pid = fork(); |
bce2937a PMF |
693 | if(result == -1) { |
694 | PERROR("fork"); | |
695 | return -1; | |
696 | } | |
697 | else if(result == 0) { | |
698 | return start_ustd(fd[1]); | |
699 | } | |
700 | else { | |
701 | char buf; | |
2730a7d6 PMF |
702 | FILE *pidfp; |
703 | ||
bce2937a PMF |
704 | result = read(fd[0], &buf, 1); |
705 | if(result == -1) { | |
706 | PERROR("read"); | |
707 | return -1; | |
708 | } | |
709 | if(result != 1) { | |
710 | ERR("did not receive valid confirmation that the daemon is started"); | |
711 | return -1; | |
712 | } | |
713 | ||
714 | result = close(fd[0]); | |
715 | if(result == -1) { | |
716 | PERROR("close"); | |
717 | } | |
718 | ||
719 | DBG("The daemon is now successfully started"); | |
720 | } | |
721 | ||
722 | /* Wait for confirmation that the server is ready. */ | |
723 | ||
724 | ||
725 | return 0; | |
726 | } | |
727 | ||
728 | int main(int argc, char **argv) | |
729 | { | |
730 | int result; | |
731 | ||
732 | result = parse_args(argc, argv); | |
733 | if(result == -1) { | |
734 | exit(1); | |
735 | } | |
736 | ||
737 | if(daemon_mode) { | |
738 | result = start_ustd_daemon(); | |
739 | } | |
740 | else { | |
741 | result = start_ustd(-1); | |
742 | } | |
743 | ||
744 | return result; | |
745 | } |