xref: /dragonfly/sbin/devd/devd.hh (revision 409b4c59)
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: src/sbin/devd/devd.hh,v 1.5 2007/12/21 01:00:04 imp Exp $
27  * $DragonFly: src/sbin/devd/devd.hh,v 1.1 2008/10/03 00:26:21 hasso Exp $
28  */
29 
30 #ifndef DEVD_HH
31 #define DEVD_HH
32 
33 class config;
34 
35 /**
36  * var_list is a collection of variables.  These collections of variables
37  * are stacked up and popped down for each event that we have to process.
38  * We have multiple levels so that we can push variables that are unique
39  * to the event in question, in addition to having global variables.  This
40  * allows for future flexibility.
41  */
42 class var_list
43 {
44 public:
45 	var_list() {}
46 	virtual ~var_list() {}
47 	/** Set a variable in this var list.
48 	 */
49 	void set_variable(const std::string &var, const std::string &val);
50 	/** Get the variable out of this, and no other, var_list.  If
51 	 * no variable of %var is set, then %bogus will be returned.
52 	 */
53 	const std::string &get_variable(const std::string &var) const;
54 	/** Is there a variable of %var set in thi stable?
55 	 */
56 	bool is_set(const std::string &var) const;
57 	/** A completely bogus string.
58 	 */
59 	static const std::string bogus;
60 	static const std::string nothing;
61 private:
62 	std::map<std::string, std::string> _vars;
63 };
64 
65 /**
66  * eps is short for event_proc_single.  It is a single entry in an
67  * event_proc.  Each keyword needs its own subclass from eps.
68  */
69 class eps
70 {
71 public:
72 	eps() {}
73 	virtual ~eps() {}
74 	/** Does this eps match the current config?
75 	 */
76 	virtual bool do_match(config &) = 0;
77 	/** Perform some action for this eps.
78 	 */
79 	virtual bool do_action(config &) = 0;
80 };
81 
82 /**
83  * match is the subclass used to match an individual variable.  Its
84  * actions are nops.
85  */
86 class match : public eps
87 {
88 public:
89 	match(config &, const char *var, const char *re);
90 	virtual ~match();
91 	virtual bool do_match(config &);
92 	virtual bool do_action(config &) { return true; }
93 private:
94 	std::string _var;
95 	std::string _re;
96 	regex_t _regex;
97 };
98 
99 /**
100  * media is the subclass used to match an individual variable.  Its
101  * actions are nops.
102  */
103 class media : public eps
104 {
105 public:
106 	media(config &, const char *var, const char *type);
107 	virtual ~media();
108 	virtual bool do_match(config &);
109 	virtual bool do_action(config &) { return true; }
110 private:
111 	std::string _var;
112 	int _type;
113 };
114 
115 /**
116  * action is used to fork a process.  It matches everything.
117  */
118 class action : public eps
119 {
120 public:
121 	action(const char *cmd);
122 	virtual ~action();
123 	virtual bool do_match(config &) { return true; }
124 	virtual bool do_action(config &);
125 private:
126 	std::string _cmd;
127 };
128 
129 class event_proc
130 {
131 public:
132 	event_proc();
133 	virtual ~event_proc();
134 	int get_priority() const { return (_prio); }
135 	void set_priority(int prio) { _prio = prio; }
136 	void add(eps *);
137 	bool matches(config &);
138 	bool run(config &);
139 private:
140 	int _prio;
141 	std::vector<eps *> _epsvec;
142 };
143 
144 class config
145 {
146 public:
147 	config() { _pidfile = ""; push_var_table(); }
148 	virtual ~config() { reset(); }
149 	void add_attach(int, event_proc *);
150 	void add_detach(int, event_proc *);
151 	void add_directory(const char *);
152 	void add_nomatch(int, event_proc *);
153 	void add_notify(int, event_proc *);
154 	void set_pidfile(const char *);
155 	void reset();
156 	void parse();
157 	void open_pidfile();
158 	void write_pidfile();
159 	void remove_pidfile();
160 	void push_var_table();
161 	void pop_var_table();
162 	void set_variable(const char *var, const char *val);
163 	const std::string &get_variable(const std::string &var);
164 	const std::string expand_string(const std::string &var);
165 	char *set_vars(char *);
166 	void find_and_execute(char);
167 protected:
168 	void sort_vector(std::vector<event_proc *> &);
169 	void parse_one_file(const char *fn);
170 	void parse_files_in_dir(const char *dirname);
171 	void expand_one(const char *&src, std::string &dst);
172 	bool is_id_char(char);
173 	bool chop_var(char *&buffer, char *&lhs, char *&rhs);
174 private:
175 	std::vector<std::string> _dir_list;
176 	std::string _pidfile;
177 	std::vector<var_list *> _var_list_table;
178 	std::vector<event_proc *> _attach_list;
179 	std::vector<event_proc *> _detach_list;
180 	std::vector<event_proc *> _nomatch_list;
181 	std::vector<event_proc *> _notify_list;
182 };
183 
184 #endif /* DEVD_HH */
185