devd.hh revision 114086
1/*-
2 * Copyright (c) 2002-2003 M. Warner Losh.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sbin/devd/devd.hh 114086 2003-04-26 20:59:04Z imp $
27 */
28
29#ifndef DEVD_HH
30#define DEVD_HH
31
32class config;
33
34/**
35 * var_list is a collection of variables.  These collections of variables
36 * are stacked up and popped down for each event that we have to process.
37 * We have multiple levels so that we can push variables that are unique
38 * to the event in question, in addition to having global variables.  This
39 * allows for future flexibility.
40 */
41class var_list
42{
43public:
44	var_list() {}
45	virtual ~var_list() {}
46	/** Set a variable in this var list.
47	 */
48	void set_variable(const std::string &var, const std::string &val);
49	/** Get the variable out of this, and no other, var_list.  If
50	 * no variable of %var is set, then %bogus will be returned.
51	 */
52	const std::string &get_variable(const std::string &var) const;
53	/** Is there a variable of %var set in thi stable?
54	 */
55	bool is_set(const std::string &var) const;
56	/** A completely bogus string.
57	 */
58	static const std::string bogus;
59	static const std::string nothing;
60private:
61	std::map<std::string, std::string> _vars;
62};
63
64/**
65 * eps is short for event_proc_single.  It is a single entry in an
66 * event_proc.  Each keyword needs its own subclass from eps.
67 */
68class eps
69{
70public:
71	eps() {}
72	virtual ~eps() {}
73	/** Does this eps match the current config?
74	 */
75	virtual bool do_match(config &) = 0;
76	/** Perform some action for this eps.
77	 */
78	virtual bool do_action(config &) = 0;
79};
80
81/**
82 * match is the subclass used to match an individual variable.  Its
83 * actions are nops.
84 */
85class match : public eps
86{
87public:
88	match(config &, const char *var, const char *re);
89	virtual ~match();
90	virtual bool do_match(config &);
91	virtual bool do_action(config &) { return true; }
92private:
93	std::string _var;
94	std::string _re;
95	regex_t _regex;
96};
97
98/**
99 * action is used to fork a process.  It matches everything.
100 */
101class action : public eps
102{
103public:
104	action(const char *cmd);
105	virtual ~action();
106	virtual bool do_match(config &) { return true; }
107	virtual bool do_action(config &);
108private:
109	std::string _cmd;
110};
111
112class event_proc
113{
114public:
115	event_proc();
116	virtual ~event_proc();
117	int get_priority() const { return (_prio); }
118	void set_priority(int prio) { _prio = prio; }
119	void add(eps *);
120	bool matches(config &);
121	bool run(config &);
122private:
123	int _prio;
124	std::vector<eps *> _epsvec;
125};
126
127class config
128{
129public:
130	config() : _pidfile("") { push_var_table(); }
131	virtual ~config() { reset(); }
132	void add_attach(int, event_proc *);
133	void add_detach(int, event_proc *);
134	void add_directory(const char *);
135	void add_nomatch(int, event_proc *);
136	void set_pidfile(const char *);
137	void reset();
138	void parse();
139	void drop_pidfile();
140	void push_var_table();
141	void pop_var_table();
142	void set_variable(const char *var, const char *val);
143	const std::string &get_variable(const std::string &var);
144	const std::string expand_string(const std::string &var);
145	char *set_vars(char *);
146	void find_and_execute(char);
147protected:
148	void sort_vector(std::vector<event_proc *> &);
149	void parse_one_file(const char *fn);
150	void parse_files_in_dir(const char *dirname);
151	void expand_one(const char *&src, std::string &dst);
152	bool is_id_char(char);
153	bool chop_var(char *&buffer, char *&lhs, char *&rhs);
154private:
155	std::vector<std::string> _dir_list;
156	std::string _pidfile;
157	std::vector<var_list *> _var_list_table;
158	std::vector<event_proc *> _attach_list;
159	std::vector<event_proc *> _detach_list;
160	std::vector<event_proc *> _nomatch_list;
161};
162
163#endif /* DEVD_HH */
164