奇妙@代码
2025-07-28 22:17:48
发布于:浙江
几段AI写的小代码,用于判断程序的运行环境
1.判断是否在容器中
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
// 检查是否存在.dockerenv文件
bool is_docker_env() {
std::ifstream dockerenv("/.dockerenv");
return dockerenv.good();
}
// 检查cgroup信息是否包含容器特征
bool is_container_cgroup() {
std::ifstream cgroup("/proc/self/cgroup");
if (!cgroup) return false;
std::string line;
while (std::getline(cgroup, line)) {
if (line.find("docker") != std::string::npos ||
line.find("lxc") != std::string::npos ||
line.find("kubepods") != std::string::npos ||
line.find("containerd") != std::string::npos) {
return true;
}
}
return false;
}
// 检查/proc/1/sched中的进程名是否包含容器特征
bool is_container_process_name() {
std::ifstream sched("/proc/1/sched");
if (!sched) return false;
std::string line;
if (std::getline(sched, line)) {
// 在容器中,init进程通常是容器运行时,而不是系统init
if (line.find("init") == std::string::npos &&
line.find("systemd") == std::string::npos) {
return true;
}
}
return false;
}
// 检查是否在Kubernetes环境中
bool is_kubernetes_env() {
const char* k8s_vars[] = {
"KUBERNETES_SERVICE_HOST",
"KUBERNETES_SERVICE_PORT",
nullptr
};
for (int i = 0; k8s_vars[i] != nullptr; i++) {
if (getenv(k8s_vars[i]) != nullptr) {
return true;
}
}
return false;
}
int main() {
bool in_container = false;
std::string container_type = "未知";
// 检查各种容器特征
if (is_docker_env()) {
in_container = true;
container_type = "Docker";
} else if (is_container_cgroup()) {
in_container = true;
container_type = "容器(CGroup特征)";
} else if (is_container_process_name()) {
in_container = true;
container_type = "容器(进程名特征)";
} else if (is_kubernetes_env()) {
in_container = true;
container_type = "Kubernetes";
}
if (in_container) {
std::cout << "当前进程运行在容器环境中 (" << container_type << ")" << std::endl;
} else {
std::cout << "当前进程运行在宿主机环境中" << std::endl;
}
return 0;
}
2.列出所有进程
#include <iostream>
#include <dirent.h>
#include <cstring>
#include <fstream>
#include <vector>
#include <algorithm>
// 获取进程的命令行信息
std::string get_process_cmdline(pid_t pid) {
std::string path = "/proc/" + std::to_string(pid) + "/cmdline";
std::ifstream cmdline_file(path);
if (!cmdline_file) {
return "[kernel process]";
}
std::string cmdline;
std::getline(cmdline_file, cmdline);
// 替换cmdline中的null字符为空格
for (char &c : cmdline) {
if (c == '\0') {
c = ' ';
}
}
return cmdline.empty() ? "[unknown]" : cmdline;
}
// 列出所有进程
void list_processes() {
DIR *proc_dir = opendir("/proc");
if (!proc_dir) {
std::cerr << "无法打开/proc目录" << std::endl;
return;
}
std::vector<pid_t> pids;
struct dirent *entry;
while ((entry = readdir(proc_dir)) != nullptr) {
// 只处理数字命名的目录(即进程ID)
if (entry->d_type == DT_DIR && isdigit(entry->d_name[0])) {
pid_t pid = atoi(entry->d_name);
pids.push_back(pid);
}
}
closedir(proc_dir);
// 按PID排序
std::sort(pids.begin(), pids.end());
// 打印表头
std::cout << "PID\tCommand\n";
std::cout << "----------------------------\n";
// 打印每个进程的信息
for (pid_t pid : pids) {
std::cout << pid << "\t" << get_process_cmdline(pid) << "\n";
}
}
int main() {
std::cout << "当前运行的进程列表:\n";
list_processes();
return 0;
}
这里空空如也
有帮助,赞一个