[patch] Re: smbmount problem

Paul PELaufer at csupomona.edu
Mon Feb 1 00:18:32 GMT 1999


I have put together a patch that should correct both the autofs race and
the problem with the kernel getting the wrong pid. This has nothing to do
with glibc's uid_t being 32 bits and the kernel's being 16.

This patch changes smbmount's behavior so that it first daemonizes, then
the child attempts the final ioctl to the kernel to complete the mount.
The parent does a "waitpid" on the child, and catches SIGTERM. If the
child finishes the mount without a problem then the parent is killed with
"SIGTERM" and exits with a return value of 0. If the child has a problem
with the final ioctl, then it exits, the parent catches this, and returns
with a return value of 1, signaling an error (consistant with the rest of
smbmount).

Result: It works with autofs (tested - no more race), and the kernel gets
the proper PID of the process handling it. Yay.

Paul Laufer
-------------- next part --------------
diff -urN samba-2.0.0.orig/source/client/smbmount.c samba-2.0.0/source/client/smbmount.c
--- samba-2.0.0.orig/source/client/smbmount.c	Mon Dec 14 17:21:01 1998
+++ samba-2.0.0/source/client/smbmount.c	Sun Jan 31 16:13:00 1999
@@ -182,19 +182,27 @@
 }
 
 static void
+term_handler(int i)
+{
+	exit(0);
+}
+
+static void
 daemonize(void)
 {
-	int i;
+	int i, *status;
 	if ((i = fork()) < 0)
 	{
 		DEBUG(0, ("could not fork\n"));
 	}
 	if (i > 0)
 	{
-		/* parent simply exits */
-		exit(0);
+		/* parent waits for child to mount, and returns
+		 * 1 if something went wrong, 0 if everything ok (PEL) */
+		CatchSignal(SIGTERM, &term_handler);
+		waitpid(i, status, 0);
+		exit(1);
 	}
-	setsid();
 	chdir("/");
 }
 
@@ -256,6 +264,7 @@
 {
 	int fd, closed = 0, res = 1;
 	int first_time = 1;
+	pid_t ppid;
 
 	while (1)
 	{
@@ -272,12 +281,30 @@
 		conn_options.fd = -1;
 		if (res)
 			conn_options.fd = Client;
+		if( first_time ) {
+	/*
+	 * Create the background process after trying the mount.
+	 * to avoid race conditions with automount and other processes.
+	 *
+	 * But if we do that then the kernel gets the wrong PID of the process
+	 * handling the connection. Thus, daemonize first, mount second, and kill
+	 * parent third. (PEL)
+	 */
+			ppid=getpid();
+			first_time = 0;
+			daemonize();
+		}
+
 		res = ioctl(fd, SMB_IOC_NEWCONN, &conn_options);
 		if (res != 0)
 		{
 			DEBUG(0, ("smbmount: ioctl failed, res=%d\n", res));
+			exit(1);
 		}
 
+		kill(ppid, SIGTERM);
+		setsid();
+
 		close_sockets();
 		close(fd);
 		/*
@@ -290,15 +317,6 @@
 			close_our_files();
 		}
 #endif
-
-		if( first_time ) {
-	/*
-	 * Create the background process after trying the mount.
-	 * to avoid race conditions with automount and other processes.
-	 */
-			first_time = 0;
-			daemonize();
-		}
 
 		/*
 		 * Wait for a signal from smbfs ...


More information about the samba-technical mailing list