util: unify getenv() logic for other PID
This commit is contained in:
parent
144f0fc0c8
commit
ab94af9201
|
@ -110,45 +110,22 @@ static int generate(char id[34]) {
|
||||||
/* If that didn't work either, see if we are running in a
|
/* If that didn't work either, see if we are running in a
|
||||||
* container, and a machine ID was passed in via
|
* container, and a machine ID was passed in via
|
||||||
* $container_uuid the way libvirt/LXC does it */
|
* $container_uuid the way libvirt/LXC does it */
|
||||||
|
|
||||||
r = detect_container(NULL);
|
r = detect_container(NULL);
|
||||||
if (r > 0) {
|
if (r > 0) {
|
||||||
FILE *f;
|
char *e;
|
||||||
|
|
||||||
f = fopen("/proc/1/environ", "re");
|
r = getenv_for_pid(1, "container_uuid", &e);
|
||||||
if (f) {
|
if (r > 0) {
|
||||||
bool done = false;
|
if (strlen(e) >= 36) {
|
||||||
|
r = shorten_uuid(id, e);
|
||||||
do {
|
if (r >= 0) {
|
||||||
char line[LINE_MAX];
|
log_info("Initializing machine ID from container UUID");
|
||||||
unsigned i;
|
free(e);
|
||||||
|
return 0;
|
||||||
for (i = 0; i < sizeof(line)-1; i++) {
|
|
||||||
int c;
|
|
||||||
|
|
||||||
c = getc(f);
|
|
||||||
if (_unlikely_(c == EOF)) {
|
|
||||||
done = true;
|
|
||||||
break;
|
|
||||||
} else if (c == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
line[i] = c;
|
|
||||||
}
|
}
|
||||||
line[i] = 0;
|
}
|
||||||
|
|
||||||
if (startswith(line, "container_uuid=") &&
|
free(e);
|
||||||
strlen(line + 15) >= 36) {
|
|
||||||
r = shorten_uuid(id, line + 15);
|
|
||||||
if (r >= 0) {
|
|
||||||
log_info("Initializing machine ID from container UUID");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (!done);
|
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6168,3 +6168,65 @@ int path_is_read_only_fs(const char *path) {
|
||||||
|
|
||||||
return !!(st.f_flag & ST_RDONLY);
|
return !!(st.f_flag & ST_RDONLY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getenv_for_pid(pid_t pid, const char *field, char **_value) {
|
||||||
|
char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL;
|
||||||
|
int r;
|
||||||
|
FILE *f;
|
||||||
|
bool done = false;
|
||||||
|
size_t l;
|
||||||
|
|
||||||
|
assert(field);
|
||||||
|
assert(_value);
|
||||||
|
|
||||||
|
if (pid == 0)
|
||||||
|
pid = getpid();
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
|
||||||
|
char_array_0(path);
|
||||||
|
|
||||||
|
f = fopen(path, "re");
|
||||||
|
if (!f)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
l = strlen(field);
|
||||||
|
r = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
char line[LINE_MAX];
|
||||||
|
unsigned i;
|
||||||
|
|
||||||
|
for (i = 0; i < sizeof(line)-1; i++) {
|
||||||
|
int c;
|
||||||
|
|
||||||
|
c = getc(f);
|
||||||
|
if (_unlikely_(c == EOF)) {
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
} else if (c == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
line[i] = c;
|
||||||
|
}
|
||||||
|
line[i] = 0;
|
||||||
|
|
||||||
|
if (memcmp(line, field, l) == 0 && line[l] == '=') {
|
||||||
|
value = strdup(line + l + 1);
|
||||||
|
if (!value) {
|
||||||
|
r = -ENOMEM;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
r = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (!done);
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
if (r >= 0)
|
||||||
|
*_value = value;
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
|
@ -540,4 +540,6 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
|
||||||
|
|
||||||
int setrlimit_closest(int resource, const struct rlimit *rlim);
|
int setrlimit_closest(int resource, const struct rlimit *rlim);
|
||||||
|
|
||||||
|
int getenv_for_pid(pid_t pid, const char *field, char **_value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -153,7 +153,8 @@ int detect_vm(const char **id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int detect_container(const char **id) {
|
int detect_container(const char **id) {
|
||||||
FILE *f;
|
char *e = NULL;
|
||||||
|
int r;
|
||||||
|
|
||||||
/* Unfortunately many of these operations require root access
|
/* Unfortunately many of these operations require root access
|
||||||
* in one way or another */
|
* in one way or another */
|
||||||
|
@ -180,63 +181,29 @@ int detect_container(const char **id) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
f = fopen("/proc/1/environ", "re");
|
r = getenv_for_pid(1, "container", &e);
|
||||||
if (f) {
|
if (r <= 0)
|
||||||
bool done = false;
|
return r;
|
||||||
|
|
||||||
do {
|
/* We only recognize a selected few here, since we want to
|
||||||
char line[LINE_MAX];
|
* enforce a redacted namespace */
|
||||||
unsigned i;
|
if (streq(e, "lxc")) {
|
||||||
|
if (id)
|
||||||
for (i = 0; i < sizeof(line)-1; i++) {
|
*id = "lxc";
|
||||||
int c;
|
} else if (streq(e, "lxc-libvirt")) {
|
||||||
|
if (id)
|
||||||
c = getc(f);
|
*id = "lxc-libvirt";
|
||||||
if (_unlikely_(c == EOF)) {
|
} else if (streq(e, "systemd-nspawn")) {
|
||||||
done = true;
|
if (id)
|
||||||
break;
|
*id = "systemd-nspawn";
|
||||||
} else if (c == 0)
|
} else {
|
||||||
break;
|
if (id)
|
||||||
|
*id = "other";
|
||||||
line[i] = c;
|
|
||||||
}
|
|
||||||
line[i] = 0;
|
|
||||||
|
|
||||||
if (streq(line, "container=lxc")) {
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (id)
|
|
||||||
*id = "lxc";
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
} else if (streq(line, "container=lxc-libvirt")) {
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (id)
|
|
||||||
*id = "lxc-libvirt";
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
} else if (streq(line, "container=systemd-nspawn")) {
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (id)
|
|
||||||
*id = "systemd-nspawn";
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
} else if (startswith(line, "container=")) {
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (id)
|
|
||||||
*id = "other";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
} while (!done);
|
|
||||||
|
|
||||||
fclose(f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
free(e);
|
||||||
|
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns a short identifier for the various VM/container implementations */
|
/* Returns a short identifier for the various VM/container implementations */
|
||||||
|
|
Loading…
Reference in a new issue