]>
Commit | Line | Data |
---|---|---|
20263082 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> | |
23 | #include <pwd.h> | |
24 | #include <libudev.h> | |
25 | #include <fcntl.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/epoll.h> | |
5eda94dd LP |
29 | #include <sys/ioctl.h> |
30 | #include <linux/vt.h> | |
20263082 LP |
31 | |
32 | #include "logind.h" | |
33 | #include "dbus-common.h" | |
34 | #include "dbus-loop.h" | |
35 | ||
20263082 LP |
36 | Manager *manager_new(void) { |
37 | Manager *m; | |
38 | ||
39 | m = new0(Manager, 1); | |
40 | if (!m) | |
41 | return NULL; | |
42 | ||
43 | m->console_active_fd = -1; | |
44 | m->bus_fd = -1; | |
45 | m->udev_fd = -1; | |
46 | m->epoll_fd = -1; | |
47 | m->n_autovts = 6; | |
48 | ||
49 | m->devices = hashmap_new(string_hash_func, string_compare_func); | |
50 | m->seats = hashmap_new(string_hash_func, string_compare_func); | |
51 | m->sessions = hashmap_new(string_hash_func, string_compare_func); | |
52 | m->users = hashmap_new(trivial_hash_func, trivial_compare_func); | |
1713813d | 53 | m->cgroups = hashmap_new(string_hash_func, string_compare_func); |
31b79c2b | 54 | m->pipe_fds = hashmap_new(trivial_hash_func, trivial_compare_func); |
20263082 LP |
55 | |
56 | if (!m->devices || !m->seats || !m->sessions || !m->users) { | |
57 | manager_free(m); | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | m->udev = udev_new(); | |
62 | if (!m->udev) { | |
63 | manager_free(m); | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | if (cg_get_user_path(&m->cgroup_path) < 0) { | |
68 | manager_free(m); | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | return m; | |
73 | } | |
74 | ||
75 | void manager_free(Manager *m) { | |
76 | Session *session; | |
77 | User *u; | |
78 | Device *d; | |
79 | Seat *s; | |
80 | ||
81 | assert(m); | |
82 | ||
83 | while ((session = hashmap_first(m->sessions))) | |
84 | session_free(session); | |
85 | ||
86 | while ((u = hashmap_first(m->users))) | |
87 | user_free(u); | |
88 | ||
89 | while ((d = hashmap_first(m->devices))) | |
90 | device_free(d); | |
91 | ||
92 | while ((s = hashmap_first(m->seats))) | |
93 | seat_free(s); | |
94 | ||
95 | hashmap_free(m->sessions); | |
96 | hashmap_free(m->users); | |
97 | hashmap_free(m->devices); | |
98 | hashmap_free(m->seats); | |
1713813d | 99 | hashmap_free(m->cgroups); |
31b79c2b | 100 | hashmap_free(m->pipe_fds); |
20263082 LP |
101 | |
102 | if (m->console_active_fd >= 0) | |
103 | close_nointr_nofail(m->console_active_fd); | |
104 | ||
105 | if (m->udev_monitor) | |
106 | udev_monitor_unref(m->udev_monitor); | |
107 | ||
108 | if (m->udev) | |
109 | udev_unref(m->udev); | |
110 | ||
111 | if (m->bus) { | |
112 | dbus_connection_flush(m->bus); | |
113 | dbus_connection_close(m->bus); | |
114 | dbus_connection_unref(m->bus); | |
115 | } | |
116 | ||
117 | if (m->bus_fd >= 0) | |
118 | close_nointr_nofail(m->bus_fd); | |
119 | ||
120 | if (m->epoll_fd >= 0) | |
121 | close_nointr_nofail(m->epoll_fd); | |
122 | ||
123 | free(m->cgroup_path); | |
124 | free(m); | |
125 | } | |
126 | ||
127 | int manager_add_device(Manager *m, const char *sysfs, Device **_device) { | |
128 | Device *d; | |
129 | ||
130 | assert(m); | |
131 | assert(sysfs); | |
132 | ||
133 | d = hashmap_get(m->devices, sysfs); | |
134 | if (d) { | |
135 | if (_device) | |
136 | *_device = d; | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | d = device_new(m, sysfs); | |
142 | if (!d) | |
143 | return -ENOMEM; | |
144 | ||
145 | if (_device) | |
146 | *_device = d; | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | int manager_add_seat(Manager *m, const char *id, Seat **_seat) { | |
152 | Seat *s; | |
153 | ||
154 | assert(m); | |
155 | assert(id); | |
156 | ||
157 | s = hashmap_get(m->seats, id); | |
158 | if (s) { | |
159 | if (_seat) | |
160 | *_seat = s; | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
165 | s = seat_new(m, id); | |
166 | if (!s) | |
167 | return -ENOMEM; | |
168 | ||
169 | if (_seat) | |
170 | *_seat = s; | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | int manager_add_session(Manager *m, User *u, const char *id, Session **_session) { | |
176 | Session *s; | |
177 | ||
178 | assert(m); | |
179 | assert(id); | |
180 | ||
181 | s = hashmap_get(m->sessions, id); | |
182 | if (s) { | |
183 | if (_session) | |
184 | *_session = s; | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | s = session_new(m, u, id); | |
190 | if (!s) | |
191 | return -ENOMEM; | |
192 | ||
193 | if (_session) | |
194 | *_session = s; | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
199 | int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) { | |
200 | User *u; | |
20263082 LP |
201 | |
202 | assert(m); | |
203 | assert(name); | |
204 | ||
205 | u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid)); | |
206 | if (u) { | |
207 | if (_user) | |
208 | *_user = u; | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
213 | u = user_new(m, uid, gid, name); | |
214 | if (!u) | |
215 | return -ENOMEM; | |
216 | ||
14c3baca LP |
217 | if (_user) |
218 | *_user = u; | |
20263082 | 219 | |
14c3baca | 220 | return 0; |
20263082 LP |
221 | } |
222 | ||
223 | int manager_add_user_by_name(Manager *m, const char *name, User **_user) { | |
224 | struct passwd *p; | |
225 | ||
226 | assert(m); | |
227 | assert(name); | |
228 | ||
229 | errno = 0; | |
230 | p = getpwnam(name); | |
231 | if (!p) | |
232 | return errno ? -errno : -ENOENT; | |
233 | ||
234 | return manager_add_user(m, p->pw_uid, p->pw_gid, name, _user); | |
235 | } | |
236 | ||
237 | int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) { | |
238 | struct passwd *p; | |
239 | ||
240 | assert(m); | |
241 | ||
242 | errno = 0; | |
243 | p = getpwuid(uid); | |
244 | if (!p) | |
245 | return errno ? -errno : -ENOENT; | |
246 | ||
247 | return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user); | |
248 | } | |
249 | ||
250 | int manager_process_device(Manager *m, struct udev_device *d) { | |
20263082 LP |
251 | Device *device; |
252 | int r; | |
253 | ||
254 | assert(m); | |
255 | ||
3f49d45a LP |
256 | /* FIXME: drop this check as soon as libudev's enum support |
257 | * honours tags and subsystem matches at the same time */ | |
258 | if (!streq_ptr(udev_device_get_subsystem(d), "graphics")) | |
259 | return 0; | |
260 | ||
14c3baca | 261 | if (streq_ptr(udev_device_get_action(d), "remove")) { |
20263082 | 262 | |
3f49d45a LP |
263 | /* FIXME: use syspath instead of sysname here, as soon as fb driver is fixed */ |
264 | device = hashmap_get(m->devices, udev_device_get_sysname(d)); | |
14c3baca LP |
265 | if (!device) |
266 | return 0; | |
20263082 | 267 | |
14c3baca LP |
268 | seat_add_to_gc_queue(device->seat); |
269 | device_free(device); | |
270 | ||
271 | } else { | |
272 | const char *sn; | |
273 | Seat *seat; | |
274 | ||
3f49d45a | 275 | sn = udev_device_get_property_value(d, "ID_SEAT"); |
14c3baca LP |
276 | if (!sn) |
277 | sn = "seat0"; | |
278 | ||
3f49d45a LP |
279 | if (!seat_name_is_valid(sn)) { |
280 | log_warning("Device with invalid seat name %s found, ignoring.", sn); | |
281 | return 0; | |
282 | } | |
14c3baca | 283 | |
3f49d45a | 284 | r = manager_add_device(m, udev_device_get_sysname(d), &device); |
14c3baca LP |
285 | if (r < 0) |
286 | return r; | |
287 | ||
288 | r = manager_add_seat(m, sn, &seat); | |
289 | if (r < 0) { | |
290 | if (!device->seat) | |
291 | device_free(device); | |
292 | ||
293 | return r; | |
294 | } | |
295 | ||
296 | device_attach(device, seat); | |
3f49d45a | 297 | seat_start(seat); |
14c3baca | 298 | } |
20263082 | 299 | |
20263082 LP |
300 | return 0; |
301 | } | |
302 | ||
303 | int manager_enumerate_devices(Manager *m) { | |
304 | struct udev_list_entry *item = NULL, *first = NULL; | |
305 | struct udev_enumerate *e; | |
14c3baca | 306 | int r; |
20263082 LP |
307 | |
308 | assert(m); | |
309 | ||
310 | /* Loads devices from udev and creates seats for them as | |
311 | * necessary */ | |
312 | ||
313 | e = udev_enumerate_new(m->udev); | |
14c3baca LP |
314 | if (!e) { |
315 | r = -ENOMEM; | |
5eda94dd | 316 | goto finish; |
14c3baca | 317 | } |
20263082 | 318 | |
14c3baca LP |
319 | r = udev_enumerate_add_match_subsystem(e, "graphics"); |
320 | if (r < 0) | |
20263082 LP |
321 | goto finish; |
322 | ||
14c3baca LP |
323 | r = udev_enumerate_add_match_tag(e, "seat"); |
324 | if (r < 0) | |
20263082 LP |
325 | goto finish; |
326 | ||
14c3baca LP |
327 | r = udev_enumerate_scan_devices(e); |
328 | if (r < 0) | |
20263082 LP |
329 | goto finish; |
330 | ||
331 | first = udev_enumerate_get_list_entry(e); | |
332 | udev_list_entry_foreach(item, first) { | |
333 | struct udev_device *d; | |
334 | int k; | |
335 | ||
336 | d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item)); | |
14c3baca LP |
337 | if (!d) { |
338 | r = -ENOMEM; | |
20263082 | 339 | goto finish; |
14c3baca | 340 | } |
20263082 LP |
341 | |
342 | k = manager_process_device(m, d); | |
343 | udev_device_unref(d); | |
344 | ||
345 | if (k < 0) | |
346 | r = k; | |
347 | } | |
348 | ||
349 | finish: | |
350 | if (e) | |
351 | udev_enumerate_unref(e); | |
352 | ||
353 | return r; | |
354 | } | |
355 | ||
356 | int manager_enumerate_seats(Manager *m) { | |
357 | DIR *d; | |
358 | struct dirent *de; | |
359 | int r = 0; | |
360 | ||
361 | assert(m); | |
362 | ||
363 | /* This loads data about seats stored on disk, but does not | |
364 | * actually create any seats. Removes data of seats that no | |
365 | * longer exist. */ | |
366 | ||
367 | d = opendir("/run/systemd/seats"); | |
368 | if (!d) { | |
369 | if (errno == ENOENT) | |
370 | return 0; | |
371 | ||
372 | log_error("Failed to open /run/systemd/seats: %m"); | |
373 | return -errno; | |
374 | } | |
375 | ||
376 | while ((de = readdir(d))) { | |
377 | Seat *s; | |
378 | int k; | |
379 | ||
380 | if (!dirent_is_file(de)) | |
381 | continue; | |
382 | ||
383 | s = hashmap_get(m->seats, de->d_name); | |
384 | if (!s) { | |
385 | unlinkat(dirfd(d), de->d_name, 0); | |
386 | continue; | |
387 | } | |
388 | ||
389 | k = seat_load(s); | |
390 | if (k < 0) | |
391 | r = k; | |
392 | } | |
393 | ||
394 | closedir(d); | |
395 | ||
396 | return r; | |
397 | } | |
398 | ||
399 | static int manager_enumerate_users_from_cgroup(Manager *m) { | |
400 | int r = 0; | |
401 | char *name; | |
402 | DIR *d; | |
403 | ||
404 | r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_path, &d); | |
405 | if (r < 0) { | |
406 | if (r == -ENOENT) | |
407 | return 0; | |
408 | ||
409 | log_error("Failed to open %s: %s", m->cgroup_path, strerror(-r)); | |
410 | return r; | |
411 | } | |
412 | ||
413 | while ((r = cg_read_subgroup(d, &name)) > 0) { | |
414 | User *user; | |
14c3baca LP |
415 | int k; |
416 | ||
417 | k = manager_add_user_by_name(m, name, &user); | |
418 | if (k < 0) { | |
419 | free(name); | |
420 | r = k; | |
421 | continue; | |
422 | } | |
423 | ||
424 | user_add_to_gc_queue(user); | |
425 | ||
426 | if (!user->cgroup_path) | |
427 | if (asprintf(&user->cgroup_path, "%s/%s", m->cgroup_path, name) < 0) { | |
428 | r = -ENOMEM; | |
429 | free(name); | |
430 | break; | |
431 | } | |
20263082 | 432 | |
20263082 | 433 | free(name); |
14c3baca LP |
434 | } |
435 | ||
436 | closedir(d); | |
437 | ||
438 | return r; | |
439 | } | |
20263082 | 440 | |
20263082 | 441 | |
14c3baca LP |
442 | static int manager_enumerate_linger_users(Manager *m) { |
443 | DIR *d; | |
444 | struct dirent *de; | |
445 | int r = 0; | |
446 | ||
447 | d = opendir("/var/lib/systemd/linger"); | |
448 | if (!d) { | |
449 | if (errno == ENOENT) | |
450 | return 0; | |
451 | ||
452 | log_error("Failed to open /var/lib/systemd/linger/: %m"); | |
453 | return -errno; | |
454 | } | |
455 | ||
456 | while ((de = readdir(d))) { | |
457 | int k; | |
458 | ||
459 | if (!dirent_is_file(de)) | |
20263082 LP |
460 | continue; |
461 | ||
14c3baca LP |
462 | k = manager_add_user_by_name(m, de->d_name, NULL); |
463 | if (k < 0) { | |
464 | log_notice("Couldn't add lingering user %s: %s", de->d_name, strerror(-k)); | |
465 | r = k; | |
20263082 LP |
466 | } |
467 | } | |
468 | ||
14c3baca | 469 | closedir(d); |
20263082 LP |
470 | |
471 | return r; | |
472 | } | |
473 | ||
474 | int manager_enumerate_users(Manager *m) { | |
475 | DIR *d; | |
476 | struct dirent *de; | |
14c3baca | 477 | int r, k; |
20263082 LP |
478 | |
479 | assert(m); | |
480 | ||
14c3baca | 481 | /* First, enumerate user cgroups */ |
20263082 LP |
482 | r = manager_enumerate_users_from_cgroup(m); |
483 | ||
14c3baca LP |
484 | /* Second, add lingering users on top */ |
485 | k = manager_enumerate_linger_users(m); | |
486 | if (k < 0) | |
487 | r = k; | |
488 | ||
489 | /* Third, read in user data stored on disk */ | |
20263082 LP |
490 | d = opendir("/run/systemd/users"); |
491 | if (!d) { | |
492 | if (errno == ENOENT) | |
493 | return 0; | |
494 | ||
495 | log_error("Failed to open /run/systemd/users: %m"); | |
496 | return -errno; | |
497 | } | |
498 | ||
499 | while ((de = readdir(d))) { | |
500 | unsigned long ul; | |
501 | User *u; | |
20263082 LP |
502 | |
503 | if (!dirent_is_file(de)) | |
504 | continue; | |
505 | ||
506 | k = safe_atolu(de->d_name, &ul); | |
507 | if (k < 0) { | |
508 | log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k)); | |
509 | continue; | |
510 | } | |
511 | ||
512 | u = hashmap_get(m->users, ULONG_TO_PTR(ul)); | |
513 | if (!u) { | |
514 | unlinkat(dirfd(d), de->d_name, 0); | |
515 | continue; | |
516 | } | |
517 | ||
518 | k = user_load(u); | |
519 | if (k < 0) | |
520 | r = k; | |
521 | } | |
522 | ||
523 | closedir(d); | |
524 | ||
525 | return r; | |
526 | } | |
527 | ||
528 | static int manager_enumerate_sessions_from_cgroup(Manager *m) { | |
529 | User *u; | |
530 | Iterator i; | |
531 | int r = 0; | |
532 | ||
533 | HASHMAP_FOREACH(u, m->users, i) { | |
534 | DIR *d; | |
535 | char *name; | |
536 | int k; | |
537 | ||
14c3baca LP |
538 | if (!u->cgroup_path) |
539 | continue; | |
540 | ||
20263082 LP |
541 | k = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &d); |
542 | if (k < 0) { | |
543 | if (k == -ENOENT) | |
544 | continue; | |
545 | ||
546 | log_error("Failed to open %s: %s", u->cgroup_path, strerror(-k)); | |
547 | r = k; | |
548 | continue; | |
549 | } | |
550 | ||
551 | while ((k = cg_read_subgroup(d, &name)) > 0) { | |
552 | Session *session; | |
553 | ||
554 | k = manager_add_session(m, u, name, &session); | |
14c3baca LP |
555 | if (k < 0) { |
556 | free(name); | |
20263082 | 557 | break; |
14c3baca | 558 | } |
20263082 | 559 | |
14c3baca | 560 | session_add_to_gc_queue(session); |
20263082 | 561 | |
14c3baca LP |
562 | if (!session->cgroup_path) |
563 | if (asprintf(&session->cgroup_path, "%s/%s", u->cgroup_path, name) < 0) { | |
564 | k = -ENOMEM; | |
565 | free(name); | |
566 | break; | |
567 | } | |
568 | ||
569 | free(name); | |
20263082 LP |
570 | } |
571 | ||
572 | closedir(d); | |
573 | ||
574 | if (k < 0) | |
575 | r = k; | |
576 | } | |
577 | ||
578 | return r; | |
579 | } | |
580 | ||
581 | int manager_enumerate_sessions(Manager *m) { | |
582 | DIR *d; | |
583 | struct dirent *de; | |
584 | int r = 0; | |
585 | ||
586 | assert(m); | |
587 | ||
14c3baca | 588 | /* First enumerate session cgroups */ |
20263082 LP |
589 | r = manager_enumerate_sessions_from_cgroup(m); |
590 | ||
14c3baca | 591 | /* Second, read in session data stored on disk */ |
20263082 LP |
592 | d = opendir("/run/systemd/sessions"); |
593 | if (!d) { | |
594 | if (errno == ENOENT) | |
595 | return 0; | |
596 | ||
597 | log_error("Failed to open /run/systemd/sessions: %m"); | |
598 | return -errno; | |
599 | } | |
600 | ||
601 | while ((de = readdir(d))) { | |
602 | struct Session *s; | |
603 | int k; | |
604 | ||
605 | if (!dirent_is_file(de)) | |
606 | continue; | |
607 | ||
608 | s = hashmap_get(m->sessions, de->d_name); | |
609 | if (!s) { | |
610 | unlinkat(dirfd(d), de->d_name, 0); | |
611 | continue; | |
612 | } | |
613 | ||
614 | k = session_load(s); | |
615 | if (k < 0) | |
616 | r = k; | |
617 | } | |
618 | ||
619 | closedir(d); | |
620 | ||
621 | return r; | |
622 | } | |
623 | ||
20263082 LP |
624 | int manager_dispatch_udev(Manager *m) { |
625 | struct udev_device *d; | |
626 | int r; | |
627 | ||
628 | assert(m); | |
629 | ||
630 | d = udev_monitor_receive_device(m->udev_monitor); | |
631 | if (!d) | |
632 | return -ENOMEM; | |
633 | ||
634 | r = manager_process_device(m, d); | |
635 | udev_device_unref(d); | |
636 | ||
637 | return r; | |
638 | } | |
639 | ||
640 | int manager_dispatch_console(Manager *m) { | |
20263082 LP |
641 | assert(m); |
642 | ||
20263082 | 643 | if (m->vtconsole) |
14c3baca | 644 | seat_read_active_vt(m->vtconsole); |
20263082 LP |
645 | |
646 | return 0; | |
647 | } | |
648 | ||
5eda94dd LP |
649 | static int vt_is_busy(int vtnr) { |
650 | struct vt_stat vt_stat; | |
651 | int r = 0, fd; | |
652 | ||
653 | assert(vtnr >= 1); | |
654 | ||
d0a522eb LP |
655 | /* We explicitly open /dev/tty1 here instead of /dev/tty0. If |
656 | * we'd open the latter we'd open the foreground tty which | |
657 | * hence would be unconditionally busy. By opening /dev/tty1 | |
658 | * we avoid this. Since tty1 is special and needs to be an | |
659 | * explicitly loaded getty or DM this is safe. */ | |
660 | ||
661 | fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC); | |
5eda94dd LP |
662 | if (fd < 0) |
663 | return -errno; | |
664 | ||
665 | if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0) | |
666 | r = -errno; | |
667 | else | |
668 | r = !!(vt_stat.v_state & (1 << vtnr)); | |
669 | ||
670 | close_nointr_nofail(fd); | |
671 | ||
672 | return r; | |
673 | } | |
674 | ||
20263082 | 675 | int manager_spawn_autovt(Manager *m, int vtnr) { |
5eda94dd | 676 | int r; |
d0a522eb LP |
677 | DBusMessage *message = NULL, *reply = NULL; |
678 | char *name = NULL; | |
679 | const char *mode = "fail"; | |
680 | DBusError error; | |
5eda94dd | 681 | |
20263082 LP |
682 | assert(m); |
683 | ||
d0a522eb LP |
684 | dbus_error_init(&error); |
685 | ||
975fd867 LP |
686 | if (vtnr > m->n_autovts) |
687 | return 0; | |
688 | ||
5eda94dd | 689 | r = vt_is_busy(vtnr); |
975fd867 | 690 | if (r < 0) |
5eda94dd | 691 | return r; |
975fd867 LP |
692 | else if (r > 0) |
693 | return -EBUSY; | |
5eda94dd | 694 | |
d0a522eb LP |
695 | message = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"); |
696 | if (!message) { | |
697 | log_error("Could not allocate message."); | |
698 | r = -ENOMEM; | |
699 | goto finish; | |
700 | } | |
5eda94dd | 701 | |
d0a522eb LP |
702 | if (asprintf(&name, "autovt-getty@tty%i.service", vtnr) < 0) { |
703 | log_error("Could not allocate service name."); | |
704 | r = -ENOMEM; | |
705 | goto finish; | |
706 | } | |
707 | ||
708 | if (!dbus_message_append_args(message, | |
709 | DBUS_TYPE_STRING, &name, | |
710 | DBUS_TYPE_STRING, &mode, | |
711 | DBUS_TYPE_INVALID)) { | |
712 | log_error("Could not attach target and flag information to message."); | |
713 | r = -ENOMEM; | |
714 | goto finish; | |
715 | } | |
716 | ||
717 | reply = dbus_connection_send_with_reply_and_block(m->bus, message, -1, &error); | |
718 | if (!reply) { | |
719 | log_error("Failed to start unit: %s", bus_error_message(&error)); | |
720 | goto finish; | |
721 | } | |
722 | ||
723 | r = 0; | |
724 | ||
725 | finish: | |
726 | free(name); | |
727 | ||
728 | if (message) | |
729 | dbus_message_unref(message); | |
730 | ||
731 | if (reply) | |
732 | dbus_message_unref(reply); | |
733 | ||
734 | dbus_error_free(&error); | |
735 | ||
736 | return r; | |
20263082 LP |
737 | } |
738 | ||
1713813d LP |
739 | void manager_cgroup_notify_empty(Manager *m, const char *cgroup) { |
740 | Session *s; | |
741 | char *p; | |
742 | ||
743 | assert(m); | |
744 | assert(cgroup); | |
745 | ||
746 | p = strdup(cgroup); | |
747 | if (!p) { | |
748 | log_error("Out of memory."); | |
749 | return; | |
750 | } | |
751 | ||
752 | for (;;) { | |
753 | char *e; | |
20263082 | 754 | |
1713813d LP |
755 | if (isempty(p) || streq(p, "/")) |
756 | break; | |
757 | ||
758 | s = hashmap_get(m->cgroups, p); | |
759 | if (s) | |
760 | session_add_to_gc_queue(s); | |
761 | ||
762 | assert_se(e = strrchr(p, '/')); | |
763 | *e = 0; | |
764 | } | |
765 | ||
766 | free(p); | |
20263082 LP |
767 | } |
768 | ||
31b79c2b LP |
769 | static void manager_pipe_notify_eof(Manager *m, int fd) { |
770 | Session *s; | |
771 | ||
772 | assert_se(m); | |
773 | assert_se(fd >= 0); | |
774 | ||
775 | assert_se(s = hashmap_get(m->pipe_fds, INT_TO_PTR(fd + 1))); | |
776 | assert(s->pipe_fd == fd); | |
777 | session_unset_pipe_fd(s); | |
778 | ||
21308c65 | 779 | session_stop(s); |
31b79c2b LP |
780 | } |
781 | ||
20263082 | 782 | static int manager_connect_bus(Manager *m) { |
20263082 LP |
783 | DBusError error; |
784 | int r; | |
785 | struct epoll_event ev; | |
786 | ||
787 | assert(m); | |
788 | assert(!m->bus); | |
789 | assert(m->bus_fd < 0); | |
790 | ||
791 | dbus_error_init(&error); | |
792 | ||
793 | m->bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); | |
794 | if (!m->bus) { | |
a2e52832 | 795 | log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error)); |
20263082 LP |
796 | r = -ECONNREFUSED; |
797 | goto fail; | |
798 | } | |
799 | ||
3f49d45a LP |
800 | if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/login1", &bus_manager_vtable, m) || |
801 | !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/seat", &bus_seat_vtable, m) || | |
802 | !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) || | |
803 | !dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/user", &bus_user_vtable, m) || | |
1713813d | 804 | !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) { |
20263082 LP |
805 | log_error("Not enough memory"); |
806 | r = -ENOMEM; | |
807 | goto fail; | |
808 | } | |
809 | ||
810 | dbus_bus_add_match(m->bus, | |
811 | "type='signal'," | |
812 | "interface='org.freedesktop.systemd1.Agent'," | |
813 | "member='Released'," | |
814 | "path='/org/freedesktop/systemd1/agent'", | |
815 | &error); | |
816 | ||
817 | if (dbus_error_is_set(&error)) { | |
a2e52832 | 818 | log_error("Failed to register match: %s", bus_error_message(&error)); |
20263082 LP |
819 | r = -EIO; |
820 | goto fail; | |
821 | } | |
822 | ||
bafd4449 LP |
823 | r = dbus_bus_request_name(m->bus, "org.freedesktop.login1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error); |
824 | if (dbus_error_is_set(&error)) { | |
825 | log_error("Failed to register name on bus: %s", bus_error_message(&error)); | |
826 | r = -EIO; | |
827 | goto fail; | |
828 | } | |
829 | ||
830 | if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { | |
831 | log_error("Failed to acquire name."); | |
20263082 LP |
832 | r = -EEXIST; |
833 | goto fail; | |
834 | } | |
835 | ||
836 | m->bus_fd = bus_loop_open(m->bus); | |
837 | if (m->bus_fd < 0) { | |
838 | r = m->bus_fd; | |
839 | goto fail; | |
840 | } | |
841 | ||
842 | zero(ev); | |
843 | ev.events = EPOLLIN; | |
844 | ev.data.u32 = FD_BUS; | |
845 | ||
846 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->bus_fd, &ev) < 0) | |
847 | goto fail; | |
848 | ||
849 | return 0; | |
850 | ||
851 | fail: | |
852 | dbus_error_free(&error); | |
853 | ||
854 | return r; | |
855 | } | |
856 | ||
857 | static int manager_connect_console(Manager *m) { | |
858 | struct epoll_event ev; | |
859 | ||
860 | assert(m); | |
861 | assert(m->console_active_fd < 0); | |
862 | ||
863 | m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC); | |
864 | if (m->console_active_fd < 0) { | |
865 | log_error("Failed to open /sys/class/tty/tty0/active: %m"); | |
866 | return -errno; | |
867 | } | |
868 | ||
869 | zero(ev); | |
14c3baca | 870 | ev.events = 0; |
20263082 LP |
871 | ev.data.u32 = FD_CONSOLE; |
872 | ||
873 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->console_active_fd, &ev) < 0) | |
874 | return -errno; | |
875 | ||
876 | return 0; | |
877 | } | |
878 | ||
879 | static int manager_connect_udev(Manager *m) { | |
880 | struct epoll_event ev; | |
14c3baca | 881 | int r; |
20263082 LP |
882 | |
883 | assert(m); | |
884 | assert(!m->udev_monitor); | |
885 | ||
886 | m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); | |
887 | if (!m->udev_monitor) | |
888 | return -ENOMEM; | |
889 | ||
14c3baca LP |
890 | r = udev_monitor_filter_add_match_tag(m->udev_monitor, "seat"); |
891 | if (r < 0) | |
892 | return r; | |
20263082 | 893 | |
14c3baca LP |
894 | r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "graphics", NULL); |
895 | if (r < 0) | |
896 | return r; | |
20263082 | 897 | |
14c3baca LP |
898 | r = udev_monitor_enable_receiving(m->udev_monitor); |
899 | if (r < 0) | |
900 | return r; | |
20263082 LP |
901 | |
902 | m->udev_fd = udev_monitor_get_fd(m->udev_monitor); | |
903 | ||
904 | zero(ev); | |
905 | ev.events = EPOLLIN; | |
906 | ev.data.u32 = FD_UDEV; | |
907 | ||
908 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_fd, &ev) < 0) | |
909 | return -errno; | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
14c3baca LP |
914 | void manager_gc(Manager *m) { |
915 | Seat *seat; | |
916 | Session *session; | |
917 | User *user; | |
918 | ||
919 | assert(m); | |
920 | ||
921 | while ((seat = m->seat_gc_queue)) { | |
922 | LIST_REMOVE(Seat, gc_queue, m->seat_gc_queue, seat); | |
923 | seat->in_gc_queue = false; | |
924 | ||
925 | if (seat_check_gc(seat) == 0) { | |
926 | seat_stop(seat); | |
927 | seat_free(seat); | |
928 | } | |
929 | } | |
930 | ||
931 | while ((session = m->session_gc_queue)) { | |
932 | LIST_REMOVE(Session, gc_queue, m->session_gc_queue, session); | |
933 | session->in_gc_queue = false; | |
934 | ||
935 | if (session_check_gc(session) == 0) { | |
936 | session_stop(session); | |
937 | session_free(session); | |
938 | } | |
939 | } | |
940 | ||
941 | while ((user = m->user_gc_queue)) { | |
942 | LIST_REMOVE(User, gc_queue, m->user_gc_queue, user); | |
943 | user->in_gc_queue = false; | |
944 | ||
945 | if (user_check_gc(user) == 0) { | |
946 | user_stop(user); | |
947 | user_free(user); | |
948 | } | |
949 | } | |
950 | } | |
951 | ||
a185c5aa LP |
952 | int manager_get_idle_hint(Manager *m, dual_timestamp *t) { |
953 | Session *s; | |
954 | bool idle_hint = true; | |
955 | dual_timestamp ts = { 0, 0 }; | |
956 | Iterator i; | |
957 | ||
958 | assert(m); | |
959 | ||
960 | HASHMAP_FOREACH(s, m->sessions, i) { | |
961 | dual_timestamp k; | |
962 | int ih; | |
963 | ||
964 | ih = session_get_idle_hint(s, &k); | |
965 | if (ih < 0) | |
966 | return ih; | |
967 | ||
968 | if (!ih) { | |
969 | if (!idle_hint) { | |
970 | if (k.monotonic < ts.monotonic) | |
971 | ts = k; | |
972 | } else { | |
973 | idle_hint = false; | |
974 | ts = k; | |
975 | } | |
976 | } else if (idle_hint) { | |
977 | ||
978 | if (k.monotonic > ts.monotonic) | |
979 | ts = k; | |
980 | } | |
981 | } | |
982 | ||
983 | if (t) | |
984 | *t = ts; | |
985 | ||
986 | return idle_hint; | |
987 | } | |
988 | ||
20263082 LP |
989 | int manager_startup(Manager *m) { |
990 | int r; | |
14c3baca LP |
991 | Seat *seat; |
992 | Session *session; | |
993 | User *user; | |
994 | Iterator i; | |
20263082 LP |
995 | |
996 | assert(m); | |
997 | assert(m->epoll_fd <= 0); | |
998 | ||
999 | m->epoll_fd = epoll_create1(EPOLL_CLOEXEC); | |
1000 | if (m->epoll_fd < 0) | |
1001 | return -errno; | |
1002 | ||
1003 | /* Connect to udev */ | |
1004 | r = manager_connect_udev(m); | |
1005 | if (r < 0) | |
1006 | return r; | |
1007 | ||
1008 | /* Connect to console */ | |
1009 | r = manager_connect_console(m); | |
1010 | if (r < 0) | |
1011 | return r; | |
1012 | ||
1013 | /* Connect to the bus */ | |
1014 | r = manager_connect_bus(m); | |
1015 | if (r < 0) | |
1016 | return r; | |
1017 | ||
14c3baca LP |
1018 | /* Instantiate magic seat 0 */ |
1019 | r = manager_add_seat(m, "seat0", &m->vtconsole); | |
1020 | if (r < 0) | |
1021 | return r; | |
1022 | ||
20263082 LP |
1023 | /* Deserialize state */ |
1024 | manager_enumerate_devices(m); | |
1025 | manager_enumerate_seats(m); | |
1026 | manager_enumerate_users(m); | |
1027 | manager_enumerate_sessions(m); | |
1028 | ||
14c3baca LP |
1029 | /* Get rid of objects that are no longer used */ |
1030 | manager_gc(m); | |
1031 | ||
1032 | /* And start everything */ | |
1033 | HASHMAP_FOREACH(seat, m->seats, i) | |
1034 | seat_start(seat); | |
1035 | ||
1036 | HASHMAP_FOREACH(user, m->users, i) | |
1037 | user_start(user); | |
1038 | ||
1039 | HASHMAP_FOREACH(session, m->sessions, i) | |
1040 | session_start(session); | |
20263082 LP |
1041 | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | int manager_run(Manager *m) { | |
1046 | assert(m); | |
1047 | ||
1048 | for (;;) { | |
1049 | struct epoll_event event; | |
1050 | int n; | |
1051 | ||
14c3baca LP |
1052 | manager_gc(m); |
1053 | ||
20263082 LP |
1054 | if (dbus_connection_dispatch(m->bus) != DBUS_DISPATCH_COMPLETE) |
1055 | continue; | |
1056 | ||
1713813d LP |
1057 | manager_gc(m); |
1058 | ||
20263082 LP |
1059 | n = epoll_wait(m->epoll_fd, &event, 1, -1); |
1060 | if (n < 0) { | |
3f49d45a LP |
1061 | if (errno == EINTR || errno == EAGAIN) |
1062 | continue; | |
1063 | ||
20263082 LP |
1064 | log_error("epoll() failed: %m"); |
1065 | return -errno; | |
1066 | } | |
1067 | ||
1068 | switch (event.data.u32) { | |
1069 | ||
1070 | case FD_UDEV: | |
1071 | manager_dispatch_udev(m); | |
1072 | break; | |
1073 | ||
1074 | case FD_CONSOLE: | |
1075 | manager_dispatch_console(m); | |
1076 | break; | |
1077 | ||
1078 | case FD_BUS: | |
1079 | bus_loop_dispatch(m->bus_fd); | |
1080 | break; | |
31b79c2b LP |
1081 | |
1082 | default: | |
1083 | if (event.data.u32 >= FD_PIPE_BASE) | |
1084 | manager_pipe_notify_eof(m, event.data.u32 - FD_PIPE_BASE); | |
20263082 LP |
1085 | } |
1086 | } | |
1087 | ||
1088 | return 0; | |
1089 | } | |
1090 | ||
1091 | int main(int argc, char *argv[]) { | |
1092 | Manager *m = NULL; | |
5eda94dd | 1093 | int r; |
20263082 LP |
1094 | |
1095 | log_set_target(LOG_TARGET_AUTO); | |
1096 | log_parse_environment(); | |
1097 | log_open(); | |
1098 | ||
1099 | if (argc != 1) { | |
1100 | log_error("This program takes no arguments."); | |
1101 | r = -EINVAL; | |
1102 | goto finish; | |
1103 | } | |
1104 | ||
1105 | umask(0022); | |
1106 | ||
1107 | m = manager_new(); | |
1108 | if (!m) { | |
1109 | log_error("Out of memory"); | |
1110 | r = -ENOMEM; | |
1111 | goto finish; | |
1112 | } | |
1113 | ||
1114 | r = manager_startup(m); | |
1115 | if (r < 0) { | |
1116 | log_error("Failed to fully start up daemon: %s", strerror(-r)); | |
1117 | goto finish; | |
1118 | } | |
1119 | ||
1120 | r = manager_run(m); | |
1121 | ||
1122 | finish: | |
1123 | if (m) | |
1124 | manager_free(m); | |
1125 | ||
1126 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
1127 | } |