summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed T. Crigler <crigler@users.sourceforge.net>2006-09-27 23:56:29 +0000
committerNed T. Crigler <crigler@users.sourceforge.net>2006-09-27 23:56:29 +0000
commit5dbd8fe920992ff576b89fe0ff7bd104a6b05191 (patch)
tree74b0ce012d45b5e57102b7b3e114094825a3fc84
parente06f8dfcdf1dbf8ac6d12075c5da9f94a894f6a9 (diff)
Try to detect and remove stale sockets when dtach -A is used. Be
paranoid about this, and only remove the specified file if connect says the connection was refused and stat says the file is a socket. Also dtach -A now only tries to create the socket if the connection was refused or the socket did not exist, instead of on any random error as before.
-rw-r--r--attach.c12
-rw-r--r--dtach.h8
-rw-r--r--main.c9
3 files changed, 27 insertions, 2 deletions
diff --git a/attach.c b/attach.c
index ece56db..9db3882 100644
--- a/attach.c
+++ b/attach.c
@@ -60,6 +60,18 @@ connect_socket(char *name)
if (connect(s, (struct sockaddr*)&sockun, sizeof(sockun)) < 0)
{
close(s);
+
+ /* ECONNREFUSED is also returned for regular files, so make
+ ** sure we are trying to connect to a socket. */
+ if (errno == ECONNREFUSED)
+ {
+ struct stat st;
+
+ if (stat(name, &st) < 0)
+ return -1;
+ else if (!S_ISSOCK(st.st_mode) || S_ISREG(st.st_mode))
+ errno = ENOTSOCK;
+ }
return -1;
}
return s;
diff --git a/dtach.h b/dtach.h
index c7377df..0e6ba9c 100644
--- a/dtach.h
+++ b/dtach.h
@@ -73,6 +73,14 @@
#include <sys/socket.h>
#include <sys/un.h>
+#ifndef S_ISREG
+#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
+#endif
+
+#ifndef S_ISSOCK
+#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
+#endif
+
extern char *progname, *sockname;
extern int detach_char, no_suspend, redraw_method;
extern struct termios orig_term;
diff --git a/main.c b/main.c
index 6687c16..0064450 100644
--- a/main.c
+++ b/main.c
@@ -256,8 +256,13 @@ main(int argc, char **argv)
** socket. */
if (attach_main(1) != 0)
{
- if (master_main(argv, 1) != 0)
- return 1;
+ if (errno == ECONNREFUSED || errno == ENOENT)
+ {
+ if (errno == ECONNREFUSED)
+ unlink(sockname);
+ if (master_main(argv, 1) != 0)
+ return 1;
+ }
return attach_main(0);
}
}