From: Pierre-Marc Fournier Date: Tue, 17 Mar 2009 17:18:32 +0000 (-0400) Subject: ustcomm: use connections; don't reconnect at every message X-Git-Tag: v1.9.1~1036 X-Git-Url: http://git.lttng.org./?a=commitdiff_plain;h=4e2a88089f60f56b118bbfcbac65f74e79166436;p=lttng-ust.git ustcomm: use connections; don't reconnect at every message --- diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 4fe46c53..f2aeb61f 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -62,7 +62,7 @@ static void signal_process(pid_t pid) sleep(1); } -int send_message_fd(int fd, const char *msg, char **reply) +static int send_message_fd(int fd, const char *msg) { int result; @@ -75,25 +75,24 @@ int send_message_fd(int fd, const char *msg, char **reply) return 0; } - if(!reply) - return 1; - - *reply = (char *) malloc(MSG_MAX+1); - result = recv(fd, *reply, MSG_MAX, 0); - if(result == -1) { - PERROR("recv"); - return -1; - } - else if(result == 0) { - return 0; - } - - (*reply)[result] = '\0'; - return 1; + +// *reply = (char *) malloc(MSG_MAX+1); +// result = recv(fd, *reply, MSG_MAX, 0); +// if(result == -1) { +// PERROR("recv"); +// return -1; +// } +// else if(result == 0) { +// return 0; +// } +// +// (*reply)[result] = '\0'; +// +// return 1; } -int send_message_path(const char *path, const char *msg, char **reply, int signalpid) +static int send_message_path(const char *path, const char *msg, int signalpid) { int fd; int result; @@ -122,30 +121,30 @@ int send_message_path(const char *path, const char *msg, char **reply, int signa return -1; } - return send_message_fd(fd, msg, reply); + return send_message_fd(fd, 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; -} +///* 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(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. */ @@ -163,7 +162,7 @@ int ustcomm_request_consumer(pid_t pid, const char *channel) asprintf(&msg, "collect %d %s", pid, channel); - send_message_path(path, msg, NULL, -1); + send_message_path(path, msg, -1); free(msg); return 0; @@ -200,7 +199,7 @@ int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_ { int result; - result = send_message_fd(src->fd, msg, NULL); + result = send_message_fd(src->fd, msg); if(result < 0) { ERR("error in send_message_fd"); return -1; @@ -390,6 +389,97 @@ static int init_named_socket(char *name, char **path_out) return -1; } +int ustcomm_send_request(struct ustcomm_connection *conn, char *req, char **reply) +{ + int result; + + result = send(conn->fd, req, strlen(req), 0); + if(result == -1) { + PERROR("send"); + return -1; + } + else if(result == 0) { + return 0; + } + + if(!reply) + return 1; + + *reply = (char *) malloc(MSG_MAX+1); + result = recv(conn->fd, *reply, MSG_MAX, 0); + if(result == -1) { + PERROR("recv"); + return -1; + } + else if(result == 0) { + return 0; + } + + (*reply)[result] = '\0'; + + return 1; +} + +int ustcomm_connect_path(char *path, struct ustcomm_connection *conn, pid_t signalpid) +{ + int fd; + int result; + struct sockaddr_un addr; + + result = fd = socket(PF_UNIX, SOCK_STREAM, 0); + if(result == -1) { + PERROR("socket"); + return -1; + } + + addr.sun_family = AF_UNIX; + + result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", path); + if(result >= UNIX_PATH_MAX) { + ERR("string overflow allocating socket name"); + return -1; + } + + if(signalpid >= 0) + signal_process(signalpid); + + result = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); + if(result == -1) { + PERROR("connect"); + return -1; + } + + conn->fd = fd; + + return 0; +} + +int ustcomm_disconnect(struct ustcomm_connection *conn) +{ + return close(conn->fd); +} + +int ustcomm_connect_app(pid_t pid, struct ustcomm_connection *conn) +{ + 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; + } + + return ustcomm_connect_path(path, conn, pid); +} + +int ustcomm_disconnect_app(struct ustcomm_connection *conn) +{ + close(conn->fd); + return 0; +} + int ustcomm_init_app(pid_t pid, struct ustcomm_app *handle) { int result; diff --git a/libustcomm/ustcomm.h b/libustcomm/ustcomm.h index 0de4ad46..7d22694c 100644 --- a/libustcomm/ustcomm.h +++ b/libustcomm/ustcomm.h @@ -36,7 +36,7 @@ struct ustcomm_source { char *strdup_malloc(const char *s); -int send_message(pid_t pid, const char *msg, char **reply); +int send_message_pid(pid_t pid, const char *msg, char **reply); int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout); int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout); diff --git a/ustd/ustd.c b/ustd/ustd.c index 830d5e26..b66f1ced 100644 --- a/ustd/ustd.c +++ b/ustd/ustd.c @@ -35,6 +35,7 @@ struct list_head buffers = LIST_HEAD_INIT(buffers); struct buffer_info { char *name; pid_t pid; + struct ustcomm_connection conn; int shmid; void *mem; @@ -207,9 +208,16 @@ int add_buffer(pid_t pid, char *bufname) buf->name = bufname; buf->pid = pid; + /* connect to app */ + result = ustcomm_connect_app(buf->pid, &buf->conn); + if(result) { + ERR("unable to connect to process"); + return -1; + } + /* get shmid */ asprintf(&send_msg, "get_shmid %s", buf->name); - send_message(pid, send_msg, &received_msg); + ustcomm_send_request(&buf->conn, send_msg, &received_msg); free(send_msg); DBG("got buffer name %s", buf->name); @@ -223,7 +231,7 @@ int add_buffer(pid_t pid, char *bufname) /* get n_subbufs */ asprintf(&send_msg, "get_n_subbufs %s", buf->name); - send_message(pid, send_msg, &received_msg); + ustcomm_send_request(&buf->conn, send_msg, &received_msg); free(send_msg); result = sscanf(received_msg, "%d", &buf->n_subbufs); @@ -236,7 +244,7 @@ int add_buffer(pid_t pid, char *bufname) /* get subbuf size */ asprintf(&send_msg, "get_subbuf_size %s", buf->name); - send_message(pid, send_msg, &received_msg); + ustcomm_send_request(&buf->conn, send_msg, &received_msg); free(send_msg); result = sscanf(received_msg, "%d", &buf->subbuf_size);