[patch] khttpd doesn't detach from the files of its parent

From: Alessandro Rubini (rubini@linux.it)
Date: Wed Oct 18 2000 - 12:32:50 EST


Hi all.

While looking at kHTTPd (linux-2.4.0-test9) I found what looks like a
bug to me.

The daemon doesn't detach itself from the files structure of the
parent process. Therefore, when it is run as a module, the files
opened by "insmod" (or whatever loads it) remain open.

Besides "aesthetical" issues, this can be a problem: if a script loads
a module redirecting stderr to a file, the filesystem won't be
unmountable before the module is removed. (I reproduced that).

The proposed patch is trivial, so I may miss some detail.

A sample session follows, then the patch.

/alessandro

rudo.root# ps aux | grep http
root 1828 0.0 0.0 0 0 ? SW 12:57 0:00 [khttpd manager]
root 1848 0.0 0.0 0 0 ? SW 12:58 0:00 [khttpd - 0]
root 1849 0.0 0.0 0 0 ? SW 12:58 0:00 [khttpd - 1]
rudo.root# ls -l /proc/1828/fd
total 48
lrwx------ 1 root root 64 Oct 18 13:17 0 -> /dev/ttyp1
lrwx------ 1 root root 64 Oct 18 13:17 1 -> /dev/ttyp1
lrwx------ 1 root root 64 Oct 18 13:17 2 -> /dev/ttyp1
rudo.root# ls -l /proc/1848/fd
total 48
lrwx------ 1 root root 64 Oct 18 13:17 0 -> /dev/ttyp1
lrwx------ 1 root root 64 Oct 18 13:17 1 -> /dev/ttyp1
lrwx------ 1 root root 64 Oct 18 13:17 2 -> /dev/ttyp1

        then, I tried "insmod khttpd < /dev/ttyS0".

rudo.root# ps aux | grep http
root 2031 0.0 0.0 0 0 ttypc SW 13:22 0:00 [khttpd manager]
root 2034 0.0 0.0 0 0 ttypc SW 13:22 0:00 [khttpd - 0]
root 2035 0.0 0.0 0 0 ttypc SW 13:22 0:00 [khttpd - 1]
rudo.root# ls -l /proc/2034/fd
total 27
lr-x------ 1 root root 64 Oct 18 13:22 0 -> /dev/ttyS0
lrwx------ 1 root root 64 Oct 18 13:22 1 -> /dev/ttypc
lrwx------ 1 root root 64 Oct 18 13:22 2 -> /dev/ttypc

        after applying the patch included below, no file remains open

rudo.root# ps aux | grep http
root 2019 0.0 0.0 0 0 ttypc SW 13:20 0:00 [khttpd manager]
root 2020 0.0 0.0 0 0 ttypc SW 13:20 0:00 [khttpd - 0]
root 2021 0.0 0.0 0 0 ttypc SW 13:20 0:00 [khttpd - 1]
rudo.root# ls -l /proc/2020/fd
total 0
rudo.root#

--- ./net/khttpd/main.c.orig Wed Oct 18 13:01:29 2000
+++ ./net/khttpd/main.c Wed Oct 18 19:13:01 2000
@@ -195,6 +195,7 @@
 {
         sigset_t tmpsig;
         int waitpid_result;
+ int i;
         
         DECLARE_WAIT_QUEUE_HEAD(WQ);
         
@@ -203,6 +204,10 @@
         lock_kernel(); /* This seems to be required for exit_mm */
         exit_mm(current);
         
+ /* init_module has stdin/stdout/stderr open: close them (ARub) */
+ for (i=255; i>=0; i--)
+ if (current->files->fd[i])
+ close(i);
 
         /* Block all signals except SIGKILL and SIGSTOP */
         spin_lock_irq(&current->sigmask_lock);
@@ -383,7 +388,7 @@
 
         StartSysctl();
         
- (void)kernel_thread(ManagementDaemon,NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
+ (void)kernel_thread(ManagementDaemon,NULL, CLONE_FS | CLONE_SIGHAND);
         
         return 0;
 }
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Mon Oct 23 2000 - 21:00:13 EST