summaryrefslogtreecommitdiffhomepage
path: root/libbb/xfuncs.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-14 01:29:06 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-14 01:29:06 +0000
commit7a431b3715299854fb423ec00d5fafc0e2c7f07b (patch)
tree4e90c9d364485ef13c2e429ab22b9b925d50ea04 /libbb/xfuncs.c
parent150f402b36197d822f8a7dd835231cd67b77e959 (diff)
By popular request reinstate fakeidentd's standalone mode.
Since this is also needed for other applets like telnetd, introduce generic driver for such things. It even supports inetd-wait ('linger') mode, when inetd hands out listen socket to child and waits to it to die, instead of handing out accepted socket and continuing listening itself (nowait mode). Code growth ~200 bytes. NB: our inetd doesn't support wait mode yet (or mabe it is buggy).
Diffstat (limited to 'libbb/xfuncs.c')
-rw-r--r--libbb/xfuncs.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 207537929..6a6bdced3 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -509,6 +509,30 @@ void xdaemon(int nochdir, int noclose)
}
#endif
+void bb_sanitize_stdio(int daemonize)
+{
+ int fd;
+ /* Mega-paranoid */
+ fd = xopen(bb_dev_null, O_RDWR);
+ while (fd < 2)
+ fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
+ if (daemonize) {
+ pid_t pid = fork();
+ if (pid < 0) /* wtf? */
+ bb_perror_msg_and_die("fork");
+ if (pid) /* parent */
+ exit(0);
+ /* child */
+ setsid();
+ /* if daemonizing, make sure we detach from stdio */
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ }
+ while (fd > 2)
+ close(fd--); /* close everything after fd#2 */
+}
+
// Die with an error message if we can't open a new socket.
int xsocket(int domain, int type, int protocol)
{