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