1 Star 0 Fork 9

wuzx/lxcfs

forked from src-openEuler/lxcfs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0002-show-dev-name-in-container.patch 7.00 KB
一键复制 编辑 原始数据 按行查看 历史
vegbir 提交于 2023-07-27 09:31 . upgrade to 5.0.4
From 79d335011ffccfee542ee5068fda6257a9ac1e59 Mon Sep 17 00:00:00 2001
From: vegbir <[email protected]>
Date: Thu, 27 Jul 2023 06:40:47 +0000
Subject: [PATCH 02/15] show dev name in container
append: fix hang
append: limit-stat-by-quota-period-setting
append: fix-proc-diskstats-show-in-container
append: proc_fuse fix wait child process hang
append: fix deadlock problem when subprocess exit
append: fix dev read memory leak in container
Signed-off-by: vegbir <[email protected]>
---
src/proc_fuse.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 181 insertions(+), 2 deletions(-)
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 25af10a..cde48ca 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -28,6 +28,7 @@
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
+#include <sys/sysmacros.h>
#include <sys/vfs.h>
#include "proc_fuse.h"
@@ -43,6 +44,26 @@
#include "proc_cpuview.h"
#include "utils.h"
+static pthread_mutex_t container_dev_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void lock_mutex(pthread_mutex_t *l)
+{
+ int ret;
+
+ ret = pthread_mutex_lock(l);
+ if (ret)
+ log_exit("%s - returned %d\n", strerror(ret), ret);
+}
+
+static void unlock_mutex(pthread_mutex_t *l)
+{
+ int ret;
+
+ ret = pthread_mutex_unlock(l);
+ if (ret)
+ log_exit("%s - returned %d\n", strerror(ret), ret);
+}
+
struct memory_stat {
uint64_t hierarchical_memory_limit;
uint64_t hierarchical_memsw_limit;
@@ -473,6 +494,146 @@ static void get_blkio_io_value(char *str, unsigned major, unsigned minor,
}
}
+struct devinfo {
+ char *name;
+ unsigned int major, minor;
+ struct devinfo *next;
+};
+
+int getns(pid_t pid, const char *ns_type)
+{
+ char fpath[100];
+ memset(fpath, 0, sizeof(fpath));
+ snprintf(fpath, 99, "/proc/%d/ns/%s", pid, ns_type);
+ return open(fpath, O_RDONLY);
+}
+
+struct devinfo* container_dev_read(pid_t pid) {
+ int nsfd;
+ DIR *dir;
+ struct dirent *ptr;
+ struct stat dev_stat;
+ struct devinfo *head = NULL, *end;
+ char fpath[261], dev_name[101];
+ pid_t child_pid;
+ int mypipe[2];
+ int dev_num;
+ FILE *stream;
+
+ if (pipe(mypipe) < 0) {
+ perror("Error creating pipe");
+ return head;
+ }
+
+ child_pid = fork();
+ if (child_pid < 0) {
+ close(mypipe[0]);
+ close(mypipe[1]);
+ perror("Error forking child");
+ return head;
+ }
+ if (child_pid == 0) {
+ /* Disallow signal reception in child process */
+ sigset_t oldset;
+ sigset_t newset;
+ sigemptyset(&newset);
+ sigaddset(&newset, SIGTERM);
+ sigaddset(&newset, SIGINT);
+ sigaddset(&newset, SIGHUP);
+ sigaddset(&newset, SIGQUIT);
+ sigprocmask(SIG_BLOCK,&newset,&oldset);
+
+ close(mypipe[0]);
+ stream = fdopen(mypipe[1], "w");
+ if (stream == NULL) {
+ lxcfs_error("Error opening pipe for writing: %s\n", strerror(errno));
+ goto child_out;
+ }
+ nsfd = getns(pid, "mnt");
+ if (nsfd < 0) {
+ lxcfs_error("Error getting mnt ns: %s\n", strerror(errno));
+ goto child_out;
+ }
+ if (setns(nsfd, 0) < 0) {
+ lxcfs_error("Error setting mnt ns: %s\n", strerror(errno));
+ goto child_out;
+ }
+ dir = opendir("/dev");
+ if (dir == NULL) {
+ lxcfs_error("Error opening dir /dev: %s\n", strerror(errno));
+ goto child_out;
+ }
+ while ((ptr = readdir(dir)) != NULL) {
+ if (ptr->d_type != DT_BLK && ptr->d_type != DT_CHR) {
+ continue;
+ }
+ memset(fpath, 0, sizeof(fpath));
+ snprintf(fpath, 261, "/dev/%s", ptr->d_name);
+ stat(fpath, &dev_stat);
+ fprintf(stream, "%s %ld ", ptr->d_name, dev_stat.st_rdev);
+ fflush(stream);
+ }
+ closedir(dir);
+ stat("/", &dev_stat);
+ dev_num = dev_stat.st_dev;
+ fprintf(stream, "sda %d end 0 ", dev_num);
+ fflush(stream);
+child_out:
+ fclose(stream);
+ _exit(EXIT_SUCCESS);
+ }
+
+ close(mypipe[1]);
+ stream = fdopen(mypipe[0], "r");
+ if (stream == NULL) {
+ lxcfs_error("Error opening pipe for reading: %s\n", strerror(errno));
+ goto err;
+ }
+ wait_for_pid(child_pid);
+ child_pid = 0;
+ memset(dev_name, 0, sizeof(dev_name));
+ while (fscanf(stream, "%100s%d", dev_name, &dev_num) == 2) {
+ if (dev_num == 0) {
+ break;
+ }
+ if (head == NULL) {
+ do {
+ head = (struct devinfo*)malloc(sizeof(struct devinfo));
+ } while (!head);
+ end = head;
+ } else {
+ do {
+ end->next = (struct devinfo*)malloc(sizeof(struct devinfo));
+ } while (!end->next);
+ end = end->next;
+ }
+ end->next = NULL;
+ end->name = must_copy_string(dev_name);
+ end->major = major(dev_num);
+ end->minor = minor(dev_num);
+ memset(dev_name, 0, sizeof(dev_name));
+ }
+err:
+ if (stream)
+ fclose(stream);
+ if (child_pid > 0)
+ wait_for_pid(child_pid);
+ return head;
+}
+
+void free_devinfo_list(struct devinfo *ptr)
+{
+ struct devinfo *tmp;
+ while (ptr != NULL) {
+ tmp = ptr;
+ ptr = ptr->next;
+ free(tmp->name);
+ tmp->name = NULL;
+ free(tmp);
+ tmp = NULL;
+ }
+}
+
struct lxcfs_diskstats {
unsigned int major; /* 1 - major number */
unsigned int minor; /* 2 - minor mumber */
@@ -505,6 +666,7 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset,
__do_fclose FILE *f = NULL;
struct fuse_context *fc = fuse_get_context();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
+ struct devinfo *container_devinfo = NULL, *ptr = NULL;
struct lxcfs_diskstats stats = {};
/* helper fields */
uint64_t read_service_time, write_service_time, discard_service_time, read_wait_time,
@@ -574,14 +736,26 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset,
if (!f)
return 0;
+ lock_mutex(&container_dev_mutex);
+ container_devinfo = container_dev_read(initpid);
+ unlock_mutex(&container_dev_mutex);
+
while (getline(&line, &linelen, f) != -1) {
ssize_t l;
char lbuf[256];
+ memset(stats.dev_name, 0, sizeof(stats.dev_name));
i = sscanf(line, "%u %u %71s", &stats.major, &stats.minor, stats.dev_name);
if (i != 3)
continue;
+ for (ptr = container_devinfo; ptr != NULL; ptr = ptr->next) {
+ if (stats.major == ptr->major && stats.minor == ptr->minor) {
+ snprintf(stats.dev_name, sizeof(stats.dev_name), "%s", ptr->name);
+ stats.dev_name[71] = '\0';
+ }
+ }
+
get_blkio_io_value(io_serviced_str, stats.major, stats.minor, "Read", &stats.read);
get_blkio_io_value(io_serviced_str, stats.major, stats.minor, "Write", &stats.write);
get_blkio_io_value(io_serviced_str, stats.major, stats.minor, "Discard", &stats.discard);
@@ -672,10 +846,14 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset,
}
l = snprintf(cache, cache_size, "%s", lbuf);
- if (l < 0)
+ if (l < 0) {
+ free_devinfo_list(container_devinfo);
return log_error(0, "Failed to write cache");
- if ((size_t)l >= cache_size)
+ }
+ if ((size_t)l >= cache_size) {
+ free_devinfo_list(container_devinfo);
return log_error(0, "Write to cache was truncated");
+ }
cache += l;
cache_size -= l;
@@ -687,6 +865,7 @@ static int proc_diskstats_read(char *buf, size_t size, off_t offset,
if (total_len > size)
total_len = size;
memcpy(buf, d->buf, total_len);
+ free_devinfo_list(container_devinfo);
return total_len;
}
--
2.41.0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzx065891/lxcfs.git
[email protected]:wuzx065891/lxcfs.git
wuzx065891
lxcfs
lxcfs
master

搜索帮助