diff options
author | Brendan Taylor <whateley@gmail.com> | 2011-09-13 09:13:33 +0000 |
---|---|---|
committer | Brendan Taylor <whateley@gmail.com> | 2011-09-13 09:13:33 +0000 |
commit | 8b87065fef2a29a6434c97af1c6311f396a3f7fc (patch) | |
tree | 60965600449d5b2e4c8aebddab58e8a712be8ad4 /src | |
parent | a2ffff8fda0ea395bc5713880263b4626ad2a4e8 (diff) |
delete stale sockets instead of reattaching
Diffstat (limited to 'src')
-rw-r--r-- | src/io.c | 36 |
1 files changed, 10 insertions, 26 deletions
@@ -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); |