1133066Sdfr/*- 2133066Sdfr * Copyright (c) 2002-2003 M. Warner Losh. 3133066Sdfr * All rights reserved. 4133066Sdfr * 5133066Sdfr * Redistribution and use in source and binary forms, with or without 6133066Sdfr * modification, are permitted provided that the following conditions 7133066Sdfr * are met: 8133066Sdfr * 1. Redistributions of source code must retain the above copyright 9133066Sdfr * notice, this list of conditions and the following disclaimer. 10133066Sdfr * 2. Redistributions in binary form must reproduce the above copyright 11133066Sdfr * notice, this list of conditions and the following disclaimer in the 12133066Sdfr * documentation and/or other materials provided with the distribution. 13133066Sdfr * 14133066Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15133066Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16133066Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17133066Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18133066Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19133066Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20133066Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21133066Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22133066Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133066Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24133066Sdfr * SUCH DAMAGE. 25133066Sdfr * 26133066Sdfr * $FreeBSD$ 27133066Sdfr */ 28133066Sdfr 29133066Sdfr#ifndef DEVD_HH 30133066Sdfr#define DEVD_HH 31133066Sdfr 32133066Sdfrclass config; 33133066Sdfr 34133066Sdfr/** 35133066Sdfr * var_list is a collection of variables. These collections of variables 36133066Sdfr * are stacked up and popped down for each event that we have to process. 37133066Sdfr * We have multiple levels so that we can push variables that are unique 38133066Sdfr * to the event in question, in addition to having global variables. This 39133066Sdfr * allows for future flexibility. 40133066Sdfr */ 41133066Sdfrclass var_list 42133066Sdfr{ 43133066Sdfrpublic: 44133066Sdfr /** Set a variable in this var list. 45133066Sdfr */ 46133066Sdfr void set_variable(const std::string &var, const std::string &val); 47133066Sdfr /** Get the variable out of this, and no other, var_list. If 48133066Sdfr * no variable of %var is set, then %bogus will be returned. 49133066Sdfr */ 50133066Sdfr const std::string &get_variable(const std::string &var) const; 51133066Sdfr /** Is there a variable of %var set in thi stable? 52133066Sdfr */ 53133066Sdfr bool is_set(const std::string &var) const; 54133066Sdfr /** A completely bogus string. 55133066Sdfr */ 56133066Sdfr static const std::string bogus; 57133066Sdfr static const std::string nothing; 58133066Sdfrprivate: 59133066Sdfr std::map<std::string, std::string> _vars; 60133066Sdfr}; 61133066Sdfr 62133066Sdfr/** 63133066Sdfr * eps is short for event_proc_single. It is a single entry in an 64133066Sdfr * event_proc. Each keyword needs its own subclass from eps. 65133066Sdfr */ 66133066Sdfrstruct eps 67133066Sdfr{ 68133066Sdfrpublic: 69133066Sdfr virtual ~eps() {} 70133066Sdfr /** Does this eps match the current config? 71133066Sdfr */ 72133066Sdfr virtual bool do_match(config &) = 0; 73133066Sdfr /** Perform some action for this eps. 74133066Sdfr */ 75133066Sdfr virtual bool do_action(config &) = 0; 76133066Sdfr}; 77133066Sdfr 78133066Sdfr/** 79133066Sdfr * match is the subclass used to match an individual variable. Its 80133066Sdfr * actions are nops. 81133066Sdfr */ 82133066Sdfrclass match : public eps 83133066Sdfr{ 84133066Sdfrpublic: 85133066Sdfr match(config &, const char *var, const char *re); 86133066Sdfr virtual ~match(); 87133066Sdfr virtual bool do_match(config &); 88133066Sdfr virtual bool do_action(config &) { return true; } 89133066Sdfrprivate: 90133066Sdfr bool _inv; 91133066Sdfr std::string _var; 92133066Sdfr std::string _re; 93133066Sdfr regex_t _regex; 94133066Sdfr}; 95133066Sdfr 96133066Sdfr/** 97133066Sdfr * media is the subclass used to match an individual variable. Its 98133066Sdfr * actions are nops. 99133066Sdfr */ 100133066Sdfrclass media : public eps 101133066Sdfr{ 102133066Sdfrpublic: 103133066Sdfr media(config &, const char *var, const char *type); 104133066Sdfr virtual ~media(); 105133066Sdfr virtual bool do_match(config &); 106133066Sdfr virtual bool do_action(config &) { return true; } 107133066Sdfrprivate: 108133066Sdfr std::string _var; 109133066Sdfr int _type; 110133066Sdfr}; 111133066Sdfr 112133066Sdfr/** 113133066Sdfr * action is used to fork a process. It matches everything. 114133066Sdfr */ 115class action : public eps 116{ 117public: 118 action(const char *cmd); 119 virtual ~action(); 120 virtual bool do_match(config &) { return true; } 121 virtual bool do_action(config &); 122private: 123 std::string _cmd; 124}; 125 126struct event_proc 127{ 128public: 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; 136private: 137 int _prio; 138 std::vector<eps *> _epsvec; 139}; 140 141class config 142{ 143public: 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); 166protected: 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; 173private: 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