/* * Timeout reached. * * What to do when timeout reached. */ static void timeout_DAEMON_WAIT_FOR_PID_FILE(active_db_h * s) { /* check if timeout have appeared */ if (g.now.tv_sec > s->time_current_state.tv_sec + PID_TIMEOUT) { process_h *process = NULL; F_("Service \"%s\" wait for pidfile timed out! Will kill " "daemon now.\n", s->name); initng_common_mark_service(s, &DAEMON_FAIL_START_TIMEOUT_PIDFILE); kill_daemon(s, SIGKILL); /* Free the process if found */ process = initng_process_db_get(&T_DAEMON, s); if (process) { initng_process_db_free(process); } return; } /* * NOW, start check for a pid */ if (!try_get_pid(s)) { /* try again in 1 second */ initng_handler_set_alarm(s, 1); } }
/* when DAEMON_TERM timeout, kill it instead */ static void timeout_DAEMON_TERM(active_db_h * daemon) { F_("Service %s TERM_TIMEOUT reached!, sending KILL signal.\n", daemon->name); kill_daemon(daemon, SIGKILL); /* Set it to DAEMON_KILL state, monitoring that TERM signal is * sent. */ initng_common_mark_service(daemon, &DAEMON_KILL); }
/* set SIGKILL on every timeout */ static void timeout_DAEMON_KILL(active_db_h * daemon) { F_("Service %s KILL_TIMEOUT of %i seconds reached! " "(%i seconds passed), sending another KILL signal.\n", daemon->name, DEFAULT_KILL_TIMEOUT, MS_DIFF(g.now, daemon->time_current_state)); kill_daemon(daemon, SIGKILL); /* Dont be afraid to kill again */ initng_handler_set_alarm(daemon, DEFAULT_KILL_TIMEOUT); }
char *run_command(char *cmd) { char *r = NULL; if (cmd) { if (check_if_stop(cmd)) kill_daemon(); if (check_if_ping(cmd)) r = pong(); if (check_if_list(cmd)) r = list(); if (check_if_block(cmd)) r = block(); if (check_if_trace(cmd)) r = trace(); } return r; }
rollwand() { if (++between >= 4) { if (roll(1, 6) == 4) { wanderer(); kill_daemon(rollwand); fuse(swander, 0, WANDERTIME, BEFORE); } between = 0; } }
static void init_DAEMON_STOP_DEPS_MET(active_db_h * service) { process_h *process = NULL; /* find the daemon, and check so it still exits */ if (!(process = initng_process_db_get(&T_DAEMON, service))) { F_("Could not find process to kill!\n"); return; } /* Check so process have a valid pid */ if (process->pid <= 0) { D_("Pid is unvalid, marked as DAEMON_STOPPED\n"); initng_common_mark_service(service, &DAEMON_STOPPED); return; } if (kill(process->pid, 0) && errno == ESRCH) { D_("Dont exist a process with pid %i, mark as " "DAEMON_STOPPED\n", process->pid); initng_common_mark_service(service, &DAEMON_STOPPED); return; } /* launch stop service */ switch (initng_execute_launch(service, &T_KILL, NULL)) { case FAIL: F_(" -- (%s): fail launch stop!\n", service->name); initng_common_mark_service(service, &DAEMON_FAIL_STOPPING); return; case FALSE: { /* if there is no module that wanna kill this * daemon, we do it ourself. */ kill_daemon(service, SIGTERM); if (!initng_common_mark_service(service, &DAEMON_TERM)) return; break; } default: /* Set its state to GOING_KILLED, to mark that we put * some effort on killing it. */ if (!initng_common_mark_service(service, &DAEMON_TERM)) return; break; } }
daemon rollwand(daemon_arg *arg) { NOOP(arg); if ((rnd(6) == 0) && (player.t_ctype != C_THIEF || (rnd(30) >= pstats.s_dext))) { wanderer(); kill_daemon(DAEMON_ROLLWAND); light_fuse(FUSE_SWANDER, 0, WANDERTIME, BEFORE); } return; }
/* * rollwand: * Called to roll to see if a wandering monster starts up */ void rollwand(void) { if (++between >= 4) { /* Theives may not awaken a monster */ if ((roll(1, 6) == 4) && ((player.t_ctype != C_THIEF) || (rnd(30) >= pstats.s_dext))) { if (levtype != POSTLEV) wanderer(); kill_daemon(rollwand); fuse(swander, 0, WANDERTIME, BEFORE); } between = 0; } }
int main(int argc, char **argv) { int alive, kill, live; openlog("angeld",LOG_PID | LOG_CONS,LOG_USER); /* Set defaults */ kill = 0; live = 0; /* Parse arguments */ if(argc <= 1) goto help; if(strcmp(argv[1],"start") == 0) live = 1; else if(strcmp(argv[1],"restart") == 0) kill = live = 1; else if(strcmp(argv[1],"stop") == 0) kill = 1; else { /* Fools! */ help: puts("usage: angeld (start|restart|stop)"); exit(EXIT_SUCCESS); } /* Check PID file */ if(kill || live) { alive = is_alive(); if(kill && !alive) syslog(LOG_INFO,"nothing to kill"); kill &= alive; if(!kill && live && alive) syslog(LOG_INFO,"angeld already alive"); live &= kill || !alive; } /* To be or not to be... */ if(kill) kill_daemon(); if(live) { make_daemon(); init_com(); monitor(); } closelog(); return !(kill || live); }
/* * come_down: * Take the hero down off her acid trip. */ come_down() { register THING *tp; register bool seemonst; if (!on(player, ISTrip)) return; kill_daemon(visuals); if (on(player, ISBLIND)) return; /* * undo the things */ for (tp = lvl_obj; tp != NULL; tp = next(tp)) if (cansee(tp->o_pos.y, tp->o_pos.x)) mvaddrawch(tp->o_pos.y, tp->o_pos.x, tp->o_type); /* * undo the stairs */ if (seenstairs) mvaddrawch(stairs.y, stairs.x, STAIRS); /* * undo the monsters */ seemonst = on(player, SEEMONST); for (tp = mlist; tp != NULL; tp = next(tp)) if (cansee(tp->t_pos.y, tp->t_pos.x)) if (!on(*tp, ISINVIS) || on(player, CANSEE)) mvaddrawch(tp->t_pos.y, tp->t_pos.x, tp->t_disguise); else mvaddrawch(tp->t_pos.y, tp->t_pos.x, chat(tp->t_pos.y, tp->t_pos.x)); else if (seemonst) { standout(); mvaddrawch(tp->t_pos.y, tp->t_pos.x, tp->t_type); standend(); } player.t_flags &= ~ISTrip; msg("Everything looks SO boring now."); }
int main(int argc, char **argv) { int i, result; progname = strrchr(argv[0], '/'); progname = progname ? progname + 1 : argv[0]; is_module = 0; ipc_init_struct(); gettimeofday(&self.start, NULL); /* * Solaris doesn't support MSG_NOSIGNAL, so * we ignore SIGPIPE globally instead */ signal(SIGPIPE, SIG_IGN); for (i = 1; i < argc; i++) { char *opt, *arg = argv[i]; if (*arg != '-') { if (!merlin_conf) { merlin_conf = arg; continue; } goto unknown_argument; } if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) usage(NULL); if (!strcmp(arg, "-k") || !strcmp(arg, "--kill")) { killing = 1; continue; } if (!strcmp(arg, "-d") || !strcmp(arg, "--debug")) { debug++; continue; } if ((opt = strchr(arg, '='))) opt++; else if (i < argc - 1) opt = argv[i + 1]; else usage("Unknown argument, or argument '%s' requires a parameter", arg); i++; if (!strcmp(arg, "--config") || !strcmp(arg, "-c")) { merlin_conf = opt; continue; } unknown_argument: usage("Unknown argument: %s", arg); } if (!merlin_conf) usage("No config-file specified\n"); if (!grok_config(merlin_conf)) { fprintf(stderr, "%s contains errors. Bailing out\n", merlin_conf); return 1; } if (!pidfile) pidfile = "/var/run/merlin.pid"; if (killing) return kill_daemon(pidfile); if (use_database && !import_program) { lwarn("Using database, but no import program configured. Are you sure about this?"); lwarn("If not, make sure you specify the import_program directive in"); lwarn("the \"daemon\" section of your merlin configuration file"); } ipc.action = ipc_action_handler; result = ipc_init(); if (result < 0) { printf("Failed to initalize ipc socket: %s\n", strerror(errno)); return 1; } if (net_init() < 0) { printf("Failed to initialize networking: %s\n", strerror(errno)); return 1; } if (!debug) { if (daemonize(merlin_user, NULL, pidfile, 0) < 0) exit(EXIT_FAILURE); /* * we'll leak these file-descriptors, but that * doesn't really matter as we just want accidental * output to go somewhere where it'll be ignored */ fclose(stdin); open("/dev/null", O_RDONLY); fclose(stdout); open("/dev/null", O_WRONLY); fclose(stderr); open("/dev/null", O_WRONLY); } signal(SIGINT, clean_exit); signal(SIGTERM, clean_exit); signal(SIGUSR1, sigusr_handler); signal(SIGUSR2, sigusr_handler); sql_init(); if (use_database) { sql_query("TRUNCATE TABLE program_status"); sql_query("INSERT INTO program_status(instance_id, instance_name, is_running) " "VALUES(0, 'Local Nagios daemon', 0)"); for (i = 0; i < (int)num_nodes; i++) { char *node_name; merlin_node *node = noc_table[i]; sql_quote(node->name, &node_name); sql_query("INSERT INTO program_status(instance_id, instance_name, is_running) " "VALUES(%d, %s, 0)", node->id + 1, node_name); safe_free(node_name); } } state_init(); linfo("Merlin daemon %s successfully initialized", merlin_version); polling_loop(); clean_exit(0); return 0; }