From b0540e11c3e9a3596b4b3865b0960d693fa66a88 Mon Sep 17 00:00:00 2001 From: Pierre-Marc Fournier Date: Mon, 23 Feb 2009 20:29:46 -0500 Subject: [PATCH] ust: continue work --- libtracectl/tracectl.c | 21 +++++++-- libustcomm/ustcomm.c | 105 ++++++++++++++++++++++++++++++++--------- libustcomm/ustcomm.h | 4 +- 3 files changed, 102 insertions(+), 28 deletions(-) diff --git a/libtracectl/tracectl.c b/libtracectl/tracectl.c index 3c5ff85..50cb303 100644 --- a/libtracectl/tracectl.c +++ b/libtracectl/tracectl.c @@ -168,6 +168,9 @@ void start_consumer(void) if(result == -1) { perror("clone"); } +// pthread_t thread; +// +// pthread_create(&thread, NULL, consumer, NULL); } static void print_markers(void) @@ -225,6 +228,8 @@ int listener_main(void *p) { int result; + DBG("LISTENER"); + for(;;) { uint32_t size; struct sockaddr_un addr; @@ -235,6 +240,7 @@ int listener_main(void *p) int len; result = ustcomm_app_recv_message(&ustcomm_app, &recvbuf); + DBG("HERE"); if(result) { WARN("error in ustcomm_app_recv_message"); continue; @@ -242,9 +248,9 @@ int listener_main(void *p) DBG("received a message! it's: %s\n", recvbuf); len = strlen(recvbuf); - if(len && recvbuf[len-1] == '\n') { - recvbuf[len-1] = '\0'; - } + //if(len && recvbuf[len-1] == '\n') { + // recvbuf[len-1] = '\0'; + //} if(!strcmp(recvbuf, "print_markers")) { print_markers(); @@ -306,15 +312,21 @@ int listener_main(void *p) } } +static char listener_stack[16384]; + void create_listener(void) { int result; static char listener_stack[16384]; + //char *listener_stack = malloc(16384); result = clone(listener_main, listener_stack+sizeof(listener_stack)-1, CLONE_FS | CLONE_FILES | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD, NULL); if(result == -1) { perror("clone"); } + //pthread_t thread; + + //pthread_create(&thread, NULL, listener_main, NULL); } /* The signal handler itself. Signals must be setup so there cannot be @@ -460,8 +472,7 @@ static void __attribute__((constructor(1000))) init() start_consumer(); } - /* Must create socket before signal handler to prevent races - * on pfd variable. + /* Must create socket before signal handler to prevent races. */ result = init_socket(); if(result == -1) { diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 380df2f..0275a73 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "ustcomm.h" #include "localerr.h" @@ -19,26 +20,29 @@ #define MSG_MAX 1000 +static void bt(void) +{ + void *buffer[100]; + int result; + + result = backtrace(&buffer, 100); + backtrace_symbols_fd(buffer, result, STDERR_FILENO); +} + static void signal_process(pid_t pid) { int result; result = kill(pid, UST_SIGNAL); if(result == -1) { - perror("kill"); + PERROR("kill"); return; } sleep(1); } -/* 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 send_message_path(const char *path, const char *msg, char **reply, int signalpid) { int fd; int result; @@ -46,23 +50,24 @@ int send_message(pid_t pid, const char *msg, char **reply) result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); if(result == -1) { - perror("socket"); + PERROR("socket"); return -1; } addr.sun_family = AF_UNIX; - result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); + result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path); if(result >= UNIX_PATH_MAX) { - fprintf(stderr, "string overflow allocating socket name"); + ERR("string overflow allocating socket name"); return -1; } - signal_process(pid); + if(signalpid >= 0) + signal_process(signalpid); result = sendto(fd, msg, strlen(msg), 0, (struct sockaddr *)&addr, sizeof(addr)); if(result == -1) { - perror("sendto"); + PERROR("sendto"); return -1; } @@ -72,7 +77,7 @@ int send_message(pid_t pid, const char *msg, char **reply) *reply = (char *) malloc(MSG_MAX+1); result = recvfrom(fd, *reply, MSG_MAX, 0, NULL, NULL); if(result == -1) { - perror("recvfrom"); + PERROR("recvfrom"); return -1; } @@ -81,25 +86,81 @@ int send_message(pid_t pid, const char *msg, char **reply) return 0; } -int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg) +/* 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 result; + char path[UNIX_PATH_MAX]; + + result = snprintf(path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); + if(result >= UNIX_PATH_MAX) { + fprintf(stderr, "string overflow allocating socket name"); + return -1; + } + + send_message_path(path, msg, reply, pid); + + return 0; +} + +/* Called by an app to ask the consumer daemon to connect to it. */ + +int ustcomm_request_consumer(pid_t pid, const char *channel) +{ + char path[UNIX_PATH_MAX]; + int result; + char *msg; + + result = snprintf(path, UNIX_PATH_MAX, "%s/ustd", SOCK_DIR); + if(result >= UNIX_PATH_MAX) { + fprintf(stderr, "string overflow allocating socket name"); + return -1; + } + + asprintf(&msg, "collect %d %s", pid, channel); + + send_message_path(path, msg, NULL, pid); + free(msg); + + return 0; +} + +static int recv_message_fd(int fd, 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); + result = recvfrom(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'; + + DBG("ustcomm_app_recv_message: result is %d, message is %s", result, (*msg)); + + bt(); return 0; } +int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg) +{ + return recv_message_fd(ustd->fd, msg); +} + +int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg) +{ + return recv_message_fd(app->fd, msg); +} + static int init_named_socket(char *name, char **path_out) { int result; @@ -124,8 +185,10 @@ static int init_named_socket(char *name, char **path_out) goto close_sock; } - if(path_out) + if(path_out) { + *path_out = ""; *path_out = strdupa(addr.sun_path); + } return fd; @@ -146,7 +209,7 @@ int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle) return -1; } - handle->fd = init_named_socket(name, &handle->socketpath); + handle->fd = init_named_socket(name, &(handle->socketpath)); if(handle->fd < 0) { goto free_name; } diff --git a/libustcomm/ustcomm.h b/libustcomm/ustcomm.h index fe41036..ab475d9 100644 --- a/libustcomm/ustcomm.h +++ b/libustcomm/ustcomm.h @@ -4,13 +4,13 @@ #include struct ustcomm_app { - /* the socket for serving the external requests */ + /* the "server" socket for serving the external requests */ int fd; char *socketpath; }; struct ustcomm_ustd { - /* the socket for serving the external requests */ + /* the "server" socket for serving the external requests */ int fd; char *socketpath; }; -- 2.34.1