11 #include <sys/types.h>
13 #include <sys/resource.h>
17 #include "dinit-util.h"
18 #include "service-constants.h"
19 #include "load-service.h"
20 #include "options-processing.h"
22 // dinitcheck: utility to check Dinit configuration for correctness/lint
24 using string = std::string;
25 using string_iterator = std::string::iterator;
27 // prelim_dep: A preliminary (unresolved) service dependency
32 dependency_type dep_type;
34 prelim_dep(const std::string &name_p, dependency_type dep_type_p)
35 : name(name_p), dep_type(dep_type_p) { }
36 prelim_dep(std::string &&name_p, dependency_type dep_type_p)
37 : name(std::move(name_p)), dep_type(dep_type_p) { }
43 service_record(std::string name_p, std::list<prelim_dep> dependencies_p)
44 : name(name_p), dependencies(dependencies_p) {}
47 std::list<prelim_dep> dependencies;
49 bool visited = false; // flag used to detect cyclic dependencies
50 bool cycle_free = false;
53 using service_set_t = std::map<std::string, service_record *>;
55 service_record *load_service(service_set_t &services, const std::string &name,
56 const service_dir_pathlist &service_dirs);
58 // Add some missing standard library functionality...
59 template <typename T> bool contains(std::vector<T> vec, const T& elem)
61 return std::find(vec.begin(), vec.end(), elem) != vec.end();
64 static bool errors_found = false;
66 int main(int argc, char **argv)
70 service_dir_opt service_dir_opts;
71 bool am_system_init = (getuid() == 0);
73 std::vector<std::string> services_to_check;
75 // Process command line
77 for (int i = 1; i < argc; i++) {
78 if (argv[i][0] == '-') {
80 if (strcmp(argv[i], "--services-dir") == 0 || strcmp(argv[i], "-d") == 0) {
82 service_dir_opts.set_specified_service_dir(argv[i]);
85 cerr << "dinitcheck: '--services-dir' (-d) requires an argument" << endl;
89 else if (strcmp(argv[i], "--help") == 0) {
90 cout << "dinitcheck: check dinit service descriptions\n"
91 " --help display help\n"
92 " --services-dir <dir>, -d <dir>\n"
93 " set base directory for service description\n"
95 " <service-name> check service with name <service-name>\n";
99 std::cerr << "dinitcheck: Unrecognized option: '" << argv[i] << "' (use '--help' for help)\n";
104 services_to_check.push_back(argv[i]);
109 service_dir_opts.build_paths(am_system_init);
111 if (services_to_check.empty()) {
112 services_to_check.push_back("boot");
115 size_t num_services_to_check = services_to_check.size();
117 // Load named service(s)
118 // - load the service, store dependencies as strings
121 std::map<std::string, service_record *> service_set;
123 for (size_t i = 0; i < services_to_check.size(); ++i) {
124 const std::string &name = services_to_check[i];
125 std::cout << "Checking service: " << name << "...\n";
127 service_record *sr = load_service(service_set, name, service_dir_opts.get_paths());
128 service_set[name] = sr;
129 // add dependencies to services_to_check
130 for (auto &dep : sr->dependencies) {
131 if (service_set.count(dep.name) == 0 && !contains(services_to_check, dep.name)) {
132 services_to_check.push_back(dep.name);
136 catch (service_load_exc &exc) {
137 std::cerr << "Unable to load service '" << name << "': " << exc.exc_description << "\n";
142 // Check for circular dependencies
143 std::vector<std::tuple<service_record *, size_t>> service_chain;
145 for (size_t i = 0; i < num_services_to_check; ++i) {
146 service_record *root = service_set[services_to_check[i]];
147 if (root->visited) continue;
149 // invariant: service_chain is empty
150 service_chain.emplace_back(root, 0);
152 // Depth first traversal. If we find a link (dependency) on a service already visited (but not
153 // marked as cycle-free), we know then that we've found a cycle.
155 auto n = service_chain.size() - 1;
156 auto &last = service_chain[n];
157 service_record *last_record = std::get<0>(last);
158 size_t &index = std::get<1>(last);
159 if (index >= last_record->dependencies.size()) {
160 // Processed all dependencies, go back up:
161 last_record->cycle_free = true;
162 service_chain.pop_back();
164 size_t &prev_index = std::get<1>(service_chain[n - 1]);
169 auto dep_it = std::next(last_record->dependencies.begin(), index);
170 service_record *next_link = service_set[dep_it->name];
171 if (next_link == nullptr) {
175 if (next_link->visited) {
176 if (! next_link->cycle_free) {
177 // We've found a cycle. Clear entries before the beginning of the cycle, then
179 auto first = std::find_if(service_chain.begin(), service_chain.end(),
180 [next_link](std::tuple<service_record *, size_t> &a) -> bool {
181 return std::get<0>(a) == next_link;
183 service_chain.erase(service_chain.begin(), first);
187 next_link->visited = true;
188 service_chain.emplace_back(next_link, 0);
191 // Report only one cycle; otherwise difficult to avoid reporting duplicates or overlapping
193 if (!service_chain.empty()) break;
196 if (!service_chain.empty()) {
198 std::cerr << "Found dependency cycle:\n";
199 for (auto chain_link : service_chain) {
200 std::cerr << " " << std::get<0>(chain_link)->name << " ->\n";
202 std::cerr << " " << std::get<0>(service_chain[0])->name << ".\n";
205 // TODO additional: check chain-to, other lint
207 if (! errors_found) {
208 std::cout << "No problems found.\n";
211 std::cout << "One or more errors found.\n";
214 return errors_found ? EXIT_FAILURE : EXIT_SUCCESS;
217 static void report_service_description_exc(service_description_exc &exc)
219 std::cerr << "Service '" << exc.service_name << "': " << exc.exc_description << "\n";
223 static void report_error(std::system_error &exc, const std::string &service_name)
225 std::cerr << "Service '" << service_name << "', error reading service description: " << exc.what() << "\n";
229 static void report_dir_error(const char *service_name, const std::string &dirpath)
231 std::cerr << "Service '" << service_name << "', error reading dependencies from directory " << dirpath
232 << ": " << strerror(errno) << "\n";
236 // Process a dependency directory - filenames contained within correspond to service names which
237 // are loaded and added as a dependency of the given type. Expected use is with a directory
238 // containing symbolic links to other service descriptions, but this isn't required.
239 // Failure to read the directory contents, or to find a service listed within, is not considered
241 static void process_dep_dir(const char *servicename,
242 const string &service_filename,
243 std::list<prelim_dep> &deplist, const std::string &depdirpath,
244 dependency_type dep_type)
246 std::string depdir_fname = combine_paths(parent_path(service_filename), depdirpath.c_str());
248 DIR *depdir = opendir(depdir_fname.c_str());
249 if (depdir == nullptr) {
250 report_dir_error(servicename, depdirpath);
255 dirent * dent = readdir(depdir);
256 while (dent != nullptr) {
257 char * name = dent->d_name;
258 if (name[0] != '.') {
259 deplist.emplace_back(name, dep_type);
261 dent = readdir(depdir);
265 report_dir_error(servicename, depdirpath);
271 service_record *load_service(service_set_t &services, const std::string &name,
272 const service_dir_pathlist &service_dirs)
275 using namespace dinit_load;
277 auto found = services.find(name);
278 if (found != services.end()) {
279 return found->second;
282 string service_filename;
283 ifstream service_file;
285 // Couldn't find one. Have to load it.
286 for (auto &service_dir : service_dirs) {
287 service_filename = service_dir.get_dir();
288 if (*(service_filename.rbegin()) != '/') {
289 service_filename += '/';
291 service_filename += name;
293 service_file.open(service_filename.c_str(), ios::in);
294 if (service_file) break;
297 if (! service_file) {
298 throw service_not_found(string(name));
301 service_settings_wrapper<prelim_dep> settings;
304 service_file.exceptions(ios::badbit);
307 process_service_file(name, service_file,
308 [&](string &line, string &setting, string_iterator &i, string_iterator &end) -> void {
310 auto process_dep_dir_n = [&](std::list<prelim_dep> &deplist, const std::string &waitsford,
311 dependency_type dep_type) -> void {
312 process_dep_dir(name.c_str(), service_filename, deplist, waitsford, dep_type);
315 auto load_service_n = [&](const string &dep_name) -> const string & {
320 process_service_line(settings, name.c_str(), line, setting, i, end, load_service_n, process_dep_dir_n);
322 catch (service_description_exc &exc) {
323 report_service_description_exc(exc);
327 catch (std::system_error &sys_err)
329 report_error(sys_err, name);
333 return new service_record(name, settings.depends);