Blob: logind.c

Blob id: b4279b20935408dc482dab0b4a8b9a090f8fabc9

Size: 5.2 KB

  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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  Enable functionality only when the user is active
 *
 *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 */

#ifdef SYSTEMD

#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>

#include <systemd/sd-login.h>

#include "obexd/src/log.h"
#include "obexd/src/logind.h"

static sd_login_monitor * monitor;
static int uid;
static gboolean active = FALSE;
static gboolean monitoring_enabled = TRUE;
static guint event_source;
static guint timeout_source;

struct callback_pair {
	logind_init_cb init_cb;
	logind_exit_cb exit_cb;
};

GSList *callbacks;

static void call_init_cb(gpointer data, gpointer user_data)
{
	int res;

	res = ((struct callback_pair *)data)->init_cb(FALSE);
	if (res)
		*(int *)user_data = res;
}
static void call_exit_cb(gpointer data, gpointer user_data)
{
	((struct callback_pair *)data)->exit_cb(FALSE);
}

static int update(void)
{
	char *state = NULL;
	gboolean state_is_active;
	int res;

	res = sd_login_monitor_flush(monitor);
	if (res < 0)
		return res;
	res = sd_uid_get_state(uid, &state);
	state_is_active = g_strcmp0(state, "active");
	free(state);
	if (res < 0)
		return res;

	if (state_is_active) {
		if (!active)
			return 0;
	} else {
		res = sd_uid_get_seats(uid, 1, NULL);
		if (res < 0)
			return res;
		if (active == !!res)
			return 0;
	}
	active ^= TRUE;
	res = 0;
	g_slist_foreach(callbacks, active ? call_init_cb : call_exit_cb, &res);
	return res;
}

static gboolean timeout_handler(gpointer user_data);

static int check_event(void)
{
	uint64_t timeout_usec;
	int res;

	res = sd_login_monitor_flush(monitor);
	if (res < 0)
		return res;
	if (!monitoring_enabled)
		return 0;
	res = update();
	if (res < 0)
		return res;

	res = sd_login_monitor_get_timeout(monitor, &timeout_usec);
	if (res < 0)
		return res;

	if (timeout_usec != (uint64_t)-1) {
		uint64_t time_usec;
		struct timespec ts;
		guint interval;

		res = clock_gettime(CLOCK_MONOTONIC, &ts);
		if (res < 0)
			return -errno;
		time_usec = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
		if (time_usec > timeout_usec)
			return check_event();
		interval = (timeout_usec - time_usec + 999) / 1000;
		timeout_source = g_timeout_add(interval, timeout_handler, NULL);
	}

	return 0;
}


static gboolean event_handler(GIOChannel *source, GIOCondition condition,
				gpointer data)
{
	int res;

	if (timeout_source) {
		g_source_remove(timeout_source);
		timeout_source = 0;
	}

	res = check_event();
	if (res) {
		error("%s: %s", __func__, strerror(-res));
		return FALSE;
	}

	return TRUE;
}

static gboolean timeout_handler(gpointer user_data)
{
	int res;

	res = check_event();
	if (res)
		error("%s: %s", __func__, strerror(-res));

	return FALSE;
}

static int logind_init(void)
{
	GIOChannel *channel;
	int events;
	int fd;
	int res;

	monitor = NULL;

	DBG("");

	if (!monitoring_enabled)
		return 0;

	uid = getuid();

	res = sd_login_monitor_new("uid", &monitor);
	if (res < 0) {
		monitor = NULL;
		goto FAIL;
	}

	// Check this after creating the monitor, in case of race conditions:
	res = update();
	if (res < 0)
		goto FAIL;

	events = res = sd_login_monitor_get_events(monitor);
	if (res < 0)
		goto FAIL;

	fd = res = sd_login_monitor_get_fd(monitor);
	if (res < 0)
		goto FAIL;

	channel = g_io_channel_unix_new(fd);

	g_io_channel_set_close_on_unref(channel, TRUE);
	g_io_channel_set_encoding(channel, NULL, NULL);
	g_io_channel_set_buffered(channel, FALSE);

	event_source = g_io_add_watch(channel, events, event_handler, NULL);

	g_io_channel_unref(channel);

	return check_event();

FAIL:
	sd_login_monitor_unref(monitor);
	monitoring_enabled = FALSE;
	active = TRUE;
	return res;
}

static void logind_exit(void)
{
	if (event_source) {
		g_source_remove(event_source);
		event_source = 0;
	}
	if (timeout_source) {
		g_source_remove(timeout_source);
		timeout_source = 0;
	}
	sd_login_monitor_unref(monitor);
}

static gint find_cb(gconstpointer a, gconstpointer b)
{
	return ((struct callback_pair *)a)->init_cb - (logind_init_cb)b;
}

int logind_register(logind_init_cb init_cb, logind_exit_cb exit_cb)
{
	struct callback_pair *cbs;

	if (!monitoring_enabled)
		return init_cb(TRUE);
	if (callbacks == NULL) {
		int res;

		res = logind_init();
		if (res) {
			error("logind_init(): %s - login detection disabled",
				strerror(-res));
			return init_cb(TRUE);
		}
	}
	cbs = g_new(struct callback_pair, 1);
	cbs->init_cb = init_cb;
	cbs->exit_cb = exit_cb;
	callbacks = g_slist_prepend(callbacks, cbs);
	return active ? init_cb(TRUE) : 0;
}
void logind_unregister(logind_init_cb init_cb, logind_exit_cb exit_cb)
{
	GSList *cb_node;

	if (!monitoring_enabled)
		return exit_cb(TRUE);
	if (active)
		exit_cb(TRUE);
	cb_node = g_slist_find_custom(callbacks, init_cb, find_cb);
	if (cb_node != NULL)
		callbacks = g_slist_delete_link(callbacks, cb_node);
	if (callbacks == NULL)
		logind_exit();
}

int logind_set(gboolean enabled)
{
	int res = 0;

	if (monitoring_enabled == enabled)
		return 0;

	monitoring_enabled = enabled;
	if (enabled) {
		active = FALSE;
		return update();
	}

	active = TRUE;
	g_slist_foreach(callbacks, call_exit_cb, &res);
	return res;
}

#endif