X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=bindings.c;h=9657160fe485cf70a2248b3f9b8345dc818f76cc;hb=3137a0a63db273005d62973a5d055a7623e98631;hp=f3aefa15004338f0718c6c6bdbc1a6ef1ea40800;hpb=2416e70e53de4d128f3e704144495b007ed6c04e;p=mirror_lxcfs.git diff --git a/bindings.c b/bindings.c index f3aefa1..9657160 100644 --- a/bindings.c +++ b/bindings.c @@ -139,6 +139,7 @@ static char **hierarchies; * another namespace using the *at() family of functions * {openat(), fchownat(), ...}. */ static int *fd_hierarchies; +static int cgroup_mount_ns_fd = -1; static void unlock_mutex(pthread_mutex_t *l) { @@ -421,6 +422,7 @@ static void print_subsystems(void) { int i; + fprintf(stderr, "mount namespace: %d\n", cgroup_mount_ns_fd); fprintf(stderr, "hierarchies:\n"); for (i = 0; i < num_hierarchies; i++) { if (hierarchies[i]) @@ -2959,23 +2961,23 @@ static void parse_memstat(char *memstat, unsigned long *cached, char *eol; while (*memstat) { - if (startswith(memstat, "cache")) { - sscanf(memstat + 5, "%lu", cached); + if (startswith(memstat, "total_cache")) { + sscanf(memstat + 11, "%lu", cached); *cached /= 1024; - } else if (startswith(memstat, "active_anon")) { - sscanf(memstat + 11, "%lu", active_anon); + } else if (startswith(memstat, "total_active_anon")) { + sscanf(memstat + 17, "%lu", active_anon); *active_anon /= 1024; - } else if (startswith(memstat, "inactive_anon")) { - sscanf(memstat + 13, "%lu", inactive_anon); + } else if (startswith(memstat, "total_inactive_anon")) { + sscanf(memstat + 19, "%lu", inactive_anon); *inactive_anon /= 1024; - } else if (startswith(memstat, "active_file")) { - sscanf(memstat + 11, "%lu", active_file); + } else if (startswith(memstat, "total_active_file")) { + sscanf(memstat + 17, "%lu", active_file); *active_file /= 1024; - } else if (startswith(memstat, "inactive_file")) { - sscanf(memstat + 13, "%lu", inactive_file); + } else if (startswith(memstat, "total_inactive_file")) { + sscanf(memstat + 19, "%lu", inactive_file); *inactive_file /= 1024; - } else if (startswith(memstat, "unevictable")) { - sscanf(memstat + 11, "%lu", unevictable); + } else if (startswith(memstat, "total_unevictable")) { + sscanf(memstat + 17, "%lu", unevictable); *unevictable /= 1024; } eol = strchr(memstat, '\n'); @@ -3165,16 +3167,16 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset, snprintf(lbuf, 100, "MemFree: %8lu kB\n", memlimit - memusage); printme = lbuf; } else if (startswith(line, "MemAvailable:")) { - snprintf(lbuf, 100, "MemAvailable: %8lu kB\n", memlimit - memusage); + snprintf(lbuf, 100, "MemAvailable: %8lu kB\n", memlimit - memusage + cached); printme = lbuf; } else if (startswith(line, "SwapTotal:") && memswlimit > 0) { sscanf(line+sizeof("SwapTotal:")-1, "%lu", &hostswtotal); - if (hostswtotal < memswlimit - memlimit) - memswlimit = hostswtotal + memlimit; - snprintf(lbuf, 100, "SwapTotal: %8lu kB\n", memswlimit - memlimit); + if (hostswtotal < memswlimit) + memswlimit = hostswtotal; + snprintf(lbuf, 100, "SwapTotal: %8lu kB\n", memswlimit); printme = lbuf; } else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0) { - unsigned long swaptotal = memswlimit - memlimit, + unsigned long swaptotal = memswlimit, swapusage = memswusage - memusage, swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0; snprintf(lbuf, 100, "SwapFree: %8lu kB\n", swapfree); @@ -3591,24 +3593,6 @@ static uint64_t get_reaper_age(pid_t pid) return procage; } -static uint64_t get_reaper_btime(pid) -{ - int ret; - struct sysinfo sys; - uint64_t procstart; - uint64_t uptime; - - ret = sysinfo(&sys); - if (ret < 0) { - lxcfs_debug("%s\n", "failed to retrieve system information"); - return 0; - } - - uptime = (uint64_t)time(NULL) - (uint64_t)sys.uptime; - procstart = get_reaper_start_time_in_sec(pid); - return uptime + procstart; -} - #define CPUALL_MAX_SIZE (BUF_RESERVE_SIZE / 2) static int proc_stat_read(char *buf, size_t size, off_t offset, struct fuse_file_info *fi) @@ -3672,10 +3656,7 @@ static int proc_stat_read(char *buf, size_t size, off_t offset, continue; if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) { /* not a ^cpuN line containing a number N, just print it */ - if (strncmp(line, "btime", 5) == 0) - l = snprintf(cache, cache_size, "btime %"PRIu64"\n", get_reaper_btime(fc->pid)); - else - l = snprintf(cache, cache_size, "%s", line); + l = snprintf(cache, cache_size, "%s", line); if (l < 0) { perror("Error writing to cache"); rv = 0; @@ -3845,10 +3826,10 @@ static int proc_uptime_read(char *buf, size_t size, off_t offset, #endif if (offset){ - if (offset > d->size) - return -EINVAL; if (!d->cached) return 0; + if (offset > d->size) + return -EINVAL; int left = d->size - offset; total_len = left > size ? size: left; memcpy(buf, cache + offset, total_len); @@ -3863,8 +3844,8 @@ static int proc_uptime_read(char *buf, size_t size, off_t offset, if (reaperage >= busytime) idletime = reaperage - busytime; - total_len = snprintf(d->buf, d->size, "%"PRIu64".00 %"PRIu64".00\n", reaperage, idletime); - if (total_len < 0 || total_len >= d->size){ + total_len = snprintf(d->buf, d->buflen, "%"PRIu64".00 %"PRIu64".00\n", reaperage, idletime); + if (total_len < 0 || total_len >= d->buflen){ lxcfs_error("%s\n", "failed to write to cache"); return 0; } @@ -4480,6 +4461,19 @@ static bool permute_root(void) return true; } +static int preserve_mnt_ns(int pid) +{ + int ret; + size_t len = sizeof("/proc/") + 21 + sizeof("/ns/mnt"); + char path[len]; + + ret = snprintf(path, len, "/proc/%d/ns/mnt", pid); + if (ret < 0 || (size_t)ret >= len) + return -1; + + return open(path, O_RDONLY | O_CLOEXEC); +} + static bool cgfs_prepare_mounts(void) { if (!mkdir_p(BASEDIR, 0700)) { @@ -4497,6 +4491,12 @@ static bool cgfs_prepare_mounts(void) return false; } + cgroup_mount_ns_fd = preserve_mnt_ns(getpid()); + if (cgroup_mount_ns_fd < 0) { + lxcfs_error("Failed to preserve mount namespace: %s.\n", strerror(errno)); + return false; + } + if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0) < 0) { lxcfs_error("Failed to remount / private: %s.\n", strerror(errno)); return false; @@ -4570,19 +4570,6 @@ static bool cgfs_setup_controllers(void) return true; } -static int preserve_ns(int pid) -{ - int ret; - size_t len = 5 /* /proc */ + 21 /* /int_as_str */ + 7 /* /ns/mnt */ + 1 /* \0 */; - char path[len]; - - ret = snprintf(path, len, "/proc/%d/ns/mnt", pid); - if (ret < 0 || (size_t)ret >= len) - return -1; - - return open(path, O_RDONLY | O_CLOEXEC); -} - static void __attribute__((constructor)) collect_and_mount_subsystems(void) { FILE *f; @@ -4626,7 +4613,7 @@ static void __attribute__((constructor)) collect_and_mount_subsystems(void) } /* Preserve initial namespace. */ - init_ns = preserve_ns(getpid()); + init_ns = preserve_mnt_ns(getpid()); if (init_ns < 0) { lxcfs_error("%s\n", "Failed to preserve initial mount namespace."); goto out; @@ -4683,4 +4670,7 @@ static void __attribute__((destructor)) free_subsystems(void) } free(hierarchies); free(fd_hierarchies); + + if (cgroup_mount_ns_fd >= 0) + close(cgroup_mount_ns_fd); }