mirror of
https://github.com/systemd/systemd.git
synced 2024-12-20 07:33:41 +08:00
util: add detect_container()
This commit is contained in:
parent
da5b3bad1c
commit
ef2df9f415
80
src/util.c
80
src/util.c
@ -3945,33 +3945,32 @@ int detect_vm(const char **id) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns a short identifier for the various VM/container implementations */
|
int detect_container(const char **id) {
|
||||||
int detect_virtualization(const char **id) {
|
|
||||||
int r;
|
|
||||||
static __thread const char *cached_id = NULL;
|
|
||||||
const char *_id;
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
if (cached_id) {
|
/* Unfortunately many of these operations require root access
|
||||||
|
* in one way or another */
|
||||||
|
|
||||||
if (cached_id == (const char*) -1)
|
if (geteuid() != 0)
|
||||||
return 0;
|
return -EPERM;
|
||||||
|
|
||||||
|
if (running_in_chroot() > 0) {
|
||||||
|
|
||||||
if (id)
|
if (id)
|
||||||
*id = cached_id;
|
*id = "chroot";
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unfortunately most of these operations require root access
|
/* /proc/vz exists in container and outside of the container,
|
||||||
* in one way or another */
|
* /proc/bc only outside of the container. */
|
||||||
if (geteuid() != 0)
|
if (access("/proc/vz", F_OK) >= 0 &&
|
||||||
return -EPERM;
|
access("/proc/bc", F_OK) < 0) {
|
||||||
|
|
||||||
if ((r = running_in_chroot()) > 0) {
|
if (id)
|
||||||
_id = "chroot";
|
*id = "openvz";
|
||||||
r = 1;
|
|
||||||
goto finish;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((f = fopen("/proc/self/cgroup", "r"))) {
|
if ((f = fopen("/proc/self/cgroup", "r"))) {
|
||||||
@ -3991,36 +3990,49 @@ int detect_virtualization(const char **id) {
|
|||||||
if (!streq(p, ":ns:/")) {
|
if (!streq(p, ":ns:/")) {
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
r = 1;
|
if (id)
|
||||||
_id = "ns";
|
*id = "ns";
|
||||||
goto finish;
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* /proc/vz exists in container and outside of the container,
|
return 0;
|
||||||
* /proc/bc only outside of the container. */
|
}
|
||||||
if (access("/proc/vz", F_OK) >= 0 &&
|
|
||||||
access("/proc/bc", F_OK) < 0) {
|
/* Returns a short identifier for the various VM/container implementations */
|
||||||
_id = "openvz";
|
int detect_virtualization(const char **id) {
|
||||||
r = 1;
|
static __thread const char *cached_id = NULL;
|
||||||
goto finish;
|
const char *_id;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
if (cached_id) {
|
||||||
|
|
||||||
|
if (cached_id == (const char*) -1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (id)
|
||||||
|
*id = cached_id;
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((r = detect_container(&_id)) != 0)
|
||||||
|
goto finish;
|
||||||
|
|
||||||
r = detect_vm(&_id);
|
r = detect_vm(&_id);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
if (r < 0)
|
if (r > 0) {
|
||||||
return r;
|
|
||||||
else if (r > 0)
|
|
||||||
cached_id = _id;
|
cached_id = _id;
|
||||||
else
|
|
||||||
cached_id = (const char*) -1;
|
|
||||||
|
|
||||||
if (id)
|
if (id)
|
||||||
*id = _id;
|
*id = _id;
|
||||||
|
} else if (r == 0)
|
||||||
|
cached_id = (const char*) -1;
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -380,6 +380,7 @@ bool tty_is_vc(const char *tty);
|
|||||||
const char *default_term_for_tty(const char *tty);
|
const char *default_term_for_tty(const char *tty);
|
||||||
|
|
||||||
int detect_vm(const char **id);
|
int detect_vm(const char **id);
|
||||||
|
int detect_container(const char **id);
|
||||||
int detect_virtualization(const char **id);
|
int detect_virtualization(const char **id);
|
||||||
|
|
||||||
void execute_directory(const char *directory, DIR *_d, char *argv[]);
|
void execute_directory(const char *directory, DIR *_d, char *argv[]);
|
||||||
|
Loading…
Reference in New Issue
Block a user