1 Star 0 Fork 56

lingjuer/dnf

forked from src-openEuler/dnf 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Add-exception-handling-when-reading-files.patch 3.84 KB
一键复制 编辑 原始数据 按行查看 历史
From b7ab85d675417db3bc2eda499ee1844f8116d51e Mon Sep 17 00:00:00 2001
From: lingjuer <zhangchenglin@kylinos.cn>
Date: Fri, 28 Jul 2023 15:40:17 +0800
Subject: [PATCH] Add exception handling when reading files
---
dnf/cli/utils.py | 92 +++++++++++++++++++++++++-----------------------
1 file changed, 47 insertions(+), 45 deletions(-)
diff --git a/dnf/cli/utils.py b/dnf/cli/utils.py
index 1c3db75..2d3e352 100644
--- a/dnf/cli/utils.py
+++ b/dnf/cli/utils.py
@@ -59,51 +59,53 @@ def seconds_to_ui_time(seconds):
def get_process_info(pid):
"""Return info dict about a process."""
-
- pid = int(pid)
-
- # Maybe true if /proc isn't mounted, or not Linux ... or something.
- if (not os.path.exists("/proc/%d/status" % pid) or
- not os.path.exists("/proc/stat") or
- not os.path.exists("/proc/%d/stat" % pid)):
- return
-
- ps = {}
- with open("/proc/%d/status" % pid) as status_file:
- for line in status_file:
- if line[-1] != '\n':
- continue
- data = line[:-1].split(':\t', 1)
- if len(data) < 2:
- continue
- data[1] = dnf.util.rtrim(data[1], ' kB')
- ps[data[0].strip().lower()] = data[1].strip()
- if 'vmrss' not in ps:
- return
- if 'vmsize' not in ps:
- return
-
- boot_time = None
- with open("/proc/stat") as stat_file:
- for line in stat_file:
- if line.startswith("btime "):
- boot_time = int(line[len("btime "):-1])
- break
- if boot_time is None:
- return
-
- with open('/proc/%d/stat' % pid) as stat_file:
- ps_stat = stat_file.read().split()
- ps['start_time'] = boot_time + jiffies_to_seconds(ps_stat[21])
- ps['state'] = {'R' : _('Running'),
- 'S' : _('Sleeping'),
- 'D' : _('Uninterruptible'),
- 'Z' : _('Zombie'),
- 'T' : _('Traced/Stopped')
- }.get(ps_stat[2], _('Unknown'))
-
- return ps
-
+ try:
+ pid = int(pid)
+
+ # Maybe true if /proc isn't mounted, or not Linux ... or something.
+ if (not os.path.exists("/proc/%d/status" % pid) or
+ not os.path.exists("/proc/stat") or
+ not os.path.exists("/proc/%d/stat" % pid)):
+ return
+
+ ps = {}
+ with open("/proc/%d/status" % pid) as status_file:
+ for line in status_file:
+ if line[-1] != '\n':
+ continue
+ data = line[:-1].split(':\t', 1)
+ if len(data) < 2:
+ continue
+ data[1] = dnf.util.rtrim(data[1], ' kB')
+ ps[data[0].strip().lower()] = data[1].strip()
+ if 'vmrss' not in ps:
+ return
+ if 'vmsize' not in ps:
+ return
+
+ boot_time = None
+ with open("/proc/stat") as stat_file:
+ for line in stat_file:
+ if line.startswith("btime "):
+ boot_time = int(line[len("btime "):-1])
+ break
+ if boot_time is None:
+ return
+
+ with open('/proc/%d/stat' % pid) as stat_file:
+ ps_stat = stat_file.read().split()
+ ps['start_time'] = boot_time + jiffies_to_seconds(ps_stat[21])
+ ps['state'] = {'R' : _('Running'),
+ 'S' : _('Sleeping'),
+ 'D' : _('Uninterruptible'),
+ 'Z' : _('Zombie'),
+ 'T' : _('Traced/Stopped')
+ }.get(ps_stat[2], _('Unknown'))
+
+ return ps
+ except (OSError, ValueError) as e:
+ logger.error("Failed to get process info: %s", e)
+ return None
def show_lock_owner(pid):
"""Output information about process holding a lock."""
--
2.39.1
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lingjuer/dnf.git
git@gitee.com:lingjuer/dnf.git
lingjuer
dnf
dnf
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385