/** Handle a RESTART message from a server connection. * * \a parv has the following elements: * \li \a parv[1] is the target server, or "*" for all. * \li \a parv[2] is either "cancel" or a time interval in seconds * \li \a parv[\a parc - 1] is the reason * * All fields must be present. Additionally, the time interval should * not be 0 for messages sent to "*", as that may not function * reliably due to buffering in the server. * * See @ref m_functions for discussion of the arguments. * @param[in] cptr Client that sent us the message. * @param[in] sptr Original source of message. * @param[in] parc Number of arguments. * @param[in] parv Argument vector. */ int ms_restart(struct Client* cptr, struct Client* sptr, int parc, char* parv[]) { const char *target, *when, *reason; if (parc < 4) return need_more_params(sptr, "RESTART"); target = parv[1]; when = parv[2]; reason = parv[parc - 1]; /* is it a message we should pay attention to? */ if (target[0] != '*' || target[1] != '\0') { if (hunt_server_cmd(sptr, CMD_RESTART, cptr, 0, "%C %s :%s", 1, parc, parv) != HUNTED_ISME) return 0; } else /* must forward the message */ sendcmdto_serv(sptr, CMD_RESTART, cptr, "* %s :%s", when, reason); /* OK, the message has been forwarded, but before we can act... */ if (!feature_bool(FEAT_NETWORK_RESTART)) return 0; /* is it a cancellation? */ if (!ircd_strcmp(when, "cancel")) exit_cancel(sptr); /* cancel a pending exit */ else /* schedule an exit */ exit_schedule(1, atoi(when), sptr, reason); return 0; }
/** Signal callback for SIGTERM. * @param[in] ev Signal event descriptor. */ static void sigterm_callback(struct Event* ev) { assert(0 != ev_signal(ev)); assert(ET_SIGNAL == ev_type(ev)); assert(SIGTERM == sig_signal(ev_signal(ev))); assert(SIGTERM == ev_data(ev)); exit_schedule(0, 0, 0, "Received signal SIGTERM"); }
/** Signal callback for SIGINT. * @param[in] ev Signal event descriptor. */ static void sigint_callback(struct Event* ev) { assert(0 != ev_signal(ev)); assert(ET_SIGNAL == ev_type(ev)); assert(SIGINT == sig_signal(ev_signal(ev))); assert(SIGINT == ev_data(ev)); exit_schedule(1, 0, 0, "Received signal SIGINT"); }
/** Handle a RESTART message from an operator connection. * * \a parv has the following elements: * \li \a parv[1] is either "cancel" or a time interval in seconds * \li \a parv[\a parc - 1] is the reason * * Either the time interval or the reason (or both) may be omitted. * * See @ref m_functions for discussion of the arguments. * @param[in] cptr Client that sent us the message. * @param[in] sptr Original source of message. * @param[in] parc Number of arguments. * @param[in] parv Argument vector. */ int mo_restart(struct Client* cptr, struct Client* sptr, int parc, char* parv[]) { time_t when = 0; const char *reason = 0; if (!HasPriv(sptr, PRIV_RESTART)) return send_reply(sptr, ERR_NOPRIVILEGES); if (parc > 1 && !ircd_strcmp(parv[1], "cancel")) { exit_cancel(sptr); /* cancel a pending exit */ return 0; } else if (parc > 2) { /* have both time and reason */ when = atoi(parv[1]); reason = parv[parc - 1]; } else if (parc > 1 && !(when = atoi(parv[1]))) reason = parv[parc - 1]; /* now, let's schedule the exit */ exit_schedule(1, when, sptr, reason); return 0; }
/** Run engine event loop. * @param[in] gen Lists of generators of various types. */ static void engine_loop(struct Generators* gen) { struct kevent *events; int events_count; struct Socket* sock; struct timespec wait; int nevs; int i; int errcode; size_t codesize; if ((events_count = feature_int(FEAT_POLLS_PER_LOOP)) < 20) events_count = 20; events = (struct kevent *)MyMalloc(sizeof(struct kevent) * events_count); while (running) { if ((i = feature_int(FEAT_POLLS_PER_LOOP)) >= 20 && i != events_count) { events = (struct kevent *)MyRealloc(events, sizeof(struct kevent) * i); events_count = i; } /* set up the sleep time */ wait.tv_sec = timer_next(gen) ? (timer_next(gen) - CurrentTime) : -1; wait.tv_nsec = 0; Debug((DEBUG_INFO, "kqueue: delay: %Tu (%Tu) %Tu", timer_next(gen), CurrentTime, wait.tv_sec)); /* check for active events */ nevs = kevent(kqueue_id, 0, 0, events, events_count, wait.tv_sec < 0 ? 0 : &wait); CurrentTime = time(0); /* set current time... */ if (nevs < 0) { if (errno != EINTR) { /* ignore kevent interrupts */ /* Log the kqueue error */ log_write(LS_SOCKET, L_ERROR, 0, "kevent() error: %m"); if (!errors++) timer_add(timer_init(&clear_error), error_clear, 0, TT_PERIODIC, ERROR_EXPIRE_TIME); else if (errors > KQUEUE_ERROR_THRESHOLD) /* too many errors... */ exit_schedule(1, 0, 0, "too many kevent errors"); } /* old code did a sleep(1) here; with usage these days, * that may be too expensive */ continue; } for (i = 0; i < nevs; i++) { if (events[i].filter == EVFILT_SIGNAL) { /* it's a signal; deal appropriately */ event_generate(ET_SIGNAL, events[i].udata, events[i].ident); continue; /* skip socket processing loop */ } assert(events[i].filter == EVFILT_READ || events[i].filter == EVFILT_WRITE); sock = sockList[events[i].ident]; if (!sock) /* slots may become empty while processing events */ continue; assert(s_fd(sock) == events[i].ident); gen_ref_inc(sock); /* can't have it going away on us */ Debug((DEBUG_ENGINE, "kqueue: Checking socket %p (fd %d) state %s, " "events %s", sock, s_fd(sock), state_to_name(s_state(sock)), sock_flags(s_events(sock)))); if (s_state(sock) != SS_NOTSOCK) { errcode = 0; /* check for errors on socket */ codesize = sizeof(errcode); if (getsockopt(s_fd(sock), SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0) errcode = errno; /* work around Solaris implementation */ if (errcode) { /* an error occurred; generate an event */ Debug((DEBUG_ENGINE, "kqueue: Error %d on fd %d, socket %p", errcode, s_fd(sock), sock)); event_generate(ET_ERROR, sock, errcode); gen_ref_dec(sock); /* careful not to leak reference counts */ continue; } } switch (s_state(sock)) { case SS_CONNECTING: if (events[i].filter == EVFILT_WRITE) { /* connection completed */ Debug((DEBUG_ENGINE, "kqueue: Connection completed")); event_generate(ET_CONNECT, sock, 0); } break; case SS_LISTENING: if (events[i].filter == EVFILT_READ) { /* connect. to be accept. */ Debug((DEBUG_ENGINE, "kqueue: Ready for accept")); event_generate(ET_ACCEPT, sock, 0); } break; case SS_NOTSOCK: /* doing nothing socket-specific */ case SS_CONNECTED: if (events[i].filter == EVFILT_READ) { /* data on socket */ Debug((DEBUG_ENGINE, "kqueue: EOF or data to be read")); event_generate(events[i].flags & EV_EOF ? ET_EOF : ET_READ, sock, 0); } if (events[i].filter == EVFILT_WRITE) { /* socket writable */ Debug((DEBUG_ENGINE, "kqueue: Data can be written")); event_generate(ET_WRITE, sock, 0); } break; case SS_DATAGRAM: case SS_CONNECTDG: if (events[i].filter == EVFILT_READ) { /* socket readable */ Debug((DEBUG_ENGINE, "kqueue: Datagram to be read")); event_generate(ET_READ, sock, 0); } if (events[i].filter == EVFILT_WRITE) { /* socket writable */ Debug((DEBUG_ENGINE, "kqueue: Datagram can be written")); event_generate(ET_WRITE, sock, 0); } break; } gen_ref_dec(sock); /* we're done with it */ } timer_run(); /* execute any pending timers */ } }