2 * uvtd - User-space VT daemon
4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@googlemail.com>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
32 #include <sys/signalfd.h>
34 #include "shl_dlist.h"
36 #include "uterm_input.h"
37 #include "uterm_monitor.h"
42 struct shl_dlist list;
44 struct uterm_monitor_seat *useat;
49 struct ev_eloop *eloop;
50 struct uterm_monitor *mon;
53 struct shl_dlist seats;
56 static int app_seat_new(struct uvtd_app *app, const char *sname,
57 struct uterm_monitor_seat *useat)
59 struct app_seat *seat;
62 seat = malloc(sizeof(*seat));
66 log_debug("new seat %p on %s", seat, sname);
68 memset(seat, 0, sizeof(*seat));
72 ret = uvtd_ctx_new(&seat->ctx, sname, app->eloop, app->ctx);
74 log_debug("ignoring seat %s as it has real VTs", sname);
80 uterm_monitor_set_seat_data(seat->useat, seat);
81 shl_dlist_link(&app->seats, &seat->list);
89 static void app_seat_free(struct app_seat *seat)
91 log_debug("free seat %p", seat);
93 shl_dlist_unlink(&seat->list);
94 uterm_monitor_set_seat_data(seat->useat, NULL);
95 uvtd_ctx_free(seat->ctx);
99 static void app_monitor_event(struct uterm_monitor *mon,
100 struct uterm_monitor_event *ev,
103 struct uvtd_app *app = data;
104 struct app_seat *seat;
108 case UTERM_MONITOR_NEW_SEAT:
109 ret = app_seat_new(app, ev->seat_name, ev->seat);
113 case UTERM_MONITOR_FREE_SEAT:
115 app_seat_free(ev->seat_data);
117 case UTERM_MONITOR_NEW_DEV:
118 seat = ev->seat_data;
122 switch (ev->dev_type) {
123 case UTERM_MONITOR_INPUT:
124 log_debug("new input device %s on seat %p",
129 case UTERM_MONITOR_FREE_DEV:
130 seat = ev->seat_data;
134 switch (ev->dev_type) {
135 case UTERM_MONITOR_INPUT:
136 log_debug("free input device %s on seat %p",
144 static void app_sig_generic(struct ev_eloop *eloop,
145 struct signalfd_siginfo *info,
148 struct uvtd_app *app = data;
150 log_info("terminating due to caught signal %d", info->ssi_signo);
151 ev_eloop_exit(app->eloop);
154 static void app_sig_ignore(struct ev_eloop *eloop,
155 struct signalfd_siginfo *info,
160 static void app_ctx_event(struct ev_fd *fd, int mask, void *data)
162 struct uvtd_app *app = data;
164 uvt_ctx_dispatch(app->ctx);
166 if (!(mask & EV_READABLE) && mask & (EV_HUP | EV_ERR)) {
167 log_error("HUP on UVT ctx fd");
173 static void destroy_app(struct uvtd_app *app)
175 ev_eloop_rm_fd(app->ctx_fd);
176 uvt_ctx_unref(app->ctx);
177 uterm_monitor_unref(app->mon);
178 ev_eloop_unregister_signal_cb(app->eloop, SIGPIPE, app_sig_ignore,
180 ev_eloop_unregister_signal_cb(app->eloop, SIGINT, app_sig_generic,
182 ev_eloop_unregister_signal_cb(app->eloop, SIGTERM, app_sig_generic,
184 ev_eloop_unref(app->eloop);
187 static int setup_app(struct uvtd_app *app)
191 shl_dlist_init(&app->seats);
193 ret = ev_eloop_new(&app->eloop, log_llog, NULL);
195 log_error("cannot create eloop object: %d", ret);
199 ret = ev_eloop_register_signal_cb(app->eloop, SIGTERM,
200 app_sig_generic, app);
202 log_error("cannot register SIGTERM signal handler: %d", ret);
206 ret = ev_eloop_register_signal_cb(app->eloop, SIGINT,
207 app_sig_generic, app);
209 log_error("cannot register SIGINT signal handler: %d", ret);
213 ret = ev_eloop_register_signal_cb(app->eloop, SIGPIPE,
214 app_sig_ignore, app);
216 log_error("cannot register SIGPIPE signal handler: %d", ret);
220 ret = uterm_monitor_new(&app->mon, app->eloop, app_monitor_event, app);
222 log_error("cannot create device monitor: %d", ret);
226 ret = uvt_ctx_new(&app->ctx, log_llog, NULL);
228 log_error("cannot create UVT context: %d", ret);
232 fd = uvt_ctx_get_fd(app->ctx);
234 ret = ev_eloop_new_fd(app->eloop, &app->ctx_fd, fd,
235 EV_READABLE, app_ctx_event, app);
237 log_error("cannot create UVT ctx efd: %d", ret);
242 log_debug("scanning for devices...");
243 uterm_monitor_scan(app->mon);
252 int main(int argc, char **argv)
257 log_set_config(&LOG_CONFIG_INFO(1, 1));
258 log_print_init("uvtd");
260 memset(&app, 0, sizeof(app));
262 ret = setup_app(&app);
266 ev_eloop_run(app.eloop, -1);
272 log_err("cannot initialize uvtd, errno %d: %s",
273 ret, strerror(-ret));