mirror of
https://gitlab.isc.org/isc-projects/dhcp
synced 2025-09-01 23:05:29 +00:00
Daemonize sooner (code from bind9/named) (rebased)
This commit is contained in:
@@ -1500,7 +1500,7 @@ start_init6(struct client_state *client)
|
|||||||
add_timeout(&tv, do_init6, client, NULL, NULL);
|
add_timeout(&tv, do_init6, client, NULL, NULL);
|
||||||
|
|
||||||
if (nowait)
|
if (nowait)
|
||||||
go_daemon();
|
detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1544,7 +1544,7 @@ start_info_request6(struct client_state *client)
|
|||||||
add_timeout(&tv, do_info_request6, client, NULL, NULL);
|
add_timeout(&tv, do_info_request6, client, NULL, NULL);
|
||||||
|
|
||||||
if (nowait)
|
if (nowait)
|
||||||
go_daemon();
|
detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1871,7 +1871,7 @@ do_init6(void *input)
|
|||||||
}
|
}
|
||||||
/* Stop if and only if this is the last client. */
|
/* Stop if and only if this is the last client. */
|
||||||
if (stopping_finished())
|
if (stopping_finished())
|
||||||
exit(2);
|
finish(2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2176,7 +2176,7 @@ do_info_request6(void *input)
|
|||||||
case CHK_TIM_ALLOC_FAILURE:
|
case CHK_TIM_ALLOC_FAILURE:
|
||||||
return;
|
return;
|
||||||
case CHK_TIM_MRD_EXCEEDED:
|
case CHK_TIM_MRD_EXCEEDED:
|
||||||
exit(2);
|
finish(2);
|
||||||
case CHK_TIM_SUCCESS:
|
case CHK_TIM_SUCCESS:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -2418,7 +2418,7 @@ do_release6(void *input)
|
|||||||
dhc6_lease_destroy(&client->active_lease, MDL);
|
dhc6_lease_destroy(&client->active_lease, MDL);
|
||||||
client->active_lease = NULL;
|
client->active_lease = NULL;
|
||||||
if (stopping_finished())
|
if (stopping_finished())
|
||||||
exit(0);
|
finish(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* status_log() just puts a status code into displayable form and logs it
|
/* status_log() just puts a status code into displayable form and logs it
|
||||||
@@ -4224,7 +4224,7 @@ reply_handler(struct packet *packet, struct client_state *client)
|
|||||||
client->active_lease = NULL;
|
client->active_lease = NULL;
|
||||||
/* We should never wait for nothing!? */
|
/* We should never wait for nothing!? */
|
||||||
if (stopping_finished())
|
if (stopping_finished())
|
||||||
exit(0);
|
finish(0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4840,7 +4840,7 @@ start_bound(struct client_state *client)
|
|||||||
dhcp4o6_start();
|
dhcp4o6_start();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
go_daemon();
|
detach();
|
||||||
|
|
||||||
if (client->old_lease != NULL) {
|
if (client->old_lease != NULL) {
|
||||||
dhc6_lease_destroy(&client->old_lease, MDL);
|
dhc6_lease_destroy(&client->old_lease, MDL);
|
||||||
@@ -5323,7 +5323,7 @@ dhc6_check_irt(struct client_state *client)
|
|||||||
#ifdef DHCP4o6
|
#ifdef DHCP4o6
|
||||||
if (!dhcpv4_over_dhcpv6)
|
if (!dhcpv4_over_dhcpv6)
|
||||||
#endif
|
#endif
|
||||||
exit(0);
|
finish(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
oc = lookup_option(&dhcpv6_universe, client->active_lease->options,
|
oc = lookup_option(&dhcpv6_universe, client->active_lease->options,
|
||||||
@@ -5382,7 +5382,7 @@ start_informed(struct client_state *client)
|
|||||||
dhcp4o6_start();
|
dhcp4o6_start();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
go_daemon();
|
detach();
|
||||||
|
|
||||||
if (client->old_lease != NULL) {
|
if (client->old_lease != NULL) {
|
||||||
dhc6_lease_destroy(&client->old_lease, MDL);
|
dhc6_lease_destroy(&client->old_lease, MDL);
|
||||||
|
@@ -84,6 +84,7 @@ u_int16_t remote_port = 0;
|
|||||||
int dhcp4o6_state = -1; /* -1 = stopped, 0 = polling, 1 = started */
|
int dhcp4o6_state = -1; /* -1 = stopped, 0 = polling, 1 = started */
|
||||||
#endif
|
#endif
|
||||||
int no_daemon = 0;
|
int no_daemon = 0;
|
||||||
|
int dfd[2] = { -1, -1 };
|
||||||
struct string_list *client_env = NULL;
|
struct string_list *client_env = NULL;
|
||||||
int client_env_count = 0;
|
int client_env_count = 0;
|
||||||
int onetry = 0;
|
int onetry = 0;
|
||||||
@@ -236,6 +237,51 @@ main(int argc, char **argv) {
|
|||||||
setlogmask(LOG_UPTO(LOG_INFO));
|
setlogmask(LOG_UPTO(LOG_INFO));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Parse arguments changing no_daemon */
|
||||||
|
for (i = 1; i < argc; i++) {
|
||||||
|
if (!strcmp(argv[i], "-r")) {
|
||||||
|
no_daemon = 1;
|
||||||
|
} else if (!strcmp(argv[i], "-x")) {
|
||||||
|
no_daemon = 0;
|
||||||
|
} else if (!strcmp(argv[i], "-d")) {
|
||||||
|
no_daemon = 1;
|
||||||
|
} else if (!strcmp(argv[i], "--version")) {
|
||||||
|
const char vstring[] = "isc-dhclient-";
|
||||||
|
IGNORE_RET(write(STDERR_FILENO, vstring,
|
||||||
|
strlen(vstring)));
|
||||||
|
IGNORE_RET(write(STDERR_FILENO,
|
||||||
|
PACKAGE_VERSION,
|
||||||
|
strlen(PACKAGE_VERSION)));
|
||||||
|
IGNORE_RET(write(STDERR_FILENO, "\n", 1));
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* When not forbidden prepare to become a daemon */
|
||||||
|
if (!no_daemon) {
|
||||||
|
int pid;
|
||||||
|
|
||||||
|
if (pipe(dfd) == -1)
|
||||||
|
log_fatal("Can't get pipe: %m");
|
||||||
|
if ((pid = fork ()) < 0)
|
||||||
|
log_fatal("Can't fork daemon: %m");
|
||||||
|
if (pid != 0) {
|
||||||
|
/* Parent: wait for the child to start */
|
||||||
|
int n;
|
||||||
|
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
do {
|
||||||
|
char buf;
|
||||||
|
|
||||||
|
n = read(dfd[0], &buf, 1);
|
||||||
|
if (n == 1)
|
||||||
|
_exit((int)buf);
|
||||||
|
} while (n == -1 && errno == EINTR);
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
/* Child */
|
||||||
|
(void) close(dfd[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/* Set up the isc and dns library managers */
|
/* Set up the isc and dns library managers */
|
||||||
status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
|
status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
|
||||||
NULL, NULL);
|
NULL, NULL);
|
||||||
@@ -260,7 +306,7 @@ main(int argc, char **argv) {
|
|||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
if (!strcmp(argv[i], "-r")) {
|
if (!strcmp(argv[i], "-r")) {
|
||||||
release_mode = 1;
|
release_mode = 1;
|
||||||
no_daemon = 1;
|
/* no_daemon = 1; */
|
||||||
#ifdef DHCPv6
|
#ifdef DHCPv6
|
||||||
} else if (!strcmp(argv[i], "-4")) {
|
} else if (!strcmp(argv[i], "-4")) {
|
||||||
if (local_family_set && local_family != AF_INET)
|
if (local_family_set && local_family != AF_INET)
|
||||||
@@ -288,7 +334,7 @@ main(int argc, char **argv) {
|
|||||||
#endif /* DHCPv6 */
|
#endif /* DHCPv6 */
|
||||||
} else if (!strcmp(argv[i], "-x")) { /* eXit, no release */
|
} else if (!strcmp(argv[i], "-x")) { /* eXit, no release */
|
||||||
release_mode = 0;
|
release_mode = 0;
|
||||||
no_daemon = 0;
|
/* no_daemon = 0; */
|
||||||
exit_mode = 1;
|
exit_mode = 1;
|
||||||
} else if (!strcmp(argv[i], "-p")) {
|
} else if (!strcmp(argv[i], "-p")) {
|
||||||
if (++i == argc)
|
if (++i == argc)
|
||||||
@@ -297,7 +343,7 @@ main(int argc, char **argv) {
|
|||||||
log_debug("binding to user-specified port %d",
|
log_debug("binding to user-specified port %d",
|
||||||
ntohs(local_port));
|
ntohs(local_port));
|
||||||
} else if (!strcmp(argv[i], "-d")) {
|
} else if (!strcmp(argv[i], "-d")) {
|
||||||
no_daemon = 1;
|
/* no_daemon = 1; */
|
||||||
quiet = 0;
|
quiet = 0;
|
||||||
} else if (!strcmp(argv[i], "-pf")) {
|
} else if (!strcmp(argv[i], "-pf")) {
|
||||||
if (++i == argc)
|
if (++i == argc)
|
||||||
@@ -422,15 +468,6 @@ main(int argc, char **argv) {
|
|||||||
std_dhcid = 1;
|
std_dhcid = 1;
|
||||||
} else if (!strcmp(argv[i], "-v")) {
|
} else if (!strcmp(argv[i], "-v")) {
|
||||||
quiet = 0;
|
quiet = 0;
|
||||||
} else if (!strcmp(argv[i], "--version")) {
|
|
||||||
const char vstring[] = "isc-dhclient-";
|
|
||||||
IGNORE_RET(write(STDERR_FILENO, vstring,
|
|
||||||
strlen(vstring)));
|
|
||||||
IGNORE_RET(write(STDERR_FILENO,
|
|
||||||
PACKAGE_VERSION,
|
|
||||||
strlen(PACKAGE_VERSION)));
|
|
||||||
IGNORE_RET(write(STDERR_FILENO, "\n", 1));
|
|
||||||
exit(0);
|
|
||||||
} else if (argv[i][0] == '-') {
|
} else if (argv[i][0] == '-') {
|
||||||
usage("Unknown command: %s", argv[i]);
|
usage("Unknown command: %s", argv[i]);
|
||||||
} else if (interfaces_requested < 0) {
|
} else if (interfaces_requested < 0) {
|
||||||
@@ -613,7 +650,7 @@ main(int argc, char **argv) {
|
|||||||
if (release_mode || (wanted_ia_na > 0) ||
|
if (release_mode || (wanted_ia_na > 0) ||
|
||||||
wanted_ia_ta || wanted_ia_pd ||
|
wanted_ia_ta || wanted_ia_pd ||
|
||||||
(interfaces_requested != 1)) {
|
(interfaces_requested != 1)) {
|
||||||
usage("Stateless commnad: %s incompatibile with "
|
usage("Stateless command: %s incompatibile with "
|
||||||
"other commands", "-S");
|
"other commands", "-S");
|
||||||
}
|
}
|
||||||
#if defined(DHCPv6) && defined(DHCP4o6)
|
#if defined(DHCPv6) && defined(DHCP4o6)
|
||||||
@@ -621,7 +658,7 @@ main(int argc, char **argv) {
|
|||||||
#else
|
#else
|
||||||
run_stateless(exit_mode, 0);
|
run_stateless(exit_mode, 0);
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
finish(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Discover all the network interfaces. */
|
/* Discover all the network interfaces. */
|
||||||
@@ -663,7 +700,7 @@ main(int argc, char **argv) {
|
|||||||
if (!persist) {
|
if (!persist) {
|
||||||
/* Nothing more to do. */
|
/* Nothing more to do. */
|
||||||
log_info("No broadcast interfaces found - exiting.");
|
log_info("No broadcast interfaces found - exiting.");
|
||||||
exit(0);
|
finish(0);
|
||||||
}
|
}
|
||||||
} else if (!release_mode && !exit_mode) {
|
} else if (!release_mode && !exit_mode) {
|
||||||
/* Call the script with the list of interfaces. */
|
/* Call the script with the list of interfaces. */
|
||||||
@@ -800,16 +837,16 @@ main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (exit_mode)
|
if (exit_mode)
|
||||||
return 0;
|
finish(0);
|
||||||
if (release_mode) {
|
if (release_mode) {
|
||||||
#ifndef DHCPv6
|
#ifndef DHCPv6
|
||||||
return 0;
|
finish(0);
|
||||||
#else
|
#else
|
||||||
if ((local_family == AF_INET6) || dhcpv4_over_dhcpv6) {
|
if ((local_family == AF_INET6) || dhcpv4_over_dhcpv6) {
|
||||||
if (onetry)
|
if (onetry)
|
||||||
return 0;
|
finish(0);
|
||||||
} else
|
} else
|
||||||
return 0;
|
finish(0);
|
||||||
#endif /* DHCPv6 */
|
#endif /* DHCPv6 */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -852,7 +889,7 @@ main(int argc, char **argv) {
|
|||||||
/* If we're not supposed to wait before getting the address,
|
/* If we're not supposed to wait before getting the address,
|
||||||
don't. */
|
don't. */
|
||||||
if (nowait)
|
if (nowait)
|
||||||
go_daemon();
|
detach();
|
||||||
|
|
||||||
/* If we're not going to daemonize, write the pid file
|
/* If we're not going to daemonize, write the pid file
|
||||||
now. */
|
now. */
|
||||||
@@ -963,7 +1000,7 @@ void run_stateless(int exit_mode, u_int16_t port)
|
|||||||
/* If we're not supposed to wait before getting the address,
|
/* If we're not supposed to wait before getting the address,
|
||||||
don't. */
|
don't. */
|
||||||
if (nowait)
|
if (nowait)
|
||||||
go_daemon();
|
detach();
|
||||||
|
|
||||||
/* If we're not going to daemonize, write the pid file
|
/* If we're not going to daemonize, write the pid file
|
||||||
now. */
|
now. */
|
||||||
@@ -1404,7 +1441,7 @@ void bind_lease (client)
|
|||||||
if (!quiet)
|
if (!quiet)
|
||||||
log_info("Unable to obtain a lease on first "
|
log_info("Unable to obtain a lease on first "
|
||||||
"try (declined). Exiting.");
|
"try (declined). Exiting.");
|
||||||
exit(2);
|
finish(2);
|
||||||
} else {
|
} else {
|
||||||
state_init(client);
|
state_init(client);
|
||||||
return;
|
return;
|
||||||
@@ -1433,7 +1470,7 @@ void bind_lease (client)
|
|||||||
(long)(client->active->renewal - cur_time));
|
(long)(client->active->renewal - cur_time));
|
||||||
client->state = S_BOUND;
|
client->state = S_BOUND;
|
||||||
reinitialize_interfaces();
|
reinitialize_interfaces();
|
||||||
go_daemon();
|
detach();
|
||||||
#if defined (NSUPDATE)
|
#if defined (NSUPDATE)
|
||||||
if (client->config->do_forward_update)
|
if (client->config->do_forward_update)
|
||||||
dhclient_schedule_updates(client, &client->active->address, 1);
|
dhclient_schedule_updates(client, &client->active->address, 1);
|
||||||
@@ -2373,7 +2410,7 @@ void state_panic (cpp)
|
|||||||
state_bound (client);
|
state_bound (client);
|
||||||
}
|
}
|
||||||
reinitialize_interfaces ();
|
reinitialize_interfaces ();
|
||||||
go_daemon ();
|
detach ();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2413,7 +2450,7 @@ void state_panic (cpp)
|
|||||||
if (!quiet)
|
if (!quiet)
|
||||||
log_info ("Unable to obtain a lease on first try.%s",
|
log_info ("Unable to obtain a lease on first try.%s",
|
||||||
" Exiting.");
|
" Exiting.");
|
||||||
exit (2);
|
finish(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
log_info ("No working leases in persistent database - sleeping.");
|
log_info ("No working leases in persistent database - sleeping.");
|
||||||
@@ -2427,7 +2464,7 @@ void state_panic (cpp)
|
|||||||
tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
|
tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
|
||||||
random() % 1000000 : cur_tv.tv_usec;
|
random() % 1000000 : cur_tv.tv_usec;
|
||||||
add_timeout(&tv, state_init, client, 0, 0);
|
add_timeout(&tv, state_init, client, 0, 0);
|
||||||
go_daemon ();
|
detach ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_request (cpp)
|
void send_request (cpp)
|
||||||
@@ -4200,10 +4237,20 @@ int dhcp_option_ev_name (buf, buflen, option)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void go_daemon ()
|
void finish (char ret)
|
||||||
{
|
{
|
||||||
static int state = 0;
|
if (no_daemon || dfd[0] == -1 || dfd[1] == -1)
|
||||||
int pid;
|
exit((int)ret);
|
||||||
|
if (write(dfd[1], &ret, 1) != 1)
|
||||||
|
log_fatal("write to parent: %m");
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
dfd[0] = dfd[1] = -1;
|
||||||
|
exit((int)ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
void detach ()
|
||||||
|
{
|
||||||
|
char buf = 0;
|
||||||
|
|
||||||
/* Don't become a daemon if the user requested otherwise. */
|
/* Don't become a daemon if the user requested otherwise. */
|
||||||
if (no_daemon) {
|
if (no_daemon) {
|
||||||
@@ -4212,18 +4259,18 @@ void go_daemon ()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Only do it once. */
|
/* Only do it once. */
|
||||||
if (state)
|
if (dfd[0] == -1 || dfd[1] == -1)
|
||||||
return;
|
return;
|
||||||
state = 1;
|
|
||||||
|
/* Signal parent we started successfully. */
|
||||||
|
if (write(dfd[1], &buf, 1) != 1)
|
||||||
|
log_fatal("write to parent: %m");
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
dfd[0] = dfd[1] = -1;
|
||||||
|
|
||||||
/* Stop logging to stderr... */
|
/* Stop logging to stderr... */
|
||||||
log_perror = 0;
|
log_perror = 0;
|
||||||
|
|
||||||
/* Become a daemon... */
|
|
||||||
if ((pid = fork ()) < 0)
|
|
||||||
log_fatal ("Can't fork daemon: %m");
|
|
||||||
else if (pid)
|
|
||||||
exit (0);
|
|
||||||
/* Become session leader and get pid... */
|
/* Become session leader and get pid... */
|
||||||
(void) setsid ();
|
(void) setsid ();
|
||||||
|
|
||||||
@@ -4240,6 +4287,7 @@ void go_daemon ()
|
|||||||
write_client_pid_file ();
|
write_client_pid_file ();
|
||||||
|
|
||||||
IGNORE_RET (chdir("/"));
|
IGNORE_RET (chdir("/"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_client_pid_file ()
|
void write_client_pid_file ()
|
||||||
@@ -4376,7 +4424,7 @@ void do_release(client)
|
|||||||
|
|
||||||
#if defined(DHCPv6) && defined(DHCP4o6)
|
#if defined(DHCPv6) && defined(DHCP4o6)
|
||||||
if (dhcpv4_over_dhcpv6)
|
if (dhcpv4_over_dhcpv6)
|
||||||
exit(0);
|
finish(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4507,7 +4555,7 @@ static void shutdown_exit (void *foo)
|
|||||||
/* get rid of the pid if we can */
|
/* get rid of the pid if we can */
|
||||||
if (no_pid_file == ISC_FALSE)
|
if (no_pid_file == ISC_FALSE)
|
||||||
(void) unlink(path_dhclient_pid);
|
(void) unlink(path_dhclient_pid);
|
||||||
exit (0);
|
finish(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined (NSUPDATE)
|
#if defined (NSUPDATE)
|
||||||
|
@@ -2975,7 +2975,8 @@ void client_envadd (struct client_state *,
|
|||||||
__attribute__((__format__(__printf__,4,5)));
|
__attribute__((__format__(__printf__,4,5)));
|
||||||
|
|
||||||
struct client_lease *packet_to_lease (struct packet *, struct client_state *);
|
struct client_lease *packet_to_lease (struct packet *, struct client_state *);
|
||||||
void go_daemon (void);
|
void finish (char);
|
||||||
|
void detach (void);
|
||||||
void write_client_pid_file (void);
|
void write_client_pid_file (void);
|
||||||
void client_location_changed (void);
|
void client_location_changed (void);
|
||||||
void do_release (struct client_state *);
|
void do_release (struct client_state *);
|
||||||
|
@@ -77,6 +77,9 @@ int missing_circuit_id = 0; /* Circuit ID option in matching RAI option
|
|||||||
was missing. */
|
was missing. */
|
||||||
int max_hop_count = 10; /* Maximum hop count */
|
int max_hop_count = 10; /* Maximum hop count */
|
||||||
|
|
||||||
|
int no_daemon = 0;
|
||||||
|
int dfd[2] = { -1, -1 };
|
||||||
|
|
||||||
#ifdef DHCPv6
|
#ifdef DHCPv6
|
||||||
/* Force use of DHCPv6 interface-id option. */
|
/* Force use of DHCPv6 interface-id option. */
|
||||||
isc_boolean_t use_if_id = ISC_FALSE;
|
isc_boolean_t use_if_id = ISC_FALSE;
|
||||||
@@ -223,7 +226,7 @@ main(int argc, char **argv) {
|
|||||||
struct server_list *sp = NULL;
|
struct server_list *sp = NULL;
|
||||||
char *service_local = NULL, *service_remote = NULL;
|
char *service_local = NULL, *service_remote = NULL;
|
||||||
u_int16_t port_local = 0, port_remote = 0;
|
u_int16_t port_local = 0, port_remote = 0;
|
||||||
int no_daemon = 0, quiet = 0;
|
int quiet = 0;
|
||||||
int fd;
|
int fd;
|
||||||
int i;
|
int i;
|
||||||
#ifdef DHCPv6
|
#ifdef DHCPv6
|
||||||
@@ -256,6 +259,50 @@ main(int argc, char **argv) {
|
|||||||
setlogmask(LOG_UPTO(LOG_INFO));
|
setlogmask(LOG_UPTO(LOG_INFO));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Parse arguments changing no_daemon */
|
||||||
|
for (i = 1; i < argc; i++) {
|
||||||
|
if (!strcmp(argv[i], "-d")) {
|
||||||
|
no_daemon = 1;
|
||||||
|
} else if (!strcmp(argv[i], "--version")) {
|
||||||
|
log_info("isc-dhcrelay-%s", PACKAGE_VERSION);
|
||||||
|
exit(0);
|
||||||
|
} else if (!strcmp(argv[i], "--help") ||
|
||||||
|
!strcmp(argv[i], "-h")) {
|
||||||
|
log_info(DHCRELAY_USAGE,
|
||||||
|
#ifdef DHCPv6
|
||||||
|
isc_file_basename(progname),
|
||||||
|
#endif
|
||||||
|
isc_file_basename(progname));
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* When not forbidden prepare to become a daemon */
|
||||||
|
if (!no_daemon) {
|
||||||
|
int pid;
|
||||||
|
|
||||||
|
if (pipe(dfd) == -1)
|
||||||
|
log_fatal("Can't get pipe: %m");
|
||||||
|
if ((pid = fork ()) < 0)
|
||||||
|
log_fatal("Can't fork daemon: %m");
|
||||||
|
if (pid != 0) {
|
||||||
|
/* Parent: wait for the child to start */
|
||||||
|
int n;
|
||||||
|
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
do {
|
||||||
|
char buf;
|
||||||
|
|
||||||
|
n = read(dfd[0], &buf, 1);
|
||||||
|
if (n == 1)
|
||||||
|
_exit(0);
|
||||||
|
} while (n == -1 && errno == EINTR);
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
/* Child */
|
||||||
|
(void) close(dfd[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Set up the isc and dns library managers */
|
/* Set up the isc and dns library managers */
|
||||||
status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
|
status = dhcp_context_create(DHCP_CONTEXT_PRE_DB | DHCP_CONTEXT_POST_DB,
|
||||||
NULL, NULL);
|
NULL, NULL);
|
||||||
@@ -288,7 +335,7 @@ main(int argc, char **argv) {
|
|||||||
local_family = AF_INET6;
|
local_family = AF_INET6;
|
||||||
#endif
|
#endif
|
||||||
} else if (!strcmp(argv[i], "-d")) {
|
} else if (!strcmp(argv[i], "-d")) {
|
||||||
no_daemon = 1;
|
/* no_daemon = 1; */
|
||||||
} else if (!strcmp(argv[i], "-q")) {
|
} else if (!strcmp(argv[i], "-q")) {
|
||||||
quiet = 1;
|
quiet = 1;
|
||||||
quiet_interface_discovery = 1;
|
quiet_interface_discovery = 1;
|
||||||
@@ -481,17 +528,6 @@ main(int argc, char **argv) {
|
|||||||
no_dhcrelay_pid = ISC_TRUE;
|
no_dhcrelay_pid = ISC_TRUE;
|
||||||
} else if (!strcmp(argv[i], "--no-pid")) {
|
} else if (!strcmp(argv[i], "--no-pid")) {
|
||||||
no_pid_file = ISC_TRUE;
|
no_pid_file = ISC_TRUE;
|
||||||
} else if (!strcmp(argv[i], "--version")) {
|
|
||||||
log_info("isc-dhcrelay-%s", PACKAGE_VERSION);
|
|
||||||
exit(0);
|
|
||||||
} else if (!strcmp(argv[i], "--help") ||
|
|
||||||
!strcmp(argv[i], "-h")) {
|
|
||||||
log_info(DHCRELAY_USAGE,
|
|
||||||
#ifdef DHCPv6
|
|
||||||
isc_file_basename(progname),
|
|
||||||
#endif
|
|
||||||
isc_file_basename(progname));
|
|
||||||
exit(0);
|
|
||||||
} else if (argv[i][0] == '-') {
|
} else if (argv[i][0] == '-') {
|
||||||
usage("Unknown command: %s", argv[i]);
|
usage("Unknown command: %s", argv[i]);
|
||||||
} else {
|
} else {
|
||||||
@@ -647,17 +683,21 @@ main(int argc, char **argv) {
|
|||||||
|
|
||||||
/* Become a daemon... */
|
/* Become a daemon... */
|
||||||
if (!no_daemon) {
|
if (!no_daemon) {
|
||||||
int pid;
|
char buf = 0;
|
||||||
FILE *pf;
|
FILE *pf;
|
||||||
int pfdesc;
|
int pfdesc;
|
||||||
|
|
||||||
log_perror = 0;
|
log_perror = 0;
|
||||||
|
|
||||||
if ((pid = fork()) < 0)
|
/* Signal parent we started successfully. */
|
||||||
log_fatal("Can't fork daemon: %m");
|
if (dfd[0] != -1 && dfd[1] != -1) {
|
||||||
else if (pid)
|
if (write(dfd[1], &buf, 1) != 1)
|
||||||
exit(0);
|
log_fatal("write to parent: %m");
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
dfd[0] = dfd[1] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create the pid file. */
|
||||||
if (no_pid_file == ISC_FALSE) {
|
if (no_pid_file == ISC_FALSE) {
|
||||||
pfdesc = open(path_dhcrelay_pid,
|
pfdesc = open(path_dhcrelay_pid,
|
||||||
O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
||||||
@@ -1865,12 +1905,19 @@ parse_allow_deny(struct option_cache **oc, struct parse *p, int i) {
|
|||||||
isc_result_t
|
isc_result_t
|
||||||
dhcp_set_control_state(control_object_state_t oldstate,
|
dhcp_set_control_state(control_object_state_t oldstate,
|
||||||
control_object_state_t newstate) {
|
control_object_state_t newstate) {
|
||||||
|
char buf = 0;
|
||||||
|
|
||||||
if (newstate != server_shutdown)
|
if (newstate != server_shutdown)
|
||||||
return ISC_R_SUCCESS;
|
return ISC_R_SUCCESS;
|
||||||
|
|
||||||
if (no_pid_file == ISC_FALSE)
|
if (no_pid_file == ISC_FALSE)
|
||||||
(void) unlink(path_dhcrelay_pid);
|
(void) unlink(path_dhcrelay_pid);
|
||||||
|
|
||||||
|
if (!no_daemon && dfd[0] != -1 && dfd[1] != -1) {
|
||||||
|
IGNORE_RET(write(dfd[1], &buf, 1));
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
dfd[0] = dfd[1] = -1;
|
||||||
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -220,6 +220,7 @@ main(int argc, char **argv) {
|
|||||||
char pbuf [20];
|
char pbuf [20];
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
int daemon = 1;
|
int daemon = 1;
|
||||||
|
int dfd[2] = { -1, -1 };
|
||||||
#endif
|
#endif
|
||||||
int quiet = 0;
|
int quiet = 0;
|
||||||
char *server = (char *)0;
|
char *server = (char *)0;
|
||||||
@@ -269,6 +270,68 @@ main(int argc, char **argv) {
|
|||||||
else if (fd != -1)
|
else if (fd != -1)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
/* Parse arguments changing daemon */
|
||||||
|
for (i = 1; i < argc; i++) {
|
||||||
|
if (!strcmp (argv [i], "-f")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
} else if (!strcmp (argv [i], "-d")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
} else if (!strcmp (argv [i], "-t")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
} else if (!strcmp (argv [i], "-T")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
} else if (!strcmp (argv [i], "--version")) {
|
||||||
|
const char vstring[] = "isc-dhcpd-";
|
||||||
|
IGNORE_RET(write(STDERR_FILENO, vstring,
|
||||||
|
strlen(vstring)));
|
||||||
|
IGNORE_RET(write(STDERR_FILENO,
|
||||||
|
PACKAGE_VERSION,
|
||||||
|
strlen(PACKAGE_VERSION)));
|
||||||
|
IGNORE_RET(write(STDERR_FILENO, "\n", 1));
|
||||||
|
exit (0);
|
||||||
|
#ifdef TRACING
|
||||||
|
} else if (!strcmp (argv [i], "-play")) {
|
||||||
|
#ifndef DEBUG
|
||||||
|
daemon = 0;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef DEBUG
|
||||||
|
/* When not forbidden prepare to become a daemon */
|
||||||
|
if (daemon) {
|
||||||
|
if (pipe(dfd) == -1)
|
||||||
|
log_fatal("Can't get pipe: %m");
|
||||||
|
if ((pid = fork ()) < 0)
|
||||||
|
log_fatal("Can't fork daemon: %m");
|
||||||
|
if (pid != 0) {
|
||||||
|
/* Parent: wait for the child to start */
|
||||||
|
int n;
|
||||||
|
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
do {
|
||||||
|
char buf;
|
||||||
|
|
||||||
|
n = read(dfd[0], &buf, 1);
|
||||||
|
if (n == 1)
|
||||||
|
_exit((int)buf);
|
||||||
|
} while (n == -1 && errno == EINTR);
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
/* Child */
|
||||||
|
(void) close(dfd[0]);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Set up the isc and dns library managers */
|
/* Set up the isc and dns library managers */
|
||||||
status = dhcp_context_create(DHCP_CONTEXT_PRE_DB,
|
status = dhcp_context_create(DHCP_CONTEXT_PRE_DB,
|
||||||
NULL, NULL);
|
NULL, NULL);
|
||||||
@@ -304,11 +367,11 @@ main(int argc, char **argv) {
|
|||||||
ntohs (local_port));
|
ntohs (local_port));
|
||||||
} else if (!strcmp (argv [i], "-f")) {
|
} else if (!strcmp (argv [i], "-f")) {
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
daemon = 0;
|
/* daemon = 0; */
|
||||||
#endif
|
#endif
|
||||||
} else if (!strcmp (argv [i], "-d")) {
|
} else if (!strcmp (argv [i], "-d")) {
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
daemon = 0;
|
/* daemon = 0; */
|
||||||
#endif
|
#endif
|
||||||
log_perror = -1;
|
log_perror = -1;
|
||||||
} else if (!strcmp (argv [i], "-s")) {
|
} else if (!strcmp (argv [i], "-s")) {
|
||||||
@@ -349,14 +412,14 @@ main(int argc, char **argv) {
|
|||||||
} else if (!strcmp (argv [i], "-t")) {
|
} else if (!strcmp (argv [i], "-t")) {
|
||||||
/* test configurations only */
|
/* test configurations only */
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
daemon = 0;
|
/* daemon = 0; */
|
||||||
#endif
|
#endif
|
||||||
cftest = 1;
|
cftest = 1;
|
||||||
log_perror = -1;
|
log_perror = -1;
|
||||||
} else if (!strcmp (argv [i], "-T")) {
|
} else if (!strcmp (argv [i], "-T")) {
|
||||||
/* test configurations and lease file only */
|
/* test configurations and lease file only */
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
daemon = 0;
|
/* daemon = 0; */
|
||||||
#endif
|
#endif
|
||||||
cftest = 1;
|
cftest = 1;
|
||||||
lftest = 1;
|
lftest = 1;
|
||||||
@@ -391,15 +454,6 @@ main(int argc, char **argv) {
|
|||||||
dhcpv4_over_dhcpv6 = 1;
|
dhcpv4_over_dhcpv6 = 1;
|
||||||
#endif /* DHCP4o6 */
|
#endif /* DHCP4o6 */
|
||||||
#endif /* DHCPv6 */
|
#endif /* DHCPv6 */
|
||||||
} else if (!strcmp (argv [i], "--version")) {
|
|
||||||
const char vstring[] = "isc-dhcpd-";
|
|
||||||
IGNORE_RET(write(STDERR_FILENO, vstring,
|
|
||||||
strlen(vstring)));
|
|
||||||
IGNORE_RET(write(STDERR_FILENO,
|
|
||||||
PACKAGE_VERSION,
|
|
||||||
strlen(PACKAGE_VERSION)));
|
|
||||||
IGNORE_RET(write(STDERR_FILENO, "\n", 1));
|
|
||||||
exit (0);
|
|
||||||
#if defined (TRACING)
|
#if defined (TRACING)
|
||||||
} else if (!strcmp (argv [i], "-tf")) {
|
} else if (!strcmp (argv [i], "-tf")) {
|
||||||
if (++i == argc)
|
if (++i == argc)
|
||||||
@@ -845,14 +899,6 @@ main(int argc, char **argv) {
|
|||||||
#endif /* DHCPv6 */
|
#endif /* DHCPv6 */
|
||||||
|
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
if (daemon) {
|
|
||||||
/* First part of becoming a daemon... */
|
|
||||||
if ((pid = fork ()) < 0)
|
|
||||||
log_fatal ("Can't fork daemon: %m");
|
|
||||||
else if (pid)
|
|
||||||
exit (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Second part of dealing with pid files. Now
|
* Second part of dealing with pid files. Now
|
||||||
* that we have forked we can write our pid if
|
* that we have forked we can write our pid if
|
||||||
@@ -894,6 +940,15 @@ main(int argc, char **argv) {
|
|||||||
log_perror = 0;
|
log_perror = 0;
|
||||||
|
|
||||||
if (daemon) {
|
if (daemon) {
|
||||||
|
if (dfd[0] != -1 && dfd[1] != -1) {
|
||||||
|
char buf = 0;
|
||||||
|
|
||||||
|
if (write(dfd[1], &buf, 1) != 1)
|
||||||
|
log_fatal("write to parent: %m");
|
||||||
|
(void) close(dfd[1]);
|
||||||
|
dfd[0] = dfd[1] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Become session leader and get pid... */
|
/* Become session leader and get pid... */
|
||||||
(void) setsid();
|
(void) setsid();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user