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