]>
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" \ | |
de07ab16 LP |
39 | " <method name=\"Kill\">\n" \ |
40 | " <arg name=\"who\" type=\"s\"/>\n" \ | |
41 | " <arg name=\"signal\" type=\"s\"/>\n" \ | |
42 | " </method>\n" \ | |
e3e9cc80 | 43 | " <property name=\"Id\" type=\"s\" access=\"read\"/>\n" \ |
3f49d45a LP |
44 | " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \ |
45 | " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \ | |
f401e48c LP |
46 | " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \ |
47 | " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \ | |
3f49d45a LP |
48 | " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \ |
49 | " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \ | |
50 | " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \ | |
51 | " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \ | |
52 | " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \ | |
53 | " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \ | |
54 | " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \ | |
55 | " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \ | |
98a28fef | 56 | " <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \ |
3f49d45a LP |
57 | " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \ |
58 | " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \ | |
59 | " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \ | |
60 | " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \ | |
61 | " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \ | |
62 | " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \ | |
63 | " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \ | |
f401e48c LP |
64 | " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \ |
65 | " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \ | |
66 | " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \ | |
3f49d45a LP |
67 | " </interface>\n" |
68 | ||
69 | #define INTROSPECTION \ | |
70 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ | |
71 | "<node>\n" \ | |
72 | BUS_SESSION_INTERFACE \ | |
73 | BUS_PROPERTIES_INTERFACE \ | |
74 | BUS_PEER_INTERFACE \ | |
75 | BUS_INTROSPECTABLE_INTERFACE \ | |
76 | "</node>\n" | |
77 | ||
78 | #define INTERFACES_LIST \ | |
79 | BUS_GENERIC_INTERFACES_LIST \ | |
80 | "org.freedesktop.login1.Session\0" | |
81 | ||
82 | static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) { | |
83 | DBusMessageIter sub; | |
84 | Session *s = data; | |
85 | const char *id, *path; | |
86 | char *p = NULL; | |
87 | ||
88 | assert(i); | |
89 | assert(property); | |
90 | assert(s); | |
91 | ||
92 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
93 | return -ENOMEM; | |
94 | ||
95 | if (s->seat) { | |
96 | id = s->seat->id; | |
97 | path = p = seat_bus_path(s->seat); | |
98 | ||
99 | if (!p) | |
100 | return -ENOMEM; | |
101 | } else { | |
102 | id = ""; | |
103 | path = "/"; | |
104 | } | |
105 | ||
106 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) || | |
107 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) { | |
108 | free(p); | |
109 | return -ENOMEM; | |
110 | } | |
111 | ||
112 | free(p); | |
113 | ||
114 | if (!dbus_message_iter_close_container(i, &sub)) | |
115 | return -ENOMEM; | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) { | |
121 | DBusMessageIter sub; | |
122 | Session *s = data; | |
123 | char *p = NULL; | |
124 | ||
125 | assert(i); | |
126 | assert(property); | |
127 | assert(s); | |
128 | ||
129 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
130 | return -ENOMEM; | |
131 | ||
132 | p = user_bus_path(s->user); | |
133 | if (!p) | |
134 | return -ENOMEM; | |
135 | ||
136 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) || | |
137 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { | |
138 | free(p); | |
139 | return -ENOMEM; | |
140 | } | |
141 | ||
142 | free(p); | |
143 | ||
144 | if (!dbus_message_iter_close_container(i, &sub)) | |
145 | return -ENOMEM; | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) { | |
151 | Session *s = data; | |
77527da0 | 152 | dbus_bool_t b; |
3f49d45a LP |
153 | |
154 | assert(i); | |
155 | assert(property); | |
156 | assert(s); | |
157 | ||
158 | b = session_is_active(s); | |
159 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
160 | return -ENOMEM; | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
a185c5aa LP |
165 | static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) { |
166 | Session *s = data; | |
77527da0 | 167 | int b; |
a185c5aa LP |
168 | |
169 | assert(i); | |
170 | assert(property); | |
171 | assert(s); | |
172 | ||
77527da0 | 173 | b = session_get_idle_hint(s, NULL) > 0; |
a185c5aa LP |
174 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) |
175 | return -ENOMEM; | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) { | |
181 | Session *s = data; | |
182 | dual_timestamp t; | |
183 | uint64_t u; | |
184 | ||
185 | assert(i); | |
186 | assert(property); | |
187 | assert(s); | |
188 | ||
189 | session_get_idle_hint(s, &t); | |
190 | u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic; | |
191 | ||
192 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u)) | |
193 | return -ENOMEM; | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
3f49d45a LP |
198 | static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType); |
199 | ||
200 | static int get_session_for_path(Manager *m, const char *path, Session **_s) { | |
201 | Session *s; | |
202 | char *id; | |
203 | ||
204 | assert(m); | |
205 | assert(path); | |
206 | assert(_s); | |
207 | ||
208 | if (!startswith(path, "/org/freedesktop/login1/session/")) | |
209 | return -EINVAL; | |
210 | ||
211 | id = bus_path_unescape(path + 32); | |
212 | if (!id) | |
213 | return -ENOMEM; | |
214 | ||
215 | s = hashmap_get(m->sessions, id); | |
216 | free(id); | |
217 | ||
218 | if (!s) | |
219 | return -ENOENT; | |
220 | ||
221 | *_s = s; | |
222 | return 0; | |
223 | } | |
224 | ||
225 | static DBusHandlerResult session_message_dispatch( | |
226 | Session *s, | |
227 | DBusConnection *connection, | |
228 | DBusMessage *message) { | |
229 | ||
230 | const BusProperty properties[] = { | |
a185c5aa LP |
231 | { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id }, |
232 | { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s }, | |
233 | { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name }, | |
234 | { "org.freedesktop.login1.Session", "Timestamp", bus_property_append_usec, "t", &s->timestamp.realtime }, | |
235 | { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec, "t", &s->timestamp.monotonic }, | |
236 | { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path }, | |
237 | { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr }, | |
238 | { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s }, | |
239 | { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty }, | |
240 | { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display }, | |
241 | { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote }, | |
242 | { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user }, | |
243 | { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host }, | |
98a28fef | 244 | { "org.freedesktop.login1.Session", "Service", bus_property_append_string, "s", s->service }, |
a185c5aa LP |
245 | { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader }, |
246 | { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id }, | |
247 | { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type }, | |
248 | { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s }, | |
249 | { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers }, | |
250 | { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers }, | |
251 | { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes }, | |
252 | { "org.freedesktop.login1.Session", "IdleHint", bus_session_append_idle_hint, "b", s }, | |
253 | { "org.freedesktop.login1.Session", "IdleSinceHint", bus_session_append_idle_hint_since, "t", s }, | |
254 | { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", s }, | |
3f49d45a LP |
255 | { NULL, NULL, NULL, NULL, NULL } |
256 | }; | |
257 | ||
bef422ae LP |
258 | DBusError error; |
259 | DBusMessage *reply = NULL; | |
260 | int r; | |
261 | ||
3f49d45a LP |
262 | assert(s); |
263 | assert(connection); | |
264 | assert(message); | |
265 | ||
bef422ae LP |
266 | dbus_error_init(&error); |
267 | ||
268 | if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) { | |
269 | ||
270 | r = session_stop(s); | |
271 | if (r < 0) | |
272 | return bus_send_error_reply(connection, message, NULL, r); | |
273 | ||
274 | reply = dbus_message_new_method_return(message); | |
275 | if (!reply) | |
276 | goto oom; | |
277 | ||
278 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) { | |
279 | ||
280 | r = session_activate(s); | |
281 | if (r < 0) | |
282 | return bus_send_error_reply(connection, message, NULL, r); | |
283 | ||
284 | reply = dbus_message_new_method_return(message); | |
285 | if (!reply) | |
286 | goto oom; | |
287 | ||
288 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") || | |
289 | dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) { | |
bef422ae | 290 | |
88e3dc90 | 291 | if (session_send_signal(s, streq(dbus_message_get_member(message), "Lock")) < 0) |
bef422ae LP |
292 | goto oom; |
293 | ||
294 | reply = dbus_message_new_method_return(message); | |
295 | if (!reply) | |
296 | goto oom; | |
297 | ||
298 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) { | |
299 | dbus_bool_t b; | |
5bc849fd | 300 | unsigned long ul; |
bef422ae LP |
301 | |
302 | if (!dbus_message_get_args( | |
303 | message, | |
304 | &error, | |
305 | DBUS_TYPE_BOOLEAN, &b, | |
306 | DBUS_TYPE_INVALID)) | |
307 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
308 | ||
5bc849fd LP |
309 | ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error); |
310 | if (ul == (unsigned long) -1) | |
311 | return bus_send_error_reply(connection, message, &error, -EIO); | |
312 | ||
313 | if (ul != 0 && ul != s->user->uid) | |
314 | return bus_send_error_reply(connection, message, NULL, -EPERM); | |
315 | ||
bef422ae LP |
316 | session_set_idle_hint(s, b); |
317 | ||
318 | reply = dbus_message_new_method_return(message); | |
319 | if (!reply) | |
320 | goto oom; | |
321 | ||
de07ab16 LP |
322 | } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) { |
323 | const char *swho; | |
324 | int32_t signo; | |
325 | KillWho who; | |
326 | ||
327 | if (!dbus_message_get_args( | |
328 | message, | |
329 | &error, | |
330 | DBUS_TYPE_STRING, &swho, | |
331 | DBUS_TYPE_INT32, &signo, | |
332 | DBUS_TYPE_INVALID)) | |
333 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
334 | ||
335 | if (isempty(swho)) | |
336 | who = KILL_ALL; | |
337 | else { | |
338 | who = kill_who_from_string(swho); | |
339 | if (who < 0) | |
340 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
341 | } | |
342 | ||
343 | if (signo <= 0 || signo >= _NSIG) | |
344 | return bus_send_error_reply(connection, message, &error, -EINVAL); | |
345 | ||
346 | r = session_kill(s, who, signo); | |
347 | if (r < 0) | |
348 | return bus_send_error_reply(connection, message, NULL, r); | |
349 | ||
350 | reply = dbus_message_new_method_return(message); | |
351 | if (!reply) | |
352 | goto oom; | |
353 | ||
bef422ae LP |
354 | } else |
355 | return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties); | |
356 | ||
357 | if (reply) { | |
358 | if (!dbus_connection_send(connection, reply, NULL)) | |
359 | goto oom; | |
360 | ||
361 | dbus_message_unref(reply); | |
362 | } | |
363 | ||
364 | return DBUS_HANDLER_RESULT_HANDLED; | |
365 | ||
366 | oom: | |
367 | if (reply) | |
368 | dbus_message_unref(reply); | |
369 | ||
370 | dbus_error_free(&error); | |
371 | ||
372 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
3f49d45a LP |
373 | } |
374 | ||
375 | static DBusHandlerResult session_message_handler( | |
376 | DBusConnection *connection, | |
377 | DBusMessage *message, | |
378 | void *userdata) { | |
379 | ||
380 | Manager *m = userdata; | |
381 | Session *s; | |
382 | int r; | |
383 | ||
384 | r = get_session_for_path(m, dbus_message_get_path(message), &s); | |
385 | if (r < 0) { | |
386 | ||
387 | if (r == -ENOMEM) | |
388 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
389 | ||
390 | if (r == -ENOENT) { | |
391 | DBusError e; | |
392 | ||
393 | dbus_error_init(&e); | |
394 | dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session"); | |
395 | return bus_send_error_reply(connection, message, &e, r); | |
396 | } | |
397 | ||
398 | return bus_send_error_reply(connection, message, NULL, r); | |
399 | } | |
400 | ||
401 | return session_message_dispatch(s, connection, message); | |
402 | } | |
403 | ||
404 | const DBusObjectPathVTable bus_session_vtable = { | |
405 | .message_function = session_message_handler | |
406 | }; | |
407 | ||
408 | char *session_bus_path(Session *s) { | |
409 | char *t, *r; | |
410 | ||
411 | assert(s); | |
412 | ||
413 | t = bus_path_escape(s->id); | |
414 | if (!t) | |
415 | return NULL; | |
416 | ||
417 | r = strappend("/org/freedesktop/login1/session/", t); | |
418 | free(t); | |
419 | ||
420 | return r; | |
421 | } | |
da119395 LP |
422 | |
423 | int session_send_signal(Session *s, bool new_session) { | |
424 | DBusMessage *m; | |
425 | int r = -ENOMEM; | |
426 | char *p = NULL; | |
427 | ||
428 | assert(s); | |
429 | ||
430 | m = dbus_message_new_signal("/org/freedesktop/login1", | |
431 | "org.freedesktop.login1.Manager", | |
432 | new_session ? "SessionNew" : "SessionRemoved"); | |
433 | ||
434 | if (!m) | |
435 | return -ENOMEM; | |
436 | ||
437 | p = session_bus_path(s); | |
438 | if (!p) | |
439 | goto finish; | |
440 | ||
441 | if (!dbus_message_append_args( | |
442 | m, | |
443 | DBUS_TYPE_STRING, &s->id, | |
444 | DBUS_TYPE_OBJECT_PATH, &p, | |
445 | DBUS_TYPE_INVALID)) | |
446 | goto finish; | |
447 | ||
448 | if (!dbus_connection_send(s->manager->bus, m, NULL)) | |
449 | goto finish; | |
450 | ||
451 | r = 0; | |
452 | ||
453 | finish: | |
454 | dbus_message_unref(m); | |
455 | free(p); | |
456 | ||
457 | return r; | |
458 | } | |
9418f147 LP |
459 | |
460 | int session_send_changed(Session *s, const char *properties) { | |
461 | DBusMessage *m; | |
462 | int r = -ENOMEM; | |
463 | char *p = NULL; | |
464 | ||
465 | assert(s); | |
466 | ||
ed18b08b LP |
467 | if (!s->started) |
468 | return 0; | |
469 | ||
9418f147 LP |
470 | p = session_bus_path(s); |
471 | if (!p) | |
472 | return -ENOMEM; | |
473 | ||
474 | m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties); | |
475 | if (!m) | |
476 | goto finish; | |
477 | ||
478 | if (!dbus_connection_send(s->manager->bus, m, NULL)) | |
479 | goto finish; | |
480 | ||
481 | r = 0; | |
482 | ||
483 | finish: | |
484 | if (m) | |
485 | dbus_message_unref(m); | |
486 | free(p); | |
487 | ||
488 | return r; | |
489 | } | |
88e3dc90 LP |
490 | |
491 | int session_send_lock(Session *s, bool lock) { | |
492 | DBusMessage *m; | |
493 | bool b; | |
494 | char *p; | |
495 | ||
496 | assert(s); | |
497 | ||
498 | p = session_bus_path(s); | |
499 | if (!p) | |
500 | return -ENOMEM; | |
501 | ||
502 | m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock"); | |
503 | free(p); | |
504 | ||
505 | if (!m) | |
506 | return -ENOMEM; | |
507 | ||
508 | b = dbus_connection_send(s->manager->bus, m, NULL); | |
509 | dbus_message_unref(m); | |
510 | ||
511 | if (!b) | |
512 | return -ENOMEM; | |
513 | ||
514 | return 0; | |
515 | } |