|
|
771b4bc |
From c22907908362b93f723f9161c81f8d55310d9647 Mon Sep 17 00:00:00 2001
|
|
|
771b4bc |
From: Lennart Poettering <lennart@poettering.net>
|
|
|
771b4bc |
Date: Sun, 22 Apr 2012 14:48:46 +0200
|
|
|
771b4bc |
Subject: [PATCH] util: unify getenv() logic for other PID (cherry picked from
|
|
|
771b4bc |
commit ab94af9201496ea3aa59bbf2a01eb750fbd1c08a)
|
|
|
771b4bc |
|
|
|
771b4bc |
---
|
|
|
771b4bc |
src/core/machine-id-setup.c | 45 +++++++------------------
|
|
|
771b4bc |
src/shared/util.c | 62 ++++++++++++++++++++++++++++++++++
|
|
|
771b4bc |
src/shared/util.h | 2 ++
|
|
|
771b4bc |
src/shared/virt.c | 77 +++++++++++++------------------------------
|
|
|
771b4bc |
4 files changed, 97 insertions(+), 89 deletions(-)
|
|
|
771b4bc |
|
|
|
771b4bc |
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
|
|
|
771b4bc |
index 94198cb..d53757c 100644
|
|
|
771b4bc |
--- a/src/core/machine-id-setup.c
|
|
|
771b4bc |
+++ b/src/core/machine-id-setup.c
|
|
|
771b4bc |
@@ -110,45 +110,22 @@ static int generate(char id[34]) {
|
|
|
771b4bc |
/* If that didn't work either, see if we are running in a
|
|
|
771b4bc |
* container, and a machine ID was passed in via
|
|
|
771b4bc |
* $container_uuid the way libvirt/LXC does it */
|
|
|
771b4bc |
-
|
|
|
771b4bc |
r = detect_container(NULL);
|
|
|
771b4bc |
if (r > 0) {
|
|
|
771b4bc |
- FILE *f;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- f = fopen("/proc/1/environ", "re");
|
|
|
771b4bc |
- if (f) {
|
|
|
771b4bc |
- bool done = false;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- do {
|
|
|
771b4bc |
- char line[LINE_MAX];
|
|
|
771b4bc |
- unsigned i;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- for (i = 0; i < sizeof(line)-1; i++) {
|
|
|
771b4bc |
- int c;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- c = getc(f);
|
|
|
771b4bc |
- if (_unlikely_(c == EOF)) {
|
|
|
771b4bc |
- done = true;
|
|
|
771b4bc |
- break;
|
|
|
771b4bc |
- } else if (c == 0)
|
|
|
771b4bc |
- break;
|
|
|
771b4bc |
+ char *e;
|
|
|
771b4bc |
|
|
|
771b4bc |
- line[i] = c;
|
|
|
771b4bc |
- }
|
|
|
771b4bc |
- line[i] = 0;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- if (startswith(line, "container_uuid=") &&
|
|
|
771b4bc |
- strlen(line + 15) >= 36) {
|
|
|
771b4bc |
- r = shorten_uuid(id, line + 15);
|
|
|
771b4bc |
- if (r >= 0) {
|
|
|
771b4bc |
- log_info("Initializing machine ID from container UUID");
|
|
|
771b4bc |
- return 0;
|
|
|
771b4bc |
- }
|
|
|
771b4bc |
+ r = getenv_for_pid(1, "container_uuid", &e);
|
|
|
771b4bc |
+ if (r > 0) {
|
|
|
771b4bc |
+ if (strlen(e) >= 36) {
|
|
|
771b4bc |
+ r = shorten_uuid(id, e);
|
|
|
771b4bc |
+ if (r >= 0) {
|
|
|
771b4bc |
+ log_info("Initializing machine ID from container UUID");
|
|
|
771b4bc |
+ free(e);
|
|
|
771b4bc |
+ return 0;
|
|
|
771b4bc |
}
|
|
|
771b4bc |
+ }
|
|
|
771b4bc |
|
|
|
771b4bc |
- } while (!done);
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- fclose(f);
|
|
|
771b4bc |
+ free(e);
|
|
|
771b4bc |
}
|
|
|
771b4bc |
}
|
|
|
771b4bc |
|
|
|
771b4bc |
diff --git a/src/shared/util.c b/src/shared/util.c
|
|
|
771b4bc |
index e314827..7a5e57c 100644
|
|
|
771b4bc |
--- a/src/shared/util.c
|
|
|
771b4bc |
+++ b/src/shared/util.c
|
|
|
771b4bc |
@@ -6168,3 +6168,65 @@ int path_is_read_only_fs(const char *path) {
|
|
|
771b4bc |
|
|
|
771b4bc |
return !!(st.f_flag & ST_RDONLY);
|
|
|
771b4bc |
}
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+int getenv_for_pid(pid_t pid, const char *field, char **_value) {
|
|
|
771b4bc |
+ char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL;
|
|
|
771b4bc |
+ int r;
|
|
|
771b4bc |
+ FILE *f;
|
|
|
771b4bc |
+ bool done = false;
|
|
|
771b4bc |
+ size_t l;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ assert(field);
|
|
|
771b4bc |
+ assert(_value);
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ if (pid == 0)
|
|
|
771b4bc |
+ pid = getpid();
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
|
|
|
771b4bc |
+ char_array_0(path);
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ f = fopen(path, "re");
|
|
|
771b4bc |
+ if (!f)
|
|
|
771b4bc |
+ return -errno;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ l = strlen(field);
|
|
|
771b4bc |
+ r = 0;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ do {
|
|
|
771b4bc |
+ char line[LINE_MAX];
|
|
|
771b4bc |
+ unsigned i;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ for (i = 0; i < sizeof(line)-1; i++) {
|
|
|
771b4bc |
+ int c;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ c = getc(f);
|
|
|
771b4bc |
+ if (_unlikely_(c == EOF)) {
|
|
|
771b4bc |
+ done = true;
|
|
|
771b4bc |
+ break;
|
|
|
771b4bc |
+ } else if (c == 0)
|
|
|
771b4bc |
+ break;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ line[i] = c;
|
|
|
771b4bc |
+ }
|
|
|
771b4bc |
+ line[i] = 0;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ if (memcmp(line, field, l) == 0 && line[l] == '=') {
|
|
|
771b4bc |
+ value = strdup(line + l + 1);
|
|
|
771b4bc |
+ if (!value) {
|
|
|
771b4bc |
+ r = -ENOMEM;
|
|
|
771b4bc |
+ break;
|
|
|
771b4bc |
+ }
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ r = 1;
|
|
|
771b4bc |
+ break;
|
|
|
771b4bc |
+ }
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ } while (!done);
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ fclose(f);
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ if (r >= 0)
|
|
|
771b4bc |
+ *_value = value;
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ return r;
|
|
|
771b4bc |
+}
|
|
|
771b4bc |
diff --git a/src/shared/util.h b/src/shared/util.h
|
|
|
771b4bc |
index f4190a3..a69807d 100644
|
|
|
771b4bc |
--- a/src/shared/util.h
|
|
|
771b4bc |
+++ b/src/shared/util.h
|
|
|
771b4bc |
@@ -540,4 +540,6 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
|
|
|
771b4bc |
|
|
|
771b4bc |
int setrlimit_closest(int resource, const struct rlimit *rlim);
|
|
|
771b4bc |
|
|
|
771b4bc |
+int getenv_for_pid(pid_t pid, const char *field, char **_value);
|
|
|
771b4bc |
+
|
|
|
771b4bc |
#endif
|
|
|
771b4bc |
diff --git a/src/shared/virt.c b/src/shared/virt.c
|
|
|
771b4bc |
index 4c526ff..a31b9e4 100644
|
|
|
771b4bc |
--- a/src/shared/virt.c
|
|
|
771b4bc |
+++ b/src/shared/virt.c
|
|
|
771b4bc |
@@ -153,7 +153,8 @@ int detect_vm(const char **id) {
|
|
|
771b4bc |
}
|
|
|
771b4bc |
|
|
|
771b4bc |
int detect_container(const char **id) {
|
|
|
771b4bc |
- FILE *f;
|
|
|
771b4bc |
+ char *e = NULL;
|
|
|
771b4bc |
+ int r;
|
|
|
771b4bc |
|
|
|
771b4bc |
/* Unfortunately many of these operations require root access
|
|
|
771b4bc |
* in one way or another */
|
|
|
771b4bc |
@@ -180,63 +181,29 @@ int detect_container(const char **id) {
|
|
|
771b4bc |
return 1;
|
|
|
771b4bc |
}
|
|
|
771b4bc |
|
|
|
771b4bc |
- f = fopen("/proc/1/environ", "re");
|
|
|
771b4bc |
- if (f) {
|
|
|
771b4bc |
- bool done = false;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- do {
|
|
|
771b4bc |
- char line[LINE_MAX];
|
|
|
771b4bc |
- unsigned i;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- for (i = 0; i < sizeof(line)-1; i++) {
|
|
|
771b4bc |
- int c;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- c = getc(f);
|
|
|
771b4bc |
- if (_unlikely_(c == EOF)) {
|
|
|
771b4bc |
- done = true;
|
|
|
771b4bc |
- break;
|
|
|
771b4bc |
- } else if (c == 0)
|
|
|
771b4bc |
- break;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- line[i] = c;
|
|
|
771b4bc |
- }
|
|
|
771b4bc |
- line[i] = 0;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- if (streq(line, "container=lxc")) {
|
|
|
771b4bc |
- fclose(f);
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- if (id)
|
|
|
771b4bc |
- *id = "lxc";
|
|
|
771b4bc |
- return 1;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- } else if (streq(line, "container=lxc-libvirt")) {
|
|
|
771b4bc |
- fclose(f);
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- if (id)
|
|
|
771b4bc |
- *id = "lxc-libvirt";
|
|
|
771b4bc |
- return 1;
|
|
|
771b4bc |
+ r = getenv_for_pid(1, "container", &e);
|
|
|
771b4bc |
+ if (r <= 0)
|
|
|
771b4bc |
+ return r;
|
|
|
771b4bc |
|
|
|
771b4bc |
- } else if (streq(line, "container=systemd-nspawn")) {
|
|
|
771b4bc |
- fclose(f);
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- if (id)
|
|
|
771b4bc |
- *id = "systemd-nspawn";
|
|
|
771b4bc |
- return 1;
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- } else if (startswith(line, "container=")) {
|
|
|
771b4bc |
- fclose(f);
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- if (id)
|
|
|
771b4bc |
- *id = "other";
|
|
|
771b4bc |
- return 1;
|
|
|
771b4bc |
- }
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- } while (!done);
|
|
|
771b4bc |
-
|
|
|
771b4bc |
- fclose(f);
|
|
|
771b4bc |
+ /* We only recognize a selected few here, since we want to
|
|
|
771b4bc |
+ * enforce a redacted namespace */
|
|
|
771b4bc |
+ if (streq(e, "lxc")) {
|
|
|
771b4bc |
+ if (id)
|
|
|
771b4bc |
+ *id = "lxc";
|
|
|
771b4bc |
+ } else if (streq(e, "lxc-libvirt")) {
|
|
|
771b4bc |
+ if (id)
|
|
|
771b4bc |
+ *id = "lxc-libvirt";
|
|
|
771b4bc |
+ } else if (streq(e, "systemd-nspawn")) {
|
|
|
771b4bc |
+ if (id)
|
|
|
771b4bc |
+ *id = "systemd-nspawn";
|
|
|
771b4bc |
+ } else {
|
|
|
771b4bc |
+ if (id)
|
|
|
771b4bc |
+ *id = "other";
|
|
|
771b4bc |
}
|
|
|
771b4bc |
|
|
|
771b4bc |
- return 0;
|
|
|
771b4bc |
+ free(e);
|
|
|
771b4bc |
+
|
|
|
771b4bc |
+ return r;
|
|
|
771b4bc |
}
|
|
|
771b4bc |
|
|
|
771b4bc |
/* Returns a short identifier for the various VM/container implementations */
|