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