void termination_handler(int sig) { PRINT_DEBUG("**********Terminating *******"); //shutdown all module threads in backwards order of startup //rtm_shutdown(); udp_shutdown(); tcp_shutdown(); icmp_shutdown(); ipv4_shutdown(); arp_shutdown(); interface_shutdown(); //TODO finish daemon_shutdown(); //TODO finish switch_shutdown(); //TODO finish //have each module free data & que/sem //TODO finish each of these //rtm_release(); udp_release(); tcp_release(); icmp_release(); ipv4_release(); arp_release(); interface_release(); daemon_release(); switch_release(); PRINT_DEBUG("FIN"); exit(-1); }
static void clean_exit(int sig) { ipc_deinit(); sql_close(); net_deinit(); daemon_shutdown(); _exit(!!sig); }
/* * Act upon receiving signals */ void signal_handler(int sig) { switch (sig) { case SIGHUP: case SIGINT: case SIGTERM: daemon_shutdown(); exit(EXIT_SUCCESS); break; default: break; } }
/// <summary>Our raspicomm daemon specific work gets done.</summary> void do_work(void) { /* setup the syslog for logging */ init_syslog(); /* read the settings from the settings file */ Settings settings; int ret; if ((ret = load_settings(&settings)) < 0) { //syslog(LOG_ERR, "loading settings failed (%i)", ret); exit(EXIT_FAILURE); } if (settings.log) { syslog(LOG_INFO, "starting..."); syslog(LOG_INFO, "using %s: port '%i' and pidfile '%s' and log=%i", ret == 0 ? "default configuration" : "configurationfile", settings.port, settings.pidfile, settings.log); } /* write the pidfile */ if ( write_pidfile(settings.pidfile) != 0) if (settings.log) syslog(LOG_ERR, "writing PIDFILE '%s' failed", settings.pidfile); /* use the settings to initialize the raspicomm */ if (daemon_init_raspicomm(&settings) != SUCCESS) exit(EXIT_FAILURE); /* init the pipe */ if (init_pipe() != 0) if (settings.log) syslog(LOG_ERR, "init_pipe failed"); /* block and work */ enter_main_loop(&settings); /* tear down raspicomm */ daemon_shutdown(); }
/* * Act upon receiving signals */ void signal_handler(int sig) { switch (sig) { case SIGHUP: if (!use_syslog) log_reopen(&lfh); /* necessary for log file * rotation */ break; case SIGINT: case SIGTERM: daemon_shutdown(); exit(EXIT_SUCCESS); break; default: break; } }
int main(int argc, char *argv[]) { printf("ISAKMP: main : <<<START>>> \n"); fd_set *rfds, *wfds; int n, m; size_t mask_size; struct timeval tv, *timeout; // closefrom(STDERR_FILENO + 1); /* * Make sure init() won't alloc fd 0, 1 or 2, as daemon() will close * them. */ /* for (n = 0; n <= 2; n++) if (fcntl(n, F_GETFL, 0) == -1 && errno == EBADF) (void) open("/dev/null", n ? O_WRONLY : O_RDONLY, 0); */ /* Log cmd line parsing and initialization errors to stderr. */ log_to(stderr); parse_args(argc, argv); log_init(debug); /* Open protocols and services databases. */ setprotoent(1); setservent(1); /* Open command fifo */ ui_init(); set_slave_signals(); /* Daemonize before forking unpriv'ed child */ if (!debug) if (daemon(0, 0)) log_fatal("main: daemon (0, 0) failed"); /* Set timezone before priv'separation */ tzset(); write_pid_file(); /* if (monitor_init(debug)) { // The parent, with privileges enters infinite monitor loop. //monitor_loop(debug); //exit(0); // Never reached. }*/ /* Child process only from this point on, no privileges left. */ init(); /* If we wanted IKE packet capture to file, initialize it now. */ if (pcap_file != 0) log_packet_init(pcap_file); /* Allocate the file descriptor sets just big enough. */ // we change here //n = getdtablesize(); n = 1000; mask_size = howmany(n, NFDBITS) * sizeof(fd_mask); rfds = (fd_set *) malloc(mask_size); if (!rfds) log_fatal("main: malloc (%lu) failed", (unsigned long)mask_size); wfds = (fd_set *) malloc(mask_size); if (!wfds) log_fatal("main: malloc (%lu) failed", (unsigned long)mask_size); //monitor_init_done(); while (1) { /* If someone has sent SIGHUP to us, reconfigure. */ if (sighupped) { sighupped = 0; log_print("SIGHUP received"); reinit(); } /* and if someone sent SIGUSR1, do a state report. */ if (sigusr1ed) { sigusr1ed = 0; log_print("SIGUSR1 received"); report(); } /* * and if someone set 'sigtermed' (SIGTERM, SIGINT or via the * UI), this indicates we should start a controlled shutdown * of the daemon. * * Note: Since _one_ message is sent per iteration of this * enclosing while-loop, and we want to send a number of * DELETE notifications, we must loop atleast this number of * times. The daemon_shutdown() function starts by queueing * the DELETEs, all other calls just increments the * 'sigtermed' variable until it reaches a "safe" value, and * the daemon exits. */ if (sigtermed) daemon_shutdown(); /* Setup the descriptors to look for incoming messages at. */ bzero(rfds, mask_size); n = transport_fd_set(rfds); FD_SET(ui_socket, rfds); if (ui_socket + 1 > n) n = ui_socket + 1; /* * XXX Some day we might want to deal with an abstract * application class instead, with many instantiations * possible. */ if (!app_none && app_socket >= 0) { FD_SET(app_socket, rfds); if (app_socket + 1 > n) n = app_socket + 1; } /* Setup the descriptors that have pending messages to send. */ bzero(wfds, mask_size); m = transport_pending_wfd_set(wfds); printf(" m = %d , n = %d \n", m,n); if (m > n) n = m; /* Find out when the next timed event is. */ timeout = &tv; timer_next_event(&timeout); printf(" select (n n= %d )\n ",n ); n = select(n, rfds, wfds, 0, timeout); if (n == -1) { if (errno != EINTR) { log_error("main: select"); /* * In order to give the unexpected error * condition time to resolve without letting * this process eat up all available CPU * we sleep for a short while. */ sleep(1); } } else if (n) { transport_handle_messages(rfds); transport_send_messages(wfds); if (FD_ISSET(ui_socket, rfds)) ui_handler(); if (!app_none && app_socket >= 0 && FD_ISSET(app_socket, rfds)) app_handler(); } timer_handle_expirations(); } }
int main(int argc, char **argv) { unsigned char buf[256]; int size, s, cntrl_s, cntrl_fd; socklen_t cntrl_len; struct sockaddr_un cntrl_addr; fd_set read_fds, write_fds; int fd_max; if (ax25_config_load_ports() == 0) { fprintf(stderr, "ax25rtd: no AX.25 port configured\n"); return 1; } load_config(); load_cache(); if (fork()) return 0; if ((s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_AX25))) == -1) { perror("AX.25 socket"); return 1; } if ((cntrl_s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { perror("Control socket"); return 1; } unlink(DATA_AX25ROUTED_CTL_SOCK); cntrl_addr.sun_family = AF_UNIX; strcpy(cntrl_addr.sun_path, DATA_AX25ROUTED_CTL_SOCK); cntrl_len = sizeof(cntrl_addr.sun_family) + strlen(DATA_AX25ROUTED_CTL_SOCK); if (bind(cntrl_s, (struct sockaddr *) &cntrl_addr, cntrl_len) < 0) { perror("bind Control socket"); daemon_shutdown(1); } chmod(DATA_AX25ROUTED_CTL_SOCK, 0600); listen(cntrl_s, 1); signal(SIGUSR1, sig_debug); signal(SIGHUP, sig_reload); signal(SIGTERM, sig_term); cntrl_fd = -1; for (;;) { fd_max = 0; FD_ZERO(&read_fds); FD_ZERO(&write_fds); FD_MAX(s); if (cntrl_fd > 0) { FD_MAX(cntrl_fd); FD_SET(cntrl_fd, &write_fds); } else { FD_MAX(cntrl_s); } if (select(fd_max + 1, &read_fds, NULL, &write_fds, NULL) < 0) { if (errno == EINTR) /* woops! */ continue; if (!FD_ISSET(cntrl_fd, &write_fds)) { perror("select"); save_cache(); daemon_shutdown(1); } else { close(cntrl_fd); cntrl_fd = -1; continue; } } if (cntrl_fd > 0) { if (FD_ISSET(cntrl_fd, &read_fds)) { size = read(cntrl_fd, buf, sizeof(buf)); if (size > 0) { buf[size] = '\0'; interpret_command(cntrl_fd, buf); } else { close(cntrl_fd); cntrl_fd = -1; } } } else if (FD_ISSET(cntrl_s, &read_fds)) { if ((cntrl_fd = accept(cntrl_s, (struct sockaddr *) &cntrl_addr, &cntrl_len)) < 0) { perror("accept Control"); save_cache(); daemon_shutdown(1); } } if (reload) reload_config(); if (FD_ISSET(s, &read_fds)) ax25_receive(s); } return 0; /* what ?! */ }
void sig_term(int d) { save_cache(); daemon_shutdown(0); }
int main (int argc, char *argv[]) { fd_set *rfds, *wfds; int n, m; size_t mask_size; struct timeval tv, *timeout; /* Make sure init() won't alloc fd 0, 1 or 2, as daemon() will close them. */ for (n = 0; n <= 2; n++) if (fcntl (n, F_GETFL, 0) == -1 && errno == EBADF) (void)open ("/dev/null", n ? O_WRONLY : O_RDONLY, 0); /* Log cmd line parsing and initialization errors to stderr. */ log_to (stderr); parse_args (argc, argv); log_init (); /* Do a clean daemon shutdown on TERM reception. (Needed by monitor). */ signal (SIGTERM, daemon_shutdown_now); if (debug) signal (SIGINT, daemon_shutdown_now); #if defined (USE_PRIVSEP) if (monitor_init ()) { /* The parent, with privileges. */ if (!debug) if (daemon (0, 0)) log_fatal ("main [priv]: daemon (0, 0) failed"); /* Enter infinite monitor loop. */ monitor_loop (debug); exit (0); /* Never reached. */ } /* Child process only from this point on, no privileges left. */ #endif init (); if (!debug) { if (daemon (0, 0)) log_fatal ("main: daemon (0, 0) failed"); /* Switch to syslog. */ log_to (0); } write_pid_file (); /* Reinitialize on HUP reception. */ signal (SIGHUP, sighup); /* Report state on USR1 reception. */ signal (SIGUSR1, sigusr1); /* Rehash soft expiration timers on USR2 reception. */ signal (SIGUSR2, sigusr2); #if defined (USE_DEBUG) /* If we wanted IKE packet capture to file, initialize it now. */ if (pcap_file != 0) log_packet_init (pcap_file); #endif /* Allocate the file descriptor sets just big enough. */ n = getdtablesize (); mask_size = howmany (n, NFDBITS) * sizeof (fd_mask); rfds = (fd_set *)malloc (mask_size); if (!rfds) log_fatal ("main: malloc (%lu) failed", (unsigned long)mask_size); wfds = (fd_set *)malloc (mask_size); if (!wfds) log_fatal ("main: malloc (%lu) failed", (unsigned long)mask_size); while (1) { /* If someone has sent SIGHUP to us, reconfigure. */ if (sighupped) { log_print ("SIGHUP received"); reinit (); sighupped = 0; } /* and if someone sent SIGUSR1, do a state report. */ if (sigusr1ed) { log_print ("SIGUSR1 received"); report (); } /* and if someone sent SIGUSR2, do a timer rehash. */ if (sigusr2ed) { log_print ("SIGUSR2 received"); rehash_timers (); } /* * and if someone set 'sigtermed' (SIGTERM or via the UI), this * indicated we should start a shutdown of the daemon. * * Note: Since _one_ message is sent per iteration of this enclosing * while-loop, and we want to send a number of DELETE notifications, * we must loop atleast this number of times. The daemon_shutdown() * function starts by queueing the DELETEs, all other calls just * increments the 'sigtermed' variable until it reaches a "safe" * value, and the daemon exits. */ if (sigtermed) daemon_shutdown (); /* Setup the descriptors to look for incoming messages at. */ memset (rfds, 0, mask_size); n = transport_fd_set (rfds); FD_SET (ui_socket, rfds); if (ui_socket + 1 > n) n = ui_socket + 1; /* * XXX Some day we might want to deal with an abstract application * class instead, with many instantiations possible. */ if (!app_none && app_socket >= 0) { FD_SET (app_socket, rfds); if (app_socket + 1 > n) n = app_socket + 1; } /* Setup the descriptors that have pending messages to send. */ memset (wfds, 0, mask_size); m = transport_pending_wfd_set (wfds); if (m > n) n = m; /* Find out when the next timed event is. */ timeout = &tv; timer_next_event (&timeout); n = select (n, rfds, wfds, 0, timeout); if (n == -1) { if (errno != EINTR) { log_error ("select"); /* * In order to give the unexpected error condition time to * resolve without letting this process eat up all available CPU * we sleep for a short while. */ sleep (1); } } else if (n) { transport_handle_messages (rfds); transport_send_messages (wfds); if (FD_ISSET (ui_socket, rfds)) ui_handler (); if (!app_none && app_socket >= 0 && FD_ISSET (app_socket, rfds)) app_handler (); } timer_handle_expirations (); } }