1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* uvtd - User-space VT daemon
*
* Copyright (c) 2012-2013 David Herrmann <dh.herrmann@googlemail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signalfd.h>
#include "eloop.h"
#include "shl_dlist.h"
#include "shl_log.h"
#include "uterm_input.h"
#include "uterm_monitor.h"
#include "uvt.h"
#include "uvtd_ctx.h"
struct app_seat {
struct shl_dlist list;
struct uvtd_app *app;
struct uterm_monitor_seat *useat;
struct uvtd_ctx *ctx;
};
struct uvtd_app {
struct ev_eloop *eloop;
struct uterm_monitor *mon;
struct uvt_ctx *ctx;
struct ev_fd *ctx_fd;
struct shl_dlist seats;
};
static int app_seat_new(struct uvtd_app *app, const char *sname,
struct uterm_monitor_seat *useat)
{
struct app_seat *seat;
int ret;
seat = malloc(sizeof(*seat));
if (!seat)
return -ENOMEM;
log_debug("new seat %p on %s", seat, sname);
memset(seat, 0, sizeof(*seat));
seat->app = app;
seat->useat = useat;
ret = uvtd_ctx_new(&seat->ctx, sname, app->eloop, app->ctx);
if (ret == -EEXIST) {
log_debug("ignoring seat %s as it has real VTs", sname);
goto err_free;
} else if (ret) {
goto err_free;
}
uterm_monitor_set_seat_data(seat->useat, seat);
shl_dlist_link(&app->seats, &seat->list);
return 0;
err_free:
free(seat);
return ret;
}
static void app_seat_free(struct app_seat *seat)
{
log_debug("free seat %p", seat);
shl_dlist_unlink(&seat->list);
uterm_monitor_set_seat_data(seat->useat, NULL);
uvtd_ctx_free(seat->ctx);
free(seat);
}
static void app_monitor_event(struct uterm_monitor *mon,
struct uterm_monitor_event *ev,
void *data)
{
struct uvtd_app *app = data;
struct app_seat *seat;
int ret;
switch (ev->type) {
case UTERM_MONITOR_NEW_SEAT:
ret = app_seat_new(app, ev->seat_name, ev->seat);
if (ret)
return;
break;
case UTERM_MONITOR_FREE_SEAT:
if (ev->seat_data)
app_seat_free(ev->seat_data);
break;
case UTERM_MONITOR_NEW_DEV:
seat = ev->seat_data;
if (!seat)
return;
switch (ev->dev_type) {
case UTERM_MONITOR_INPUT:
log_debug("new input device %s on seat %p",
ev->dev_node, seat);
break;
}
break;
case UTERM_MONITOR_FREE_DEV:
seat = ev->seat_data;
if (!seat)
return;
switch (ev->dev_type) {
case UTERM_MONITOR_INPUT:
log_debug("free input device %s on seat %p",
ev->dev_node, seat);
break;
}
break;
}
}
static void app_sig_generic(struct ev_eloop *eloop,
struct signalfd_siginfo *info,
void *data)
{
struct uvtd_app *app = data;
log_info("terminating due to caught signal %d", info->ssi_signo);
ev_eloop_exit(app->eloop);
}
static void app_sig_ignore(struct ev_eloop *eloop,
struct signalfd_siginfo *info,
void *data)
{
}
static void app_ctx_event(struct ev_fd *fd, int mask, void *data)
{
struct uvtd_app *app = data;
uvt_ctx_dispatch(app->ctx);
if (!(mask & EV_READABLE) && mask & (EV_HUP | EV_ERR)) {
log_error("HUP on UVT ctx fd");
ev_eloop_rm_fd(fd);
app->ctx_fd = NULL;
}
}
static void destroy_app(struct uvtd_app *app)
{
ev_eloop_rm_fd(app->ctx_fd);
uvt_ctx_unref(app->ctx);
uterm_monitor_unref(app->mon);
ev_eloop_unregister_signal_cb(app->eloop, SIGPIPE, app_sig_ignore,
app);
ev_eloop_unregister_signal_cb(app->eloop, SIGINT, app_sig_generic,
app);
ev_eloop_unregister_signal_cb(app->eloop, SIGTERM, app_sig_generic,
app);
ev_eloop_unref(app->eloop);
}
static int setup_app(struct uvtd_app *app)
{
int ret, fd;
shl_dlist_init(&app->seats);
ret = ev_eloop_new(&app->eloop, log_llog, NULL);
if (ret) {
log_error("cannot create eloop object: %d", ret);
goto err_app;
}
ret = ev_eloop_register_signal_cb(app->eloop, SIGTERM,
app_sig_generic, app);
if (ret) {
log_error("cannot register SIGTERM signal handler: %d", ret);
goto err_app;
}
ret = ev_eloop_register_signal_cb(app->eloop, SIGINT,
app_sig_generic, app);
if (ret) {
log_error("cannot register SIGINT signal handler: %d", ret);
goto err_app;
}
ret = ev_eloop_register_signal_cb(app->eloop, SIGPIPE,
app_sig_ignore, app);
if (ret) {
log_error("cannot register SIGPIPE signal handler: %d", ret);
goto err_app;
}
ret = uterm_monitor_new(&app->mon, app->eloop, app_monitor_event, app);
if (ret) {
log_error("cannot create device monitor: %d", ret);
goto err_app;
}
ret = uvt_ctx_new(&app->ctx, log_llog, NULL);
if (ret) {
log_error("cannot create UVT context: %d", ret);
goto err_app;
}
fd = uvt_ctx_get_fd(app->ctx);
if (fd >= 0) {
ret = ev_eloop_new_fd(app->eloop, &app->ctx_fd, fd,
EV_READABLE, app_ctx_event, app);
if (ret) {
log_error("cannot create UVT ctx efd: %d", ret);
goto err_app;
}
}
log_debug("scanning for devices...");
uterm_monitor_scan(app->mon);
return 0;
err_app:
destroy_app(app);
return ret;
}
int main(int argc, char **argv)
{
int ret;
struct uvtd_app app;
log_set_config(&LOG_CONFIG_INFO(1, 1));
log_print_init("uvtd");
memset(&app, 0, sizeof(app));
ret = setup_app(&app);
if (ret)
goto err_out;
ev_eloop_run(app.eloop, -1);
ret = 0;
destroy_app(&app);
err_out:
if (ret)
log_err("cannot initialize uvtd, errno %d: %s",
ret, strerror(-ret));
log_info("exiting");
return -ret;
}
|