How to Spawn Child Processes that Don't Die with Parent?

前端 未结 6 1196
说谎
说谎 2021-01-18 11:09

I have a C++ program that acts as a watchdog over others. If it detects that a process is no longer running, it restarts it via system. The problem is, if I kil

6条回答
  •  花落未央
    2021-01-18 11:40

    You should avoid the use of system.

    Do not use system() from a program with set-user-ID or set-group-ID privileges, because strange values for some environment variables might be used to subvert system integrity. Use the exec(3) family of functions instead.

    and also if you want to check the return value or anything else

    Just go for execl

    #include 
    #include 
    pid_t pid = fork()
    if (pid == -1)
    {
       exit(EXIT_FAILURE);
    }
    else if (pid == 0)
    {
      if (execl("processname", "processname", "/path/to/processConfig.xml", NULL) == -1)
        exit(EXIT_FAILURE); 
    }
    else
    ...
    

提交回复
热议问题