diff --git a/src/Makefile.am b/src/Makefile.am index 581ddee..eba3be7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,6 +32,8 @@ usb_moded_SOURCES = \ usb_moded-mac.h \ usb_moded-dyn-config.c \ usb_moded-dyn-config.h \ + usb_moded-systemd.c \ + usb_moded-systemd.h \ usb_moded-modules.c if GCONF diff --git a/src/usb_moded-appsync.c b/src/usb_moded-appsync.c index 9fae12f..1723d7e 100644 --- a/src/usb_moded-appsync.c +++ b/src/usb_moded-appsync.c @@ -126,6 +126,8 @@ static struct list_elem *read_file(const gchar *filename) log_debug("Launch mode = %s\n", list_item->mode); list_item->upstart = g_key_file_get_integer(settingsfile, APP_INFO_ENTRY, APP_INFO_UPSTART_KEY, NULL); log_debug("Upstart control = %d\n", list_item->upstart); + list_item->systemd = g_key_file_get_integer(settingsfile, APP_INFO_ENTRY, APP_INFO_SYSTEMD_KEY, NULL); + log_debug("Systemd control = %d\n", list_item->systemd); cleanup: @@ -201,14 +203,17 @@ int activate_sync(const char *mode) if(!strcmp(mode, data->mode)) { log_debug("launching app %s\n", data->launch); + if(data->systemd) + { + } #ifdef UPSTART - if(data->upstart) + else if(data->upstart) { if(!upstart_control_job(data->name, UPSTART_START)) mark_active(data->name); } - else #endif /* UPSTART */ + else usb_moded_dbus_app_launch(data->launch); } } @@ -291,7 +296,6 @@ static gboolean enumerate_usb(gpointer data) return FALSE; } -#ifdef UPSTART int appsync_stop(void) { GList *iter = 0; @@ -299,14 +303,19 @@ int appsync_stop(void) for( iter = sync_list; iter; iter = g_list_next(iter) ) { struct list_elem *data = iter->data; - if(data->upstart) + + if(data->systemd) + { + } +#ifdef UPSTART + else if(data->upstart) { log_debug("Stopping %s\n", data->launch); if(upstart_control_job(data->name, UPSTART_STOP)) log_debug("Failed to stop %s\n", data->name); } +#endif /* UPSTART */ } return(0); } -#endif /* UPSTART */ diff --git a/src/usb_moded-appsync.h b/src/usb_moded-appsync.h index be71dd0..1902ccc 100644 --- a/src/usb_moded-appsync.h +++ b/src/usb_moded-appsync.h @@ -26,6 +26,7 @@ #define APP_INFO_NAME_KEY "name" #define APP_INFO_LAUNCH_KEY "launch" #define APP_INFO_UPSTART_KEY "upstart" +#define APP_INFO_SYSTEMD_KEY "systemd" /** * keep all the needed info together for launching an app @@ -38,6 +39,7 @@ typedef struct list_elem char *launch; /* dbus launch command/address */ int active; /* marker to check if the app has started sucessfully */ int upstart; /* marker to know if we start it with upstart or not */ + int systemd; /* marker to know if we start it with systemd or not */ /*@}*/ }list_elem; diff --git a/src/usb_moded-modesetting.c b/src/usb_moded-modesetting.c index 649dbd8..4021569 100644 --- a/src/usb_moded-modesetting.c +++ b/src/usb_moded-modesetting.c @@ -366,9 +366,7 @@ int usb_moded_mode_cleanup(const char *module) log_debug("Cleaning up mode\n"); -#ifdef UPSTART appsync_stop(); -#endif /* UPSTART */ if(!strcmp(module, MODULE_MASS_STORAGE)|| !strcmp(module, MODULE_FILE_STORAGE)) { diff --git a/src/usb_moded-systemd.c b/src/usb_moded-systemd.c new file mode 100644 index 0000000..4ea7a68 --- /dev/null +++ b/src/usb_moded-systemd.c @@ -0,0 +1,85 @@ +/** + @file usb_moded-systemd.c + + Copyright (C) 2013 Jolla oy. All rights reserved. + + @author: Philippe De Swert + + This program is free software; you can redistribute it and/or + modify it under the terms of the Lesser GNU General Public License + version 2 as published by the Free Software Foundation. + + 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 Lesser GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#define _GNU_SOURCE +#include +#include + +#include +#include +#include + +#include "usb_moded-dbus.h" +#include "usb_moded-dbus-private.h" +#include "usb_moded.h" +#include "usb_moded-log.h" +#include "usb_moded-systemd.h" + +static DBusConnection * get_systemd_dbus_connection(void) +{ + DBusError error; + DBusConnection *conn = 0; + + dbus_error_init(&error); + + conn = dbus_connection_open_private("unix:path=/run/systemd/private", &error); + if (!conn) + { + if (dbus_error_is_set(&error)) + log_err("Cannot connect to systemd: %s", error.message); + else + log_err("Cannot connect to systemd"); + return 0; + } + + return conn; +} + +// mode = isolate +// method = StartUnit or StopUnit +int systemd_control_service(const char *name, const char *method) +{ + + DBusConnection *bus; + DBusError error; + DBusMessage *msg = NULL, *reply = NULL; + int ret = 1; + + bus = get_systemd_dbus_connection(); + + msg = dbus_message_new_method_call("org.freedesktop.systemd1", + "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", method); + if(msg) + { + dbus_message_append_args (msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, "isolate", DBUS_TYPE_INVALID); + reply = dbus_connection_send_with_reply_and_block(bus, msg, -1, &error); + if(reply) + ret = 0; + dbus_message_unref(msg); + } + + dbus_message_unref(msg); + dbus_connection_close(bus); + dbus_connection_unref(bus); + + return(ret); +} diff --git a/src/usb_moded-systemd.h b/src/usb_moded-systemd.h new file mode 100644 index 0000000..9fbdf39 --- /dev/null +++ b/src/usb_moded-systemd.h @@ -0,0 +1,22 @@ +/* + + Copyright (C) 2013 Jolla Oy. All rights reserved. + + author: Philippe De Swert + + This program is free software; you can redistribute it and/or + modify it under the terms of the Lesser GNU General Public License + version 2 as published by the Free Software Foundation. + + 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 Lesser GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +int systemd_control_service(const char *name, const char *method);