From: Shawn Routhier Date: Fri, 15 Apr 2011 21:58:12 +0000 (+0000) Subject: Add the option "--no-pid" to the client, relay and server code, X-Git-Tag: v4_3_0a1~197 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a5bfeacbe463cb577f6edbf0143916847ea5feb;p=thirdparty%2Fdhcp.git Add the option "--no-pid" to the client, relay and server code, to disable writing a pid file. Add the option "-pf pidfile" to the relay to allow the user to supply the pidfile name at runtime. Add the "with-relay6-pid-file" option to configure to allow the user to supply the pidfile name for the relay in v6 mode at configure time. [ISC-Bugs #23351] [ISC-Bugs #17541] --- diff --git a/RELNOTES b/RELNOTES index 2b5f9e823..690c933c1 100644 --- a/RELNOTES +++ b/RELNOTES @@ -49,6 +49,14 @@ work on other platforms. Please report any problems and suggested fixes to - DHCPv6 server now responds properly if client asks for a prefix that is already assigned to a different client. [ISC-Bugs #23948] + +- Add the option "--no-pid" to the client, relay and server code, + to disable writing a pid file. Add the option "-pf pidfile" + to the relay to allow the user to supply the pidfile name at + runtime. Add the "with-relay6-pid-file" option to configure + to allow the user to supply the pidfile name for the relay + in v6 mode at configure time. + [ISC-Bugs #23351] [ISC-Bugs #17541] Changes since 4.2.0 diff --git a/client/dhclient.8 b/client/dhclient.8 index 388745831..7326830b3 100644 --- a/client/dhclient.8 +++ b/client/dhclient.8 @@ -1,6 +1,6 @@ -.\" $Id: dhclient.8,v 1.35 2010/07/14 20:01:14 sar Exp $ +.\" $Id: dhclient.8,v 1.36 2011/04/15 21:58:12 sar Exp $ .\" -.\" Copyright (c) 2004,2007-2010 by Internet Systems Consortium, Inc. ("ISC") +.\" Copyright (c) 2004,2007-2011 by Internet Systems Consortium, Inc. ("ISC") .\" Copyright (c) 1996-2003 by Internet Software Consortium .\" .\" Permission to use, copy, modify, and distribute this software for any @@ -90,6 +90,9 @@ dhclient - Dynamic Host Configuration Protocol Client .I pid-file ] [ +.B --no-pid +] +[ .B -cf .I config-file ] @@ -99,7 +102,7 @@ dhclient - Dynamic Host Configuration Protocol Client ] [ .B -s -.I server +.I server-addr ] [ .B -g @@ -305,7 +308,7 @@ If a different port is specified on which the client should listen and transmit, the client will also use a different destination port - one less than the specified port. .TP -.BI \-s \ server +.BI \-s \ server-addr Specify the server IP address or fully qualified domain name to use as a destination for DHCP protocol messages before .B dhclient @@ -381,6 +384,12 @@ Path to the process ID file. If unspecified, the default .B RUNDIR/dhclient.pid is used. .TP +.BI \--no-pid +Option to disable writing pid files. By default the program +will write a pid file. If the program is invoked with this +option it will not attempt to kill any existing client processes +even if invoked with \fB-r\fR or \fB-x\fR. +.TP .BI \-sf \ script-file Path to the network configuration script invoked by .B dhclient diff --git a/client/dhclient.c b/client/dhclient.c index d30bd0b38..e4b533dc6 100644 --- a/client/dhclient.c +++ b/client/dhclient.c @@ -3,7 +3,7 @@ DHCP Client. */ /* - * Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 2004-2011 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1995-2003 by Internet Software Consortium * * Permission to use, copy, modify, and distribute this software for any @@ -48,6 +48,9 @@ const char *path_dhclient_pid = NULL; static char path_dhclient_script_array[] = _PATH_DHCLIENT_SCRIPT; char *path_dhclient_script = path_dhclient_script_array; +/* False (default) => we write and use a pid file */ +isc_boolean_t no_pid_file = ISC_FALSE; + int dhcp_max_agent_option_packet_length = 0; int interfaces_requested = 0; @@ -196,6 +199,8 @@ main(int argc, char **argv) { usage(); path_dhclient_pid = argv[i]; no_dhclient_pid = 1; + } else if (!strcmp(argv[i], "--no-pid")) { + no_pid_file = ISC_TRUE; } else if (!strcmp(argv[i], "-cf")) { if (++i == argc) usage(); @@ -385,8 +390,13 @@ main(int argc, char **argv) { log_fatal("%s: %s", path, strerror(errno)); } - /* first kill off any currently running client */ - if (release_mode || exit_mode) { + /* + * See if we should kill off any currently running client + * we don't try to kill it off if the user told us not + * to write a pid file - we assume they are controlling + * the process in some other fashion. + */ + if ((release_mode || exit_mode) && (no_pid_file == ISC_FALSE)) { FILE *pidfd; pid_t oldpid; long temp; @@ -677,16 +687,17 @@ static void usage() log_info(arr); log_info(url); - log_error("Usage: dhclient %s %s", + + log_fatal("Usage: dhclient " #ifdef DHCPv6 - "[-4|-6] [-SNTP1dvrx] [-nw] [-p ] [-D LL|LLT]", + "[-4|-6] [-SNTP1dvrx] [-nw] [-p ] [-D LL|LLT]\n" #else /* DHCPv6 */ - "[-1dvrx] [-nw] [-p ]", + "[-1dvrx] [-nw] [-p ]\n" #endif /* DHCPv6 */ - "[-s server]"); - log_error(" [-cf config-file] [-lf lease-file]%s", - "[-pf pid-file] [-e VAR=val]"); - log_fatal(" [-sf script-file] [interface]"); + " [-s server-addr] [-cf config-file] " + "[-lf lease-file]\n" + " [-pf pid-file] [--no-pid] [-e VAR=val]\n" + " [-sf script-file] [interface]"); } void run_stateless(int exit_mode) @@ -3370,6 +3381,11 @@ void write_client_pid_file () FILE *pf; int pfdesc; + /* nothing to do if the user doesn't want a pid file */ + if (no_pid_file == ISC_TRUE) { + return; + } + pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (pfdesc < 0) { diff --git a/configure.ac b/configure.ac index 391da15e2..7b2288349 100644 --- a/configure.ac +++ b/configure.ac @@ -317,6 +317,12 @@ AC_ARG_WITH(relay-pid-file, (default is LOCALSTATEDIR/run/dhcrelay.pid)]), AC_DEFINE_UNQUOTED([_PATH_DHCRELAY_PID], ["$withval"], [File for dhcrelay process information.])) +AC_ARG_WITH(relay6-pid-file, + AC_HELP_STRING([--with-relay6-pid-file=PATH], + [File for dhcrelay6 process information + (default is LOCALSTATEDIR/run/dhcrelay6.pid)]), + AC_DEFINE_UNQUOTED([_PATH_DHCRELAY6_PID], ["$withval"], + [File for dhcrelay6 process information.])) # Check basic types. AC_TYPE_INT8_T diff --git a/relay/dhcrelay.8 b/relay/dhcrelay.8 index 0177071c5..eef0ece75 100644 --- a/relay/dhcrelay.8 +++ b/relay/dhcrelay.8 @@ -1,6 +1,6 @@ .\" dhcrelay.8 .\" -.\" Copyright (c) 2009-2010 by Internet Systems Consortium, Inc. ("ISC") +.\" Copyright (c) 2009-2011 by Internet Systems Consortium, Inc. ("ISC") .\" Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC") .\" Copyright (c) 1997-2003 by Internet Software Consortium .\" @@ -28,7 +28,7 @@ .\" Support and other services are available for ISC products - see .\" https://www.isc.org for more information or to learn more about ISC. .\" -.\" $Id: dhcrelay.8,v 1.18 2010/07/02 23:09:14 sar Exp $ +.\" $Id: dhcrelay.8,v 1.19 2011/04/15 21:58:12 sar Exp $ .\" .TH dhcrelay 8 .SH NAME @@ -54,6 +54,13 @@ dhcrelay - Dynamic Host Configuration Protocol Relay Agent .I length ] [ +.B -pf +.I pid-file +] +[ +.B --no-pid +] +[ .B -m .I append | @@ -89,6 +96,13 @@ dhcrelay - Dynamic Host Configuration Protocol Relay Agent .B -c .I count ] +[ +.B -pf +.I pid-file +] +[ +.B --no-pid +] .B -l .I lower0 [ @@ -150,6 +164,13 @@ purposes. Default is port 67 for DHCPv4/BOOTP, or port 547 for DHCPv6. -q Quiet mode. Prevents dhcrelay6 from printing its network configuration on startup. +.TP +-pf pid-file +Path to alternate pid file. +.TP +--no-pid +Option to disable writing pid files. By default the program +will write a pid file. .PP \fIOptions available in DHCPv4 mode only:\fR .TP diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c index b127328af..62689fbcc 100644 --- a/relay/dhcrelay.c +++ b/relay/dhcrelay.c @@ -3,7 +3,7 @@ DHCP/BOOTP Relay Agent. */ /* - * Copyright(c) 2004-2009 by Internet Systems Consortium, Inc.("ISC") + * Copyright(c) 2004-2011 by Internet Systems Consortium, Inc.("ISC") * Copyright(c) 1997-2003 by Internet Software Consortium * * Permission to use, copy, modify, and distribute this software for any @@ -49,6 +49,9 @@ char *token_line; char *tlname; const char *path_dhcrelay_pid = _PATH_DHCRELAY_PID; +isc_boolean_t no_dhcrelay_pid = ISC_FALSE; +/* False (default) => we write and use a pid file */ +isc_boolean_t no_pid_file = ISC_FALSE; int bogus_agent_drops = 0; /* Packets dropped because agent option field was specified and we're not relaying @@ -138,10 +141,12 @@ static const char url[] = #define DHCRELAY_USAGE \ "Usage: dhcrelay [-4] [-d] [-q] [-a] [-D]\n"\ " [-A ] [-c ] [-p ]\n" \ +" [-pf ] [--no-pid]\n"\ " [-m append|replace|forward|discard]\n" \ " [-i interface0 [ ... -i interfaceN]\n" \ " server0 [ ... serverN]\n\n" \ " dhcrelay -6 [-d] [-q] [-I] [-c ] [-p ]\n" \ +" [-pf ] [--no-pid]\n"\ " -l lower0 [ ... -l lowerN]\n" \ " -u upper0 [ ... -u upperN]\n" \ " lower (client link): [address%%]interface[#index]\n" \ @@ -149,6 +154,7 @@ static const char url[] = #else #define DHCRELAY_USAGE \ "Usage: dhcrelay [-d] [-q] [-a] [-D] [-A ] [-c ] [-p ]\n" \ +" [-pf ] [--no-pid]\n"\ " [-m append|replace|forward|discard]\n" \ " [-i interface0 [ ... -i interfaceN]\n" \ " server0 [ ... serverN]\n\n" @@ -350,6 +356,13 @@ main(int argc, char **argv) { sl->next = upstreams; upstreams = sl; #endif + } else if (!strcmp(argv[i], "-pf")) { + if (++i == argc) + usage(); + path_dhcrelay_pid = argv[i]; + no_dhcrelay_pid = ISC_TRUE; + } else if (!strcmp(argv[i], "--no-pid")) { + no_pid_file = ISC_TRUE; } else if (!strcmp(argv[i], "--version")) { log_info("isc-dhcrelay-%s", PACKAGE_VERSION); exit(0); @@ -394,18 +407,24 @@ main(int argc, char **argv) { } } - if (local_family == AF_INET) { - path_dhcrelay_pid = getenv("PATH_DHCRELAY_PID"); - if (path_dhcrelay_pid == NULL) - path_dhcrelay_pid = _PATH_DHCRELAY_PID; - } + /* + * If the user didn't specify a pid file directly + * find one from environment variables or defaults + */ + if (no_dhcrelay_pid == ISC_FALSE) { + if (local_family == AF_INET) { + path_dhcrelay_pid = getenv("PATH_DHCRELAY_PID"); + if (path_dhcrelay_pid == NULL) + path_dhcrelay_pid = _PATH_DHCRELAY_PID; + } #ifdef DHCPv6 - else { - path_dhcrelay_pid = getenv("PATH_DHCRELAY6_PID"); - if (path_dhcrelay_pid == NULL) - path_dhcrelay_pid = _PATH_DHCRELAY6_PID; - } + else { + path_dhcrelay_pid = getenv("PATH_DHCRELAY6_PID"); + if (path_dhcrelay_pid == NULL) + path_dhcrelay_pid = _PATH_DHCRELAY6_PID; + } #endif + } if (!quiet) { log_info("%s %s", message, PACKAGE_VERSION); @@ -519,20 +538,23 @@ main(int argc, char **argv) { else if (pid) exit(0); - pfdesc = open(path_dhcrelay_pid, - O_CREAT | O_TRUNC | O_WRONLY, 0644); + if (no_pid_file == ISC_FALSE) { + pfdesc = open(path_dhcrelay_pid, + O_CREAT | O_TRUNC | O_WRONLY, 0644); - if (pfdesc < 0) { - log_error("Can't create %s: %m", path_dhcrelay_pid); - } else { - pf = fdopen(pfdesc, "w"); - if (!pf) - log_error("Can't fdopen %s: %m", - path_dhcrelay_pid); - else { - fprintf(pf, "%ld\n",(long)getpid()); - fclose(pf); - } + if (pfdesc < 0) { + log_error("Can't create %s: %m", + path_dhcrelay_pid); + } else { + pf = fdopen(pfdesc, "w"); + if (!pf) + log_error("Can't fdopen %s: %m", + path_dhcrelay_pid); + else { + fprintf(pf, "%ld\n",(long)getpid()); + fclose(pf); + } + } } close(0); diff --git a/server/dhcpd.8 b/server/dhcpd.8 index bb1dd1ea8..0a5aace72 100644 --- a/server/dhcpd.8 +++ b/server/dhcpd.8 @@ -1,6 +1,6 @@ .\" dhcpd.8 .\" -.\" Copyright (c) 2009-2010 by Internet Systems Consortium, Inc. ("ISC") +.\" Copyright (c) 2009-2011 by Internet Systems Consortium, Inc. ("ISC") .\" Copyright (c) 2004-2007 by Internet Systems Consortium, Inc. ("ISC") .\" Copyright (c) 1996-2003 by Internet Software Consortium .\" @@ -28,7 +28,7 @@ .\" Support and other services are available for ISC products - see .\" https://www.isc.org for more information or to learn more about ISC. .\" -.\" $Id: dhcpd.8,v 1.33 2010/07/14 20:01:14 sar Exp $ +.\" $Id: dhcpd.8,v 1.34 2011/04/15 21:58:12 sar Exp $ .\" .TH dhcpd 8 .SH NAME @@ -75,6 +75,9 @@ dhcpd - Dynamic Host Configuration Protocol Server .I pid-file ] [ +.B --no-pid +] +[ .B -tf .I trace-output-file ] @@ -283,6 +286,11 @@ Path to alternate lease file. .TP .BI \-pf \ pid-file Path to alternate pid file. +.TP +.BI \--no-pid +Option to disable writing pid files. By default the program +will write a pid file. If the program is invoked with this +option it will not check for an existing server process. .PP .SH CONFIGURATION The syntax of the dhcpd.conf(5) file is discussed separately. This diff --git a/server/dhcpd.c b/server/dhcpd.c index 1cac5397f..b5577cd54 100644 --- a/server/dhcpd.c +++ b/server/dhcpd.c @@ -3,7 +3,7 @@ DHCP Server Daemon. */ /* - * Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 2004-2011 by Internet Systems Consortium, Inc. ("ISC") * Copyright (c) 1996-2003 by Internet Software Consortium * * Permission to use, copy, modify, and distribute this software for any @@ -160,6 +160,8 @@ int ddns_update_style; const char *path_dhcpd_conf = _PATH_DHCPD_CONF; const char *path_dhcpd_db = _PATH_DHCPD_DB; const char *path_dhcpd_pid = _PATH_DHCPD_PID; +/* False (default) => we write and use a pid file */ +isc_boolean_t no_pid_file = ISC_FALSE; int dhcp_max_agent_option_packet_length = DHCP_MTU_MAX; @@ -351,6 +353,8 @@ main(int argc, char **argv) { usage (); path_dhcpd_pid = argv [i]; no_dhcpd_pid = 1; + } else if (!strcmp(argv[i], "--no-pid")) { + no_pid_file = ISC_TRUE; } else if (!strcmp (argv [i], "-t")) { /* test configurations only */ #ifndef DEBUG @@ -783,33 +787,41 @@ main(int argc, char **argv) { } #endif /* PARANOIA */ - /* Read previous pid file. */ - if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) { - status = read(i, pbuf, (sizeof pbuf) - 1); - close (i); - if (status > 0) { - pbuf[status] = 0; - pid = atoi(pbuf); - - /* - * If there was a previous server process and it's - * is still running, abort - */ - if (!pid || (pid != getpid() && kill(pid, 0) == 0)) - log_fatal("There's already a " - "DHCP server running."); + /* + * Deal with pid files. If the user told us + * not to write a file we don't read one either + */ + if (no_pid_file == ISC_FALSE) { + /*Read previous pid file. */ + if ((i = open (path_dhcpd_pid, O_RDONLY)) >= 0) { + status = read(i, pbuf, (sizeof pbuf) - 1); + close (i); + if (status > 0) { + pbuf[status] = 0; + pid = atoi(pbuf); + + /* + * If there was a previous server process and + * it is still running, abort + */ + if (!pid || + (pid != getpid() && kill(pid, 0) == 0)) + log_fatal("There's already a " + "DHCP server running."); + } } - } - - /* Write new pid file. */ - if ((i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0) { - sprintf(pbuf, "%d\n", (int) getpid()); - IGNORE_RET (write(i, pbuf, strlen(pbuf))); - close(i); - } else { - log_error("Can't create PID file %s: %m.", path_dhcpd_pid); - } + /* Write new pid file. */ + i = open(path_dhcpd_pid, O_WRONLY|O_CREAT|O_TRUNC, 0644); + if (i >= 0) { + sprintf(pbuf, "%d\n", (int) getpid()); + IGNORE_RET (write(i, pbuf, strlen(pbuf))); + close(i); + } else { + log_error("Can't create PID file %s: %m.", + path_dhcpd_pid); + } + } /* If we were requested to log to stdout on the command line, keep doing so; otherwise, stop. */ @@ -902,7 +914,7 @@ void postconf_initialization (int quiet) &global_scope, oc, MDL)) { s = dmalloc (db.len + 1, MDL); if (!s) - log_fatal ("no memory for lease db filename."); + log_fatal ("no memory for pid filename."); memcpy (s, db.data, db.len); s [db.len] = 0; data_string_forget (&db, MDL); @@ -938,7 +950,7 @@ void postconf_initialization (int quiet) oc, MDL)) { s = dmalloc (db.len + 1, MDL); if (!s) - log_fatal ("no memory for lease db filename."); + log_fatal ("no memory for pid filename."); memcpy (s, db.data, db.len); s [db.len] = 0; data_string_forget (&db, MDL); @@ -1210,7 +1222,8 @@ usage(void) { " [-tf trace-output-file]\n" " [-play trace-input-file]\n" #endif /* TRACING */ - " [-pf pid-file] [-s server] [if0 [...ifN]]"); + " [-pf pid-file] [--no-pid] [-s server]\n" + " [if0 [...ifN]]"); } void lease_pinged (from, packet, length)