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>
37 /* return value: 0 = subbuffer is finished, it won't produce data anymore
38 * 1 = got subbuffer successfully
42 #define GET_SUBBUF_OK 1
43 #define GET_SUBBUF_DONE 0
44 #define GET_SUBBUF_DIED 2
46 #define PUT_SUBBUF_OK 1
47 #define PUT_SUBBUF_DIED 0
48 #define PUT_SUBBUF_PUSHED 2
52 int test_sigpipe(void)
57 result
= sigemptyset(&sigset
);
59 perror("sigemptyset");
62 result
= sigaddset(&sigset
, SIGPIPE
);
68 result
= sigtimedwait(&sigset
, NULL
, &(struct timespec
){0,0});
69 if(result
== -1 && errno
== EAGAIN
) {
70 /* no signal received */
73 else if(result
== -1) {
74 perror("sigtimedwait");
77 else if(result
== SIGPIPE
) {
78 /* received sigpipe */
86 int get_subbuffer(struct buffer_info
*buf
)
94 asprintf(&send_msg
, "get_subbuffer %s", buf
->name
);
95 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
98 WARN("process %d destroyed before we could connect to it", buf
->pid
);
99 return GET_SUBBUF_DONE
;
101 else if(result
< 0) {
102 ERR("get_subbuffer: ustcomm_send_request failed");
105 else if(result
== 0) {
106 DBG("app died while being traced");
107 return GET_SUBBUF_DIED
;
110 result
= sscanf(received_msg
, "%as %ld", &rep_code
, &buf
->consumed_old
);
111 if(result
!= 2 && result
!= 1) {
112 ERR("unable to parse response to get_subbuffer");
116 DBG("received msg is %s", received_msg
);
118 if(!strcmp(rep_code
, "OK")) {
119 DBG("got subbuffer %s", buf
->name
);
120 retval
= GET_SUBBUF_OK
;
122 else if(nth_token_is(received_msg
, "END", 0) == 1) {
123 return GET_SUBBUF_DONE
;
126 DBG("error getting subbuffer %s", buf
->name
);
130 /* FIMXE: free correctly the stuff */
136 int put_subbuffer(struct buffer_info
*buf
)
144 asprintf(&send_msg
, "put_subbuffer %s %ld", buf
->name
, buf
->consumed_old
);
145 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
147 ERR("put_subbuffer: send_message failed");
152 result
= sscanf(received_msg
, "%as", &rep_code
);
154 ERR("unable to parse response to put_subbuffer");
159 if(!strcmp(rep_code
, "OK")) {
160 DBG("subbuffer put %s", buf
->name
);
161 retval
= PUT_SUBBUF_OK
;
164 DBG("put_subbuffer: received error, we were pushed");
165 return PUT_SUBBUF_PUSHED
;
172 /* This write is patient because it restarts if it was incomplete.
175 ssize_t
patient_write(int fd
, const void *buf
, size_t count
)
177 const char *bufc
= (const char *) buf
;
181 result
= write(fd
, bufc
, count
);
193 return bufc
-(const char *)buf
;
196 void *consumer_thread(void *arg
)
198 struct buffer_info
*buf
= (struct buffer_info
*) arg
;
202 /* get the subbuffer */
203 result
= get_subbuffer(buf
);
205 ERR("error getting subbuffer");
208 else if(result
== GET_SUBBUF_DONE
) {
212 else if(result
== GET_SUBBUF_DIED
) {
213 finish_consuming_dead_subbuffer(buf
);
217 /* write data to file */
218 result
= patient_write(buf
->file_fd
, buf
->mem
+ (buf
->consumed_old
& (buf
->n_subbufs
* buf
->subbuf_size
-1)), buf
->subbuf_size
);
221 /* FIXME: maybe drop this trace */
224 /* put the subbuffer */
225 result
= put_subbuffer(buf
);
227 ERR("unknown error putting subbuffer (channel=%s)", buf
->name
);
230 else if(result
== PUT_SUBBUF_PUSHED
) {
231 ERR("Buffer overflow (channel=%s), reader pushed. This channel will not be usable passed this point.", buf
->name
);
234 else if(result
== PUT_SUBBUF_DIED
) {
235 WARN("application died while putting subbuffer");
236 /* FIXME: probably need to skip the first subbuffer in finish_consuming_dead_subbuffer */
237 finish_consuming_dead_subbuffer(buf
);
239 else if(result
== PUT_SUBBUF_OK
) {
243 DBG("thread for buffer %s is stopping", buf
->name
);
245 /* FIXME: destroy, unalloc... */
250 int create_dir_if_needed(char *dir
)
253 result
= mkdir(dir
, 0777);
255 if(errno
!= EEXIST
) {
264 int add_buffer(pid_t pid
, char *bufname
)
266 struct buffer_info
*buf
;
273 struct shmid_ds shmds
;
275 buf
= (struct buffer_info
*) malloc(sizeof(struct buffer_info
));
277 ERR("add_buffer: insufficient memory");
285 result
= ustcomm_connect_app(buf
->pid
, &buf
->conn
);
287 WARN("unable to connect to process, it probably died before we were able to connect");
292 asprintf(&send_msg
, "get_shmid %s", buf
->name
);
293 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
296 ERR("problem in ustcomm_send_request(get_shmid)");
300 result
= sscanf(received_msg
, "%d %d", &buf
->shmid
, &buf
->bufstruct_shmid
);
302 ERR("unable to parse response to get_shmid");
306 DBG("got shmids %d %d", buf
->shmid
, buf
->bufstruct_shmid
);
309 asprintf(&send_msg
, "get_n_subbufs %s", buf
->name
);
310 result
= ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
313 ERR("problem in ustcomm_send_request(g_n_subbufs)");
317 result
= sscanf(received_msg
, "%d", &buf
->n_subbufs
);
319 ERR("unable to parse response to get_n_subbufs");
323 DBG("got n_subbufs %d", buf
->n_subbufs
);
325 /* get subbuf size */
326 asprintf(&send_msg
, "get_subbuf_size %s", buf
->name
);
327 ustcomm_send_request(&buf
->conn
, send_msg
, &received_msg
);
330 result
= sscanf(received_msg
, "%d", &buf
->subbuf_size
);
332 ERR("unable to parse response to get_subbuf_size");
336 DBG("got subbuf_size %d", buf
->subbuf_size
);
339 buf
->mem
= shmat(buf
->shmid
, NULL
, 0);
340 if(buf
->mem
== (void *) 0) {
344 DBG("successfully attached buffer memory");
346 buf
->bufstruct_mem
= shmat(buf
->bufstruct_shmid
, NULL
, 0);
347 if(buf
->bufstruct_mem
== (void *) 0) {
351 DBG("successfully attached buffer bufstruct memory");
353 /* obtain info on the memory segment */
354 result
= shmctl(buf
->shmid
, IPC_STAT
, &shmds
);
359 buf
->memlen
= shmds
.shm_segsz
;
361 /* open file for output */
362 result
= create_dir_if_needed(USTD_DEFAULT_TRACE_PATH
);
364 ERR("could not create directory %s", USTD_DEFAULT_TRACE_PATH
);
368 asprintf(&tmp
, "%s/%u", USTD_DEFAULT_TRACE_PATH
, buf
->pid
);
369 result
= create_dir_if_needed(tmp
);
371 ERR("could not create directory %s", tmp
);
377 asprintf(&tmp
, "%s/%u/%s_0", USTD_DEFAULT_TRACE_PATH
, buf
->pid
, buf
->name
);
378 result
= fd
= open(tmp
, O_WRONLY
| O_CREAT
| O_TRUNC
, 00600);
381 ERR("failed opening trace file %s", tmp
);
387 pthread_create(&thr
, NULL
, consumer_thread
, buf
);
392 int main(int argc
, char **argv
)
394 struct ustcomm_ustd ustd
;
398 result
= ustcomm_init_ustd(&ustd
);
400 ERR("failed to initialize socket");
404 result
= sigemptyset(&sigset
);
406 perror("sigemptyset");
409 result
= sigaddset(&sigset
, SIGPIPE
);
414 result
= sigprocmask(SIG_BLOCK
, &sigset
, NULL
);
416 perror("sigprocmask");
424 /* check for requests on our public socket */
425 result
= ustcomm_ustd_recv_message(&ustd
, &recvbuf
, NULL
, 100);
427 ERR("error in ustcomm_ustd_recv_message");
431 if(!strncmp(recvbuf
, "collect", 7)) {
436 result
= sscanf(recvbuf
, "%*s %d %50as", &pid
, &bufname
);
438 fprintf(stderr
, "parsing error: %s\n", recvbuf
);
441 result
= add_buffer(pid
, bufname
);
443 ERR("error in add_buffer");
This page took 0.040107 seconds and 5 git commands to generate.