bool PhysicalSocketServer::SetPosixSignalHandler(int signum, void (*handler)(int)) { // If handler is SIG_IGN or SIG_DFL then clear our user-level handler, // otherwise set one. if (handler == SIG_IGN || handler == SIG_DFL) { if (!InstallSignal(signum, handler)) { return false; } if (signal_dispatcher_) { signal_dispatcher_->ClearHandler(signum); if (!signal_dispatcher_->HasHandlers()) { Remove(signal_dispatcher_); delete signal_dispatcher_; signal_dispatcher_ = nullptr; } } } else { if (!signal_dispatcher_) { signal_dispatcher_ = new PosixSignalDispatcher(); Add(signal_dispatcher_); } signal_dispatcher_->SetHandler(signum, handler); if (!InstallSignal(signum, &GlobalSignalHandler)) { return false; } } return true; }
int main(int argc, char** argv) { if (argc < 2) { fprintf(stderr, "use: --exec your_process start --log your_log_file {default[/var/log/bumo-daemon.log]} \n"); exit(EXIT_FAILURE); } std::string skey = ""; std::map<std::string, std::string> commands; for (int i = 1; i < argc;i++) { std::string cmd(argv[i]); if (cmd.find("--") != std::string::npos) { skey = cmd; } else { if (commands[skey].empty()) commands[skey] = cmd; else commands[skey] = commands[skey] + std::string(" ") + cmd; } } std::string str_exec; std::string str_log; if (commands.find("--exec") == commands.end()) { fprintf(stderr, "use: --exec your_process start --log your_log_file {default[/var/log/bumo-daemon.log]} \n"); exit(EXIT_FAILURE); } str_exec = commands["--exec"]; if (commands.find("--log") == commands.end()) { str_log = "/var/log/bumo-daemon.log"; } else { str_log = commands["--log"]; } if (!loginit(str_log.c_str())) { fprintf(stderr, "open log file (%s) fail", str_log.c_str()); exit(EXIT_FAILURE); } LOG("the command is : [%s]", str_exec.c_str()); do { int fd; pthread_mutex_t *mptr = NULL; pthread_mutexattr_t mattr; fd = open("/dev/zero", O_RDWR, 0); mptr = (pthread_mutex_t*)mmap(0, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); pthread_mutexattr_init(&mattr); pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(mptr, &mattr); void *shm = NULL;//分配的共享内存的原始首地址 int64_t *shared;//指向shm int shmid;//共享内存标识符 //创建共享内存 shmid = shmget((key_t)1234, sizeof(int64_t), 0666 | IPC_CREAT); if (shmid == -1) { LOG("shmget failed"); break; } //将共享内存连接到当前进程的地址空间 shm = shmat(shmid, 0, 0); if (shm == (void*)-1) { break; } shared = (int64_t*)shm; int64_t last = 0; g_enable_ = true; InstallSignal(); while (g_enable_)//读取共享内存中的数据 { sleep(100); pthread_mutex_lock(mptr); LOG("last:%ld now:%ld ", last, *shared); if (last == *shared) { LOG("something wrong with process... do (%s)", str_exec.c_str()); system(str_exec.c_str()); } last = *shared; pthread_mutex_unlock(mptr); } //把共享内存从当前进程中分离 if (shmdt(shm) == -1) { LOG("shmdt failed"); break; } //删除共享内存 if (shmctl(shmid, IPC_RMID, 0) == -1) { LOG("shmctl(IPC_RMID) failed"); break; } log_close(); } while (false); log_close(); exit(EXIT_SUCCESS); }