Commit 4eb2872b authored by Ruslan Kuprieiev's avatar Ruslan Kuprieiev Committed by Pavel Emelyanov

v2 crtools: write pidfile, when service/page server is run as daemon and "--pidfile" is set

When service/page server becomes daemon, we may need to know it's pid.

Signed-off-by: Ruslan Kuprieiev<kupruser@gmail.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent c6b79bd3
...@@ -885,21 +885,6 @@ struct cr_clone_arg { ...@@ -885,21 +885,6 @@ struct cr_clone_arg {
CoreEntry *core; CoreEntry *core;
}; };
static void write_pidfile(char *pfname, int pid)
{
int fd;
fd = open(pfname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
if (fd == -1) {
pr_perror("Can't open %s", pfname);
kill(pid, SIGKILL);
return;
}
dprintf(fd, "%d", pid);
close(fd);
}
static inline int fork_with_pid(struct pstree_item *item) static inline int fork_with_pid(struct pstree_item *item)
{ {
int ret = -1, fd; int ret = -1, fd;
...@@ -971,8 +956,11 @@ static inline int fork_with_pid(struct pstree_item *item) ...@@ -971,8 +956,11 @@ static inline int fork_with_pid(struct pstree_item *item)
if (ca.clone_flags & CLONE_NEWPID) if (ca.clone_flags & CLONE_NEWPID)
item->pid.real = ret; item->pid.real = ret;
if (opts.pidfile && root_item == item) if (opts.pidfile && root_item == item) {
write_pidfile(opts.pidfile, ret); ret = write_pidfile(opts.pidfile, ret);
if (ret < 0)
pr_perror("Can't write pidfile");
}
err_unlock: err_unlock:
if (ca.fd >= 0) { if (ca.fd >= 0) {
......
...@@ -231,12 +231,19 @@ int cr_service(bool daemon_mode) ...@@ -231,12 +231,19 @@ int cr_service(bool daemon_mode)
} }
if (daemon_mode) { if (daemon_mode) {
if (daemon(0, 0) == -1) { if (daemon(1, 1) == -1) {
pr_perror("Can't run service server in the background"); pr_perror("Can't run service server in the background");
return -errno; return -errno;
} }
} }
if (opts.pidfile) {
if (write_pidfile(opts.pidfile, getpid()) == -1) {
pr_perror("Can't write pidfile");
return -1;
}
}
/* FIXME Do not ignore children's return values */ /* FIXME Do not ignore children's return values */
signal(SIGCHLD, SIG_IGN); signal(SIGCHLD, SIG_IGN);
......
...@@ -19,6 +19,8 @@ extern int vprint_num(char *buf, int blen, int num, char **ps); ...@@ -19,6 +19,8 @@ extern int vprint_num(char *buf, int blen, int num, char **ps);
extern void print_on_level(unsigned int loglevel, const char *format, ...) extern void print_on_level(unsigned int loglevel, const char *format, ...)
__attribute__ ((__format__ (__printf__, 2, 3))); __attribute__ ((__format__ (__printf__, 2, 3)));
extern int write_pidfile(char *pfname, int pid);
#ifndef LOG_PREFIX #ifndef LOG_PREFIX
# define LOG_PREFIX # define LOG_PREFIX
#endif #endif
......
...@@ -191,3 +191,19 @@ void print_on_level(unsigned int loglevel, const char *format, ...) ...@@ -191,3 +191,19 @@ void print_on_level(unsigned int loglevel, const char *format, ...)
__print_on_level(loglevel, format, params); __print_on_level(loglevel, format, params);
va_end(params); va_end(params);
} }
int write_pidfile(char *pfname, int pid)
{
int fd;
fd = open(pfname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
if (fd == -1) {
pr_perror("Can't open %s", pfname);
kill(pid, SIGKILL);
return -1;
}
dprintf(fd, "%d", pid);
close(fd);
return 0;
}
...@@ -273,6 +273,13 @@ int cr_page_server(bool daemon_mode) ...@@ -273,6 +273,13 @@ int cr_page_server(bool daemon_mode)
return -errno; return -errno;
} }
if (opts.pidfile) {
if (write_pidfile(opts.pidfile, getpid()) == -1) {
pr_perror("Can't write pidfile");
return -1;
}
}
ask = accept(sk, (struct sockaddr *)&caddr, &clen); ask = accept(sk, (struct sockaddr *)&caddr, &clen);
if (ask < 0) if (ask < 0)
pr_perror("Can't accept connection to server"); pr_perror("Can't accept connection to server");
......
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