remove unused header
[poetteringd.git] / systemd.c
blob455d7b2bd1a91a39537ccdb294779df05f2ae7f9
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include <dbus/dbus-protocol.h>
7 #include <glib.h>
8 #include <gio/gio.h>
9 #include <polkit/polkit.h>
11 #include "systemd.h"
12 #include "systemd1-generated.h"
13 #include "main.h"
14 #include "utils.h"
16 #include "rc.h"
18 #define QUOTE(macro) #macro
19 #define STR(macro) QUOTE(macro)
21 struct unit_invoked_name {
22 GDBusMethodInvocation *invocation;
23 gchar *name; /* newly allocated */
24 gchar *mode; /* newly allocated */
25 gint action;
28 static guint bus_id = 0;
29 static gboolean read_only = FALSE;
31 static PoetteringdSystemdSystemd1Manager *systemd1 = NULL;
33 G_LOCK_DEFINE_STATIC (unit);
35 static void
36 on_handle_unit_authorized_cb(GObject *source_object,
37 GAsyncResult *res,
38 gpointer user_data)
40 GError *err = NULL;
41 gchar **tokens = NULL;
42 gchar *error_msg = NULL;
43 struct unit_invoked_name *data = (struct unit_invoked_name *) user_data;
45 G_LOCK(unit);
47 if (!check_polkit_finish (res, &err)) {
48 g_dbus_method_invocation_return_gerror (data->invocation, err);
49 goto out;
52 tokens = g_strsplit(data->name, ".", 0);
54 if (!rc_cmd(tokens[0], RC_GET)) {
55 error_msg = g_strdup_printf("Unit name %s is not valid.", tokens[0]);
56 g_dbus_method_invocation_return_dbus_error (data->invocation,
57 DBUS_ERROR_INVALID_ARGS,
58 error_msg);
59 g_free(error_msg);
60 goto out;
63 if (!rc_cmd(tokens[0], data->action)) {
64 error_msg = g_strdup_printf("Unit name %s failed for action %d.", tokens[0], data->action);
65 g_dbus_method_invocation_return_dbus_error (data->invocation,
66 DBUS_ERROR_INVALID_ARGS,
67 error_msg);
68 g_free(error_msg);
69 goto out;
72 poetteringd_systemd_systemd1_manager_complete_start_unit (systemd1,
73 data->invocation, "/org/freedesktop/systemd1/job/0");
75 out:
76 G_UNLOCK (unit);
77 g_strfreev(tokens);
78 g_free (data);
79 if (err != NULL)
80 g_error_free (err);
83 static gboolean
84 on_handle_unit (PoetteringdSystemdSystemd1Manager *systemd1,
85 GDBusMethodInvocation *invocation,
86 const gchar *name,
87 const gchar *mode,
88 const gboolean user_interaction,
89 gpointer user_data)
91 if (read_only)
92 g_dbus_method_invocation_return_dbus_error (invocation,
93 DBUS_ERROR_NOT_SUPPORTED,
94 "poetteringd hostnamed is in read-only mode");
95 else {
96 const gchar *method = g_dbus_method_invocation_get_method_name(invocation);
97 struct unit_invoked_name *data = g_new0 (struct unit_invoked_name, 1);
99 data = g_new0 (struct unit_invoked_name, 1);
100 data->invocation = invocation;
101 data->name = g_strdup (name);
102 data->mode = g_strdup (mode);
104 if (!g_strcmp0(method, "StartUnit"))
105 data->action = RC_START;
106 else if (!g_strcmp0(method, "StopUnit"))
107 data->action = RC_STOP;
108 else if (!g_strcmp0(method, "RestartUnit"))
109 data->action = RC_RESTART;
110 else if (!g_strcmp0(method, "ReloadUnit"))
111 data->action = RC_RELOAD;
113 check_polkit_async (g_dbus_method_invocation_get_sender (invocation),
114 "org.freedesktop.systemd1.manage-units", user_interaction,
115 on_handle_unit_authorized_cb, data);
118 return TRUE;
121 static void
122 on_bus_acquired (GDBusConnection *connection,
123 const gchar *bus_name,
124 gpointer user_data)
126 GError *err = NULL;
128 g_debug ("Acquired a message bus connection");
130 systemd1 = poetteringd_systemd_systemd1_manager_skeleton_new ();
132 g_signal_connect (systemd1, "handle-start-unit", G_CALLBACK (on_handle_unit), NULL);
133 g_signal_connect (systemd1, "handle-stop-unit", G_CALLBACK (on_handle_unit), NULL);
134 g_signal_connect (systemd1, "handle-restart-unit", G_CALLBACK (on_handle_unit), NULL);
135 g_signal_connect (systemd1, "handle-reload-unit", G_CALLBACK (on_handle_unit), NULL);
137 if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (systemd1),
138 connection,
139 "/org/freedesktop/systemd1",
140 &err)) {
141 if (err != NULL) {
142 g_critical ("Failed to export interface on /org/freedesktop/systemd1: %s", err->message);
143 poetteringd_exit (1);
148 static void
149 on_name_acquired (GDBusConnection *connection,
150 const gchar *bus_name,
151 gpointer user_data)
153 g_debug ("Acquired the name %s", bus_name);
154 poetteringd_component_started ();
157 static void
158 on_name_lost (GDBusConnection *connection,
159 const gchar *bus_name,
160 gpointer user_data)
162 if (connection == NULL)
163 g_critical ("Failed to acquire a dbus connection");
164 else
165 g_critical ("Failed to acquire dbus name %s", bus_name);
167 poetteringd_exit (1);
170 /* Public functions */
172 void
173 systemd_init (gboolean _read_only)
175 read_only = _read_only;
177 bus_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
178 "org.freedesktop.systemd1",
179 G_BUS_NAME_OWNER_FLAGS_NONE,
180 on_bus_acquired,
181 on_name_acquired,
182 on_name_lost,
183 NULL,
184 NULL);
187 void
188 systemd_destroy (void)
190 g_bus_unown_name (bus_id);
191 bus_id = 0;
192 read_only = FALSE;