improve error handling
[ust.git] / libustcomm / ustcomm.c
index e773f7189fe313f78f0704f1ad907730a715aa38..2e490b56b7f3b9bde99a3db8ade8b66f99f090da 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/un.h>
 #include <unistd.h>
 #include <poll.h>
+#include <sys/stat.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -378,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;
@@ -390,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 != EPIPE)
+                       PERROR("send");
                return -1;
        }
 
@@ -406,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) {
@@ -494,7 +506,7 @@ int ustcomm_init_app(pid_t pid, struct ustcomm_app *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 (%s). Check that directory exists and that it is writable.", name);
                goto free_name;
        }
        free(name);
@@ -539,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 == ' ') {
This page took 0.023458 seconds and 4 git commands to generate.