瀏覽代碼

bprocess_example: use BUnixSignal instead of BSignal

ambrop7 14 年之前
父節點
當前提交
6613186880
共有 1 個文件被更改,包括 28 次插入8 次删除
  1. 28 8
      examples/bprocess_example.c

+ 28 - 8
examples/bprocess_example.c

@@ -27,15 +27,16 @@
 #include <base/DebugObject.h>
 #include <base/BLog.h>
 #include <system/BReactor.h>
-#include <system/BSignal.h>
+#include <system/BUnixSignal.h>
 #include <system/BTime.h>
 #include <system/BProcess.h>
 
 BReactor reactor;
+BUnixSignal unixsignal;
 BProcessManager manager;
 BProcess process;
 
-static void signal_handler (void *user);
+static void unixsignal_handler (void *user, int signo);
 static void process_handler (void *user, int normally, uint8_t normally_exit_status);
 
 int main (int argc, char **argv)
@@ -53,20 +54,31 @@ int main (int argc, char **argv)
     
     char *program = argv[1];
     
+    // init time
     BTime_Init();
     
+    // init logger
     BLog_InitStdout();
     
+    // init reactor (event loop)
     if (!BReactor_Init(&reactor)) {
         DEBUG("BReactor_Init failed");
         goto fail1;
     }
     
-    if (!BSignal_Init(&reactor, signal_handler, NULL)) {
-        DEBUG("BSignal_Init failed");
+    // choose signals to catch
+    sigset_t set;
+    sigemptyset(&set);
+    sigaddset(&set, SIGINT);
+    sigaddset(&set, SIGTERM);
+    
+    // init BUnixSignal for catching signals
+    if (!BUnixSignal_Init(&unixsignal, &reactor, set, unixsignal_handler, NULL)) {
+        DEBUG("BUnixSignal_Init failed");
         goto fail2;
     }
     
+    // init process manager
     if (!BProcessManager_Init(&manager, &reactor)) {
         DEBUG("BProcessManager_Init failed");
         goto fail3;
@@ -74,18 +86,24 @@ int main (int argc, char **argv)
     
     char **p_argv = argv + 1;
     
-    if (!BProcess_Init(&process, &manager, process_handler, NULL, program, p_argv, NULL)) {
+    // map fds 0, 1, 2 in child to fds 0, 1, 2 in parent
+    int fds[] = { 0, 1, 2, -1 };
+    int fds_map[] = { 0, 1, 2 };
+    
+    // start child process
+    if (!BProcess_InitWithFds(&process, &manager, process_handler, NULL, program, p_argv, NULL, fds, fds_map)) {
         DEBUG("BProcess_Init failed");
         goto fail4;
     }
     
+    // enter event loop
     ret = BReactor_Exec(&reactor);
     
     BProcess_Free(&process);
 fail4:
     BProcessManager_Free(&manager);
 fail3:
-    BSignal_Finish();
+    BUnixSignal_Free(&unixsignal, 0);
 fail2:
     BReactor_Free(&reactor);
 fail1:
@@ -96,10 +114,11 @@ fail0:
     return ret;
 }
 
-void signal_handler (void *user)
+void unixsignal_handler (void *user, int signo)
 {
-    DEBUG("termination requested, passing to child");
+    DEBUG("received %s, terminating child", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
     
+    // send SIGTERM to child
     BProcess_Terminate(&process);
 }
 
@@ -109,5 +128,6 @@ void process_handler (void *user, int normally, uint8_t normally_exit_status)
     
     int ret = (normally ? normally_exit_status : 1);
     
+    // return from event loop
     BReactor_Quit(&reactor, ret);
 }