From 8b87065fef2a29a6434c97af1c6311f396a3f7fc Mon Sep 17 00:00:00 2001 From: Brendan Taylor Date: Tue, 13 Sep 2011 09:13:33 +0000 Subject: delete stale sockets instead of reattaching --- src/io.c | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/io.c b/src/io.c index 9dbfa3d..edeca68 100644 --- a/src/io.c +++ b/src/io.c @@ -84,11 +84,9 @@ init_fifo(gchar *dir) { /* return dir or, on error, free dir and return NULL */ gchar *path = build_stream_name(FIFO, dir); - if (file_exists(path)) { - /* something exists at that path. try to delete it. */ - if (unlink(path) == -1) - g_warning ("Fifo: Can't unlink old fifo at %s\n", path); - } + /* if something exists at that path, try to delete it. */ + if (file_exists(path) && unlink(path) == -1) + g_warning ("Fifo: Can't unlink old fifo at %s\n", path); if (mkfifo (path, 0666) == 0) { if(attach_fifo(path)) @@ -302,33 +300,19 @@ init_socket(gchar *dir) { /* return dir or, on error, free dir and return NULL * return NULL; } - struct sockaddr_un local; gchar *path = build_stream_name(SOCKET, dir); + /* if something exists at that path, try to delete it. */ + if(file_exists(path) && unlink(path) == -1) + g_warning ("Fifo: Can't unlink old fifo at %s\n", path); + + struct sockaddr_un local; local.sun_family = AF_UNIX; strcpy (local.sun_path, path); - if(!file_exists(path) && attach_socket(path, &local)) { - /* it's free for the taking. */ + if(attach_socket(path, &local)) { return dir; - } else { - /* see if anybody's listening on the socket path we want. */ - int sock = socket (AF_UNIX, SOCK_STREAM, 0); - if(connect(sock, (struct sockaddr *) &local, sizeof(local)) < 0) { - /* some error occurred, presumably nobody's listening. - * we can attach ourselves to it. */ - unlink(path); - if(attach_socket(path, &local)) - return dir; - else - g_warning("init_socket: can't attach to existing socket %s: %s\n", path, strerror(errno)); - } else { - /* somebody's there, we can't use that socket path. */ - close(sock); - /* whatever, this instance can live without a socket. */ - g_warning ("init_socket: can't create %s: socket exists and is occupied\n", path); - } - } + } else g_warning("init_socket: can't attach to %s: %s\n", path, strerror(errno)); /* if we got this far, there was an error; cleanup */ g_free(path); -- cgit v1.2.3