2008-07-16 Dan Williams <dcbw@redhat.com>

Patch from Roy Marples <roy@marples.name>

	* configure.in
		- Add --with-dhcp-client option

	* src/dhcp-manager/Makefile.am
		- pass DHCP_CLIENT_PATH on compile line

	* src/dhcp-manager/nm-dhcp-manager.c
	  src/dhcp-manager/nm-dhcp-manager.h
		- Genericize for both dhcpcd and dhclient

	* src/dhcp-manager/nm-dhcp-dhclient.c
		- Move dhclient stuff out to it's own file from nm-dhcp-manager.c

	* src/dhcp-manager/nm-dhcp-dhcpcd.c
		- Implement support for dhcpcd too



git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@3823 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
This commit is contained in:
Dan Williams 2008-07-16 20:39:58 +00:00
parent 7d0e1d691b
commit 87e7e74e13
8 changed files with 535 additions and 329 deletions

View file

@ -1,3 +1,23 @@
2008-07-16 Dan Williams <dcbw@redhat.com>
Patch from Roy Marples <roy@marples.name>
* configure.in
- Add --with-dhcp-client option
* src/dhcp-manager/Makefile.am
- pass DHCP_CLIENT_PATH on compile line
* src/dhcp-manager/nm-dhcp-manager.c
src/dhcp-manager/nm-dhcp-manager.h
- Genericize for both dhcpcd and dhclient
* src/dhcp-manager/nm-dhcp-dhclient.c
- Move dhclient stuff out to it's own file from nm-dhcp-manager.c
* src/dhcp-manager/nm-dhcp-dhcpcd.c
- Implement support for dhcpcd too
2008-07-16 Tambet Ingo <tambet@gmail.com>
* system-settings/src/nm-system-config-interface.c

View file

@ -334,6 +334,50 @@ else
fi
AC_SUBST(PPPD_PLUGIN_DIR)
# DHCP client
AC_ARG_WITH([dhcp-client], AS_HELP_STRING([--with-dhcp-client=dhcpcd|dhclient], [path to the chosen dhcp client]))
if test "x$with_dhcp_client" = "x" || test x`basename "$with_dhcp_client"` = "xdhclient"; then
# We only work with ISC dhclient - the FreeBSD and OpenBSD derivatives don't have the same userland.
AC_MSG_CHECKING(for dhclient)
for client in "$with_dhcp_client" /sbin/dhclient /usr/pkg/sbin/dhclient /usr/local/sbin/dhclient; do
test -x "$client" || continue
case `"$client" --version 2>&1` in
"isc-dhclient-"*) DHCP_CLIENT_PATH="$client"; break;;
esac
done
if test "x$DHCP_CLIENT_PATH" = "x"; then
AC_MSG_RESULT(no)
if test "x$with_dhcp_client" != "x"; then
AC_MSG_ERROR([Could not find ISC dhclient])
fi
fi
AC_MSG_RESULT($DHCP_CLIENT_PATH)
fi
if test "x$with_dhcp_client" = "x" -a "x$DHCP_CLIENT_PATH" = "x" || test x`basename "$with_dhcp_client"` = "xdhcpcd"; then
# We fully work with upstream dhcpcd-4
AC_MSG_CHECKING([for dhcpcd])
for client in "$with_dhcp_client" /sbin/dhcpcd /usr/pkg/sbin/dhcpcd /usr/local/sbin/dhcpcd; do
test -x "$client" || continue
case `"$client" --version 2>/dev/null` in
"dhcpcd "[123]*);;
"dhcpcd "*) DHCP_CLIENT_PATH="$client"; break;;
esac
done
if test "x$DHCP_CLIENT_PATH" = "x"; then
AC_MSG_RESULT(no)
if test "x$with_dhcp_client" != "x"; then
AC_MSG_ERROR([Could not find dhcpcd-4 or newer])
fi
fi
AC_MSG_RESULT($DHCP_CLIENT_PATH)
fi
if test "x$DHCP_CLIENT_PATH" = "x"; then
AC_MSG_ERROR([Could not find a suitable DHCP client])
fi
AC_SUBST(DHCP_CLIENT_PATH)
DHCP_CLIENT=`basename "$DHCP_CLIENT_PATH"`
AC_SUBST(DHCP_CLIENT)
AC_ARG_ENABLE(more-warnings,
AS_HELP_STRING([--enable-more-warnings], [Maximum compiler warnings]), set_more_warnings="$enableval",set_more_warnings=yes)
AC_MSG_CHECKING(for more warnings, including -Werror)

View file

@ -3,7 +3,7 @@
# Please keep this file sorted alphabetically.
src/nm-netlink-monitor.c
src/NetworkManager.c
src/dhcp-manager/nm-dhcp-manager.c
src/dhcp-manager/nm-dhcp-dhclient.c
src/named-manager/nm-named-manager.c
system-settings/plugins/ifcfg-fedora/plugin.c
system-settings/src/main.c

View file

@ -10,7 +10,8 @@ noinst_LTLIBRARIES = libdhcp-manager.la
libdhcp_manager_la_SOURCES = \
nm-dhcp-manager.c \
nm-dhcp-manager.h
nm-dhcp-manager.h \
nm-dhcp-@DHCP_CLIENT@.c
libdhcp_manager_la_CPPFLAGS = \
$(DBUS_CFLAGS) \
@ -22,11 +23,14 @@ libdhcp_manager_la_CPPFLAGS = \
-DDATADIR=\"$(datadir)\" \
-DSYSCONFDIR=\"$(sysconfdir)\" \
-DLIBEXECDIR=\"$(libexecdir)\" \
-DLOCALSTATEDIR=\"$(localstatedir)\"
-DLOCALSTATEDIR=\"$(localstatedir)\" \
-DDHCP_CLIENT_PATH=\"$(DHCP_CLIENT_PATH)\"
libdhcp_manager_la_LIBADD = \
$(DBUS_LIBS) \
$(GTHREAD_LIBS) \
$(top_builddir)/marshallers/libmarshallers.la
EXTRA_DIST = \
nm-dhcp-dhclient.c \
nm-dhcp-dhcpcd.c

View file

@ -0,0 +1,279 @@
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
/* nm-dhcp-dhclient.c - dhclient specific hooks for NetworkManager
*
* Copyright (C) 2005 Dan Williams
*
* This program 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, 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include "nm-dhcp-manager.h"
#include "nm-utils.h"
#define NM_DHCP_MANAGER_PID_FILENAME "dhclient"
#define NM_DHCP_MANAGER_PID_FILE_EXT "pid"
#define NM_DHCP_MANAGER_LEASE_FILENAME "dhclient"
#define NM_DHCP_MANAGER_LEASE_FILE_EXT "lease"
#define ACTION_SCRIPT_PATH LIBEXECDIR "/nm-dhcp-client.action"
static char *
get_pidfile_for_iface (const char * iface)
{
return g_strdup_printf ("%s/%s-%s.%s",
NM_DHCP_MANAGER_RUN_DIR,
NM_DHCP_MANAGER_PID_FILENAME,
iface,
NM_DHCP_MANAGER_PID_FILE_EXT);
}
static char *
get_leasefile_for_iface (const char * iface)
{
return g_strdup_printf ("%s/%s-%s.%s",
NM_DHCP_MANAGER_RUN_DIR,
NM_DHCP_MANAGER_LEASE_FILENAME,
iface,
NM_DHCP_MANAGER_LEASE_FILE_EXT);
}
#define DHCP_CLIENT_ID_TAG "send dhcp-client-identifier"
#define DHCP_CLIENT_ID_FORMAT DHCP_CLIENT_ID_TAG " \"%s\"; # added by NetworkManager"
#define DHCP_HOSTNAME_TAG "send host-name"
#define DHCP_HOSTNAME_FORMAT DHCP_HOSTNAME_TAG " \"%s\"; # added by NetworkManager"
static gboolean
merge_dhclient_config (NMDHCPDevice *device,
NMSettingIP4Config *s_ip4,
const char *contents,
const char *orig,
GError **error)
{
GString *new_contents;
gboolean success = FALSE;
g_return_val_if_fail (device != NULL, FALSE);
g_return_val_if_fail (device->iface != NULL, FALSE);
new_contents = g_string_new (_("# Created by NetworkManager\n"));
/* Add existing options, if any, but ignore stuff NM will replace. */
if (contents) {
char **lines = NULL, **line;
g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig);
lines = g_strsplit_set (contents, "\n\r", 0);
for (line = lines; lines && *line; line++) {
gboolean ignore = FALSE;
if (!strlen (g_strstrip (*line)))
continue;
if ( s_ip4
&& s_ip4->dhcp_client_id
&& !strncmp (*line, DHCP_CLIENT_ID_TAG, strlen (DHCP_CLIENT_ID_TAG)))
ignore = TRUE;
if ( s_ip4
&& s_ip4->dhcp_client_id
&& !strncmp (*line, DHCP_HOSTNAME_TAG, strlen (DHCP_HOSTNAME_TAG)))
ignore = TRUE;
if (!ignore) {
g_string_append (new_contents, *line);
g_string_append_c (new_contents, '\n');
}
}
if (lines)
g_strfreev (lines);
} else
g_string_append_c (new_contents, '\n');
/* Add NM options from connection */
if (s_ip4 && s_ip4->dhcp_client_id)
g_string_append_printf (new_contents, DHCP_CLIENT_ID_FORMAT "\n", s_ip4->dhcp_client_id);
if (s_ip4 && s_ip4->dhcp_hostname)
g_string_append_printf (new_contents, DHCP_HOSTNAME_FORMAT "\n", s_ip4->dhcp_hostname);
if (g_file_set_contents (device->conf_file, new_contents->str, -1, error))
success = TRUE;
g_string_free (new_contents, TRUE);
return success;
}
/* NM provides interface-specific options; thus the same dhclient config
* file cannot be used since DHCP transactions can happen in parallel.
* Since some distros don't have default per-interface dhclient config files,
* read their single config file and merge that into a custom per-interface
* config file along with the NM options.
*/
static gboolean
create_dhclient_config (NMDHCPDevice *device, NMSettingIP4Config *s_ip4)
{
char *orig = NULL, *contents = NULL;
GError *error = NULL;
gboolean success = FALSE;
char *tmp;
g_return_val_if_fail (device != NULL, FALSE);
#if defined(TARGET_SUSE)
orig = g_strdup (SYSCONFDIR "/dhclient.conf");
#elif defined(TARGET_DEBIAN)
orig = g_strdup (SYSCONFDIR "/dhcp3/dhclient.conf");
#else
orig = g_strdup_printf (SYSCONFDIR "/dhclient-%s.conf", device->iface);
#endif
if (!orig) {
nm_warning ("%s: not enough memory for dhclient options.", device->iface);
return FALSE;
}
tmp = g_strdup_printf ("nm-dhclient-%s.conf", device->iface);
device->conf_file = g_build_filename ("/var", "run", tmp, NULL);
g_free (tmp);
if (!g_file_test (orig, G_FILE_TEST_EXISTS))
goto out;
if (!g_file_get_contents (orig, &contents, NULL, &error)) {
nm_warning ("%s: error reading dhclient configuration %s: %s",
device->iface, orig, error->message);
g_error_free (error);
goto out;
}
out:
error = NULL;
if (merge_dhclient_config (device, s_ip4, contents, orig, &error))
success = TRUE;
else {
nm_warning ("%s: error creating dhclient configuration: %s",
device->iface, error->message);
g_error_free (error);
}
g_free (contents);
g_free (orig);
return success;
}
static void
dhclient_child_setup (gpointer user_data G_GNUC_UNUSED)
{
/* We are in the child process at this point */
pid_t pid = getpid ();
setpgid (pid, pid);
}
gboolean
nm_dhcp_client_start (NMDHCPDevice *device, NMSettingIP4Config *s_ip4)
{
GPtrArray * dhclient_argv = NULL;
GPid pid;
GError * error = NULL;
gboolean success = FALSE;
char * pid_contents = NULL;
if (!g_file_test (DHCP_CLIENT_PATH, G_FILE_TEST_EXISTS)) {
nm_warning (DHCP_CLIENT_PATH " does not exist.");
goto out;
}
device->pid_file = get_pidfile_for_iface (device->iface);
if (!device->pid_file) {
nm_warning ("%s: not enough memory for dhclient options.", device->iface);
goto out;
}
device->lease_file = get_leasefile_for_iface (device->iface);
if (!device->lease_file) {
nm_warning ("%s: not enough memory for dhclient options.", device->iface);
goto out;
}
if (!create_dhclient_config (device, s_ip4))
goto out;
/* Kill any existing dhclient bound to this interface */
if (g_file_get_contents (device->pid_file, &pid_contents, NULL, NULL)) {
unsigned long int tmp = strtoul (pid_contents, NULL, 10);
if (!((tmp == ULONG_MAX) && (errno == ERANGE)))
nm_dhcp_client_stop (device->iface, (pid_t) tmp, TRUE);
remove (device->pid_file);
}
dhclient_argv = g_ptr_array_new ();
g_ptr_array_add (dhclient_argv, (gpointer) DHCP_CLIENT_PATH);
g_ptr_array_add (dhclient_argv, (gpointer) "-d");
g_ptr_array_add (dhclient_argv, (gpointer) "-sf"); /* Set script file */
g_ptr_array_add (dhclient_argv, (gpointer) ACTION_SCRIPT_PATH );
g_ptr_array_add (dhclient_argv, (gpointer) "-pf"); /* Set pid file */
g_ptr_array_add (dhclient_argv, (gpointer) device->pid_file);
g_ptr_array_add (dhclient_argv, (gpointer) "-lf"); /* Set lease file */
g_ptr_array_add (dhclient_argv, (gpointer) device->lease_file);
g_ptr_array_add (dhclient_argv, (gpointer) "-cf"); /* Set interface config file */
g_ptr_array_add (dhclient_argv, (gpointer) device->conf_file);
g_ptr_array_add (dhclient_argv, (gpointer) device->iface);
g_ptr_array_add (dhclient_argv, NULL);
if (!g_spawn_async (NULL, (char **) dhclient_argv->pdata, NULL, 0,
&dhclient_child_setup, NULL, &pid, &error)) {
nm_warning ("dhclient failed to start. error: '%s'", error->message);
g_error_free (error);
goto out;
}
nm_info ("dhclient started with pid %d", pid);
device->pid = pid;
success = TRUE;
out:
g_free (pid_contents);
g_ptr_array_free (dhclient_argv, TRUE);
return success;
}

View file

@ -0,0 +1,119 @@
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
/* nm-dhcp-dhcpcd.c - dhcpcd specific hooks for NetworkManager
*
* Copyright (C) 2008 Roy Marples
*
* This program 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, 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include "nm-dhcp-manager.h"
#include "nm-utils.h"
#define NM_DHCP_MANAGER_PID_FILENAME "dhcpcd"
#define NM_DHCP_MANAGER_PID_FILE_EXT "pid"
#define ACTION_SCRIPT_PATH LIBEXECDIR "/nm-dhcp-client.action"
static char *
get_pidfile_for_iface (const char * iface)
{
return g_strdup_printf ("/var/run/%s-%s.%s",
NM_DHCP_MANAGER_PID_FILENAME,
iface,
NM_DHCP_MANAGER_PID_FILE_EXT);
}
static void
dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED)
{
/* We are in the child process at this point */
pid_t pid = getpid ();
setpgid (pid, pid);
}
gboolean
nm_dhcp_client_start (NMDHCPDevice *device, NMSettingIP4Config *s_ip4)
{
GPtrArray * argv = NULL;
GPid pid;
GError * error = NULL;
gboolean success = FALSE;
char * pid_contents = NULL;
if (!g_file_test (DHCP_CLIENT_PATH, G_FILE_TEST_EXISTS)) {
nm_warning (DHCP_CLIENT_PATH " does not exist.");
goto out;
}
device->pid_file = get_pidfile_for_iface (device->iface);
if (!device->pid_file) {
nm_warning ("%s: not enough memory for dhcpcd options.", device->iface);
goto out;
}
/* Kill any existing dhcpcd bound to this interface */
if (g_file_get_contents (device->pid_file, &pid_contents, NULL, NULL)) {
unsigned long int tmp = strtoul (pid_contents, NULL, 10);
if (!((tmp == ULONG_MAX) && (errno == ERANGE)))
nm_dhcp_client_stop (device->iface, (pid_t) tmp, TRUE);
remove (device->pid_file);
}
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (gpointer) DHCP_CLIENT_PATH);
g_ptr_array_add (argv, (gpointer) "-X"); /* Don't fork */
g_ptr_array_add (argv, (gpointer) "-L"); /* Disable built-in IPv4LL since we use avahi-autoipd */
g_ptr_array_add (argv, (gpointer) "-c"); /* Set script file */
g_ptr_array_add (argv, (gpointer) ACTION_SCRIPT_PATH );
g_ptr_array_add (argv, (gpointer) device->iface);
g_ptr_array_add (argv, NULL);
if (!g_spawn_async (NULL, (char **) argv->pdata, NULL, 0,
&dhcpcd_child_setup, NULL, &pid, &error)) {
nm_warning ("dhcpcd failed to start. error: '%s'", error->message);
g_error_free (error);
goto out;
}
nm_info ("dhcpcd started with pid %d", pid);
device->pid = pid;
success = TRUE;
out:
g_free (pid_contents);
g_ptr_array_free (argv, TRUE);
return success;
}

View file

@ -45,38 +45,8 @@
#define NM_DHCP_CLIENT_DBUS_SERVICE "org.freedesktop.nm_dhcp_client"
#define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client"
#define NM_DHCP_MANAGER_RUN_DIR LOCALSTATEDIR "/run"
#define NM_DHCP_MANAGER_PID_FILENAME "dhclient"
#define NM_DHCP_MANAGER_PID_FILE_EXT "pid"
#define NM_DHCP_MANAGER_LEASE_FILENAME "dhclient"
#define NM_DHCP_MANAGER_LEASE_FILE_EXT "lease"
#define ACTION_SCRIPT_PATH LIBEXECDIR "/nm-dhcp-client.action"
#define NM_DHCP_TIMEOUT 45 /* DHCP timeout, in seconds */
static const char *dhclient_binary_paths[] =
{
"/sbin/dhclient",
"/usr/sbin/dhclient",
"/usr/local/sbin/dhclient",
NULL
};
typedef struct {
char * iface;
guchar state;
GPid dhclient_pid;
char * dhclient_conf;
guint timeout_id;
guint watch_id;
NMDHCPManager * manager;
GHashTable * options;
} NMDHCPDevice;
typedef struct {
NMDBusManager * dbus_mgr;
GHashTable * devices;
@ -91,7 +61,6 @@ G_DEFINE_TYPE (NMDHCPManager, nm_dhcp_manager, G_TYPE_OBJECT)
enum {
STATE_CHANGED,
TIMEOUT,
LAST_SIGNAL
};
@ -101,29 +70,6 @@ static NMDHCPManager *nm_dhcp_manager_new (void);
static void nm_dhcp_manager_cancel_transaction_real (NMDHCPDevice *device, gboolean blocking);
static void stop_dhclient (const char * iface, const pid_t pid, gboolean blocking);
static char *
get_pidfile_for_iface (const char * iface)
{
return g_strdup_printf ("%s/%s-%s.%s",
NM_DHCP_MANAGER_RUN_DIR,
NM_DHCP_MANAGER_PID_FILENAME,
iface,
NM_DHCP_MANAGER_PID_FILE_EXT);
}
static char *
get_leasefile_for_iface (const char * iface)
{
return g_strdup_printf ("%s/%s-%s.%s",
NM_DHCP_MANAGER_RUN_DIR,
NM_DHCP_MANAGER_LEASE_FILENAME,
iface,
NM_DHCP_MANAGER_LEASE_FILE_EXT);
}
NMDHCPManager *
nm_dhcp_manager_get (void)
{
@ -191,7 +137,8 @@ static gboolean state_is_bound (guint8 state)
if ((state == DHC_BOUND)
|| (state == DHC_RENEW)
|| (state == DHC_REBOOT)
|| (state == DHC_REBIND))
|| (state == DHC_REBIND)
|| (state == DHC_IPV4LL))
return TRUE;
return FALSE;
@ -227,11 +174,13 @@ nm_dhcp_device_destroy (NMDHCPDevice *device)
if (device->options)
g_hash_table_destroy (device->options);
if (device->dhclient_conf) {
ret = unlink (device->dhclient_conf);
g_free (device->dhclient_conf);
if (device->conf_file) {
ret = unlink (device->conf_file);
g_free (device->conf_file);
}
g_free (device->pid_file);
g_free (device->lease_file);
g_free (device->iface);
g_slice_free (NMDHCPDevice, device);
}
@ -246,6 +195,8 @@ state_to_string (guint32 state)
return "preinit";
case DHC_BOUND:
return "bound";
case DHC_IPV4LL:
return "bound (ipv4ll)";
case DHC_RENEW:
return "renew";
case DHC_REBOOT:
@ -283,6 +234,8 @@ string_to_state (const char *state)
return DHC_PREINIT;
else if (strcmp("BOUND", state) == 0)
return DHC_BOUND;
else if (strcmp("IPV4LL", state) == 0)
return DHC_IPV4LL;
else if (strcmp("RENEW", state) == 0)
return DHC_RENEW;
else if (strcmp("REBOOT", state) == 0)
@ -429,7 +382,7 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy,
pid_str = get_option (options, "pid");
if (pid_str == NULL) {
nm_warning ("DHCP event didn't have associated dhclient PID.");
nm_warning ("DHCP event didn't have associated PID.");
goto out;
}
@ -440,10 +393,10 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy,
}
pid = (pid_t) temp;
if (pid != device->dhclient_pid) {
if (pid != device->pid) {
nm_warning ("Received DHCP event from unexpected PID %u (expected %u)",
pid,
device->dhclient_pid);
device->pid);
goto out;
}
@ -534,7 +487,6 @@ nm_dhcp_device_new (NMDHCPManager *manager, const char *iface)
{
NMDHCPDevice *device;
GHashTable * hash = NM_DHCP_MANAGER_GET_PRIVATE (manager)->devices;
char *tmp;
device = g_slice_new0 (NMDHCPDevice);
if (!device) {
@ -552,10 +504,6 @@ nm_dhcp_device_new (NMDHCPManager *manager, const char *iface)
device->manager = manager;
tmp = g_strdup_printf ("nm-dhclient-%s.conf", iface);
device->dhclient_conf = g_build_filename ("/var", "run", tmp, NULL);
g_free (tmp);
nm_dhcp_manager_cancel_transaction_real (device, FALSE);
/* Do this after the transaction cancel since that clears options out */
@ -580,20 +528,20 @@ error:
/*
* dhclient_watch_cb
* dhcp_watch_cb
*
* Watch our child dhclient process and get notified of events from it.
*
*/
static void dhclient_watch_cb (GPid pid, gint status, gpointer user_data)
static void dhcp_watch_cb (GPid pid, gint status, gpointer user_data)
{
NMDHCPDevice *device = (NMDHCPDevice *)user_data;
if (!WIFEXITED (status)) {
device->state = DHC_ABEND;
nm_warning ("dhclient died abnormally");
nm_warning ("dhcp client died abnormally");
}
device->dhclient_pid = 0;
device->pid = 0;
nm_dhcp_device_watch_cleanup (device);
nm_dhcp_device_timeout_cleanup (device);
@ -601,235 +549,6 @@ static void dhclient_watch_cb (GPid pid, gint status, gpointer user_data)
g_signal_emit (G_OBJECT (device->manager), signals[STATE_CHANGED], 0, device->iface, device->state);
}
/*
* nm_vpn_service_child_setup
*
* Give child a different process group to ensure signal separation.
*
*/
static void
dhclient_child_setup (gpointer user_data G_GNUC_UNUSED)
{
/* We are in the child process at this point */
pid_t pid = getpid ();
setpgid (pid, pid);
}
#define DHCP_CLIENT_ID_TAG "send dhcp-client-identifier"
#define DHCP_CLIENT_ID_FORMAT DHCP_CLIENT_ID_TAG " \"%s\"; # added by NetworkManager"
#define DHCP_HOSTNAME_TAG "send host-name"
#define DHCP_HOSTNAME_FORMAT DHCP_HOSTNAME_TAG " \"%s\"; # added by NetworkManager"
static gboolean
merge_dhclient_config (NMDHCPDevice *device,
NMSettingIP4Config *s_ip4,
const char *contents,
const char *orig,
GError **error)
{
GString *new_contents;
gboolean success = FALSE;
g_return_val_if_fail (device != NULL, FALSE);
g_return_val_if_fail (device->iface != NULL, FALSE);
new_contents = g_string_new (_("# Created by NetworkManager\n"));
/* Add existing options, if any, but ignore stuff NM will replace. */
if (contents) {
char **lines = NULL, **line;
g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig);
lines = g_strsplit_set (contents, "\n\r", 0);
for (line = lines; lines && *line; line++) {
gboolean ignore = FALSE;
if (!strlen (g_strstrip (*line)))
continue;
if ( s_ip4
&& s_ip4->dhcp_client_id
&& !strncmp (*line, DHCP_CLIENT_ID_TAG, strlen (DHCP_CLIENT_ID_TAG)))
ignore = TRUE;
if ( s_ip4
&& s_ip4->dhcp_client_id
&& !strncmp (*line, DHCP_HOSTNAME_TAG, strlen (DHCP_HOSTNAME_TAG)))
ignore = TRUE;
if (!ignore) {
g_string_append (new_contents, *line);
g_string_append_c (new_contents, '\n');
}
}
if (lines)
g_strfreev (lines);
} else
g_string_append_c (new_contents, '\n');
/* Add NM options from connection */
if (s_ip4 && s_ip4->dhcp_client_id)
g_string_append_printf (new_contents, DHCP_CLIENT_ID_FORMAT "\n", s_ip4->dhcp_client_id);
if (s_ip4 && s_ip4->dhcp_hostname)
g_string_append_printf (new_contents, DHCP_HOSTNAME_FORMAT "\n", s_ip4->dhcp_hostname);
if (g_file_set_contents (device->dhclient_conf, new_contents->str, -1, error))
success = TRUE;
g_string_free (new_contents, TRUE);
return success;
}
/* NM provides interface-specific options; thus the same dhclient config
* file cannot be used since DHCP transactions can happen in parallel.
* Since some distros don't have default per-interface dhclient config files,
* read their single config file and merge that into a custom per-interface
* config file along with the NM options.
*/
static gboolean
create_dhclient_config (NMDHCPDevice *device, NMSettingIP4Config *s_ip4)
{
char *orig = NULL, *contents = NULL;
GError *error = NULL;
gboolean success = FALSE;
g_return_val_if_fail (device != NULL, FALSE);
#if defined(TARGET_SUSE)
orig = g_strdup (SYSCONFDIR "/dhclient.conf");
#elif defined(TARGET_DEBIAN)
orig = g_strdup (SYSCONFDIR "/dhcp3/dhclient.conf");
#else
orig = g_strdup_printf (SYSCONFDIR "/dhclient-%s.conf", device->iface);
#endif
if (!orig) {
nm_warning ("%s: not enough memory for dhclient options.", device->iface);
return FALSE;
}
if (!g_file_test (orig, G_FILE_TEST_EXISTS))
goto out;
if (!g_file_get_contents (orig, &contents, NULL, &error)) {
nm_warning ("%s: error reading dhclient configuration %s: %s",
device->iface, orig, error->message);
g_error_free (error);
goto out;
}
out:
error = NULL;
if (merge_dhclient_config (device, s_ip4, contents, orig, &error))
success = TRUE;
else {
nm_warning ("%s: error creating dhclient configuration: %s",
device->iface, error->message);
g_error_free (error);
}
g_free (contents);
g_free (orig);
return success;
}
static gboolean
dhclient_run (NMDHCPDevice *device, NMSettingIP4Config *s_ip4)
{
const char ** dhclient_binary = NULL;
GPtrArray * dhclient_argv = NULL;
GPid pid;
GError * error = NULL;
char * pidfile = NULL;
char * leasefile = NULL;
gboolean success = FALSE;
char * pid_contents = NULL;
/* Find dhclient */
dhclient_binary = dhclient_binary_paths;
while (*dhclient_binary != NULL) {
if (g_file_test (*dhclient_binary, G_FILE_TEST_EXISTS))
break;
dhclient_binary++;
}
if (!*dhclient_binary) {
nm_warning ("Could not find dhclient binary.");
goto out;
}
pidfile = get_pidfile_for_iface (device->iface);
if (!pidfile) {
nm_warning ("%s: not enough memory for dhclient options.", device->iface);
goto out;
}
leasefile = get_leasefile_for_iface (device->iface);
if (!leasefile) {
nm_warning ("%s: not enough memory for dhclient options.", device->iface);
goto out;
}
if (!create_dhclient_config (device, s_ip4))
goto out;
/* Kill any existing dhclient bound to this interface */
if (g_file_get_contents (pidfile, &pid_contents, NULL, NULL)) {
unsigned long int tmp = strtoul (pid_contents, NULL, 10);
if (!((tmp == ULONG_MAX) && (errno == ERANGE)))
stop_dhclient (device->iface, (pid_t) tmp, TRUE);
remove (pidfile);
}
dhclient_argv = g_ptr_array_new ();
g_ptr_array_add (dhclient_argv, (gpointer) (*dhclient_binary));
g_ptr_array_add (dhclient_argv, (gpointer) "-d");
g_ptr_array_add (dhclient_argv, (gpointer) "-sf"); /* Set script file */
g_ptr_array_add (dhclient_argv, (gpointer) ACTION_SCRIPT_PATH );
g_ptr_array_add (dhclient_argv, (gpointer) "-pf"); /* Set pid file */
g_ptr_array_add (dhclient_argv, (gpointer) pidfile);
g_ptr_array_add (dhclient_argv, (gpointer) "-lf"); /* Set lease file */
g_ptr_array_add (dhclient_argv, (gpointer) leasefile);
g_ptr_array_add (dhclient_argv, (gpointer) "-cf"); /* Set interface config file */
g_ptr_array_add (dhclient_argv, (gpointer) device->dhclient_conf);
g_ptr_array_add (dhclient_argv, (gpointer) device->iface);
g_ptr_array_add (dhclient_argv, NULL);
if (!g_spawn_async (NULL, (char **) dhclient_argv->pdata, NULL, 0,
&dhclient_child_setup, NULL, &pid, &error)) {
nm_warning ("dhclient failed to start. error: '%s'", error->message);
g_error_free (error);
goto out;
}
nm_info ("dhclient started with pid %d", pid);
device->dhclient_pid = pid;
device->watch_id = g_child_watch_add (pid,
(GChildWatchFunc) dhclient_watch_cb,
device);
success = TRUE;
out:
g_free (pid_contents);
g_free (leasefile);
g_free (pidfile);
g_ptr_array_free (dhclient_argv, TRUE);
return success;
}
gboolean
nm_dhcp_manager_begin_transaction (NMDHCPManager *manager,
const char *iface,
@ -863,15 +582,17 @@ nm_dhcp_manager_begin_transaction (NMDHCPManager *manager,
nm_dhcp_manager_handle_timeout,
device);
dhclient_run (device, s_ip4);
nm_dhcp_client_start (device, s_ip4);
device->watch_id = g_child_watch_add (device->pid,
(GChildWatchFunc) dhcp_watch_cb,
device);
return TRUE;
}
static void
stop_dhclient (const char * iface,
pid_t pid,
gboolean blocking)
void
nm_dhcp_client_stop (const char * iface,
pid_t pid,
gboolean blocking)
{
int i = 20; /* 4 seconds */
@ -896,7 +617,7 @@ stop_dhclient (const char * iface,
}
if (i <= 0) {
nm_warning ("%s: dhclient pid %d didn't exit, will kill it.", iface, pid);
nm_warning ("%s: dhcp client pid %d didn't exit, will kill it.", iface, pid);
kill (pid, SIGKILL);
}
}
@ -904,37 +625,35 @@ stop_dhclient (const char * iface,
static void
nm_dhcp_manager_cancel_transaction_real (NMDHCPDevice *device, gboolean blocking)
{
char * pidfile;
char * leasefile;
if (!device->dhclient_pid)
if (!device->pid)
return;
stop_dhclient (device->iface, device->dhclient_pid, blocking);
nm_dhcp_client_stop (device->iface, device->pid, blocking);
nm_info ("%s: canceled DHCP transaction, dhclient pid %d",
nm_info ("%s: canceled DHCP transaction, dhcp client pid %d",
device->iface,
device->dhclient_pid);
device->pid);
device->dhclient_pid = 0;
device->pid = 0;
device->state = DHC_END;
/* Clean up the pidfile if it got left around */
pidfile = get_pidfile_for_iface (device->iface);
if (pidfile) {
remove (pidfile);
g_free (pidfile);
if (device->pid_file) {
remove (device->pid_file);
g_free (device->pid_file);
}
/* Clean up the leasefile if it got left around */
leasefile = get_leasefile_for_iface (device->iface);
if (leasefile) {
remove (leasefile);
g_free (leasefile);
if (device->lease_file) {
remove (device->lease_file);
g_free (device->lease_file);
}
/* Clean up config file if it got left around */
remove (device->dhclient_conf);
if (device->conf_file) {
remove (device->conf_file);
g_free (device->conf_file);
}
nm_dhcp_device_watch_cleanup (device);
nm_dhcp_device_timeout_cleanup (device);
@ -962,7 +681,7 @@ nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager,
device = (NMDHCPDevice *) g_hash_table_lookup (priv->devices, iface);
if (!device || !device->dhclient_pid)
if (!device || !device->pid)
return;
nm_dhcp_manager_cancel_transaction_real (device, TRUE);
@ -998,7 +717,7 @@ nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager,
}
if (!state_is_bound (device->state)) {
nm_warning ("%s: dhclient didn't bind to a lease.", device->iface);
nm_warning ("%s: dhcp client didn't bind to a lease.", device->iface);
return NULL;
}

View file

@ -28,6 +28,8 @@
#include "nm-ip4-config.h"
#define NM_DHCP_MANAGER_RUN_DIR LOCALSTATEDIR "/run"
#define NM_TYPE_DHCP_MANAGER (nm_dhcp_manager_get_type ())
#define NM_DHCP_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_MANAGER, NMDHCPManager))
#define NM_DHCP_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_MANAGER, NMDHCPManagerClass))
@ -39,6 +41,7 @@ typedef enum {
DHC_NBI=0, /* no broadcast interfaces found */
DHC_PREINIT, /* configuration started */
DHC_BOUND, /* lease obtained */
DHC_IPV4LL, /* IPv4LL address obtained */
DHC_RENEW, /* lease renewed */
DHC_REBOOT, /* have valid lease, but now obtained a different one */
DHC_REBIND, /* new, different lease */
@ -66,6 +69,19 @@ typedef struct {
void (*timeout) (NMDHCPManager *manager, char *iface);
} NMDHCPManagerClass;
typedef struct {
char * iface;
guchar state;
GPid pid;
char * pid_file;
char * conf_file;
char * lease_file;
guint timeout_id;
guint watch_id;
NMDHCPManager * manager;
GHashTable * options;
} NMDHCPDevice;
GType nm_dhcp_manager_get_type (void);
NMDHCPManager *nm_dhcp_manager_get (void);
@ -80,4 +96,9 @@ NMDHCPState nm_dhcp_manager_get_state_for_device (NMDHCPManager *manager, con
gboolean nm_dhcp_manager_process_signal (NMDHCPManager *manager, DBusMessage *message);
gboolean nm_dhcp_client_start (NMDHCPDevice *device, NMSettingIP4Config *s_ip4);
void nm_dhcp_client_stop (const char * iface,
pid_t pid,
gboolean blocking);
#endif /* NM_DHCP_MANAGER_H */