FlamesRunner / shellinabox

Automatically exported from code.google.com/p/shellinabox
Other
0 stars 1 forks source link

Daemonizing shellinaboxd will keep filedescriptors open #213

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I am using shellinabox on an embedded device which uses a simple "cgi-GUI" to 
its "services". The daemons are started and then a "status" is displayed on a 
cgi-page.

Starting shellinabox as a daemon will continue to load this page "for ever", so 
the webserver seemed to be waiting for an open filedescriptor to close. You 
will see open fds after starting shellinaboxd with "-b" option.

What steps will reproduce the problem?
1. running "shellinaboxd -b"  
2. running "lsof" will show open fd's 
3.

What is the expected output? What do you see instead?

running "lsof" will show open filedescriptors to pty's, so from my point of 
view shellinabox is not "fully daemonized". (Note, only the fork in 
shellinaboxd.c shows this, the other forks [from "launcher.c"] seem to 
close/redirect them correctly [to /dev/null]):

/var/tmp # shellinaboxd -b --pidfile=/var/run/shellinaboxd.pid -c 
/tmp/shellinabox -p 4200 -u nobody
/var/tmp # lsof -n | grep shellin
shellinab 16832  nobody  cwd       DIR       0,10     1580     57 /var/tmp
shellinab 16832  nobody  rtd       DIR       31,0      213    201 /
shellinab 16832  nobody  txt       REG       31,0  1215808   1399 
/usr/bin/shellinaboxd
shellinab 16832  nobody    0u      CHR      136,0      0t0      2 /dev/pts/0
shellinab 16832  nobody    1u      CHR      136,0      0t0      2 /dev/pts/0
shellinab 16832  nobody    2u      CHR      136,0      0t0      2 /dev/pts/0
shellinab 16832  nobody    3u     unix 0x8493f240      0t0 238327 socket
shellinab 16832  nobody    4u     IPv4     238338      0t0    TCP *:4200 
(LISTEN)
shellinab 16833  nobody  cwd       DIR       0,10     1580     57 /var/tmp
shellinab 16833  nobody  rtd       DIR       31,0      213    201 /
shellinab 16833  nobody  txt       REG       31,0  1215808   1399 
/usr/bin/shellinaboxd
shellinab 16833  nobody    0u      CHR        1,3      0t0    154 /dev/null
shellinab 16833  nobody    1u      CHR        1,3      0t0    154 /dev/null
shellinab 16833  nobody    2u      CHR      136,0      0t0      2 /dev/pts/0
shellinab 16833  nobody    4u     unix 0x86b6ede0      0t0 238328 socket
/var/tmp # 

Using "daemon()" of glibc or ulibc instead (see patch below), shows the 
expected behavior:

/var/tmp # killall shellinaboxd
/var/tmp # /tmp/shellinaboxd -b --pidfile=/var/run/shellinaboxd.pid -c 
/tmp/shellinabox -p 4200 -u nobody
/var/tmp # lsof -n | grep shellin
shellinab 17315  nobody  cwd       DIR       31,0      213    201 /
shellinab 17315  nobody  rtd       DIR       31,0      213    201 /
shellinab 17315  nobody  txt       REG       0,10  1217952 183496 
/var/tmp/shellinaboxd
shellinab 17315  nobody    0u      CHR        1,3      0t0    154 /dev/null
shellinab 17315  nobody    1u      CHR        1,3      0t0    154 /dev/null
shellinab 17315  nobody    2u      CHR        1,3      0t0    154 /dev/null
shellinab 17315  nobody    3u     unix 0x8493f240      0t0 238696 socket
shellinab 17315  nobody    4u     IPv4     238707      0t0    TCP *:4200 
(LISTEN)
shellinab 17316  nobody  cwd       DIR       31,0      213    201 /
shellinab 17316  nobody  rtd       DIR       31,0      213    201 /
shellinab 17316  nobody  txt       REG       0,10  1217952 183496 
/var/tmp/shellinaboxd
shellinab 17316  nobody    0u      CHR        1,3      0t0    154 /dev/null
shellinab 17316  nobody    1u      CHR        1,3      0t0    154 /dev/null
shellinab 17316  nobody    2u      CHR        1,3      0t0    154 /dev/null
shellinab 17316  nobody    4u     unix 0x84aa8e00      0t0 238697 socket
/var/tmp # 

What version of the product are you using? On what operating system?

I'm using version shellinabox-2.14 (though it shows "ShellInABox version 2.10 
(revision 239)") on a mips-linux device.
Exactly the same behaviour on my Ubuntu 12.10 Linux System.

Please provide any additional information below.

I only found "the problem", all credits for the "solution" go to Oliver Metz. I 
used this simple patch of him to get the "corrected" output:

--- shellinabox/shellinaboxd.c.orig 2012-04-21 19:30:44.000000000 +0200
+++ shellinabox/shellinaboxd.c  2012-12-27 18:45:33.000000000 +0100
@@ -1159,12 +1159,9 @@
   }

   if (demonize) {
-    pid_t pid;
-    check((pid             = fork()) >= 0);
-    if (pid) {
-      _exit(0);
+    if (daemon(0, 0) < 0) {
+         fatal("Failed to daemonize: %s", strerror(errno));
     }
-    setsid();
   }
   if (pidfile) {
 #ifndef O_LARGEFILE 

Original issue reported on code.google.com by joerg.ei...@googlemail.com on 28 Dec 2012 at 1:09