summaryrefslogtreecommitdiff
path: root/inotify.c
blob: f362920c1e5e426acb35bfe289ec7f1d06b8a3f2 (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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <sys/inotify.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <inttypes.h>

#include "main.h"
#include "uring.h"
#include "config.h"
#include "server.h"

static void
eat_whitespace_and_comments(char **pos)
{
	while (true) {
		while (isspace(**pos))
			(*pos)++;

		if (**pos == '#') {
			while (**pos != '\r' && **pos != '\n' && **pos != '\0')
				(*pos)++;
			continue;
		}
		
		return;
	}
}

static char *
get_line(char **pos)
{
	char *begin = *pos;
	char *end;

	while (isspace(*begin))
		begin++;

	if (*begin == '\0')
		return NULL;

	end = begin;
	while (*end != '\n' && *end != '\0')
		end++;

	if (*end == '\0')
		*pos = end;
	else
		*pos = end + 1;

	while (isspace(*end)) {
		*end = '\0';
		end--;
	}

	return begin;
}

static bool
strtosockaddrs(const char *str, struct list_head *list)
{
	char *tmp;
	uint16_t port;
	int r;
	struct sockaddr_in46 *addr;

	if (!str || *str == '\0' || !list)
		return false;
	list_init(list);

	if (*str == '[') {
		/* IPv6, [a:b:c...h]:p or [*]:p */
		str++;
		tmp = strchr(str, ']');
		if (!tmp)
			goto out;
		*tmp = '\0';

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		list_add(&addr->list, list);

		if (!strcmp(str, "*"))
			addr->in6.sin6_addr = in6addr_any;
		else if (inet_pton(AF_INET6, str, &addr->in6.sin6_addr) <= 0)
			goto out;

		tmp++;
		if (*tmp != ':')
			goto out;

		tmp++;
		if (strtou16_strict(tmp, &port) < 0)
			goto out;

		addr->in6.sin6_family = AF_INET6;
		addr->in6.sin6_port = htons(port);
		addr->addrlen = sizeof(addr->in6);

	} else if (*str == '*') {
		/* IPv4, *:p */
		str++;
		if (*str != ':')
			goto out;

		str++;
		if (strtou16_strict(str, &port) < 0)
			goto out;

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		list_add(&addr->list, list);

		addr->in4.sin_family = AF_INET;
		addr->in4.sin_addr.s_addr = INADDR_ANY;
		addr->in4.sin_port = htons(port);
		addr->addrlen = sizeof(addr->in4);

	} else if ((tmp = strchr(str, ':'))) {
		/* IPv4, a.b.c.d:p or IPv4/6 hostname:p */
		fprintf(stderr, "Got an IPv4:port or hostname:port\n");
		*tmp = '\0';
		tmp++;
		if (strtou16_strict(tmp, &port) < 0)
			goto out;

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;

		if (inet_pton(AF_INET, str, &addr->in4.sin_addr) > 0) {
			fprintf(stderr, "...Got an IPv4:port (0x%p, list 0x%p)\n", addr, &addr->list);
			addr->in4.sin_family = AF_INET;
			addr->in4.sin_port = htons(port);
			addr->addrlen = sizeof(addr->in4);
			list_add(&addr->list, list);
			goto success;
		} else {
			free(addr);
		}

		struct addrinfo hints = {
			.ai_family = AF_UNSPEC,
			.ai_socktype = SOCK_STREAM,
			.ai_protocol = 0,
			.ai_flags = 0,
		};
		struct addrinfo *results, *ai;

		/* FIXME: This is completely synchronous but getaddrinfo_a is not very ergonomic */
		r = getaddrinfo(str, tmp, &hints, &results);
		if (r != 0) {
			fprintf(stderr, "gettaddrinfo(%s): %s\n", str, gai_strerror(r));
			goto out;
		}

		fprintf(stderr, "...Got an hostname:port\n");
		for (ai = results; ai; ai = ai->ai_next) {
			fprintf(stderr, "Got a result from getaddrinfo\n");

			addr = zmalloc(sizeof(*addr));
			if (!addr) {
				freeaddrinfo(results);
				goto out;
			}

			switch (ai->ai_family) {
			case AF_INET: {
				struct sockaddr_in *naddr = (struct sockaddr_in *)ai->ai_addr;

				addr->in4.sin_family = AF_INET;
				addr->in4.sin_addr = naddr->sin_addr;
				addr->in4.sin_port = naddr->sin_port;
				addr->addrlen = sizeof(addr->in4);
				list_add(&addr->list, list);
				break;
			}
			case AF_INET6: {
				struct sockaddr_in6 *naddr = (struct sockaddr_in6 *)ai->ai_addr;

				addr->in6.sin6_family = AF_INET6;
				addr->in6.sin6_addr = naddr->sin6_addr;
				addr->in6.sin6_port = naddr->sin6_port;
				addr->addrlen = sizeof(addr->in6);
				list_add(&addr->list, list);
				break;
			}
			default:
				fprintf(stderr, "  Fam: Unknown (%i)\n", ai->ai_family);
				free(addr);
				continue;
			}
		}

		freeaddrinfo(results);

	} else if (strtou16_strict(tmp, &port) == 0) {
		/* Port */

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		addr->in6.sin6_family = AF_INET6;
		addr->in6.sin6_addr = in6addr_any;
		addr->in6.sin6_port = htons(port);
		addr->addrlen = sizeof(addr->in6);
		list_add(&addr->list, list);

		addr = zmalloc(sizeof(*addr));
		if (!addr)
			goto out;
		addr->in4.sin_family = AF_INET;
		addr->in4.sin_addr.s_addr = INADDR_ANY;
		addr->in4.sin_port = htons(port);
		addr->addrlen = sizeof(addr->in4);
		list_add(&addr->list, list);
	}

success:
	if (list_empty(list)) {
		fprintf(stderr, "Success but empty list!?\n");
		return false;
	} else {
		int i = 0;
		struct list_head *pos;

		list_for_each(pos, list)
			i++;
		fprintf(stderr, "Success, %i entries\n", i);
	}

	return true;

out:
	if (!list_empty(list)) {
		struct sockaddr_in46 *tmpaddr;

		list_for_each_entry_safe(addr, tmpaddr, list, list) {
			list_del(&addr->list);
			free(addr);
		}
	}
	return false;
}

enum scfg_keys {
	SCFG_KEY_TYPE,
	SCFG_KEY_NAME,
	SCFG_KEY_PORT,
	SCFG_KEY_LOCAL,
	SCFG_KEY_REMOTE,
	SCFG_KEY_INVALID
};

enum value_type {
	VAL_TYPE_STRING,
	VAL_TYPE_UINT16,
	VAL_TYPE_ADDR,
	VAL_TYPE_INVALID
};

struct scfg_key_map {
	const char *key_name;
	enum scfg_keys key_value;
	enum value_type value_type;
} scfg_key_map[] = {
	{
		.key_name = "type",
		.key_value = SCFG_KEY_TYPE,
		.value_type = VAL_TYPE_STRING,
	}, {
		.key_name = "name",
		.key_value = SCFG_KEY_NAME,
		.value_type = VAL_TYPE_STRING,
	}, {
		.key_name = "port",
		.key_value = SCFG_KEY_PORT,
		.value_type = VAL_TYPE_UINT16,
	}, {
		.key_name = "local",
		.key_value = SCFG_KEY_LOCAL,
		.value_type = VAL_TYPE_ADDR,
	}, {
		.key_name = "remote",
		.key_value = SCFG_KEY_REMOTE,
		.value_type = VAL_TYPE_ADDR,
	}
};

union cfg_value {
	const char *str;
	uint16_t uint16;
	struct list_head addr_list;
};


static void
line_get_key_value(char *line, enum scfg_keys *rkey, union cfg_value *rvalue)
{
	char *tmp, *key;
	int i;

	*rkey = SCFG_KEY_INVALID;
	if (!line)
		return;

	tmp = line;
	while (isspace(*tmp))
		tmp++;

	if (*tmp == '\0')
		return;

	key = tmp;
	while (*tmp != '\0' && !isspace(*tmp))
		tmp++;

	if (*tmp == '\0')
		return;

	*tmp = '\0';
	tmp++;

	while (isspace(*tmp))
		tmp++;

	if (*tmp != '=')
		return;

	tmp++;
	while (isspace(*tmp))
		tmp++;

	if (*tmp == '\0')
		return;

	for (i = 0; i < ARRAY_SIZE(scfg_key_map); i++) {
		if (strcmp(scfg_key_map[i].key_name, key))
			continue;

		switch (scfg_key_map[i].value_type) {

		case VAL_TYPE_STRING:
			rvalue->str = tmp;
			break;

		case VAL_TYPE_UINT16: {
			uint16_t v;

			if (strtou16_strict(tmp, &v) < 0)
				return;
			rvalue->uint16 = v;
			break;
		}

		case VAL_TYPE_ADDR: {
			if (!strtosockaddrs(tmp, &rvalue->addr_list))
				return;
			if (list_empty(&rvalue->addr_list)) {
				fprintf(stderr, "VAL_TYPE_ADDR with zero list!?\n");
				return;
			} else {
				int i = 0;
				struct list_head *pos;

				list_for_each(pos, &rvalue->addr_list)
					i++;
				fprintf(stderr, "VAL_TYPE_ADDR with list %i entries\n", i);
			}
			break;
		}

		case VAL_TYPE_INVALID:
			/* fall through */
		default:
			return;
		}
		*rkey = scfg_key_map[i].key_value;
		break;
	}
}

static void
scfg_parse(struct cfg *cfg, struct server *scfg)
{
	char *pos = &scfg->buf[0];
	char *line;

	eat_whitespace_and_comments(&pos);

	line = get_line(&pos);
	if (!line) {
		printf("Cfg: premature EOF\n");
		return;
	}

	if (strcmp(line, "[server]")) {
		printf("Invalid line: %s\n", line);
		return;
	}

	while (true) {
		enum scfg_keys key;
		union cfg_value value;

		eat_whitespace_and_comments(&pos);
		line = get_line(&pos);
		printf("Examining line: %s\n", line);
		line_get_key_value(line, &key, &value);
		if (key == SCFG_KEY_INVALID)
			break;
		printf("Got a key-value pair: %i = something\n", key);

		switch (key) {

		case SCFG_KEY_TYPE:
			if (!strcmp(value.str, "proxy")) {
				if (!server_set_type(cfg, scfg, SERVER_TYPE_PROXY))
					return;
			} else if (!strcmp(value.str, "announce")) {
				if (!server_set_type(cfg, scfg, SERVER_TYPE_ANNOUNCE))
					return;
			}
			break;

		case SCFG_KEY_NAME:
			if (!server_set_pretty_name(cfg, scfg, value.str))
				return;
			break;

		case SCFG_KEY_PORT:
			if (!server_set_port(cfg, scfg, value.uint16))
				return;
			break;

		case SCFG_KEY_LOCAL: {
			struct sockaddr_in46 *addr, *tmp;

			list_for_each_entry_safe(addr, tmp, &value.addr_list, list) {
				list_del(&addr->list);
				server_add_local(cfg, scfg, addr);
			}
			break;
		}

		case SCFG_KEY_REMOTE: {
			struct sockaddr_in46 *addr, *tmp;

			list_for_each_entry_safe(addr, tmp, &value.addr_list, list) {
				list_del(&addr->list);
				server_add_remote(cfg, scfg, addr);
			}
			break;
		}

		case SCFG_KEY_INVALID:
		default:
			break;
		}
	}

	//printf("Cfg:\n%s\n\n", pos);
}

static void
scfg_read_cb(struct cfg *cfg, struct uring_task *task, int res)
{
	struct server *scfg = container_of(task, struct server, task);

	printf("Asked to parse server cfg %s (bytes %i)\n", scfg->name, res);

	if (res < 0) {
		perrordie("read");
	} else if (res > 0) {
		scfg->len += res;
		if (scfg->len + 1 >= sizeof(scfg->buf)) {
			fprintf(stderr, "Server config too large\n");
			server_delete(cfg, scfg);
			return;
		}

		uring_read(cfg, &scfg->task, scfg->buf + scfg->len, sizeof(scfg->buf) - scfg->len, scfg->len, scfg_read_cb);
		return;
	} else {
		/* EOF */
		scfg->buf[scfg->len] = '\0';
		uring_task_close_fd(cfg, &scfg->task);
		scfg_parse(cfg, scfg);
		server_commit(cfg, scfg);
	}
}

static void 
scfg_open_cb(struct cfg *cfg, struct uring_task *task, int res)
{
	struct server *scfg = container_of(task, struct server, task);

	if (res < 0) {
		fprintf(stderr, "Open failed\n");
		server_delete(cfg, scfg);
		return;
	}

	printf("Asked to read server cfg %s (fd %i)\n", scfg->name, res);
	uring_task_set_fd(&scfg->task, res);
	scfg->len = 0;
	uring_read(cfg, &scfg->task, scfg->buf, sizeof(scfg->buf), 0, scfg_read_cb);
}

static bool
scfg_valid_filename(const char *name)
{
	const char *suffix;

	if (!name)
		return false;
	if (name[0] == '\0')
		return false;
	if (name[0] == '.')
		return false;
	if ((suffix = strrchr(name, '.')) == NULL)
		return false;
	if (strcmp(suffix, ".server"))
		return false;

	return true;
}

struct inotify_ev {
	struct uring_task task;
	char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event))));
};

static void
inotify_free(struct uring_task *task)
{
	struct inotify_ev *iev = container_of(task, struct inotify_ev, task);
	struct cfg *cfg = container_of(task->parent, struct cfg, task);

	fprintf(stderr, "%s called\n", __func__);
	if (!iev || !cfg)
		die("%s: iev or cfg is NULL!?\n", __func__);

	free(iev);
	cfg->iev = NULL;
	uring_task_put(cfg, &cfg->task);
}

static void
inotify_event_dump(const struct inotify_event *event)
{
	printf("Event:\n");
	printf(" * WD     : %i\n", event->wd);
	printf(" * Cookie : %" PRIu32 "\n", event->cookie);
	printf(" * Length : %" PRIu32 "\n", event->len);
	printf(" * Name   : %s\n", event->name);
	printf(" * Mask   : %" PRIu32 "\n", event->mask);
	if (event->mask & IN_ACCESS)
		printf("\tIN_ACCESS\n");
	else if(event->mask & IN_MODIFY)
		printf("\tIN_MODIFY\n");
	else if(event->mask & IN_ATTRIB)
		printf("\tIN_ATTRIB\n");
	else if(event->mask & IN_CLOSE_WRITE)
		printf("\tIN_CLOSE_WRITE\n");
	else if(event->mask & IN_CLOSE_NOWRITE)
		printf("\tIN_CLOSE_NOWRITE\n");
	else if(event->mask & IN_OPEN)
		printf("\tIN_OPEN\n");
	else if(event->mask & IN_MOVED_FROM)
		printf("\tIN_MOVED_FROM\n");
	else if(event->mask & IN_MOVED_TO)
		printf("\tIN_MOVED_TO\n");
	else if(event->mask & IN_CREATE)
		printf("\tIN_CREATE\n");
	else if(event->mask & IN_DELETE)
		printf("\tIN_DELETE\n");
	else if(event->mask & IN_DELETE_SELF)
		printf("\tIN_DELETE_SELF\n");
	else if(event->mask & IN_MOVE_SELF)
		printf("\tIN_MOVE_SELF\n");
	else if(event->mask & IN_UNMOUNT)
		printf("\tIN_UNMOUNT\n");
	else if(event->mask & IN_Q_OVERFLOW)
		printf("\tIN_Q_OVERFLOW\n");
	else if(event->mask & IN_IGNORED)
		printf("\tIN_IGNORED\n");
	printf("\n");
}

static void
inotify_cb(struct cfg *cfg, struct uring_task *task, int res)
{
	struct inotify_ev *iev = container_of(task, struct inotify_ev, task);
	const struct inotify_event *event;
	char *ptr;
	struct server *scfg;

	fprintf(stderr, "%s: ret is %i (ref %u)\n", __func__, res, task->refcount);

	if (task->dead) {
		fprintf(stderr, "%s: task is dead\n", __func__);
		uring_task_put(cfg, task);
		return;
	}

	if (res <= 0)
		perrordie("inotify_read");

	for (ptr = iev->buf; ptr < iev->buf + res; ptr += sizeof(struct inotify_event) + event->len) {
		event = (const struct inotify_event *)ptr;

		if (debuglvl > 0)
			inotify_event_dump(event);

		if (event->mask & (IN_IGNORED | IN_MOVE_SELF | IN_DELETE_SELF | IN_UNMOUNT))
			die("Configuration directory gone, exiting\n");

		if (event->mask & IN_Q_OVERFLOW) {
			error("inotify queue overflow!\n");
			continue;
		}

		if (!scfg_valid_filename(event->name))
			continue;

		if (event->mask & (IN_MOVED_FROM | IN_DELETE))
			server_delete_by_name(cfg, event->name);
		else if (event->mask & (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) {
			scfg = server_new(cfg, event->name);
			uring_openat(cfg, &scfg->task, event->name, scfg_open_cb);
		} else
			error("inotify: weird, unknown event: 0x%08x\n", event->mask);
	}

	uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), 0, inotify_cb);
}

void
inotify_refdump(struct inotify_ev *iev)
{
	uring_task_refdump(&iev->task);
}

void
scfg_stop_monitor_dir(struct cfg *cfg)
{
	if (!cfg->iev) {
		fprintf(stderr, "%s called with no iev!\n", __func__);
		return;
	}

	fprintf(stderr, "%s called, closing fd %i\n", __func__, cfg->iev->task.fd);
	uring_cancel(cfg, &cfg->iev->task);
	cfg->iev = NULL;
}

void
scfg_monitor_dir(struct cfg *cfg)
{
	int ifd;
	int iwd;
	struct inotify_ev *iev;

	iev = malloc(sizeof(*iev));
	if (!iev)
		perrordie("malloc");

	ifd = inotify_init1(IN_CLOEXEC);
	if (ifd < 0)
		perrordie("inotify_init1");

	/* ln = IN_CREATE, cp/vi/mv = IN_CREATE, IN_OPEN, IN_CLOSE_WRITE */
	iwd = inotify_add_watch(ifd, ".",
				IN_CLOSE_WRITE | IN_DELETE | IN_CREATE |
				IN_DELETE_SELF | IN_MOVE_SELF | IN_MOVED_TO |
				IN_MOVED_FROM | IN_DONT_FOLLOW |
				IN_EXCL_UNLINK | IN_ONLYDIR );
	if (iwd < 0)
		perrordie("inotify_add_watch");

	uring_task_init(&iev->task, "iev", &cfg->task, inotify_free);
	uring_task_set_fd(&iev->task, ifd);
	cfg->iev = iev;
	uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), 0, inotify_cb);
}

void
scfg_read_all(struct cfg *cfg)
{

	DIR *cfgdir;
	struct dirent *dent;
	struct server *scfg;

	cfgdir = opendir(".");
	if (!cfgdir) {
		perror("opendir");
		free(cfg);
		return;
	}

	while ((dent = readdir(cfgdir)) != NULL) {
		char *suffix;

		if (dent->d_name[0] == '.')
			continue;
		if (dent->d_type != DT_REG && dent->d_type != DT_UNKNOWN)
			continue;
		if ((suffix = strrchr(dent->d_name, '.')) == NULL)
			continue;
		if (strcmp(suffix, ".server"))
			continue;

		scfg = server_new(cfg, dent->d_name);
		uring_openat(cfg, &scfg->task, dent->d_name, scfg_open_cb);
	}

	closedir(cfgdir);
}