struct ub_ctx* 
ub_ctx_create(void)
{
	struct ub_ctx* ctx = ub_ctx_create_nopipe();
	if(!ctx)
		return NULL;
	if((ctx->qq_pipe = tube_create()) == NULL) {
		int e = errno;
		ub_randfree(ctx->seed_rnd);
		config_delete(ctx->env->cfg);
		modstack_desetup(&ctx->mods, ctx->env);
		free(ctx->env);
		free(ctx);
		errno = e;
		return NULL;
	}
	if((ctx->rr_pipe = tube_create()) == NULL) {
		int e = errno;
		tube_delete(ctx->qq_pipe);
		ub_randfree(ctx->seed_rnd);
		config_delete(ctx->env->cfg);
		modstack_desetup(&ctx->mods, ctx->env);
		free(ctx->env);
		free(ctx);
		errno = e;
		return NULL;
	}
	return ctx;
}
Exemple #2
0
static int _appbroker(char const * outfile, char const * filename)
{
	AppBroker appbroker;

	if((appbroker.config = config_new()) == NULL)
		return error_print(APPBROKER_PROGNAME);
	if(config_load(appbroker.config, filename) != 0)
	{
		config_delete(appbroker.config);
		return error_print(APPBROKER_PROGNAME);
	}
	appbroker.prefix = config_get(appbroker.config, NULL, "service");
	if((appbroker.outfile = outfile) == NULL)
		appbroker.fp = stdout;
	else if((appbroker.fp = fopen(outfile, "w")) == NULL)
	{
		config_delete(appbroker.config);
		return error_set_print(APPBROKER_PROGNAME, 1, "%s: %s", outfile,
				strerror(errno));
	}
	_appbroker_head(&appbroker);
	_appbroker_constants(&appbroker);
	_appbroker_calls(&appbroker);
	_appbroker_tail(&appbroker);
	if(outfile != NULL)
		fclose(appbroker.fp);
	config_delete(appbroker.config);
	return 0;
}
Exemple #3
0
int camera_save(Camera * camera)
{
	int ret = -1;
	char * filename;
	Config * config;
	char const * sformats[CSF_COUNT] = { NULL, "png", "jpeg" };

	if((filename = _camera_get_config_filename(camera, CAMERA_CONFIG_FILE))
			== NULL)
		return -1;
	if((config = config_new()) != NULL
			&& access(filename, R_OK) == 0
			&& config_load(config, filename) == 0)
	{
		/* XXX may fail */
		_save_variable_bool(camera, config, NULL, "hflip",
				camera->hflip);
		_save_variable_bool(camera, config, NULL, "vflip",
				camera->vflip);
		_save_variable_bool(camera, config, NULL, "ratio",
				camera->ratio);
		_save_variable_string(camera, config, "snapshot", "format",
				sformats[camera->snapshot_format]);
		_save_variable_int(camera, config, "snapshot", "quality",
				camera->snapshot_quality);
		/* FIXME also implement interpolation and overlay images */
		ret = config_save(config, filename);
	}
	if(config != NULL)
		config_delete(config);
	free(filename);
	return ret;
}
Exemple #4
0
/**
 * Run the daemon. 
 * @param cfgfile: the config file name.
 * @param cmdline_verbose: verbosity resulting from commandline -v.
 *    These increase verbosity as specified in the config file.
 * @param debug_mode: if set, do not daemonize.
 * @param log_default_identity: Default identity to report in logs
 */
static void 
run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode, const char* log_default_identity)
{
	struct config_file* cfg = NULL;
	struct daemon* daemon = NULL;
	int done_setup = 0;

	if(!(daemon = daemon_init()))
		fatal_exit("alloc failure");
	while(!daemon->need_to_exit) {
		if(done_setup)
			verbose(VERB_OPS, "Restart of %s.", PACKAGE_STRING);
		else	verbose(VERB_OPS, "Start of %s.", PACKAGE_STRING);

		/* config stuff */
		if(!(cfg = config_create()))
			fatal_exit("Could not alloc config defaults");
		if(!config_read(cfg, cfgfile, daemon->chroot)) {
			if(errno != ENOENT)
				fatal_exit("Could not read config file: %s",
					cfgfile);
			log_warn("Continuing with default config settings");
		}
		apply_settings(daemon, cfg, cmdline_verbose, debug_mode, log_default_identity);
		if(!done_setup)
			config_lookup_uid(cfg);
	
		/* prepare */
		if(!daemon_open_shared_ports(daemon))
			fatal_exit("could not open ports");
		if(!done_setup) { 
			perform_setup(daemon, cfg, debug_mode, &cfgfile);
			done_setup = 1; 
		} else {
			/* reopen log after HUP to facilitate log rotation */
			if(!cfg->use_syslog)
				log_init(cfg->logfile, 0, cfg->chrootdir);
		}
		/* work */
		daemon_fork(daemon);

		/* clean up for restart */
		verbose(VERB_ALGO, "cleanup.");
		daemon_cleanup(daemon);
		config_delete(cfg);
	}
	verbose(VERB_ALGO, "Exit cleanup.");
	/* this unlink may not work if the pidfile is located outside
	 * of the chroot/workdir or we no longer have permissions */
	if(daemon->pidfile) {
		int fd;
		/* truncate pidfile */
		fd = open(daemon->pidfile, O_WRONLY | O_TRUNC, 0644);
		if(fd != -1)
			close(fd);
		/* delete pidfile */
		unlink(daemon->pidfile);
	}
	daemon_delete(daemon);
}
Exemple #5
0
/* main */
int main(int argc, char * argv[])
{
	Config * config;
	Compose * compose;
	char const * subject = NULL;
	int o;

	if(setlocale(LC_ALL, "") == NULL)
		_error("setlocale", 1);
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	gtk_init(&argc, &argv);
	while((o = getopt(argc, argv, "s:")) != -1)
		switch(o)
		{
			case 's':
				subject = optarg;
				break;
			default:
				return _usage();
		}
	config = _compose_config();
	if((compose = _compose(config, subject, argc - optind, &argv[optind]))
			!= NULL)
	{
		gtk_main();
		compose_delete(compose);
	}
	if(config != NULL)
		config_delete(config);
	return 0;
}
Exemple #6
0
/**
 * Deinit the service
 */
static void
service_deinit(struct daemon* daemon, struct config_file* cfg)
{
	daemon_cleanup(daemon);
	config_delete(cfg);
	daemon_delete(daemon);
}
int main(int argc, char **argv)
{
    struct configuration *config = NULL;
    struct question_db *qdb = NULL;
    struct template_db *tdb = NULL;
    int flags = 0;
    char *owner;
    int i;

    setlocale(LC_ALL, "");

    config = config_new();
    parsecmdline(config, argc, argv);

    /* always load all translations if running standalone */
    unsetenv("DEBCONF_DROP_TRANSLATIONS");

    /* If debconf is already running, use debconfclient to load
     * the templates;
     * This is a hack until we introduce a standard debconf
     * primitive for doing this.
     */
    if (getenv("DEBIAN_HAS_FRONTEND") != NULL)
    {
         add_questions_debconf(argc, argv);
         exit(0);
    }

    /* parse the configuration info */
    if (config->read(config, DEBCONFCONFIG) == 0)
        DIE("Error reading configuration information");

    /* initialize database modules */
    if ((tdb = template_db_new(config, NULL)) == 0)
        DIE("Cannot initialize DebConf template database");
    if ((qdb = question_db_new(config, tdb, NULL)) == 0)
        DIE("Cannot initialize DebConf config database");

    tdb->methods.load(tdb);
    qdb->methods.load(qdb);

    owner = argv[optind];
    i = optind + 1;
    if (merge)
        flags |= DC_LOADTEMPLATE_MERGE;
    while (i < argc)
    {
        template_db_loadfile(tdb, qdb, argv[i++], owner, flags);
    }

    if (tdb->methods.save(tdb) != DC_OK)
	exit(1);
    if (qdb->methods.save(qdb) != DC_OK)
	exit(1);
    template_db_delete(tdb);
    question_db_delete(qdb);
    config_delete(config);

    return EXIT_SUCCESS;
}
Exemple #8
0
/** go ahead and read config, contact server and perform command and display */
static int
go(const char* cfgfile, char* svr, int argc, char* argv[])
{
	struct config_file* cfg;
	int fd, ret;
	SSL_CTX* ctx;
	SSL* ssl;

	/* read config */
	if(!(cfg = config_create()))
		fatal_exit("out of memory");
	if(!config_read(cfg, cfgfile, NULL))
		fatal_exit("could not read config file");
	if(!cfg->remote_control_enable)
		log_warn("control-enable is 'no' in the config file.");
	ctx = setup_ctx(cfg);
	
	/* contact server */
	fd = contact_server(svr, cfg, argc>0&&strcmp(argv[0],"status")==0);
	ssl = setup_ssl(ctx, fd);
	
	/* send command */
	ret = go_cmd(ssl, argc, argv);

	SSL_free(ssl);
#ifndef USE_WINSOCK
	close(fd);
#else
	closesocket(fd);
#endif
	SSL_CTX_free(ctx);
	config_delete(cfg);
	return ret;
}
Exemple #9
0
/* note_delete */
void note_delete(Note * note)
{
	string_delete(note->description);
	free(note->filename);
	if(note->config != NULL)
		config_delete(note->config);
	object_delete(note);
}
Exemple #10
0
/* directory_delete */
void directory_delete(Directory * directory)
{
	if(directory->config != NULL)
		config_delete(directory->config);
	if(directory->appserver != NULL)
		appserver_delete(directory->appserver);
	object_delete(directory);
}
Exemple #11
0
/* ussd_destroy */
static void _ussd_destroy(USSD * ussd)
{
	if(ussd->window != NULL)
		gtk_widget_destroy(ussd->window);
	free(ussd->_operator);
	if(ussd->config != NULL)
		config_delete(ussd->config);
	object_delete(ussd);
}
Exemple #12
0
static void _new_config(Directory * directory)
{
	char const * root = NULL;

	if((directory->config = config_new()) == NULL)
		return;
	if(config_load(directory->config, DIRECTORY_CONF) != 0)
	{
		config_delete(directory->config);
		directory->config = NULL;
		return;
	}
	if((root = config_get(directory->config, "", "root")) != NULL
			&& chdir(root) == 0)
		return; /* succeeded */
	if(root != NULL)
		error_set_code(1, "%s%s%s", root, ": ", strerror(errno));
	config_delete(directory->config);
	directory->config = NULL;
}
Exemple #13
0
void 
ub_ctx_delete(struct ub_ctx* ctx)
{
	struct alloc_cache* a, *na;
	int do_stop = 1;
	if(!ctx) return;

	/* see if bg thread is created and if threads have been killed */
	/* no locks, because those may be held by terminated threads */
	/* for processes the read pipe is closed and we see that on read */
#ifdef HAVE_PTHREAD
	if(ctx->created_bg && ctx->dothread) {
		if(pthread_kill(ctx->bg_tid, 0) == ESRCH) {
			/* thread has been killed */
			do_stop = 0;
		}
	}
#endif /* HAVE_PTHREAD */
	if(do_stop)
		ub_stop_bg(ctx);
	libworker_delete_event(ctx->event_worker);

	modstack_desetup(&ctx->mods, ctx->env);
	a = ctx->alloc_list;
	while(a) {
		na = a->super;
		a->super = &ctx->superalloc;
		alloc_clear(a);
		free(a);
		a = na;
	}
	local_zones_delete(ctx->local_zones);
	lock_basic_destroy(&ctx->qqpipe_lock);
	lock_basic_destroy(&ctx->rrpipe_lock);
	lock_basic_destroy(&ctx->cfglock);
	tube_delete(ctx->qq_pipe);
	tube_delete(ctx->rr_pipe);
	if(ctx->env) {
		slabhash_delete(ctx->env->msg_cache);
		rrset_cache_delete(ctx->env->rrset_cache);
		infra_delete(ctx->env->infra_cache);
		config_delete(ctx->env->cfg);
		edns_known_options_delete(ctx->env);
		auth_zones_delete(ctx->env->auth_zones);
		free(ctx->env);
	}
	ub_randfree(ctx->seed_rnd);
	alloc_clear(&ctx->superalloc);
	traverse_postorder(&ctx->queries, delq, NULL);
	free(ctx);
#ifdef USE_WINSOCK
	WSACleanup();
#endif
}
Exemple #14
0
int camera_load(Camera * camera)
{
	int ret = 0;
	char * filename;
	Config * config;
	char const * p;
	char * q;
	char const jpeg[] = "jpeg";
	int i;

	if((filename = _camera_get_config_filename(camera, CAMERA_CONFIG_FILE))
			== NULL)
		return -1;
	if((config = config_new()) == NULL
			|| config_load(config, filename) != 0)
		ret = -1;
	else
	{
		/* horizontal flipping */
		camera->hflip = FALSE;
		if((p = _load_variable(camera, config, NULL, "hflip")) != NULL
				&& strtoul(p, NULL, 0) != 0)
			camera->hflip = TRUE;
		/* vertical flipping */
		camera->vflip = FALSE;
		if((p = _load_variable(camera, config, NULL, "vflip")) != NULL
				&& strtoul(p, NULL, 0) != 0)
			camera->vflip = TRUE;
		/* aspect ratio */
		camera->ratio = TRUE;
		if((p = _load_variable(camera, config, NULL, "ratio")) != NULL
				&& strtoul(p, NULL, 0) == 0)
			camera->ratio = FALSE;
		/* snapshot format */
		camera->snapshot_format = CSF_PNG;
		if((p = _load_variable(camera, config, "snapshot", "format"))
				!= NULL
				&& strcmp(p, jpeg) == 0)
			camera->snapshot_format = CSF_JPEG;
		/* snapshot quality */
		camera->snapshot_quality = 100;
		if((p = _load_variable(camera, config, "snapshot", "quality"))
				!= NULL
				&& p[0] != '\0' && (i = strtol(p, &q, 10)) >= 0
				&& *q == '\0' && i <= 100)
			camera->snapshot_quality = i;
		/* FIXME also implement interpolation and overlay images */
	}
	if(config != NULL)
		config_delete(config);
	free(filename);
	return ret;
}
Exemple #15
0
/* panel_delete */
void panel_delete(Panel * panel)
{
	if(panel->source != 0)
		g_source_remove(panel->source);
	/* FIXME destroy plugins as well */
	if(panel->top != NULL)
		panel_window_delete(panel->top);
	if(panel->bottom != NULL)
		panel_window_delete(panel->bottom);
	if(panel->config != NULL)
		config_delete(panel->config);
	object_delete(panel);
}
Exemple #16
0
int main(){
   Config* conf = config_new("config.conf");
   if(conf){
      int i;
      for(i=0; i<conf->cnt; ++i){
          printf("%s=%s\n",conf->keys[i],conf->values[i]);
      }
   }
   printf("section size:%d\n",config_section_size(conf));
   config_foreach_section(conf,each_section_fn,NULL);
   config_delete(conf);
   conf = config_new_from_strbuf(" aa=bb   vendor=AMD ");
   if(conf){
      int i;
      for(i=0; i<conf->cnt; ++i){
          printf("%s=%s\n",conf->keys[i],conf->values[i]);
      }
   }
   config_delete(conf);

   return 0;
}
Exemple #17
0
static void set_config(void)
{
	char config_key[6];
	int i;

	strcpy(config_key, "ir_");
	for(i=0;i<64;i++) {
		sprintf(&config_key[3], "%02x", i);
		if(key_bindings[i][0] != 0)
			config_write_string(config_key, key_bindings[i]);
		else
			config_delete(config_key);
	}
}
Exemple #18
0
static int _settings_browse(Settings * settings)
{
	int ret = 0;
	Config * config;
	GtkTreeModel * model;
	char const * path;
	char * p;
	size_t i;
	size_t j;
	int datadir = 1;

	if((config = config_new()) == NULL)
		return -_settings_error(error_get(NULL), 1);
	model = _settings_get_model(settings);
	gtk_list_store_clear(GTK_LIST_STORE(model));
	/* read through every XDG application folder */
	if((path = getenv("XDG_DATA_DIRS")) == NULL || strlen(path) == 0)
	{
		path = "/usr/local/share:/usr/share";
		datadir = 0;
	}
	if((p = strdup(path)) == NULL)
		_settings_error(error_get(NULL), 1);
	else
		for(i = 0, j = 0;; i++)
			if(p[i] == '\0')
			{
				string_rtrim(&p[j], "/");
				_settings_browse_folder(settings, config,
						&p[j]);
				datadir |= (strcmp(&p[j], DATADIR) == 0);
				break;
			}
			else if(p[i] == ':')
			{
				p[i] = '\0';
				string_rtrim(&p[j], "/");
				_settings_browse_folder(settings, config,
						&p[j]);
				datadir |= (strcmp(&p[j], DATADIR) == 0);
				j = i + 1;
			}
	free(p);
	if(datadir == 0)
		ret = _settings_browse_folder(settings, config, DATADIR);
	ret |= _settings_browse_home(settings, config);
	config_delete(config);
	return ret;
}
Exemple #19
0
static void _trash_list(Trash * trash)
{
	Config * config;
	char * path;
	DIR * dir;
	struct dirent * de;
	time_t sixmonths;

#ifdef DEBUG
	fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
	/* FIXME report errors */
	if((path = _trash_get_path(DATA_TRASHINFO)) == NULL)
		return;
	if((config = config_new()) == NULL)
	{
		free(path);
		return;
	}
	/* FIXME should try to create the directory */
	if((dir = opendir(path)) == NULL)
	{
		config_delete(config);
		free(path);
		return;
	}
	/* FIXME refresh only if necessary */
	_list_reset(trash);
	sixmonths = time(NULL) - 15552000;
	while((de = readdir(dir)) != NULL)
		_list_add(trash, config, path, de->d_name, sixmonths);
	closedir(dir);
	_list_purge(trash);
	config_delete(config);
	free(path);
}
Exemple #20
0
/** check config file */
static void
checkconf(const char* cfgfile, const char* opt)
{
	struct config_file* cfg = config_create();
	if(!cfg)
		fatal_exit("out of memory");
	if(!config_read(cfg, cfgfile, NULL)) {
		/* config_read prints messages to stderr */
		config_delete(cfg);
		exit(1);
	}
	morechecks(cfg, cfgfile);
	check_mod(cfg, iter_get_funcblock());
	check_mod(cfg, val_get_funcblock());
#ifdef WITH_PYTHONMODULE
	if(strstr(cfg->module_conf, "python"))
		check_mod(cfg, pythonmod_get_funcblock());
#endif
	check_fwd(cfg);
	check_hints(cfg);
	if(opt) print_option(cfg, opt);
	else	printf("unbound-checkconf: no errors in %s\n", cfgfile);
	config_delete(cfg);
}
/************************************************************************
 * Function: cleanup
 * Inputs: none
 * Outputs: none
 * Description: cleans up if before we exit
 * Assumptions: none
 ************************************************************************/
static void cleanup(void)
{
	if (g_frontend != NULL)
		frontend_delete(g_frontend);
	if (g_templates != NULL)
	{
		g_templates->methods.save(g_templates);
		template_db_delete(g_templates);
	}
	if (g_questions != NULL)
	{
		g_questions->methods.save(g_questions);
		question_db_delete(g_questions);
	}
	if (g_config != NULL)
		config_delete(g_config);
}
Exemple #22
0
/* main */
int main(int argc, char * argv[])
{
	PhonePluginHelper helper;
	PhoneEngineering pe;
	Phone * p;

	gtk_init(&argc, &argv);
	pe.config = config_new();
	config_load(pe.config, "/home/khorben/.phone"); /* FIXME hardcoded */
	p = &pe;
	helper.phone = p;
	helper.config_get = _helper_config_get;
	pe.plugin = _engineering_init(&helper);
	gtk_main();
	_engineering_destroy(pe.plugin);
	config_delete(pe.config);
	return 0;
}
/** print statistics from shm memory segment */
static void print_stats_shm(const char* cfgfile)
{
#ifdef HAVE_SHMGET
	struct config_file* cfg;
	struct ub_stats_info* stats;
	struct ub_shm_stat_info* shm_stat;
	int id_ctl, id_arr;
	/* read config */
	if(!(cfg = config_create()))
		fatal_exit("out of memory");
	if(!config_read(cfg, cfgfile, NULL))
		fatal_exit("could not read config file");
	/* get shm segments */
	id_ctl = shmget(cfg->shm_key, sizeof(int), SHM_R|SHM_W);
	if(id_ctl == -1) {
		fatal_exit("shmget(%d): %s", cfg->shm_key, strerror(errno));
	}
	id_arr = shmget(cfg->shm_key+1, sizeof(int), SHM_R|SHM_W);
	if(id_arr == -1) {
		fatal_exit("shmget(%d): %s", cfg->shm_key+1, strerror(errno));
	}
	shm_stat = (struct ub_shm_stat_info*)shmat(id_ctl, NULL, 0);
	if(shm_stat == (void*)-1) {
		fatal_exit("shmat(%d): %s", id_ctl, strerror(errno));
	}
	stats = (struct ub_stats_info*)shmat(id_arr, NULL, 0);
	if(stats == (void*)-1) {
		fatal_exit("shmat(%d): %s", id_arr, strerror(errno));
	}

	/* print the stats */
	do_stats_shm(cfg, stats, shm_stat);

	/* shutdown */
	shmdt(shm_stat);
	shmdt(stats);
	config_delete(cfg);
#else
	(void)cfgfile;
#endif /* HAVE_SHMGET */
}
Exemple #24
0
/// Cleanup utility function.
///
/// This will delete the global objects, if they've been allocated.
static void _cleanup(locality_t *l) {
  if (!l)
    return;

#ifdef HAVE_APEX
  apex_finalize();
#endif

  if (l->percolation) {
    percolation_delete(l->percolation);
    l->percolation = NULL;
  }

  if (l->gas) {
    gas_dealloc(l->gas);
    l->gas = NULL;
  }

  dbg_fini();

  if (l->boot) {
    boot_delete(l->boot);
    l->boot = NULL;
  }

  if (l->topology) {
    topology_delete(l->topology);
    l->topology = NULL;
  }

  action_table_finalize();
  inst_fini();

  if (l->config) {
    config_delete(l->config);
  }

  free(l);
}
Exemple #25
0
struct shear* shear_delete(struct shear* shear) {

    if (shear != NULL) {
        shear->fptr = shear_close_output(shear);

        shear->config   = config_delete(shear->config);
        shear->lcat     = lcat_delete(shear->lcat);
        shear->scat     = scat_delete(shear->scat);
        shear->hpix     = hpix_delete(shear->hpix);
        shear->cosmo    = cosmo_delete(shear->cosmo);
        shear->pixstack = i64stack_delete(shear->pixstack);
#ifdef NO_INCREMENTAL_WRITE
        shear->lensums  = lensums_delete(shear->lensums);
#else
        shear->lensum  = lensum_delete(shear->lensum);
        shear->lensum_tot  = lensum_delete(shear->lensum_tot);
#endif

    }
    free(shear);
    return NULL;
}
Exemple #26
0
int configure(Prefs * prefs, char const * directory)
{
	int ret;
	Configure cfgr;
	configArray * ca;
	const int flags = prefs->flags;
	int i;
	Config * p;

	if((ca = configarray_new()) == NULL)
		return error_print(PROGNAME);
	cfgr.prefs = prefs;
	_configure_detect(&cfgr);
	_configure_detect_extensions(&cfgr);
	_configure_detect_programs(&cfgr);
	if((ret = _configure_load(prefs, directory, ca)) == 0)
	{
		if(prefs->flags & PREFS_n)
			ret = _configure_do(&cfgr, ca);
		else
		{
			prefs->flags = PREFS_n;
			if(_configure_do(&cfgr, ca) == 0)
			{
				prefs->flags = flags;
				ret = _configure_do(&cfgr, ca);
			}
		}
	}
	for(i = array_count(ca); i > 0; i--)
	{
		array_get_copy(ca, i - 1, &p);
		config_delete(p);
	}
	array_delete(ca);
	return ret;
}
Exemple #27
0
void
ub_ctx_delete(struct ub_ctx* ctx)
{
    struct alloc_cache* a, *na;
    if(!ctx) return;
    /* stop the bg thread */
    lock_basic_lock(&ctx->cfglock);
    if(ctx->created_bg) {
        uint8_t* msg;
        uint32_t len;
        uint32_t cmd = UB_LIBCMD_QUIT;
        lock_basic_unlock(&ctx->cfglock);
        lock_basic_lock(&ctx->qqpipe_lock);
        (void)tube_write_msg(ctx->qq_pipe, (uint8_t*)&cmd,
                             (uint32_t)sizeof(cmd), 0);
        lock_basic_unlock(&ctx->qqpipe_lock);
        lock_basic_lock(&ctx->rrpipe_lock);
        while(tube_read_msg(ctx->rr_pipe, &msg, &len, 0)) {
            /* discard all results except a quit confirm */
            if(context_serial_getcmd(msg, len) == UB_LIBCMD_QUIT) {
                free(msg);
                break;
            }
            free(msg);
        }
        lock_basic_unlock(&ctx->rrpipe_lock);

        /* if bg worker is a thread, wait for it to exit, so that all
         * resources are really gone. */
        lock_basic_lock(&ctx->cfglock);
        if(ctx->dothread) {
            lock_basic_unlock(&ctx->cfglock);
            ub_thread_join(ctx->bg_tid);
        } else {
            lock_basic_unlock(&ctx->cfglock);
        }
    }
    else {
        lock_basic_unlock(&ctx->cfglock);
    }


    modstack_desetup(&ctx->mods, ctx->env);
    a = ctx->alloc_list;
    while(a) {
        na = a->super;
        a->super = &ctx->superalloc;
        alloc_clear(a);
        free(a);
        a = na;
    }
    local_zones_delete(ctx->local_zones);
    lock_basic_destroy(&ctx->qqpipe_lock);
    lock_basic_destroy(&ctx->rrpipe_lock);
    lock_basic_destroy(&ctx->cfglock);
    tube_delete(ctx->qq_pipe);
    tube_delete(ctx->rr_pipe);
    if(ctx->env) {
        slabhash_delete(ctx->env->msg_cache);
        rrset_cache_delete(ctx->env->rrset_cache);
        infra_delete(ctx->env->infra_cache);
        config_delete(ctx->env->cfg);
        free(ctx->env);
    }
    ub_randfree(ctx->seed_rnd);
    alloc_clear(&ctx->superalloc);
    traverse_postorder(&ctx->queries, delq, NULL);
    free(ctx);
#ifdef USE_WINSOCK
    WSACleanup();
#endif
}
Exemple #28
0
struct config_file* 
config_create(void)
{
	struct config_file* cfg;
	cfg = (struct config_file*)calloc(1, sizeof(struct config_file));
	if(!cfg)
		return NULL;
	/* the defaults if no config is present */
	cfg->verbosity = 1;
	cfg->stat_interval = 0;
	cfg->stat_cumulative = 0;
	cfg->stat_extended = 0;
	cfg->num_threads = 1;
	cfg->port = UNBOUND_DNS_PORT;
	cfg->do_ip4 = 1;
	cfg->do_ip6 = 1;
	cfg->do_udp = 1;
	cfg->do_tcp = 1;
	cfg->tcp_upstream = 0;
	cfg->ssl_service_key = NULL;
	cfg->ssl_service_pem = NULL;
	cfg->ssl_port = 443;
	cfg->ssl_upstream = 0;
	cfg->use_syslog = 1;
	cfg->log_time_ascii = 0;
	cfg->log_queries = 0;
#ifndef USE_WINSOCK
#  ifdef USE_MINI_EVENT
	/* select max 1024 sockets */
	cfg->outgoing_num_ports = 960;
	cfg->num_queries_per_thread = 512;
#  else
	/* libevent can use many sockets */
	cfg->outgoing_num_ports = 4096;
	cfg->num_queries_per_thread = 1024;
#  endif
	cfg->outgoing_num_tcp = 10;
	cfg->incoming_num_tcp = 10;
#else
	cfg->outgoing_num_ports = 48; /* windows is limited in num fds */
	cfg->num_queries_per_thread = 24;
	cfg->outgoing_num_tcp = 2; /* leaves 64-52=12 for: 4if,1stop,thread4 */
	cfg->incoming_num_tcp = 2; 
#endif
	cfg->edns_buffer_size = 4096; /* 4k from rfc recommendation */
	cfg->msg_buffer_size = 65552; /* 64 k + a small margin */
	cfg->msg_cache_size = 4 * 1024 * 1024;
	cfg->msg_cache_slabs = 4;
	cfg->jostle_time = 200;
	cfg->rrset_cache_size = 4 * 1024 * 1024;
	cfg->rrset_cache_slabs = 4;
	cfg->host_ttl = 900;
	cfg->bogus_ttl = 60;
	cfg->min_ttl = 0;
	cfg->max_ttl = 3600 * 24;
	cfg->prefetch = 0;
	cfg->prefetch_key = 0;
	cfg->infra_cache_slabs = 4;
	cfg->infra_cache_numhosts = 10000;
	if(!(cfg->outgoing_avail_ports = (int*)calloc(65536, sizeof(int))))
		goto error_exit;
	init_outgoing_availports(cfg->outgoing_avail_ports, 65536);
	if(!(cfg->username = strdup(UB_USERNAME))) goto error_exit;
#ifdef HAVE_CHROOT
	if(!(cfg->chrootdir = strdup(CHROOT_DIR))) goto error_exit;
#endif
	if(!(cfg->directory = strdup(RUN_DIR))) goto error_exit;
	if(!(cfg->logfile = strdup(""))) goto error_exit;
	if(!(cfg->pidfile = strdup(PIDFILE))) goto error_exit;
	if(!(cfg->target_fetch_policy = strdup("3 2 1 0 0"))) goto error_exit;
	cfg->donotqueryaddrs = NULL;
	cfg->donotquery_localhost = 1;
	cfg->root_hints = NULL;
	cfg->do_daemonize = 1;
	cfg->if_automatic = 0;
	cfg->so_rcvbuf = 0;
	cfg->so_sndbuf = 0;
	cfg->num_ifs = 0;
	cfg->ifs = NULL;
	cfg->num_out_ifs = 0;
	cfg->out_ifs = NULL;
	cfg->stubs = NULL;
	cfg->forwards = NULL;
	cfg->acls = NULL;
	cfg->harden_short_bufsize = 0;
	cfg->harden_large_queries = 0;
	cfg->harden_glue = 1;
	cfg->harden_dnssec_stripped = 1;
	cfg->harden_below_nxdomain = 0;
	cfg->harden_referral_path = 0;
	cfg->use_caps_bits_for_id = 0;
	cfg->private_address = NULL;
	cfg->private_domain = NULL;
	cfg->unwanted_threshold = 0;
	cfg->hide_identity = 0;
	cfg->hide_version = 0;
	cfg->identity = NULL;
	cfg->version = NULL;
	cfg->auto_trust_anchor_file_list = NULL;
	cfg->trust_anchor_file_list = NULL;
	cfg->trust_anchor_list = NULL;
	cfg->trusted_keys_file_list = NULL;
	cfg->dlv_anchor_file = NULL;
	cfg->dlv_anchor_list = NULL;
	cfg->domain_insecure = NULL;
	cfg->val_date_override = 0;
	cfg->val_sig_skew_min = 3600; /* at least daylight savings trouble */
	cfg->val_sig_skew_max = 86400; /* at most timezone settings trouble */
	cfg->val_clean_additional = 1;
	cfg->val_log_level = 0;
	cfg->val_log_squelch = 0;
	cfg->val_permissive_mode = 0;
	cfg->ignore_cd = 0;
	cfg->add_holddown = 30*24*3600;
	cfg->del_holddown = 30*24*3600;
	cfg->keep_missing = 366*24*3600; /* one year plus a little leeway */
	cfg->key_cache_size = 4 * 1024 * 1024;
	cfg->key_cache_slabs = 4;
	cfg->neg_cache_size = 1 * 1024 * 1024;
	cfg->local_zones = NULL;
	cfg->local_zones_nodefault = NULL;
	cfg->local_data = NULL;
	cfg->python_script = NULL;
	cfg->remote_control_enable = 0;
	cfg->control_ifs = NULL;
	cfg->control_port = UNBOUND_CONTROL_PORT;
	cfg->minimal_responses = 0;
	cfg->rrset_roundrobin = 0;
	if(!(cfg->server_key_file = strdup(RUN_DIR"/unbound_server.key"))) 
		goto error_exit;
	if(!(cfg->server_cert_file = strdup(RUN_DIR"/unbound_server.pem"))) 
		goto error_exit;
	if(!(cfg->control_key_file = strdup(RUN_DIR"/unbound_control.key"))) 
		goto error_exit;
	if(!(cfg->control_cert_file = strdup(RUN_DIR"/unbound_control.pem"))) 
		goto error_exit;

	if(!(cfg->module_conf = strdup("validator iterator"))) goto error_exit;
	if(!(cfg->val_nsec3_key_iterations = 
		strdup("1024 150 2048 500 4096 2500"))) goto error_exit;
	return cfg;
error_exit:
	config_delete(cfg); 
	return NULL;
}
Exemple #29
0
/**
 * The main function for the service.
 * Called by the services API when starting unbound on windows in background.
 * Arguments could have been present in the string 'path'.
 * @param argc: nr args
 * @param argv: arg text.
 */
static void 
service_main(DWORD ATTR_UNUSED(argc), LPTSTR* ATTR_UNUSED(argv))
{
	struct config_file* cfg = NULL;
	struct daemon* daemon = NULL;

	service_status_handle = RegisterServiceCtrlHandler(SERVICE_NAME, 
		(LPHANDLER_FUNCTION)hdlr);
	if(!service_status_handle) {
		reportev("Could not RegisterServiceCtrlHandler");
		return;
	}
	
	service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	service_status.dwServiceSpecificExitCode = 0;

	/* see if we have root anchor update enabled */
	call_root_update();

	/* we are now starting up */
	report_status(SERVICE_START_PENDING, NO_ERROR, 3000);
	if(!service_init(0, &daemon, &cfg)) {
		reportev("Could not service_init");
		report_status(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}

	/* event that gets signalled when we want to quit; it
	 * should get registered in the worker-0 waiting loop. */
	service_stop_event = WSACreateEvent();
	if(service_stop_event == WSA_INVALID_EVENT) {
		log_err("WSACreateEvent: %s", wsa_strerror(WSAGetLastError()));
		reportev("Could not WSACreateEvent");
		report_status(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}
	if(!WSAResetEvent(service_stop_event)) {
		log_err("WSAResetEvent: %s", wsa_strerror(WSAGetLastError()));
	}

	/* SetServiceStatus SERVICE_RUNNING;*/
	report_status(SERVICE_RUNNING, NO_ERROR, 0);
	verbose(VERB_QUERY, "winservice - init complete");

	/* daemon performs work */
	while(!service_stop_shutdown) {
		daemon_fork(daemon);
		if(!service_stop_shutdown) {
			daemon_cleanup(daemon);
			config_delete(cfg); cfg=NULL;
			if(!service_init(1, &daemon, &cfg)) {
				reportev("Could not service_init");
				report_status(SERVICE_STOPPED, NO_ERROR, 0);
				return;
			}
		}
	}

	/* exit */
	verbose(VERB_ALGO, "winservice - cleanup.");
	report_status(SERVICE_STOP_PENDING, NO_ERROR, 0);
	service_deinit(daemon, cfg);
	free(service_cfgfile);
	if(service_stop_event) (void)WSACloseEvent(service_stop_event);
	verbose(VERB_QUERY, "winservice - full stop");
	report_status(SERVICE_STOPPED, NO_ERROR, 0);
}
Exemple #30
0
/**
 * Init service. Keeps calling status pending to tell service control
 * manager that this process is not hanging.
 * @param r: restart, true on restart
 * @param d: daemon returned here.
 * @param c: config file returned here.
 * @return false if failed.
 */
static int
service_init(int r, struct daemon** d, struct config_file** c)
{
	struct config_file* cfg = NULL;
	struct daemon* daemon = NULL;

	if(!service_cfgfile) {
		char* newf = lookup_reg_str("Software\\Unbound", "ConfigFile");
		if(newf) service_cfgfile = newf;
		else 	service_cfgfile = strdup(CONFIGFILE);
		if(!service_cfgfile) fatal_exit("out of memory");
	}

	/* create daemon */
	if(r) 	daemon = *d;
	else 	daemon = daemon_init();
	if(!daemon) return 0;
	if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2800);

	/* read config */
	cfg = config_create();
	if(!cfg) return 0;
	if(!config_read(cfg, service_cfgfile, daemon->chroot)) {
		if(errno != ENOENT) {
			log_err("error in config file");
			return 0;
		}
		log_warn("could not open config file, using defaults");
	}
	if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2600);

	verbose(VERB_QUERY, "winservice - apply settings");
	/* apply settings and init */
	verbosity = cfg->verbosity + service_cmdline_verbose;
	if(cfg->directory && cfg->directory[0]) {
		if(chdir(cfg->directory)) {
			log_err("could not chdir to %s: %s", 
				cfg->directory, strerror(errno));
			if(errno != ENOENT)
				return 0;
			log_warn("could not change directory - continuing");
		} else
			verbose(VERB_QUERY, "chdir to %s", cfg->directory);
	}
	log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
	if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2400);
	verbose(VERB_QUERY, "winservice - apply cfg");
	daemon_apply_cfg(daemon, cfg);

	if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2300);
	if(!(daemon->rc = daemon_remote_create(cfg))) {
		log_err("could not set up remote-control");
		daemon_delete(daemon);
		config_delete(cfg);
		return 0;
	}

	/* open ports */
	/* keep reporting that we are busy starting */
	if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2200);
	verbose(VERB_QUERY, "winservice - open ports");
	if(!daemon_open_shared_ports(daemon)) return 0;
	verbose(VERB_QUERY, "winservice - ports opened");
	if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2000);

	*d = daemon;
	*c = cfg;
	return 1;
}