Line data Source code
1 : // 2 : // Copyright (c) 2019 Juniper Networks, Inc. All rights reserved. 3 : // Copyright Contributors to the OpenSDN project 4 : // 5 : 6 : #include <sys/wait.h> 7 : #include <boost/system/error_code.hpp> 8 : 9 : #include <io/process_signal.h> 10 : 11 : namespace process { 12 : 13 218 : boost::system::error_code Signal::InitializeSigChild() { 14 218 : boost::system::error_code ec; 15 : 16 218 : if (!sigchld_callbacks_.empty() || always_handle_sigchild_) { 17 172 : ec = AddSignal(SIGCHLD); 18 : } 19 : 20 218 : return ec; 21 : } 22 : 23 0 : void Signal::RegisterHandler(SignalChildHandler handler) { 24 0 : if (sigchld_callbacks_.empty()) { 25 : // Add signal first 26 0 : AddSignal(SIGCHLD); 27 : } 28 0 : sigchld_callbacks_.push_back(handler); 29 0 : } 30 : 31 120 : bool Signal::HandleSigOsSpecific(const boost::system::error_code& error, 32 : int sig) { 33 120 : if (sig != SIGCHLD) { 34 46 : return false; 35 : } 36 : 37 74 : int status = 0; 38 74 : pid_t pid = 0; 39 : 40 136 : while ((pid = WaitPid(-1, &status, WNOHANG)) > 0) { 41 62 : NotifySigChld(error, sig, pid, status); 42 : } 43 : 44 74 : return true; 45 : } 46 : 47 : } // namespace process