From dc9f7c0adc0b65aec010586e8b9848db5f0964de Mon Sep 17 00:00:00 2001 From: Rui Matos Date: Wed, 11 Oct 2017 18:10:54 +0200 Subject: [PATCH 3/3] Revert "sharing: Use systemd to track running services" This reverts commit e0b7f4143bdd201c824499dd09159f5890a07c6a. --- plugins/sharing/gsd-sharing-manager.c | 114 ++++++++++++++++------------------ 1 file changed, 55 insertions(+), 59 deletions(-) diff --git a/plugins/sharing/gsd-sharing-manager.c b/plugins/sharing/gsd-sharing-manager.c index 26663442..b24c4814 100644 --- a/plugins/sharing/gsd-sharing-manager.c +++ b/plugins/sharing/gsd-sharing-manager.c @@ -38,6 +38,8 @@ typedef struct { const char *name; GSettings *settings; + gboolean started; + GSubprocess *process; } ServiceInfo; struct GsdSharingManagerPrivate @@ -104,72 +106,48 @@ static const char * const services[] = { }; static void -handle_unit_cb (GObject *source_object, - GAsyncResult *res, - gpointer user_data) +gsd_sharing_manager_start_service (GsdSharingManager *manager, + ServiceInfo *service) { + GDesktopAppInfo *app; + const char *exec; + char *desktop, **argvp; GError *error = NULL; - GVariant *ret; - const char *operation = user_data; - ret = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), - res, &error); - if (!ret) { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) - g_warning ("Failed to %s service: %s", operation, error->message); - g_error_free (error); + if (service->started) return; - } + g_debug ("About to start %s", service->name); - g_variant_unref (ret); + desktop = g_strdup_printf ("%s.desktop", service->name); + app = g_desktop_app_info_new (desktop); + g_free (desktop); -} + if (!app) { + g_warning ("Could not find desktop file for service '%s'", service->name); + return; + } -static void -gsd_sharing_manager_handle_service (GsdSharingManager *manager, - const char *method, - GAsyncReadyCallback callback, - ServiceInfo *service) -{ - char *service_file; - - service_file = g_strdup_printf ("%s.service", service->name); - g_dbus_connection_call (manager->priv->connection, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", - method, - g_variant_new ("(ss)", service_file, "replace"), - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - manager->priv->cancellable, - callback, - manager); - g_free (service_file); -} + exec = g_app_info_get_commandline (G_APP_INFO (app)); -static void -gsd_sharing_manager_start_service (GsdSharingManager *manager, - ServiceInfo *service) -{ - g_debug ("About to start %s", service->name); + if (!g_shell_parse_argv (exec, NULL, &argvp, &error)) { + g_warning ("Could not parse command-line '%s': %s", exec, error->message); + g_error_free (error); + g_object_unref (app); + return; + } - /* We use StartUnit, not StartUnitReplace, since the latter would - * cancel any pending start we already have going from an - * earlier _start_service() call */ - gsd_sharing_manager_handle_service (manager, "StartUnit", - handle_unit_cb, "start"); -} + service->process = g_subprocess_newv ((const gchar * const*) argvp, G_SUBPROCESS_FLAGS_NONE, &error); -static void -gsd_sharing_manager_stop_service (GsdSharingManager *manager, - ServiceInfo *service) -{ - g_debug ("About to stop %s", service->name); + if (!service->process) { + g_warning ("Could not start command-line '%s': %s", exec, error->message); + g_error_free (error); + service->started = FALSE; + } else { + service->started = TRUE; + } - gsd_sharing_manager_handle_service (manager, "StopUnit", - handle_unit_cb, "stop"); + g_strfreev (argvp); + g_object_unref (app); } #ifdef HAVE_NETWORK_MANAGER @@ -202,6 +180,22 @@ service_is_enabled_on_current_connection (GsdSharingManager *manager, #endif /* HAVE_NETWORK_MANAGER */ static void +gsd_sharing_manager_stop_service (GsdSharingManager *manager, + ServiceInfo *service) +{ + if (!service->started || + service->process == NULL) { + return; + } + + g_debug ("About to stop %s", service->name); + + g_subprocess_send_signal (service->process, SIGTERM); + g_clear_object (&service->process); + service->started = FALSE; +} + +static void gsd_sharing_manager_sync_services (GsdSharingManager *manager) { GList *services, *l; @@ -216,10 +210,12 @@ gsd_sharing_manager_sync_services (GsdSharingManager *manager) service_is_enabled_on_current_connection (manager, service)) should_be_started = TRUE; - if (should_be_started) - gsd_sharing_manager_start_service (manager, service); - else - gsd_sharing_manager_stop_service (manager, service); + if (service->started != should_be_started) { + if (service->started) + gsd_sharing_manager_stop_service (manager, service); + else + gsd_sharing_manager_start_service (manager, service); + } } g_list_free (services); } -- 2.13.5