Browse Source

move kill_dhcp_client to dhcp.c, add DHCP_HOSTNAME handler

r15238
master
Joshua Kwan 18 years ago
parent
commit
c24071499b
  1. 74
      dhcp.c
  2. 34
      netcfg-common.c
  3. 2
      netcfg.c
  4. 2
      netcfg.h

74
dhcp.c

@ -5,6 +5,7 @@
*/
#include "netcfg.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <debian-installer.h>
@ -235,7 +236,8 @@ int ask_dhcp_retry (struct debconfclient *client)
/* Here comes another Satan machine. */
int netcfg_activate_dhcp (struct debconfclient *client)
{
enum { START, POLL, DHCP_HOSTNAME, HOSTNAME, STATIC, END } state = START;
char* dhostname = NULL;
enum { START, ASK_RETRY, POLL, DHCP_HOSTNAME, HOSTNAME, STATIC, END } state = START;
loop_setup();
@ -244,30 +246,44 @@ int netcfg_activate_dhcp (struct debconfclient *client)
switch (state)
{
case START:
if (start_dhcp_client(client, NULL))
if (start_dhcp_client(client, dhostname))
netcfg_die(client); /* change later */
else
state = POLL;
break;
case DHCP_HOSTNAME:
break;
case POLL:
if (poll_dhcp_client(client)) /* could not get a lease */
if (netcfg_get_hostname(client, "netcfg/dhcp_hostname", &dhostname, 0) == GO_BACK)
state = ASK_RETRY;
else
{
/* ooh, now it's a select */
switch (ask_dhcp_retry (client))
if (empty_str(dhostname))
{
case GO_BACK: exit(10); /* XXX */
case 0: state = POLL; break;
case 1: state = DHCP_HOSTNAME; break;
case 2: state = STATIC; break;
case 3: /* no net config at this time :( */
kill_dhcp_client();
exit(0);
free(dhostname);
dhostname = NULL;
}
kill_dhcp_client();
state = START;
}
break;
case ASK_RETRY:
/* ooh, now it's a select */
switch (ask_dhcp_retry (client))
{
case GO_BACK: exit(10); /* XXX */
case 0: state = POLL; break;
case 1: state = DHCP_HOSTNAME; break;
case 2: state = STATIC; break;
case 3: /* no net config at this time :( */
kill_dhcp_client();
state = END;
}
break;
case POLL:
if (poll_dhcp_client(client)) /* could not get a lease */
state = ASK_RETRY;
else
{
char buf[MAXHOSTNAMELEN + 1];
@ -284,13 +300,14 @@ int netcfg_activate_dhcp (struct debconfclient *client)
break;
case HOSTNAME:
if (netcfg_get_hostname (client, "netcfg/get_hostname", &hostname))
exit(10); /* go back */
if (netcfg_get_hostname (client, "netcfg/get_hostname", &hostname, 1))
exit(10); /* go back, going back to poll isn't intuitive */
else
state = END;
break;
case STATIC:
return 15;
break;
case END:
@ -298,3 +315,26 @@ int netcfg_activate_dhcp (struct debconfclient *client)
}
}
}
int kill_dhcp_client(void)
{
if (dhcp_pid != -1)
{
int s[] = { SIGTERM, SIGKILL, 0 }, *sigs = s;
while (*sigs)
{
kill(dhcp_pid, 0);
/* looks like it died */
if (errno == ESRCH)
return 1;
kill(dhcp_pid, *sigs);
sleep(2);
}
}
return 0;
}

34
netcfg-common.c

@ -364,7 +364,7 @@ int netcfg_get_interface(struct debconfclient *client, char **interface,
* Set the hostname.
* @return 0 on success, 30 on BACKUP being selected.
*/
int netcfg_get_hostname(struct debconfclient *client, char *template, char **hostname)
int netcfg_get_hostname(struct debconfclient *client, char *template, char **hostname, short hdset)
{
static const char *valid_chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.";
@ -373,7 +373,8 @@ int netcfg_get_hostname(struct debconfclient *client, char *template, char **hos
char *s;
do {
have_domain = 0;
if (hdset)
have_domain = 0;
debconf_input(client, "high", template);
ret = debconf_go(client);
@ -407,10 +408,10 @@ int netcfg_get_hostname(struct debconfclient *client, char *template, char **hos
{
if (s[1] == '\0') /* "somehostname." <- . should be ignored */
*s = '\0';
else /* assume we have a valid domain name here */
else if (hdset) /* assume we have a valid domain name here */
{
have_domain = 1;
debconf_set(client, "netcfg/get_domain", strdup(s + 1));
have_domain = 1;
}
}
return 0;
@ -480,35 +481,10 @@ void netcfg_write_common(const char *prebaseconfig, struct in_addr ipaddress,
}
int kill_dhcp_client(void)
{
if (dhcp_pid != -1)
{
int s[] = { SIGTERM, SIGKILL, 0 }, *sigs = s;
while (*sigs)
{
kill(dhcp_pid, 0);
/* looks like it died */
if (errno == ESRCH)
return 1;
kill(dhcp_pid, *sigs);
sleep(2);
}
}
return 0;
}
int deconfigure_network(void) {
char buf[256];
kill_dhcp_client();
/* deconfiguring network interfaces */
di_exec_shell_log("ifconfig lo down");
snprintf(buf, sizeof(buf), "ifconfig %s down", interface);

2
netcfg.c

@ -118,7 +118,7 @@ int main(void)
case 30:
state = BACKUP;
break;
default:
case 15:
state = GET_STATIC;
break;
}

2
netcfg.h

@ -67,7 +67,7 @@ extern void netcfg_die (struct debconfclient *client);
extern int netcfg_get_interface(struct debconfclient *client, char **interface, int *num_interfaces);
extern int netcfg_get_hostname(struct debconfclient *client, char *template, char **hostname);
extern int netcfg_get_hostname(struct debconfclient *client, char *template, char **hostname, short hdset);
extern int netcfg_get_nameservers (struct debconfclient *client, char **nameservers);

Loading…
Cancel
Save