int main(int argc, char *argv[]) { time_t delay = 0; aConfItem* aconf; if(geteuid() == 0) { fprintf(stderr, "ERROR: Don't run ircd as root!\n"); return -1; } /* * save server boot time right away, so getrusage works correctly */ if ((CurrentTime = time(0)) == -1) { fprintf(stderr, "ERROR: Clock Failure: %s\n", strerror(errno)); exit(errno); } /* * Setup corefile size immediately after boot */ setup_corefile(); /* * set initialVMTop before we allocate any memory */ initialVMTop = get_vm_top(); /* * Initialize the Blockheap allocator */ initBlockHeap(); ServerRunning = 0; memset(&me, 0, sizeof(me)); GlobalClientList = &me; /* Pointer to beginning of Client list */ cold_start = YES; /* set when server first starts up */ memset(&Count, 0, sizeof(Count)); Count.server = 1; /* us */ initialize_global_set_options(); #ifdef REJECT_HOLD reject_held_fds = 0; #endif ConfigFileEntry.dpath = DPATH; ConfigFileEntry.configfile = CPATH; /* Server configuration file */ #ifdef KPATH ConfigFileEntry.klinefile = KPATH; /* Server kline file */ #else ConfigFileEntry.klinefile = CPATH; #endif /* KPATH */ #ifdef DLPATH ConfigFileEntry.dlinefile = DLPATH; #else ConfigFileEntry.dlinefile = CPATH; #endif /* DLPATH */ #ifdef GLINES ConfigFileEntry.glinefile = GLINEFILE; #endif #ifdef ZIP_LINKS /* Make sure the include files match the library version number. */ /* configure takes care of looking for zlib and zlibVersion(). */ if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) { fprintf(stderr, "WARNING: zlib include files differ from library.\n"); fprintf(stderr, "WARNING: ZIPLINKS may fail!\n"); fprintf(stderr, "WARNING: library %s, include files %s\n", zlibVersion(), ZLIB_VERSION); } #endif myargv = argv; umask(077); /* better safe than sorry --SRB */ parse_command_line(argc, argv); if (chdir(ConfigFileEntry.dpath)) { perror("chdir"); exit(-1); } /* * Check if daemon is already running */ check_pidfile(); init_sys(bootDaemon); init_log(logFileName); setup_signals(); initialize_message_files(); isupport = make_isupport(); dbuf_init(); /* set up some dbuf stuff to control paging */ init_hash(); clear_scache_hash_table(); /* server cache name table */ clear_ip_hash_table(); /* client host ip hash table */ clear_Dline_table(); /* d line tree */ initlists(); initclass(); initwhowas(); init_stats(); init_tree_parse(msgtab); /* tree parse code (orabidoo) */ fdlist_init(); init_netio(); read_conf_files(YES); /* cold start init conf files */ aconf = find_me(); if (EmptyString(me.name)) strncpy_irc(me.name, aconf->host, HOSTLEN); strncpy_irc(me.host, aconf->host, HOSTLEN); me.fd = -1; me.from = &me; me.servptr = &me; SetMe(&me); make_server(&me); me.serv->up = me.name; me.lasttime = me.since = me.firsttime = CurrentTime; add_to_client_hash_table(me.name, &me); check_class(); write_pidfile(); log(L_NOTICE, "Server Ready"); ServerRunning = 1; while (ServerRunning) { usleep(100000); do_adns_io(); delay = io_loop(delay); do_adns_io(); } return 0; }
/** Run the daemon. * @param[in] argc Number of arguments in \a argv. * @param[in] argv Arguments to program execution. */ int main(int argc, char **argv) { CurrentTime = time(NULL); thisServer.argc = argc; thisServer.argv = argv; thisServer.uid = getuid(); thisServer.euid = geteuid(); #ifdef MDEBUG mem_dbg_initialise(); #endif #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_CORE) set_core_limit(); #endif umask(077); /* better safe than sorry --SRB */ memset(&me, 0, sizeof(me)); memset(&me_con, 0, sizeof(me_con)); cli_connect(&me) = &me_con; cli_fd(&me) = -1; parse_command_line(argc, argv); if (chdir(dpath)) { fprintf(stderr, "Fail: Cannot chdir(%s): %s, check DPATH\n", dpath, strerror(errno)); return 2; } if (!set_userid_if_needed()) return 3; /* Check paths for accessibility */ if (!check_file_access(SPATH, 'S', X_OK) || !check_file_access(configfile, 'C', R_OK)) return 4; if (!init_connection_limits()) return 9; close_connections(!(thisServer.bootopt & (BOOT_DEBUG | BOOT_TTY | BOOT_CHKCONF))); /* daemon_init() must be before event_init() because kqueue() FDs * are, perversely, not inherited across fork(). */ daemon_init(thisServer.bootopt & BOOT_TTY); #ifdef DEBUGMODE /* Must reserve fd 2... */ if (debuglevel >= 0 && !(thisServer.bootopt & BOOT_TTY)) { int fd; if ((fd = open("/dev/null", O_WRONLY)) < 0) { fprintf(stderr, "Unable to open /dev/null (to reserve fd 2): %s\n", strerror(errno)); return 8; } if (fd != 2 && dup2(fd, 2) < 0) { fprintf(stderr, "Unable to reserve fd 2; dup2 said: %s\n", strerror(errno)); return 8; } } #endif event_init(MAXCONNECTIONS); setup_signals(); feature_init(); /* initialize features... */ log_init(*argv); set_nomem_handler(outofmemory); initload(); init_list(); init_hash(); init_class(); initwhowas(); initmsgtree(); initstats(); /* we need this for now, when we're modular this should be removed -- hikari */ ircd_crypt_init(); motd_init(); if (!init_conf()) { log_write(LS_SYSTEM, L_CRIT, 0, "Failed to read configuration file %s", configfile); return 7; } if (thisServer.bootopt & BOOT_CHKCONF) { if (dbg_client) conf_debug_iline(dbg_client); fprintf(stderr, "Configuration file %s checked okay.\n", configfile); return 0; } debug_init(thisServer.bootopt & BOOT_TTY); if (check_pid()) { Debug((DEBUG_FATAL, "Failed to acquire PID file lock after fork")); exit(2); } init_server_identity(); uping_init(); stats_init(); IPcheck_init(); timer_add(timer_init(&connect_timer), try_connections, 0, TT_RELATIVE, 1); timer_add(timer_init(&ping_timer), check_pings, 0, TT_RELATIVE, 1); timer_add(timer_init(&destruct_event_timer), exec_expired_destruct_events, 0, TT_PERIODIC, 60); timer_add(timer_init(&mute_timer), check_expired_mutes, 0, TT_PERIODIC, 30); CurrentTime = time(NULL); SetMe(&me); cli_magic(&me) = CLIENT_MAGIC; cli_from(&me) = &me; make_server(&me); cli_serv(&me)->timestamp = TStime(); /* Abuse own link timestamp as start TS */ cli_serv(&me)->prot = atoi(MAJOR_PROTOCOL); cli_serv(&me)->up = &me; cli_serv(&me)->down = NULL; cli_handler(&me) = SERVER_HANDLER; SetYXXCapacity(&me, MAXCLIENTS); cli_lasttime(&me) = cli_since(&me) = cli_firsttime(&me) = CurrentTime; hAddClient(&me); write_pidfile(); init_counters(); Debug((DEBUG_NOTICE, "Server ready...")); log_write(LS_SYSTEM, L_NOTICE, 0, "Server Ready"); event_loop(); return 0; }
int main(int argc, char *argv[]) { uid_t uid, euid; int portarg = 0, fd; #ifdef SAVE_MAXCLIENT_STATS FILE *mcsfp; #endif memset(&me, 0, sizeof(aClient)); if ((timeofday = time(NULL)) == -1) { (void) fprintf(stderr, "ERROR: Clock Failure (%d)\n", errno); exit(errno); } build_version(); Count.server = 1; /* us */ Count.oper = 0; Count.chan = 0; Count.local = 0; Count.total = 0; Count.invisi = 0; Count.unknown = 0; Count.max_loc = 0; Count.max_tot = 0; Count.today = 0; Count.weekly = 0; Count.monthly = 0; Count.yearly = 0; Count.start = NOW; Count.day = NOW; Count.week = NOW; Count.month = NOW; Count.year = NOW; #ifdef SAVE_MAXCLIENT_STATS mcsfp=fopen(DPATH "/.maxclients", "r"); if(mcsfp!=NULL) { fscanf(mcsfp, "%d %d %li %li %li %ld %ld %ld %ld", &Count.max_loc, &Count.max_tot, &Count.weekly, &Count.monthly, &Count.yearly, &Count.start, &Count.week, &Count.month, &Count.year); fclose(mcsfp); } #endif /* * this code by [email protected] * it is intended to keep the ircd from being swapped out. BSD * swapping criteria do not match the requirements of ircd */ #ifdef INITIAL_DBUFS dbuf_init(); /* set up some dbuf stuff to control paging */ #endif sbrk0 = (char *) sbrk((size_t) 0); uid = getuid(); euid = geteuid(); #ifdef PROFIL (void) monstartup(0, etext); (void) moncontrol(1); (void) signal(SIGUSR1, s_monitor); #endif myargv = argv; (void) umask(077); /* better safe than sorry --SRB */ memset((char *) &me, '\0', sizeof(me)); setup_signals(); /* * * All command line parameters have the syntax "-fstring" or "-f * string" (e.g. the space is optional). String may be empty. Flag * characters cannot be concatenated (like "-fxyz"), it would * conflict with the form "-fstring". */ while (--argc > 0 && (*++argv)[0] == '-') { char *p = argv[0] + 1; int flag = *p++; if (flag == '\0' || *p == '\0') { if (argc > 1 && argv[1][0] != '-') { p = *++argv; argc -= 1; } else p = ""; } switch (flag) { case 'a': bootopt |= BOOT_AUTODIE; break; case 'c': bootopt |= BOOT_CONSOLE; break; case 'q': bootopt |= BOOT_QUICK; break; case 'd': (void) setuid((uid_t) uid); dpath = p; break; case 'o': /* Per user local daemon... */ (void) setuid((uid_t) uid); bootopt |= BOOT_OPER; break; #ifdef CMDLINE_CONFIG case 'f': (void) setuid((uid_t) uid); configfile = p; break; # ifdef KPATH case 'k': (void) setuid((uid_t) uid); klinefile = p; break; # endif #endif case 'h': strncpyzt(me.name, p, sizeof(me.name)); break; case 'i': bootopt |= BOOT_INETD | BOOT_AUTODIE; break; case 'p': if ((portarg = atoi(p)) > 0) portnum = portarg; break; case 's': bootopt |= BOOT_STDERR; break; case 't': (void) setuid((uid_t) uid); bootopt |= BOOT_TTY; break; case 'v': (void) printf("ircd %s\n", version); exit(0); case 'x': #ifdef DEBUGMODE (void) setuid((uid_t) uid); debuglevel = atoi(p); debugmode = *p ? p : "0"; bootopt |= BOOT_DEBUG; break; #else (void) fprintf(stderr, "%s: DEBUGMODE must be defined for -x y\n", myargv[0]); exit(0); #endif default: bad_command(); break; } } if (chdir(dpath)) { perror("chdir"); exit(-1); } if ((uid != euid) && !euid) { (void) fprintf(stderr, "ERROR: do not run ircd setuid root. Make it setuid a normal user.\n"); exit(-1); } if (argc > 0) return bad_command(); /* This should exit out */ initialize_ssl(); motd = (aMotd *) NULL; helpfile = (aMotd *) NULL; motd_tm = NULL; #ifdef SHORT_MOTD shortmotd = NULL; #endif read_motd(MOTD); read_help(HELPFILE); #ifdef SHORT_MOTD read_shortmotd(SHORTMOTD); #endif clear_client_hash_table(); clear_channel_hash_table(); clear_scache_hash_table(); /* server cache name table */ clear_ip_hash_table(); /* client host ip hash table */ initlists(); initclass(); initwhowas(); initstats(); init_tree_parse(msgtab); init_send(); NOW = time(NULL); open_debugfile(); NOW = time(NULL); init_fdlist(&serv_fdlist); init_fdlist(&oper_fdlist); init_fdlist(&listen_fdlist); #ifndef NO_PRIORITY init_fdlist(&busycli_fdlist); #endif init_fdlist(&default_fdlist); { int i; for (i = MAXCONNECTIONS + 1; i > 0; i--) { default_fdlist.entry[i] = i - 1; } } if ((timeofday = time(NULL)) == -1) { #ifdef USE_SYSLOG syslog(LOG_WARNING, "Clock Failure (%d), TS can be corrupted", errno); #endif sendto_ops("Clock Failure (%d), TS can be corrupted", errno); } #ifdef WINGATE_NOTICE strcpy(ProxyMonURL, "http://"); strncpyzt((ProxyMonURL + 7), DEFAULT_PROXY_INFO_URL, (TOPICLEN + 1) - 7); strncpyzt(ProxyMonHost, MONITOR_HOST, (HOSTLEN + 1)); #endif if (portnum < 0) portnum = PORTNUM; me.port = portnum; (void) init_sys(); me.flags = FLAGS_LISTEN; #ifndef _WIN32 if (bootopt & BOOT_INETD) { me.fd = 0; local[0] = &me; me.flags = FLAGS_LISTEN; } else #endif me.fd = -1; #ifdef USE_SYSLOG # define SYSLOG_ME "ircd" openlog(SYSLOG_ME, LOG_PID | LOG_NDELAY, LOG_FACILITY); #endif if ((fd = openconf(configfile)) == -1) { Debug((DEBUG_FATAL, "Failed in reading configuration file %s", configfile)); (void) printf("Couldn't open configuration file %s\n", configfile); exit(-1); } (void) initconf(bootopt, fd); /* comstuds SEPARATE_QUOTE_KLINES_BY_DATE code */ #ifdef SEPARATE_QUOTE_KLINES_BY_DATE { struct tm *tmptr; char timebuffer[20], filename[200]; tmptr = localtime(&NOW); (void) strftime(timebuffer, 20, "%y%m%d", tmptr); ircsprintf(filename, "%s.%s", klinefile, timebuffer); if ((fd = openconf(filename)) == -1) { Debug((DEBUG_ERROR, "Failed reading kline file %s", filename)); (void) printf("Couldn't open kline file %s\n", filename); } else (void) initconf(0, fd); } #else # ifdef KPATH if ((fd = openconf(klinefile)) == -1) { Debug((DEBUG_ERROR, "Failed reading kline file %s", klinefile)); (void) printf("Couldn't open kline file %s\n", klinefile); } else (void) initconf(0, fd); # endif #endif if (!(bootopt & BOOT_INETD)) { static char star[] = "*"; aConfItem *aconf; u_long vaddr; if ((aconf = find_me()) && portarg <= 0 && aconf->port > 0) portnum = aconf->port; Debug((DEBUG_ERROR, "Port = %d", portnum)); if ((aconf->passwd[0] != '\0') && (aconf->passwd[0] != '*')) vaddr = inet_addr(aconf->passwd); else vaddr = (u_long) NULL; if (inetport(&me, star, portnum, vaddr)) { if (bootopt & BOOT_STDERR) fprintf(stderr, "Couldn't bind to primary port %d\n", portnum); #ifdef USE_SYSLOG (void) syslog(LOG_CRIT, "Couldn't bind to primary port %d\n", portnum); #endif exit(1); } } else if (inetport(&me, "*", 0, 0)) { if (bootopt & BOOT_STDERR) fprintf(stderr, "Couldn't bind to port passed from inetd\n"); #ifdef USE_SYSLOG (void) syslog(LOG_CRIT, "Couldn't bind to port passed from inetd\n"); #endif exit(1); } (void) get_my_name(&me, me.sockhost, sizeof(me.sockhost) - 1); if (me.name[0] == '\0') strncpyzt(me.name, me.sockhost, sizeof(me.name)); me.hopcount = 0; me.authfd = -1; me.confs = NULL; me.next = NULL; me.user = NULL; me.from = &me; SetMe(&me); make_server(&me); me.serv->up = me.name; me.lasttime = me.since = me.firsttime = NOW; (void) add_to_client_hash_table(me.name, &me); /* We don't want to calculate these every time they are used :) */ sprintf(REPORT_DO_DNS, REPORT_DO_DNS_, me.name); sprintf(REPORT_FIN_DNS, REPORT_FIN_DNS_, me.name); sprintf(REPORT_FIN_DNSC, REPORT_FIN_DNSC_, me.name); sprintf(REPORT_FAIL_DNS, REPORT_FAIL_DNS_, me.name); sprintf(REPORT_DO_ID, REPORT_DO_ID_, me.name); sprintf(REPORT_FIN_ID, REPORT_FIN_ID_, me.name); sprintf(REPORT_FAIL_ID, REPORT_FAIL_ID_, me.name); R_do_dns = strlen(REPORT_DO_DNS); R_fin_dns = strlen(REPORT_FIN_DNS); R_fin_dnsc = strlen(REPORT_FIN_DNSC); R_fail_dns = strlen(REPORT_FAIL_DNS); R_do_id = strlen(REPORT_DO_ID); R_fin_id = strlen(REPORT_FIN_ID); R_fail_id = strlen(REPORT_FAIL_ID); check_class(); if (bootopt & BOOT_OPER) { aClient *tmp = add_connection(&me, 0); if (!tmp) exit(1); SetMaster(tmp); } else write_pidfile(); Debug((DEBUG_NOTICE, "Server ready...")); #ifdef USE_SYSLOG syslog(LOG_NOTICE, "Server Ready"); #endif NOW = time(NULL); #ifndef NO_PRIORITY check_fdlists(); #endif if ((timeofday = time(NULL)) == -1) { #ifdef USE_SYSLOG syslog(LOG_WARNING, "Clock Failure (%d), TS can be corrupted", errno); #endif sendto_ops("Clock Failure (%d), TS can be corrupted", errno); } #ifdef DUMP_DEBUG dumpfp=fopen("dump.log", "w"); #endif io_loop(); return 0; }
int main(int argc, char *argv[]) { /* Check to see if the user is running us as root, which is a nono */ if(geteuid() == 0) { fprintf(stderr, "Don't run ircd as root!!!\n"); return -1; } /* * save server boot time right away, so getrusage works correctly */ set_time(); /* * Setup corefile size immediately after boot -kre */ setup_corefile(); /* * set initialVMTop before we allocate any memory */ initialVMTop = get_vm_top(); ServerRunning = 0; /* It ain't random, but it ought to be a little harder to guess */ srand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20))); memset(&me, 0, sizeof(me)); memset(&meLocalUser, 0, sizeof(meLocalUser)); me.localClient = &meLocalUser; /* Make sure all lists are zeroed */ memset(&unknown_list, 0, sizeof(unknown_list)); memset(&lclient_list, 0, sizeof(lclient_list)); memset(&serv_list, 0, sizeof(serv_list)); memset(&global_serv_list, 0, sizeof(global_serv_list)); memset(&oper_list, 0, sizeof(oper_list)); dlinkAddTail(&me, &me.node, &global_client_list); memset((void *) &Count, 0, sizeof(Count)); memset((void *) &ServerInfo, 0, sizeof(ServerInfo)); memset((void *) &AdminInfo, 0, sizeof(AdminInfo)); /* Initialise the channel capability usage counts... */ init_chcap_usage_counts(); ConfigFileEntry.dpath = DPATH; ConfigFileEntry.configfile = CPATH; /* Server configuration file */ ConfigFileEntry.klinefile = KPATH; /* Server kline file */ ConfigFileEntry.dlinefile = DLPATH; /* dline file */ ConfigFileEntry.xlinefile = XPATH; ConfigFileEntry.resvfile = RESVPATH; ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ myargv = argv; umask(077); /* better safe than sorry --SRB */ parseargs(&argc, &argv, myopts); if(printVersion) { printf("ircd: version %s\n", ircd_version); exit(EXIT_SUCCESS); } if(chdir(ConfigFileEntry.dpath)) { fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); exit(EXIT_FAILURE); } setup_signals(); #ifdef __CYGWIN__ server_state_foreground = 1; #endif if (testing_conf) server_state_foreground = 1; /* We need this to initialise the fd array before anything else */ fdlist_init(); if(!server_state_foreground) { comm_close_all(); } /* Check if there is pidfile and daemon already running */ if(!testing_conf) { check_pidfile(pidFileName); if(!server_state_foreground) make_daemon(); else print_startup(getpid()); } init_netio(); /* This needs to be setup early ! -- adrian */ /* Init the event subsystem */ eventInit(); init_sys(); init_main_logfile(); initBlockHeap(); init_dlink_nodes(); init_patricia(); newconf_init(); init_s_conf(); init_s_newconf(); linebuf_init(); /* set up some linebuf stuff to control paging */ init_hash(); clear_scache_hash_table(); /* server cache name table */ init_host_hash(); clear_hash_parse(); init_client(); initUser(); init_channels(); initclass(); initwhowas(); init_stats(); init_hook(); init_reject(); init_cache(); init_monitor(); load_all_modules(1); #ifndef STATIC_MODULES load_core_modules(1); #endif init_auth(); /* Initialise the auth code */ init_resolver(); /* Needs to be setup before the io loop */ if (testing_conf) fprintf(stderr, "\nBeginning config test\n"); read_conf_files(YES); /* cold start init conf files */ rehash_bans(0); #ifndef STATIC_MODULES mod_add_path(MODULE_DIR); mod_add_path(MODULE_DIR "/autoload"); #endif initialize_server_capabs(); /* Set up default_server_capabs */ initialize_global_set_options(); if(ServerInfo.name == NULL) { fprintf(stderr, "ERROR: No server name specified in serverinfo block.\n"); ilog(L_MAIN, "No server name specified in serverinfo block."); exit(EXIT_FAILURE); } strlcpy(me.name, ServerInfo.name, sizeof(me.name)); if(ServerInfo.sid[0] == '\0') { fprintf(stderr, "ERROR: No server sid specified in serverinfo block.\n"); ilog(L_MAIN, "No server sid specified in serverinfo block."); exit(EXIT_FAILURE); } strcpy(me.id, ServerInfo.sid); init_uid(); /* serverinfo{} description must exist. If not, error out. */ if(ServerInfo.description == NULL) { fprintf(stderr, "ERROR: No server description specified in serverinfo block.\n"); ilog(L_MAIN, "ERROR: No server description specified in serverinfo block."); exit(EXIT_FAILURE); } strlcpy(me.info, ServerInfo.description, sizeof(me.info)); if (testing_conf) { fprintf(stderr, "\nConfig testing complete.\n"); fflush(stderr); exit(EXIT_SUCCESS); } me.from = &me; me.servptr = &me; SetMe(&me); make_server(&me); me.serv->up = me.name; startup_time = CurrentTime; add_to_client_hash(me.name, &me); add_to_id_hash(me.id, &me); dlinkAddAlloc(&me, &global_serv_list); check_class(); write_pidfile(pidFileName); load_help(); open_logfiles(); ilog(L_MAIN, "Server Ready"); eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME); /* We want try_connections to be called as soon as possible now! -- adrian */ /* No, 'cause after a restart it would cause all sorts of nick collides */ /* um. by waiting even longer, that just means we have even *more* * nick collisions. what a stupid idea. set an event for the IO loop --fl */ eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); eventAddOnce("try_connections_startup", try_connections, NULL, 0); eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME); /* Setup the timeout check. I'll shift it later :) -- adrian */ eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1); if(ConfigServerHide.links_delay > 0) eventAddIsh("cache_links", cache_links, NULL, ConfigServerHide.links_delay); else ConfigServerHide.links_disabled = 1; if(splitmode) eventAdd("check_splitmode", check_splitmode, NULL, 2); ServerRunning = 1; io_loop(); return 0; }