X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=libustcomm%2Fustcomm.c;h=225d1990be042a18cf0cde7c297e92c61b48c386;hb=c39c72ee5890c7727ae2697f321ba1b3d6c862f5;hp=4fe46c537d880d7f9cb27dd13d73c1a660343a3e;hpb=3a7b90de71f2a82f73f06fb14a7b77805aea1064;p=ust.git diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 4fe46c5..225d199 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -1,3 +1,20 @@ +/* Copyright (C) 2009 Pierre-Marc Fournier + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + #define _GNU_SOURCE #include #include @@ -49,20 +66,23 @@ char *strdup_malloc(const char *s) return retval; } -static void signal_process(pid_t pid) +static int signal_process(pid_t pid) { int result; result = kill(pid, UST_SIGNAL); if(result == -1) { PERROR("kill"); - return; + return -1; } - sleep(1); + /* FIXME: should wait in a better way */ + //sleep(1); + + return 0; } -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 +95,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; @@ -113,8 +132,13 @@ int send_message_path(const char *path, const char *msg, char **reply, int signa return -1; } - if(signalpid >= 0) - signal_process(signalpid); + if(signalpid >= 0) { + result = signal_process(signalpid); + if(result == -1) { + ERR("could not signal process"); + return -1; + } + } result = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if(result == -1) { @@ -122,30 +146,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 +187,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 +224,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 +414,99 @@ 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; + } + + 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) { + result = signal_process(signalpid); + if(result == -1) { + ERR("could not signal process"); + return -1; + } + } + + 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; @@ -430,7 +547,7 @@ int ustcomm_init_ustd(struct ustcomm_ustd *handle) handle->server.listen_fd = init_named_socket(name, &handle->server.socketpath); if(handle->server.listen_fd < 0) { - ERR("error initializing named socket"); + ERR("error initializing named socket at %s", name); goto free_name; } free(name);