[CRIU] [PATCH] crtools: write pidfile, when service server is run as daemon and "--pidfile" is set
Ruslan Kuprieiev
kupruser at gmail.com
Sun Sep 15 13:06:06 EDT 2013
Hi!
When service server becomes daemon, we may need to know it's pid.
Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
-------------- next part --------------
diff --git a/cr-restore.c b/cr-restore.c
index 12b3462..c35ec7f 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -885,21 +885,6 @@ struct cr_clone_arg {
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)
{
int ret = -1, fd;
@@ -971,8 +956,11 @@ static inline int fork_with_pid(struct pstree_item *item)
if (ca.clone_flags & CLONE_NEWPID)
item->pid.real = ret;
- if (opts.pidfile && root_item == item)
- write_pidfile(opts.pidfile, ret);
+ if (opts.pidfile && root_item == item) {
+ ret = write_pidfile(opts.pidfile, ret);
+ if (ret < 0)
+ pr_perror("Can't write pidfile");
+ }
err_unlock:
if (ca.fd >= 0) {
diff --git a/cr-service.c b/cr-service.c
index 79ba032..0f7b3dc 100644
--- a/cr-service.c
+++ b/cr-service.c
@@ -218,12 +218,19 @@ int cr_service(bool 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");
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 */
signal(SIGCHLD, SIG_IGN);
diff --git a/include/log.h b/include/log.h
index a38f441..17f624b 100644
--- a/include/log.h
+++ b/include/log.h
@@ -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, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
+extern int write_pidfile(char *pfname, int pid);
+
#ifndef LOG_PREFIX
# define LOG_PREFIX
#endif
diff --git a/log.c b/log.c
index feb2263..1e5fcad 100644
--- a/log.c
+++ b/log.c
@@ -191,3 +191,19 @@ void print_on_level(unsigned int loglevel, const char *format, ...)
__print_on_level(loglevel, format, 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;
+}
More information about the CRIU
mailing list