#include <sys/reboot.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
-#include <libcgroup.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/types.h>
#include "dbus-job.h"
#include "missing.h"
#include "path-lookup.h"
+#include "special.h"
/* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
#define GC_QUEUE_ENTRIES_MAX 16
struct sockaddr_un un;
} sa;
struct epoll_event ev;
- char *ne[2], **t;
int one = 1;
assert(m);
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
return -errno;
- if (asprintf(&ne[0], "NOTIFY_SOCKET=@%s", sa.un.sun_path+1) < 0)
+ if (!(m->notify_socket = strdup(sa.un.sun_path+1)))
return -ENOMEM;
- ne[1] = NULL;
- t = strv_env_merge(m->environment, ne, NULL);
- free(ne[0]);
-
- if (!t)
- return -ENOMEM;
-
- strv_free(m->environment);
- m->environment = t;
-
return 0;
}
assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
assert_se(sigemptyset(&mask) == 0);
- assert_se(sigaddset(&mask, SIGCHLD) == 0);
- assert_se(sigaddset(&mask, SIGTERM) == 0);
- assert_se(sigaddset(&mask, SIGHUP) == 0);
- assert_se(sigaddset(&mask, SIGUSR1) == 0);
- assert_se(sigaddset(&mask, SIGUSR2) == 0);
- assert_se(sigaddset(&mask, SIGINT) == 0); /* Kernel sends us this on control-alt-del */
- assert_se(sigaddset(&mask, SIGWINCH) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
- assert_se(sigaddset(&mask, SIGPWR) == 0); /* Some kernel drivers and upsd send us this on power failure */
+
+ sigset_add_many(&mask,
+ SIGCHLD, /* Child died */
+ SIGTERM, /* Reexecute daemon */
+ SIGHUP, /* Reload configuration */
+ SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
+ SIGUSR2, /* systemd: dump status */
+ SIGINT, /* Kernel sends us this on control-alt-del */
+ SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
+ SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
+ SIGRTMIN+0, /* systemd: start default.target */
+ SIGRTMIN+1, /* systemd: start rescue.target */
+ SIGRTMIN+2, /* systemd: isolate emergency.target */
+ SIGRTMIN+3, /* systemd: start halt.target */
+ SIGRTMIN+4, /* systemd: start poweroff.target */
+ SIGRTMIN+5, /* systemd: start reboot.target */
+ -1);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
m->signal_watch.type = WATCH_SIGNAL;
if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
return -errno;
- if (m->running_as == MANAGER_INIT)
+ if (m->running_as == MANAGER_SYSTEM)
return enable_special_signals(m);
return 0;
}
-int manager_new(ManagerRunningAs running_as, bool confirm_spawn, Manager **_m) {
+int manager_new(ManagerRunningAs running_as, Manager **_m) {
Manager *m;
int r = -ENOMEM;
+ char *p;
assert(_m);
assert(running_as >= 0);
if (!(m = new0(Manager, 1)))
return -ENOMEM;
- timestamp_get(&m->startup_timestamp);
+ dual_timestamp_get(&m->startup_timestamp);
m->running_as = running_as;
- m->confirm_spawn = confirm_spawn;
- m->name_data_slot = -1;
+ m->name_data_slot = m->subscribed_data_slot = -1;
m->exit_code = _MANAGER_EXIT_CODE_INVALID;
+ m->pin_cgroupfs_fd = -1;
m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = -1;
m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
goto fail;
/* Try to connect to the busses, if possible. */
- if ((r = bus_init_system(m)) < 0 ||
- (r = bus_init_api(m)) < 0)
+ if ((r = bus_init(m)) < 0)
goto fail;
+ if (asprintf(&p, "%s/%s", m->cgroup_mount_point, m->cgroup_hierarchy) < 0) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ m->pin_cgroupfs_fd = open(p, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK);
+ free(p);
+
*_m = m;
return 0;
while ((meta = m->cleanup_queue)) {
assert(meta->in_cleanup_queue);
- unit_free(UNIT(meta));
+ unit_free((Unit*) meta);
n++;
}
while ((meta = m->gc_queue)) {
assert(meta->in_gc_queue);
- unit_gc_sweep(UNIT(meta), gc_marker);
+ unit_gc_sweep((Unit*) meta, gc_marker);
LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
meta->in_gc_queue = false;
meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
log_debug("Collecting %s", meta->id);
meta->gc_marker = gc_marker + GC_OFFSET_BAD;
- unit_add_to_cleanup_queue(UNIT(meta));
+ unit_add_to_cleanup_queue((Unit*) meta);
}
}
/* If we reexecute ourselves, we keep the root cgroup
* around */
- manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
+ if (m->exit_code != MANAGER_REEXECUTE)
+ manager_shutdown_cgroup(m);
- bus_done_api(m);
- bus_done_system(m);
+ bus_done(m);
hashmap_free(m->units);
hashmap_free(m->jobs);
if (m->notify_watch.fd >= 0)
close_nointr_nofail(m->notify_watch.fd);
+ free(m->notify_socket);
+
lookup_paths_free(&m->lookup_paths);
strv_free(m->environment);
free(m->cgroup_controller);
free(m->cgroup_hierarchy);
+ free(m->cgroup_mount_point);
hashmap_free(m->cgroup_bondings);
+ if (m->pin_cgroupfs_fd >= 0)
+ close_nointr_nofail(m->pin_cgroupfs_fd);
+
free(m);
}
return -ENOEXEC;
/* Ok, we can drop one, so let's do so. */
- log_debug("Trying to fix job merging by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
+ log_notice("Trying to fix job merging by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
transaction_delete_job(m, d, true);
return 0;
}
* job to remove. We use the marker to find our way
* back, since smart how we are we stored our way back
* in there. */
- log_debug("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
+ log_warning("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
- log_debug("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
+ log_info("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
if (!k->installed &&
!unit_matters_to_anchor(k->unit, k)) {
/* Ok, we can drop this one, so let's
* do so. */
- log_debug("Breaking order cycle by deleting job %s/%s", k->unit->meta.id, job_type_to_string(k->type));
+ log_warning("Breaking order cycle by deleting job %s/%s", k->unit->meta.id, job_type_to_string(k->type));
transaction_delete_unit(m, k->unit);
return -EAGAIN;
}
break;
}
- log_debug("Unable to break cycle");
+ log_error("Unable to break cycle");
return -ENOEXEC;
}
continue;
if (stops_running_service)
- log_debug("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
+ log_info("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
if (changes_existing_job)
- log_debug("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
+ log_info("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
/* Ok, let's get rid of this */
- log_debug("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
+ log_info("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
transaction_delete_job(m, j, true);
again = true;
break;
if (r != -EAGAIN) {
- log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r));
+ log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r));
goto rollback;
}
break;
if (r != -EAGAIN) {
- log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r));
+ log_warning("Requested transaction contains unmergable jobs: %s", strerror(-r));
goto rollback;
}
/* Ninth step: check whether we can actually apply this */
if (mode == JOB_FAIL)
if ((r = transaction_is_destructive(m)) < 0) {
- log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
+ log_notice("Requested transaction contradicts existing jobs: %s", strerror(-r));
goto rollback;
}
/* Tenth step: apply changes */
if ((r = transaction_apply(m)) < 0) {
- log_debug("Failed to apply transaction: %s", strerror(-r));
+ log_warning("Failed to apply transaction: %s", strerror(-r));
goto rollback;
}
job_dependency_free(j->object_list);
if (other && delete_dependencies) {
- log_debug("Deleting job %s/%s as dependency of job %s/%s",
+ log_info("Deleting job %s/%s as dependency of job %s/%s",
other->unit->meta.id, job_type_to_string(other->type),
j->unit->meta.id, job_type_to_string(j->type));
transaction_delete_job(m, other, delete_dependencies);
continue;
/* No need to stop inactive jobs */
- if (unit_active_state(u) == UNIT_INACTIVE)
+ if (UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(u)))
continue;
/* Is there already something listed for this? */
while ((meta = m->load_queue)) {
assert(meta->in_load_queue);
- unit_load(UNIT(meta));
+ unit_load((Unit*) meta);
n++;
}
while ((meta = m->dbus_unit_queue)) {
assert(meta->in_dbus_queue);
- bus_unit_send_change_signal(UNIT(meta));
+ bus_unit_send_change_signal((Unit*) meta);
n++;
}
continue;
}
- char_array_0(buf);
+ assert((size_t) n < sizeof(buf));
+ buf[n] = 0;
if (!(tags = strv_split(buf, "\n\r")))
return -ENOMEM;
log_debug("Got notification message for unit %s", u->meta.id);
if (UNIT_VTABLE(u)->notify_message)
- UNIT_VTABLE(u)->notify_message(u, tags);
+ UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
strv_free(tags);
}
char *name = NULL;
get_process_name(si.si_pid, &name);
- log_debug("Got SIGCHLD for process %llu (%s)", (unsigned long long) si.si_pid, strna(name));
+ log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
free(name);
}
if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
continue;
- log_debug("Child %llu died (code=%s, status=%i/%s)",
- (long long unsigned) si.si_pid,
+ log_debug("Child %lu died (code=%s, status=%i/%s)",
+ (long unsigned) si.si_pid,
sigchld_code_to_string(si.si_code),
si.si_status,
strna(si.si_code == CLD_EXITED ? exit_status_to_string(si.si_status) : strsignal(si.si_status)));
return 0;
}
-static int manager_start_target(Manager *m, const char *name) {
+static int manager_start_target(Manager *m, const char *name, JobMode mode) {
int r;
- if ((r = manager_add_job_by_name(m, JOB_START, name, JOB_REPLACE, true, NULL)) < 0)
+ if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, NULL)) < 0)
log_error("Failed to enqueue %s job: %s", name, strerror(-r));
return r;
break;
case SIGTERM:
- if (m->running_as == MANAGER_INIT) {
+ if (m->running_as == MANAGER_SYSTEM) {
/* This is for compatibility with the
* original sysvinit */
m->exit_code = MANAGER_REEXECUTE;
/* Fall through */
case SIGINT:
- if (m->running_as == MANAGER_INIT) {
- manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET);
+ if (m->running_as == MANAGER_SYSTEM) {
+ manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
break;
}
/* Run the exit target if there is one, if not, just exit. */
- if (manager_start_target(m, SPECIAL_EXIT_SERVICE) < 0) {
+ if (manager_start_target(m, SPECIAL_EXIT_SERVICE, JOB_REPLACE) < 0) {
m->exit_code = MANAGER_EXIT;
return 0;
}
break;
case SIGWINCH:
- if (m->running_as == MANAGER_INIT)
- manager_start_target(m, SPECIAL_KBREQUEST_TARGET);
+ if (m->running_as == MANAGER_SYSTEM)
+ manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
/* This is a nop on non-init */
break;
case SIGPWR:
- if (m->running_as == MANAGER_INIT)
- manager_start_target(m, SPECIAL_SIGPWR_TARGET);
+ if (m->running_as == MANAGER_SYSTEM)
+ manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
/* This is a nop on non-init */
break;
if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
log_info("Trying to reconnect to bus...");
- bus_init_system(m);
- bus_init_api(m);
+ bus_init(m);
}
if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
log_info("Loading D-Bus service...");
- manager_start_target(m, SPECIAL_DBUS_SERVICE);
+ manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
}
break;
m->exit_code = MANAGER_RELOAD;
break;
- default:
+ default: {
+ static const char * const table[] = {
+ [0] = SPECIAL_DEFAULT_TARGET,
+ [1] = SPECIAL_RESCUE_TARGET,
+ [2] = SPECIAL_EMERGENCY_SERVICE,
+ [3] = SPECIAL_HALT_TARGET,
+ [4] = SPECIAL_POWEROFF_TARGET,
+ [5] = SPECIAL_REBOOT_TARGET
+ };
+
+ if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
+ (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(table)) {
+ manager_start_target(m, table[sfsi.ssi_signo - SIGRTMIN],
+ (sfsi.ssi_signo == 1 || sfsi.ssi_signo == 2) ? JOB_ISOLATE : JOB_REPLACE);
+ break;
+ }
+
log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
}
+ }
}
if (sigchld)
if (m->utmp_reboot_written)
return;
- if (m->running_as != MANAGER_INIT)
+ if (m->running_as != MANAGER_SYSTEM)
return;
if (!manager_utmp_good(m))
if (u->meta.type != UNIT_TARGET)
return;
- if (m->running_as != MANAGER_INIT)
+ if (m->running_as != MANAGER_SYSTEM)
return;
if (!manager_utmp_good(m))
return r;
}
+bool manager_is_booting_or_shutting_down(Manager *m) {
+ Unit *u;
+
+ assert(m);
+
+ /* Is the initial job still around? */
+ if (manager_get_job(m, 1))
+ return true;
+
+ /* Is there a job for the shutdown target? */
+ if (((u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET))))
+ return !!u->meta.job;
+
+ return false;
+}
+
static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
- [MANAGER_INIT] = "init",
[MANAGER_SYSTEM] = "system",
[MANAGER_SESSION] = "session"
};