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.
This commit is contained in:
Dan Winship
2015-01-12 15:50:22 -05:00
parent c22e3f327a
commit fb792af7cb
10 changed files with 32 additions and 82 deletions

View File

@@ -2546,3 +2546,21 @@ nm_utils_ip6_routes_from_gvalue (const GValue *value)
return g_slist_reverse (list); 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);
}

View File

@@ -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_ip4_routes_from_gvalue (const GValue *value);
GSList *nm_utils_ip6_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__ */ #endif /* __NETWORKMANAGER_UTILS_H__ */

View File

@@ -2716,17 +2716,6 @@ aipd_timeout_cb (gpointer user_data)
return FALSE; 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 */ /* default to installed helper, but can be modified for testing */
const char *nm_device_autoipd_helper_path = LIBEXECDIR "/nm-avahi-autoipd.action"; 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); g_free (cmdline);
if (!g_spawn_async ("/", (char **) argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, 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, _LOGW (LOGD_DEVICE | LOGD_AUTOIP4,
"Activation: Stage 3 of 5 (IP Configure Start) failed" "Activation: Stage 3 of 5 (IP Configure Start) failed"
" to start avahi-autoipd: %s", " to start avahi-autoipd: %s",

View File

@@ -382,17 +382,6 @@ teamd_process_watch_cb (GPid pid, gint status, gpointer user_data)
teamd_cleanup (device, TRUE); teamd_cleanup (device, TRUE);
} }
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 static void
nm_device_team_watch_dbus (NMDeviceTeam *self) nm_device_team_watch_dbus (NMDeviceTeam *self)
{ {
@@ -486,7 +475,7 @@ teamd_start (NMDevice *device, NMSettingTeam *s_team)
priv->teamd_timeout = g_timeout_add_seconds (5, teamd_timeout_cb, device); 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, 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); g_ptr_array_free (argv, TRUE);
if (!ret) { if (!ret) {
_LOGW (LOGD_TEAM, "Activation: (team) failed to start teamd: %s", error->message); _LOGW (LOGD_TEAM, "Activation: (team) failed to start teamd: %s", error->message);

View File

@@ -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 static gboolean
dhclient_start (NMDhcpClient *client, dhclient_start (NMDhcpClient *client,
const char *mode_opt, const char *mode_opt,
@@ -450,7 +442,7 @@ dhclient_start (NMDhcpClient *client,
if (g_spawn_async (NULL, (char **) argv->pdata, NULL, 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, 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); g_assert (pid > 0);
nm_log_info (log_domain, "dhclient started with pid %d", pid); nm_log_info (log_domain, "dhclient started with pid %d", pid);
if (release == FALSE) if (release == FALSE)

View File

@@ -58,14 +58,6 @@ nm_dhcp_dhcpcd_get_path (void)
return path; 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 static gboolean
ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last_ip4_address) 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, 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, 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); g_assert (pid > 0);
nm_log_info (LOGD_DHCP4, "dhcpcd started with pid %d", pid); nm_log_info (LOGD_DHCP4, "dhcpcd started with pid %d", pid);
nm_dhcp_client_watch_child (client, pid); nm_dhcp_client_watch_child (client, pid);

View File

@@ -135,14 +135,6 @@ watch_cb (GPid pid, gint status, gpointer user_data)
g_signal_emit (self, signals[CHILD_QUIT], 0, status); 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 GPid
nm_dns_plugin_child_spawn (NMDnsPlugin *self, nm_dns_plugin_child_spawn (NMDnsPlugin *self,
const char **argv, const char **argv,
@@ -176,8 +168,8 @@ nm_dns_plugin_child_spawn (NMDnsPlugin *self,
priv->pid = 0; priv->pid = 0;
if (g_spawn_async (NULL, (char **) argv, NULL, if (g_spawn_async (NULL, (char **) argv, NULL,
G_SPAWN_DO_NOT_REAP_CHILD, G_SPAWN_DO_NOT_REAP_CHILD,
child_setup, nm_utils_setpgid, NULL,
NULL, &priv->pid, &priv->pid,
&error)) { &error)) {
nm_log_dbg (LOGD_DNS, "%s started with pid %d", priv->progname, priv->pid); 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); priv->watch_id = g_child_watch_add (priv->pid, (GChildWatchFunc) watch_cb, self);

View File

@@ -295,14 +295,6 @@ create_dm_cmd_line (const char *iface,
return cmd; return cmd;
} }
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 static void
kill_existing_for_iface (const char *iface, const char *pidfile) kill_existing_for_iface (const char *iface, const char *pidfile)
{ {
@@ -368,8 +360,8 @@ nm_dnsmasq_manager_start (NMDnsMasqManager *manager,
priv->pid = 0; priv->pid = 0;
if (!g_spawn_async (NULL, (char **) dm_cmd->array->pdata, NULL, if (!g_spawn_async (NULL, (char **) dm_cmd->array->pdata, NULL,
G_SPAWN_DO_NOT_REAP_CHILD, G_SPAWN_DO_NOT_REAP_CHILD,
dm_child_setup, nm_utils_setpgid, NULL,
NULL, &priv->pid, error)) { &priv->pid, error)) {
goto out; goto out;
} }

View File

@@ -1025,14 +1025,6 @@ create_pppd_cmd_line (NMPPPManager *self,
return cmd; return cmd;
} }
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 static void
pppoe_fill_defaults (NMSettingPpp *setting) pppoe_fill_defaults (NMSettingPpp *setting)
{ {
@@ -1131,8 +1123,8 @@ nm_ppp_manager_start (NMPPPManager *manager,
priv->pid = 0; priv->pid = 0;
if (!g_spawn_async (NULL, (char **) ppp_cmd->array->pdata, NULL, if (!g_spawn_async (NULL, (char **) ppp_cmd->array->pdata, NULL,
G_SPAWN_DO_NOT_REAP_CHILD, G_SPAWN_DO_NOT_REAP_CHILD,
pppd_child_setup, nm_utils_setpgid, NULL,
NULL, &priv->pid, err)) { &priv->pid, err)) {
goto out; goto out;
} }

View File

@@ -166,14 +166,6 @@ nm_vpn_service_stop_connections (NMVpnService *service,
g_clear_pointer (&priv->pending, g_slist_free); 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 static gboolean
_daemon_exec_timeout (gpointer data) _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[0] = priv->program;
vpn_argv[1] = NULL; 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) { if (success) {
nm_log_info (LOGD_VPN, "VPN service '%s' started (%s), PID %ld", nm_log_info (LOGD_VPN, "VPN service '%s' started (%s), PID %ld",
priv->name, priv->dbus_service, (long int) pid); priv->name, priv->dbus_service, (long int) pid);