From: Pierre-Marc Fournier Date: Tue, 24 Feb 2009 22:34:22 +0000 (-0500) Subject: ust: improve message parsing X-Git-Tag: v1.9.1~1046 X-Git-Url: http://git.lttng.org./?a=commitdiff_plain;h=b02e31e53039229b50d2f54ee31d68709aba1412;p=lttng-ust.git ust: improve message parsing --- diff --git a/libtracectl/tracectl.c b/libtracectl/tracectl.c index d041e6bb..e926c9ef 100644 --- a/libtracectl/tracectl.c +++ b/libtracectl/tracectl.c @@ -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); } diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index d9c79563..c944214a 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -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; +} + diff --git a/libustcomm/ustcomm.h b/libustcomm/ustcomm.h index 08d74803..53ced514 100644 --- a/libustcomm/ustcomm.h +++ b/libustcomm/ustcomm.h @@ -2,6 +2,7 @@ #define USTCOMM_H #include +#include 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 */