Skip to content

Commit

Permalink
bprocess_example: use BUnixSignal instead of BSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrop7 committed Jul 14, 2011
1 parent cf2b1d4 commit 6613186
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions examples/bprocess_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -53,39 +54,56 @@ 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;
}

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:
Expand All @@ -96,10 +114,11 @@ int main (int argc, char **argv)
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);
}

Expand All @@ -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);
}

0 comments on commit 6613186

Please sign in to comment.