Saturday, June 14, 2014

How to set PID using ns_last_pid

There is a cool project called CRIU(Checkpoint/Restore In Userspace). I was wondering, how it gets certain PID when restoring process. I thought, one can't easily set PID.

I did some code investigation and here is what I figured out. There is a file /proc/sys/kernel/ns_last_pid, it contains the last pid that was assigned by the kernel. So, when kernel needs to assign a new one, it looks into ns_last_pid, gets last_pid and assigns last_pid+1.

ns_last_pid was added by CRIU guys and is available since 3.3 kernel. It requires CONFIG_CHECKPOINT_RESTORE to be set. Most probably, it will work by default with your kernel(Tested on ubuntu). Btw, I don't know why, but in Arch kernel this option isn't set since ~3.11 until now (3.14.6-1).

Here is some C code to set PID for forked child.

Update: I've also added this example to criu wiki https://criu.org/Pid_restore.

BEWARE! This program requires root. I don't take any responsibility for what this code might do to your system.

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int fd, pid;
    char buf[32];

    if (argc != 2)
     return 1;

    printf("Opening ns_last_pid...\n");
    fd = open("/proc/sys/kernel/ns_last_pid", O_RDWR | O_CREAT, 0644);
    if (fd < 0) {
        perror("Can't open ns_last_pid");
        return 1;
    }
    printf("Done\n");

    printf("Locking ns_last_pid...\n");
    if (flock(fd, LOCK_EX)) {
        close(fd);
        printf("Can't lock ns_last_pid\n");
        return 1;
    }
    printf("Done\n");

    pid = atoi(argv[1]);
    snprintf(buf, sizeof(buf), "%d", pid - 1);

    printf("Writing pid-1 to ns_last_pid...\n");
    if (write(fd, buf, strlen(buf)) != strlen(buf)) {
        printf("Can't write to buf\n");
        return 1;
    }
    printf("Done\n");

    printf("Forking...\n");
    int new_pid;
    new_pid = fork();
    if (new_pid == 0) {
        printf("I'm child!\n");
        exit(0);
    } else if (new_pid == pid) {
        printf("I'm parent. My child got right pid!\n");
    } else {
        printf("pid does not match expected one\n");
    }
    printf("Done\n");

    printf("Cleaning up...");
    if (flock(fd, LOCK_UN)) {
        printf("Can't unlock");
    }

    close(fd);

    printf("Done\n");

    return 0;
}

2 comments:

  1. Wow. And is there any another method to set PID via Linux API?

    ReplyDelete
    Replies
    1. If there was a better way CRIU would have used it and they wouldn't have to send their own patch to kernel to enable the mechanism described above =).

      Delete