aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2004-07-15 17:52:38 +0000
committerH. Peter Anvin <hpa@zytor.com>2004-07-15 17:52:38 +0000
commit23c7c39252a4ca004f7826898d77dec98e705852 (patch)
treea72ccb6d0fa9d1072ccb882532245c492e122656
parentcf47fd9038ee73c81d243a5cf9d0501ab119e6ea (diff)
downloadklibc-23c7c39252a4ca004f7826898d77dec98e705852.tar.gz
Add daemon() helper functionklibc-0.152
-rw-r--r--include/unistd.h2
-rw-r--r--klibc/Makefile1
-rw-r--r--klibc/daemon.c37
-rw-r--r--klibc/include/unistd.h2
4 files changed, 42 insertions, 0 deletions
diff --git a/include/unistd.h b/include/unistd.h
index c5ded91756757..d80ddbf99e563 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -129,6 +129,8 @@ static __inline__ int __getpageshift(void) {
return __page_shift;
}
+__extern int daemon(int, int);
+
/* Standard file descriptor numbers. */
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
diff --git a/klibc/Makefile b/klibc/Makefile
index 9238a22cfcbc3..f9a4032fc2dc5 100644
--- a/klibc/Makefile
+++ b/klibc/Makefile
@@ -18,6 +18,7 @@ LIBOBJS = vsnprintf.o snprintf.o vsprintf.o sprintf.o \
globals.o exitc.o atexit.o onexit.o \
execl.o execle.o execv.o execvpe.o execvp.o execlp.o execlpe.o \
fork.o wait.o wait3.o waitpid.o system.o setpgrp.o getpgrp.o \
+ daemon.o \
printf.o vprintf.o fprintf.o vfprintf.o perror.o \
statfs.o fstatfs.o \
open.o fopen.o fread.o fread2.o fgetc.o fgets.o \
diff --git a/klibc/daemon.c b/klibc/daemon.c
new file mode 100644
index 0000000000000..7510b84a9932d
--- /dev/null
+++ b/klibc/daemon.c
@@ -0,0 +1,37 @@
+/*
+ * daemon.c - "daemonize" a process
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int daemon(int nochdir, int noclose)
+{
+ int nullfd;
+ pid_t f;
+
+ if ( !nochdir ) {
+ if ( chdir("/") )
+ return -1;
+ }
+
+ if ( !noclose ) {
+ if ( (nullfd = open("/dev/null", O_RDWR)) < 0 ||
+ dup2(nullfd, 0) ||
+ dup2(nullfd, 1) ||
+ dup2(nullfd, 2) )
+ return -1;
+ }
+
+ f = fork();
+ if ( f < 0 )
+ return -1;
+ else if ( f > 0 )
+ _exit(0);
+
+
+ return setsid();
+}
+
+
diff --git a/klibc/include/unistd.h b/klibc/include/unistd.h
index c5ded91756757..d80ddbf99e563 100644
--- a/klibc/include/unistd.h
+++ b/klibc/include/unistd.h
@@ -129,6 +129,8 @@ static __inline__ int __getpageshift(void) {
return __page_shift;
}
+__extern int daemon(int, int);
+
/* Standard file descriptor numbers. */
#define STDIN_FILENO 0
#define STDOUT_FILENO 1