diff --git a/cr-restore.c b/cr-restore.c index 88857ef82..73657b726 100644 --- a/cr-restore.c +++ b/cr-restore.c @@ -547,6 +547,20 @@ static inline int fork_with_pid(struct pstree_item *item, unsigned long ns_clone if (ca.clone_flags & CLONE_NEWPID) item->pid.real = ret; + + if (opts.pidfile && root_item == item) { + int fd; + + fd = open(opts.pidfile, O_WRONLY | O_TRUNC | O_CREAT, 0600); + if (fd == -1) { + pr_perror("Can't open %s", opts.pidfile); + kill(ret, SIGKILL); + } else { + dprintf(fd, "%d", ret); + close(fd); + } + } + err_unlock: if (flock(ca.fd, LOCK_UN)) pr_perror("%d: Can't unlock %s", pid, LAST_PID_PATH); diff --git a/crtools.c b/crtools.c index a79e4f40a..3c10a81bc 100644 --- a/crtools.c +++ b/crtools.c @@ -89,6 +89,7 @@ int main(int argc, char *argv[]) { "log-pid", no_argument, 0, 44}, { "version", no_argument, 0, 'V'}, { "evasive-devices", no_argument, 0, 45}, + { "pidfile", required_argument, 0, 46}, { }, }; @@ -169,6 +170,9 @@ int main(int argc, char *argv[]) case 45: opts.evasive_devices = true; break; + case 46: + opts.pidfile = optarg; + break; case 'V': pr_msg("Version: %d.%d\n", CRIU_VERSION_MAJOR, CRIU_VERSION_MINOR); return 0; @@ -249,6 +253,7 @@ usage: pr_msg(" -d|--restore-detached detach after restore\n"); pr_msg(" -s|--leave-stopped leave tasks in stopped state after checkpoint instead of killing them\n"); pr_msg(" -D|--images-dir directory where to put images to\n"); + pr_msg(" --pidfile [FILE] write a pid of a root task in this file\n"); pr_msg("\n* Special resources support:\n"); pr_msg(" -n|--namespaces checkpoint/restore namespaces - values must be separated by comma\n"); diff --git a/include/crtools.h b/include/crtools.h index 702196b2a..e8a53dc09 100644 --- a/include/crtools.h +++ b/include/crtools.h @@ -92,6 +92,7 @@ struct cr_options { bool log_file_per_pid; char *output; char *root; + char *pidfile; }; extern struct cr_options opts;