:: Re: [DNG] Custom OS initiator. In n…
Forside
Slet denne besked
Besvar denne besked
Skribent: Didier Kryn
Dato:  
Til: dng
Emne: Re: [DNG] Custom OS initiator. In need of some hints...
Le 14/06/2016 08:54, Edward Bartolo a écrit :
> Hi,
>
> I downloaded and compiled Felker's minimal init but found it didn't
> work. Examining the code it seems that the website had been
> vandalised. According to my logic the endless loops should be at the
> end rather than at the middle of the code. Furthermore, the code seems
> to first block signals then it enables them back afterwards
> contradicting the text.
>
> I am quoting the website:
> http://ewontfix.com/14/
>
> The C code for Felker's minimal init is:
> -------------------------------------------------------------
> #define _XOPEN_SOURCE 700
> #include <signal.h>
> #include <unistd.h>
>
> int main()
> {
>      sigset_t set;
>      int status;

>
>      if (getpid() != 1) return 1;

>
>      sigfillset(&set);
>      sigprocmask(SIG_BLOCK, &set, 0);

>
>      if (fork()) for (;;) wait(&status);

>
>      sigprocmask(SIG_UNBLOCK, &set, 0);

>
>      setsid();
>      setpgid(0, 0);
>      return execve("/etc/rc", (char *[]){ "rc", 0 }, (char *[]){ 0 });
> }

>

     Let me remind you that the way to create a process is fork(). 
execve() does not create a process; it replaces the current program by 
another, within the same process. If you want to "spawn" a process 
running rc, you must invoke both fork() and then execve().


     fork() creates two identical processes. It returns the pid of the 
child in the old process and 0 in the new.


     If fork() returns 0, you are the new process and you execute rc, 
after unblocking the signals etc. If you are still the parent, you just 
wait() for children indefinitely.


     Didier


     I find strange that the code doesn't include sys/types.h and 
sys/wait.h .


     Didier