X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=libustcomm%2Fustcomm.c;h=4fe46c537d880d7f9cb27dd13d73c1a660343a3e;hb=3a7b90de71f2a82f73f06fb14a7b77805aea1064;hp=e445ed96b55339a52f98bd3c98238bd4bc490206;hpb=811e4b93ba6acb72226a9243ae9a525f444e7e80;p=lttng-ust.git diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index e445ed96..4fe46c53 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -35,6 +35,20 @@ // backtrace_symbols_fd(buffer, result, STDERR_FILENO); //} +char *strdup_malloc(const char *s) +{ + char *retval; + + if(s == NULL) + return NULL; + + retval = (char *) malloc(strlen(s)+1); + + strcpy(retval, s); + + return retval; +} + static void signal_process(pid_t pid) { int result; @@ -57,9 +71,12 @@ int send_message_fd(int fd, const char *msg, char **reply) PERROR("send"); return -1; } + else if(result == 0) { + return 0; + } if(!reply) - return 0; + return 1; *reply = (char *) malloc(MSG_MAX+1); result = recv(fd, *reply, MSG_MAX, 0); @@ -67,10 +84,13 @@ int send_message_fd(int fd, const char *msg, char **reply) PERROR("recv"); return -1; } + else if(result == 0) { + return 0; + } (*reply)[result] = '\0'; - return 0; + return 1; } int send_message_path(const char *path, const char *msg, char **reply, int signalpid) @@ -149,7 +169,10 @@ int ustcomm_request_consumer(pid_t pid, const char *channel) return 0; } - +/* returns 1 to indicate a message was received + * returns 0 to indicate no message was received (cannot happen) + * returns -1 to indicate an error + */ static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src) { @@ -159,7 +182,7 @@ static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src) result = recv(fd, *msg, MSG_MAX, 0); if(result == -1) { - PERROR("recvfrom"); + PERROR("recv"); return -1; } @@ -170,7 +193,7 @@ static int recv_message_fd(int fd, char **msg, struct ustcomm_source *src) if(src) src->fd = fd; - return 0; + return 1; } int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_source *src) @@ -178,7 +201,7 @@ int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_ int result; result = send_message_fd(src->fd, msg, NULL); - if(result) { + if(result < 0) { ERR("error in send_message_fd"); return -1; } @@ -186,7 +209,14 @@ int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_ return 0; } -int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src) +/* @timeout: max blocking time in milliseconds, -1 means infinity + * + * returns 1 to indicate a message was received + * returns 0 to indicate no message was received + * returns -1 to indicate an error + */ + +int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustcomm_source *src, int timeout) { struct pollfd *fds; struct ustcomm_connection *conn; @@ -218,12 +248,15 @@ int ustcomm_recv_message(struct ustcomm_server *server, char **msg, struct ustco idx++; } - result = poll(fds, n_fds, -1); + result = poll(fds, n_fds, timeout); if(result == -1) { PERROR("poll"); return -1; } + if(result == 0) + return 0; + if(fds[0].revents) { struct ustcomm_connection *newconn; int newfd; @@ -273,14 +306,34 @@ free_fds_return: return retval; } -int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src) +int ustcomm_ustd_recv_message(struct ustcomm_ustd *ustd, char **msg, struct ustcomm_source *src, int timeout) { - return ustcomm_recv_message(&ustd->server, msg, src); + return ustcomm_recv_message(&ustd->server, msg, src, timeout); } -int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src) +int ustcomm_app_recv_message(struct ustcomm_app *app, char **msg, struct ustcomm_source *src, int timeout) { - return ustcomm_recv_message(&app->server, msg, src); + return ustcomm_recv_message(&app->server, msg, src, timeout); +} + +/* This removes src from the list of active connections of app. + */ + +int ustcomm_app_detach_client(struct ustcomm_app *app, struct ustcomm_source *src) +{ + struct ustcomm_server *server = (struct ustcomm_server *)app; + struct ustcomm_connection *conn; + + list_for_each_entry(conn, &server->connections, list) { + if(conn->fd == src->fd) { + list_del(&conn->list); + goto found; + } + } + + return -1; +found: + return src->fd; } static int init_named_socket(char *name, char **path_out)