2006-11-28 04:23:22 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2007-11-06 23:03:38 +08:00
|
|
|
* Copyright 2006-2007 Roy Marples <roy@marples.name>
|
2006-11-28 04:23:22 +08:00
|
|
|
*
|
2007-11-08 00:26:41 +08:00
|
|
|
* Distributed under the terms of the GNU General Public License v2
|
2006-11-28 04:23:22 +08:00
|
|
|
*/
|
|
|
|
|
2006-12-15 19:23:32 +08:00
|
|
|
/* We need to define this to get kill on GNU systems */
|
|
|
|
#ifdef __linux__
|
2007-09-04 20:48:40 +08:00
|
|
|
#define _BSD_SOURCE
|
2006-12-15 19:23:32 +08:00
|
|
|
#define _POSIX_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2007-05-13 20:28:54 +08:00
|
|
|
#include <sys/file.h>
|
2006-12-15 19:23:32 +08:00
|
|
|
#include <sys/types.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-05-11 00:08:49 +08:00
|
|
|
#include "config.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "client.h"
|
|
|
|
#include "dhcpcd.h"
|
|
|
|
#include "dhcp.h"
|
|
|
|
#include "interface.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
static int doversion = 0;
|
|
|
|
static int dohelp = 0;
|
|
|
|
#define EXTRA_OPTS
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"arp", no_argument, NULL, 'a'},
|
|
|
|
{"script", required_argument, NULL, 'c'},
|
|
|
|
{"debug", no_argument, NULL, 'd'},
|
|
|
|
{"hostname", optional_argument, NULL, 'h'},
|
|
|
|
{"classid", optional_argument, NULL, 'i'},
|
|
|
|
{"release", no_argument, NULL, 'k'},
|
|
|
|
{"leasetime", required_argument, NULL, 'l'},
|
|
|
|
{"metric", required_argument, NULL, 'm'},
|
|
|
|
{"renew", no_argument, NULL, 'n'},
|
|
|
|
{"persistent", no_argument, NULL, 'p'},
|
|
|
|
{"inform", optional_argument, NULL, 's'},
|
|
|
|
{"request", optional_argument, NULL, 'r'},
|
|
|
|
{"timeout", required_argument, NULL, 't'},
|
|
|
|
{"userclass", required_argument, NULL, 'u'},
|
|
|
|
{"exit", no_argument, NULL, 'x'},
|
|
|
|
{"lastlease", no_argument, NULL, 'E'},
|
|
|
|
{"fqdn", required_argument, NULL, 'F'},
|
|
|
|
{"nogateway", no_argument, NULL, 'G'},
|
|
|
|
{"sethostname", no_argument, NULL, 'H'},
|
|
|
|
{"clientid", optional_argument, NULL, 'I'},
|
|
|
|
{"noipv4ll", no_argument, NULL, 'L'},
|
|
|
|
{"nomtu", no_argument, NULL, 'M'},
|
|
|
|
{"nontp", no_argument, NULL, 'N'},
|
|
|
|
{"nodns", no_argument, NULL, 'R'},
|
|
|
|
{"test", no_argument, NULL, 'T'},
|
|
|
|
{"nonis", no_argument, NULL, 'Y'},
|
|
|
|
{"help", no_argument, &dohelp, 1},
|
|
|
|
{"version", no_argument, &doversion, 1},
|
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
{"daemonised", no_argument, NULL, 'f'},
|
|
|
|
{"skiproutes", required_argument, NULL, 'g'},
|
|
|
|
#endif
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-11-28 04:23:22 +08:00
|
|
|
#define STRINGINT(_string, _int) { \
|
2007-04-11 21:18:33 +08:00
|
|
|
char *_tmp; \
|
|
|
|
long _number = strtol (_string, &_tmp, 0); \
|
|
|
|
errno = 0; \
|
|
|
|
if ((errno != 0 && _number == 0) || _string == _tmp || \
|
|
|
|
(errno == ERANGE && (_number == LONG_MAX || _number == LONG_MIN))) \
|
|
|
|
{ \
|
|
|
|
logger (LOG_ERR, "`%s' out of range", _string);; \
|
|
|
|
exit (EXIT_FAILURE); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
_int = (int) _number; \
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
2007-09-04 20:48:40 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
char dhcpcd[PATH_MAX];
|
|
|
|
char **dhcpcd_argv = NULL;
|
|
|
|
int dhcpcd_argc = 0;
|
2007-10-04 17:57:50 +08:00
|
|
|
char *dhcpcd_skiproutes = NULL;
|
2007-10-11 21:26:16 +08:00
|
|
|
#undef EXTRA_OPTS
|
|
|
|
#define EXTRA_OPTS "fg:"
|
2007-09-04 20:48:40 +08:00
|
|
|
#endif
|
|
|
|
|
2007-08-10 00:25:20 +08:00
|
|
|
static pid_t read_pid (const char *pidfile)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2007-04-11 21:18:33 +08:00
|
|
|
FILE *fp;
|
2007-08-10 00:25:20 +08:00
|
|
|
pid_t pid = 0;
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2007-04-11 21:18:33 +08:00
|
|
|
if ((fp = fopen (pidfile, "r")) == NULL) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2007-04-11 21:18:33 +08:00
|
|
|
fscanf (fp, "%d", &pid);
|
|
|
|
fclose (fp);
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2007-04-11 21:18:33 +08:00
|
|
|
return pid;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usage ()
|
|
|
|
{
|
2007-07-12 20:52:27 +08:00
|
|
|
printf ("usage: "PACKAGE" [-adknpEGHMNRTY] [-c script] [-h hostame] [-i classID]\n"
|
2007-10-22 03:15:23 +08:00
|
|
|
" [-l leasetime] [-m metric] [-r ipaddress] [-s ipaddress]\n"
|
|
|
|
" [-t timeout] [-u userclass] [-F none | ptr | both]\n"
|
|
|
|
" [-I clientID] <interface>\n");
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2007-10-11 21:26:16 +08:00
|
|
|
options_t *options;
|
2007-04-11 21:18:33 +08:00
|
|
|
int userclasses = 0;
|
2007-05-14 21:34:36 +08:00
|
|
|
int opt;
|
2007-04-11 21:18:33 +08:00
|
|
|
int option_index = 0;
|
2007-10-11 21:26:16 +08:00
|
|
|
char *prefix;
|
2007-04-11 21:18:33 +08:00
|
|
|
pid_t pid;
|
|
|
|
int debug = 0;
|
2007-04-27 00:16:56 +08:00
|
|
|
int i;
|
2007-07-12 20:52:27 +08:00
|
|
|
int pidfd = -1;
|
2007-08-03 20:24:17 +08:00
|
|
|
int sig = 0;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-04-27 00:16:56 +08:00
|
|
|
/* Close any un-needed fd's */
|
|
|
|
for (i = getdtablesize() - 1; i >= 3; --i)
|
|
|
|
close (i);
|
2007-04-11 21:18:33 +08:00
|
|
|
|
|
|
|
openlog (PACKAGE, LOG_PID, LOG_LOCAL0);
|
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
options = xmalloc (sizeof (options_t));
|
|
|
|
memset (options, 0, sizeof (options_t));
|
|
|
|
options->script = (char *) DEFAULT_SCRIPT;
|
|
|
|
snprintf (options->classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION);
|
|
|
|
options->classid_len = strlen (options->classid);
|
|
|
|
|
|
|
|
options->doarp = true;
|
|
|
|
options->dodns = true;
|
|
|
|
options->domtu = true;
|
|
|
|
options->donis = true;
|
|
|
|
options->dontp = true;
|
|
|
|
options->dogateway = true;
|
|
|
|
options->daemonise = true;
|
|
|
|
options->doinform = false;
|
|
|
|
options->doipv4ll = true;
|
|
|
|
options->timeout = DEFAULT_TIMEOUT;
|
|
|
|
|
|
|
|
gethostname (options->hostname, sizeof (options->hostname));
|
|
|
|
if (strcmp (options->hostname, "(none)") == 0 ||
|
|
|
|
strcmp (options->hostname, "localhost") == 0)
|
|
|
|
memset (options->hostname, 0, sizeof (options->hostname));
|
2007-05-15 21:34:17 +08:00
|
|
|
|
|
|
|
/* Don't set any optional arguments here so we retain POSIX
|
|
|
|
* compatibility with getopt */
|
2007-10-11 21:26:16 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, EXTRA_OPTS
|
|
|
|
"c:dh:i:kl:m:npr:s:t:u:xAEF:GHI:LMNRTY",
|
2007-05-14 21:34:36 +08:00
|
|
|
longopts, &option_index)) != -1)
|
2007-05-11 18:51:32 +08:00
|
|
|
{
|
2007-05-14 21:34:36 +08:00
|
|
|
switch (opt) {
|
2007-04-11 21:18:33 +08:00
|
|
|
case 0:
|
|
|
|
if (longopts[option_index].flag)
|
|
|
|
break;
|
|
|
|
logger (LOG_ERR, "option `%s' should set a flag",
|
|
|
|
longopts[option_index].name);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
case 'c':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->script = optarg;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debug++;
|
|
|
|
switch (debug) {
|
|
|
|
case 1:
|
|
|
|
setloglevel (LOG_DEBUG);
|
|
|
|
break;
|
|
|
|
case 2:
|
2007-10-11 21:26:16 +08:00
|
|
|
options->daemonise = false;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-04 20:48:40 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
case 'f':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->daemonised = true;
|
2007-09-04 20:48:40 +08:00
|
|
|
close_fds ();
|
|
|
|
break;
|
2007-10-04 17:57:50 +08:00
|
|
|
case 'g':
|
|
|
|
dhcpcd_skiproutes = xstrdup (optarg);
|
|
|
|
break;
|
2007-09-04 20:48:40 +08:00
|
|
|
#endif
|
2007-04-11 21:18:33 +08:00
|
|
|
case 'h':
|
2007-05-15 21:34:17 +08:00
|
|
|
if (! optarg)
|
2007-10-11 21:26:16 +08:00
|
|
|
memset (options->hostname, 0, sizeof (options->hostname));
|
2007-05-15 21:34:17 +08:00
|
|
|
else if (strlen (optarg) > MAXHOSTNAMELEN) {
|
2007-05-12 05:31:14 +08:00
|
|
|
logger (LOG_ERR, "`%s' too long for HostName string, max is %d",
|
|
|
|
optarg, MAXHOSTNAMELEN);
|
2007-04-11 21:18:33 +08:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
} else
|
2007-10-11 21:26:16 +08:00
|
|
|
strlcpy (options->hostname, optarg, sizeof (options->hostname));
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'i':
|
2007-05-15 21:34:17 +08:00
|
|
|
if (! optarg) {
|
2007-10-11 21:26:16 +08:00
|
|
|
memset (options->classid, 0, sizeof (options->classid));
|
|
|
|
options->classid_len = 0;
|
2007-05-15 21:34:17 +08:00
|
|
|
} else if (strlen (optarg) > CLASS_ID_MAX_LEN) {
|
2007-04-11 21:18:33 +08:00
|
|
|
logger (LOG_ERR, "`%s' too long for ClassID string, max is %d",
|
|
|
|
optarg, CLASS_ID_MAX_LEN);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
} else
|
2007-10-11 21:26:16 +08:00
|
|
|
options->classid_len = strlcpy (options->classid, optarg,
|
|
|
|
sizeof (options->classid));
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'k':
|
2007-08-03 20:24:17 +08:00
|
|
|
sig = SIGHUP;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'l':
|
2007-10-11 21:26:16 +08:00
|
|
|
STRINGINT (optarg, options->leasetime);
|
|
|
|
if (options->leasetime <= 0) {
|
2007-04-11 21:18:33 +08:00
|
|
|
logger (LOG_ERR, "leasetime must be a positive value");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
2007-10-11 21:26:16 +08:00
|
|
|
STRINGINT (optarg, options->metric);
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
2007-08-03 20:24:17 +08:00
|
|
|
sig = SIGALRM;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'p':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->persistent = true;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 's':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->doinform = true;
|
|
|
|
options->doarp = false;
|
2007-05-15 21:34:17 +08:00
|
|
|
if (! optarg || strlen (optarg) == 0) {
|
2007-10-11 21:26:16 +08:00
|
|
|
options->request_address.s_addr = 0;
|
2007-05-15 21:34:17 +08:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
char *slash = strchr (optarg, '/');
|
|
|
|
if (slash) {
|
|
|
|
int cidr;
|
|
|
|
/* nullify the slash, so the -r option can read the
|
|
|
|
* address */
|
|
|
|
*slash++ = '\0';
|
2007-09-04 20:48:40 +08:00
|
|
|
if (sscanf (slash, "%d", &cidr) != 1 ||
|
2007-10-11 21:26:16 +08:00
|
|
|
inet_cidrtoaddr (cidr, &options->request_netmask) != 0) {
|
2007-05-15 21:34:17 +08:00
|
|
|
logger (LOG_ERR, "`%s' is not a valid CIDR", slash);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
}
|
|
|
|
case 'r':
|
2007-10-11 21:26:16 +08:00
|
|
|
if (! options->doinform)
|
|
|
|
options->dorequest = true;
|
2007-05-15 21:34:17 +08:00
|
|
|
if (strlen (optarg) > 0 &&
|
2007-10-11 21:26:16 +08:00
|
|
|
! inet_aton (optarg, &options->request_address))
|
2007-05-15 21:34:17 +08:00
|
|
|
{
|
2007-04-11 21:18:33 +08:00
|
|
|
logger (LOG_ERR, "`%s' is not a valid IP address", optarg);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 't':
|
2007-10-11 21:26:16 +08:00
|
|
|
STRINGINT (optarg, options->timeout);
|
|
|
|
if (options->timeout < 0) {
|
2007-04-11 21:18:33 +08:00
|
|
|
logger (LOG_ERR, "timeout must be a positive value");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
for (i = 0; i < userclasses; i++)
|
2007-10-11 21:26:16 +08:00
|
|
|
offset += (int) options->userclass[offset] + 1;
|
2007-04-11 21:18:33 +08:00
|
|
|
if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) {
|
|
|
|
logger (LOG_ERR, "userclass overrun, max is %d",
|
|
|
|
USERCLASS_MAX_LEN);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
userclasses++;
|
2007-10-11 21:26:16 +08:00
|
|
|
memcpy (options->userclass + offset + 1 , optarg, strlen (optarg));
|
|
|
|
options->userclass[offset] = strlen (optarg);
|
|
|
|
options->userclass_len += (strlen (optarg)) + 1;
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
|
|
|
break;
|
2007-08-03 18:33:07 +08:00
|
|
|
case 'x':
|
2007-08-03 20:24:17 +08:00
|
|
|
sig = SIGTERM;
|
2007-08-03 18:33:07 +08:00
|
|
|
break;
|
2007-07-18 19:26:59 +08:00
|
|
|
case 'A':
|
|
|
|
#ifndef ENABLE_ARP
|
|
|
|
logger (LOG_ERR, "arp support not compiled into dhcpcd");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
#endif
|
2007-10-11 21:26:16 +08:00
|
|
|
options->doarp = false;
|
2007-07-18 19:26:59 +08:00
|
|
|
break;
|
2007-05-10 17:39:26 +08:00
|
|
|
case 'E':
|
2007-07-12 20:52:27 +08:00
|
|
|
#ifndef ENABLE_INFO
|
|
|
|
logger (LOG_ERR, "info support not compiled into dhcpcd");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
#endif
|
2007-10-11 21:26:16 +08:00
|
|
|
options->dolastlease = true;
|
2007-05-10 17:39:26 +08:00
|
|
|
break;
|
2007-04-11 21:18:33 +08:00
|
|
|
case 'F':
|
2007-05-15 06:12:01 +08:00
|
|
|
if (strncmp (optarg, "none", strlen (optarg)) == 0)
|
2007-10-11 21:26:16 +08:00
|
|
|
options->fqdn = FQDN_NONE;
|
2007-05-15 06:12:01 +08:00
|
|
|
else if (strncmp (optarg, "ptr", strlen (optarg)) == 0)
|
2007-10-11 21:26:16 +08:00
|
|
|
options->fqdn = FQDN_PTR;
|
2007-05-15 06:12:01 +08:00
|
|
|
else if (strncmp (optarg, "both", strlen (optarg)) == 0)
|
2007-10-11 21:26:16 +08:00
|
|
|
options->fqdn = FQDN_BOTH;
|
2007-05-15 06:12:01 +08:00
|
|
|
else {
|
|
|
|
logger (LOG_ERR, "invalid value `%s' for FQDN", optarg);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'G':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->dogateway = false;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'H':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->dohostname++;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'I':
|
2007-05-15 21:34:17 +08:00
|
|
|
if (optarg) {
|
|
|
|
if (strlen (optarg) > CLIENT_ID_MAX_LEN) {
|
|
|
|
logger (LOG_ERR, "`%s' is too long for ClientID, max is %d",
|
|
|
|
optarg, CLIENT_ID_MAX_LEN);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-10-11 21:26:16 +08:00
|
|
|
options->clientid_len = strlcpy (options->clientid, optarg,
|
|
|
|
sizeof (options->clientid));
|
2007-05-15 21:34:17 +08:00
|
|
|
/* empty string disabled duid */
|
2007-10-11 21:26:16 +08:00
|
|
|
if (options->clientid_len == 0)
|
|
|
|
options->clientid_len = -1;
|
2007-05-15 21:34:17 +08:00
|
|
|
} else {
|
2007-10-11 21:26:16 +08:00
|
|
|
memset (options->clientid, 0, sizeof (options->clientid));
|
|
|
|
options->clientid_len = -1;
|
2007-05-15 21:34:17 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
2007-07-18 19:26:59 +08:00
|
|
|
case 'L':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->doipv4ll = false;
|
2007-07-18 19:26:59 +08:00
|
|
|
break;
|
2007-04-11 21:18:33 +08:00
|
|
|
case 'M':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->domtu = false;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'N':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->dontp = false;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'R':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->dodns = false;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
2007-07-12 20:52:27 +08:00
|
|
|
case 'T':
|
|
|
|
#ifndef ENABLE_INFO
|
|
|
|
logger (LOG_ERR, "info support not compiled into dhcpcd");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
#endif
|
2007-10-11 21:26:16 +08:00
|
|
|
options->test = true;
|
|
|
|
options->persistent = true;
|
2007-07-12 20:52:27 +08:00
|
|
|
break;
|
2007-04-11 21:18:33 +08:00
|
|
|
case 'Y':
|
2007-10-11 21:26:16 +08:00
|
|
|
options->donis = false;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
usage ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-05-11 18:51:32 +08:00
|
|
|
}
|
2007-10-22 21:06:18 +08:00
|
|
|
if (doversion) {
|
2007-04-11 21:18:33 +08:00
|
|
|
printf (""PACKAGE" "VERSION"\n");
|
2007-10-22 21:06:18 +08:00
|
|
|
printf ("Compile time options:"
|
|
|
|
#ifdef ENABLE_ARP
|
|
|
|
" ARP"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_DUID
|
|
|
|
" DUID"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_INFO
|
|
|
|
" INFO"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_INFO_COMPAT
|
|
|
|
" INFO_COMPAT"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_IPV4LL
|
|
|
|
" IPV4LL"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NIS
|
|
|
|
" NIS"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_NTP
|
|
|
|
" NTP"
|
|
|
|
#endif
|
2007-11-08 18:37:52 +08:00
|
|
|
#ifdef ENABLE_ORC
|
|
|
|
" ORC"
|
|
|
|
#elif ENABLE_RC
|
|
|
|
" RC"
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_RESOLVCONF
|
|
|
|
" RESOLVCONF"
|
|
|
|
#endif
|
2007-10-22 21:06:18 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
" THERE_IS_NO_FORK"
|
|
|
|
#endif
|
|
|
|
"\n");
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
|
|
|
if (dohelp)
|
|
|
|
usage ();
|
|
|
|
|
2007-09-04 20:48:40 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
dhcpcd_argv = argv;
|
|
|
|
dhcpcd_argc = argc;
|
|
|
|
|
|
|
|
/* We need the full path to the dhcpcd */
|
|
|
|
if (*argv[0] == '/')
|
|
|
|
strlcpy (dhcpcd, argv[0], sizeof (dhcpcd));
|
|
|
|
else {
|
|
|
|
char pwd[PATH_MAX];
|
|
|
|
if (! getcwd (pwd, PATH_MAX)) {
|
|
|
|
logger (LOG_ERR, "getcwd: %s", strerror (errno));
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
snprintf (dhcpcd, sizeof (dhcpcd), "%s/%s", pwd, argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-04-11 21:18:33 +08:00
|
|
|
if (optind < argc) {
|
|
|
|
if (strlen (argv[optind]) > IF_NAMESIZE) {
|
|
|
|
logger (LOG_ERR, "`%s' is too long for an interface name (max=%d)",
|
|
|
|
argv[optind], IF_NAMESIZE);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-10-11 21:26:16 +08:00
|
|
|
strlcpy (options->interface, argv[optind],
|
|
|
|
sizeof (options->interface));
|
2007-04-11 21:18:33 +08:00
|
|
|
} else {
|
|
|
|
/* If only version was requested then exit now */
|
|
|
|
if (doversion || dohelp)
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
|
|
|
|
logger (LOG_ERR, "no interface specified");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
if (strchr (options->hostname, '.')) {
|
|
|
|
if (options->fqdn == FQDN_DISABLE)
|
|
|
|
options->fqdn = FQDN_BOTH;
|
2007-04-27 00:16:56 +08:00
|
|
|
} else
|
2007-10-11 21:26:16 +08:00
|
|
|
options->fqdn = FQDN_DISABLE;
|
2007-05-12 03:55:00 +08:00
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
if (options->request_address.s_addr == 0 && options->doinform) {
|
|
|
|
if ((options->request_address.s_addr = get_address (options->interface)) != 0)
|
|
|
|
options->keep_address = true;
|
2007-05-12 03:55:00 +08:00
|
|
|
}
|
|
|
|
|
2007-10-22 03:15:23 +08:00
|
|
|
if (IN_LINKLOCAL (options->request_address.s_addr)) {
|
|
|
|
logger (LOG_ERR, "you are not allowed to request a link local address");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-04-11 21:18:33 +08:00
|
|
|
if (geteuid ()) {
|
|
|
|
logger (LOG_ERR, "you need to be root to run "PACKAGE);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-10-12 04:30:24 +08:00
|
|
|
prefix = xmalloc (sizeof (char) * (IF_NAMESIZE + 3));
|
2007-10-11 21:26:16 +08:00
|
|
|
snprintf (prefix, IF_NAMESIZE, "%s: ", options->interface);
|
2007-04-11 21:18:33 +08:00
|
|
|
setlogprefix (prefix);
|
2007-10-11 21:26:16 +08:00
|
|
|
snprintf (options->pidfile, sizeof (options->pidfile), PIDFILE,
|
|
|
|
options->interface);
|
|
|
|
free (prefix);
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-06-26 20:02:26 +08:00
|
|
|
chdir ("/");
|
2007-04-11 21:18:33 +08:00
|
|
|
umask (022);
|
2007-09-04 20:48:40 +08:00
|
|
|
|
2007-04-11 21:18:33 +08:00
|
|
|
if (mkdir (CONFIGDIR, S_IRUSR |S_IWUSR |S_IXUSR | S_IRGRP | S_IXGRP
|
|
|
|
| S_IROTH | S_IXOTH) && errno != EEXIST )
|
|
|
|
{
|
|
|
|
logger (LOG_ERR, "mkdir(\"%s\",0): %s\n", CONFIGDIR, strerror (errno));
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir (ETCDIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP
|
|
|
|
| S_IROTH | S_IXOTH) && errno != EEXIST )
|
|
|
|
{
|
|
|
|
logger (LOG_ERR, "mkdir(\"%s\",0): %s\n", ETCDIR, strerror (errno));
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
if (options->test) {
|
|
|
|
if (options->dorequest || options->doinform) {
|
2007-07-12 20:52:27 +08:00
|
|
|
logger (LOG_ERR, "cannot test with --inform or --request");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
if (options->dolastlease) {
|
2007-07-12 20:52:27 +08:00
|
|
|
logger (LOG_ERR, "cannot test with --lastlease");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-08-03 20:24:17 +08:00
|
|
|
if (sig != 0) {
|
2007-07-12 20:52:27 +08:00
|
|
|
logger (LOG_ERR, "cannot test with --release or --renew");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-05-13 20:28:54 +08:00
|
|
|
}
|
|
|
|
|
2007-09-04 20:48:40 +08:00
|
|
|
if (sig != 0) {
|
2007-07-12 20:52:27 +08:00
|
|
|
int killed = -1;
|
2007-10-11 21:26:16 +08:00
|
|
|
pid = read_pid (options->pidfile);
|
2007-07-12 20:52:27 +08:00
|
|
|
if (pid != 0)
|
2007-08-03 20:24:17 +08:00
|
|
|
logger (LOG_INFO, "sending signal %d to pid %d", sig, pid);
|
2007-07-12 20:52:27 +08:00
|
|
|
|
2007-08-03 20:24:17 +08:00
|
|
|
if (! pid || (killed = kill (pid, sig)))
|
|
|
|
logger (sig == SIGALRM ? LOG_INFO : LOG_ERR, ""PACKAGE" not running");
|
2007-07-12 20:52:27 +08:00
|
|
|
|
2007-08-03 20:24:17 +08:00
|
|
|
if (pid != 0 && (sig != SIGALRM || killed != 0))
|
2007-10-11 21:26:16 +08:00
|
|
|
unlink (options->pidfile);
|
2007-07-12 20:52:27 +08:00
|
|
|
|
|
|
|
if (killed == 0)
|
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
|
2007-08-03 20:24:17 +08:00
|
|
|
if (sig != SIGALRM)
|
2007-07-12 20:52:27 +08:00
|
|
|
exit (EXIT_FAILURE);
|
2007-05-13 20:28:54 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
if (! options->test && ! options->daemonised) {
|
|
|
|
if ((pid = read_pid (options->pidfile)) > 0 && kill (pid, 0) == 0) {
|
2007-07-12 20:52:27 +08:00
|
|
|
logger (LOG_ERR, ""PACKAGE" already running on pid %d (%s)",
|
2007-10-11 21:26:16 +08:00
|
|
|
pid, options->pidfile);
|
2007-07-12 20:52:27 +08:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
pidfd = open (options->pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0660);
|
2007-07-12 20:52:27 +08:00
|
|
|
if (pidfd == -1) {
|
2007-10-11 21:26:16 +08:00
|
|
|
logger (LOG_ERR, "open `%s': %s", options->pidfile, strerror (errno));
|
2007-07-12 20:52:27 +08:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock the file so that only one instance of dhcpcd runs on an interface */
|
|
|
|
if (flock (pidfd, LOCK_EX | LOCK_NB) == -1) {
|
2007-10-11 21:26:16 +08:00
|
|
|
logger (LOG_ERR, "flock `%s': %s", options->pidfile, strerror (errno));
|
2007-07-12 20:52:27 +08:00
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dhcpcd.sh should not interhit this fd */
|
2007-07-18 19:35:50 +08:00
|
|
|
if ((i = fcntl (pidfd, F_GETFD, 0)) == -1 ||
|
|
|
|
fcntl (pidfd, F_SETFD, i | FD_CLOEXEC) == -1)
|
2007-05-13 21:46:39 +08:00
|
|
|
logger (LOG_ERR, "fcntl: %s", strerror (errno));
|
|
|
|
|
2007-10-21 00:58:13 +08:00
|
|
|
writepid (pidfd, getpid ());
|
2007-07-12 20:52:27 +08:00
|
|
|
logger (LOG_INFO, PACKAGE " " VERSION " starting");
|
|
|
|
}
|
|
|
|
|
2007-07-18 19:26:59 +08:00
|
|
|
/* Seed random */
|
|
|
|
srandomdev ();
|
|
|
|
|
2007-09-04 20:48:40 +08:00
|
|
|
i = EXIT_FAILURE;
|
2007-10-11 21:26:16 +08:00
|
|
|
if (dhcp_run (options, &pidfd) == 0)
|
2007-09-04 20:48:40 +08:00
|
|
|
i = EXIT_SUCCESS;
|
|
|
|
|
2007-10-21 00:58:13 +08:00
|
|
|
/* If we didn't daemonise then we need to punt the pidfile now */
|
|
|
|
if (pidfd > -1) {
|
|
|
|
close (pidfd);
|
|
|
|
unlink (options->pidfile);
|
|
|
|
}
|
|
|
|
|
2007-10-11 21:26:16 +08:00
|
|
|
free (options);
|
|
|
|
|
2007-10-04 17:57:50 +08:00
|
|
|
#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
|
|
|
|
|
2007-09-04 20:48:40 +08:00
|
|
|
logger (LOG_INFO, "exiting");
|
|
|
|
|
|
|
|
exit (i);
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|