Merge pull request #7631 from cgwalters/systemctl-offline
systemctl,verbs: Introduce SYSTEMCTL_OFFLINE environment variable
This commit is contained in:
+48
-3
@@ -23,12 +23,54 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "env-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "string-util.h"
|
||||
#include "verbs.h"
|
||||
#include "virt.h"
|
||||
|
||||
/* Wraps running_in_chroot() which is used in various places,
|
||||
* but also adds an environment variable check so external processes
|
||||
* can reliably force this on.
|
||||
*/
|
||||
bool running_in_chroot_or_offline(void) {
|
||||
int r;
|
||||
|
||||
/* Added to support use cases like rpm-ostree, where from %post
|
||||
* scripts we only want to execute "preset", but not "start"/"restart"
|
||||
* for example.
|
||||
*
|
||||
* See ENVIRONMENT.md for docs.
|
||||
*/
|
||||
r = getenv_bool("SYSTEMD_OFFLINE");
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Parsing SYSTEMD_OFFLINE: %m");
|
||||
else if (r == 0)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
|
||||
/* We've had this condition check for a long time which basically
|
||||
* checks for legacy chroot case like Fedora's
|
||||
* "mock", which is used for package builds. We don't want
|
||||
* to try to start systemd services there, since without --new-chroot
|
||||
* we don't even have systemd running, and even if we did, adding
|
||||
* a concept of background daemons to builds would be an enormous change,
|
||||
* requiring considering things like how the journal output is handled, etc.
|
||||
* And there's really not a use case today for a build talking to a service.
|
||||
*
|
||||
* Note this call itself also looks for a different variable SYSTEMD_IGNORE_CHROOT=1.
|
||||
*/
|
||||
r = running_in_chroot();
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "running_in_chroot(): %m");
|
||||
else if (r > 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
|
||||
const Verb *verb;
|
||||
const char *name;
|
||||
@@ -84,12 +126,15 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if ((verb->flags & VERB_NOCHROOT) && running_in_chroot() > 0) {
|
||||
log_info("Running in chroot, ignoring request.");
|
||||
if ((verb->flags & VERB_ONLINE_ONLY) && running_in_chroot_or_offline()) {
|
||||
if (name)
|
||||
log_info("Running in chroot, ignoring request: %s", name);
|
||||
else
|
||||
log_info("Running in chroot, ignoring request.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (verb->flags & VERB_MUSTBEROOT) {
|
||||
if (verb->flags & VERB_MUST_BE_ROOT) {
|
||||
r = must_be_root();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
+5
-3
@@ -23,9 +23,9 @@
|
||||
#define VERB_ANY ((unsigned) -1)
|
||||
|
||||
typedef enum VerbFlags {
|
||||
VERB_DEFAULT = 1 << 0,
|
||||
VERB_NOCHROOT = 1 << 1,
|
||||
VERB_MUSTBEROOT = 1 << 2,
|
||||
VERB_DEFAULT = 1 << 0,
|
||||
VERB_ONLINE_ONLY = 1 << 1,
|
||||
VERB_MUST_BE_ROOT = 1 << 2,
|
||||
} VerbFlags;
|
||||
|
||||
typedef struct {
|
||||
@@ -35,4 +35,6 @@ typedef struct {
|
||||
int (* const dispatch)(int argc, char *argv[], void *userdata);
|
||||
} Verb;
|
||||
|
||||
bool running_in_chroot_or_offline(void);
|
||||
|
||||
int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata);
|
||||
|
||||
Reference in New Issue
Block a user