2006-11-28 04:23:22 +08:00
|
|
|
/*
|
|
|
|
* dhcpcd - DHCP client daemon -
|
2007-01-17 01:54:07 +08:00
|
|
|
* Copyright 2006-2007 Roy Marples <uberlord@gentoo.org>
|
2006-11-28 04:23:22 +08:00
|
|
|
*
|
|
|
|
* dhcpcd is an RFC2131 compliant DHCP client daemon.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
#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-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-04-12 21:12:25 +08:00
|
|
|
" [-l leasetime] [-m metric] [-s ipaddress] [-t timeout]\n"
|
2007-05-15 06:12:01 +08:00
|
|
|
" [-u userclass] [-F none | ptr | both] [-I clientID]\n");
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2007-04-11 21:18:33 +08:00
|
|
|
options_t options;
|
|
|
|
int doversion = 0;
|
|
|
|
int dohelp = 0;
|
|
|
|
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;
|
|
|
|
char prefix[IF_NAMESIZE + 3];
|
|
|
|
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
|
|
|
|
|
|
|
const struct option longopts[] = {
|
2007-05-12 03:55:00 +08:00
|
|
|
{"arp", no_argument, NULL, 'a'},
|
|
|
|
{"script", required_argument, NULL, 'c'},
|
|
|
|
{"debug", no_argument, NULL, 'd'},
|
2007-05-15 21:34:17 +08:00
|
|
|
{"hostname", optional_argument, NULL, 'h'},
|
|
|
|
{"classid", optional_argument, NULL, 'i'},
|
2007-05-12 03:55:00 +08:00
|
|
|
{"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'},
|
2007-05-15 21:34:17 +08:00
|
|
|
{"inform", optional_argument, NULL, 's'},
|
|
|
|
{"request", optional_argument, NULL, 'r'},
|
2007-05-12 03:55:00 +08:00
|
|
|
{"timeout", required_argument, NULL, 't'},
|
|
|
|
{"userclass", required_argument, NULL, 'u'},
|
2007-08-03 18:33:07 +08:00
|
|
|
{"exit", no_argument, NULL, 'x'},
|
2007-05-12 03:55:00 +08:00
|
|
|
{"lastlease", no_argument, NULL, 'E'},
|
2007-05-15 06:12:01 +08:00
|
|
|
{"fqdn", required_argument, NULL, 'F'},
|
2007-05-12 03:55:00 +08:00
|
|
|
{"nogateway", no_argument, NULL, 'G'},
|
|
|
|
{"sethostname", no_argument, NULL, 'H'},
|
2007-05-15 21:34:17 +08:00
|
|
|
{"clientid", optional_argument, NULL, 'I'},
|
2007-07-18 19:26:59 +08:00
|
|
|
{"noipv4ll", no_argument, NULL, 'L'},
|
2007-05-12 03:55:00 +08:00
|
|
|
{"nomtu", no_argument, NULL, 'M'},
|
|
|
|
{"nontp", no_argument, NULL, 'N'},
|
|
|
|
{"nodns", no_argument, NULL, 'R'},
|
2007-07-12 20:52:27 +08:00
|
|
|
{"test", no_argument, NULL, 'T'},
|
2007-05-10 17:39:26 +08:00
|
|
|
{"nonis", no_argument, NULL, 'Y'},
|
2007-05-12 03:55:00 +08:00
|
|
|
{"help", no_argument, &dohelp, 1},
|
|
|
|
{"version", no_argument, &doversion, 1},
|
2007-09-04 20:48:40 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
{"daemonised", no_argument, NULL, 'f'},
|
2007-10-04 17:57:50 +08:00
|
|
|
{"skiproutes", required_argument, NULL, 'g'},
|
2007-09-04 20:48:40 +08:00
|
|
|
#endif
|
2007-05-12 03:55:00 +08:00
|
|
|
{NULL, 0, NULL, 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);
|
|
|
|
|
|
|
|
memset (&options, 0, sizeof (options_t));
|
|
|
|
options.script = (char *) DEFAULT_SCRIPT;
|
2007-05-15 21:34:17 +08:00
|
|
|
snprintf (options.classid, CLASS_ID_MAX_LEN, "%s %s", PACKAGE, VERSION);
|
|
|
|
options.classid_len = strlen (options.classid);
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-07-18 19:26:59 +08:00
|
|
|
options.doarp = true;
|
2007-04-11 21:18:33 +08:00
|
|
|
options.dodns = true;
|
|
|
|
options.domtu = true;
|
|
|
|
options.donis = true;
|
|
|
|
options.dontp = true;
|
|
|
|
options.dogateway = true;
|
|
|
|
options.daemonise = true;
|
2007-05-12 03:55:00 +08:00
|
|
|
options.doinform = false;
|
2007-07-18 19:26:59 +08:00
|
|
|
options.doipv4ll = true;
|
2007-04-11 21:18:33 +08:00
|
|
|
options.timeout = DEFAULT_TIMEOUT;
|
|
|
|
|
2007-05-15 21:34:17 +08:00
|
|
|
gethostname (options.hostname, sizeof (options.hostname));
|
|
|
|
if (strcmp (options.hostname, "(none)") == 0 ||
|
|
|
|
strcmp (options.hostname, "localhost") == 0)
|
|
|
|
memset (options.hostname, 0, sizeof (options.hostname));
|
|
|
|
|
|
|
|
/* Don't set any optional arguments here so we retain POSIX
|
|
|
|
* compatibility with getopt */
|
2007-08-03 18:33:07 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, "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':
|
|
|
|
options.script = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debug++;
|
|
|
|
switch (debug) {
|
|
|
|
case 1:
|
|
|
|
setloglevel (LOG_DEBUG);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
options.daemonise = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2007-09-04 20:48:40 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
case 'f':
|
|
|
|
options.daemonised = true;
|
|
|
|
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)
|
|
|
|
memset (options.hostname, 0, sizeof (options.hostname));
|
|
|
|
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
|
|
|
|
strlcpy (options.hostname, optarg, sizeof (options.hostname));
|
|
|
|
break;
|
|
|
|
case 'i':
|
2007-05-15 21:34:17 +08:00
|
|
|
if (! optarg) {
|
|
|
|
memset (options.classid, 0, sizeof (options.classid));
|
|
|
|
options.classid_len = 0;
|
|
|
|
} 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-05-15 21:34:17 +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':
|
|
|
|
STRINGINT (optarg, options.leasetime);
|
|
|
|
if (options.leasetime <= 0) {
|
|
|
|
logger (LOG_ERR, "leasetime must be a positive value");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
STRINGINT (optarg, options.metric);
|
|
|
|
break;
|
|
|
|
case 'n':
|
2007-08-03 20:24:17 +08:00
|
|
|
sig = SIGALRM;
|
2007-04-11 21:18:33 +08:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
options.persistent = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
2007-05-15 21:34:17 +08:00
|
|
|
options.doinform = true;
|
2007-09-04 20:48:40 +08:00
|
|
|
options.doarp = false;
|
2007-05-15 21:34:17 +08:00
|
|
|
if (! optarg || strlen (optarg) == 0) {
|
|
|
|
options.request_address.s_addr = 0;
|
|
|
|
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 ||
|
|
|
|
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':
|
|
|
|
if (! options.doinform)
|
|
|
|
options.dorequest = true;
|
|
|
|
if (strlen (optarg) > 0 &&
|
|
|
|
! inet_aton (optarg, &options.request_address))
|
|
|
|
{
|
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':
|
|
|
|
STRINGINT (optarg, options.timeout);
|
|
|
|
if (options.timeout < 0) {
|
|
|
|
logger (LOG_ERR, "timeout must be a positive value");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
for (i = 0; i < userclasses; i++)
|
|
|
|
offset += (int) options.userclass[offset] + 1;
|
|
|
|
if (offset + 1 + strlen (optarg) > USERCLASS_MAX_LEN) {
|
|
|
|
logger (LOG_ERR, "userclass overrun, max is %d",
|
|
|
|
USERCLASS_MAX_LEN);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
userclasses++;
|
|
|
|
memcpy (options.userclass + offset + 1 , optarg, strlen (optarg));
|
|
|
|
options.userclass[offset] = strlen (optarg);
|
|
|
|
options.userclass_len += (strlen (optarg)) + 1;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
options.doarp = false;
|
|
|
|
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-05-10 17:39:26 +08:00
|
|
|
options.dolastlease = true;
|
|
|
|
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)
|
|
|
|
options.fqdn = FQDN_NONE;
|
|
|
|
else if (strncmp (optarg, "ptr", strlen (optarg)) == 0)
|
|
|
|
options.fqdn = FQDN_PTR;
|
|
|
|
else if (strncmp (optarg, "both", strlen (optarg)) == 0)
|
2007-04-11 21:18:33 +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':
|
|
|
|
options.dogateway = false;
|
|
|
|
break;
|
|
|
|
case 'H':
|
2007-05-28 20:15:27 +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);
|
|
|
|
}
|
|
|
|
options.clientid_len = strlcpy (options.clientid, optarg,
|
|
|
|
sizeof (options.clientid));
|
|
|
|
/* empty string disabled duid */
|
|
|
|
if (options.clientid_len == 0)
|
|
|
|
options.clientid_len = -1;
|
|
|
|
} else {
|
|
|
|
memset (options.clientid, 0, sizeof (options.clientid));
|
2007-05-11 18:51:32 +08:00
|
|
|
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':
|
|
|
|
options.doipv4ll = false;
|
|
|
|
break;
|
2007-04-11 21:18:33 +08:00
|
|
|
case 'M':
|
|
|
|
options.domtu = false;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
options.dontp = false;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
options.dodns = false;
|
|
|
|
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
|
|
|
|
options.test = true;
|
|
|
|
options.persistent = true;
|
|
|
|
break;
|
2007-04-11 21:18:33 +08:00
|
|
|
case 'Y':
|
|
|
|
options.donis = false;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
usage ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-05-11 18:51:32 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
if (doversion)
|
|
|
|
printf (""PACKAGE" "VERSION"\n");
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
strlcpy (options.interface, argv[optind],
|
|
|
|
sizeof (options.interface));
|
|
|
|
} 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-04-27 00:16:56 +08:00
|
|
|
if (strchr (options.hostname, '.')) {
|
|
|
|
if (options.fqdn == FQDN_DISABLE)
|
|
|
|
options.fqdn = FQDN_BOTH;
|
|
|
|
} else
|
|
|
|
options.fqdn = FQDN_DISABLE;
|
2007-05-12 03:55:00 +08:00
|
|
|
|
2007-05-15 21:34:17 +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-04-11 21:18:33 +08:00
|
|
|
if (geteuid ()) {
|
|
|
|
logger (LOG_ERR, "you need to be root to run "PACKAGE);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf (prefix, IF_NAMESIZE, "%s: ", options.interface);
|
|
|
|
setlogprefix (prefix);
|
|
|
|
snprintf (options.pidfile, sizeof (options.pidfile), PIDFILE,
|
|
|
|
options.interface);
|
|
|
|
|
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-07-12 20:52:27 +08:00
|
|
|
if (options.test) {
|
|
|
|
if (options.dorequest || options.doinform) {
|
|
|
|
logger (LOG_ERR, "cannot test with --inform or --request");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-07-12 20:52:27 +08:00
|
|
|
if (options.dolastlease) {
|
|
|
|
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;
|
|
|
|
pid = read_pid (options.pidfile);
|
|
|
|
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-07-12 20:52:27 +08:00
|
|
|
unlink (options.pidfile);
|
|
|
|
|
|
|
|
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-09-04 20:48:40 +08:00
|
|
|
if (! options.test && ! options.daemonised) {
|
2007-07-12 20:52:27 +08:00
|
|
|
if ((pid = read_pid (options.pidfile)) > 0 && kill (pid, 0) == 0) {
|
|
|
|
logger (LOG_ERR, ""PACKAGE" already running on pid %d (%s)",
|
|
|
|
pid, options.pidfile);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
pidfd = open (options.pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0660);
|
|
|
|
if (pidfd == -1) {
|
|
|
|
logger (LOG_ERR, "open `%s': %s", options.pidfile, strerror (errno));
|
|
|
|
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) {
|
|
|
|
logger (LOG_ERR, "flock `%s': %s", options.pidfile, strerror (errno));
|
|
|
|
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-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;
|
|
|
|
if (dhcp_run (&options, &pidfd) == 0)
|
|
|
|
i = EXIT_SUCCESS;
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
if (pidfd > -1)
|
|
|
|
close (pidfd);
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-09-04 20:48:40 +08:00
|
|
|
exit (i);
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|