2007-01-03 10:42:32 +00:00
|
|
|
/*
|
|
|
|
* daemon/unbound.c - main program for unbound DNS resolver daemon.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007, NLnet Labs. All rights reserved.
|
|
|
|
*
|
2007-01-25 12:39:51 +00:00
|
|
|
* This software is open source.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* Neither the name of the NLNET LABS nor the names of its contributors may
|
|
|
|
* be used to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2007-01-03 10:42:32 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-01-03 14:55:21 +00:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
|
|
|
* Main program to start the DNS resolver daemon.
|
|
|
|
*/
|
|
|
|
|
2007-01-03 10:42:32 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "util/log.h"
|
2007-02-22 16:22:54 +00:00
|
|
|
#include "daemon/daemon.h"
|
2007-02-20 16:19:00 +00:00
|
|
|
#include "util/config_file.h"
|
2007-03-26 10:33:41 +00:00
|
|
|
#include "util/storage/slabhash.h"
|
2007-05-09 07:58:57 +00:00
|
|
|
#include "services/listen_dnsport.h"
|
2007-05-16 12:48:48 +00:00
|
|
|
#include "services/cache/rrset.h"
|
2007-05-16 14:29:46 +00:00
|
|
|
#include "services/cache/infra.h"
|
2007-03-26 10:33:41 +00:00
|
|
|
#include "util/data/msgreply.h"
|
2007-05-16 12:48:48 +00:00
|
|
|
#include "util/module.h"
|
2007-02-23 13:38:54 +00:00
|
|
|
#include <signal.h>
|
2007-02-22 16:22:54 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
2007-05-09 07:58:57 +00:00
|
|
|
#include <sys/resource.h>
|
2007-01-25 12:39:51 +00:00
|
|
|
|
2008-02-13 13:24:41 +00:00
|
|
|
|
|
|
|
#ifdef USE_MINI_EVENT
|
|
|
|
#include "util/mini_event.h"
|
|
|
|
#else
|
|
|
|
#include <event.h>
|
|
|
|
#endif
|
|
|
|
|
2007-08-09 10:14:32 +00:00
|
|
|
/** global debug value to keep track of heap memory allocation */
|
|
|
|
void* unbound_start_brk = 0;
|
|
|
|
|
2007-01-25 12:50:40 +00:00
|
|
|
/** print usage. */
|
2007-01-25 12:39:51 +00:00
|
|
|
static void usage()
|
|
|
|
{
|
2007-11-09 13:52:13 +00:00
|
|
|
printf("usage: unbound [options]\n");
|
2007-02-06 16:26:19 +00:00
|
|
|
printf(" start unbound daemon DNS resolver.\n");
|
2007-01-25 16:08:52 +00:00
|
|
|
printf("-h this help\n");
|
2007-11-09 13:52:13 +00:00
|
|
|
printf("-c file config file to read instead of %s\n", CONFIGFILE);
|
|
|
|
printf(" file format is described in unbound.conf(5).\n");
|
2007-02-23 15:23:33 +00:00
|
|
|
printf("-d do not fork into the background.\n");
|
2007-11-15 14:27:47 +00:00
|
|
|
printf("-v verbose (more times to increase verbosity)\n");
|
2007-01-25 12:39:51 +00:00
|
|
|
printf("Version %s\n", PACKAGE_VERSION);
|
2008-02-13 13:24:41 +00:00
|
|
|
printf("libevent %s, libldns %s\n",
|
|
|
|
event_get_version(), ldns_version());
|
2007-02-06 16:26:19 +00:00
|
|
|
printf("BSD licensed, see LICENSE in source package for details.\n");
|
|
|
|
printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
|
2007-01-25 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
2007-05-09 07:58:57 +00:00
|
|
|
/** check file descriptor count */
|
|
|
|
static void
|
|
|
|
checkrlimits(struct config_file* cfg)
|
|
|
|
{
|
2007-05-09 09:04:11 +00:00
|
|
|
int list = ((cfg->do_ip4?1:0) + (cfg->do_ip6?1:0)) *
|
2007-08-30 08:36:41 +00:00
|
|
|
((cfg->do_udp?1:0) + (cfg->do_tcp?1 +
|
|
|
|
(int)cfg->incoming_num_tcp:0));
|
2007-05-09 09:04:11 +00:00
|
|
|
size_t ifs = (size_t)(cfg->num_ifs==0?1:cfg->num_ifs);
|
2007-05-09 07:58:57 +00:00
|
|
|
size_t listen_num = list*ifs;
|
2007-10-19 08:32:36 +00:00
|
|
|
size_t out_ifs = (size_t)(cfg->num_out_ifs==0?1:cfg->num_out_ifs);
|
|
|
|
size_t outnum = cfg->outgoing_num_ports*out_ifs + cfg->outgoing_num_tcp;
|
2007-05-09 07:58:57 +00:00
|
|
|
size_t misc = 4; /* logfile, pidfile, stdout... */
|
|
|
|
size_t perthread = listen_num + outnum + 2/*cmdpipe*/ + 2/*libevent*/
|
|
|
|
+ misc;
|
|
|
|
#if !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS)
|
2007-05-09 09:04:11 +00:00
|
|
|
int numthread = 1; /* it forks */
|
2007-05-09 07:58:57 +00:00
|
|
|
#else
|
2007-05-09 09:04:11 +00:00
|
|
|
int numthread = cfg->num_threads;
|
2007-05-09 07:58:57 +00:00
|
|
|
#endif
|
|
|
|
size_t total = numthread * perthread + misc;
|
|
|
|
struct rlimit rlim;
|
|
|
|
if(getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
|
|
|
|
log_warn("getrlimit: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2007-06-20 13:01:30 +00:00
|
|
|
if(rlim.rlim_cur == (rlim_t)RLIM_INFINITY)
|
2007-06-20 07:47:56 +00:00
|
|
|
return;
|
2007-05-09 07:58:57 +00:00
|
|
|
if((size_t)rlim.rlim_cur < total) {
|
|
|
|
log_err("Not enough sockets available. Increase "
|
|
|
|
"ulimit(open files).");
|
|
|
|
log_err("or decrease number of threads, outgoing num ports, "
|
|
|
|
"outgoing num tcp or number of interfaces");
|
|
|
|
log_err("estimate %u fds high mark, %u available",
|
|
|
|
(unsigned)total, (unsigned)rlim.rlim_cur);
|
|
|
|
fatal_exit("Not enough file descriptors available");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 10:56:31 +00:00
|
|
|
/** set verbosity, check rlimits, cache settings */
|
2007-02-23 13:38:54 +00:00
|
|
|
static void
|
2007-10-31 10:56:31 +00:00
|
|
|
apply_settings(struct daemon* daemon, struct config_file* cfg,
|
|
|
|
int cmdline_verbose)
|
2007-02-23 13:38:54 +00:00
|
|
|
{
|
2007-03-29 13:00:36 +00:00
|
|
|
/* apply if they have changed */
|
2007-02-23 13:38:54 +00:00
|
|
|
daemon->cfg = cfg;
|
|
|
|
verbosity = cmdline_verbose + cfg->verbosity;
|
2007-10-31 07:46:30 +00:00
|
|
|
config_apply(cfg);
|
2007-05-16 14:29:46 +00:00
|
|
|
if(!daemon->env->msg_cache ||
|
|
|
|
cfg->msg_cache_size != slabhash_get_size(daemon->env->msg_cache) ||
|
|
|
|
cfg->msg_cache_slabs != daemon->env->msg_cache->size) {
|
|
|
|
slabhash_delete(daemon->env->msg_cache);
|
|
|
|
daemon->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
|
2007-03-26 10:33:41 +00:00
|
|
|
HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
|
|
|
|
msgreply_sizefunc, query_info_compare,
|
|
|
|
query_entry_delete, reply_info_delete, NULL);
|
2007-05-16 14:29:46 +00:00
|
|
|
if(!daemon->env->msg_cache) {
|
2007-03-26 10:33:41 +00:00
|
|
|
fatal_exit("malloc failure updating config settings");
|
|
|
|
}
|
|
|
|
}
|
2007-05-16 14:29:46 +00:00
|
|
|
if((daemon->env->rrset_cache = rrset_cache_adjust(
|
|
|
|
daemon->env->rrset_cache, cfg, &daemon->superalloc)) == 0)
|
|
|
|
fatal_exit("malloc failure updating config settings");
|
|
|
|
if((daemon->env->infra_cache = infra_adjust(daemon->env->infra_cache,
|
|
|
|
cfg))==0)
|
|
|
|
fatal_exit("malloc failure updating config settings");
|
2007-05-09 07:58:57 +00:00
|
|
|
checkrlimits(cfg);
|
2007-02-23 13:38:54 +00:00
|
|
|
}
|
|
|
|
|
2007-06-12 14:51:49 +00:00
|
|
|
/** Read existing pid from pidfile.
|
|
|
|
* @param file: file name of pid file.
|
|
|
|
* @return: the pid from the file or -1 if none.
|
|
|
|
*/
|
2007-02-23 13:38:54 +00:00
|
|
|
static pid_t
|
|
|
|
readpid (const char* file)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
pid_t pid;
|
|
|
|
char pidbuf[32];
|
|
|
|
char* t;
|
2007-02-23 13:59:45 +00:00
|
|
|
ssize_t l;
|
2007-02-23 13:38:54 +00:00
|
|
|
|
|
|
|
if ((fd = open(file, O_RDONLY)) == -1) {
|
2007-03-29 13:00:36 +00:00
|
|
|
if(errno != ENOENT)
|
|
|
|
log_err("Could not read pidfile %s: %s",
|
|
|
|
file, strerror(errno));
|
2007-02-23 13:38:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((l = read(fd, pidbuf, sizeof(pidbuf)))) == -1) {
|
2007-03-29 13:00:36 +00:00
|
|
|
if(errno != ENOENT)
|
|
|
|
log_err("Could not read pidfile %s: %s",
|
|
|
|
file, strerror(errno));
|
2007-02-23 13:38:54 +00:00
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* Empty pidfile means no pidfile... */
|
|
|
|
if (l == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-16 13:03:57 +00:00
|
|
|
pidbuf[sizeof(pidbuf)-1] = 0;
|
2007-09-26 11:15:42 +00:00
|
|
|
pid = (pid_t)strtol(pidbuf, &t, 10);
|
2007-02-23 13:38:54 +00:00
|
|
|
|
|
|
|
if (*t && *t != '\n') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2007-06-12 14:51:49 +00:00
|
|
|
/** write pid to file.
|
|
|
|
* @param pidfile: file name of pid file.
|
|
|
|
* @param pid: pid to write to file.
|
|
|
|
*/
|
2007-02-23 13:38:54 +00:00
|
|
|
static void
|
|
|
|
writepid (const char* pidfile, pid_t pid)
|
|
|
|
{
|
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
if ((f = fopen(pidfile, "w")) == NULL ) {
|
|
|
|
log_err("cannot open pidfile %s: %s",
|
|
|
|
pidfile, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(fprintf(f, "%lu\n", (unsigned long)pid) < 0) {
|
|
|
|
log_err("cannot write to pidfile %s: %s",
|
|
|
|
pidfile, strerror(errno));
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2007-02-23 13:59:45 +00:00
|
|
|
/**
|
|
|
|
* check old pid file.
|
|
|
|
* @param cfg: the config settings
|
|
|
|
*/
|
2007-02-23 13:38:54 +00:00
|
|
|
static void
|
|
|
|
checkoldpid(struct config_file* cfg)
|
|
|
|
{
|
|
|
|
pid_t old;
|
2007-10-31 10:56:31 +00:00
|
|
|
char* file = cfg->pidfile;
|
|
|
|
if(cfg->chrootdir && cfg->chrootdir[0] &&
|
|
|
|
strncmp(file, cfg->chrootdir, strlen(cfg->chrootdir))==0) {
|
|
|
|
file += strlen(cfg->chrootdir);
|
|
|
|
}
|
|
|
|
if((old = readpid(file)) != -1) {
|
2007-03-29 13:00:36 +00:00
|
|
|
/* see if it is still alive */
|
2007-02-23 13:38:54 +00:00
|
|
|
if(kill(old, 0) == 0 || errno == EPERM)
|
2007-02-27 15:46:28 +00:00
|
|
|
log_warn("unbound is already running as pid %u.",
|
|
|
|
(unsigned)old);
|
|
|
|
else log_warn("did not exit gracefully last time (%u)",
|
|
|
|
(unsigned)old);
|
2007-02-23 13:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 13:00:36 +00:00
|
|
|
/** detach from command line */
|
|
|
|
static void
|
|
|
|
detach(struct config_file* cfg)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
/* Take off... */
|
|
|
|
switch (fork()) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
unlink(cfg->pidfile);
|
|
|
|
fatal_exit("fork failed: %s", strerror(errno));
|
|
|
|
default:
|
|
|
|
/* exit interactive session */
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
/* detach */
|
|
|
|
if(setsid() == -1)
|
|
|
|
fatal_exit("setsid() failed: %s", strerror(errno));
|
|
|
|
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
|
|
|
|
(void)dup2(fd, STDIN_FILENO);
|
|
|
|
(void)dup2(fd, STDOUT_FILENO);
|
|
|
|
(void)dup2(fd, STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
(void)close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-22 16:22:54 +00:00
|
|
|
/** daemonize, drop user priviliges and chroot if needed */
|
|
|
|
static void
|
2007-02-23 15:23:33 +00:00
|
|
|
do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
|
2007-02-22 16:22:54 +00:00
|
|
|
{
|
2007-11-08 10:44:30 +00:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
/* initialize, but not to 0 (root) */
|
2007-12-04 17:54:14 +00:00
|
|
|
memset(&uid, 112, sizeof(uid));
|
|
|
|
memset(&gid, 112, sizeof(gid));
|
2007-02-22 16:22:54 +00:00
|
|
|
log_assert(cfg);
|
|
|
|
|
|
|
|
/* daemonize last to be able to print error to user */
|
2007-11-01 13:22:12 +00:00
|
|
|
if(cfg->directory && cfg->directory[0]) {
|
2007-10-31 10:56:31 +00:00
|
|
|
if(chdir(cfg->directory)) {
|
|
|
|
fatal_exit("Could not chdir to %s: %s",
|
|
|
|
cfg->directory, strerror(errno));
|
|
|
|
}
|
2008-02-07 09:46:49 +00:00
|
|
|
verbose(VERB_QUERY, "chdir to %s", cfg->directory);
|
2007-11-01 13:22:12 +00:00
|
|
|
}
|
2007-02-22 16:22:54 +00:00
|
|
|
if(cfg->username && cfg->username[0]) {
|
|
|
|
struct passwd *pwd;
|
|
|
|
if((pwd = getpwnam(cfg->username)) == NULL)
|
|
|
|
fatal_exit("user '%s' does not exist.", cfg->username);
|
2007-11-08 10:44:30 +00:00
|
|
|
uid = pwd->pw_uid;
|
|
|
|
gid = pwd->pw_gid;
|
2007-02-22 16:22:54 +00:00
|
|
|
endpwent();
|
|
|
|
}
|
2007-11-01 13:22:12 +00:00
|
|
|
if(cfg->chrootdir && cfg->chrootdir[0]) {
|
2007-11-01 12:48:37 +00:00
|
|
|
if(chroot(cfg->chrootdir))
|
|
|
|
fatal_exit("unable to chroot to %s: %s",
|
|
|
|
cfg->chrootdir, strerror(errno));
|
2008-02-07 09:46:49 +00:00
|
|
|
verbose(VERB_QUERY, "chroot to %s", cfg->chrootdir);
|
2007-11-01 13:22:12 +00:00
|
|
|
}
|
2007-11-08 10:44:30 +00:00
|
|
|
if(cfg->username && cfg->username[0]) {
|
|
|
|
if(setgid(gid) != 0)
|
2007-11-15 14:27:47 +00:00
|
|
|
fatal_exit("unable to set group id of %s: %s",
|
|
|
|
cfg->username, strerror(errno));
|
2007-11-08 10:44:30 +00:00
|
|
|
if(setuid(uid) != 0)
|
2007-11-15 14:27:47 +00:00
|
|
|
fatal_exit("unable to set user id of %s: %s",
|
|
|
|
cfg->username, strerror(errno));
|
2008-02-07 09:46:49 +00:00
|
|
|
verbose(VERB_QUERY, "drop user privileges, run as %s",
|
2007-11-08 10:44:30 +00:00
|
|
|
cfg->username);
|
|
|
|
}
|
2007-02-23 13:38:54 +00:00
|
|
|
/* check old pid file before forking */
|
|
|
|
if(cfg->pidfile && cfg->pidfile[0]) {
|
|
|
|
checkoldpid(cfg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init logfile just before fork */
|
2007-10-31 10:56:31 +00:00
|
|
|
log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
|
2007-02-23 15:23:33 +00:00
|
|
|
if(!debug_mode && cfg->do_daemonize) {
|
2007-03-29 13:00:36 +00:00
|
|
|
detach(cfg);
|
2007-02-22 16:22:54 +00:00
|
|
|
}
|
2007-02-23 13:38:54 +00:00
|
|
|
if(cfg->pidfile && cfg->pidfile[0]) {
|
2007-10-31 10:56:31 +00:00
|
|
|
char* pf = cfg->pidfile;
|
|
|
|
if(cfg->chrootdir && cfg->chrootdir[0] &&
|
|
|
|
strncmp(pf, cfg->chrootdir, strlen(cfg->chrootdir))==0)
|
|
|
|
pf += strlen(cfg->chrootdir);
|
|
|
|
writepid(pf, getpid());
|
|
|
|
if(!(daemon->pidfile = strdup(pf)))
|
2007-03-29 13:00:36 +00:00
|
|
|
log_err("pidf: malloc failed");
|
2007-02-23 13:38:54 +00:00
|
|
|
}
|
2007-02-22 16:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-02-22 13:36:29 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2007-02-23 15:23:33 +00:00
|
|
|
* @param debug_mode: if set, do not daemonize.
|
2007-02-22 13:36:29 +00:00
|
|
|
*/
|
2007-03-29 13:00:36 +00:00
|
|
|
static void
|
2007-10-19 08:03:08 +00:00
|
|
|
run_daemon(char* cfgfile, int cmdline_verbose, int debug_mode)
|
2007-02-22 13:36:29 +00:00
|
|
|
{
|
2007-02-22 16:22:54 +00:00
|
|
|
struct config_file* cfg = NULL;
|
|
|
|
struct daemon* daemon = NULL;
|
|
|
|
int done_chroot = 0;
|
2007-02-22 13:36:29 +00:00
|
|
|
|
2007-02-22 16:22:54 +00:00
|
|
|
if(!(daemon = daemon_init()))
|
|
|
|
fatal_exit("alloc failure");
|
|
|
|
while(!daemon->need_to_exit) {
|
|
|
|
if(done_chroot)
|
2007-11-28 09:34:06 +00:00
|
|
|
verbose(VERB_OPS, "Restart of %s.", PACKAGE_STRING);
|
|
|
|
else verbose(VERB_OPS, "Start of %s.", PACKAGE_STRING);
|
2007-02-22 13:36:29 +00:00
|
|
|
|
2007-02-22 16:22:54 +00:00
|
|
|
/* config stuff */
|
|
|
|
if(!(cfg = config_create()))
|
|
|
|
fatal_exit("Could not alloc config defaults");
|
|
|
|
if(!config_read(cfg, cfgfile))
|
|
|
|
fatal_exit("Could not read config file: %s", cfgfile);
|
2007-10-31 10:56:31 +00:00
|
|
|
apply_settings(daemon, cfg, cmdline_verbose);
|
2007-02-22 13:36:29 +00:00
|
|
|
|
2007-02-22 16:22:54 +00:00
|
|
|
/* prepare */
|
2007-02-23 13:38:54 +00:00
|
|
|
if(!daemon_open_shared_ports(daemon))
|
2007-02-22 16:22:54 +00:00
|
|
|
fatal_exit("could not open ports");
|
|
|
|
if(!done_chroot) {
|
2007-02-23 15:23:33 +00:00
|
|
|
do_chroot(daemon, cfg, debug_mode);
|
2007-02-22 16:22:54 +00:00
|
|
|
done_chroot = 1;
|
2007-10-31 10:56:31 +00:00
|
|
|
} else log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir);
|
2007-02-22 16:22:54 +00:00
|
|
|
/* work */
|
|
|
|
daemon_fork(daemon);
|
2007-02-22 13:36:29 +00:00
|
|
|
|
2007-02-22 16:22:54 +00:00
|
|
|
/* clean up for restart */
|
|
|
|
verbose(VERB_ALGO, "cleanup.");
|
|
|
|
daemon_cleanup(daemon);
|
|
|
|
config_delete(cfg);
|
|
|
|
}
|
2007-02-22 13:36:29 +00:00
|
|
|
verbose(VERB_ALGO, "Exit cleanup.");
|
2007-02-23 13:38:54 +00:00
|
|
|
if(daemon->pidfile)
|
|
|
|
unlink(daemon->pidfile);
|
2007-02-22 16:22:54 +00:00
|
|
|
daemon_delete(daemon);
|
2007-02-22 13:36:29 +00:00
|
|
|
}
|
|
|
|
|
2007-01-25 12:50:40 +00:00
|
|
|
/** getopt global, in case header files fail to declare it. */
|
2007-01-25 12:39:51 +00:00
|
|
|
extern int optind;
|
2007-01-25 12:50:40 +00:00
|
|
|
/** getopt global, in case header files fail to declare it. */
|
2007-01-25 12:39:51 +00:00
|
|
|
extern char* optarg;
|
2007-01-03 10:42:32 +00:00
|
|
|
|
2007-01-03 14:55:21 +00:00
|
|
|
/**
|
|
|
|
* main program. Set options given commandline arguments.
|
|
|
|
* @param argc: number of commandline arguments.
|
|
|
|
* @param argv: array of commandline arguments.
|
2007-02-06 16:26:19 +00:00
|
|
|
* @return: exit status of the program.
|
2007-01-03 14:55:21 +00:00
|
|
|
*/
|
2007-01-03 10:42:32 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2007-01-25 12:39:51 +00:00
|
|
|
int c;
|
2007-11-09 13:52:13 +00:00
|
|
|
char* cfgfile = CONFIGFILE;
|
2007-02-22 13:36:29 +00:00
|
|
|
int cmdline_verbose = 0;
|
2007-02-23 15:23:33 +00:00
|
|
|
int debug_mode = 0;
|
2007-01-25 12:39:51 +00:00
|
|
|
|
2007-08-09 10:14:32 +00:00
|
|
|
/* take debug snapshot of heap */
|
|
|
|
unbound_start_brk = sbrk(0);
|
|
|
|
|
2007-10-31 10:56:31 +00:00
|
|
|
log_init(NULL, 0, NULL);
|
2007-01-25 12:39:51 +00:00
|
|
|
/* parse the options */
|
2007-07-27 06:41:42 +00:00
|
|
|
while( (c=getopt(argc, argv, "c:dhv")) != -1) {
|
2007-01-25 12:39:51 +00:00
|
|
|
switch(c) {
|
2007-02-20 16:19:00 +00:00
|
|
|
case 'c':
|
|
|
|
cfgfile = optarg;
|
|
|
|
break;
|
2007-01-25 12:39:51 +00:00
|
|
|
case 'v':
|
2007-02-22 13:36:29 +00:00
|
|
|
cmdline_verbose ++;
|
|
|
|
verbosity++;
|
2007-01-25 12:39:51 +00:00
|
|
|
break;
|
2007-02-23 15:23:33 +00:00
|
|
|
case 'd':
|
|
|
|
debug_mode = 1;
|
|
|
|
break;
|
2007-01-25 12:39:51 +00:00
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if(argc != 0) {
|
|
|
|
usage();
|
2007-01-03 10:42:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-01-25 12:39:51 +00:00
|
|
|
|
2007-02-23 15:23:33 +00:00
|
|
|
run_daemon(cfgfile, cmdline_verbose, debug_mode);
|
2007-10-31 10:56:31 +00:00
|
|
|
log_init(NULL, 0, NULL); /* close logfile */
|
2007-01-03 10:42:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|