uvtd: vt: implement VT_GETMODE/SETMODE ioctl state-tracking
[platform/upstream/kmscon.git] / src / uterm_systemd.c
1 /*
2  * uterm - Linux User-Space Terminal
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Systemd integration
28  * Systemd provides multi-seat support and other helpers that we can use in
29  * uterm.
30  */
31
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <systemd/sd-daemon.h>
38 #include <systemd/sd-login.h>
39 #include "shl_log.h"
40 #include "uterm_monitor.h"
41 #include "uterm_systemd_internal.h"
42
43 #define LOG_SUBSYSTEM "systemd"
44
45 struct uterm_sd {
46         sd_login_monitor *mon;
47 };
48
49 int uterm_sd_new(struct uterm_sd **out)
50 {
51         int ret;
52         struct uterm_sd *sd;
53
54         if (!out)
55                 return -EINVAL;
56
57         ret = sd_booted();
58         if (ret < 0) {
59                 log_warning("cannot determine whether system booted with systemd (%d): %s",
60                             ret, strerror(-ret));
61                 return -EOPNOTSUPP;
62         } else if (!ret) {
63                 log_info("system not booted with systemd, disabling multi-seat support");
64                 return -EOPNOTSUPP;
65         }
66
67         log_info("system booted with systemd, enabling multi-seat support");
68
69         sd = malloc(sizeof(*sd));
70         if (!sd)
71                 return -ENOMEM;
72         memset(sd, 0, sizeof(*sd));
73
74         ret = sd_login_monitor_new("seat", &sd->mon);
75         if (ret) {
76                 log_err("cannot create systemd login monitor (%d): %s",
77                         ret, strerror(-ret));
78                 ret = -EFAULT;
79                 goto err_free;
80         }
81
82         *out = sd;
83         return 0;
84
85 err_free:
86         free(sd);
87         return ret;
88 }
89
90 void uterm_sd_free(struct uterm_sd *sd)
91 {
92         if (!sd)
93                 return;
94
95         sd_login_monitor_unref(sd->mon);
96         free(sd);
97 }
98
99 int uterm_sd_get_fd(struct uterm_sd *sd)
100 {
101         if (!sd)
102                 return -EINVAL;
103
104         return sd_login_monitor_get_fd(sd->mon);
105 }
106
107 void uterm_sd_flush(struct uterm_sd *sd)
108 {
109         if (!sd)
110                 return;
111
112         sd_login_monitor_flush(sd->mon);
113 }
114
115 int uterm_sd_get_seats(struct uterm_sd *sd, char ***seats)
116 {
117         int ret;
118         char **s;
119
120         if (!sd || !seats)
121                 return -EINVAL;
122
123         ret = sd_get_seats(&s);
124         if (ret < 0) {
125                 log_warning("cannot read seat information from systemd: %d",
126                             ret);
127                 return -EFAULT;
128         }
129
130         *seats = s;
131         return ret;
132 }