aboutsummaryrefslogtreecommitdiff
path: root/libseat/backend/seatd.c
blob: 96b4681a35dd3469f0874082dc8d31911eb9ad0c (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include "backend.h"
#include "connection.h"
#include "libseat.h"
#include "linked_list.h"
#include "log.h"
#include "protocol.h"

#ifdef BUILTIN_ENABLED
#include "poller.h"
#include "server.h"
#endif

const struct seat_impl seatd_impl;
const struct seat_impl builtin_impl;

struct pending_event {
	struct linked_list link; // backend_seat::link
	int opcode;
};

struct backend_seatd {
	struct libseat base;
	struct connection connection;
	struct libseat_seat_listener *seat_listener;
	void *seat_listener_data;
	struct linked_list pending_events;
	bool error;

	char seat_name[MAX_SEAT_LEN];
};

static int set_nonblock(int fd) {
	int flags;
	if ((flags = fcntl(fd, F_GETFD)) == -1 || fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
		return -1;
	}
	if ((flags = fcntl(fd, F_GETFL)) == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
		return -1;
	}
	return 0;
}

static int seatd_connect(void) {
	union {
		struct sockaddr_un unix;
		struct sockaddr generic;
	} addr = {{0}};
	int fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		log_errorf("Could not create socket: %s", strerror(errno));
		return -1;
	}
	if (set_nonblock(fd) == -1) {
		log_errorf("Could not make socket non-blocking: %s", strerror(errno));
		close(fd);
		return -1;
	}
	const char *path = getenv("SEATD_SOCK");
	if (path == NULL) {
		path = SEATD_DEFAULTPATH;
	}
	addr.unix.sun_family = AF_UNIX;
	strncpy(addr.unix.sun_path, path, sizeof addr.unix.sun_path - 1);
	socklen_t size = offsetof(struct sockaddr_un, sun_path) + strlen(addr.unix.sun_path);
	if (connect(fd, &addr.generic, size) == -1) {
		if (errno == ENOENT) {
			log_infof("Could not connect to socket %s: %s", path, strerror(errno));
		} else {
			log_errorf("Could not connect to socket %s: %s", path, strerror(errno));
		}
		close(fd);
		return -1;
	};
	return fd;
}

static struct backend_seatd *backend_seatd_from_libseat_backend(struct libseat *base) {
	assert(base);
#ifdef BUILTIN_ENABLED
	assert(base->impl == &seatd_impl || base->impl == &builtin_impl);
#else
	assert(base->impl == &seatd_impl);
#endif
	return (struct backend_seatd *)base;
}

static void cleanup(struct backend_seatd *backend) {
	if (backend->connection.fd != -1) {
		close(backend->connection.fd);
		backend->connection.fd = -1;
	}
	connection_close_fds(&backend->connection);
	while (!linked_list_empty(&backend->pending_events)) {
		struct pending_event *ev = (struct pending_event *)backend->pending_events.next;
		linked_list_remove(&ev->link);
		free(ev);
	}
}

static void destroy(struct backend_seatd *backend) {
	cleanup(backend);
	free(backend);
}

static void set_error(struct backend_seatd *backend) {
	if (backend->error) {
		return;
	}

	backend->error = true;
	cleanup(backend);
}

static inline int conn_put(struct backend_seatd *backend, const void *data, const size_t data_len) {
	if (connection_put(&backend->connection, data, data_len) == -1) {
		log_errorf("Could not send request: %s", strerror(errno));
		set_error(backend);
		return -1;
	}
	return 0;
}

static inline int conn_flush(struct backend_seatd *backend) {
	if (connection_flush(&backend->connection) == -1) {
		log_errorf("Could not flush connection: %s", strerror(errno));
		set_error(backend);
		return -1;
	}
	return 0;
}

static inline int conn_get(struct backend_seatd *backend, void *target, const size_t target_len) {
	if (connection_get(&backend->connection, target, target_len) == -1) {
		log_error("Invalid message: insufficient data received");
		set_error(backend);
		errno = EBADMSG;
		return -1;
	}
	return 0;
}

static inline int conn_get_fd(struct backend_seatd *backend, int *fd) {
	if (connection_get_fd(&backend->connection, fd) == -1) {
		log_error("Invalid message: insufficient data received");
		set_error(backend);
		errno = EBADMSG;
		return -1;
	}
	return 0;
}

static size_t read_header(struct backend_seatd *backend, uint16_t expected_opcode,
			  size_t expected_size, bool variable) {
	struct proto_header header;
	if (conn_get(backend, &header, sizeof header) == -1) {
		set_error(backend);
		return SIZE_MAX;
	}
	if (header.opcode != expected_opcode) {
		struct proto_server_error msg;
		if (header.opcode != SERVER_ERROR) {
			log_errorf("Unexpected response: expected opcode %d, received opcode %d",
				   expected_opcode, header.opcode);
			set_error(backend);
			errno = EBADMSG;
		} else if (header.size != sizeof msg || conn_get(backend, &msg, sizeof msg) == -1) {
			set_error(backend);
			errno = EBADMSG;
		} else {
			errno = msg.error_code;
		}
		return SIZE_MAX;
	}

	if ((!variable && header.size != expected_size) || (variable && header.size < expected_size)) {
		log_errorf("Invalid message: does not match expected size: variable: %d, header.size: %d, expected size: %zd",
			   variable, header.size, expected_size);
		set_error(backend);
		errno = EBADMSG;
		return SIZE_MAX;
	}
	return header.size;
}

static int queue_event(struct backend_seatd *backend, int opcode) {
	struct pending_event *ev = calloc(1, sizeof(struct pending_event));
	if (ev == NULL) {
		log_errorf("Allocation failed: %s", strerror(errno));
		return -1;
	}

	ev->opcode = opcode;
	linked_list_insert(&backend->pending_events, &ev->link);
	return 0;
}

static int execute_events(struct backend_seatd *backend) {
	struct linked_list list;
	linked_list_init(&list);
	linked_list_take(&list, &backend->pending_events);
	int executed = 0;
	while (!linked_list_empty(&list)) {
		struct pending_event *ev = (struct pending_event *)list.next;
		int opcode = ev->opcode;
		linked_list_remove(&ev->link);
		free(ev);

		switch (opcode) {
		case SERVER_DISABLE_SEAT:
			log_info("Disabling seat");
			backend->seat_listener->disable_seat(&backend->base,
							     backend->seat_listener_data);
			break;
		case SERVER_ENABLE_SEAT:
			log_info("Enabling seat");
			backend->seat_listener->enable_seat(&backend->base,
							    backend->seat_listener_data);
			break;
		default:
			log_errorf("Invalid opcode: %d", opcode);
			abort();
		}
		executed++;
	}
	return executed;
}

static int dispatch_pending(struct backend_seatd *backend, int *opcode) {
	int packets = 0;
	struct proto_header header;
	while (connection_get(&backend->connection, &header, sizeof header) != -1) {
		packets++;
		switch (header.opcode) {
		case SERVER_DISABLE_SEAT:
		case SERVER_ENABLE_SEAT:
			if (queue_event(backend, header.opcode) == -1) {
				set_error(backend);
				return -1;
			}
			break;
		default:
			if (opcode != NULL &&
			    connection_pending(&backend->connection) >= header.size) {
				*opcode = header.opcode;
			}
			connection_restore(&backend->connection, sizeof header);
			return packets;
		}
	}
	return packets;
}

static int dispatch_pending_and_execute(struct backend_seatd *backend) {
	int dispatched = dispatch_pending(backend, NULL);
	if (dispatched == -1) {
		return -1;
	}
	dispatched += execute_events(backend);
	return dispatched;
}

static int poll_connection(struct backend_seatd *backend, int timeout) {
	struct pollfd fd = {
		.fd = backend->connection.fd,
		.events = POLLIN,
	};

	if (poll(&fd, 1, timeout) == -1) {
		return (errno == EAGAIN || errno == EINTR) ? 0 : -1;
	}

	if (fd.revents & (POLLERR | POLLHUP)) {
		errno = EPIPE;
		return -1;
	}

	int len = 0;
	if (fd.revents & POLLIN) {
		len = connection_read(&backend->connection);
		if (len == 0) {
			errno = EIO;
			return -1;
		} else if (len == -1 && errno != EAGAIN) {
			return -1;
		}
	}

	return len;
}

static int dispatch(struct backend_seatd *backend) {
	if (conn_flush(backend) == -1) {
		return -1;
	}
	while (true) {
		int opcode = 0;
		if (dispatch_pending(backend, &opcode) == -1) {
			log_errorf("Could not dispatch pending messages: %s", strerror(errno));
			return -1;
		}
		if (opcode != 0) {
			break;
		}
		if (poll_connection(backend, -1) == -1) {
			log_errorf("Could not poll connection: %s", strerror(errno));
			return -1;
		}
	}
	return 0;
}

static int get_fd(struct libseat *base) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	return backend->connection.fd;
}

static int dispatch_and_execute(struct libseat *base, int timeout) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	if (backend->error) {
		errno = ENOTCONN;
		return -1;
	}

	int predispatch = dispatch_pending_and_execute(backend);
	if (predispatch == -1) {
		return -1;
	}

	// We don't want to block if we dispatched something, as the
	// caller might be waiting for the result. However, we'd also
	// like to read anything pending.
	int read = 0;
	if (predispatch == 0 || timeout == 0) {
		read = connection_read(&backend->connection);
	} else {
		read = poll_connection(backend, timeout);
	}

	if (read == 0) {
		return predispatch;
	} else if (read == -1 && errno != EAGAIN) {
		log_errorf("Could not read from connection: %s", strerror(errno));
		return -1;
	}

	int postdispatch = dispatch_pending_and_execute(backend);
	if (postdispatch == -1) {
		return -1;
	}

	return predispatch + postdispatch;
}

static struct libseat *_open_seat(struct libseat_seat_listener *listener, void *data, int fd) {
	assert(listener != NULL);
	assert(listener->enable_seat != NULL && listener->disable_seat != NULL);
	struct backend_seatd *backend = calloc(1, sizeof(struct backend_seatd));
	if (backend == NULL) {
		log_errorf("Allocation failed: %s", strerror(errno));
		goto alloc_error;
	}

	backend->seat_listener = listener;
	backend->seat_listener_data = data;
	backend->connection.fd = fd;
	backend->base.impl = &seatd_impl;
	linked_list_init(&backend->pending_events);

	struct proto_header header = {
		.opcode = CLIENT_OPEN_SEAT,
		.size = 0,
	};
	if (conn_put(backend, &header, sizeof header) == -1 || dispatch(backend) == -1) {
		goto backend_error;
	}

	struct proto_server_seat_opened rmsg;
	size_t size = read_header(backend, SERVER_SEAT_OPENED, sizeof rmsg, true);
	if (size == SIZE_MAX || conn_get(backend, &rmsg, sizeof rmsg) == -1) {
		goto backend_error;
	}
	if (rmsg.seat_name_len != size - sizeof rmsg) {
		log_errorf("Invalid message: seat_name_len does not match remaining message size (%d != %zd)",
			   rmsg.seat_name_len, size);
		errno = EBADMSG;
		goto backend_error;
	}
	if (conn_get(backend, backend->seat_name, rmsg.seat_name_len) == -1) {
		goto backend_error;
	}

	execute_events(backend);
	return &backend->base;

backend_error:
	destroy(backend);
alloc_error:
	close(fd);
	return NULL;
}

static struct libseat *open_seat(struct libseat_seat_listener *listener, void *data) {
	int fd = seatd_connect();
	if (fd == -1) {
		return NULL;
	}

	return _open_seat(listener, data, fd);
}

static int close_seat(struct libseat *base) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);

	struct proto_header header = {
		.opcode = CLIENT_CLOSE_SEAT,
		.size = 0,
	};
	if (conn_put(backend, &header, sizeof header) == -1 || dispatch(backend) == -1) {
		goto error;
	}

	if (read_header(backend, SERVER_SEAT_CLOSED, 0, false) == SIZE_MAX) {
		goto error;
	}

	execute_events(backend);
	destroy(backend);
	return 0;

error:
	execute_events(backend);
	destroy(backend);
	return -1;
}

static const char *seat_name(struct libseat *base) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	return backend->seat_name;
}

static int open_device(struct libseat *base, const char *path, int *fd) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	if (backend->error) {
		errno = ENOTCONN;
		return -1;
	}
	size_t pathlen = strlen(path) + 1;
	if (pathlen > MAX_PATH_LEN) {
		errno = EINVAL;
		return -1;
	}

	struct proto_client_open_device msg = {
		.path_len = (uint16_t)pathlen,
	};
	struct proto_header header = {
		.opcode = CLIENT_OPEN_DEVICE,
		.size = sizeof msg + pathlen,
	};
	if (conn_put(backend, &header, sizeof header) == -1 ||
	    conn_put(backend, &msg, sizeof msg) == -1 || conn_put(backend, path, pathlen) == -1 ||
	    dispatch(backend) == -1) {
		goto error;
	}

	struct proto_server_device_opened rmsg;
	if (read_header(backend, SERVER_DEVICE_OPENED, sizeof rmsg, false) == SIZE_MAX ||
	    conn_get(backend, &rmsg, sizeof rmsg) == -1 || conn_get_fd(backend, fd)) {
		goto error;
	}

	execute_events(backend);
	return rmsg.device_id;

error:
	execute_events(backend);
	return -1;
}

static int close_device(struct libseat *base, int device_id) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	if (backend->error) {
		errno = ENOTCONN;
		return -1;
	}
	if (device_id < 0) {
		errno = EINVAL;
		return -1;
	}

	struct proto_client_close_device msg = {
		.device_id = device_id,
	};
	struct proto_header header = {
		.opcode = CLIENT_CLOSE_DEVICE,
		.size = sizeof msg,
	};
	if (conn_put(backend, &header, sizeof header) == -1 ||
	    conn_put(backend, &msg, sizeof msg) == -1 || dispatch(backend) == -1) {
		goto error;
	}

	if (read_header(backend, SERVER_DEVICE_CLOSED, 0, false) == SIZE_MAX) {
		goto error;
	}

	execute_events(backend);
	return 0;

error:
	execute_events(backend);
	return -1;
}

static int switch_session(struct libseat *base, int session) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	if (backend->error) {
		errno = ENOTCONN;
		return -1;
	}
	if (session < 0) {
		return -1;
	}

	struct proto_client_switch_session msg = {
		.session = session,
	};
	struct proto_header header = {
		.opcode = CLIENT_SWITCH_SESSION,
		.size = sizeof msg,
	};
	if (conn_put(backend, &header, sizeof header) == -1 ||
	    conn_put(backend, &msg, sizeof msg) == -1 || conn_flush(backend) == -1) {
		return -1;
	}

	return 0;
}

static int disable_seat(struct libseat *base) {
	struct backend_seatd *backend = backend_seatd_from_libseat_backend(base);
	if (backend->error) {
		errno = ENOTCONN;
		return -1;
	}
	struct proto_header header = {
		.opcode = CLIENT_DISABLE_SEAT,
		.size = 0,
	};
	if (conn_put(backend, &header, sizeof header) == -1 || conn_flush(backend) == -1) {
		return -1;
	}

	return 0;
}

const struct seat_impl seatd_impl = {
	.open_seat = open_seat,
	.disable_seat = disable_seat,
	.close_seat = close_seat,
	.seat_name = seat_name,
	.open_device = open_device,
	.close_device = close_device,
	.switch_session = switch_session,
	.get_fd = get_fd,
	.dispatch = dispatch_and_execute,
};

#ifdef BUILTIN_ENABLED
#include <signal.h>

static int set_deathsig(int signal);

#if defined(__linux__)
#include <sys/prctl.h>

static int set_deathsig(int signal) {
	return prctl(PR_SET_PDEATHSIG, signal);
}
#elif defined(__FreeBSD__)
#include <sys/procctl.h>

static int set_deathsig(int signal) {
	return procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signal);
}
#else
#error Unsupported platform
#endif

static struct libseat *builtin_open_seat(struct libseat_seat_listener *listener, void *data) {
	int fds[2];
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
		log_errorf("Could not create socket pair: %s", strerror(errno));
		return NULL;
	}

	if (geteuid() != 0) {
		log_error("Built-in seatd instance requires root privileges");
		return NULL;
	}

	pid_t pid = fork();
	if (pid == -1) {
		log_errorf("Could not fork: %s", strerror(errno));
		close(fds[0]);
		close(fds[1]);
		return NULL;
	} else if (pid == 0) {
		int fd = fds[0];
		int res = 0;
		struct server server = {0};
		if (server_init(&server) == -1) {
			log_errorf("Could not init embedded seatd server: %s", strerror(errno));
			res = 1;
			goto error;
		}
		if (server_add_client(&server, fd) == -1) {
			log_errorf("Could not add client to embedded seatd server: %s",
				   strerror(errno));
			res = 1;
			goto server_error;
		}
		set_deathsig(SIGTERM);
		while (server.running) {
			if (poller_poll(&server.poller) == -1) {
				log_errorf("Could not poll server socket: %s", strerror(errno));
				res = 1;
				break;
			}
		}
	server_error:
		server_finish(&server);
	error:
		close(fd);
		exit(res);
	} else {
		int fd = fds[1];
		return _open_seat(listener, data, fd);
	}
}

const struct seat_impl builtin_impl = {
	.open_seat = builtin_open_seat,
	.disable_seat = disable_seat,
	.close_seat = close_seat,
	.seat_name = seat_name,
	.open_device = open_device,
	.close_device = close_device,
	.switch_session = switch_session,
	.get_fd = get_fd,
	.dispatch = dispatch_and_execute,
};
#endif