mirror of
https://gitlab.isc.org/isc-projects/dhcp
synced 2025-09-02 15:25:48 +00:00
Add options to not go into daemon mode; log errors to stderr as well as syslogd; rewrite pid file as soon as possible.
This commit is contained in:
115
dhcpd.c
115
dhcpd.c
@@ -40,6 +40,8 @@
|
|||||||
* Enterprises, see ``http://www.vix.com''.
|
* Enterprises, see ``http://www.vix.com''.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static char objcopyright[] =
|
||||||
|
"$Id: dhcpd.c,v 1.26 1996/08/27 09:44:54 mellon Exp $ Copyright 1995, 1996 The Internet Software Consortium.";
|
||||||
static char copyright[] =
|
static char copyright[] =
|
||||||
"Copyright 1995, 1996 The Internet Software Consortium.";
|
"Copyright 1995, 1996 The Internet Software Consortium.";
|
||||||
static char arr [] = "All rights reserved.";
|
static char arr [] = "All rights reserved.";
|
||||||
@@ -50,9 +52,7 @@ static char message [] = "Internet Software Consortium DHCPD $Name: $";
|
|||||||
static void usage PROTO ((void));
|
static void usage PROTO ((void));
|
||||||
|
|
||||||
TIME cur_time;
|
TIME cur_time;
|
||||||
TIME default_lease_time = 43200; /* 12 hours... */
|
struct group root_group;
|
||||||
TIME max_lease_time = 86400; /* 24 hours... */
|
|
||||||
struct tree_cache *global_options [256];
|
|
||||||
|
|
||||||
struct iaddr server_identifier;
|
struct iaddr server_identifier;
|
||||||
int server_identifier_matched;
|
int server_identifier_matched;
|
||||||
@@ -68,37 +68,29 @@ int main (argc, argv, envp)
|
|||||||
int argc;
|
int argc;
|
||||||
char **argv, **envp;
|
char **argv, **envp;
|
||||||
{
|
{
|
||||||
int i;
|
int i, status;
|
||||||
struct sockaddr_in name;
|
struct sockaddr_in name;
|
||||||
struct servent *ent;
|
struct servent *ent;
|
||||||
|
int pidfilewritten = 0;
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
int pid;
|
int pid;
|
||||||
|
char pbuf [20];
|
||||||
|
int daemon = 1;
|
||||||
|
int log_perror = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Initially, log errors to stderr as well as to syslogd. */
|
||||||
#ifdef SYSLOG_4_2
|
#ifdef SYSLOG_4_2
|
||||||
openlog ("dhcpd", LOG_NDELAY);
|
openlog ("dhcpd", LOG_NDELAY | LOG_PERROR);
|
||||||
log_priority = LOG_DAEMON;
|
log_priority = DHCPD_LOG_FACILITY;
|
||||||
#else
|
#else
|
||||||
openlog ("dhcpd", LOG_NDELAY, LOG_DAEMON);
|
openlog ("dhcpd", LOG_NDELAY | LOG_PERROR, DHCPD_LOG_FACILITY);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef NO_PUTENV
|
|
||||||
/* ensure mktime() calls are processed in UTC */
|
|
||||||
putenv("TZ=GMT0");
|
|
||||||
#endif /* !NO_PUTENV */
|
|
||||||
|
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
#ifndef SYSLOG_4_2
|
#ifndef SYSLOG_4_2
|
||||||
setlogmask (LOG_UPTO (LOG_INFO));
|
setlogmask (LOG_UPTO (LOG_INFO));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Become a daemon... */
|
|
||||||
if ((pid = fork ()) < 0)
|
|
||||||
error ("Can't fork daemon: %m");
|
|
||||||
else if (pid)
|
|
||||||
exit (0);
|
|
||||||
/* Become session leader and get pid... */
|
|
||||||
pid = setsid ();
|
|
||||||
#endif
|
#endif
|
||||||
note (message);
|
note (message);
|
||||||
note (copyright);
|
note (copyright);
|
||||||
@@ -111,6 +103,15 @@ int main (argc, argv, envp)
|
|||||||
server_port = htons (atoi (argv [i]));
|
server_port = htons (atoi (argv [i]));
|
||||||
debug ("binding to user-specified port %d",
|
debug ("binding to user-specified port %d",
|
||||||
ntohs (server_port));
|
ntohs (server_port));
|
||||||
|
} else if (!strcmp (argv [i], "-f")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
} else if (!strcmp (argv [i], "-d")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
log_perror = 1;
|
||||||
|
#endif
|
||||||
} else if (argv [i][0] == '-') {
|
} else if (argv [i][0] == '-') {
|
||||||
usage ();
|
usage ();
|
||||||
} else {
|
} else {
|
||||||
@@ -128,6 +129,56 @@ int main (argc, argv, envp)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If the user didn't ask for debug mode, close the log and
|
||||||
|
reopen it without the LOG_STDERR flag. */
|
||||||
|
#ifndef DEBUG
|
||||||
|
if (!log_perror) {
|
||||||
|
closelog ();
|
||||||
|
|
||||||
|
#ifdef SYSLOG_4_2
|
||||||
|
openlog ("dhcpd", LOG_NDELAY);
|
||||||
|
log_priority = DHCPD_LOG_FACILITY;
|
||||||
|
#else
|
||||||
|
openlog ("dhcpd", LOG_NDELAY, DHCPD_LOG_FACILITY);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SYSLOG_4_2
|
||||||
|
setlogmask (LOG_UPTO (LOG_INFO));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (daemon) {
|
||||||
|
/* Become a daemon... */
|
||||||
|
if ((pid = fork ()) < 0)
|
||||||
|
error ("Can't fork daemon: %m");
|
||||||
|
else if (pid)
|
||||||
|
exit (0);
|
||||||
|
/* Become session leader and get pid... */
|
||||||
|
pid = setsid ();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read previous pid file. */
|
||||||
|
if ((i = open (_PATH_DHCPD_PID, O_RDONLY)) >= 0) {
|
||||||
|
status = read (i, pbuf, (sizeof pbuf) - 1);
|
||||||
|
close (i);
|
||||||
|
pbuf [status] = 0;
|
||||||
|
pid = atoi (pbuf);
|
||||||
|
|
||||||
|
/* If the previous server process is not still running,
|
||||||
|
write a new pid file immediately. */
|
||||||
|
if (pid && kill (pid, 0) < 0) {
|
||||||
|
unlink (_PATH_DHCPD_PID);
|
||||||
|
if ((i = open (_PATH_DHCPD_PID,
|
||||||
|
O_WRONLY | O_CREAT, 0640)) >= 0) {
|
||||||
|
sprintf (pbuf, "%d\n", (int)getpid ());
|
||||||
|
write (i, pbuf, strlen (pbuf));
|
||||||
|
close (i);
|
||||||
|
pidfilewritten = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* !DEBUG */
|
||||||
|
|
||||||
/* Default to the DHCP/BOOTP port. */
|
/* Default to the DHCP/BOOTP port. */
|
||||||
if (!server_port)
|
if (!server_port)
|
||||||
{
|
{
|
||||||
@@ -151,14 +202,22 @@ int main (argc, argv, envp)
|
|||||||
/* Discover all the network interfaces and initialize them. */
|
/* Discover all the network interfaces and initialize them. */
|
||||||
discover_interfaces ();
|
discover_interfaces ();
|
||||||
|
|
||||||
/* Write a pid file. */
|
#ifndef DEBUG
|
||||||
unlink (_PATH_DHCPD_PID);
|
/* If we didn't write the pid file earlier because we found a
|
||||||
if ((i = open (_PATH_DHCPD_PID, O_WRONLY | O_CREAT, 0640)) >= 0) {
|
process running the logged pid, but we made it to here,
|
||||||
char obuf [20];
|
meaning nothing is listening on the bootp port, then write
|
||||||
sprintf (obuf, "%d\n", (int)getpid ());
|
the pid file out - what's in it now is bogus anyway. */
|
||||||
write (i, obuf, strlen (obuf));
|
if (!pidfilewritten) {
|
||||||
close (i);
|
unlink (_PATH_DHCPD_PID);
|
||||||
|
if ((i = open (_PATH_DHCPD_PID,
|
||||||
|
O_WRONLY | O_CREAT, 0640)) >= 0) {
|
||||||
|
sprintf (pbuf, "%d\n", (int)getpid ());
|
||||||
|
write (i, pbuf, strlen (pbuf));
|
||||||
|
close (i);
|
||||||
|
pidfilewritten = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
#endif /* !DEBUG */
|
||||||
|
|
||||||
/* Receive packets and dispatch them... */
|
/* Receive packets and dispatch them... */
|
||||||
dispatch ();
|
dispatch ();
|
||||||
@@ -171,7 +230,7 @@ int main (argc, argv, envp)
|
|||||||
|
|
||||||
static void usage ()
|
static void usage ()
|
||||||
{
|
{
|
||||||
error ("Usage: dhcpd [-p <port>] [-a <ip-addr>]");
|
error ("Usage: dhcpd [-p <port>] [-f] [if0 [...ifN]]");
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup ()
|
void cleanup ()
|
||||||
|
115
server/dhcpd.c
115
server/dhcpd.c
@@ -40,6 +40,8 @@
|
|||||||
* Enterprises, see ``http://www.vix.com''.
|
* Enterprises, see ``http://www.vix.com''.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static char objcopyright[] =
|
||||||
|
"$Id: dhcpd.c,v 1.26 1996/08/27 09:44:54 mellon Exp $ Copyright 1995, 1996 The Internet Software Consortium.";
|
||||||
static char copyright[] =
|
static char copyright[] =
|
||||||
"Copyright 1995, 1996 The Internet Software Consortium.";
|
"Copyright 1995, 1996 The Internet Software Consortium.";
|
||||||
static char arr [] = "All rights reserved.";
|
static char arr [] = "All rights reserved.";
|
||||||
@@ -50,9 +52,7 @@ static char message [] = "Internet Software Consortium DHCPD $Name: $";
|
|||||||
static void usage PROTO ((void));
|
static void usage PROTO ((void));
|
||||||
|
|
||||||
TIME cur_time;
|
TIME cur_time;
|
||||||
TIME default_lease_time = 43200; /* 12 hours... */
|
struct group root_group;
|
||||||
TIME max_lease_time = 86400; /* 24 hours... */
|
|
||||||
struct tree_cache *global_options [256];
|
|
||||||
|
|
||||||
struct iaddr server_identifier;
|
struct iaddr server_identifier;
|
||||||
int server_identifier_matched;
|
int server_identifier_matched;
|
||||||
@@ -68,37 +68,29 @@ int main (argc, argv, envp)
|
|||||||
int argc;
|
int argc;
|
||||||
char **argv, **envp;
|
char **argv, **envp;
|
||||||
{
|
{
|
||||||
int i;
|
int i, status;
|
||||||
struct sockaddr_in name;
|
struct sockaddr_in name;
|
||||||
struct servent *ent;
|
struct servent *ent;
|
||||||
|
int pidfilewritten = 0;
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
int pid;
|
int pid;
|
||||||
|
char pbuf [20];
|
||||||
|
int daemon = 1;
|
||||||
|
int log_perror = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Initially, log errors to stderr as well as to syslogd. */
|
||||||
#ifdef SYSLOG_4_2
|
#ifdef SYSLOG_4_2
|
||||||
openlog ("dhcpd", LOG_NDELAY);
|
openlog ("dhcpd", LOG_NDELAY | LOG_PERROR);
|
||||||
log_priority = LOG_DAEMON;
|
log_priority = DHCPD_LOG_FACILITY;
|
||||||
#else
|
#else
|
||||||
openlog ("dhcpd", LOG_NDELAY, LOG_DAEMON);
|
openlog ("dhcpd", LOG_NDELAY | LOG_PERROR, DHCPD_LOG_FACILITY);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef NO_PUTENV
|
|
||||||
/* ensure mktime() calls are processed in UTC */
|
|
||||||
putenv("TZ=GMT0");
|
|
||||||
#endif /* !NO_PUTENV */
|
|
||||||
|
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
#ifndef SYSLOG_4_2
|
#ifndef SYSLOG_4_2
|
||||||
setlogmask (LOG_UPTO (LOG_INFO));
|
setlogmask (LOG_UPTO (LOG_INFO));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Become a daemon... */
|
|
||||||
if ((pid = fork ()) < 0)
|
|
||||||
error ("Can't fork daemon: %m");
|
|
||||||
else if (pid)
|
|
||||||
exit (0);
|
|
||||||
/* Become session leader and get pid... */
|
|
||||||
pid = setsid ();
|
|
||||||
#endif
|
#endif
|
||||||
note (message);
|
note (message);
|
||||||
note (copyright);
|
note (copyright);
|
||||||
@@ -111,6 +103,15 @@ int main (argc, argv, envp)
|
|||||||
server_port = htons (atoi (argv [i]));
|
server_port = htons (atoi (argv [i]));
|
||||||
debug ("binding to user-specified port %d",
|
debug ("binding to user-specified port %d",
|
||||||
ntohs (server_port));
|
ntohs (server_port));
|
||||||
|
} else if (!strcmp (argv [i], "-f")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
} else if (!strcmp (argv [i], "-d")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
log_perror = 1;
|
||||||
|
#endif
|
||||||
} else if (argv [i][0] == '-') {
|
} else if (argv [i][0] == '-') {
|
||||||
usage ();
|
usage ();
|
||||||
} else {
|
} else {
|
||||||
@@ -128,6 +129,56 @@ int main (argc, argv, envp)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If the user didn't ask for debug mode, close the log and
|
||||||
|
reopen it without the LOG_STDERR flag. */
|
||||||
|
#ifndef DEBUG
|
||||||
|
if (!log_perror) {
|
||||||
|
closelog ();
|
||||||
|
|
||||||
|
#ifdef SYSLOG_4_2
|
||||||
|
openlog ("dhcpd", LOG_NDELAY);
|
||||||
|
log_priority = DHCPD_LOG_FACILITY;
|
||||||
|
#else
|
||||||
|
openlog ("dhcpd", LOG_NDELAY, DHCPD_LOG_FACILITY);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef SYSLOG_4_2
|
||||||
|
setlogmask (LOG_UPTO (LOG_INFO));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (daemon) {
|
||||||
|
/* Become a daemon... */
|
||||||
|
if ((pid = fork ()) < 0)
|
||||||
|
error ("Can't fork daemon: %m");
|
||||||
|
else if (pid)
|
||||||
|
exit (0);
|
||||||
|
/* Become session leader and get pid... */
|
||||||
|
pid = setsid ();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read previous pid file. */
|
||||||
|
if ((i = open (_PATH_DHCPD_PID, O_RDONLY)) >= 0) {
|
||||||
|
status = read (i, pbuf, (sizeof pbuf) - 1);
|
||||||
|
close (i);
|
||||||
|
pbuf [status] = 0;
|
||||||
|
pid = atoi (pbuf);
|
||||||
|
|
||||||
|
/* If the previous server process is not still running,
|
||||||
|
write a new pid file immediately. */
|
||||||
|
if (pid && kill (pid, 0) < 0) {
|
||||||
|
unlink (_PATH_DHCPD_PID);
|
||||||
|
if ((i = open (_PATH_DHCPD_PID,
|
||||||
|
O_WRONLY | O_CREAT, 0640)) >= 0) {
|
||||||
|
sprintf (pbuf, "%d\n", (int)getpid ());
|
||||||
|
write (i, pbuf, strlen (pbuf));
|
||||||
|
close (i);
|
||||||
|
pidfilewritten = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* !DEBUG */
|
||||||
|
|
||||||
/* Default to the DHCP/BOOTP port. */
|
/* Default to the DHCP/BOOTP port. */
|
||||||
if (!server_port)
|
if (!server_port)
|
||||||
{
|
{
|
||||||
@@ -151,14 +202,22 @@ int main (argc, argv, envp)
|
|||||||
/* Discover all the network interfaces and initialize them. */
|
/* Discover all the network interfaces and initialize them. */
|
||||||
discover_interfaces ();
|
discover_interfaces ();
|
||||||
|
|
||||||
/* Write a pid file. */
|
#ifndef DEBUG
|
||||||
unlink (_PATH_DHCPD_PID);
|
/* If we didn't write the pid file earlier because we found a
|
||||||
if ((i = open (_PATH_DHCPD_PID, O_WRONLY | O_CREAT, 0640)) >= 0) {
|
process running the logged pid, but we made it to here,
|
||||||
char obuf [20];
|
meaning nothing is listening on the bootp port, then write
|
||||||
sprintf (obuf, "%d\n", (int)getpid ());
|
the pid file out - what's in it now is bogus anyway. */
|
||||||
write (i, obuf, strlen (obuf));
|
if (!pidfilewritten) {
|
||||||
close (i);
|
unlink (_PATH_DHCPD_PID);
|
||||||
|
if ((i = open (_PATH_DHCPD_PID,
|
||||||
|
O_WRONLY | O_CREAT, 0640)) >= 0) {
|
||||||
|
sprintf (pbuf, "%d\n", (int)getpid ());
|
||||||
|
write (i, pbuf, strlen (pbuf));
|
||||||
|
close (i);
|
||||||
|
pidfilewritten = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
#endif /* !DEBUG */
|
||||||
|
|
||||||
/* Receive packets and dispatch them... */
|
/* Receive packets and dispatch them... */
|
||||||
dispatch ();
|
dispatch ();
|
||||||
@@ -171,7 +230,7 @@ int main (argc, argv, envp)
|
|||||||
|
|
||||||
static void usage ()
|
static void usage ()
|
||||||
{
|
{
|
||||||
error ("Usage: dhcpd [-p <port>] [-a <ip-addr>]");
|
error ("Usage: dhcpd [-p <port>] [-f] [if0 [...ifN]]");
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanup ()
|
void cleanup ()
|
||||||
|
Reference in New Issue
Block a user