/* * Merge a remote configuration to the result */ static void conf_merge(struct conf *c, const struct conf *sc) { char buf[BUFSIZ]; if (debug) { (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "merge:\t", "", sc)); (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "to:\t", "", c)); } if (sc->c_name[0]) memcpy(c->c_name, sc->c_name, CONFNAMESZ); if (sc->c_uid != FEQUAL) c->c_uid = sc->c_uid; if (sc->c_rmask != FEQUAL) c->c_lmask = c->c_rmask = sc->c_rmask; if (sc->c_nfail != FEQUAL) c->c_nfail = sc->c_nfail; if (sc->c_duration != FEQUAL) c->c_duration = sc->c_duration; if (debug) (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "result:\t", "", c)); }
/* * test main for conf module, usage: a.out conffile */ int main(int argc, char *argv[]) { struct opts *opts; err_init(argv[0]); setbuf(stdout, NULL); opts_init(Opttable, Opttable_cnt); opts = opts_parse(NULL, NULL, 0); if (argc != 2) err(EF_RAW, "usage: %s conffile\n", argv[0]); conf_open(argv[1], argv[1], opts); printf("conffile <%s>:\n", argv[1]); conf_print(stdout, NULL); conf_close(opts); err_done(0); /* NOTREACHED */ return (0); }
int main(int argc, char **argv) { struct sigaction sig_stop; struct sigaction sig_time; _main = main_init(argc, argv); _log = log_init(); _main->conf = conf_init(argc, argv); _main->work = work_init(); _main->tcp = tcp_init(); _main->node = node_init(); _main->mime = mime_init(); /* Check configuration */ conf_print(); /* Catch SIG INT */ unix_signal(&sig_stop, &sig_time); /* Fork daemon */ unix_fork(log_console(_log)); /* Increase limits */ unix_limits(_main->conf->cores, CONF_EPOLL_MAX_EVENTS); /* Load mime types */ mime_load(); mime_hash(); /* Prepare TCP daemon */ tcp_start(); /* Drop privileges */ unix_dropuid0(); /* Start worker threads */ work_start(); /* Stop worker threads */ work_stop(); /* Stop TCP daemon */ tcp_stop(); mime_free(); node_free(); tcp_free(); work_free(); conf_free(); log_free(_log); main_free(); return 0; }
static void dumpkey(const struct conf *k) { char buf[10240]; blhexdump(buf, sizeof(buf), __func__, k, sizeof(*k)); (*lfun)(LOG_DEBUG, "%s", buf); (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "", "", k)); }
/* * conf_close -- close the configuration file */ void conf_close(struct opts *opts) { FILE *fp; if (Confchanged && opts_count(opts, "n") == 0 && Conffd != -1) { if (opts_count(opts, "v")) (void) out("# writing changes to %s\n", Confname); if (Debug > 1) { (void) fprintf(stderr, "conf_close, %s changed to:\n", Confname); conf_print(stderr); } if (lseek(Conffd, (off_t)0, SEEK_SET) < 0) err(EF_SYS, "lseek on %s", Confname); if (ftruncate(Conffd, (off_t)0) < 0) err(EF_SYS, "ftruncate on %s", Confname); if ((fp = fdopen(Conffd, "w")) == NULL) err(EF_SYS, "fdopen on %s", Confname); conf_print(fp); if (fclose(fp) < 0) err(EF_SYS, "fclose on %s", Confname); Conffd = -1; Confchanged = B_FALSE; } else if (opts_count(opts, "v")) { (void) out("# %s unchanged\n", Confname); } if (Conffd != -1) { (void) close(Conffd); Conffd = -1; } if (Conflut) { lut_free(Conflut, free); Conflut = NULL; } if (Confentries) { fn_list_free(Confentries); Confentries = NULL; } }
static void confset_list(const struct confset *cs, const char *msg, const char *where) { char buf[BUFSIZ]; (*lfun)(LOG_DEBUG, "[%s]", msg); (*lfun)(LOG_DEBUG, "%20.20s\ttype\tproto\towner\tname\tnfail\tduration", where); for (size_t i = 0; i < cs->cs_n; i++) (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf), "", "\t", &cs->cs_c[i])); }
/* * Apply the local config match to the result */ static void conf_apply(struct conf *c, const struct conf *sc) { char buf[BUFSIZ]; if (debug) { (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "merge:\t", "", sc)); (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "to:\t", "", c)); } memcpy(c->c_name, sc->c_name, CONFNAMESZ); c->c_uid = sc->c_uid; c->c_rmask = sc->c_rmask; c->c_nfail = sc->c_nfail; c->c_duration = sc->c_duration; if (debug) (*lfun)(LOG_DEBUG, "%s: %s", __func__, conf_print(buf, sizeof(buf), "result:\t", "", c)); }
/* * Match a configuration against the given list and apply the function * to it, returning the matched entry number. */ static size_t confset_match(const struct confset *cs, struct conf *c, void (*fun)(struct conf *, const struct conf *)) { char buf[BUFSIZ]; size_t i; for (i = 0; i < cs->cs_n; i++) { if (debug) (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf), "check:\t", "", &cs->cs_c[i])); if (conf_eq(c, &cs->cs_c[i])) { if (debug) (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf), "found:\t", "", &cs->cs_c[i])); (*fun)(c, &cs->cs_c[i]); break; } } return i; }
/* * test main for conf module, usage: a.out conffile */ int main(int argc, char *argv[]) { err_init(argv[0]); setbuf(stdout, NULL); if (argc != 2) err(EF_RAW, "usage: %s conffile\n", argv[0]); conf_open(argv[1], 1); printf("conffile <%s>:\n", argv[1]); conf_print(stdout); conf_close(opts_parse(NULL, 0)); err_done(0); /* NOTREACHED */ return (0); }
/***************************************************************************** * get commandline options. * * @return 0 on success, < 0 on error. *****************************************************************************/ int get_options( int argc, char ** argv ) { char c = 0; int not_daemon = 0, cc; int want_printout = 0; char * progname = argv[0]; conf_defaults(); while( (cc = getopt( argc, argv, "c:dhP")) != EOF ) { c = (char)cc; //added by CMC 8/3/2001 switch(c) { case 'c': conf_load(optarg); break; case 'd': not_daemon = 1; break; case 'h': usage(progname,""); return -1; case 'P': want_printout = 1; break; default: usage(progname,""); return -1; } } /** unset daemon-mode if -d was given. */ if( not_daemon ) { config.daemon_mode = 0; } if( want_printout ) { conf_print(); exit(0); } return 0; }
/****************************************************************************** * *Function: conf_read * *Description: read config file and save its contents * *Input: none * *Output: none * *Return: none * *Date: 2016/8/22 * ****************************************************************************/ void conf_read() { // open xml file and read its contents if(conf_init() == CONF_FAIL) { conf_default(); return; } conf_get_dbname(); conf_get_dbtable(); conf_get_debug(); conf_get_debug_fname(); conf_get_debug_fd(); conf_get_xml_fname(); // close xml file conf_finish(); // for debug conf_print(); return; }
/* * conf_close -- close the configuration file */ void conf_close(struct opts *opts) { char cuname[PATH_MAX], tuname[PATH_MAX]; int cfd, tfd; FILE *cfp = NULL, *tfp = NULL; boolean_t safe_update = B_TRUE; if (Changed == CHG_NONE || opts_count(opts, "n") != 0) { if (opts_count(opts, "v")) (void) out("# %s and %s unchanged\n", Confname, Timesname); goto cleanup; } if (Debug > 1) { (void) fprintf(stderr, "conf_close, saving logadm context:\n"); conf_print(stderr, NULL); } cuname[0] = tuname[0] = '\0'; LOCAL_ERR_BEGIN { if (SETJMP) { safe_update = B_FALSE; LOCAL_ERR_BREAK; } if (Changed == CHG_BOTH) { if (Canchange != CHG_BOTH) err(EF_JMP, "internal error: attempting " "to update %s without locking", Confname); (void) snprintf(cuname, sizeof (cuname), "%sXXXXXX", Confname); if ((cfd = mkstemp(cuname)) == -1) err(EF_SYS|EF_JMP, "open %s replacement", Confname); if (opts_count(opts, "v")) (void) out("# writing changes to %s\n", cuname); if (fchmod(cfd, 0644) == -1) err(EF_SYS|EF_JMP, "chmod %s", cuname); if ((cfp = fdopen(cfd, "w")) == NULL) err(EF_SYS|EF_JMP, "fdopen on %s", cuname); } else { /* just toss away the configuration data */ cfp = fopen("/dev/null", "w"); } if (!Singlefile) { if (Canchange == CHG_NONE) err(EF_JMP, "internal error: attempting " "to update %s without locking", Timesname); (void) snprintf(tuname, sizeof (tuname), "%sXXXXXX", Timesname); if ((tfd = mkstemp(tuname)) == -1) err(EF_SYS|EF_JMP, "open %s replacement", Timesname); if (opts_count(opts, "v")) (void) out("# writing changes to %s\n", tuname); if (fchmod(tfd, 0644) == -1) err(EF_SYS|EF_JMP, "chmod %s", tuname); if ((tfp = fdopen(tfd, "w")) == NULL) err(EF_SYS|EF_JMP, "fdopen on %s", tuname); } conf_print(cfp, tfp); if (fclose(cfp) < 0) err(EF_SYS|EF_JMP, "fclose on %s", Confname); if (tfp != NULL && fclose(tfp) < 0) err(EF_SYS|EF_JMP, "fclose on %s", Timesname); LOCAL_ERR_END } if (!safe_update) { if (cuname[0] != 0) (void) unlink(cuname); if (tuname[0] != 0) (void) unlink(tuname); err(EF_JMP, "unsafe to update configuration file " "or timestamps"); return; } /* rename updated files into place */ if (cuname[0] != '\0') if (rename(cuname, Confname) < 0) err(EF_SYS, "rename %s to %s", cuname, Confname); if (tuname[0] != '\0') if (rename(tuname, Timesname) < 0) err(EF_SYS, "rename %s to %s", tuname, Timesname); Changed = CHG_NONE; cleanup: if (Conffd != -1) { (void) close(Conffd); Conffd = -1; } if (Timesfd != -1) { (void) close(Timesfd); Timesfd = -1; } if (Conflut) { lut_free(Conflut, free); Conflut = NULL; } if (Confentries) { fn_list_free(Confentries); Confentries = NULL; } }
const struct conf * conf_find(int fd, uid_t uid, const struct sockaddr_storage *rss, struct conf *cr) { int proto; socklen_t slen; struct sockaddr_storage lss; size_t i; char buf[BUFSIZ]; memset(cr, 0, sizeof(*cr)); slen = sizeof(lss); memset(&lss, 0, slen); if (getsockname(fd, (void *)&lss, &slen) == -1) { (*lfun)(LOG_ERR, "getsockname failed (%m)"); return NULL; } slen = sizeof(proto); if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &proto, &slen) == -1) { (*lfun)(LOG_ERR, "getsockopt failed (%m)"); return NULL; } if (debug) { sockaddr_snprintf(buf, sizeof(buf), "%a:%p", (void *)&lss); (*lfun)(LOG_DEBUG, "listening socket: %s", buf); } switch (proto) { case SOCK_STREAM: cr->c_proto = IPPROTO_TCP; break; case SOCK_DGRAM: cr->c_proto = IPPROTO_UDP; break; default: (*lfun)(LOG_ERR, "unsupported protocol %d", proto); return NULL; } switch (lss.ss_family) { case AF_INET: cr->c_port = ntohs(((struct sockaddr_in *)&lss)->sin_port); break; case AF_INET6: cr->c_port = ntohs(((struct sockaddr_in6 *)&lss)->sin6_port); break; default: (*lfun)(LOG_ERR, "unsupported family %d", lss.ss_family); return NULL; } cr->c_ss = lss; cr->c_lmask = FSTAR; cr->c_uid = (int)uid; cr->c_family = lss.ss_family; cr->c_name[0] = '\0'; cr->c_rmask = FSTAR; cr->c_nfail = FSTAR; cr->c_duration = FSTAR; if (debug) (*lfun)(LOG_DEBUG, "%s", conf_print(buf, sizeof(buf), "look:\t", "", cr)); /* match the local config */ i = confset_match(&lconf, cr, conf_apply); if (i == lconf.cs_n) { if (debug) (*lfun)(LOG_DEBUG, "not found"); return NULL; } conf_addr_set(cr, rss); /* match the remote config */ confset_match(&rconf, cr, conf_merge); /* to apply the mask */ conf_addr_set(cr, &cr->c_ss); return cr; }