summaryrefslogtreecommitdiffstats
path: root/common/glib-ectomy.c
blob: fb7376429560edd0f81b8deaa36ac9bbfa524417 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <limits.h>
#include <sys/time.h>
#include <time.h>

#include "glib-ectomy.h"
#include "list.h"

GIOError g_io_channel_read(GIOChannel *channel, gchar *buf, gsize count, gsize *bytes_read)
{
	int fd = channel->fd;
	gssize result;

	/* At least according to the Debian manpage for read */
	if (count > SSIZE_MAX)
		count = SSIZE_MAX;

retry:
	result = read (fd, buf, count);

	if (result < 0) {
		*bytes_read = 0;

		switch (errno) {
#ifdef EINTR
		case EINTR:
			goto retry;
#endif
#ifdef EAGAIN
		case EAGAIN:
			return G_IO_STATUS_AGAIN;
#endif
		default:
			return G_IO_STATUS_ERROR;
		}
	}

	*bytes_read = result;

	return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
}

void g_io_channel_close(GIOChannel *channel)
{
	if (!channel)
		return;

	close(channel->fd);

	memset(channel, 0, sizeof(channel));
	free(channel);
}

GIOChannel *g_io_channel_unix_new(int fd)
{
	GIOChannel *channel;

	channel = malloc(sizeof(GIOChannel));
	if (!channel)
		return NULL;

	channel->fd = fd;

	return channel;
}

gint g_io_channel_unix_get_fd(GIOChannel *channel)
{
	return channel->fd;
}

struct watch {
	guint id;
	GIOChannel *channel;
	gint priority;
	GIOCondition condition;
	short *revents;
	GIOFunc func;
	gpointer user_data;
	GDestroyNotify destroy;

	struct watch *prev;
	struct watch *next;
};

static struct watch watch_head = { .id = 0, .prev = 0, .next = 0, .revents = 0 };

static GMainContext *default_context = NULL;

static void watch_remove(struct watch *w)
{
	struct watch *p, *n;

	if (!w)
		return;

	p = w->prev;
	n = w->next;

	if (p)
		p->next = n;

	if (n)
		n->prev = p;

	free(w);
}

void g_io_remove_watch(guint id)
{
	struct watch *w, *n;

	for (w = watch_head.next; w; w = n) {
		n = w->next;
		if (w->id != id)
			continue;

		watch_remove(w);
		return;
	}
}

guint g_io_add_watch_full(GIOChannel *channel, gint priority,
				GIOCondition condition, GIOFunc func,
				gpointer user_data, GDestroyNotify notify)
{
	struct watch *watch = malloc(sizeof(struct watch));

	watch->id = ++watch_head.id;
	watch->channel = channel;
	watch->priority = priority;
	watch->condition = condition;
	watch->func = func;
	watch->user_data = user_data;
	watch->destroy = notify;

	watch->prev = &watch_head;
	watch->next = watch_head.next;
	if (watch_head.next)
		watch_head.next->prev = watch;

	watch_head.next = watch;

	return watch->id;
}

guint g_io_add_watch(GIOChannel *channel, GIOCondition condition,
					GIOFunc func, gpointer user_data)
{
	return g_io_add_watch_full(channel, 0, condition,
						func, user_data, NULL);
}

static void timeout_free(void *data, void *user_data)
{
	struct timeout *t = data;

	if (t)
		free (t);
}

static GMainContext *g_main_context_default()
{
	if (default_context)
		return default_context;

	default_context = malloc(sizeof(GMainContext));
	if (!default_context)
		return NULL;

	memset(default_context, 0, sizeof(GMainContext));

	default_context->timeout = -1;

	return default_context;
}

GMainLoop *g_main_loop_new(GMainContext *context, gboolean is_running)
{
	GMainLoop *ml;

	ml = malloc(sizeof(GMainLoop));
	if (!ml)
		return NULL;

	memset(ml, 0, sizeof(GMainLoop));

	if (!context)
		ml->context = g_main_context_default();
	else
		ml->context = context;

	ml->bail = 0;

	return ml;
}

static void timeout_handlers_prepare(GMainContext *context)
{
	struct slist *l = context->ltimeout;
	struct timeout *t;
	struct timeval tv;
	glong msec, timeout = LONG_MAX;

	gettimeofday(&tv, NULL);

	while (l) {
		t = l->data;
		l = l->next;

		/* calculate the remainning time */
		msec = (t->expiration.tv_sec - tv.tv_sec) * 1000 +
				(t->expiration.tv_usec - tv.tv_usec) / 1000;
		if (msec < 0)
			msec = 0;

		timeout = MIN_TIMEOUT(timeout, msec);
	}

	/* set to min value found or NO timeout */
	context->timeout = (timeout != LONG_MAX ? timeout: -1);
}

static int timeout_cmp(const void *t1, const void *t2)
{
	const struct timeout *tout1 = t1;
	const struct timeout *tout2 = t2;

	if (!tout2)
		return -1;

	if (tout1 != tout2)
		return -1;

	return tout1->id - tout2->id;
}

static void timeout_handlers_check(GMainContext *context)
{
	struct slist *l = context->ltimeout;
	struct timeout *t;
	struct timeval tv;

	gettimeofday(&tv, NULL);

	while (l) {
		t = l->data;
		l = l->next;

		if (timercmp(&tv, &t->expiration, <))
			continue;

		if (t->function(t->data)) {
			struct slist *match;
			/* if false/expired: remove it from the list
			 * Before remove check again in order to cover the situation
			 * when the handler is removed/freed by the callback function
			 */
			match = slist_find(context->ltimeout, t, timeout_cmp);
			if (match) {
				t = match->data;
				context->ltimeout = slist_remove(context->ltimeout, t);
				free(t);
			}
		} else {
			glong secs, msecs;
			/* update the next expiration time */
			secs = t->interval / 1000;
			msecs = t->interval - secs * 1000;

			t->expiration.tv_sec = tv.tv_sec + secs;
			t->expiration.tv_usec = tv.tv_usec + msecs * 1000;
			if (t->expiration.tv_usec >= 1000000) {
				t->expiration.tv_usec -= 1000000;
				t->expiration.tv_sec++;
			}
		}
	}
}

void g_main_loop_run(GMainLoop *loop)
{
	int open_max = sysconf(_SC_OPEN_MAX);
	struct pollfd *ufds;

	ufds = malloc(open_max * sizeof(struct pollfd));
	if (!ufds)
		return;

	while (!loop->bail) {
		int nfds, rc;
		struct watch *n, *w;

		nfds = 0;
		for (w = watch_head.next; w != NULL; w = w->next) {
			ufds[nfds].fd = w->channel->fd;
			ufds[nfds].events = w->condition;
			ufds[nfds].revents = 0;
			w->revents = &ufds[nfds].revents;
			nfds++;
		}

		/* calculate the next timeout */
		timeout_handlers_prepare(loop->context);

		rc = poll(ufds, nfds, loop->context->timeout);
		if (rc < 0)
			continue;

		w = watch_head.next;
		while (w) {
			if (!*w->revents || w->func(w->channel, *w->revents, w->user_data)) {
				w = w->next;
				continue;
			}

			n = w->next;

			if (w->destroy)
				w->destroy(w->user_data);
			watch_remove(w);

			w = n;
		}

		/* check expired timers */
		timeout_handlers_check(loop->context);
	}

	free(ufds);
}

void g_main_loop_quit(GMainLoop *loop)
{
	struct watch *w;

	loop->bail = 1;

	for (w = watch_head.next; w; w = w->next) {
		if (w->destroy)
			w->destroy(w->user_data);
		watch_head.next = w->next;
		free(w);
	}
}

void g_main_loop_unref(GMainLoop *loop)
{
	if (!loop->context)
		return;

	slist_foreach(loop->context->ltimeout, timeout_free, NULL);
	slist_free(loop->context->ltimeout);
	free(loop->context);
}

guint g_timeout_add(guint interval, GSourceFunc function, gpointer data)
{
	struct timeval tv;
	guint secs;
	guint msecs;
	struct timeout *t;

	if (!default_context || !function)
		return 0;

	t = malloc(sizeof(*t));

	if (!t)
		return 0;

	memset(t, 0, sizeof(*t));
	t->interval = interval;
	t->function = function;
	t->data = data;

	gettimeofday(&tv, NULL);

	secs = interval /1000;
	msecs = interval - secs * 1000;

	t->expiration.tv_sec = tv.tv_sec + secs;
	t->expiration.tv_usec = tv.tv_usec + msecs * 1000;

	if (t->expiration.tv_usec >= 1000000) {
		t->expiration.tv_usec -= 1000000;
		t->expiration.tv_sec++;
	}

	/* attach the timeout the default context */
	t->id = ++default_context->next_id;
	default_context->ltimeout = slist_append(default_context->ltimeout, t);

	return t->id;
}

gint g_timeout_remove(const guint id)
{
	struct slist *l;
	struct timeout *t;

	if (!default_context)
		return -1;

	l = default_context->ltimeout;

	while (l) {
		t = l->data;
		l = l->next;

		if (t->id != id)
			continue;

		default_context->ltimeout = slist_remove(default_context->ltimeout, t);
		free(t);

		return 0;
	}

	return -1;
}