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