Remove loads of code specific to fork()less systems. dhcpcd no longer backgrounds on these systems, but is a lot lighter.

This commit is contained in:
Roy Marples 2008-08-11 08:32:52 +00:00
parent 37e7de1899
commit 523f398e5e
4 changed files with 11 additions and 184 deletions

4
README
View File

@ -17,6 +17,8 @@ OS=BSD | Linux
If you're building for an MMU-less system where fork() does not work, you
should add -DTHERE_IS_NO_FORK to your CPPFLAGS.
This also puts the --no-background flag on and stops the --background flag
from working.
You can change the default dir with these knobs.
For example, to satisfy FHS compliance you would do this:-
@ -62,4 +64,4 @@ ChangeLog
---------
We no longer supply a ChangeLog.
However, you're more than welcome to read the git commit comments at
http://git.marples.name/?p=dhcpcd/.git;a=summary
http://git.marples.name/?p=dhcpcd.git;a=summary

View File

@ -182,19 +182,17 @@ get_dhcp_op(uint8_t type)
return NULL;
}
#ifdef THERE_IS_NO_FORK
#define daemonise(a,b) 0
#else
static int
daemonise(struct if_state *state, const struct options *options)
{
pid_t pid;
sigset_t full;
sigset_t old;
#ifdef THERE_IS_NO_FORK
char **argv;
int i;
#else
char buf = '\0';
int sidpipe[2];
#endif
if (state->options & DHCPCD_DAEMONISED ||
!(options->options & DHCPCD_DAEMONISE))
@ -203,7 +201,6 @@ daemonise(struct if_state *state, const struct options *options)
sigfillset(&full);
sigprocmask(SIG_SETMASK, &full, &old);
#ifndef THERE_IS_NO_FORK
/* Setup a signal pipe so parent knows when to exit. */
if (pipe(sidpipe) == -1) {
logger(LOG_ERR,"pipe: %s", strerror(errno));
@ -233,36 +230,6 @@ daemonise(struct if_state *state, const struct options *options)
close(sidpipe[0]);
break;
}
#else
logger(LOG_INFO, "forking to background");
/* We need to add --daemonise to our options */
argv = xmalloc(sizeof(char *) * (dhcpcd_argc + 4));
argv[0] = dhcpcd;
for (i = 1; i < dhcpcd_argc; i++)
argv[i] = dhcpcd_argv[i];
argv[i] = (char *)"--daemonised";
if (dhcpcd_skiproutes) {
argv[++i] = (char *)"--skiproutes";
argv[++i] = dhcpcd_skiproutes;
}
argv[i + 1] = NULL;
switch (pid = vfork()) {
case -1:
logger(LOG_ERR, "vfork: %s", strerror(errno));
_exit(EXIT_FAILURE);
case 0:
signal_reset();
sigprocmask(SIG_SETMASK, &old, NULL);
execvp(dhcpcd, argv);
/* Must not use stdio here. */
write(STDERR_FILENO, "exec failed\n", 12);
_exit(EXIT_FAILURE);
}
free(argv);
#endif
/* Done with the fd now */
if (pid != 0) {
@ -280,6 +247,7 @@ daemonise(struct if_state *state, const struct options *options)
state->options |= DHCPCD_PERSISTENT | DHCPCD_FORKED;
return -1;
}
#endif
#define THIRTY_YEARS_IN_SECONDS 946707779
static size_t
@ -446,10 +414,8 @@ get_old_lease(struct if_state *state)
dhcp->servername[0] = '\0';
if (!IN_LINKLOCAL(ntohl(dhcp->yiaddr))) {
#ifndef THERE_IS_NO_FORK
if (!(state->options & DHCPCD_LASTLEASE))
goto eexit;
#endif
/* Ensure that we can still use the lease */
if (gettimeofday(&tv, NULL) == -1) {
@ -521,19 +487,6 @@ client_setup(struct if_state *state, const struct options *options)
logger(LOG_ERR, "cannot request a link local address");
return -1;
}
#ifdef THERE_IS_NO_FORK
if (options->options & DHCPCD_DAEMONISED) {
iface->addr.s_addr = lease->addr.s_addr;
iface->net.s_addr = lease->net.s_addr;
state->new = state->offer;
state->offer = NULL;
get_option_addr(&lease->server.s_addr,
state->new, DHCP_SERVERID);
open_socket(iface, ETHERTYPE_ARP);
state->state = STATE_ANNOUNCING;
state->timeout.tv_sec = ANNOUNCE_INTERVAL;
}
#endif
} else {
lease->addr.s_addr = options->request_address.s_addr;
lease->net.s_addr = options->request_netmask.s_addr;

View File

@ -244,19 +244,6 @@ configure_routes(struct interface *iface, const struct dhcp_message *dhcp,
int retval = 0;
char *addr;
#ifdef THERE_IS_NO_FORK
char *skipp;
size_t skiplen;
int skip = 0;
free(dhcpcd_skiproutes);
/* We can never have more than 255 routes. So we need space
* for 255 3 digit numbers and commas */
skiplen = 255 * 4 + 1;
skipp = dhcpcd_skiproutes = xmalloc(sizeof(char) * skiplen);
*skipp = '\0';
#endif
ort = get_option_routes(dhcp);
#ifdef IPV4LL_ALWAYSROUTE
@ -286,43 +273,6 @@ configure_routes(struct interface *iface, const struct dhcp_message *dhcp,
}
#endif
#ifdef THERE_IS_NO_FORK
if (dhcpcd_skiproutes) {
int i = -1;
char *sk, *skp, *token;
free_routes(iface->routes);
for (rt = ort; rt; rt = rt->next) {
i++;
/* Check that we did add this route or not */
sk = skp = xstrdup(dhcpcd_skiproutes);
while ((token = strsep(&skp, ","))) {
if (isdigit((unsigned char)*token) &&
atoi(token) == i)
break;
}
free(sk);
if (token)
continue;
if (nr) {
rtn->next = xmalloc(sizeof(*rtn));
rtn = rtn->next;
} else {
nr = rtn = xmalloc(sizeof(*rtn));
}
rtn->dest.s_addr = rt->dest.s_addr;
rtn->net.s_addr = rt->net.s_addr;
rtn->gate.s_addr = rt->gate.s_addr;
rtn->next = NULL;
}
iface->routes = nr;
nr = NULL;
/* We no longer need this */
free(dhcpcd_skiproutes);
dhcpcd_skiproutes = NULL;
}
#endif
/* Now remove old routes we no longer use.
* We should do this in reverse order. */
iface->routes = reverse_routes(iface->routes);
@ -369,37 +319,10 @@ configure_routes(struct interface *iface, const struct dhcp_message *dhcp,
rtn->gate.s_addr = rt->gate.s_addr;
rtn->next = NULL;
}
#ifdef THERE_IS_NO_FORK
/* If we have daemonised yet we need to record which routes
* we failed to add so we can skip them */
else if (!(options->options & DHCPCD_DAEMONISED)) {
/* We can never have more than 255 / 4 routes,
* so 3 chars is plently */
printf("foo\n");
if (*skipp)
*skipp++ = ',';
skipp += snprintf(skipp,
dhcpcd_skiproutes + skiplen - skipp,
"%d", skip);
}
skip++;
#endif
}
free_routes(ort);
free_routes(iface->routes);
iface->routes = nr;
#ifdef THERE_IS_NO_FORK
if (dhcpcd_skiproutes) {
if (*dhcpcd_skiproutes)
*skipp = '\0';
else {
free(dhcpcd_skiproutes);
dhcpcd_skiproutes = NULL;
}
}
#endif
return retval;
}

View File

@ -28,7 +28,6 @@
const char copyright[] = "Copyright (c) 2006-2008 Roy Marples";
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <arpa/inet.h>
@ -92,10 +91,6 @@ static const struct option longopts[] = {
{"blacklist", required_argument, NULL, 'X'},
{"help", no_argument, &dohelp, 1},
{"version", no_argument, &doversion, 1},
#ifdef THERE_IS_NO_FORK
{"daemonised", no_argument, NULL, 'z'},
{"skiproutes", required_argument, NULL, 'Z'},
#endif
#ifdef CMDLINE_COMPAT
{"nohostname", no_argument, NULL, 'H'},
{"nomtu", no_argument, NULL, 'M'},
@ -107,14 +102,6 @@ static const struct option longopts[] = {
{NULL, 0, NULL, '\0'}
};
#ifdef THERE_IS_NO_FORK
char dhcpcd[PATH_MAX];
char **dhcpcd_argv = NULL;
int dhcpcd_argc = 0;
char *dhcpcd_skiproutes = NULL;
#define EXTRA_OPTS "zZ:"
#endif
#ifdef CMDLINE_COMPAT
# define EXTRA_OPTS "HMNRSY"
#endif
@ -608,11 +595,6 @@ main(int argc, char **argv)
FILE *f;
char *cf = NULL;
char *intf = NULL;
#ifdef THERE_IS_NO_FORK
char argvp[PATH_MAX];
char *path, *token;
struct stat sb;
#endif
closefrom(3);
/* Saves calling fflush(stream) in the logger */
@ -645,27 +627,6 @@ main(int argc, char **argv)
}
#endif
#ifdef THERE_IS_NO_FORK
dhcpcd_argv = argv;
dhcpcd_argc = argc;
if (*argv[0] == '/' || *argv[0] == '.')
strncpy(argvp, argv[0], sizeof(argvp));
else {
p = path = xstrdup(getenv("PATH"));
while ((token = strsep(&p, ":"))) {
snprintf(argvp, sizeof(argvp), "%s/%s", token, argv[0]);
if (stat(argvp, &sb) == 0)
break;
}
free(path);
}
if (!realpath(argvp, dhcpcd)) {
logger(LOG_ERR, "unable to resolve the path `%s': %s\n",
argv[0], strerror(errno));
goto abort;
}
#endif
gethostname(options->hostname + 1, sizeof(options->hostname));
if (strcmp(options->hostname + 1, "(none)") == 0 ||
strcmp(options->hostname + 1, "localhost") == 0)
@ -790,15 +751,6 @@ main(int argc, char **argv)
break;
case 'f':
break;
#ifdef THERE_IS_NO_FORK
case 'z':
options->options |= DHCPCD_DAEMONISED;
close_fds();
break;
case 'Z':
dhcpcd_skiproutes = xstrdup(optarg);
break;
#endif
case 'k':
sig = SIGHUP;
break;
@ -842,6 +794,10 @@ main(int argc, char **argv)
}
}
#ifdef THERE_IS_NO_FORK
options->options &= ~DHCPCD_DAEMONISE;
#endif
if (options->request_address.s_addr == 0 &&
(options->options & DHCPCD_INFORM ||
options->options & DHCPCD_REQUEST))
@ -972,13 +928,6 @@ abort:
}
free(options->blacklist);
free(options);
#ifdef THERE_IS_NO_FORK
/* There may have been an error before the dhcp_run function
* clears this, so just do it here to be safe */
free(dhcpcd_skiproutes);
#endif
exit(retval);
/* NOTREACHED */
}