From d6e6134a7db83cb43ef3e11245b72841ed71117f Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 30 Dec 2014 17:18:47 +0000 Subject: [PATCH] - main.c: style corrections git-svn-id: svn://svn.ircd-hybrid.org/svnroot/hopm/branches/1.0.x@5218 82007160-df01-0410-b94d-b575c5fd34c7 --- src/main.c | 349 ++++++++++++++++++++++++++--------------------------- 1 file changed, 168 insertions(+), 181 deletions(-) diff --git a/src/main.c b/src/main.c index 66260a0..03a6abd 100644 --- a/src/main.c +++ b/src/main.c @@ -50,236 +50,223 @@ along with this program; if not, write to the Free Software #include "main.h" -static void do_signal(int signum); +static int RESTART = 0; /* Flagged to restart on next cycle */ +static int ALARMED = 0; /* Flagged to call timer functions on next cycle */ +static int REOPEN = 0; /* Flagged to reopen log files on next cycle */ -int RESTART = 0; /* Flagged to restart on next cycle */ -int ALARMED = 0; /* Flagged to call timer functions on next cycle */ -int REOPEN = 0; /* Flagged to reopen log files on next cycle */ -unsigned int OPT_DEBUG = 0; /* Debug level */ +static struct sigaction ALARMACTION; +static struct sigaction INTACTION; +static struct sigaction USR1ACTION; -char *CONFNAME = DEFAULTNAME; +static char *CONFNAME = DEFAULTNAME; static const char *CONFDIR = HOPM_ETCDIR; static const char *LOGDIR = HOPM_LOGDIR; -char *CONFFILE, *LOGFILE; +static char *CONFFILE, *LOGFILE; + +unsigned int OPT_DEBUG = 0; /* Debug level */ -struct sigaction ALARMACTION; -struct sigaction INTACTION; -struct sigaction USR1ACTION; -int main(int argc, char *argv[]) +static void +do_signal(int signum) { - pid_t pid; - int c; - size_t lenc, lenl; - unsigned int i; - FILE *pidout; - struct rlimit rlim; - - while (1) - { - c = getopt(argc, argv, "dc:"); - - if (c == -1) - break; + switch (signum) + { + case SIGALRM: + ALARMED = 1; + alarm(1); + break; + case SIGINT: + log_printf("MAIN -> Caught SIGINT, bye!"); + exit(0); + break; + case SIGUSR1: + REOPEN = 1; + break; + } +} - switch (c) - { - case 'c': - CONFNAME = xstrdup(optarg); - break; - case 'd': - OPT_DEBUG++; - break; - case '?': - default: - /* Unknown arg, guess we'll just do nothing for now. */ - break; - } - } +int +main(int argc, char *argv[]) +{ + pid_t pid; + size_t lenc, lenl; + FILE *pidout; + struct rlimit rlim; - lenc = strlen(CONFDIR) + strlen(CONFNAME) + strlen(CONFEXT) + 3; - lenl = strlen(LOGDIR) + strlen(CONFNAME) + strlen(LOGEXT) + 3; + while (1) + { + int c = getopt(argc, argv, "dc:"); - CONFFILE = MyMalloc(lenc * sizeof *CONFFILE); - LOGFILE = MyMalloc(lenl * sizeof *LOGFILE); + if (c == -1) + break; - snprintf(CONFFILE, lenc, "%s/%s.%s", CONFDIR, CONFNAME, CONFEXT); - snprintf(LOGFILE, lenl, "%s/%s.%s", LOGDIR, CONFNAME, LOGEXT); + switch (c) + { + case 'c': + CONFNAME = xstrdup(optarg); + break; + case 'd': + OPT_DEBUG++; + break; + default: /* Unknown arg, guess we'll just do nothing for now. */ + break; + } + } - /* Fork off. */ + lenc = strlen(CONFDIR) + strlen(CONFNAME) + strlen(CONFEXT) + 3; + lenl = strlen(LOGDIR) + strlen(CONFNAME) + strlen(LOGEXT) + 3; - if (OPT_DEBUG <= 0) - { - if ((pid = fork()) < 0) - { - perror("fork()"); - exit(EXIT_FAILURE); - } - else if (pid != 0) - { - _exit(EXIT_SUCCESS); - } + CONFFILE = MyMalloc(lenc * sizeof *CONFFILE); + LOGFILE = MyMalloc(lenl * sizeof *LOGFILE); - /* Get us in our own process group. */ - if (setpgid(0, 0) < 0) - { - perror("setpgid()"); - exit(EXIT_FAILURE); - } + snprintf(CONFFILE, lenc, "%s/%s.%s", CONFDIR, CONFNAME, CONFEXT); + snprintf(LOGFILE, lenl, "%s/%s.%s", LOGDIR, CONFNAME, LOGEXT); - /* Reset file mode. */ - /* shasta: o+w is BAD, mmkay? */ - umask(002); + /* Fork off. */ + if (OPT_DEBUG <= 0) + { + if ((pid = fork()) < 0) + { + perror("fork()"); + exit(EXIT_FAILURE); + } + else if (pid != 0) + _exit(EXIT_SUCCESS); - /* Close file descriptors. */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); + /* Get us in our own process group. */ + if (setpgid(0, 0) < 0) + { + perror("setpgid()"); + exit(EXIT_FAILURE); + } - log_open(LOGFILE); - } - else - log_printf("MAIN -> Debug level %d", OPT_DEBUG); + /* Reset file mode. */ + /* shasta: o+w is BAD, mmkay? */ + umask(002); + /* Close file descriptors. */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); - log_printf("MAIN -> HOPM %s started.", VERSION); - log_printf("MAIN -> Reading configuration file..."); + log_open(LOGFILE); + } + else + log_printf("MAIN -> Debug level %d", OPT_DEBUG); - config_load(CONFFILE); + log_printf("MAIN -> HOPM %s started.", VERSION); + log_printf("MAIN -> Reading configuration file..."); - if (OptionsItem->scanlog) - scanlog_open(OptionsItem->scanlog); + config_load(CONFFILE); - pidout = fopen(OptionsItem->pidfile, "w"); + if (OptionsItem->scanlog) + scanlog_open(OptionsItem->scanlog); - if (pidout) - { - char spid[16]; + pidout = fopen(OptionsItem->pidfile, "w"); - snprintf(spid, sizeof(spid), "%u", getpid()); - fwrite(spid, sizeof(char), strlen(spid), pidout); - fclose(pidout); - } - else - { - log_printf("MAIN -> Error opening %s: %s", OptionsItem->pidfile, - strerror(errno)); - exit(EXIT_FAILURE); - } + if (pidout) + { + char spid[16]; - /* Setup alarm & int handlers. */ + snprintf(spid, sizeof(spid), "%u", getpid()); + fwrite(spid, sizeof(char), strlen(spid), pidout); + fclose(pidout); + } + else + { + log_printf("MAIN -> Error opening %s: %s", OptionsItem->pidfile, + strerror(errno)); + exit(EXIT_FAILURE); + } - ALARMACTION.sa_handler = &(do_signal); - ALARMACTION.sa_flags = SA_RESTART; - INTACTION.sa_handler = &(do_signal); - USR1ACTION.sa_handler = &(do_signal); + /* Setup alarm & int handlers. */ + ALARMACTION.sa_handler = &do_signal; + ALARMACTION.sa_flags = SA_RESTART; + INTACTION.sa_handler = &do_signal; + USR1ACTION.sa_handler = &do_signal; - sigaction(SIGALRM, &ALARMACTION, 0); - sigaction(SIGINT, &INTACTION, 0); - sigaction(SIGUSR1, &USR1ACTION, 0); + sigaction(SIGALRM, &ALARMACTION, 0); + sigaction(SIGINT, &INTACTION, 0); + sigaction(SIGUSR1, &USR1ACTION, 0); - /* Ignore SIGPIPE. */ - signal(SIGPIPE, SIG_IGN); + /* Ignore SIGPIPE. */ + signal(SIGPIPE, SIG_IGN); - alarm(1); + alarm(1); - while (1) - { - + while (1) + { + /* Main cycles */ + irc_cycle(); + scan_cycle(); - /* Main cycles */ - irc_cycle(); - scan_cycle(); + /* Restart bopm if main_restart() was called (usually happens by m_kill in irc.c) */ + if (RESTART) + { + /* If restarted in debug mode, die */ + if (OPT_DEBUG) + return 1; + log_printf("MAIN -> Restarting process"); - /* Restart bopm if main_restart() was called (usually happens by m_kill in irc.c) */ - if(RESTART) + /* Get upper file descriptor limit */ + if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) { - /* If restarted in debug mode, die */ - if(OPT_DEBUG) - return(1); - - log_printf("MAIN -> Restarting process"); - - /* Get upper file descriptor limit */ - if(getrlimit(RLIMIT_NOFILE, &rlim) == -1) - { - log_printf("MAIN RESTART -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno)); - return(1); - } - - /* Set file descriptors 0-rlim_cur close on exec */ - for(i = 0; i < rlim.rlim_cur; i++) - fcntl(i, F_SETFD, FD_CLOEXEC); - - /* execute new process */ - if(execve(argv[0], argv, NULL) == -1) - log_printf("MAIN RESTART -> Execution of \"%s\" failed. ERROR: %s", argv[0], strerror(errno)); - - /* Should only get here if execve failed */ - RESTART = 0; + log_printf("MAIN RESTART -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno)); + return 1; } - /* Check for log reopen */ - if(REOPEN) - { - log_printf("MAIN -> Caught SIGUSR1, reopening logfiles"); - log_close(); - log_open(LOGFILE); + /* Set file descriptors 0-rlim_cur close on exec */ + for (unsigned int i = 0; i < rlim.rlim_cur; ++i) + fcntl(i, F_SETFD, FD_CLOEXEC); - if(OptionsItem->scanlog) - { - scanlog_close(); - scanlog_open(OptionsItem->scanlog); - } + /* execute new process */ + if (execve(argv[0], argv, NULL) == -1) + log_printf("MAIN RESTART -> Execution of \"%s\" failed. ERROR: %s", argv[0], strerror(errno)); - log_printf("MAIN -> reopened logfiles"); + /* Should only get here if execve failed */ + RESTART = 0; + } - REOPEN = 0; - } + /* Check for log reopen */ + if (REOPEN) + { + log_printf("MAIN -> Caught SIGUSR1, reopening logfiles"); + log_close(); + log_open(LOGFILE); - /* Call 1 second timers */ - if(ALARMED) + if (OptionsItem->scanlog) { - irc_timer(); - scan_timer(); - command_timer(); - - ALARMED = 0; + scanlog_close(); + scanlog_open(OptionsItem->scanlog); } + log_printf("MAIN -> reopened logfiles"); - } + REOPEN = 0; + } - if (!OPT_DEBUG) - log_close(); + /* Call 1 second timers */ + if (ALARMED) + { + irc_timer(); + scan_timer(); + command_timer(); - /* If there's no scanlog open then this will do nothing anyway */ - scanlog_close(); + ALARMED = 0; + } + } - return(0); -} + if (!OPT_DEBUG) + log_close(); -static void do_signal(int signum) -{ - switch (signum) - { - case SIGALRM: - ALARMED = 1; - alarm(1); - break; - case SIGINT: - log_printf("MAIN -> Caught SIGINT, bye!"); - exit(0); - break; - case SIGUSR1: - REOPEN = 1; - break; - } + /* If there's no scanlog open then this will do nothing anyway */ + scanlog_close(); + return 0; } - -void main_restart(void) +void +main_restart(void) { - RESTART = 1; + RESTART = 1; } -- 2.30.2