/** set default logfile identity based on value from argv[0] at startup **/ static void log_ident_set_fromdefault(struct config_file* cfg, const char *log_default_identity) { if(cfg->log_identity == NULL || cfg->log_identity[0] == 0) log_ident_set(log_default_identity); else log_ident_set(cfg->log_identity); }
/** create context functionality, but no pipes */ static struct ub_ctx* ub_ctx_create_nopipe(void) { struct ub_ctx* ctx; unsigned int seed; #ifdef USE_WINSOCK int r; WSADATA wsa_data; #endif log_init(NULL, 0, NULL); /* logs to stderr */ log_ident_set("libunbound"); #ifdef USE_WINSOCK if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) { log_err("could not init winsock. WSAStartup: %s", wsa_strerror(r)); return NULL; } #endif verbosity = 0; /* errors only */ checklock_start(); ctx = (struct ub_ctx*)calloc(1, sizeof(*ctx)); if(!ctx) { errno = ENOMEM; return NULL; } alloc_init(&ctx->superalloc, NULL, 0); seed = (unsigned int)time(NULL) ^ (unsigned int)getpid(); if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) { seed = 0; ub_randfree(ctx->seed_rnd); free(ctx); errno = ENOMEM; return NULL; } seed = 0; lock_basic_init(&ctx->qqpipe_lock); lock_basic_init(&ctx->rrpipe_lock); lock_basic_init(&ctx->cfglock); ctx->env = (struct module_env*)calloc(1, sizeof(*ctx->env)); if(!ctx->env) { ub_randfree(ctx->seed_rnd); free(ctx); errno = ENOMEM; return NULL; } ctx->env->cfg = config_create_forlib(); if(!ctx->env->cfg) { free(ctx->env); ub_randfree(ctx->seed_rnd); free(ctx); errno = ENOMEM; return NULL; } ctx->env->alloc = &ctx->superalloc; ctx->env->worker = NULL; ctx->env->need_to_validate = 0; modstack_init(&ctx->mods); rbtree_init(&ctx->queries, &context_query_cmp); return ctx; }
/** main program for delayer */ int main(int argc, char** argv) { int c; /* defaults */ const char* server = "127.0.0.1@53"; const char* bindto = "0.0.0.0"; int bindport = 0; size_t memsize = 10*1024*1024; int delay = 100; verbosity = 0; log_init(0, 0, 0); log_ident_set("delayer"); srandom(time(NULL) ^ getpid()); if(argc == 1) usage(argv); while( (c=getopt(argc, argv, "b:d:f:hm:p:")) != -1) { switch(c) { case 'b': bindto = optarg; break; case 'd': if(atoi(optarg)==0 && strcmp(optarg,"0")!=0) { printf("bad delay: %s\n", optarg); return 1; } delay = atoi(optarg); break; case 'f': server = optarg; break; case 'm': if(!cfg_parse_memsize(optarg, &memsize)) { printf("bad memsize: %s\n", optarg); return 1; } break; case 'p': if(atoi(optarg)==0 && strcmp(optarg,"0")!=0) { printf("bad port nr: %s\n", optarg); return 1; } bindport = atoi(optarg); break; case 'h': case '?': default: usage(argv); } } argc -= optind; argv += optind; if(argc != 0) usage(argv); printf("bind to %s @ %d and forward to %s after %d msec\n", bindto, bindport, server, delay); service(bindto, bindport, server, memsize, delay); return 0; }
/** Main routine for checkconf */ int main(int argc, char* argv[]) { int c; const char* f; const char* opt = NULL; const char* cfgfile = CONFIGFILE; log_ident_set("unbound-checkconf"); log_init(NULL, 0, NULL); checklock_start(); #ifdef USE_WINSOCK /* use registry config file in preference to compiletime location */ if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile"))) cfgfile = CONFIGFILE; #endif /* USE_WINSOCK */ /* parse the options */ while( (c=getopt(argc, argv, "ho:")) != -1) { switch(c) { case 'o': opt = optarg; break; case '?': case 'h': default: usage(); } } argc -= optind; argv += optind; if(argc != 0 && argc != 1) usage(); if(argc == 1) f = argv[0]; else f = cfgfile; checkconf(f, opt); checklock_stop(); return 0; }
/** main program to verify all traces passed */ int main(int argc, char* argv[]) { rbtree_t* all_locks; int i; time_t starttime = time(NULL); #ifdef USE_THREAD_DEBUG /* do not overwrite the ublocktrace files with the ones generated * by this program (i.e. when the log code creates a lock) */ check_locking_order = 0; #endif if(argc <= 1) { usage(); return 1; } log_init(NULL, 0, NULL); log_ident_set("lock-verify"); /* init */ all_locks = rbtree_create(order_lock_cmp); errors_detected = 0; /* read the input files */ for(i=1; i<argc; i++) { readinput(all_locks, argv[i]); } /* check ordering */ check_order(all_locks); /* do not free a thing, OS will do it */ printf("checked %d locks in %d seconds with %d errors.\n", (int)all_locks->count, (int)(time(NULL)-starttime), errors_detected); if(errors_detected) return 1; return 0; }
/** Main routine for unbound-control */ int main(int argc, char* argv[]) { int c, ret; const char* cfgfile = CONFIGFILE; char* svr = NULL; #ifdef USE_WINSOCK int r; WSADATA wsa_data; #endif #ifdef USE_THREAD_DEBUG /* stop the file output from unbound-control, overwites the servers */ extern int check_locking_order; check_locking_order = 0; #endif /* USE_THREAD_DEBUG */ log_ident_set("unbound-control"); log_init(NULL, 0, NULL); checklock_start(); #ifdef USE_WINSOCK if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) fatal_exit("WSAStartup failed: %s", wsa_strerror(r)); #endif ERR_load_crypto_strings(); ERR_load_SSL_strings(); OpenSSL_add_all_algorithms(); (void)SSL_library_init(); if(!RAND_status()) { /* try to seed it */ unsigned char buf[256]; unsigned int v, seed=(unsigned)time(NULL) ^ (unsigned)getpid(); size_t i; for(i=0; i<256/sizeof(v); i++) { memmove(buf+i*sizeof(v), &v, sizeof(v)); v = v*seed + (unsigned int)i; } RAND_seed(buf, 256); log_warn("no entropy, seeding openssl PRNG with time\n"); } /* parse the options */ while( (c=getopt(argc, argv, "c:s:h")) != -1) { switch(c) { case 'c': cfgfile = optarg; break; case 's': svr = optarg; break; case '?': case 'h': default: usage(); } } argc -= optind; argv += optind; if(argc == 0) usage(); if(argc >= 1 && strcmp(argv[0], "start")==0) { if(execlp("unbound", "unbound", "-c", cfgfile, (char*)NULL) < 0) { fatal_exit("could not exec unbound: %s", strerror(errno)); } } ret = go(cfgfile, svr, argc, argv); #ifdef USE_WINSOCK WSACleanup(); #endif checklock_stop(); return ret; }
/** * main program. Set options given commandline arguments. * @param argc: number of commandline arguments. * @param argv: array of commandline arguments. * @return: exit status of the program. */ int main(int argc, char* argv[]) { int c; const char* cfgfile = CONFIGFILE; const char* winopt = NULL; const char* log_ident_default; int cmdline_verbose = 0; int debug_mode = 0; int need_pidfile = 1; #ifdef UB_ON_WINDOWS int cmdline_cfg = 0; #endif log_init(NULL, 0, NULL); log_ident_default = strrchr(argv[0],'/')?strrchr(argv[0],'/')+1:argv[0]; log_ident_set(log_ident_default); /* parse the options */ while( (c=getopt(argc, argv, "c:dhpvw:")) != -1) { switch(c) { case 'c': cfgfile = optarg; #ifdef UB_ON_WINDOWS cmdline_cfg = 1; #endif break; case 'v': cmdline_verbose++; verbosity++; break; case 'p': need_pidfile = 0; break; case 'd': debug_mode++; break; case 'w': winopt = optarg; break; case '?': case 'h': default: usage(); return 1; } } argc -= optind; /* argv += optind; not using further arguments */ if(winopt) { #ifdef UB_ON_WINDOWS wsvc_command_option(winopt, cfgfile, cmdline_verbose, cmdline_cfg); #else fatal_exit("option not supported"); #endif } if(argc != 0) { usage(); return 1; } run_daemon(cfgfile, cmdline_verbose, debug_mode, log_ident_default, need_pidfile); log_init(NULL, 0, NULL); /* close logfile */ #ifndef unbound_testbound if(log_get_lock()) { lock_quick_destroy((lock_quick_type*)log_get_lock()); } #endif return 0; }
/** Main routine for unbound-control */ int main(int argc, char* argv[]) { int c, ret; int quiet = 0; const char* cfgfile = CONFIGFILE; char* svr = NULL; #ifdef USE_WINSOCK int r; WSADATA wsa_data; #endif #ifdef USE_THREAD_DEBUG /* stop the file output from unbound-control, overwrites the servers */ extern int check_locking_order; check_locking_order = 0; #endif /* USE_THREAD_DEBUG */ log_ident_set("unbound-control"); log_init(NULL, 0, NULL); checklock_start(); #ifdef USE_WINSOCK /* use registry config file in preference to compiletime location */ if(!(cfgfile=w_lookup_reg_str("Software\\Unbound", "ConfigFile"))) cfgfile = CONFIGFILE; #endif /* parse the options */ while( (c=getopt(argc, argv, "c:s:qh")) != -1) { switch(c) { case 'c': cfgfile = optarg; break; case 's': svr = optarg; break; case 'q': quiet = 1; break; case '?': case 'h': default: usage(); } } argc -= optind; argv += optind; if(argc == 0) usage(); if(argc >= 1 && strcmp(argv[0], "start")==0) { if(execlp("unbound", "unbound", "-c", cfgfile, (char*)NULL) < 0) { fatal_exit("could not exec unbound: %s", strerror(errno)); } } if(argc >= 1 && strcmp(argv[0], "stats_shm")==0) { print_stats_shm(cfgfile); return 0; } #ifdef USE_WINSOCK if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) fatal_exit("WSAStartup failed: %s", wsa_strerror(r)); #endif #ifdef HAVE_ERR_LOAD_CRYPTO_STRINGS ERR_load_crypto_strings(); #endif #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL) ERR_load_SSL_strings(); #endif #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO) OpenSSL_add_all_algorithms(); #else OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); #endif #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL) (void)SSL_library_init(); #else (void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL); #endif if(!RAND_status()) { /* try to seed it */ unsigned char buf[256]; unsigned int seed=(unsigned)time(NULL) ^ (unsigned)getpid(); unsigned int v = seed; size_t i; for(i=0; i<256/sizeof(v); i++) { memmove(buf+i*sizeof(v), &v, sizeof(v)); v = v*seed + (unsigned int)i; } RAND_seed(buf, 256); log_warn("no entropy, seeding openssl PRNG with time\n"); } ret = go(cfgfile, svr, quiet, argc, argv); #ifdef USE_WINSOCK WSACleanup(); #endif checklock_stop(); return ret; }
/** * main program. Set options given commandline arguments. * @param argc: number of commandline arguments. * @param argv: array of commandline arguments. * @return: exit status of the program. */ int main(int argc, char* argv[]) { int c; const char* cfgfile = CONFIGFILE; const char* winopt = NULL; const char* log_ident_default; int cmdline_verbose = 0; int debug_mode = 0; #ifdef UB_ON_WINDOWS int cmdline_cfg = 0; #endif log_init(NULL, 0, NULL); log_ident_default = strrchr(argv[0],'/')?strrchr(argv[0],'/')+1:argv[0]; log_ident_set(log_ident_default); /* parse the options */ while( (c=getopt(argc, argv, "c:dhvw:")) != -1) { switch(c) { case 'c': cfgfile = optarg; #ifdef UB_ON_WINDOWS cmdline_cfg = 1; #endif break; case 'v': cmdline_verbose++; verbosity++; break; case 'd': debug_mode++; break; case 'w': winopt = optarg; break; case '?': case 'h': default: usage(); return 1; } } argc -= optind; argv += optind; if(winopt) { #ifdef UB_ON_WINDOWS wsvc_command_option(winopt, cfgfile, cmdline_verbose, cmdline_cfg); #else fatal_exit("option not supported"); #endif } if(argc != 0) { usage(); return 1; } run_daemon(cfgfile, cmdline_verbose, debug_mode, log_ident_default); log_init(NULL, 0, NULL); /* close logfile */ return 0; }
/** main program for perf */ int main(int argc, char* argv[]) { char* nm = argv[0]; int c; struct perfinfo info; #ifdef USE_WINSOCK int r; WSADATA wsa_data; #endif /* defaults */ memset(&info, 0, sizeof(info)); info.io_num = 16; log_init(NULL, 0, NULL); log_ident_set("perf"); checklock_start(); #ifdef USE_WINSOCK if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) fatal_exit("WSAStartup failed: %s", wsa_strerror(r)); #endif info.buf = sldns_buffer_new(65553); if(!info.buf) fatal_exit("out of memory"); /* parse the options */ while( (c=getopt(argc, argv, "d:ha:f:q")) != -1) { switch(c) { case 'q': info.quiet = 1; break; case 'd': if(atoi(optarg)==0 && strcmp(optarg, "0")!=0) { printf("-d not a number %s", optarg); return 1; } info.duration = atoi(optarg); break; case 'a': qlist_add_line(&info, optarg, 0); break; case 'f': qlist_read_file(&info, optarg); break; case '?': case 'h': default: usage(nm); } } argc -= optind; argv += optind; if(argc != 1) { printf("error: pass server IP address on commandline.\n"); usage(nm); } if(!extstrtoaddr(argv[0], &info.dest, &info.destlen)) { printf("Could not parse ip: %s\n", argv[0]); return 1; } if(info.qlist_size == 0) { printf("No queries to make, use -f or -a.\n"); return 1; } /* do the performance test */ perfmain(&info); sldns_buffer_free(info.buf); #ifdef USE_WINSOCK WSACleanup(); #endif checklock_stop(); return 0; }