diff options
author | Dan Winship <danw@redhat.com> | 2015-01-12 15:50:22 -0500 |
---|---|---|
committer | Dan Winship <danw@redhat.com> | 2015-01-19 11:29:13 -0500 |
commit | fb792af7cb5dde72b05e2312c2ce7d92aa29f34d (patch) | |
tree | 28fa91046c8e23a7a28eafea44393a1530072e69 | |
parent | c22e3f327aac18c0b3f2ab76fc6b60fbb467d898 (diff) | |
download | NetworkManager-fb792af7cb5dde72b05e2312c2ce7d92aa29f34d.tar.gz |
core: add nm_utils_setpgid(), and use it
Add nm_utils_setpgid() as a g_spawn*() child setup function for
calling setpgid(), and use it where appropriate rather than
reimplementing it every time.
-rw-r--r-- | src/NetworkManagerUtils.c | 18 | ||||
-rw-r--r-- | src/NetworkManagerUtils.h | 2 | ||||
-rw-r--r-- | src/devices/nm-device.c | 13 | ||||
-rw-r--r-- | src/devices/team/nm-device-team.c | 13 | ||||
-rw-r--r-- | src/dhcp-manager/nm-dhcp-dhclient.c | 10 | ||||
-rw-r--r-- | src/dhcp-manager/nm-dhcp-dhcpcd.c | 10 | ||||
-rw-r--r-- | src/dns-manager/nm-dns-plugin.c | 12 | ||||
-rw-r--r-- | src/dnsmasq-manager/nm-dnsmasq-manager.c | 14 | ||||
-rw-r--r-- | src/ppp-manager/nm-ppp-manager.c | 12 | ||||
-rw-r--r-- | src/vpn-manager/nm-vpn-service.c | 10 |
10 files changed, 32 insertions, 82 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 82cfd46c6f..268abb84a8 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -2546,3 +2546,21 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value) return g_slist_reverse (list); } + +/** + * nm_utils_setpgid: + * @unused: unused + * + * This can be passed as a child setup function to the g_spawn*() family + * of functions, to ensure that the child is in its own process group + * (and thus, in some situations, will not be killed when NetworkManager + * is killed). + */ +void +nm_utils_setpgid (gpointer unused G_GNUC_UNUSED) +{ + pid_t pid; + + pid = getpid (); + setpgid (pid, pid); +} diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 40315c6efc..99543df314 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -210,4 +210,6 @@ GHashTable *nm_utils_connection_dict_to_hash (GVariant *dict); GSList *nm_utils_ip4_routes_from_gvalue (const GValue *value); GSList *nm_utils_ip6_routes_from_gvalue (const GValue *value); +void nm_utils_setpgid (gpointer unused); + #endif /* __NETWORKMANAGER_UTILS_H__ */ diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 566ab055cc..0ad774d29b 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -2716,17 +2716,6 @@ aipd_timeout_cb (gpointer user_data) return FALSE; } -static void -aipd_child_setup (gpointer user_data G_GNUC_UNUSED) -{ - /* We are in the child process at this point. - * Give child it's own program group for signal - * separation. - */ - pid_t pid = getpid (); - setpgid (pid, pid); -} - /* default to installed helper, but can be modified for testing */ const char *nm_device_autoipd_helper_path = LIBEXECDIR "/nm-avahi-autoipd.action"; @@ -2766,7 +2755,7 @@ aipd_start (NMDevice *self, NMDeviceStateReason *reason) g_free (cmdline); if (!g_spawn_async ("/", (char **) argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, - &aipd_child_setup, NULL, &(priv->aipd_pid), &error)) { + nm_utils_setpgid, NULL, &(priv->aipd_pid), &error)) { _LOGW (LOGD_DEVICE | LOGD_AUTOIP4, "Activation: Stage 3 of 5 (IP Configure Start) failed" " to start avahi-autoipd: %s", diff --git a/src/devices/team/nm-device-team.c b/src/devices/team/nm-device-team.c index dcd52f2013..a03ad3f98b 100644 --- a/src/devices/team/nm-device-team.c +++ b/src/devices/team/nm-device-team.c @@ -383,17 +383,6 @@ teamd_process_watch_cb (GPid pid, gint status, gpointer user_data) } static void -teamd_child_setup (gpointer user_data G_GNUC_UNUSED) -{ - /* We are in the child process at this point. - * Give child it's own program group for signal - * separation. - */ - pid_t pid = getpid (); - setpgid (pid, pid); -} - -static void nm_device_team_watch_dbus (NMDeviceTeam *self) { NMDeviceTeamPrivate *priv = NM_DEVICE_TEAM_GET_PRIVATE (self); @@ -486,7 +475,7 @@ teamd_start (NMDevice *device, NMSettingTeam *s_team) priv->teamd_timeout = g_timeout_add_seconds (5, teamd_timeout_cb, device); ret = g_spawn_async ("/", (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, - &teamd_child_setup, NULL, &priv->teamd_pid, &error); + nm_utils_setpgid, NULL, &priv->teamd_pid, &error); g_ptr_array_free (argv, TRUE); if (!ret) { _LOGW (LOGD_TEAM, "Activation: (team) failed to start teamd: %s", error->message); diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index 7843b2fddd..91515ddac1 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -300,14 +300,6 @@ create_dhclient_config (const char *iface, } -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); -} - static gboolean dhclient_start (NMDhcpClient *client, const char *mode_opt, @@ -450,7 +442,7 @@ dhclient_start (NMDhcpClient *client, if (g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, - &dhclient_child_setup, NULL, &pid, &error)) { + nm_utils_setpgid, NULL, &pid, &error)) { g_assert (pid > 0); nm_log_info (log_domain, "dhclient started with pid %d", pid); if (release == FALSE) diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index 464733c4fb..86aba4dac1 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -58,14 +58,6 @@ nm_dhcp_dhcpcd_get_path (void) return path; } -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); -} - static gboolean ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) { @@ -136,7 +128,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last if (g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, - &dhcpcd_child_setup, NULL, &pid, &error)) { + nm_utils_setpgid, NULL, &pid, &error)) { g_assert (pid > 0); nm_log_info (LOGD_DHCP4, "dhcpcd started with pid %d", pid); nm_dhcp_client_watch_child (client, pid); diff --git a/src/dns-manager/nm-dns-plugin.c b/src/dns-manager/nm-dns-plugin.c index 9cb0613029..3d593cce8d 100644 --- a/src/dns-manager/nm-dns-plugin.c +++ b/src/dns-manager/nm-dns-plugin.c @@ -135,14 +135,6 @@ watch_cb (GPid pid, gint status, gpointer user_data) g_signal_emit (self, signals[CHILD_QUIT], 0, status); } -static void -child_setup (gpointer user_data G_GNUC_UNUSED) -{ - /* We are in the child process at this point */ - pid_t pid = getpid (); - setpgid (pid, pid); -} - GPid nm_dns_plugin_child_spawn (NMDnsPlugin *self, const char **argv, @@ -176,8 +168,8 @@ nm_dns_plugin_child_spawn (NMDnsPlugin *self, priv->pid = 0; if (g_spawn_async (NULL, (char **) argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, - child_setup, - NULL, &priv->pid, + nm_utils_setpgid, NULL, + &priv->pid, &error)) { nm_log_dbg (LOGD_DNS, "%s started with pid %d", priv->progname, priv->pid); priv->watch_id = g_child_watch_add (priv->pid, (GChildWatchFunc) watch_cb, self); diff --git a/src/dnsmasq-manager/nm-dnsmasq-manager.c b/src/dnsmasq-manager/nm-dnsmasq-manager.c index 7cf80907e2..a85631bcb1 100644 --- a/src/dnsmasq-manager/nm-dnsmasq-manager.c +++ b/src/dnsmasq-manager/nm-dnsmasq-manager.c @@ -296,14 +296,6 @@ create_dm_cmd_line (const char *iface, } static void -dm_child_setup (gpointer user_data G_GNUC_UNUSED) -{ - /* We are in the child process at this point */ - pid_t pid = getpid (); - setpgid (pid, pid); -} - -static void kill_existing_for_iface (const char *iface, const char *pidfile) { char *contents = NULL; @@ -367,9 +359,9 @@ nm_dnsmasq_manager_start (NMDnsMasqManager *manager, priv->pid = 0; if (!g_spawn_async (NULL, (char **) dm_cmd->array->pdata, NULL, - G_SPAWN_DO_NOT_REAP_CHILD, - dm_child_setup, - NULL, &priv->pid, error)) { + G_SPAWN_DO_NOT_REAP_CHILD, + nm_utils_setpgid, NULL, + &priv->pid, error)) { goto out; } diff --git a/src/ppp-manager/nm-ppp-manager.c b/src/ppp-manager/nm-ppp-manager.c index c436ab4f43..5d22f52557 100644 --- a/src/ppp-manager/nm-ppp-manager.c +++ b/src/ppp-manager/nm-ppp-manager.c @@ -1026,14 +1026,6 @@ create_pppd_cmd_line (NMPPPManager *self, } static void -pppd_child_setup (gpointer user_data G_GNUC_UNUSED) -{ - /* We are in the child process at this point */ - pid_t pid = getpid (); - setpgid (pid, pid); -} - -static void pppoe_fill_defaults (NMSettingPpp *setting) { if (!nm_setting_ppp_get_mtu (setting)) @@ -1131,8 +1123,8 @@ nm_ppp_manager_start (NMPPPManager *manager, priv->pid = 0; if (!g_spawn_async (NULL, (char **) ppp_cmd->array->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, - pppd_child_setup, - NULL, &priv->pid, err)) { + nm_utils_setpgid, NULL, + &priv->pid, err)) { goto out; } diff --git a/src/vpn-manager/nm-vpn-service.c b/src/vpn-manager/nm-vpn-service.c index 23b0e8604d..77220dd2ea 100644 --- a/src/vpn-manager/nm-vpn-service.c +++ b/src/vpn-manager/nm-vpn-service.c @@ -166,14 +166,6 @@ nm_vpn_service_stop_connections (NMVpnService *service, g_clear_pointer (&priv->pending, g_slist_free); } -static void -_daemon_setup (gpointer user_data G_GNUC_UNUSED) -{ - /* We are in the child process at this point */ - pid_t pid = getpid (); - setpgid (pid, pid); -} - static gboolean _daemon_exec_timeout (gpointer data) { @@ -200,7 +192,7 @@ nm_vpn_service_daemon_exec (NMVpnService *service, GError **error) vpn_argv[0] = priv->program; vpn_argv[1] = NULL; - success = g_spawn_async (NULL, vpn_argv, NULL, 0, _daemon_setup, NULL, &pid, &spawn_error); + success = g_spawn_async (NULL, vpn_argv, NULL, 0, nm_utils_setpgid, NULL, &pid, &spawn_error); if (success) { nm_log_info (LOGD_VPN, "VPN service '%s' started (%s), PID %ld", priv->name, priv->dbus_service, (long int) pid); |