akaros/tests/daemonize.c
<<
>>
Prefs
   1#include <parlib/event.h>
   2#include <parlib/parlib.h>
   3#include <parlib/uthread.h>
   4
   5#include <assert.h>
   6#include <stdio.h>
   7#include <stdlib.h>
   8#include <sys/types.h>
   9#include <sys/wait.h>
  10
  11static void ev_handler(struct event_msg *msg, unsigned int ev_type, void *data)
  12{
  13        int rv;
  14
  15        assert(msg != NULL);
  16        assert(ev_type == EV_USER_IPI);
  17        (void)data;
  18        rv = msg->ev_arg1;
  19        exit(rv);
  20}
  21
  22int main(int argc, char *argv[], char *envp[])
  23{
  24        struct event_queue *evq, *triggered;
  25        pid_t pid;
  26        struct event_msg msg;
  27        int wstatus;
  28
  29        register_ev_handler(EV_USER_IPI, ev_handler, 0);
  30        evq = get_eventq(EV_MBOX_UCQ);
  31        evq->ev_flags = EVENT_IPI | EVENT_INDIR | EVENT_SPAM_INDIR |
  32                        EVENT_WAKEUP;
  33        register_kevent_q(evq, EV_USER_IPI);
  34
  35        pid = create_child_with_stdfds(argv[1], argc - 1, argv + 1, envp);
  36        if (pid < 0) {
  37                perror("child creation failed");
  38                exit(-1);
  39        }
  40        sys_proc_run(pid);
  41
  42        waitpid(pid, &wstatus, 0);
  43        if (!WIFEXITED(wstatus)) {
  44                fprintf(stderr, "child %s did not exit normally!\n", argv[1]);
  45                return -1;
  46        }
  47        return WEXITSTATUS(wstatus);
  48}
  49