static void *inthandler(void *_arg) { sigset_t sigs; int sig; int n; fprintf(stderr, "inthandler: enter\n"); /* unblock interrupt signal only here */ sigemptyset(&sigs); sigaddset(&sigs, SIGINT); pth_sigmask(SIG_UNBLOCK, &sigs, NULL); /* but the user has to hit CTRL-C three times */ for (n = 0; n < 3; n++) { pth_sigwait(&sigs, &sig); fprintf(stderr, "inthandler: SIGINT received (#%d)\n", n); } fprintf(stderr, "inthandler: cancelling child1 and child2\n"); pth_cancel(child1); pth_cancel(child2); fprintf(stderr, "inthandler: leave\n"); return NULL; }
int main (int ac, char *ag[]) { int index; pth_init (); argp_parse (&argp, ac, ag, ARGP_IN_ORDER, &index, &arg); // if you ever want this to be fatal, doing it here would be too late if (getuid () == 0) ERRORPRINTF (arg.tracer(), E_WARNING | 20, 0, "EIBD should not run as root"); signal (SIGPIPE, SIG_IGN); if (arg.daemon) { int fd = open (arg.daemon, O_WRONLY | O_APPEND | O_CREAT, FILE_MODE); if (fd == -1) die ("Can not open file %s", arg.daemon); int i = fork (); if (i < 0) die ("fork failed"); if (i > 0) exit (0); close (1); close (2); close (0); dup2 (fd, 1); dup2 (fd, 2); close (fd); setsid (); } FILE *pidf; if (arg.pidfile) if ((pidf = fopen (arg.pidfile, "w")) != NULL) { fprintf (pidf, "%d", getpid ()); fclose (pidf); } signal (SIGINT, SIG_IGN); signal (SIGTERM, SIG_IGN); // main loop #ifdef HAVE_SYSTEMD sd_notify(0,"READY=1"); #endif int sig; if (! arg.stop_now) do { sigset_t t1; sigemptyset (&t1); sigaddset (&t1, SIGINT); sigaddset (&t1, SIGHUP); sigaddset (&t1, SIGTERM); pth_sigwait (&t1, &sig); if (sig == SIGHUP && arg.daemon) { int fd = open (arg.daemon, O_WRONLY | O_APPEND | O_CREAT, FILE_MODE); if (fd == -1) { ERRORPRINTF (arg.tracer(), E_ERROR | 21, 0, "can't open log file %s", arg.daemon); continue; } close (1); close (2); dup2 (fd, 1); dup2 (fd, 2); close (fd); } } while (sig == SIGHUP); #ifdef HAVE_SYSTEMD sd_notify(0,"STOPPING=1"); #endif signal (SIGINT, SIG_DFL); signal (SIGTERM, SIG_DFL); #ifdef HAVE_GROUPCACHE DeleteGroupCache (); #endif arg.free_l3(); if (arg.pidfile) unlink (arg.pidfile); pth_yield (0); pth_yield (0); pth_yield (0); pth_yield (0); pth_exit (0); return 0; }
int main (int ac, char *ag[]) { int index; Queue < Server * >server; Server *s; Layer2Interface *l2; Layer3 *l3; #ifdef HAVE_EIBNETIPSERVER EIBnetServer *serv = 0; #endif memset (&arg, 0, sizeof (arg)); arg.addr = 0x0001; arg.errorlevel = LEVEL_WARNING; argp_parse (&argp, ac, ag, 0, &index, &arg); if (index > ac - 1) die ("url expected"); if (index < ac - 1) die ("unexpected parameter"); if (arg.port == 0 && arg.name == 0 && arg.serverip == 0) die ("No listen-address given"); signal (SIGPIPE, SIG_IGN); pth_init (); Trace t; t.SetTraceLevel (arg.tracelevel); t.SetErrorLevel (arg.errorlevel); /* if (getuid () == 0) ERRORPRINTF (&t, 0x37000001, 0, "EIBD should not run as root"); */ if(arg.eibnetname) { if(arg.eibnetname[0] == '=') arg.eibnetname++; if(strlen(arg.eibnetname) >= 30) die("EIBnetServer/IP name can't be longer then 30 char"); } if (arg.daemon) { int fd = open (arg.daemon, O_WRONLY | O_APPEND | O_CREAT, FILE_MODE); if (fd == -1) die ("Can not open file %s", arg.daemon); int i = fork (); if (i < 0) die ("fork failed"); if (i > 0) exit (0); close (1); close (2); close (0); dup2 (fd, 1); dup2 (fd, 2); close (fd); setsid (); } FILE *pidf; if (arg.pidfile) if ((pidf = fopen (arg.pidfile, "w")) != NULL) { fprintf (pidf, "%d", getpid ()); fclose (pidf); } l2 = Create (ag[index], arg.backendflags, &t); if (!l2 || !l2->init ()) die ("initialisation of the backend failed"); l3 = new Layer3 (l2, &t); if (arg.port) { s = new InetServer (l3, &t, arg.port); if (!s->init ()) die ("initialisation of the knxd inet protocol failed"); server.put (s); } if (arg.name) { s = new LocalServer (l3, &t, arg.name); if (!s->init ()) die ("initialisation of the knxd unix protocol failed"); server.put (s); } #ifdef HAVE_EIBNETIPSERVER serv = startServer (l3, &t, arg.eibnetname); #endif #ifdef HAVE_GROUPCACHE if (!CreateGroupCache (l3, &t, arg.groupcache)) die ("initialisation of the group cache failed"); #endif signal (SIGINT, SIG_IGN); signal (SIGTERM, SIG_IGN); int sig; do { sigset_t t1; sigemptyset (&t1); sigaddset (&t1, SIGINT); sigaddset (&t1, SIGHUP); sigaddset (&t1, SIGTERM); pth_sigwait (&t1, &sig); if (sig == SIGHUP && arg.daemon) { int fd = open (arg.daemon, O_WRONLY | O_APPEND | O_CREAT, FILE_MODE); if (fd == -1) { ERRORPRINTF (&t, 0x27000002, 0, "can't open log file %s", arg.daemon); continue; } close (1); close (2); dup2 (fd, 1); dup2 (fd, 2); close (fd); } } while (sig == SIGHUP); signal (SIGINT, SIG_DFL); signal (SIGTERM, SIG_DFL); while (!server.isempty ()) delete server.get (); #ifdef HAVE_EIBNETIPSERVER if (serv) delete serv; #endif #ifdef HAVE_GROUPCACHE DeleteGroupCache (); #endif delete l3; if (Cleanup) Cleanup (); if (arg.pidfile) unlink (arg.pidfile); pth_exit (0); return 0; }