ust: improve message parsing
authorPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Tue, 24 Feb 2009 22:34:22 +0000 (17:34 -0500)
committerPierre-Marc Fournier <pierre-marc.fournier@polymtl.ca>
Tue, 24 Feb 2009 22:34:22 +0000 (17:34 -0500)
libtracectl/tracectl.c
libustcomm/ustcomm.c
libustcomm/ustcomm.h

index d041e6bb0ce3caef7ac72bea86fe677c8349df60..e926c9efdfd7eb369a8ea88bc2e349fe6b7d68dd 100644 (file)
@@ -12,7 +12,7 @@
 #include "localerr.h"
 #include "ustcomm.h"
 
-#define USE_CLONE
+//#define USE_CLONE
 
 #define UNIX_PATH_MAX 108
 
@@ -244,8 +244,9 @@ int listener_main(void *p)
                char trace_type[] = "ustrelay";
                char *recvbuf;
                int len;
+               struct ustcomm_source src;
 
-               result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf);
+               result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf, &src);
                DBG("HERE");
                if(result) {
                        WARN("error in ustcomm_app_recv_message");
@@ -313,7 +314,7 @@ int listener_main(void *p)
                                return;
                        }
                }
-               else if(!strncmp(recvbuf, "get_shmid ", 10)) {
+               else if(nth_token_is(recvbuf, "get_shmid", 0) == 1) {
                        struct ltt_trace_struct *trace;
                        char trace_name[] = "auto";
                        int i;
@@ -337,6 +338,13 @@ int listener_main(void *p)
 
                        }
                }
+               else if(nth_token_is(recvbuf, "load_probe_lib", 0) == 1) {
+                       char *libfile;
+
+                       libfile = nth_token(recvbuf, 1);
+
+                       DBG("load_probe_lib loading %s", libfile);
+               }
 
                free(recvbuf);
        }
index d9c795633b238ac103a4315a7045fbe5e0c5afea..c944214ae2ea93aec91febf836e6b8018bc873d5 100644 (file)
@@ -130,12 +130,26 @@ int ustcomm_request_consumer(pid_t pid, const char *channel)
        return 0;
 }
 
-static int recv_message_fd(int fd, char **msg)
+static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src)
 {
        int result;
+       size_t initial_addrlen,addrlen;
 
        *msg = (char *) malloc(MSG_MAX+1);
-       result = recvfrom(fd, *msg, MSG_MAX, 0, NULL, NULL);
+
+       if(src) {
+               initial_addrlen = addrlen = sizeof(src->addr);
+
+               result = recvfrom(fd, *msg, MSG_MAX, 0, &src->addr, &addrlen);
+               if(initial_addrlen != addrlen) {
+                       ERR("recvfrom: unexpected address length");
+                       return -1;
+               }
+       }
+       else {
+               result = recvfrom(fd, *msg, MSG_MAX, 0, NULL, NULL);
+       }
+
        if(result == -1) {
                PERROR("recvfrom");
                return -1;
@@ -148,14 +162,14 @@ static int recv_message_fd(int fd, char **msg)
        return 0;
 }
 
-int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg)
+int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src)
 {
-       return recv_message_fd(ustd->fd, msg);
+       return recv_message_fd(ustd->fd, msg, src);
 }
 
-int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg)
+int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src)
 {
-       return recv_message_fd(app->fd, msg);
+       return recv_message_fd(app->fd, msg, src);
 }
 
 static int init_named_socket(char *name, char **path_out)
@@ -237,3 +251,88 @@ int ustcomm_init_ustd(struct ustcomm_ustd *handle)
 
        return 0;
 }
+
+char *find_tok(const char *str)
+{
+       while(*str == ' ') {
+               str++;
+
+               if(*str == 0)
+                       return NULL;
+       }
+
+       return str;
+}
+
+static char *find_sep(char *str)
+{
+       while(*str != ' ') {
+               str++;
+
+               if(*str == 0)
+                       break;
+       }
+
+       return str;
+}
+
+int nth_token_is(char *str, char *token, int tok_no)
+{
+       int i;
+       char *start;
+       char *end;
+
+       for(i=0; i<=tok_no; i++) {
+               str = find_tok(str);
+               if(str == NULL)
+                       return -1;
+
+               start = str;
+
+               str = find_sep(str);
+               if(str == NULL)
+                       return -1;
+
+               end = str;
+       }
+
+       if(end-start != strlen(token))
+               return 0;
+
+       if(strncmp(start, token, end-start))
+               return 0;
+
+       return 1;
+}
+
+char *nth_token(char *str, int tok_no)
+{
+       static char *retval = NULL;
+       int i;
+       char *start;
+       char *end;
+
+       for(i=0; i<=tok_no; i++) {
+               str = find_tok(str);
+               if(str == NULL)
+                       return NULL;
+
+               start = str;
+
+               str = find_sep(str);
+               if(str == NULL)
+                       return NULL;
+
+               end = str;
+       }
+
+       if(retval) {
+               free(retval);
+               retval = NULL;
+       }
+
+       retval = strndupa(start, end-start);
+
+       return retval;
+}
+
index 08d74803857feecee46be5b3befac1f799201463..53ced5140c962d604fc50992350db50dda48ba6b 100644 (file)
@@ -2,6 +2,7 @@
 #define USTCOMM_H
 
 #include <sys/types.h>
+#include <sys/un.h>
 
 struct ustcomm_app {
        /* the "server" socket for serving the external requests */
@@ -15,13 +16,21 @@ struct ustcomm_ustd {
        char *socketpath;
 };
 
+struct ustcomm_source {
+       struct sockaddr_un addr;
+};
+
 int send_message(pid_t pid, const char *msg, char **reply);
 
-int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg);
-int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg);
+int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src);
+int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src);
 
 int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle);
 
 int ustcomm_init_ustd(struct ustcomm_ustd *handle);
 
+int nth_token_is(char *str, char *token, int tok_no);
+
+char *nth_token(char *str, int tok_no);
+
 #endif /* USTCOMM_H */
This page took 0.028279 seconds and 4 git commands to generate.