ust: continue moving communication stuff to libustcomm and cleanup
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Mon, 23 Feb 2009 22:58:06 +0000 (17:58 -0500)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Mon, 23 Feb 2009 22:58:06 +0000 (17:58 -0500)
hello/hello.c
libtracectl/Makefile
libtracectl/marker-control.c
libtracectl/tracectl.c
libtracing/tracer.h
libustcomm/ustcomm.c
libustcomm/ustcomm.h

index 88087dff3a1a6d36488310f0135e4eba7e58e76e..3bbb2bd94961523bb7721fae991ef5a5740e423a 100644 (file)
@@ -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);
index e30409710ddd0e73d5a19b50f9ad68d031367aa9..acbfbead11ba0f10099ecbd868217327cb5e0ccc 100644 (file)
@@ -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
index ab5413dcbc4c2c757f8aa01d1a56dc4b1c00d42c..29c43b09816e9ccf196b883335756b0999e90faf 100644 (file)
@@ -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);
 
index a25bd3b214dd0018b2765f3363ec2296e7d6a4de..3c5ff859c7f5f9b15139e86ca11e2f3c4b2d3184 100644 (file)
@@ -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();
 }
index e39947f0185141146ae86d8eea61e9ff134b0bdb..9af31944cda740b346d94d833abd7f03edee32cb 100644 (file)
@@ -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 */
index 3843921cf08933a4e6212f0e31e3762452b69456..380df2f51a645fc1a8f56680322ff44c3455c6e7 100644 (file)
@@ -1,16 +1,24 @@
+#define _GNU_SOURCE
 #include <sys/types.h>
 #include <signal.h>
 #include <errno.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <unistd.h>
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
+#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;
+}
index adbd0ae3e730f55903496ba1a2701acbfff62dab..fe4103644329ad7c25a94b6580cd3a8550cbd07d 100644 (file)
@@ -3,6 +3,22 @@
 
 #include <sys/types.h>
 
-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 */
This page took 0.031567 seconds and 4 git commands to generate.