ustcomm: move function to destroy app socket to ustcomm
[ust.git] / libustcomm / ustcomm.c
index 1f4bce95cbc4a7f726c1d612ca28e5cb130d42d2..3b53471ac6d69c81acd1379351d985b9b8ed177b 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>
@@ -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;
@@ -522,6 +539,32 @@ free_name:
        return retval;
 }
 
+void ustcomm_fini_app(struct ustcomm_app *handle)
+{
+       int result;
+       struct stat st;
+
+       /* Destroy socket */
+       ERR("socket path is: %s", handle->server.socketpath);
+       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.022753 seconds and 4 git commands to generate.