X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=libustcomm%2Fustcomm.c;h=8b459df32975499a4c1137ee379fb9d40600b231;hb=34b460e6b2180ef5e42cdc0455b3ac1a0afd912c;hp=1f4bce95cbc4a7f726c1d612ca28e5cb130d42d2;hpb=c97d443730716166051e0b6b95d26ab3faadf8dc;p=ust.git diff --git a/libustcomm/ustcomm.c b/libustcomm/ustcomm.c index 1f4bce9..8b459df 100644 --- a/libustcomm/ustcomm.c +++ b/libustcomm/ustcomm.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -191,6 +192,23 @@ int ustcomm_send_reply(struct ustcomm_server *server, char *msg, struct ustcomm_ return 0; } +/* Called after a fork. */ + +int ustcomm_close_all_connections(struct ustcomm_server *server) +{ + struct ustcomm_connection *conn; + struct ustcomm_connection *deletable_conn = NULL; + + list_for_each_entry(conn, &server->connections, list) { + free(deletable_conn); + deletable_conn = conn; + close(conn->fd); + list_del(&conn->list); + } + + return 0; +} + /* @timeout: max blocking time in milliseconds, -1 means infinity * * returns 1 to indicate a message was received @@ -361,8 +379,7 @@ static int init_named_socket(const char *name, char **path_out) } if(path_out) { - *path_out = ""; - *path_out = strdupa(addr.sun_path); + *path_out = strdup(addr.sun_path); } return fd; @@ -373,13 +390,24 @@ static int init_named_socket(const char *name, char **path_out) return -1; } +/* + * Return value: + * 0: Success, but no reply because recv() returned 0 + * 1: Success + * -1: Error + * + * On error, the error message is printed, except on + * ECONNRESET, which is normal when the application dies. + */ + int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char **reply) { int result; - result = send(conn->fd, req, strlen(req), 0); + result = send(conn->fd, req, strlen(req), MSG_NOSIGNAL); if(result == -1) { - PERROR("send"); + if(errno != ECONNRESET) + PERROR("send"); return -1; } @@ -389,7 +417,8 @@ int ustcomm_send_request(struct ustcomm_connection *conn, const char *req, char *reply = (char *) malloc(MSG_MAX+1); result = recv(conn->fd, *reply, MSG_MAX, 0); if(result == -1) { - PERROR("recv"); + if(errno != ECONNRESET) + PERROR("recv"); return -1; } else if(result == 0) { @@ -522,6 +551,31 @@ free_name: return retval; } +void ustcomm_fini_app(struct ustcomm_app *handle) +{ + int result; + struct stat st; + + /* Destroy socket */ + result = stat(handle->server.socketpath, &st); + if(result == -1) { + PERROR("stat (%s)", handle->server.socketpath); + return; + } + + /* Paranoid check before deleting. */ + result = S_ISSOCK(st.st_mode); + if(!result) { + ERR("The socket we are about to delete is not a socket."); + return; + } + + result = unlink(handle->server.socketpath); + if(result == -1) { + PERROR("unlink"); + } +} + static char *find_tok(char *str) { while(*str == ' ') {