1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
11 #include "load-fragment.h"
13 Manager* manager_new(void) {
16 if (!(m = new0(Manager, 1)))
19 if (!(m->names = hashmap_new(string_hash_func, string_compare_func)))
22 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
25 if (!(m->jobs_to_add = hashmap_new(trivial_hash_func, trivial_compare_func)))
28 if (!(m->jobs_to_remove = set_new(trivial_hash_func, trivial_compare_func)))
38 void manager_free(Manager *m) {
43 while ((n = hashmap_first(m->names)))
46 hashmap_free(m->names);
47 hashmap_free(m->jobs);
49 /* FIXME: This is incomplete */
51 hashmap_free(m->jobs_to_add);
52 set_free(m->jobs_to_remove);
57 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, Job **_ret) {
64 assert(type < _JOB_TYPE_MAX);
66 assert(mode < _JOB_MODE_MAX);
69 /* Check for conflicts, first against the jobs we shall
71 if ((other = hashmap_get(m->jobs_to_add, name))) {
73 if (other->type != type)
76 } else if (name->meta.job) {
78 if (name->meta.job->type != type) {
83 if ((r = set_put(m->jobs_to_remove, name->meta.job)) < 0)
88 if (!(ret = job_new(m, type, name)))
91 if ((r = hashmap_put(m->jobs_to_add, name, ret)) < 0)
94 if (type == JOB_START || type == JOB_VERIFY_STARTED || type == JOB_RESTART_FINISH) {
95 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
96 if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
98 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
99 if ((r = manager_add_job(m, type, dep, JOB_FAIL, NULL)) < 0)
101 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
102 if ((r = manager_add_job(m, type, dep, JOB_FAIL, NULL)) < 0)
104 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
105 if ((r = manager_add_job(m, JOB_VERIFY_STARTED, dep, mode, NULL)) < 0)
107 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
108 if ((r = manager_add_job(m, JOB_VERIFY_STARTED, dep, JOB_FAIL, NULL)) < 0)
110 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
111 if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
114 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
116 SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
117 if ((r = manager_add_job(m, type, dep, mode, NULL)) < 0)
133 Job *manager_get_job(Manager *m, uint32_t id) {
136 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
139 Name *manager_get_name(Manager *m, const char *name) {
143 return hashmap_get(m->names, name);
146 static int verify_type(Name *name) {
152 /* Checks that all aliases of this name have the same and valid type */
154 SET_FOREACH(n, name->meta.names, state) {
157 if ((t = name_type_from_string(n)) == _NAME_TYPE_INVALID)
160 if (name->meta.type == _NAME_TYPE_INVALID) {
165 if (name->meta.type != t)
169 if (name->meta.type == _NAME_TYPE_INVALID)
175 static int service_load_sysv(Service *s) {
178 /* Load service data from SysV init scripts, preferably with
184 static int name_load_fstab(Name *n) {
186 assert(n->meta.type == NAME_MOUNT || n->meta.type == NAME_AUTOMOUNT);
188 /* Load mount data from /etc/fstab */
193 static int snapshot_load(Snapshot *s) {
196 /* Load snapshots from disk */
201 static int name_load_dropin(Name *n) {
204 /* Load dependencies from drop-in directories */
209 static int load(Name *name) {
214 if (name->meta.state != NAME_STUB)
217 if ((r = verify_type(name)) < 0)
220 if (name->meta.type == NAME_SERVICE) {
222 /* Load a .service file */
223 if ((r = name_load_fragment(name)) == 0)
226 /* Load a classic init script */
228 if ((r = service_load_sysv(SERVICE(name))) == 0)
231 } else if (name->meta.type == NAME_MOUNT ||
232 name->meta.type == NAME_AUTOMOUNT) {
234 if ((r = name_load_fstab(name)) == 0)
237 } else if (name->meta.type == NAME_SNAPSHOT) {
239 if ((r = snapshot_load(SNAPSHOT(name))) == 0)
243 if ((r = name_load_fragment(name)) == 0)
247 name->meta.state = NAME_FAILED;
251 if ((r = name_load_dropin(name)) < 0)
254 name->meta.state = NAME_LOADED;
258 static int dispatch_load_queue(Manager *m) {
263 /* Make sure we are not run recursively */
264 if (m->dispatching_load_queue)
267 m->dispatching_load_queue = true;
269 /* Dispatches the load queue. Takes a name from the queue and
270 * tries to load its data until the queue is empty */
272 while ((meta = m->load_queue)) {
274 LIST_REMOVE(Meta, m->load_queue, meta);
277 m->dispatching_load_queue = false;
282 int manager_load_name(Manager *m, const char *name, Name **_ret) {
292 if (!name_is_valid(name))
295 /* This will load the service information files, but not actually
296 * start any services or anything */
298 if ((ret = manager_get_name(m, name)))
301 if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
304 if (!(ret = name_new(m)))
309 if (!(n = strdup(name))) {
314 if (set_put(ret->meta.names, n) < 0) {
320 if ((r = name_link(ret)) < 0) {
325 /* At this point the new entry is created and linked. However,
326 * not loaded. Now load this entry and all its dependencies
329 dispatch_load_queue(m);