if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
return -EINVAL;
+ if (u->meta.type != _UNIT_TYPE_INVALID &&
+ UNIT_VTABLE(u)->no_alias &&
+ !set_isempty(u->meta.names))
+ return -EEXIST;
+
if (!(s = strdup(text)))
return -ENOMEM;
return r;
}
- if (u->meta.type == _UNIT_TYPE_INVALID)
+ if (u->meta.type == _UNIT_TYPE_INVALID) {
LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
- u->meta.type = t;
+ u->meta.type = t;
+
+ if (UNIT_VTABLE(u)->init)
+ UNIT_VTABLE(u)->init(u);
+ }
if (!u->meta.id)
u->meta.id = s;
bus_unit_send_removed_signal(u);
/* Detach from next 'bigger' objects */
-
- cgroup_bonding_free_list(u->meta.cgroup_bondings);
-
SET_FOREACH(t, u->meta.names, i)
hashmap_remove_value(u->meta.manager->units, t, u);
if (u->meta.in_cleanup_queue)
LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
+ /* Free data and next 'smaller' objects */
+ if (u->meta.job)
+ job_free(u->meta.job);
+
if (u->meta.load_state != UNIT_STUB)
if (UNIT_VTABLE(u)->done)
UNIT_VTABLE(u)->done(u);
- /* Free data and next 'smaller' objects */
- if (u->meta.job)
- job_free(u->meta.job);
+ cgroup_bonding_free_list(u->meta.cgroup_bondings);
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
bidi_set_free(u, u->meta.dependencies[d]);
if (other == u)
return 0;
- /* This merges 'other' into 'unit'. FIXME: This does not
- * rollback on failure. */
-
if (u->meta.type != u->meta.type)
return -EINVAL;
}
/* Common implementation for multiple backends */
-int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state) {
+int unit_load_fragment_and_dropin(Unit *u) {
int r;
assert(u);
- assert(new_state);
- assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
/* Load a .service file */
- if ((r = unit_load_fragment(u, new_state)) < 0)
+ if ((r = unit_load_fragment(u)) < 0)
return r;
- if (*new_state == UNIT_STUB)
+ if (u->meta.load_state == UNIT_STUB)
return -ENOENT;
/* Load drop-in directory data */
}
/* Common implementation for multiple backends */
-int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state) {
+int unit_load_fragment_and_dropin_optional(Unit *u) {
int r;
assert(u);
- assert(new_state);
- assert(*new_state == UNIT_STUB || *new_state == UNIT_LOADED);
/* Same as unit_load_fragment_and_dropin(), but whether
* something can be loaded or not doesn't matter. */
/* Load a .service file */
- if ((r = unit_load_fragment(u, new_state)) < 0)
+ if ((r = unit_load_fragment(u)) < 0)
return r;
- if (*new_state == UNIT_STUB)
- *new_state = UNIT_LOADED;
+ if (u->meta.load_state == UNIT_STUB)
+ u->meta.load_state = UNIT_LOADED;
/* Load drop-in directory data */
if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
int unit_load(Unit *u) {
int r;
- UnitLoadState res;
assert(u);
u->meta.in_load_queue = false;
}
+ if (u->meta.type == _UNIT_TYPE_INVALID)
+ return -EINVAL;
+
if (u->meta.load_state != UNIT_STUB)
return 0;
- if (UNIT_VTABLE(u)->init) {
- res = UNIT_STUB;
- if ((r = UNIT_VTABLE(u)->init(u, &res)) < 0)
+ if (UNIT_VTABLE(u)->load)
+ if ((r = UNIT_VTABLE(u)->load(u)) < 0)
goto fail;
- }
- if (res == UNIT_STUB) {
+ if (u->meta.load_state == UNIT_STUB) {
r = -ENOENT;
goto fail;
}
- u->meta.load_state = res;
assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
unit_add_to_dbus_queue(unit_follow_merge(u));
if (!UNIT_VTABLE(u)->stop)
return -EBADR;
- if (state == UNIT_DEACTIVATING)
- return 0;
-
unit_add_to_dbus_queue(u);
return UNIT_VTABLE(u)->stop(u);
}
retroactively_stop_dependencies(u);
}
- if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
-
+ /* Some names are special */
+ if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
- log_info("D-Bus became available, trying to reconnect.");
- /* The bus just got started, hence try to connect to it. */
+ /* The bus just might have become available,
+ * hence try to connect to it, if we aren't
+ * yet connected. */
bus_init_system(u->meta.manager);
bus_init_api(u->meta.manager);
}
- if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) {
- /* The syslog daemon just got started, hence try to connect to it. */
- log_info("Syslog became available, trying to reconnect.");
+ if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
+ /* The syslog daemon just might have become
+ * available, hence try to connect to it, if
+ * we aren't yet connected. */
log_open_syslog();
- }
- } else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
+ if (u->meta.type == UNIT_MOUNT)
+ /* Another directory became available, let's
+ * check if that is enough to write our utmp
+ * entry. */
+ manager_write_utmp_reboot(u->meta.manager);
+
+ if (u->meta.type == UNIT_TARGET)
+ /* A target got activated, maybe this is a runlevel? */
+ manager_write_utmp_runlevel(u->meta.manager, u);
+
+ } else if (!UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
- /* The syslog daemon just got terminated, hence try to disconnect from it. */
+ /* The syslog daemon might just have
+ * terminated, hence try to disconnect from
+ * it. */
log_close_syslog();
/* We don't care about D-Bus here, since we'll get an
return 0;
}
+static char *default_cgroup_path(Unit *u) {
+ char *p;
+
+ assert(u);
+
+ if (asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
+ return NULL;
+
+ return p;
+}
+
int unit_add_cgroup_from_text(Unit *u, const char *name) {
size_t n;
- const char *p;
- char *controller;
- CGroupBonding *b;
+ char *controller = NULL, *path = NULL;
+ CGroupBonding *b = NULL;
int r;
assert(u);
assert(name);
/* Detect controller name */
- n = strcspn(name, ":/");
+ n = strcspn(name, ":");
- /* Only controller name, no path? No path? */
- if (name[n] == 0)
- return -EINVAL;
+ if (name[n] == 0 ||
+ (name[n] == ':' && name[n+1] == 0)) {
- if (n > 0) {
- if (name[n] != ':')
- return -EINVAL;
+ /* Only controller name, no path? */
- p = name+n+1;
- } else
- p = name;
+ if (!(path = default_cgroup_path(u)))
+ return -ENOMEM;
- /* Insist in absolute paths */
- if (p[0] != '/')
- return -EINVAL;
+ } else {
+ const char *p;
- if (!(controller = strndup(name, n)))
- return -ENOMEM;
+ /* Controller name, and path. */
+ p = name+n+1;
- if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
- free(controller);
- return -EEXIST;
+ if (!path_is_absolute(p))
+ return -EINVAL;
+
+ if (!(path = strdup(p)))
+ return -ENOMEM;
}
- if (!(b = new0(CGroupBonding, 1))) {
- free(controller);
- return -ENOMEM;
+ if (n > 0)
+ controller = strndup(name, n);
+ else
+ controller = strdup(u->meta.manager->cgroup_controller);
+
+ if (!controller) {
+ r = -ENOMEM;
+ goto fail;
}
- b->controller = controller;
+ if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
+ r = -EEXIST;
+ goto fail;
+ }
- if (!(b->path = strdup(p))) {
+ if (!(b = new0(CGroupBonding, 1))) {
r = -ENOMEM;
goto fail;
}
+ b->controller = controller;
+ b->path = path;
b->only_us = false;
b->clean_up = false;
return 0;
fail:
- free(b->path);
- free(b->controller);
+ free(path);
+ free(controller);
free(b);
return r;
if (!(b->controller = strdup(u->meta.manager->cgroup_controller)))
goto fail;
- if (asprintf(&b->path, "%s/%s", u->meta.manager->cgroup_hierarchy, unit_id(u)) < 0)
+ if (!(b->path = default_cgroup_path(u)))
goto fail;
b->clean_up = true;
return cgroup_bonding_find_list(u->meta.cgroup_bondings, u->meta.manager->cgroup_controller);
}
+int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
+ char *t;
+ int r;
+
+ assert(u);
+ assert(type);
+ assert(_found);
+
+ if (!(t = unit_name_change_suffix(unit_id(u), type)))
+ return -ENOMEM;
+
+ assert(!unit_has_name(u, t));
+
+ r = manager_load_unit(u->meta.manager, t, _found);
+ free(t);
+
+ if (r >= 0)
+ assert(*_found != u);
+
+ return r;
+}
+
static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
[UNIT_SERVICE] = "service",
[UNIT_TIMER] = "timer",