job.c revision 3b6fdb5b5afebc49a7e987e3e3bf7aa2615d1671
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <errno.h>
#include "set.h"
#include "unit.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"
#include "load-dropin.h"
#include "log.h"
#include "dbus-job.h"
Job *j;
assert(m);
return NULL;
j->manager = m;
j->id = m->current_job_id++;
/* We don't link it here, that's what job_dependency() is for */
return j;
}
assert(j);
/* Detach from next 'bigger' objects */
if (j->installed) {
bus_job_send_removed_signal(j, !j->failed);
unit_add_to_gc_queue(j->unit);
}
j->installed = false;
}
/* Detach from next 'smaller' objects */
manager_transaction_unlink_job(j->manager, j, true);
if (j->in_run_queue)
if (j->in_dbus_queue)
free(j->bus_client);
free(j);
}
JobDependency *l;
/* Adds a new job link, which encodes that the 'subject' job
* needs the 'object' job in some way. If 'subject' is NULL
* this means the 'anchor' job (i.e. the one the user
* explcitily asked for) is the requester. */
return NULL;
if (subject)
else
return l;
}
void job_dependency_free(JobDependency *l) {
assert(l);
if (l->subject)
else
free(l);
}
JobDependency *l;
break;
}
if (!l) {
if (matters)
*matters = false;
return;
}
if (matters)
}
assert(j);
assert(f);
if (!prefix)
prefix = "";
fprintf(f,
"%s-> Job %u:\n"
"%s\tAction: %s -> %s\n"
"%s\tState: %s\n"
"%s\tForced: %s\n",
}
bool job_is_anchor(Job *j) {
JobDependency *l;
assert(j);
if (!l->subject)
return true;
return false;
}
return
(a == c && b == d) ||
(a == d && b == c);
}
if (*a == b)
return 0;
/* Merging is associative! a merged with b merged with c is
* the same as a merged with c merged with b. */
/* Mergeability is transitive! if a can be merged with b and b
* with c then a also with c */
/* Also, if a merged with b cannot be merged with c, then
* either a or b cannot be merged with c either */
*a = JOB_START;
*a = JOB_RELOAD_OR_START;
*a = JOB_RESTART;
*a = JOB_RELOAD;
*a = JOB_TRY_RESTART;
else
return -EEXIST;
return 0;
}
return job_type_merge(&a, b) >= 0;
}
/* Checks whether operation a is a "superset" of b in its
* actions */
if (a == b)
return true;
switch (a) {
case JOB_START:
return b == JOB_VERIFY_ACTIVE;
case JOB_RELOAD:
return
b == JOB_VERIFY_ACTIVE;
case JOB_RELOAD_OR_START:
return
b == JOB_RELOAD ||
b == JOB_START ||
b == JOB_VERIFY_ACTIVE;
case JOB_RESTART:
return
b == JOB_START ||
b == JOB_VERIFY_ACTIVE ||
b == JOB_RELOAD ||
b == JOB_RELOAD_OR_START ||
b == JOB_TRY_RESTART;
case JOB_TRY_RESTART:
return
b == JOB_VERIFY_ACTIVE ||
b == JOB_RELOAD;
default:
return false;
}
}
assert(a >= 0 && a < _JOB_TYPE_MAX);
assert(b >= 0 && b < _JOB_TYPE_MAX);
}
switch (a) {
case JOB_START:
return
b == UNIT_ACTIVE ||
b == UNIT_RELOADING;
case JOB_STOP:
return
b == UNIT_INACTIVE ||
b == UNIT_MAINTENANCE;
case JOB_VERIFY_ACTIVE:
return
b == UNIT_ACTIVE ||
b == UNIT_RELOADING;
case JOB_RELOAD:
return
b == UNIT_RELOADING;
case JOB_RELOAD_OR_START:
return
b == UNIT_ACTIVATING ||
b == UNIT_RELOADING;
case JOB_RESTART:
return
b == UNIT_ACTIVATING;
case JOB_TRY_RESTART:
return
b == UNIT_ACTIVATING;
default:
assert_not_reached("Invalid job type");
}
}
bool job_is_runnable(Job *j) {
Iterator i;
assert(j);
/* Checks whether there is any job running for the units this
* job needs to be running after (in the case of a 'positive'
* job type) or before (in the case of a 'negative' job type
* . */
j->type == JOB_VERIFY_ACTIVE ||
j->type == JOB_RELOAD ||
j->type == JOB_RELOAD_OR_START) {
/* Immediate result is that the job is or might be
* started. In this case lets wait for the
* dependencies, regardless whether they are
* starting or stopping something. */
return false;
}
/* Also, if something else is being stopped and we should
* change state after it, then lets wait. */
return false;
/* This means that for a service a and a service b where b
* shall be started after a:
*
* start a + start b → 1st step start a, 2nd step start b
* start a + stop b → 1st step stop b, 2nd step start a
* stop a + start b → 1st step stop a, 2nd step start b
* stop a + stop b → 1st step stop b, 2nd step stop a
*
* This has the side effect that restarts are properly
* synchronized too. */
return true;
}
int job_run_and_invalidate(Job *j) {
int r;
assert(j);
if (j->in_run_queue) {
j->in_run_queue = false;
}
if (j->state != JOB_WAITING)
return 0;
if (!job_is_runnable(j))
return -EAGAIN;
j->state = JOB_RUNNING;
switch (j->type) {
case JOB_START:
r = unit_start(j->unit);
if (r == -EBADR)
r = 0;
break;
case JOB_VERIFY_ACTIVE: {
if (UNIT_IS_ACTIVE_OR_RELOADING(t))
r = -EALREADY;
else if (t == UNIT_ACTIVATING)
r = -EAGAIN;
else
r = -ENOEXEC;
break;
}
case JOB_STOP:
break;
case JOB_RELOAD:
r = unit_reload(j->unit);
break;
case JOB_RELOAD_OR_START:
r = unit_reload(j->unit);
else
r = unit_start(j->unit);
break;
case JOB_RESTART: {
r = unit_start(j->unit);
} else
break;
}
case JOB_TRY_RESTART: {
r = -ENOEXEC;
else if (t == UNIT_ACTIVATING) {
r = unit_start(j->unit);
} else
break;
}
default:
assert_not_reached("Unknown job type");
}
if (r == -EALREADY)
r = job_finish_and_invalidate(j, true);
else if (r == -EAGAIN) {
j->state = JOB_WAITING;
return -EAGAIN;
} else if (r < 0)
r = job_finish_and_invalidate(j, false);
return r;
}
Unit *u;
JobType t;
Iterator i;
assert(j);
/* Patch restart jobs so that they become normal start jobs */
log_debug("Converting job %s/%s -> %s/%s",
j->state = JOB_WAITING;
return 0;
}
log_debug("Job %s/%s finished, success=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(success));
u = j->unit;
t = j->type;
job_free(j);
if (!success)
unit_status_printf(u, "Starting %s " ANSI_HIGHLIGHT_ON "failed" ANSI_HIGHLIGHT_OFF ".\n", unit_description(u));
/* Fail depending jobs on failure */
if (!success) {
if (t == JOB_START ||
t == JOB_VERIFY_ACTIVE ||
t == JOB_RELOAD_OR_START) {
} else if (t == JOB_STOP) {
}
}
/* Try to start the next jobs that can be started */
return 0;
}
void job_add_to_run_queue(Job *j) {
assert(j);
if (j->in_run_queue)
return;
j->in_run_queue = true;
}
void job_add_to_dbus_queue(Job *j) {
assert(j);
if (j->in_dbus_queue)
return;
/* We don't check if anybody is subscribed here, since this
* job might just have been created and not yet assigned to a
* connection/client. */
j->in_dbus_queue = true;
}
char *job_dbus_path(Job *j) {
char *p;
assert(j);
return NULL;
return p;
}
static const char* const job_state_table[_JOB_STATE_MAX] = {
[JOB_WAITING] = "waiting",
[JOB_RUNNING] = "running"
};
static const char* const job_type_table[_JOB_TYPE_MAX] = {
[JOB_START] = "start",
[JOB_VERIFY_ACTIVE] = "verify-active",
[JOB_STOP] = "stop",
[JOB_RELOAD] = "reload",
[JOB_RELOAD_OR_START] = "reload-or-start",
[JOB_RESTART] = "restart",
[JOB_TRY_RESTART] = "try-restart",
};
static const char* const job_mode_table[_JOB_MODE_MAX] = {
[JOB_FAIL] = "fail",
[JOB_REPLACE] = "replace",
[JOB_ISOLATE] = "isolate"
};