0

I know this can be done in the user space and this has been answered before.

However, I would like to be able to do it from with a kernel function/module.

UPDATE The c code (referred to from the other answered question) is part of the criu project. And the file /proc/sys/kernel/ns_last_pid does not exist in all systems. So, the code does not work on ubuntu. Is there any way I can make it work on ubuntu?

My final goal is: fork a process and give it the ID of the parent.

Steps: staring with a process names P with id x.

  1. Fork a process from within P. Name the child Q.
  2. Assign the parent process P a temp id, say a.
  3. Change the id of the child process Q and set it to be x.
  4. Another kernel function will rewire the parenthood of child process Q. So that parent of P becomes parent of Q. In other words, the child process will take the identity of its parent.
  5. Process P dies off since it is not waiting on any process.

I know how to do the steps 4-5. I have already implemented that. I am hacking another Linux kernel project. So, I have no other way around this. I have to implement this way.

Thanks all.

Community
  • 1
  • 1
feeling_lonely
  • 6,665
  • 4
  • 27
  • 53
  • 1
    And what is your question? How to find the implementation of `/proc/sys/kernel/ns_last_pid`? – CL. Sep 25 '14 at 11:24
  • @CL. Why you down voted the question, now it is never gonna get answered. I have updated the question with more details. – feeling_lonely Sep 30 '14 at 22:29
  • possible duplicate of [How to set process ID in Linux for an specific program](http://stackoverflow.com/questions/18122592/how-to-set-process-id-in-linux-for-an-specific-program) – Ruslan Kuprieiev Oct 19 '14 at 11:12
  • Since you expect process P to go away anyway, why not just `execve()` process Q? That will replace the process image of P with that of Q while retaining the same PID. – twalberg Dec 17 '14 at 17:27

1 Answers1

0

The kernel assumes that a process is identified by its pid; changing the pid of an already-running process is unlikely to work without changing lots of code all over the kernel.

To give a process Q the same pid as a process P, use a third process R that sets ns_last_pid after P has terminated but before Q is started. (For how to use ns_last_pid, see this example.)

CL.
  • 173,858
  • 17
  • 217
  • 259