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