1 /* $Id: main.c,v 1.15 2002-03-07 02:29:14 gerg Exp $
3 * dhcpcd - DHCP client daemon -
4 * Copyright (C) 1996 - 1997 Yoichi Hariguchi <yoichi@fore.com>
6 * dhcpcd is an RFC2131 and RFC1541 compliant DHCP client daemon.
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sys/types.h>
34 #include <arpa/inet.h>
37 #include "signal-handler.h"
38 #include "error-handler.h"
43 #define DEFAULT_IF "eth0"
45 char *CommandFile = NULL; /* invoked command file name when dhcpcd
46 * succeeds in getting an IP address
48 int BeRFC1541 = 0; /* default is InternetDraft mode */
49 char *Hostname = NULL; /* hostname in the DHCP msg for xmit */
50 int Persistent = 0; /* Keep trying until you get address */
51 int ArpCheck = 1; /* Check if address already used? */
58 char pidfile[128]; /* file name in which pid is stored */
59 static char VersionStr[] = "dhcpcd 0.70\n";
63 char Ifname[16]; /* global interface name */
71 char ifname[16]; /* interface name */
72 char *clientId = NULL; /* ptr to client identifier user specified */
73 int killFlag = 0; /* if 1: kill the running proc and exit */
76 DebugFlag = 0; /* default is NON debug mode */
77 srand((u_int)time(NULL));
79 umask(0); /* clear umask */
80 classIDsetup(NULL); /* setup default class identifier */
84 if ( **argv == '-' ) {
85 switch ( argv[0][1] ) {
87 if ( *++argv == NULL ) {
90 if ( (CommandFile = malloc(strlen(*argv)+1)) == NULL ) {
93 strcpy(CommandFile, *argv);
105 if ( *++argv == NULL ) {
108 Hostname = smalloc(strlen(*argv)+1);
109 strcpy(Hostname, *argv);
112 if ( *++argv == NULL ) {
115 classIDsetup(*argv); /* overwrite class identifier */
118 if ( *++argv == NULL ) {
124 killFlag = 1; /* kill running process and exit */
128 if ( *argv == NULL || **argv == '-' ) {
131 SuggestLeaseTime = atol(*argv);
134 BeRFC1541 = 1; /* Be RFC1541 compliant */
138 fputs(VersionStr, stderr);
142 case 'A': /* auto IP support if no dhcp lease is obtained */
147 case 'A': /* auto IP support option is ignored but logged */
148 syslog(LOG_WARNING, "-A (enable auto IP) option was specified but will be ignored.");
159 if ( getuid() != 0 && geteuid() != 0 ) {
160 errQuit("Must be root");
163 strncpy(ifname, *argv, sizeof(ifname));
165 strncpy(ifname, DEFAULT_IF, sizeof(ifname));
169 * Don't do this for now...
172 sprintf(pidfile, PIDFILE, ifname);
173 killCurProc(pidfile);
176 sprintf(pidfile, PIDFILE, ifname);
181 logOpen("dhcpcd", LOG_PID, LOG_LOCAL0);
182 ifReset(ifname); /* reset interface, 'Ifbuf' */
183 clientIDsetup(clientId, ifname);
194 fputs("Usage: dhcpcd [-c filename] [-dpa] [-i classIdentifier] [-I clientIdentifier] [-k] [-l leasetime] [-h hostname] [ifname]\n",
197 fputs("Usage: dhcpcd [-c filename] [-dpaA] [-i classIdentifier] [-I clientIdentifier] [-k] [-l leasetime] [-h hostname] [ifname]\n", stderr);
213 * Write out the pid into the file specified by pidfile.
214 * return code: 0 - everything worked like a dream
215 * 1 - there was a stale PID file
216 * -1 - something went wrong
217 * -2 - the program is already running
226 fd = open(pidfile, O_CREAT | O_EXCL | O_RDWR, S_IRWXU);
228 if (errno == EEXIST) {
229 /* See if the old program is still alive */
230 if ((rc = isOldPid()) < 0) {
232 } else if (rc == 1) {
238 fprintf(stderr, "DHCPCD: unexpected errno value"
245 if ((file = fopen(pidfile, "w")) == NULL) {
246 fprintf(stderr, "DHCPCD: open pid file errno=%d\n", errno);
249 rc = fprintf(file, "%d\n", pid);
253 fprintf(stderr, "DHCPCD: pif file errno=%d\n", errno);
262 * Check whether a pidfile contains the pid for a current active dhcpcd, or an
263 * old defunct one (which would hang around in the case of a -9)
264 * return code: 1 - the pidfile is NOT a currently active dhcpcd
265 * 0 - the pidfile is a currently active dhcpcd
271 char oldCmd[7] = {'\0'};
275 if ((file = fopen(pidfile, "r")) == NULL)
277 rc = fscanf(file, "%d", &pid);
283 sprintf(fileName, "/proc/%d/cmdline", pid);
284 if ((file = fopen(fileName, "r")) == NULL)
286 rc = fread(oldCmd, sizeof(oldCmd) - 1, 1, file);
293 if(strcmp("dhcpcd", oldCmd) == 0)