1 /* Copyright (C) 2009 Pierre-Marc Fournier
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.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
20 #include <sys/types.h>
40 /* return value: 0 = subbuffer is finished, it won't produce data anymore
41 * 1 = got subbuffer successfully
45 #define GET_SUBBUF_OK 1
46 #define GET_SUBBUF_DONE 0
47 #define GET_SUBBUF_DIED 2
49 #define PUT_SUBBUF_OK 1
50 #define PUT_SUBBUF_DIED 0
51 #define PUT_SUBBUF_PUSHED 2
54 char *trace_path
=NULL
;
56 /* Number of active buffers and the mutex to protect it. */
57 int active_buffers
= 0;
58 pthread_mutex_t active_buffers_mutex
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Whether a request to end the program was received. */
60 sig_atomic_t terminate_req
= 0;
62 int test_sigpipe(void)
67 result
= sigemptyset(&sigset
);
69 PERROR("sigemptyset");
72 result
= sigaddset(&sigset
, SIGPIPE
);
78 result
= sigtimedwait(&sigset
, NULL
, &(struct timespec
){0,0});
79 if(result
== -1 && errno
== EAGAIN
) {
80 /* no signal received */
83 else if(result
== -1) {
84 PERROR("sigtimedwait");
87 else if(result
== SIGPIPE
) {
88 /* received sigpipe */
96 int get_subbuffer(struct buffer_info
*buf
)
99 char *received_msg
=NULL
;
104 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
105 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
107 WARN("process %d destroyed before we could connect to it", buf
->pid
);
108 retval
= GET_SUBBUF_DONE
;
111 else if(result
< 0) {
112 ERR("get_subbuffer: ustcomm_send_request failed");
117 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
118 if(result
!= 2 && result
!= 1) {
119 ERR("unable to parse response to get_subbuffer");
124 DBG("received msg is %s", received_msg
);
126 if(!strcmp(rep_code
, "OK")) {
127 DBG("got subbuffer %s", buf
->name
);
128 retval
= GET_SUBBUF_OK
;
130 else if(nth_token_is(received_msg
, "END", 0) == 1) {
131 retval
= GET_SUBBUF_DONE
;
135 DBG("error getting subbuffer %s", buf
->name
);
139 /* FIMXE: free correctly the stuff */
152 int put_subbuffer(struct buffer_info
*buf
)
155 char *received_msg
=NULL
;
160 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
161 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
162 if(result
< 0 && errno
== ECONNRESET
) {
163 retval
= PUT_SUBBUF_DIED
;
167 ERR("put_subbuffer: send_message failed");
172 result
= sscanf(received_msg
, "%as", &rep_code
);
174 ERR("unable to parse response to put_subbuffer");
179 if(!strcmp(rep_code
, "OK")) {
180 DBG("subbuffer put %s", buf
->name
);
181 retval
= PUT_SUBBUF_OK
;
184 DBG("put_subbuffer: received error, we were pushed");
185 retval
= PUT_SUBBUF_PUSHED
;
202 void decrement_active_buffers(void *arg
)
204 pthread_mutex_lock(&active_buffers_mutex
);
206 pthread_mutex_unlock(&active_buffers_mutex
);
209 void *consumer_thread(void *arg
)
211 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
214 pthread_cleanup_push(decrement_active_buffers
, NULL
);
217 /* get the subbuffer */
218 result
= get_subbuffer(buf
);
220 ERR("error getting subbuffer");
223 else if(result
== GET_SUBBUF_DONE
) {
227 else if(result
== GET_SUBBUF_DIED
) {
228 finish_consuming_dead_subbuffer(buf
);
232 /* write data to file */
233 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
236 /* FIXME: maybe drop this trace */
239 /* put the subbuffer */
240 result
= put_subbuffer(buf
);
242 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
245 else if(result
== PUT_SUBBUF_PUSHED
) {
246 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
249 else if(result
== PUT_SUBBUF_DIED
) {
250 WARN("application died while putting subbuffer");
251 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
252 finish_consuming_dead_subbuffer(buf
);
255 else if(result
== PUT_SUBBUF_OK
) {
259 DBG("thread for buffer %s is stopping", buf
->name
);
261 /* FIXME: destroy, unalloc... */
263 pthread_cleanup_pop(1);
268 int create_dir_if_needed(char *dir
)
271 result
= mkdir(dir
, 0777);
273 if(errno
!= EEXIST
) {
282 int is_directory(const char *dir
)
287 result
= stat(dir
, &st
);
293 if(!S_ISDIR(st
.st_mode
)) {
300 int add_buffer(pid_t pid
, char *bufname
)
302 struct buffer_info
*buf
;
309 struct shmid_ds shmds
;
311 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
313 ERR("add_buffer: insufficient memory");
321 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
323 WARN("unable to connect to process, it probably died before we were able to connect");
328 asprintf(&send_msg
, "get_pidunique");
329 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
332 ERR("problem in ustcomm_send_request(get_pidunique)");
336 result
= sscanf(received_msg
, "%lld", &buf
->pidunique
);
338 ERR("unable to parse response to get_pidunique");
342 DBG("got pidunique %lld", buf
->pidunique
);
345 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
346 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
349 ERR("problem in ustcomm_send_request(get_shmid)");
353 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
355 ERR("unable to parse response to get_shmid");
359 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
362 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
363 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
366 ERR("problem in ustcomm_send_request(g_n_subbufs)");
370 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
372 ERR("unable to parse response to get_n_subbufs");
376 DBG("got n_subbufs %d", buf
->n_subbufs
);
378 /* get subbuf size */
379 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
380 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
383 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
385 ERR("unable to parse response to get_subbuf_size");
389 DBG("got subbuf_size %d", buf
->subbuf_size
);
392 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
393 if(buf
->mem
== (void *) 0) {
397 DBG("successfully attached buffer memory");
399 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
400 if(buf
->bufstruct_mem
== (void *) 0) {
404 DBG("successfully attached buffer bufstruct memory");
406 /* obtain info on the memory segment */
407 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
412 buf
->memlen
= shmds
.shm_segsz
;
414 /* open file for output */
416 /* Only create the directory if using the default path, because
417 * of the risk of typo when using trace path override. We don't
418 * want to risk creating plenty of useless directories in that case.
420 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
422 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
426 trace_path
= USTD_DEFAULT_TRACE_PATH
;
429 asprintf(&tmp
, "%s/%u_%lld", trace_path
, buf
->pid
, buf
->pidunique
);
430 result
= create_dir_if_needed(tmp
);
432 ERR("could not create directory %s", tmp
);
438 asprintf(&tmp
, "%s/%u_%lld/%s_0", trace_path
, buf
->pid
, buf
->pidunique
, buf
->name
);
439 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_EXCL
, 00600);
442 ERR("failed opening trace file %s", tmp
);
448 pthread_mutex_lock(&active_buffers_mutex
);
450 pthread_mutex_unlock(&active_buffers_mutex
);
452 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
459 fprintf(stderr
, "Usage:\nustd OPTIONS\n\nOptions:\n"
460 "\t-h\t\tDisplay this usage.\n"
461 "\t-o DIR\t\tSpecify the directory where to output the traces.\n"
462 "\t-s PATH\t\tSpecify the path to use for the daemon socket.\n");
465 int parse_args(int argc
, char **argv
)
470 int option_index
= 0;
471 static struct option long_options
[] = {
473 {"version", 0, 0, 'V'},
477 c
= getopt_long(argc
, argv
, "hs:o:", long_options
, &option_index
);
483 printf("option %s", long_options
[option_index
].name
);
485 printf(" with arg %s", optarg
);
493 if(!is_directory(trace_path
)) {
494 ERR("Not a valid directory. (%s)", trace_path
);
502 printf("Version 0.0\n");
506 /* unknown option or other error; error is
507 printed by getopt, just return */
515 void sigterm_handler(int sig
)
520 int main(int argc
, char **argv
)
522 struct ustcomm_ustd ustd
;
527 result
= sigemptyset(&sigset
);
529 PERROR("sigemptyset");
532 sa
.sa_handler
= sigterm_handler
;
534 sa
.sa_flags
= SA_RESTART
;
535 result
= sigaction(SIGTERM
, &sa
, NULL
);
541 result
= parse_args(argc
, argv
);
546 result
= ustcomm_init_ustd(&ustd
, sock_path
);
548 ERR("failed to initialize socket");
552 /* setup handler for SIGPIPE */
553 result
= sigemptyset(&sigset
);
555 PERROR("sigemptyset");
558 result
= sigaddset(&sigset
, SIGPIPE
);
563 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
565 PERROR("sigprocmask");
573 /* check for requests on our public socket */
574 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
576 ERR("error in ustcomm_ustd_recv_message");
580 if(!strncmp(recvbuf
, "collect", 7)) {
585 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
587 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
590 result
= add_buffer(pid
, bufname
);
592 ERR("error in add_buffer");
601 pthread_mutex_lock(&active_buffers_mutex
);
602 if(active_buffers
== 0) {
603 pthread_mutex_unlock(&active_buffers_mutex
);
606 pthread_mutex_unlock(&active_buffers_mutex
);