Commit 29952618 authored by Pavel Emelyanov's avatar Pavel Emelyanov

daemon: Write own daemon routine

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: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 040fe771
...@@ -271,6 +271,7 @@ extern void shfree_last(void *ptr); ...@@ -271,6 +271,7 @@ extern void shfree_last(void *ptr);
extern int run_scripts(char *action); extern int run_scripts(char *action);
extern int cr_system(int in, int out, int err, char *cmd, char *const argv[]); 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); extern int is_root_user(void);
static inline bool dir_dots(struct dirent *de) static inline bool dir_dots(struct dirent *de)
......
...@@ -572,6 +572,35 @@ out: ...@@ -572,6 +572,35 @@ out:
return ret; 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() int is_root_user()
{ {
if (geteuid() != 0) { if (geteuid() != 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment