[CRIU] [PATCH 3/8] daemon: Write own daemon routine
Pavel Emelyanov
xemul at parallels.com
Tue Jan 28 10:35:59 PST 2014
RPC will start page-server daemon and needs to get the
controll back to report back to caller, but the glibc's
daemon() does exit() in parent context preventing it.
Thus -- introduce own daemonizing routine.
Strictly speaking, this is not pure daemon() clone, as the
parent process has to exit himself. But this is OK for now.
Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
include/util.h | 1 +
util.c | 29 +++++++++++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/include/util.h b/include/util.h
index c081921..8e2497a 100644
--- a/include/util.h
+++ b/include/util.h
@@ -272,6 +272,7 @@ extern void shfree_last(void *ptr);
extern int run_scripts(char *action);
extern int cr_system(int in, int out, int err, char *cmd, char *const argv[]);
+extern int cr_daemon(int nochdir, int noclose);
extern int is_root_user(void);
static inline bool dir_dots(struct dirent *de)
diff --git a/util.c b/util.c
index 4c57406..314cd63 100644
--- a/util.c
+++ b/util.c
@@ -581,6 +581,35 @@ out:
return ret;
}
+int cr_daemon(int nochdir, int noclose)
+{
+ int pid;
+
+ pid = fork();
+ if (pid < 0) {
+ pr_perror("Can't fork");
+ return -1;
+ }
+
+ if (pid > 0)
+ return pid;
+
+ setsid();
+ if (!nochdir)
+ chdir("/");
+ if (!noclose) {
+ int fd;
+
+ fd = open("/dev/null", O_RDWR);
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ close(fd);
+ }
+
+ return 0;
+}
+
int is_root_user()
{
if (geteuid() != 0) {
--
1.8.4.2
More information about the CRIU
mailing list