c++ linux下安全启动进程收集

来源:互联网 发布:windows阻止软件自启 编辑:程序博客网 时间:2024/06/03 14:40

下面的功能有待验证,需要做一下试验

#include <unistd.h>#include <wait.h>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>using namespace  std;void startServ(std::string &servPath){    char servName[] = "sername";    pid_t pid = fork();    if(pid == 0)//first child    {        pid_t cPid = fork();        if(cPid == 0)//second child                       //这里缺少切换到servPath工作路径的代码                       execl(servPath.c_str(),servName,NULL);//替换上面的second  child而存在            cout << "execl failed pid:" << endl;            exit(EXIT_FAILURE);        }        cout << "start server: "<< servName << " success!"             << " pid: " << cPid <<endl;        _exit(0);    } else if (pid < 0)    {        cout << "first fork failed!" << endl;    }    if (waitpid(pid, NULL, 0) != pid)  // wait for first child  no  block    {        cout << "waitpid first child error!" << endl;    }    cout << "first child returned, waitpid:" << pid << endl;}int main(int argc, char **argv){    string path = "path";    startServ(path);    cout << "execl end" << endl;  return 0;}