]>
Commit | Line | Data |
---|---|---|
3f49d45a LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <errno.h> | |
a185c5aa | 23 | #include <string.h> |
3f49d45a LP |
24 | |
25 | #include "logind.h" | |
26 | #include "logind-session.h" | |
27 | #include "dbus-common.h" | |
28 | #include "util.h" | |
29 | ||
30 | #define BUS_SESSION_INTERFACE \ | |
31 | " <interface name=\"org.freedesktop.login1.Session\">\n" \ | |
32 | " <method name=\"Terminate\"/>\n" \ | |
33 | " <method name=\"Activate\"/>\n" \ | |
f401e48c LP |
34 | " <method name=\"Lock\"/>\n" \ |
35 | " <method name=\"Unlock\"/>\n" \ | |
36 | " <method name=\"SetIdleHint\">\n" \ | |
37 | " <arg name=\"b\" type=\"b\"/>\n" \ | |
38 | " </method>\n" \ | |
e3e9cc80 | 39 | " <property name=\"Id\" type=\"s\" access=\"read\"/>\n" \ |
3f49d45a LP |
40 | " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \ |
41 | " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \ | |
f401e48c LP |
42 | " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \ |
43 | " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \ | |
3f49d45a LP |
44 | " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \ |
45 | " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \ | |
46 | " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \ | |
47 | " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \ | |
48 | " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \ | |
49 | " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \ | |
50 | " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \ | |
51 | " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \ | |
98a28fef | 52 | " <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \ |
3f49d45a LP |
53 | " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \ |
54 | " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \ | |
55 | " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \ | |
56 | " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \ | |
57 | " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \ | |
58 | " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \ | |
59 | " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \ | |
f401e48c LP |
60 | " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \ |
61 | " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \ | |
62 | " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \ | |
3f49d45a LP |
63 | " </interface>\n" |
64 | ||
65 | #define INTROSPECTION \ | |
66 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ | |
67 | "<node>\n" \ | |
68 | BUS_SESSION_INTERFACE \ | |
69 | BUS_PROPERTIES_INTERFACE \ | |
70 | BUS_PEER_INTERFACE \ | |
71 | BUS_INTROSPECTABLE_INTERFACE \ | |
72 | "</node>\n" | |
73 | ||
74 | #define INTERFACES_LIST \ | |
75 | BUS_GENERIC_INTERFACES_LIST \ | |
76 | "org.freedesktop.login1.Session\0" | |
77 | ||
78 | static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) { | |
79 | DBusMessageIter sub; | |
80 | Session *s = data; | |
81 | const char *id, *path; | |
82 | char *p = NULL; | |
83 | ||
84 | assert(i); | |
85 | assert(property); | |
86 | assert(s); | |
87 | ||
88 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
89 | return -ENOMEM; | |
90 | ||
91 | if (s->seat) { | |
92 | id = s->seat->id; | |
93 | path = p = seat_bus_path(s->seat); | |
94 | ||
95 | if (!p) | |
96 | return -ENOMEM; | |
97 | } else { | |
98 | id = ""; | |
99 | path = "/"; | |
100 | } | |
101 | ||
102 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) || | |
103 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) { | |
104 | free(p); | |
105 | return -ENOMEM; | |
106 | } | |
107 | ||
108 | free(p); | |
109 | ||
110 | if (!dbus_message_iter_close_container(i, &sub)) | |
111 | return -ENOMEM; | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) { | |
117 | DBusMessageIter sub; | |
118 | Session *s = data; | |
119 | char *p = NULL; | |
120 | ||
121 | assert(i); | |
122 | assert(property); | |
123 | assert(s); | |
124 | ||
125 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
126 | return -ENOMEM; | |
127 | ||
128 | p = user_bus_path(s->user); | |
129 | if (!p) | |
130 | return -ENOMEM; | |
131 | ||
132 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) || | |
133 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { | |
134 | free(p); | |
135 | return -ENOMEM; | |
136 | } | |
137 | ||
138 | free(p); | |
139 | ||
140 | if (!dbus_message_iter_close_container(i, &sub)) | |
141 | return -ENOMEM; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) { | |
147 | Session *s = data; | |
77527da0 | 148 | dbus_bool_t b; |
3f49d45a LP |
149 | |
150 | assert(i); | |
151 | assert(property); | |
152 | assert(s); | |
153 | ||
154 | b = session_is_active(s); | |
155 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
156 | return -ENOMEM; | |
157 | ||
158 | return 0; | |
159 | } | |
160 | ||
a185c5aa LP |
161 | static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) { |
162 | Session *s = data; | |
77527da0 | 163 | int b; |
a185c5aa LP |
164 | |
165 | assert(i); | |
166 | assert(property); | |
167 | assert(s); | |
168 | ||
77527da0 | 169 | b = session_get_idle_hint(s, NULL) > 0; |
a185c5aa LP |
170 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) |
171 | return -ENOMEM; | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) { | |
177 | Session *s = data; | |
178 | dual_timestamp t; | |
179 | uint64_t u; | |
180 | ||
181 | assert(i); | |
182 | assert(property); | |
183 | assert(s); | |
184 | ||
185 | session_get_idle_hint(s, &t); | |
186 | u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic; | |
187 | ||
188 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) | |
189 | return -ENOMEM; | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
3f49d45a LP |
194 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType); |
195 | ||
196 | static int get_session_for_path(Manager *m, const char *path, Session **_s) { | |
197 | Session *s; | |
198 | char *id; | |
199 | ||
200 | assert(m); | |
201 | assert(path); | |
202 | assert(_s); | |
203 | ||
204 | if (!startswith(path, "/org/freedesktop/login1/session/")) | |
205 | return -EINVAL; | |
206 | ||
207 | id = bus_path_unescape(path + 32); | |
208 | if (!id) | |
209 | return -ENOMEM; | |
210 | ||
211 | s = hashmap_get(m->sessions, id); | |
212 | free(id); | |
213 | ||
214 | if (!s) | |
215 | return -ENOENT; | |
216 | ||
217 | *_s = s; | |
218 | return 0; | |
219 | } | |
220 | ||
221 | static DBusHandlerResult session_message_dispatch( | |
222 | Session *s, | |
223 | DBusConnection *connection, | |
224 | DBusMessage *message) { | |
225 | ||
226 | const BusProperty properties[] = { | |
a185c5aa LP |
227 | { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id }, |
228 | { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s }, | |
229 | { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name }, | |
230 | { "org.freedesktop.login1.Session", "Timestamp", bus_property_append_usec, "t", &s->timestamp.realtime }, | |
231 | { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec, "t", &s->timestamp.monotonic }, | |
232 | { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path }, | |
233 | { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr }, | |
234 | { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s }, | |
235 | { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty }, | |
236 | { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display }, | |
237 | { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote }, | |
238 | { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user }, | |
239 | { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host }, | |
98a28fef | 240 | { "org.freedesktop.login1.Session", "Service", bus_property_append_string, "s", s->service }, |
a185c5aa LP |
241 | { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader }, |
242 | { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id }, | |
243 | { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type }, | |
244 | { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s }, | |
245 | { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers }, | |
246 | { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers }, | |
247 | { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes }, | |
248 | { "org.freedesktop.login1.Session", "IdleHint", bus_session_append_idle_hint, "b", s }, | |
249 | { "org.freedesktop.login1.Session", "IdleSinceHint", bus_session_append_idle_hint_since, "t", s }, | |
250 | { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", s }, | |
3f49d45a LP |
251 | { NULL, NULL, NULL, NULL, NULL } |
252 | }; | |
253 | ||
bef422ae LP |
254 | DBusError error; |
255 | DBusMessage *reply = NULL; | |
256 | int r; | |
257 | ||
3f49d45a LP |
258 | assert(s); |
259 | assert(connection); | |
260 | assert(message); | |
261 | ||
bef422ae LP |
262 | dbus_error_init(&error); |
263 | ||
264 | if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) { | |
265 | ||
266 | r = session_stop(s); | |
267 | if (r < 0) | |
268 | return bus_send_error_reply(connection, message, NULL, r); | |
269 | ||
270 | reply = dbus_message_new_method_return(message); | |
271 | if (!reply) | |
272 | goto oom; | |
273 | ||
274 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) { | |
275 | ||
276 | r = session_activate(s); | |
277 | if (r < 0) | |
278 | return bus_send_error_reply(connection, message, NULL, r); | |
279 | ||
280 | reply = dbus_message_new_method_return(message); | |
281 | if (!reply) | |
282 | goto oom; | |
283 | ||
284 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") || | |
285 | dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) { | |
bef422ae | 286 | |
88e3dc90 | 287 | if (session_send_signal(s, streq(dbus_message_get_member(message), "Lock")) < 0) |
bef422ae LP |
288 | goto oom; |
289 | ||
290 | reply = dbus_message_new_method_return(message); | |
291 | if (!reply) | |
292 | goto oom; | |
293 | ||
294 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) { | |
295 | dbus_bool_t b; | |
5bc849fd | 296 | unsigned long ul; |
bef422ae LP |
297 | |
298 | if (!dbus_message_get_args( | |
299 | message, | |
300 | &error, | |
301 | DBUS_TYPE_BOOLEAN, &b, | |
302 | DBUS_TYPE_INVALID)) | |
303 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
304 | ||
5bc849fd LP |
305 | ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error); |
306 | if (ul == (unsigned long) -1) | |
307 | return bus_send_error_reply(connection, message, &error, -EIO); | |
308 | ||
309 | if (ul != 0 && ul != s->user->uid) | |
310 | return bus_send_error_reply(connection, message, NULL, -EPERM); | |
311 | ||
bef422ae LP |
312 | session_set_idle_hint(s, b); |
313 | ||
314 | reply = dbus_message_new_method_return(message); | |
315 | if (!reply) | |
316 | goto oom; | |
317 | ||
318 | } else | |
319 | return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); | |
320 | ||
321 | if (reply) { | |
322 | if (!dbus_connection_send(connection, reply, NULL)) | |
323 | goto oom; | |
324 | ||
325 | dbus_message_unref(reply); | |
326 | } | |
327 | ||
328 | return DBUS_HANDLER_RESULT_HANDLED; | |
329 | ||
330 | oom: | |
331 | if (reply) | |
332 | dbus_message_unref(reply); | |
333 | ||
334 | dbus_error_free(&error); | |
335 | ||
336 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
3f49d45a LP |
337 | } |
338 | ||
339 | static DBusHandlerResult session_message_handler( | |
340 | DBusConnection *connection, | |
341 | DBusMessage *message, | |
342 | void *userdata) { | |
343 | ||
344 | Manager *m = userdata; | |
345 | Session *s; | |
346 | int r; | |
347 | ||
348 | r = get_session_for_path(m, dbus_message_get_path(message), &s); | |
349 | if (r < 0) { | |
350 | ||
351 | if (r == -ENOMEM) | |
352 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
353 | ||
354 | if (r == -ENOENT) { | |
355 | DBusError e; | |
356 | ||
357 | dbus_error_init(&e); | |
358 | dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session"); | |
359 | return bus_send_error_reply(connection, message, &e, r); | |
360 | } | |
361 | ||
362 | return bus_send_error_reply(connection, message, NULL, r); | |
363 | } | |
364 | ||
365 | return session_message_dispatch(s, connection, message); | |
366 | } | |
367 | ||
368 | const DBusObjectPathVTable bus_session_vtable = { | |
369 | .message_function = session_message_handler | |
370 | }; | |
371 | ||
372 | char *session_bus_path(Session *s) { | |
373 | char *t, *r; | |
374 | ||
375 | assert(s); | |
376 | ||
377 | t = bus_path_escape(s->id); | |
378 | if (!t) | |
379 | return NULL; | |
380 | ||
381 | r = strappend("/org/freedesktop/login1/session/", t); | |
382 | free(t); | |
383 | ||
384 | return r; | |
385 | } | |
da119395 LP |
386 | |
387 | int session_send_signal(Session *s, bool new_session) { | |
388 | DBusMessage *m; | |
389 | int r = -ENOMEM; | |
390 | char *p = NULL; | |
391 | ||
392 | assert(s); | |
393 | ||
394 | m = dbus_message_new_signal("/org/freedesktop/login1", | |
395 | "org.freedesktop.login1.Manager", | |
396 | new_session ? "SessionNew" : "SessionRemoved"); | |
397 | ||
398 | if (!m) | |
399 | return -ENOMEM; | |
400 | ||
401 | p = session_bus_path(s); | |
402 | if (!p) | |
403 | goto finish; | |
404 | ||
405 | if (!dbus_message_append_args( | |
406 | m, | |
407 | DBUS_TYPE_STRING, &s->id, | |
408 | DBUS_TYPE_OBJECT_PATH, &p, | |
409 | DBUS_TYPE_INVALID)) | |
410 | goto finish; | |
411 | ||
412 | if (!dbus_connection_send(s->manager->bus, m, NULL)) | |
413 | goto finish; | |
414 | ||
415 | r = 0; | |
416 | ||
417 | finish: | |
418 | dbus_message_unref(m); | |
419 | free(p); | |
420 | ||
421 | return r; | |
422 | } | |
9418f147 LP |
423 | |
424 | int session_send_changed(Session *s, const char *properties) { | |
425 | DBusMessage *m; | |
426 | int r = -ENOMEM; | |
427 | char *p = NULL; | |
428 | ||
429 | assert(s); | |
430 | ||
ed18b08b LP |
431 | if (!s->started) |
432 | return 0; | |
433 | ||
9418f147 LP |
434 | p = session_bus_path(s); |
435 | if (!p) | |
436 | return -ENOMEM; | |
437 | ||
438 | m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties); | |
439 | if (!m) | |
440 | goto finish; | |
441 | ||
442 | if (!dbus_connection_send(s->manager->bus, m, NULL)) | |
443 | goto finish; | |
444 | ||
445 | r = 0; | |
446 | ||
447 | finish: | |
448 | if (m) | |
449 | dbus_message_unref(m); | |
450 | free(p); | |
451 | ||
452 | return r; | |
453 | } | |
88e3dc90 LP |
454 | |
455 | int session_send_lock(Session *s, bool lock) { | |
456 | DBusMessage *m; | |
457 | bool b; | |
458 | char *p; | |
459 | ||
460 | assert(s); | |
461 | ||
462 | p = session_bus_path(s); | |
463 | if (!p) | |
464 | return -ENOMEM; | |
465 | ||
466 | m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock"); | |
467 | free(p); | |
468 | ||
469 | if (!m) | |
470 | return -ENOMEM; | |
471 | ||
472 | b = dbus_connection_send(s->manager->bus, m, NULL); | |
473 | dbus_message_unref(m); | |
474 | ||
475 | if (!b) | |
476 | return -ENOMEM; | |
477 | ||
478 | return 0; | |
479 | } |