summaryrefslogtreecommitdiffstats
path: root/sbin/devd/devd.cc
blob: aa6c7045d12d2990b03e1b1e2cfc83623d37e9cf (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
/*-
 * Copyright (c) 2002 M. Warner Losh.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * DEVD control daemon.
 */

// TODO list:
//	o rewrite the main loop:
//	  - find best match
//	  - execute it.
//	o need to insert the event_proc structures in order of priority.  
//        bigger numbers mean higher priority.
//	o devd.conf and devd man pages need a lot of help:
//	  - devd.conf needs to lose the warning about zone files.
//	  - devd.conf needs more details on the supported statements.
//	  - devd.conf needs an example or two.

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/types.h>

#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <regex.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <map>
#include <string>
#include <vector>

#include "devd.h"

#define CF "/etc/devd.conf"

using namespace std;

extern FILE *yyin;
extern int lineno;

int dflag;
int romeo_must_die = 0;

static void event_loop(void);
static void usage(void);

class config;


class var_list
{
public:
	var_list() {}
	virtual ~var_list() {}
	void set_variable(const string &var, const string &val);
	const string &get_variable(const string &var) const;
	bool is_set(const string &var) const;
	static const string bogus;
	static const string nothing;
private:
	map<string, string> _vars;
};

class eps
{
public:
	eps() {}
	virtual ~eps() {}
	virtual bool do_match(config &) = 0;
	virtual bool do_action(config &) = 0;
};

class match : public eps
{
public:
	match(config &, const char *var, const char *re);
	virtual ~match();
	virtual bool do_match(config &);
	virtual bool do_action(config &) { return true; }
private:
	string _var;
	string _re;
	regex_t _regex;
};

class action : public eps
{
public:
	action(const char *cmd);
	virtual ~action();
	virtual bool do_match(config &) { return true; }
	virtual bool do_action(config &);
private:
	string _cmd;
};

class event_proc
{
public:
	event_proc();
	virtual ~event_proc();
	int get_priority() { return (_prio); }
	void set_priority(int prio) { _prio = prio; }
	void add(eps *);
	bool matches(config &);
	bool run(config &);
private:
	int _prio;
	vector<eps *> _epsvec;
};

class config
{
public:
	config() : _pidfile("") { push_var_table(); }
	virtual ~config() { reset(); }
	void add_attach(int, event_proc *);
	void add_detach(int, event_proc *);
	void add_directory(const char *);
	void add_nomatch(int, event_proc *);
	void set_pidfile(const char *);
	void reset();
	void parse();
	void drop_pidfile();
	void push_var_table();
	void pop_var_table();
	void set_variable(const char *var, const char *val);
	const string &get_variable(const string &var);
	const string expand_string(const string &var);
protected:
	void parse_one_file(const char *fn);
	void parse_files_in_dir(const char *dirname);
	void expand_one(const char *&src, char *&dst, char *eod);
private:
	vector<string> _dir_list;
	string _pidfile;
	vector<var_list *> _var_list_table;
	vector<event_proc *> _attach_list;
	vector<event_proc *> _detach_list;
	vector<event_proc *> _nomatch_list;
};

config cfg;

event_proc::event_proc() : _prio(-1)
{
	// nothing
}

event_proc::~event_proc()
{
	vector<eps *>::const_iterator i;

	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
		delete *i;
	_epsvec.clear();
}

void
event_proc::add(eps *eps)
{
	_epsvec.push_back(eps);
}

bool
event_proc::matches(config &c)
{
	vector<eps *>::const_iterator i;

	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
		if (!(*i)->do_match(c))
			return (false);
	return (true);
}

bool
event_proc::run(config &c)
{
	vector<eps *>::const_iterator i;
		
	for (i = _epsvec.begin(); i != _epsvec.end(); i++)
		if (!(*i)->do_action(c))
			return (false);
	return (true);
}

action::action(const char *cmd)
	: _cmd(cmd) 
{
	// nothing
}

action::~action()
{
	// nothing
}

bool
action::do_action(config &c)
{
	// xxx
	::system(c.expand_string(_cmd).c_str());
	return (true);
}

match::match(config &c, const char *var, const char *re)
	: _var(var), _re(re)
{
	string pattern = "^";
	pattern.append(c.expand_string(_re));
	pattern.append("$");
	regcomp(&_regex, pattern.c_str(), REG_EXTENDED | REG_NOSUB);
}

match::~match()
{
	regfree(&_regex);
}

bool
match::do_match(config &c)
{
	string value = c.get_variable(_var);
	bool retval;

	retval = (regexec(&_regex, value.c_str(), 0, NULL, 0) == 0);
	return retval;
}

const string var_list::bogus = "_$_$_$_$_B_O_G_U_S_$_$_$_$_";
const string var_list::nothing = "";

const string &
var_list::get_variable(const string &var) const
{
	map<string, string>::const_iterator i;

	i = _vars.find(var);
	if (i == _vars.end())
		return var_list::bogus;
	return (i->second);
}

bool
var_list::is_set(const string &var) const
{
	return (_vars.find(var) != _vars.end());
}

void
var_list::set_variable(const string &var, const string &val)
{
	_vars[var] = val;
}

void
config::reset(void)
{
	_dir_list.clear();
	_var_list_table.clear();
	// XXX need to cleanup _{attach,detach,nomatch}_list
}

void
config::parse_one_file(const char *fn)
{
	if (dflag)
		printf("Parsing %s\n", fn);
	yyin = fopen(fn, "r");
	if (yyin == NULL)
		err(1, "Cannot open config file %s", fn);
	if (yyparse() != 0)
		errx(1, "Cannot parse %s at line %d", fn, lineno);
	fclose(yyin);
}

void
config::parse_files_in_dir(const char *dirname)
{
	DIR *dirp;
	struct dirent *dp;
	char path[PATH_MAX];

	if (dflag)
		printf("Parsing files in %s\n", dirname);
	dirp = opendir(dirname);
	if (dirp == NULL)
		return;
	readdir(dirp);		/* Skip . */
	readdir(dirp);		/* Skip .. */
	while ((dp = readdir(dirp)) != NULL) {
		if (strcmp(dp->d_name + dp->d_namlen - 5, ".conf") == 0) {
			snprintf(path, sizeof(path), "%s/%s",
			    dirname, dp->d_name);
			parse_one_file(path);
		}
	}
}

void
config::parse(void)
{
	vector<string>::const_iterator i;

	parse_one_file(CF);
	for (i = _dir_list.begin(); i != _dir_list.end(); i++)
		parse_files_in_dir((*i).c_str());
}

void
config::drop_pidfile()
{
	FILE *fp;
	
	if (_pidfile == "")
		return;
	fp = fopen(_pidfile.c_str(), "w");
	if (fp == NULL)
		return;
	fprintf(fp, "%d\n", getpid());
	fclose(fp);
}

void
config::add_attach(int prio, event_proc *p)
{
	p->set_priority(prio);
	_attach_list.push_back(p);
}

void
config::add_detach(int prio, event_proc *p)
{
	p->set_priority(prio);
	_detach_list.push_back(p);
}

void
config::add_directory(const char *dir)
{
	_dir_list.push_back(string(dir));
}

void
config::add_nomatch(int prio, event_proc *p)
{
	p->set_priority(prio);
	_nomatch_list.push_back(p);
}

void
config::set_pidfile(const char *fn)
{
	_pidfile = string(fn);
}

void
config::push_var_table()
{
	var_list *vl;
	
	vl = new var_list();
	_var_list_table.push_back(vl);
}

void
config::pop_var_table()
{
	delete _var_list_table.back();
	_var_list_table.pop_back();
}

void
config::set_variable(const char *var, const char *val)
{
	_var_list_table.back()->set_variable(var, val);
}

const string &
config::get_variable(const string &var)
{
	vector<var_list *>::reverse_iterator i;

	for (i = _var_list_table.rbegin(); i != _var_list_table.rend(); i++) {
		if ((*i)->is_set(var))
			return (var);
	}
	return (var_list::nothing);
}

// Hey script |<idz, here's a routine chock-full-o-buffer-overflows.
// XXX
// imp should learn how to make effective use of the string class for the shit.
void
config::expand_one(const char *&src, char *&dst, char *)
{
	int count;
	const char *var;
	char buffer[1024];
	string varstr;

	// $$ -> $
	if (*src == '$') {
		*dst++ = *src++;
		return;
	}
		
	// $(foo) -> $(foo)
	// Not sure if I want to support this or not, so for now we just pass it through.
	if (*src == '(') {
		*dst++ = '$';
		count = 1;
		while (count > 0) {
			if (*src == ')')
				count--;
			else if (*src == '(')
				count++;
			*dst++ = *src++;
		}
		return;
	}
	
	// ${^A-Za-z] -> $\1
	if (!isalpha(*src)) {
		*dst++ = '$';
		*dst++ = *src++;
		return;
	}

	// $var -> replace with value
	var = src++;
	while (*src && isalpha(*src) || isdigit(*src) || *src == '_' || *src == '-')
		src++;
	memcpy(buffer, var, src - var);
	buffer[src - var] = '\0';
	varstr = get_variable(buffer);
	strcpy(dst, varstr.c_str());
	dst += strlen(dst);
}

const string
config::expand_string(const string &s)
{
	const char *src;
	char *dst;
	char buffer[1024];

	src = s.c_str();
	dst = buffer;
	while (*src) {
		if (*src == '$')
			expand_one(++src, dst, buffer + sizeof(buffer));
		else
			*dst++ = *src++;
	}
	*dst++ = '\0';

	return (buffer);
}


static void
process_event(const char *buffer)
{
	char type;
	char cmd[1024];
	char *sp;

	// XXX should involve config
	// XXX and set some variables
	// XXX run the list and so forth

	// Ignore unknown devices for now.
	if (*buffer == '?')
		return;
	type = *buffer++;
	sp = strchr(buffer, ' ');
	if (sp == NULL)
		return;	/* Can't happen? */
	*sp = '\0';
	snprintf(cmd, sizeof(cmd), "/etc/devd-generic %s %s", buffer,
	    type == '+' ? "start" : "stop");
	if (dflag)
		printf("Trying '%s'\n", cmd);
	system(cmd);
}

static void
event_loop(void)
{
	int rv;
	int fd;
	char buffer[DEVCTL_MAXBUF];

	fd = open(PATH_DEVCTL, O_RDONLY);
	if (fd == -1)
		err(1, "Can't open devctl");
	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
		err(1, "Can't set close-on-exec flag");
	while (1) {
		if (romeo_must_die)
			break;
		rv = read(fd, buffer, sizeof(buffer) - 1);
		if (rv > 0) {
			buffer[rv] = '\0';
			while (buffer[--rv] == '\n')
				buffer[rv] = '\0';
			process_event(buffer);
		} else if (rv < 0) {
			if (errno != EINTR)
				break;
		} else {
			/* EOF */
			break;
		}
	}
	close(fd);
}

/*
 * functions that the parser uses.
 */
void
add_attach(int prio, event_proc *p)
{
	cfg.add_attach(prio, p);
}

void
add_detach(int prio, event_proc *p)
{
	cfg.add_detach(prio, p);
}

void
add_directory(const char *dir)
{
	cfg.add_directory(dir);
	free(const_cast<char *>(dir));
}

void
add_nomatch(int prio, event_proc *p)
{
	cfg.add_nomatch(prio, p);
}

event_proc *
add_to_event_proc(event_proc *ep, eps *eps)
{
	if (ep == NULL)
		ep = new event_proc();
	ep->add(eps);
	return (ep);
}

eps *
new_action(const char *cmd)
{
	eps *e = new action(cmd);
	free(const_cast<char *>(cmd));
	return (e);
}

eps *
new_match(const char *var, const char *re)
{
	eps *e = new match(cfg, var, re);
	free(const_cast<char *>(var));
	free(const_cast<char *>(re));
	return (e);
}

void
set_pidfile(const char *name)
{
	cfg.set_pidfile(name);
	free(const_cast<char *>(name));
}

void
set_variable(const char *var, const char *val)
{
	cfg.set_variable(var, val);
	free(const_cast<char *>(var));
	free(const_cast<char *>(val));
}



static void
gensighand(int)
{
	romeo_must_die++;
	_exit(0);
}

static void
usage()
{
	fprintf(stderr, "usage: %s [-d]", getprogname());
	exit(1);
}

/*
 * main
 */
int
main(int argc, char **argv)
{
	int ch;

	while ((ch = getopt(argc, argv, "d")) != -1) {
		switch (ch) {
		case 'd':
			dflag++;
			break;
		default:
			usage();
		}
	}

	cfg.parse();
	if (!dflag)
		daemon(0, 0);
	cfg.drop_pidfile();
	signal(SIGHUP, gensighand);
	signal(SIGINT, gensighand);
	signal(SIGTERM, gensighand);
	event_loop();
	return (0);
}
OpenPOWER on IntegriCloud