1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <signal.h> #include <thread> #include <vector> #include <cstring>
int g_pid = -1; bool g_restart = true; char g_execPath[1024] = "\0"; std::vector<char*> g_argv;
int CreateProcess() { int pid = fork(); if(pid == -1) { printf("fork error\n"); return -1; }else if(pid == 0){ int ret = execv(g_execPath, g_argv.data()); if(ret < 0) { printf("execv : %s(%d)\n", strerror(errno), errno); return -2; } } return pid; }
bool ProcExist(int pid) { char proc_path[256]; struct stat file_stat;
snprintf(proc_path, sizeof(proc_path), "/proc/%d", pid);
if (stat(proc_path, &file_stat) == 0 && S_ISDIR(file_stat.st_mode)) { return true; } else { return false; } }
void EntryThread() { while(true) { if (g_restart && !ProcExist(g_pid)) { printf("Process(%d) terminated\n", g_pid); g_pid = CreateProcess(); printf("Process(%d) restarted\n", g_pid); g_restart = false; } usleep(100000); } return; }
void ChildProcess(int signo) { pid_t pid; int stat; pid = wait(&stat); g_restart = true; }
int main(int argc, char* argv[]) { for(int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-exec_path")==0 && i+1 < argc) strcpy(g_execPath, argv[++i]); else g_argv.push_back(argv[i]); } g_argv.push_back(nullptr);
if(g_execPath[0] == '\0') { int len = strlen(argv[0]); strcpy(g_execPath, argv[0]); while (len-- > 0) { if (g_execPath[len] == '/') { g_execPath[len + 1] = '\0'; break; } } if (len < 0) { strcpy(g_execPath, "./\0"); } strcat(g_execPath, "test_exec"); } signal(SIGCHLD, &ChildProcess); std::thread th(EntryThread); th.join(); }
|