6 #include <dbus/dbus-protocol.h>
9 #include <polkit/polkit.h>
12 #include "systemd1-generated.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 */
28 static guint bus_id
= 0;
29 static gboolean read_only
= FALSE
;
31 static PoetteringdSystemdSystemd1Manager
*systemd1
= NULL
;
33 G_LOCK_DEFINE_STATIC (unit
);
36 on_handle_unit_authorized_cb(GObject
*source_object
,
41 gchar
**tokens
= NULL
;
42 gchar
*error_msg
= NULL
;
43 struct unit_invoked_name
*data
= (struct unit_invoked_name
*) user_data
;
47 if (!check_polkit_finish (res
, &err
)) {
48 g_dbus_method_invocation_return_gerror (data
->invocation
, err
);
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
,
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
,
72 poetteringd_systemd_systemd1_manager_complete_start_unit (systemd1
,
73 data
->invocation
, "/org/freedesktop/systemd1/job/0");
84 on_handle_unit (PoetteringdSystemdSystemd1Manager
*systemd1
,
85 GDBusMethodInvocation
*invocation
,
88 const gboolean user_interaction
,
92 g_dbus_method_invocation_return_dbus_error (invocation
,
93 DBUS_ERROR_NOT_SUPPORTED
,
94 "poetteringd hostnamed is in read-only mode");
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
);
122 on_bus_acquired (GDBusConnection
*connection
,
123 const gchar
*bus_name
,
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
),
139 "/org/freedesktop/systemd1",
142 g_critical ("Failed to export interface on /org/freedesktop/systemd1: %s", err
->message
);
143 poetteringd_exit (1);
149 on_name_acquired (GDBusConnection
*connection
,
150 const gchar
*bus_name
,
153 g_debug ("Acquired the name %s", bus_name
);
154 poetteringd_component_started ();
158 on_name_lost (GDBusConnection
*connection
,
159 const gchar
*bus_name
,
162 if (connection
== NULL
)
163 g_critical ("Failed to acquire a dbus connection");
165 g_critical ("Failed to acquire dbus name %s", bus_name
);
167 poetteringd_exit (1);
170 /* Public functions */
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
,
188 systemd_destroy (void)
190 g_bus_unown_name (bus_id
);