]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/logind-session-dbus.c
mechanisms: add mechanisms to change system locale and clock
[thirdparty/systemd.git] / src / logind-session-dbus.c
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>
23
24 #include "logind.h"
25 #include "logind-session.h"
26 #include "dbus-common.h"
27 #include "util.h"
28
29 #define BUS_SESSION_INTERFACE \
30 " <interface name=\"org.freedesktop.login1.Session\">\n" \
31 " <method name=\"Terminate\"/>\n" \
32 " <method name=\"Activate\"/>\n" \
33 " <method name=\"Lock\"/>\n" \
34 " <method name=\"Unlock\"/>\n" \
35 " <method name=\"SetIdleHint\">\n" \
36 " <arg name=\"b\" type=\"b\"/>\n" \
37 " </method>\n" \
38 " <property name=\"Id\" type=\"u\" access=\"read\"/>\n" \
39 " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \
40 " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
42 " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
43 " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
44 " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \
45 " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \
46 " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \
47 " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \
48 " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \
49 " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
50 " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
51 " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \
52 " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
53 " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
54 " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
55 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
56 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
57 " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
58 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
59 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
60 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
61 " </interface>\n"
62
63 #define INTROSPECTION \
64 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
65 "<node>\n" \
66 BUS_SESSION_INTERFACE \
67 BUS_PROPERTIES_INTERFACE \
68 BUS_PEER_INTERFACE \
69 BUS_INTROSPECTABLE_INTERFACE \
70 "</node>\n"
71
72 #define INTERFACES_LIST \
73 BUS_GENERIC_INTERFACES_LIST \
74 "org.freedesktop.login1.Session\0"
75
76 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
77 DBusMessageIter sub;
78 Session *s = data;
79 const char *id, *path;
80 char *p = NULL;
81
82 assert(i);
83 assert(property);
84 assert(s);
85
86 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
87 return -ENOMEM;
88
89 if (s->seat) {
90 id = s->seat->id;
91 path = p = seat_bus_path(s->seat);
92
93 if (!p)
94 return -ENOMEM;
95 } else {
96 id = "";
97 path = "/";
98 }
99
100 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
101 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
102 free(p);
103 return -ENOMEM;
104 }
105
106 free(p);
107
108 if (!dbus_message_iter_close_container(i, &sub))
109 return -ENOMEM;
110
111 return 0;
112 }
113
114 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
115 DBusMessageIter sub;
116 Session *s = data;
117 char *p = NULL;
118
119 assert(i);
120 assert(property);
121 assert(s);
122
123 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
124 return -ENOMEM;
125
126 p = user_bus_path(s->user);
127 if (!p)
128 return -ENOMEM;
129
130 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) ||
131 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
132 free(p);
133 return -ENOMEM;
134 }
135
136 free(p);
137
138 if (!dbus_message_iter_close_container(i, &sub))
139 return -ENOMEM;
140
141 return 0;
142 }
143
144 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
145 Session *s = data;
146 bool b;
147
148 assert(i);
149 assert(property);
150 assert(s);
151
152 b = session_is_active(s);
153 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
154 return -ENOMEM;
155
156 return 0;
157 }
158
159 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
160
161 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
162 Session *s;
163 char *id;
164
165 assert(m);
166 assert(path);
167 assert(_s);
168
169 if (!startswith(path, "/org/freedesktop/login1/session/"))
170 return -EINVAL;
171
172 id = bus_path_unescape(path + 32);
173 if (!id)
174 return -ENOMEM;
175
176 s = hashmap_get(m->sessions, id);
177 free(id);
178
179 if (!s)
180 return -ENOENT;
181
182 *_s = s;
183 return 0;
184 }
185
186 static DBusHandlerResult session_message_dispatch(
187 Session *s,
188 DBusConnection *connection,
189 DBusMessage *message) {
190
191 const BusProperty properties[] = {
192 { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id },
193 { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s },
194 { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name },
195 { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path },
196 { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr },
197 { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s },
198 { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty },
199 { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display },
200 { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote },
201 { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user },
202 { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host },
203 { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader },
204 { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id },
205 { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type },
206 { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s },
207 { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers },
208 { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers },
209 { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes },
210 { NULL, NULL, NULL, NULL, NULL }
211 };
212
213 assert(s);
214 assert(connection);
215 assert(message);
216
217 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
218 }
219
220 static DBusHandlerResult session_message_handler(
221 DBusConnection *connection,
222 DBusMessage *message,
223 void *userdata) {
224
225 Manager *m = userdata;
226 Session *s;
227 int r;
228
229 r = get_session_for_path(m, dbus_message_get_path(message), &s);
230 if (r < 0) {
231
232 if (r == -ENOMEM)
233 return DBUS_HANDLER_RESULT_NEED_MEMORY;
234
235 if (r == -ENOENT) {
236 DBusError e;
237
238 dbus_error_init(&e);
239 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
240 return bus_send_error_reply(connection, message, &e, r);
241 }
242
243 return bus_send_error_reply(connection, message, NULL, r);
244 }
245
246 return session_message_dispatch(s, connection, message);
247 }
248
249 const DBusObjectPathVTable bus_session_vtable = {
250 .message_function = session_message_handler
251 };
252
253 char *session_bus_path(Session *s) {
254 char *t, *r;
255
256 assert(s);
257
258 t = bus_path_escape(s->id);
259 if (!t)
260 return NULL;
261
262 r = strappend("/org/freedesktop/login1/session/", t);
263 free(t);
264
265 return r;
266 }