From d0b5f2b948ed0da3f8acd10817f85f5200749121 Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Mon, 23 Feb 2009 17:58:06 -0500 Subject: [PATCH] ust: continue moving communication stuff to libustcomm and cleanup --- hello/hello.c | 2 +- libtracectl/Makefile | 2 +- libtracectl/marker-control.c | 4 +- libtracectl/tracectl.c | 188 +++++++++++++++-------------------- libtracing/tracer.h | 2 + libustcomm/ustcomm.c | 124 +++++++++++++++++++++-- libustcomm/ustcomm.h | 18 +++- 7 files changed, 218 insertions(+), 122 deletions(-) diff --git a/hello/hello.c b/hello/hello.c index 88087dff..3bbb2bd9 100644 --- a/hello/hello.c +++ b/hello/hello.c @@ -63,7 +63,7 @@ int main() printf("Hello, World!\n"); sleep(1); - for(i=0; i<50; i++) { + for(i=0; i<5000; i++) { trace_mark(ust, bar, "str %s", "FOOBAZ"); trace_mark(ust, bar2, "number1 %d number2 %d", 53, 9800); usleep(100000); diff --git a/libtracectl/Makefile b/libtracectl/Makefile index e3040971..acbfbead 100644 --- a/libtracectl/Makefile +++ b/libtracectl/Makefile @@ -1,7 +1,7 @@ all: libtracectl.so libtracectl.so: tracectl.c - gcc -g -shared -fPIC -I../libmarkers -I../libtracing -I../share $(CFLAGS) -o libtracectl.so tracectl.c marker-control.c ../share/usterr.c + gcc -g -shared -fPIC -I../libmarkers -I../libtracing -I../share -I../libustcomm -I. $(CFLAGS) -o libtracectl.so tracectl.c marker-control.c ../share/usterr.c ../libustcomm/ustcomm.c clean: rm -rf *.so *.o diff --git a/libtracectl/marker-control.c b/libtracectl/marker-control.c index ab5413dc..29c43b09 100644 --- a/libtracectl/marker-control.c +++ b/libtracectl/marker-control.c @@ -34,7 +34,7 @@ #include "kernelcompat.h" #include "list.h" #include "tracer.h" -#include "usterr.h" +#include "localerr.h" #define DEFAULT_CHANNEL "cpu" #define DEFAULT_PROBE "default" @@ -432,8 +432,6 @@ void __attribute__((constructor)) init_marker_control(void) initialized = 1; } - - return 0; } //ust// module_init(marker_control_init); diff --git a/libtracectl/tracectl.c b/libtracectl/tracectl.c index a25bd3b2..3c5ff859 100644 --- a/libtracectl/tracectl.c +++ b/libtracectl/tracectl.c @@ -9,11 +9,11 @@ #include "marker.h" #include "tracer.h" -#include "usterr.h" +#include "localerr.h" +#include "ustcomm.h" #define UNIX_PATH_MAX 108 -//#define SOCKETDIR "/var/run/ust/socks" #define SOCKETDIR "/tmp/socks" #define SOCKETDIRLEN sizeof(SOCKETDIR) #define USTSIGNAL SIGIO @@ -24,6 +24,8 @@ char consumer_stack[10000]; +static struct ustcomm_app ustcomm_app; + struct tracecmd { /* no padding */ uint32_t size; uint16_t command; @@ -45,7 +47,7 @@ struct trctl_msg { pid_t mypid; char mysocketfile[UNIX_PATH_MAX] = ""; -int pfd = -1; +//int pfd = -1; struct consumer_channel { int fd; @@ -172,6 +174,7 @@ static void print_markers(void) { struct marker_iter iter; + lock_markers(); marker_iter_reset(&iter); marker_iter_start(&iter); @@ -179,6 +182,7 @@ static void print_markers(void) fprintf(stderr, "marker: %s_%s \"%s\"\n", iter.marker->channel, iter.marker->name, iter.marker->format); marker_iter_next(&iter); } + unlock_markers(); } void do_command(struct tracecmd *cmd) @@ -211,7 +215,11 @@ void notif_cb(void) } } -char recvbuf[10000]; +#define CONSUMER_DAEMON_SOCK SOCKETDIR "/ustd" + +static int inform_consumer_daemon(void) +{ +} int listener_main(void *p) { @@ -223,82 +231,78 @@ int listener_main(void *p) socklen_t addrlen = sizeof(addr); char trace_name[] = "auto"; char trace_type[] = "ustrelay"; + char *recvbuf; + int len; - for(;;) { - struct trctl_msg msg; - int len; - - result = len = recvfrom(pfd, recvbuf, sizeof(recvbuf-1), 0, &addr, &addrlen); - if(result == -1) { - PERROR("recvfrom"); - continue; - } - - if(recvbuf[len-1] == '\n') - recvbuf[len-1] = '\0'; - else - recvbuf[len] = 0; + result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf); + if(result) { + WARN("error in ustcomm_app_recv_message"); + continue; + } - fprintf(stderr, "received a message! it's: %s\n", recvbuf); + DBG("received a message! it's: %s\n", recvbuf); + len = strlen(recvbuf); + if(len && recvbuf[len-1] == '\n') { + recvbuf[len-1] = '\0'; + } + if(!strcmp(recvbuf, "print_markers")) { + print_markers(); + } + else if(!strcmp(recvbuf, "trace_setup")) { + DBG("trace setup"); - if(!strcmp(recvbuf, "print_markers")) { - print_markers(); + result = ltt_trace_setup(trace_name); + if(result < 0) { + ERR("ltt_trace_setup failed"); + return; } - else if(!strcmp(recvbuf, "trace_setup")) { - DBG("trace setup"); - - result = ltt_trace_setup(trace_name); - if(result < 0) { - ERR("ltt_trace_setup failed"); - return; - } - - result = ltt_trace_set_type(trace_name, trace_type); - if(result < 0) { - ERR("ltt_trace_set_type failed"); - return; - } + + result = ltt_trace_set_type(trace_name, trace_type); + if(result < 0) { + ERR("ltt_trace_set_type failed"); + return; } - else if(!strcmp(recvbuf, "trace_alloc")) { - DBG("trace alloc"); - - result = ltt_trace_alloc(trace_name); - if(result < 0) { - ERR("ltt_trace_alloc failed"); - return; - } + } + else if(!strcmp(recvbuf, "trace_alloc")) { + DBG("trace alloc"); + + result = ltt_trace_alloc(trace_name); + if(result < 0) { + ERR("ltt_trace_alloc failed"); + return; } - else if(!strcmp(recvbuf, "trace_start")) { - DBG("trace start"); - - result = ltt_trace_start(trace_name); - if(result < 0) { - ERR("ltt_trace_start failed"); - return; - } + } + else if(!strcmp(recvbuf, "trace_start")) { + DBG("trace start"); + + result = ltt_trace_start(trace_name); + if(result < 0) { + ERR("ltt_trace_start failed"); + continue; } - else if(!strcmp(recvbuf, "trace_stop")) { - DBG("trace stop"); - - result = ltt_trace_stop(trace_name); - if(result < 0) { - ERR("ltt_trace_stop failed"); - return; - } + } + else if(!strcmp(recvbuf, "trace_stop")) { + DBG("trace stop"); + + result = ltt_trace_stop(trace_name); + if(result < 0) { + ERR("ltt_trace_stop failed"); + return; } - else if(!strcmp(recvbuf, "trace_destroy")) { + } + else if(!strcmp(recvbuf, "trace_destroy")) { - DBG("trace destroy"); + DBG("trace destroy"); - result = ltt_trace_destroy(trace_name); - if(result < 0) { - ERR("ltt_trace_destroy failed"); - return; - } + result = ltt_trace_destroy(trace_name); + if(result < 0) { + ERR("ltt_trace_destroy failed"); + return; } } - next_conn:; + + free(recvbuf); } } @@ -313,12 +317,18 @@ void create_listener(void) } } -/* The signal handler itself. */ +/* The signal handler itself. Signals must be setup so there cannot be + nested signals. */ void sighandler(int sig) { + static char have_listener = 0; DBG("sighandler"); - create_listener(); + + if(!have_listener) { + create_listener(); + have_listener = 1; + } } /* Called by the app signal handler to chain it to us. */ @@ -330,43 +340,7 @@ void chain_signal(void) static int init_socket(void) { - int result; - int fd; - char pidstr[6]; - int pidlen; - - struct sockaddr_un addr; - - result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); - if(result == -1) { - PERROR("socket"); - return -1; - } - - addr.sun_family = AF_UNIX; - - result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCKETDIR, mypid); - if(result >= UNIX_PATH_MAX) { - ERR("string overflow allocating socket name"); - goto close_sock; - } - //DBG("opening socket at %s", addr.sun_path); - - result = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); - if(result == -1) { - PERROR("bind"); - goto close_sock; - } - - strcpy(mysocketfile, addr.sun_path); - - pfd = fd; - return 0; - - close_sock: - close(fd); - - return -1; + return ustcomm_init_app(getpid(), &ustcomm_app); } static void destroy_socket(void) @@ -528,7 +502,7 @@ static void __attribute__((destructor)) fini() } /* FIXME: wait for the consumer to be done */ - sleep(3); + sleep(1); destroy_socket(); } diff --git a/libtracing/tracer.h b/libtracing/tracer.h index e39947f0..9af31944 100644 --- a/libtracing/tracer.h +++ b/libtracing/tracer.h @@ -692,4 +692,6 @@ void ltt_unlock_traces(void); //ust// #endif /* CONFIG_LTT */ +struct ltt_trace_struct *_ltt_trace_find(const char *trace_name); + #endif /* _LTT_TRACER_H */ diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 3843921c..380df2f5 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -1,16 +1,24 @@ +#define _GNU_SOURCE #include #include #include #include #include +#include #include #include +#include + +#include "ustcomm.h" +#include "localerr.h" #define UNIX_PATH_MAX 108 #define SOCK_DIR "/tmp/socks" #define UST_SIGNAL SIGIO +#define MSG_MAX 1000 + static void signal_process(pid_t pid) { int result; @@ -24,17 +32,22 @@ static void signal_process(pid_t pid) sleep(1); } -int send_message(pid_t pid, const char *msg, const char *reply) +/* pid: the pid of the trace process that must receive the msg + msg: pointer to a null-terminated message to send + reply: location where to put the null-terminated string of the reply; + it must be free'd after usage + */ + +int send_message(pid_t pid, const char *msg, char **reply) { int fd; int result; struct sockaddr_un addr; - char *buf; result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); if(result == -1) { perror("socket"); - return 1; + return -1; } addr.sun_family = AF_UNIX; @@ -42,22 +55,115 @@ int send_message(pid_t pid, const char *msg, const char *reply) result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); if(result >= UNIX_PATH_MAX) { fprintf(stderr, "string overflow allocating socket name"); - return 1; + return -1; } - asprintf(&buf, "%s\n", msg); - signal_process(pid); - result = sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); + result = sendto(fd, msg, strlen(msg), 0, (struct sockaddr *)&addr, sizeof(addr)); if(result == -1) { perror("sendto"); - return 1; + return -1; } - free(buf); + if(!reply) + return 0; + + *reply = (char *) malloc(MSG_MAX+1); + result = recvfrom(fd, *reply, MSG_MAX, 0, NULL, NULL); + if(result == -1) { + perror("recvfrom"); + return -1; + } + + (*reply)[result] = '\0'; return 0; } +int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg) +{ + int fd; + int result; + struct sockaddr_un addr; + + *msg = (char *) malloc(MSG_MAX+1); + result = recvfrom(app->fd, *msg, MSG_MAX, 0, NULL, NULL); + if(result == -1) { + PERROR("recvfrom"); + return -1; + } + + DBG("ustcomm_app_recv_message: result is %d, message[1] is %hhd", result, (*msg)[1]); + (*msg)[result] = '\0'; + return 0; +} + +static int init_named_socket(char *name, char **path_out) +{ + int result; + int fd; + + struct sockaddr_un addr; + + result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); + if(result == -1) { + PERROR("socket"); + return -1; + } + + addr.sun_family = AF_UNIX; + + strncpy(addr.sun_path, name, UNIX_PATH_MAX); + addr.sun_path[UNIX_PATH_MAX-1] = '\0'; + + result = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); + if(result == -1) { + PERROR("bind"); + goto close_sock; + } + + if(path_out) + *path_out = strdupa(addr.sun_path); + + return fd; + + close_sock: + close(fd); + + return -1; +} + +int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle) +{ + int result; + char *name; + + result = asprintf(&name, "%s/%d", SOCK_DIR, (int)pid); + if(result >= UNIX_PATH_MAX) { + ERR("string overflow allocating socket name"); + return -1; + } + + handle->fd = init_named_socket(name, &handle->socketpath); + if(handle->fd < 0) { + goto free_name; + } + free(name); + + return 0; + +free_name: + free(name); + return -1; +} + +int ustcomm_init_ustd(struct ustcomm_ustd *handle) +{ + handle->fd = init_named_socket("ustd", &handle->socketpath); + if(handle->fd < 0) + return handle->fd; + + return 0; +} diff --git a/libustcomm/ustcomm.h b/libustcomm/ustcomm.h index adbd0ae3..fe410364 100644 --- a/libustcomm/ustcomm.h +++ b/libustcomm/ustcomm.h @@ -3,6 +3,22 @@ #include -int send_message(pid_t pid, const char *msg, const char *reply); +struct ustcomm_app { + /* the socket for serving the external requests */ + int fd; + char *socketpath; +}; + +struct ustcomm_ustd { + /* the socket for serving the external requests */ + int fd; + char *socketpath; +}; + +int send_message(pid_t pid, const char *msg, char **reply); + +int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle); + +int ustcomm_init_ustd(struct ustcomm_ustd *handle); #endif /* USTCOMM_H */ -- 2.34.1