Commit fb792af7 authored by Dan Winship's avatar Dan Winship

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.
parent c22e3f32
......@@ -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);
}
......@@ -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__ */
......@@ -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",
......
......@@ -382,17 +382,6 @@ teamd_process_watch_cb (GPid pid, gint status, gpointer user_data)
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
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);
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);
......
......@@ -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)
......
......@@ -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);
......
......@@ -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);
......
......@@ -295,14 +295,6 @@ create_dm_cmd_line (const char *iface,
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
kill_existing_for_iface (const char *iface, const char *pidfile)
{
......@@ -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;
}
......
......@@ -1025,14 +1025,6 @@ create_pppd_cmd_line (NMPPPManager *self,
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
pppoe_fill_defaults (NMSettingPpp *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;
}
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment