Module Name:    src
Committed By:   pooka
Date:           Sun Dec 12 17:58:28 UTC 2010

Modified Files:
        src/lib/librumpuser: rumpuser_sp.c sp_common.c

Log Message:
Unlink unix socket as part of server exit.
(whatever happened to the code that was supposed to do it automatically
when the binding process exits?)


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/lib/librumpuser/rumpuser_sp.c
cvs rdiff -u -r1.14 -r1.15 src/lib/librumpuser/sp_common.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/librumpuser/rumpuser_sp.c
diff -u src/lib/librumpuser/rumpuser_sp.c:1.24 src/lib/librumpuser/rumpuser_sp.c:1.25
--- src/lib/librumpuser/rumpuser_sp.c:1.24	Sun Dec 12 17:10:36 2010
+++ src/lib/librumpuser/rumpuser_sp.c	Sun Dec 12 17:58:28 2010
@@ -1,4 +1,4 @@
-/*      $NetBSD: rumpuser_sp.c,v 1.24 2010/12/12 17:10:36 pooka Exp $	*/
+/*      $NetBSD: rumpuser_sp.c,v 1.25 2010/12/12 17:58:28 pooka Exp $	*/
 
 /*
  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: rumpuser_sp.c,v 1.24 2010/12/12 17:10:36 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_sp.c,v 1.25 2010/12/12 17:58:28 pooka Exp $");
 
 #include <sys/types.h>
 #include <sys/atomic.h>
@@ -806,6 +806,8 @@
 	return NULL;
 }
 
+static unsigned cleanupidx;
+static struct sockaddr *cleanupsa;
 int
 rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
 {
@@ -838,6 +840,9 @@
 	sarg->sps_sock = s;
 	sarg->sps_connhook = parsetab[idx].connhook;
 
+	cleanupidx = idx;
+	cleanupsa = sap;
+
 	/* sloppy error recovery */
 
 	/*LINTED*/
@@ -845,6 +850,7 @@
 		fprintf(stderr, "rump_sp: server bind failed\n");
 		return errno;
 	}
+
 	if (listen(s, MAXCLI) == -1) {
 		fprintf(stderr, "rump_sp: server listen failed\n");
 		return errno;
@@ -864,6 +870,7 @@
 {
 
 	if (spclist[0].spc_fd) {
+		parsetab[cleanupidx].cleanup(cleanupsa);
 		shutdown(spclist[0].spc_fd, SHUT_RDWR);
 		spfini = 1;
 	}

Index: src/lib/librumpuser/sp_common.c
diff -u src/lib/librumpuser/sp_common.c:1.14 src/lib/librumpuser/sp_common.c:1.15
--- src/lib/librumpuser/sp_common.c:1.14	Tue Nov 30 20:33:43 2010
+++ src/lib/librumpuser/sp_common.c	Sun Dec 12 17:58:28 2010
@@ -1,4 +1,4 @@
-/*      $NetBSD: sp_common.c,v 1.14 2010/11/30 20:33:43 pooka Exp $	*/
+/*      $NetBSD: sp_common.c,v 1.15 2010/12/12 17:58:28 pooka Exp $	*/
 
 /*
  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
@@ -36,6 +36,7 @@
 #include <sys/queue.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <sys/syslimits.h>
 
 #include <arpa/inet.h>
 #include <netinet/in.h>
@@ -156,6 +157,7 @@
 
 typedef int (*addrparse_fn)(const char *, struct sockaddr **, int);
 typedef int (*connecthook_fn)(int);
+typedef void (*cleanup_fn)(struct sockaddr *);
 
 static int readframe(struct spclient *);
 static void handlereq(struct spclient *);
@@ -571,12 +573,29 @@
 
 	/*
 	 * The pathname can be all kinds of spaghetti elementals,
-	 * so meek and obidient we accept everything.
+	 * so meek and obidient we accept everything.  However, use
+	 * full path for easy cleanup in case someone gives a relative
+	 * one and the server does a chdir() between now than the
+	 * cleanup.
 	 */
 	memset(&sun, 0, sizeof(sun));
 	sun.sun_family = AF_LOCAL;
-	strlcpy(sun.sun_path, addr, sizeof(sun.sun_path));
-	sun.sun_len = slen = SUN_LEN(&sun);
+	if (*addr != '/') {
+		char mywd[PATH_MAX];
+
+		if (getcwd(mywd, sizeof(mywd)) == NULL) {
+			fprintf(stderr, "warning: cannot determine cwd, "
+			    "omitting socket cleanup\n");
+		} else {
+			if (strlen(addr) + strlen(mywd) > sizeof(sun.sun_path))
+				return ENAMETOOLONG;
+			strlcpy(sun.sun_path, mywd, sizeof(sun.sun_path));
+			strlcat(sun.sun_path, "/", sizeof(sun.sun_path));
+		}
+	}
+	strlcat(sun.sun_path, addr, sizeof(sun.sun_path));
+	sun.sun_len = SUN_LEN(&sun);
+	slen = sun.sun_len+1; /* get the 0 too */
 
 	*sa = malloc(slen);
 	if (*sa == NULL)
@@ -586,6 +605,19 @@
 	return 0;
 }
 
+static void
+unix_cleanup(struct sockaddr *sa)
+{
+	struct sockaddr_un *sun = (void *)sa;
+
+	/*
+	 * cleanup only absolute paths.  see unix_parse() above
+	 */
+	if (*sun->sun_path == '/') {
+		unlink(sun->sun_path);
+	}
+}
+
 /*ARGSUSED*/
 static int
 notsupp(void)
@@ -607,10 +639,12 @@
 	int domain;
 	addrparse_fn ap;
 	connecthook_fn connhook;
+	cleanup_fn cleanup;
 } parsetab[] = {
-	{ "tcp", PF_INET, tcp_parse, tcp_connecthook },
-	{ "unix", PF_LOCAL, unix_parse, (connecthook_fn)success },
-	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success },
+	{ "tcp", PF_INET, tcp_parse, tcp_connecthook, (cleanup_fn)success },
+	{ "unix", PF_LOCAL, unix_parse, (connecthook_fn)success, unix_cleanup },
+	{ "tcp6", PF_INET6, (addrparse_fn)notsupp, (connecthook_fn)success,
+			    (cleanup_fn)success },
 };
 #define NPARSE (sizeof(parsetab)/sizeof(parsetab[0]))
 

Reply via email to