chiark / gitweb /
mac: also rename use_{smack,selinux,apparmor}() calls so that they share the new...
[elogind.git] / src / login / logind-device.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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <string.h>
24
25 #include "util.h"
26 #include "logind-device.h"
27
28 Device* device_new(Manager *m, const char *sysfs, bool master) {
29         Device *d;
30
31         assert(m);
32         assert(sysfs);
33
34         d = new0(Device, 1);
35         if (!d)
36                 return NULL;
37
38         d->sysfs = strdup(sysfs);
39         if (!d->sysfs) {
40                 free(d);
41                 return NULL;
42         }
43
44         if (hashmap_put(m->devices, d->sysfs, d) < 0) {
45                 free(d->sysfs);
46                 free(d);
47                 return NULL;
48         }
49
50         d->manager = m;
51         d->master = master;
52         dual_timestamp_get(&d->timestamp);
53
54         return d;
55 }
56
57 static void device_detach(Device *d) {
58         Seat *s;
59         SessionDevice *sd;
60
61         assert(d);
62
63         if (!d->seat)
64                 return;
65
66         while ((sd = d->session_devices))
67                 session_device_free(sd);
68
69         s = d->seat;
70         LIST_REMOVE(devices, d->seat->devices, d);
71         d->seat = NULL;
72
73         if (!seat_has_master_device(s)) {
74                 seat_add_to_gc_queue(s);
75                 seat_send_changed(s, "CanGraphical", NULL);
76         }
77 }
78
79 void device_free(Device *d) {
80         assert(d);
81
82         device_detach(d);
83
84         hashmap_remove(d->manager->devices, d->sysfs);
85
86         free(d->sysfs);
87         free(d);
88 }
89
90 void device_attach(Device *d, Seat *s) {
91         Device *i;
92         bool had_master;
93
94         assert(d);
95         assert(s);
96
97         if (d->seat == s)
98                 return;
99
100         if (d->seat)
101                 device_detach(d);
102
103         d->seat = s;
104         had_master = seat_has_master_device(s);
105
106         /* We keep the device list sorted by the "master" flag. That is, master
107          * devices are at the front, other devices at the tail. As there is no
108          * way to easily add devices at the list-tail, we need to iterate the
109          * list to find the first non-master device when adding non-master
110          * devices. We assume there is only a few (normally 1) master devices
111          * per seat, so we iterate only a few times. */
112
113         if (d->master || !s->devices)
114                 LIST_PREPEND(devices, s->devices, d);
115         else {
116                 LIST_FOREACH(devices, i, s->devices) {
117                         if (!i->devices_next || !i->master) {
118                                 LIST_INSERT_AFTER(devices, s->devices, i, d);
119                                 break;
120                         }
121                 }
122         }
123
124         if (!had_master && d->master)
125                 seat_send_changed(s, "CanGraphical", NULL);
126 }