coredump feature.

git-svn-id: file:///svn/unbound/trunk@402 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2007-06-20 07:47:56 +00:00
parent 6f9ec93b4a
commit 1c80108a5b
3 changed files with 73 additions and 36 deletions

View file

@ -55,6 +55,9 @@
#include <pwd.h>
#include <sys/resource.h>
/** if we want to enable coredumps after daemonize */
static int enable_coredumps = 0;
/** print usage. */
static void usage()
{
@ -63,6 +66,7 @@ static void usage()
printf("-h this help\n");
printf("-c file config file to read, unbound.conf(5).\n");
printf("-d do not fork into the background.\n");
printf("-C try to enable coredumps after fork into background.\n");
printf("-v verbose (multiple times increase verbosity)\n");
printf("Version %s\n", PACKAGE_VERSION);
printf("BSD licensed, see LICENSE in source package for details.\n");
@ -92,6 +96,8 @@ checkrlimits(struct config_file* cfg)
log_warn("getrlimit: %s", strerror(errno));
return;
}
if(rlim.rlim_cur == RLIM_INFINITY)
return;
if((size_t)rlim.rlim_cur < total) {
log_err("Not enough sockets available. Increase "
"ulimit(open files).");
@ -103,6 +109,49 @@ checkrlimits(struct config_file* cfg)
}
}
/** printout rlimit prettily */
static void
print_rlim_pretty(const char* str, struct rlimit* rlim)
{
if(rlim->rlim_cur == RLIM_INFINITY && rlim->rlim_max == RLIM_INFINITY)
log_info("%s unlimited, max unlimited", str);
else if(rlim->rlim_max == RLIM_INFINITY)
log_info("%s %d, max unlimited", str, (int)rlim->rlim_cur);
else if(rlim->rlim_cur == RLIM_INFINITY)
log_info("%s unlimited, max %d", str, (int)rlim->rlim_max);
else log_info("%s %d, max %d", str, (int)rlim->rlim_cur,
(int)rlim->rlim_max);
}
/** try to enable coredumps */
static void
do_coredump_enable()
{
struct rlimit rlim;
if(getrlimit(RLIMIT_CORE, &rlim) < 0) {
log_warn("getrlimit(core): %s", strerror(errno));
return;
}
print_rlim_pretty("rlimit(core) is", &rlim);
if(rlim.rlim_cur == RLIM_INFINITY && rlim.rlim_max == RLIM_INFINITY) {
return;
}
if(rlim.rlim_cur > 10000) {
return;
}
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;
if(setrlimit(RLIMIT_CORE, &rlim) < 0) {
log_warn("setrlimit(core): %s", strerror(errno));
return;
}
if(getrlimit(RLIMIT_CORE, &rlim) < 0) {
log_warn("getrlimit(core, upd): %s", strerror(errno));
return;
}
print_rlim_pretty("updated rlimit(core) is", &rlim);
}
/** to changedir, logfile */
static void
apply_dir(struct daemon* daemon, struct config_file* cfg, int cmdline_verbose)
@ -281,6 +330,8 @@ do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
log_init(cfg->logfile);
if(!debug_mode && cfg->do_daemonize) {
detach(cfg);
if(enable_coredumps)
do_coredump_enable();
}
if(cfg->pidfile && cfg->pidfile[0]) {
writepid(cfg->pidfile, getpid());
@ -359,8 +410,11 @@ main(int argc, char* argv[])
log_init(NULL);
/* parse the options */
while( (c=getopt(argc, argv, "c:dhv")) != -1) {
while( (c=getopt(argc, argv, "Cc:dhv")) != -1) {
switch(c) {
case 'C':
enable_coredumps = 1;
break;
case 'c':
cfgfile = optarg;
break;

View file

@ -1,3 +1,7 @@
20 June 2007: Wouter
- new -C option to enable coredumps after forking away.
- doc update.
19 June 2007: Wouter
- nicer layout in stats.c, review 0.3 change.
- spelling improvement, review 0.3 change.

View file

@ -1,38 +1,11 @@
.ig
unbound.8 -- unbound manual
Copyright (c) 2007, NLnet Labs. All rights reserved.
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.
..
.\"
.\" unbound.8 -- unbound manual
.\"
.\" Copyright (c) 2007, NLnet Labs. All rights reserved.
.\"
.\" See LICENSE for the license.
.\"
.\"
.Dd @date@
.Dt unbound 8
.Sh NAME
@ -43,6 +16,7 @@ unbound
.Op Fl h
.Op Fl d
.Op Fl v
.Op Fl C
.Op Fl c Ar cfgfile
.Sh DESCRIPTION
@ -69,6 +43,11 @@ console.
Increase verbosity. If given multiple times, more information is logged.
This is in addition to the verbosity (if any) from the config file.
.It Fl C
Try to enable coredumps after forking into the background. If you do not
have permission to alter the ulimits, then this will fail. Performs
the equivalent of 'ulimit -c unlimited'. For debug purposes.
.El
.Sh SEE ALSO
.Xr unbound.conf 5 .