if ((r = unit_load_related_unit(u, ".mount", (Unit**) &a->mount)) < 0)
return r;
- if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount))) < 0)
+ if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
return r;
}
return automount_state_to_string(AUTOMOUNT(u)->state);
}
+static bool automount_check_gc(Unit *u) {
+ Automount *a = AUTOMOUNT(u);
+
+ assert(a);
+
+ return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
+}
+
static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
+ Automount *a = AUTOMOUNT(u);
union autofs_v5_packet_union packet;
ssize_t l;
int r;
- Automount *a = AUTOMOUNT(u);
-
assert(a);
assert(fd == a->pipe_fd);
.active_state = automount_active_state,
.sub_state_to_string = automount_sub_state_to_string,
+ .check_gc = automount_check_gc,
+
.fd_event = automount_fd_event,
.bus_message_handler = bus_automount_message_handler,
} else {
/* Send a new signal */
- if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobNew")))
+ if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobNew")))
goto oom;
if (!dbus_message_append_args(m,
if (!(p = job_dbus_path(j)))
goto oom;
- if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "JobRemoved")))
+ if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "JobRemoved")))
goto oom;
if (!dbus_message_append_args(m,
DBUS_TYPE_INVALID))
goto oom;
- } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetJob")) {
+ } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
uint32_t id;
Job *j;
} else {
/* Send a new signal */
- if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew")))
+ if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
goto oom;
if (!dbus_message_append_args(m,
if (!(p = unit_dbus_path(u)))
goto oom;
- if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved")))
+ if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
goto oom;
if (!dbus_message_append_args(m,
goto fail;
}
- r = unit_add_dependency_by_name(u, UNIT_WANTS, NULL, e);
+ r = unit_add_dependency_by_name(u, UNIT_WANTS, NULL, e, true);
free(e);
if (r < 0)
log_debug("Running request %s", target);
- if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1", "GetUnit"))) {
+ if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "GetUnit"))) {
log_error("Could not allocate message.");
goto finish;
}
if (j->installed) {
bus_job_send_removed_signal(j);
- if (j->unit->meta.job == j)
+ if (j->unit->meta.job == j) {
j->unit->meta.job = NULL;
+ unit_add_to_gc_queue(j->unit);
+ }
hashmap_remove(j->manager->jobs, UINT32_TO_PTR(j->id));
j->installed = false;
goto finish;
}
- r = unit_add_dependency_by_name(u, UNIT_WANTS, de->d_name, f);
+ r = unit_add_dependency_by_name(u, UNIT_WANTS, de->d_name, f, true);
free(f);
if (r < 0)
if (!k)
return -ENOMEM;
- r = unit_add_dependency_by_name(u, d, k, NULL);
+ r = unit_add_dependency_by_name(u, d, k, NULL, true);
free(k);
if (r < 0)
#include "dbus-unit.h"
#include "dbus-job.h"
+/* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
+#define GC_QUEUE_ENTRIES_MAX 16
+
+/* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
+#define GC_QUEUE_USEC_MAX (5*USEC_PER_SEC)
+
static int enable_special_signals(Manager *m) {
char fd;
return n;
}
+static void unit_gc_sweep(Unit *u, int gc_marker) {
+ Iterator i;
+ Unit *other;
+
+ assert(u);
+
+ if (u->meta.gc_marker == gc_marker ||
+ u->meta.gc_marker == -gc_marker)
+ return;
+
+ if (!u->meta.in_cleanup_queue)
+ goto bad;
+
+ if (unit_check_gc(u))
+ goto good;
+
+ SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
+ unit_gc_sweep(other, gc_marker);
+
+ if (other->meta.gc_marker == gc_marker)
+ goto good;
+ }
+
+bad:
+ /* So there is no reason to keep this unit around, hence let's get rid of it */
+ u->meta.gc_marker = -gc_marker;
+ return;
+
+good:
+ u->meta.gc_marker = gc_marker;
+}
+
+static unsigned manager_dispatch_gc_queue(Manager *m) {
+ Meta *meta;
+ unsigned n = 0;
+ int gc_marker;
+
+ assert(m);
+
+ if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
+ (m->gc_queue_timestamp <= 0 ||
+ (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
+ return 0;
+
+ log_debug("Running GC...");
+
+ gc_marker = m->gc_marker;
+ m->gc_marker = MIN(0, m->gc_marker + 1);
+
+ while ((meta = m->gc_queue)) {
+ assert(meta->in_gc_queue);
+
+ LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
+ meta->in_gc_queue = false;
+
+ n++;
+
+ unit_gc_sweep(UNIT(meta), gc_marker);
+
+ if (meta->gc_marker == -gc_marker) {
+ log_debug("Collecting %s", meta->id);
+ unit_add_to_cleanup_queue(UNIT(meta));
+ }
+ }
+
+ m->n_in_gc_queue = 0;
+ m->gc_queue_timestamp = 0;
+
+ return n;
+}
+
static void manager_clear_jobs_and_units(Manager *m) {
Job *j;
Unit *u;
if (manager_dispatch_cleanup_queue(m) > 0)
continue;
+ if (manager_dispatch_gc_queue(m) > 0)
+ continue;
+
if (manager_dispatch_load_queue(m) > 0)
continue;
LIST_HEAD(Meta, dbus_unit_queue);
LIST_HEAD(Job, dbus_job_queue);
+ /* Units to remove */
LIST_HEAD(Meta, cleanup_queue);
+ /* Units to check when doing GC */
+ LIST_HEAD(Meta, gc_queue);
+
/* Jobs to be added */
Hashmap *transaction_jobs; /* Unit object => Job object list 1:1 */
JobDependency *transaction_anchor;
char *cgroup_controller;
char *cgroup_hierarchy;
+ usec_t gc_queue_timestamp;
+
+ int gc_marker;
+ unsigned n_in_gc_queue;
+
/* Flags */
ManagerRunningAs running_as;
ManagerExitCode exit_code:4;
if (r < 0)
return r;
- if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, device)) < 0)
+ if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, device, true)) < 0)
return r;
- if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, device)) < 0)
+ if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, device, true)) < 0)
return r;
if (UNIT(m)->meta.manager->running_as == MANAGER_INIT ||
UNIT(m)->meta.manager->running_as == MANAGER_SYSTEM)
- if ((r = unit_add_dependency(device, UNIT_WANTS, UNIT(m))) < 0)
+ if ((r = unit_add_dependency(device, UNIT_WANTS, UNIT(m), false)) < 0)
return r;
return 0;
if (path_startswith(m->where, n->where)) {
- if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(other))) < 0)
+ if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, UNIT(other), true)) < 0)
return r;
if (n->from_etc_fstab || n->from_fragment)
- if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(other))) < 0)
+ if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, UNIT(other), true)) < 0)
return r;
} else if (path_startswith(n->where, m->where)) {
- if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(other))) < 0)
+ if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(other), true)) < 0)
return r;
if (m->from_etc_fstab || m->from_fragment)
- if ((r = unit_add_dependency(UNIT(other), UNIT_REQUIRES, UNIT(m))) < 0)
+ if ((r = unit_add_dependency(UNIT(other), UNIT_REQUIRES, UNIT(m), true)) < 0)
return r;
}
}
if ((r = unit_load_related_unit(UNIT(m), ".automount", &am)) < 0)
return r;
- if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(am))) < 0)
+ if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(am), true)) < 0)
return r;
- return unit_add_dependency(UNIT(am), UNIT_BEFORE, tu);
+ return unit_add_dependency(UNIT(am), UNIT_BEFORE, tu, true);
} else {
if (handle)
- if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(m))) < 0)
+ if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(m), true)) < 0)
return r;
- return unit_add_dependency(UNIT(m), UNIT_BEFORE, tu);
+ return unit_add_dependency(UNIT(m), UNIT_BEFORE, tu, true);
}
}
return mount_state_to_string(MOUNT(u)->state);
}
+static bool mount_check_gc(Unit *u) {
+ Mount *m = MOUNT(u);
+
+ assert(m);
+
+ return m->from_etc_fstab || m->from_proc_self_mountinfo;
+}
+
static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
Mount *m = MOUNT(u);
bool success;
.active_state = mount_active_state,
.sub_state_to_string = mount_sub_state_to_string,
+ .check_gc = mount_check_gc,
+
.sigchld_event = mount_sigchld_event,
.timer_event = mount_timer_event,
send_member="GetAll"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="GetUnit"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="GetJob"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="ListUnits"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="ListJobs"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="Subscribe"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="Unsubscribe"/>
<allow send_destination="org.freedesktop.systemd1"
- send_interface="org.freedesktop.systemd1"
+ send_interface="org.freedesktop.systemd1.Manager"
send_member="Dump"/>
<allow receive_sender="org.freedesktop.systemd1"/>
/* FIXME: Maybe we should compare the name here lexicographically? */
- if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
+ if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
return r;
}
if (unit_name_to_type(m) == UNIT_SERVICE)
r = unit_add_name(u, m);
else {
- if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL)) >= 0)
- r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL);
+ if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
+ r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
}
free(m);
if (r == 0)
continue;
- r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL);
+ r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL, true);
free(m);
if (r < 0)
* needed for early boot) and don't create any links
* to it. */
- if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL)) < 0 ||
- (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL)) < 0)
+ if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
+ (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
goto finish;
}
return service_state_to_string(SERVICE(u)->state);
}
+static bool service_check_gc(Unit *u) {
+ Service *s = SERVICE(u);
+
+ assert(s);
+
+ return !!s->sysv_path;
+}
+
+static bool service_check_snapshot(Unit *u) {
+ Service *s = SERVICE(u);
+
+ assert(s);
+
+ return !s->got_socket_fd;
+}
+
static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
Service *s = SERVICE(u);
bool success;
goto finish;
if (de->d_name[0] == 'S') {
- if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
+ if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service, true)) < 0)
goto finish;
- if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
+ if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service, true)) < 0)
goto finish;
} else if (de->d_name[0] == 'K' &&
* implicitly added by the
* core logic. */
- if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
+ if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service, true)) < 0)
goto finish;
- if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
+ if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service, true)) < 0)
goto finish;
}
}
return -EAGAIN;
s->socket_fd = fd;
+ s->got_socket_fd = true;
return 0;
}
.active_state = service_active_state,
.sub_state_to_string = service_sub_state_to_string,
+ .check_gc = service_check_gc,
+ .check_snapshot = service_check_snapshot,
+
.sigchld_event = service_sigchld_event,
.timer_event = service_timer_event,
bool bus_name_good:1;
+ bool got_socket_fd:1;
+
bool sysv_has_lsb:1;
char *sysv_path;
int sysv_start_priority;
} else if (streq(key, "requires")) {
- if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, value, NULL)) < 0)
+ if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, value, NULL, true)) < 0)
return r;
- if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, value, NULL)) < 0)
+ if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, value, NULL, true)) < 0)
return r;
} else
log_debug("Unknown serialization key '%s'", key);
if (k != other->meta.id)
continue;
+ if (UNIT_VTABLE(other)->check_snapshot)
+ if (!UNIT_VTABLE(other)->check_snapshot(other))
+ continue;
+
if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
continue;
- if ((r = unit_add_dependency(u, UNIT_REQUIRES, other)) < 0)
+ if ((r = unit_add_dependency(u, UNIT_REQUIRES, other, true)) < 0)
goto fail;
- if ((r = unit_add_dependency(u, UNIT_AFTER, other)) < 0)
+ if ((r = unit_add_dependency(u, UNIT_AFTER, other, true)) < 0)
goto fail;
}
.no_alias = true,
.no_instances = true,
.no_snapshots = true,
+ .no_gc = true,
.load = unit_load_nop,
.coldplug = snapshot_coldplug,
if ((r = unit_load_related_unit(u, ".service", (Unit**) &s->service)))
return r;
- if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service))) < 0)
+ if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(s->service), true)) < 0)
return r;
}
manager = bus.get_object(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
- "org.freedesktop.systemd1") as Manager;
+ "org.freedesktop.systemd1.Manager") as Manager;
manager.unit_new += on_unit_new;
manager.job_new += on_job_new;
Manager manager = bus.get_object (
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
- "org.freedesktop.systemd1") as Manager;
+ "org.freedesktop.systemd1.Manager") as Manager;
if (args[1] == "list-units" || args.length <= 1) {
var list = manager.list_units();
return 0;
}
+bool unit_check_gc(Unit *u) {
+ assert(u);
+
+ if (UNIT_VTABLE(u)->no_gc)
+ return true;
+
+ if (u->meta.job)
+ return true;
+
+ if (unit_active_state(u) != UNIT_INACTIVE)
+ return true;
+
+ if (UNIT_VTABLE(u)->check_gc)
+ if (UNIT_VTABLE(u)->check_gc(u))
+ return true;
+
+ return false;
+}
+
void unit_add_to_load_queue(Unit *u) {
assert(u);
assert(u->meta.type != _UNIT_TYPE_INVALID);
u->meta.in_cleanup_queue = true;
}
+void unit_add_to_gc_queue(Unit *u) {
+ assert(u);
+
+ if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
+ return;
+
+ if (unit_check_gc(u))
+ return;
+
+ LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
+ u->meta.in_gc_queue = true;
+
+ u->meta.manager->n_in_gc_queue ++;
+
+ if (u->meta.manager->gc_queue_timestamp <= 0)
+ u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
+}
+
void unit_add_to_dbus_queue(Unit *u) {
assert(u);
assert(u->meta.type != _UNIT_TYPE_INVALID);
for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
set_remove(other->meta.dependencies[d], u);
+
+ unit_add_to_gc_queue(other);
}
set_free(s);
if (u->meta.in_cleanup_queue)
LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
+ if (u->meta.in_gc_queue) {
+ LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
+ u->meta.manager->n_in_gc_queue--;
+ }
+
/* Free data and next 'smaller' objects */
if (u->meta.job)
job_free(u->meta.job);
/* If syslog or kernel logging is requested, make sure our own
* logging daemon is run first. */
- if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL)) < 0)
+ if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
return r;
if (u->meta.manager->running_as != MANAGER_SESSION)
- if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL)) < 0)
+ if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
return r;
return 0;
"%s\tUnit Load State: %s\n"
"%s\tUnit Active State: %s\n"
"%s\tActive Enter Timestamp: %s\n"
- "%s\tActive Exit Timestamp: %s\n",
+ "%s\tActive Exit Timestamp: %s\n"
+ "%s\tGC Check Good: %s\n",
prefix, u->meta.id,
prefix, unit_description(u),
prefix, strna(u->meta.instance),
prefix, unit_load_state_to_string(u->meta.load_state),
prefix, unit_active_state_to_string(unit_active_state(u)),
prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.active_enter_timestamp)),
- prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)));
+ prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_exit_timestamp)),
+ prefix, yes_no(unit_check_gc(u)));
SET_FOREACH(t, u->meta.names, i)
fprintf(f, "%s\tName: %s\n", prefix, t);
assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
unit_add_to_dbus_queue(unit_follow_merge(u));
+ unit_add_to_gc_queue(u);
return 0;
unit_check_uneeded(u);
unit_add_to_dbus_queue(u);
+ unit_add_to_gc_queue(u);
}
int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
}
}
-int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
+int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
[UNIT_REQUIRES] = UNIT_REQUIRED_BY,
[UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
[UNIT_CONFLICTS] = UNIT_CONFLICTS,
[UNIT_BEFORE] = UNIT_AFTER,
- [UNIT_AFTER] = UNIT_BEFORE
+ [UNIT_AFTER] = UNIT_BEFORE,
+ [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
+ [UNIT_REFERENCED_BY] = UNIT_REFERENCES
};
- int r;
+ int r, q = 0, v = 0, w = 0;
assert(u);
assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
return -EINVAL;
}
- if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
+ if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0 ||
+ (r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
return r;
- if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
- return r;
+ if (add_reference)
+ if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
+ (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
+ return r;
- if ((r = set_put(u->meta.dependencies[d], other)) < 0)
- return r;
+ if ((q = set_put(u->meta.dependencies[d], other)) < 0)
+ return q;
- if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
- set_remove(u->meta.dependencies[d], other);
- return r;
+ if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
+ r = v;
+ goto fail;
+ }
+
+ if (add_reference) {
+ if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) {
+ r = w;
+ goto fail;
+ }
+
+ if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0)
+ goto fail;
}
unit_add_to_dbus_queue(u);
return 0;
+
+fail:
+ if (q > 0)
+ set_remove(u->meta.dependencies[d], other);
+
+ if (v > 0)
+ set_remove(other->meta.dependencies[inverse_table[d]], u);
+
+ if (w > 0)
+ set_remove(u->meta.dependencies[UNIT_REFERENCES], other);
+
+ return r;
}
static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
return s;
}
-int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path) {
+int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
Unit *other;
int r;
char *s;
if ((r = manager_load_unit(u->meta.manager, name, path, &other)) < 0)
goto finish;
- r = unit_add_dependency(u, d, other);
+ r = unit_add_dependency(u, d, other, add_reference);
finish:
free(s);
return r;
}
-int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path) {
+int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
Unit *other;
int r;
char *s;
if ((r = manager_load_unit(u->meta.manager, name, path, &other)) < 0)
goto finish;
- r = unit_add_dependency(other, d, u);
+ r = unit_add_dependency(other, d, u, add_reference);
finish:
free(s);
[UNIT_CONFLICTS] = "Conflicts",
[UNIT_BEFORE] = "Before",
[UNIT_AFTER] = "After",
+ [UNIT_REFERENCES] = "References",
+ [UNIT_REFERENCED_BY] = "ReferencedBy"
};
DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
/* Order */
- UNIT_BEFORE, /* inverse of before is after and vice versa */
+ UNIT_BEFORE, /* inverse of 'before' is 'after' and vice versa */
UNIT_AFTER,
+ /* Reference information for GC logic */
+ UNIT_REFERENCES, /* Inverse of 'references' is 'referenced_by' */
+ UNIT_REFERENCED_BY,
+
_UNIT_DEPENDENCY_MAX,
_UNIT_DEPENDENCY_INVALID = -1
};
/* Counterparts in the cgroup filesystem */
CGroupBonding *cgroup_bondings;
- /* Load queue */
- LIST_FIELDS(Meta, load_queue);
-
/* Per type list */
LIST_FIELDS(Meta, units_per_type);
+ /* Load queue */
+ LIST_FIELDS(Meta, load_queue);
+
/* D-Bus queue */
LIST_FIELDS(Meta, dbus_queue);
/* Cleanup queue */
LIST_FIELDS(Meta, cleanup_queue);
+ /* GC queue */
+ LIST_FIELDS(Meta, gc_queue);
+
+ /* Used during GC sweeps */
+ int gc_marker;
+
/* If we go down, pull down everything that depends on us, too */
bool recursive_stop;
bool in_load_queue:1;
bool in_dbus_queue:1;
bool in_cleanup_queue:1;
+ bool in_gc_queue:1;
+
bool sent_dbus_new_signal:1;
};
* unit is in. */
const char* (*sub_state_to_string)(Unit *u);
+ /* Return true when there is reason to keep this entry around
+ * even nothing references it and it isn't active in any
+ * way */
+ bool (*check_gc)(Unit *u);
+
+ /* Return true when this unit is suitable for snapshotting */
+ bool (*check_snapshot)(Unit *u);
+
void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
/* Exclude this type from snapshots */
bool no_snapshots:1;
+
+ /* Exclude from automatic gc */
+ bool no_gc:1;
};
extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
int unit_add_name(Unit *u, const char *name);
-int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
-int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename);
-int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename);
+int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);
+int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
+int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
int unit_add_exec_dependencies(Unit *u, ExecContext *c);
int unit_choose_id(Unit *u, const char *name);
int unit_set_description(Unit *u, const char *description);
+bool unit_check_gc(Unit *u);
+
void unit_add_to_load_queue(Unit *u);
void unit_add_to_dbus_queue(Unit *u);
void unit_add_to_cleanup_queue(Unit *u);
+void unit_add_to_gc_queue(Unit *u);
int unit_merge(Unit *u, Unit *other);
int unit_merge_by_name(Unit *u, const char *other);