uname系统调用及示例

uname - 获取系统信息

函数介绍

uname系统调用用于获取当前Linux系统的详细信息,包括内核名称、版本、硬件架构等。这些信息对于系统管理和程序兼容性检查非常有用。

函数原型

1
2
3
4
#include <sys/utsname.h>

int uname(struct utsname *buf);

功能

获取系统标识信息,包括内核名称、网络节点名、内核版本、硬件架构等。

参数

  • struct utsname *buf: 指向utsname结构体的指针,用于存储系统信息struct utsname { char sysname[]; // 系统名称 (如 “Linux”) char nodename[]; // 网络节点名 (主机名) char release[]; // 内核发行版本 char version[]; // 内核版本 char machine[]; // 硬件架构 char domainname[]; // 域名 (NIS/YP) };

返回值

  • 成功时返回0

  • 失败时返回-1,并设置errno

相似函数

  • sysinfo(): 获取系统统计信息

  • 命令行uname工具

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <errno.h>
#include <string.h>

int main() {
struct utsname system_info;

printf("=== Uname函数示例 ===\n");

// 调用uname获取系统信息
if (uname(&system_info) == -1) {
perror("uname调用失败");
exit(EXIT_FAILURE);
}

// 打印系统信息
printf("系统名称 (sysname): %s\n", system_info.sysname);
printf("网络节点名 (nodename): %s\n", system_info.nodename);
printf("内核发行版本 (release): %s\n", system_info.release);
printf("内核版本 (version): %s\n", system_info.version);
printf("硬件架构 (machine): %s\n", system_info.machine);

#ifdef _GNU_SOURCE
printf("域名 (domainname): %s\n", system_info.domainname);
#endif

// 示例应用:根据系统类型执行不同操作
printf("\n=== 实际应用示例 ===\n");

// 检查操作系统类型
if (strcmp(system_info.sysname, "Linux") == 0) {
printf("检测到Linux系统\n");
} else {
printf("检测到其他系统: %s\n", system_info.sysname);
}

// 检查硬件架构
if (strcmp(system_info.machine, "x86_64") == 0) {
printf("运行在64位x86架构上\n");
} else if (strcmp(system_info.machine, "aarch64") == 0) {
printf("运行在64位ARM架构上\n");
} else {
printf("运行在%s架构上\n", system_info.machine);
}

// 检查内核版本(简单比较)
printf("内核版本信息: %s\n", system_info.release);

// 解析版本号示例
int major, minor, patch;
if (sscanf(system_info.release, "%d.%d.%d", &major, &minor, &patch) == 3) {
printf("解析的内核版本: %d.%d.%d\n", major, minor, patch);

// 根据内核版本做兼容性检查
if (major >= 4) {
printf("内核版本 >= 4.0,支持较新特性\n");
} else {
printf("内核版本较低,可能需要特殊处理\n");
}
}

// 获取主机名
printf("主机名: %s\n", system_info.nodename);

return 0;
}

uname函数详解

  1. 函数介绍

uname函数是Linux系统中用于获取系统信息的标准函数,它的名字来源于”Unix name”。这个函数就像系统的”身份证”一样,能够提供关于当前运行系统的详细信息,包括操作系统名称、版本、硬件架构等。

data-ad-format="fluid" data-ad-layout-key="-7k+ex-4a-9w+4a">

可以把uname想象成一个”系统信息查询员”,当你需要了解当前系统的基本信息时,它能够快速提供准确的答案。无论是在程序中需要根据系统类型执行不同逻辑,还是在调试时需要确认系统环境,uname都是一个非常实用的工具。

使用场景:

  • 程序的系统兼容性检查

  • 系统信息显示和日志记录

  • 根据系统类型执行特定代码

  • 系统管理和监控工具

  • 软件安装程序的环境检测

  1. 函数原型
1
2
3
4
#include <sys/utsname.h>

int uname(struct utsname *buf);

  1. 功能

uname函数的主要功能是获取当前系统的标识信息。它填充一个utsname结构体,其中包含以下系统信息:

  • 操作系统名称

  • 网络节点名称(主机名)

  • 操作系统发行版本

  • 操作系统版本

  • 硬件架构类型

  1. 参数

buf: 系统信息缓冲区

  • 类型:struct utsname*

  • 含义:指向utsname结构体的指针,用于存储系统信息

该结构体包含以下字段:

  • sysname[]: 操作系统名称

  • nodename[]: 网络节点名称(主机名)

  • release[]: 操作系统发行版本

  • version[]: 操作系统版本

  • machine[]: 硬件架构类型

  • domainname[]: 网络域名(某些系统支持)

  1. 返回值
  • 成功: 返回0

失败: 返回-1,并设置errno错误码

  • EFAULT:buf参数指向无效内存地址

  • EINVAL:参数无效(理论上不会发生)

  1. 相似函数或关联函数
  • gethostname(): 获取主机名

  • sysconf(): 获取系统配置信息

  • getdomainname(): 获取网络域名

  • uname命令: 命令行下的uname工具

  • /proc/version: 系统版本信息文件

  • system(): 执行系统命令

  1. 示例代码

示例1:基础uname使用 - 获取系统信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <errno.h>

int main() {
struct utsname system_info;

printf("=== 基础uname使用示例 ===\n");

// 调用uname函数获取系统信息
if (uname(&system_info) == -1) {
perror("uname调用失败");
exit(EXIT_FAILURE);
}

// 显示系统信息
printf("系统信息:\n");
printf("----------------------------\n");
printf("操作系统名称: %s\n", system_info.sysname);
printf("网络节点名: %s\n", system_info.nodename);
printf("内核发行版: %s\n", system_info.release);
printf("内核版本: %s\n", system_info.version);
printf("硬件架构: %s\n", system_info.machine);

// 如果支持域名信息
#ifdef _GNU_SOURCE
printf("网络域名: %s\n", system_info.domainname);
#endif

printf("----------------------------\n");

// 分析系统类型
printf("\n系统类型分析:\n");
if (strcmp(system_info.sysname, "Linux") == 0) {
printf("✓ 这是一个Linux系统\n");
} else if (strcmp(system_info.sysname, "Darwin") == 0) {
printf("✓ 这是一个macOS系统\n");
} else {
printf("? 这是一个%s系统\n", system_info.sysname);
}

// 分析硬件架构
printf("硬件架构分析:\n");
if (strcmp(system_info.machine, "x86_64") == 0) {
printf("✓ 64位x86架构\n");
} else if (strcmp(system_info.machine, "i386") == 0 ||
strcmp(system_info.machine, "i686") == 0) {
printf("✓ 32位x86架构\n");
} else if (strncmp(system_info.machine, "arm", 3) == 0) {
printf("✓ ARM架构\n");
} else if (strncmp(system_info.machine, "aarch64", 7) == 0) {
printf("✓ 64位ARM架构\n");
} else {
printf("? %s架构\n", system_info.machine);
}

return 0;
}

示例2:详细的系统信息分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <ctype.h>

// 判断是否为Linux系统
int is_linux_system(const char* sysname) {
return (strcmp(sysname, "Linux") == 0);
}

// 判断是否为64位系统
int is_64bit_system(const char* machine) {
return (strcmp(machine, "x86_64") == 0 ||
strcmp(machine, "aarch64") == 0 ||
strcmp(machine, "ppc64") == 0 ||
strcmp(machine, "s390x") == 0 ||
strncmp(machine, "mips64", 6) == 0);
}

// 判断是否为ARM架构
int is_arm_architecture(const char* machine) {
return (strncmp(machine, "arm", 3) == 0 ||
strncmp(machine, "aarch64", 7) == 0);
}

// 分析内核版本
void analyze_kernel_version(const char* release) {
printf("内核版本分析:\n");

// Linux内核版本通常是 x.y.z 格式
int major, minor, patch;
if (sscanf(release, "%d.%d.%d", &major, &minor, &patch) == 3) {
printf(" 主版本号: %d\n", major);
printf(" 次版本号: %d\n", minor);
printf(" 修订版本: %d\n", patch);

// 根据主版本号判断内核新旧
if (major >= 5) {
printf(" ✓ 现代Linux内核 (5.x 或更新)\n");
} else if (major == 4) {
printf(" ✓ 较新的Linux内核 (4.x)\n");
} else if (major == 3) {
printf(" ⚠ 较旧的Linux内核 (3.x)\n");
} else {
printf(" ⚠ 非常旧的Linux内核 (%d.x)\n", major);
}
} else {
printf(" 无法解析版本格式: %s\n", release);
}
}

// 分析主机名
void analyze_hostname(const char* nodename) {
printf("主机名分析:\n");
printf(" 主机名: %s\n", nodename);

// 检查主机名长度
size_t len = strlen(nodename);
printf(" 长度: %zu 字符\n", len);

// 检查是否包含特殊字符
int has_special = 0;
for (size_t i = 0; i < len; i++) {
if (!isalnum(nodename&#91;i]) && nodename&#91;i] != '-' && nodename&#91;i] != '.') {
has_special = 1;
break;
}
}

if (has_special) {
printf(" ⚠ 主机名包含特殊字符\n");
} else {
printf(" ✓ 主机名格式规范\n");
}
}

// 显示系统兼容性信息
void show_compatibility_info(const struct utsname* info) {
printf("系统兼容性信息:\n");

// 可执行文件格式
if (is_64bit_system(info->machine)) {
printf(" 可执行格式: 64位 ELF\n");
} else {
printf(" 可执行格式: 32位 ELF\n");
}

// 库兼容性
if (is_linux_system(info->sysname)) {
printf(" 系统调用: Linux ABI\n");
}

// 字节序信息(通过架构推断)
if (strcmp(info->machine, "x86_64") == 0 ||
strcmp(info->machine, "i386") == 0) {
printf(" 字节序: 小端序 (Little Endian)\n");
}
}

int main() {
struct utsname system_info;

printf("=== 详细系统信息分析 ===\n\n");

// 获取系统信息
if (uname(&system_info) == -1) {
perror("uname调用失败");
exit(EXIT_FAILURE);
}

// 基本信息显示
printf("1. 基本系统信息:\n");
printf(" 操作系统: %s\n", system_info.sysname);
printf(" 主机名: %s\n", system_info.nodename);
printf(" 内核版本: %s\n", system_info.release);
printf(" 构建版本: %s\n", system_info.version);
printf(" 硬件架构: %s\n", system_info.machine);

// 详细分析
printf("\n2. 详细分析:\n");
analyze_hostname(system_info.nodename);
printf("\n");
analyze_kernel_version(system_info.release);
printf("\n");
show_compatibility_info(&system_info);

// 系统分类
printf("\n3. 系统分类:\n");
if (is_linux_system(system_info.sysname)) {
printf(" ✓ Linux系统家族\n");

// 进一步分类Linux发行版(基于内核版本等信息)
if (strstr(system_info.version, "Ubuntu")) {
printf(" ✓ 可能是Ubuntu发行版\n");
} else if (strstr(system_info.version, "Debian")) {
printf(" ✓ 可能是Debian发行版\n");
} else if (strstr(system_info.version, "CentOS") ||
strstr(system_info.version, "Red Hat")) {
printf(" ✓ 可能是Red Hat系列发行版\n");
} else {
printf(" ✓ 其他Linux发行版\n");
}
}

if (is_64bit_system(system_info.machine)) {
printf(" ✓ 64位系统\n");
} else {
printf(" ✓ 32位系统\n");
}

if (is_arm_architecture(system_info.machine)) {
printf(" ✓ ARM架构系统\n");
}

// 生成系统指纹(用于唯一标识)
printf("\n4. 系统指纹:\n");
printf(" 指纹字符串: %s-%s-%s\n",
system_info.sysname,
system_info.release,
system_info.machine);

// 应用场景示例
printf("\n5. 应用场景适配:\n");

// 根据系统类型决定编译选项
if (is_linux_system(system_info.sysname)) {
printf(" 编译建议: 使用Linux特定优化\n");
}

// 根据架构决定二进制分发
if (is_64bit_system(system_info.machine)) {
printf(" 分发建议: 提供64位版本\n");
} else {
printf(" 分发建议: 提供32位版本\n");
}

if (is_arm_architecture(system_info.machine)) {
printf(" 优化建议: 针对ARM架构优化\n");
}

return 0;
}

示例3:系统信息比较和兼容性检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <time.h>

// 系统信息结构体
typedef struct {
struct utsname info;
time_t timestamp;
} system_snapshot_t;

// 保存系统快照
int save_system_snapshot(system_snapshot_t* snapshot) {
if (uname(&snapshot->info) == -1) {
return -1;
}
snapshot->timestamp = time(NULL);
return 0;
}

// 比较两个系统快照
void compare_system_snapshots(const system_snapshot_t* snap1,
const system_snapshot_t* snap2) {
printf("=== 系统信息比较 ===\n");
printf("快照1时间: %s", ctime(&snap1->timestamp));
printf("快照2时间: %s", ctime(&snap2->timestamp));

printf("比较结果:\n");

// 比较操作系统名称
if (strcmp(snap1->info.sysname, snap2->info.sysname) == 0) {
printf("✓ 操作系统名称相同: %s\n", snap1->info.sysname);
} else {
printf("✗ 操作系统名称不同: %s vs %s\n",
snap1->info.sysname, snap2->info.sysname);
}

// 比较主机名
if (strcmp(snap1->info.nodename, snap2->info.nodename) == 0) {
printf("✓ 主机名相同: %s\n", snap1->info.nodename);
} else {
printf("⚠ 主机名不同: %s vs %s\n",
snap1->info.nodename, snap2->info.nodename);
}

// 比较内核版本
if (strcmp(snap1->info.release, snap2->info.release) == 0) {
printf("✓ 内核版本相同: %s\n", snap1->info.release);
} else {
printf("⚠ 内核版本不同: %s vs %s\n",
snap1->info.release, snap2->info.release);
}

// 比较硬件架构
if (strcmp(snap1->info.machine, snap2->info.machine) == 0) {
printf("✓ 硬件架构相同: %s\n", snap1->info.machine);
} else {
printf("✗ 硬件架构不同: %s vs %s\n",
snap1->info.machine, snap2->info.machine);
}
}

// 检查系统兼容性
int check_system_compatibility(const struct utsname* info) {
printf("=== 系统兼容性检查 ===\n");
int compatible = 1;

// 检查操作系统
if (strcmp(info->sysname, "Linux") != 0) {
printf("✗ 不支持的操作系统: %s\n", info->sysname);
compatible = 0;
} else {
printf("✓ 支持的操作系统: %s\n", info->sysname);
}

// 检查内核版本(假设需要3.0以上)
int major, minor;
if (sscanf(info->release, "%d.%d", &major, &minor) >= 2) {
if (major >= 3) {
printf("✓ 支持的内核版本: %s\n", info->release);
} else {
printf("✗ 内核版本过低: %s (需要3.0+)\n", info->release);
compatible = 0;
}
} else {
printf("? 无法确定内核版本格式\n");
}

// 检查硬件架构(假设支持x86_64和ARM64)
if (strcmp(info->machine, "x86_64") == 0 ||
strcmp(info->machine, "aarch64") == 0) {
printf("✓ 支持的硬件架构: %s\n", info->machine);
} else {
printf("⚠ 可能不支持的硬件架构: %s\n", info->machine);
// 这里可以根据需要决定是否标记为不兼容
}

return compatible;
}

// 生成系统报告
void generate_system_report(const struct utsname* info) {
printf("\n=== 系统详细报告 ===\n");

// 基本信息
printf("【基本信息】\n");
printf(" 操作系统: %s\n", info->sysname);
printf(" 主机名称: %s\n", info->nodename);
printf(" 硬件架构: %s\n", info->machine);

// 内核信息
printf("【内核信息】\n");
printf(" 发行版本: %s\n", info->release);
printf(" 构建信息: %s\n", info->version);

// 系统特征
printf("【系统特征】\n");

// 架构特征
if (strcmp(info->machine, "x86_64") == 0) {
printf(" 架构类型: 64位Intel/AMD x86\n");
printf(" 指令集: 支持SSE, AVX等扩展指令\n");
} else if (strcmp(info->machine, "aarch64") == 0) {
printf(" 架构类型: 64位ARM\n");
printf(" 指令集: ARMv8-A架构\n");
} else if (strncmp(info->machine, "arm", 3) == 0) {
printf(" 架构类型: 32位ARM\n");
printf(" 指令集: ARM架构\n");
} else {
printf(" 架构类型: %s\n", info->machine);
}

// 操作系统特征
if (strcmp(info->sysname, "Linux") == 0) {
printf(" 系统类型: 类Unix操作系统\n");
printf(" 系统调用: POSIX兼容\n");
printf(" 文件系统: 支持ext4, xfs, btrfs等\n");
}

// 版本特征
printf("【版本特征】\n");
if (strstr(info->version, "Ubuntu")) {
printf(" 发行版: Ubuntu系列\n");
} else if (strstr(info->version, "Debian")) {
printf(" 发行版: Debian系列\n");
} else if (strstr(info->version, "CentOS") ||
strstr(info->version, "Red Hat")) {
printf(" 发行版: Red Hat系列\n");
} else if (strstr(info->version, "SUSE")) {
printf(" 发行版: SUSE系列\n");
}

// 安全特征
printf("【安全特征】\n");
printf(" 用户权限: 支持多用户权限管理\n");
printf(" 进程隔离: 支持进程间隔离\n");
printf(" 内存保护: 支持内存保护机制\n");
}

int main() {
struct utsname current_info;
system_snapshot_t snapshot1, snapshot2;

printf("=== 系统信息综合应用示例 ===\n\n");

// 获取当前系统信息
if (uname(&current_info) == -1) {
perror("uname调用失败");
exit(EXIT_FAILURE);
}

// 保存快照
if (save_system_snapshot(&snapshot1) == -1) {
perror("保存快照1失败");
exit(EXIT_FAILURE);
}

printf("1. 当前系统信息:\n");
printf(" 操作系统: %s\n", current_info.sysname);
printf(" 内核版本: %s\n", current_info.release);
printf(" 硬件架构: %s\n", current_info.machine);

// 模拟系统变化(实际中可能需要等待系统更新)
sleep(1);

if (save_system_snapshot(&snapshot2) == -1) {
perror("保存快照2失败");
exit(EXIT_FAILURE);
}

// 比较快照
printf("\n2. 系统快照比较:\n");
compare_system_snapshots(&snapshot1, &snapshot2);

// 兼容性检查
printf("\n3. 系统兼容性检查:\n");
int compatible = check_system_compatibility(&current_info);
if (compatible) {
printf("✓ 系统兼容性检查通过\n");
} else {
printf("✗ 系统兼容性检查未通过\n");
}

// 生成详细报告
printf("\n4. 生成系统详细报告:\n");
generate_system_report(&current_info);

// 应用场景演示
printf("\n5. 应用场景演示:\n");

// 根据系统信息选择不同的处理逻辑
if (strcmp(current_info.sysname, "Linux") == 0) {
printf(" 执行Linux特定代码路径\n");

// 根据架构选择优化
if (strcmp(current_info.machine, "x86_64") == 0) {
printf(" 启用x86_64优化选项\n");
} else if (strcmp(current_info.machine, "aarch64") == 0) {
printf(" 启用ARM64优化选项\n");
}

// 根据内核版本启用特性
int major, minor;
if (sscanf(current_info.release, "%d.%d", &major, &minor) >= 2) {
if (major >= 4) {
printf(" 启用现代内核特性\n");
}
}
}

printf("\n=== 演示完成 ===\n");

return 0;
}

示例4:跨平台系统信息工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <string.h>
#include <time.h>

#ifdef __APPLE__
#include <sys/sysctl.h>
#endif

// 系统信息结构体
typedef struct {
char os_name&#91;256];
char hostname&#91;256];
char kernel_version&#91;256];
char architecture&#91;256];
char distribution&#91;256];
long uptime_seconds;
int cpu_count;
} extended_system_info_t;

// 获取扩展系统信息
int get_extended_system_info(extended_system_info_t* info) {
struct utsname basic_info;

// 获取基本系统信息
if (uname(&basic_info) == -1) {
return -1;
}

// 复制基本信息
strncpy(info->os_name, basic_info.sysname, sizeof(info->os_name) - 1);
strncpy(info->hostname, basic_info.nodename, sizeof(info->hostname) - 1);
strncpy(info->kernel_version, basic_info.release, sizeof(info->kernel_version) - 1);
strncpy(info->architecture, basic_info.machine, sizeof(info->architecture) - 1);

// 初始化其他字段
strcpy(info->distribution, "Unknown");
info->uptime_seconds = 0;
info->cpu_count = 1;

// 根据不同系统获取额外信息
if (strcmp(basic_info.sysname, "Linux") == 0) {
// Linux系统特有信息

// 尝试读取发行版信息
FILE* fp = fopen("/etc/os-release", "r");
if (fp) {
char line&#91;256];
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "PRETTY_NAME=", 12) == 0) {
char* start = strchr(line, '"');
if (start) {
char* end = strchr(start + 1, '"');
if (end) {
*end = '\0';
strncpy(info->distribution, start + 1, sizeof(info->distribution) - 1);
break;
}
}
}
}
fclose(fp);
}

// 获取系统运行时间
fp = fopen("/proc/uptime", "r");
if (fp) {
double uptime;
if (fscanf(fp, "%lf", &uptime) == 1) {
info->uptime_seconds = (long)uptime;
}
fclose(fp);
}

// 获取CPU数量
fp = fopen("/proc/cpuinfo", "r");
if (fp) {
char line&#91;256];
int cpu_count = 0;
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "processor", 9) == 0) {
cpu_count++;
}
}
if (cpu_count > 0) {
info->cpu_count = cpu_count;
}
fclose(fp);
}

}
#ifdef __APPLE__
else if (strcmp(basic_info.sysname, "Darwin") == 0) {
// macOS系统特有信息
strcpy(info->distribution, "macOS");

// 获取CPU数量
int mib&#91;2] = {CTL_HW, HW_NCPU};
size_t len = sizeof(info->cpu_count);
sysctl(mib, 2, &info->cpu_count, &len, NULL, 0);
}
#endif

return 0;
}

// 格式化显示时间
void format_uptime(long seconds, char* buffer, size_t buffer_size) {
long days = seconds / 86400;
long hours = (seconds % 86400) / 3600;
long minutes = (seconds % 3600) / 60;

if (days > 0) {
snprintf(buffer, buffer_size, "%ld天 %ld小时 %ld分钟", days, hours, minutes);
} else if (hours > 0) {
snprintf(buffer, buffer_size, "%ld小时 %ld分钟", hours, minutes);
} else {
snprintf(buffer, buffer_size, "%ld分钟", minutes);
}
}

// 显示系统信息
void display_system_info(const extended_system_info_t* info) {
printf("╔══════════════════════════════════════════════════════════════╗\n");
printf("║ 系统信息报告 ║\n");
printf("╠══════════════════════════════════════════════════════════════╣\n");
printf("║ 操作系统: %-48s ║\n", info->os_name);
printf("║ 主机名称: %-48s ║\n", info->hostname);
printf("║ 内核版本: %-48s ║\n", info->kernel_version);
printf("║ 硬件架构: %-48s ║\n", info->architecture);
printf("║ 发行版本: %-48s ║\n", info->distribution);

// 显示系统运行时间
if (info->uptime_seconds > 0) {
char uptime_str&#91;64];
format_uptime(info->uptime_seconds, uptime_str, sizeof(uptime_str));
printf("║ 运行时间: %-48s ║\n", uptime_str);
}

printf("║ CPU核心数: %-47d ║\n", info->cpu_count);
printf("╚══════════════════════════════════════════════════════════════╝\n");
}

// 生成JSON格式的系统信息
void generate_json_info(const extended_system_info_t* info) {
printf("\nJSON格式系统信息:\n");
printf("{\n");
printf(" \"os_name\": \"%s\",\n", info->os_name);
printf(" \"hostname\": \"%s\",\n", info->hostname);
printf(" \"kernel_version\": \"%s\",\n", info->kernel_version);
printf(" \"architecture\": \"%s\",\n", info->architecture);
printf(" \"distribution\": \"%s\",\n", info->distribution);
printf(" \"uptime_seconds\": %ld,\n", info->uptime_seconds);
printf(" \"cpu_count\": %d\n", info->cpu_count);
printf("}\n");
}

// 系统健康检查
void system_health_check(const extended_system_info_t* info) {
printf("\n系统健康检查:\n");
printf("----------------\n");

// 检查系统类型
if (strcmp(info->os_name, "Linux") == 0) {
printf("✓ Linux系统环境\n");
} else {
printf("ℹ 非Linux系统: %s\n", info->os_name);
}

// 检查架构
if (strcmp(info->architecture, "x86_64") == 0) {
printf("✓ 64位x86架构\n");
} else if (strcmp(info->architecture, "aarch64") == 0) {
printf("✓ 64位ARM架构\n");
} else {
printf("ℹ 其他架构: %s\n", info->architecture);
}

// 检查CPU数量
if (info->cpu_count >= 4) {
printf("✓ 多核心系统 (%d核心)\n", info->cpu_count);
} else if (info->cpu_count >= 2) {
printf("✓ 双核心系统\n");
} else {
printf("ℹ 单核心系统\n");
}

// 检查运行时间
if (info->uptime_seconds > 0) {
if (info->uptime_seconds > 86400) { // 超过一天
printf("✓ 系统稳定运行中\n");
} else {
printf("ℹ 系统运行时间较短\n");
}
}
}

int main() {
extended_system_info_t sys_info;

printf("=== 跨平台系统信息工具 ===\n\n");

// 获取扩展系统信息
if (get_extended_system_info(&sys_info) == -1) {
perror("获取系统信息失败");
exit(EXIT_FAILURE);
}

// 显示系统信息
display_system_info(&sys_info);

// 系统健康检查
system_health_check(&sys_info);

// 生成JSON格式信息
generate_json_info(&sys_info);

// 应用场景示例
printf("\n应用场景适配:\n");
printf("----------------\n");

// 根据系统类型选择不同的处理
if (strcmp(sys_info.os_name, "Linux") == 0) {
printf("→ 启用Linux优化模式\n");

// 根据发行版调整配置
if (strstr(sys_info.distribution, "Ubuntu")) {
printf("→ 应用Ubuntu特定配置\n");
} else if (strstr(sys_info.distribution, "CentOS") ||
strstr(sys_info.distribution, "Red Hat")) {
printf("→ 应用Red Hat特定配置\n");
}

} else if (strcmp(sys_info.os_name, "Darwin") == 0) {
printf("→ 启用macOS优化模式\n");
} else {
printf("→ 使用通用配置\n");
}

// 根据CPU数量调整并行度
printf("→ 建议并行任务数: %d\n", sys_info.cpu_count);

printf("\n=== 工具执行完成 ===\n");

return 0;
}

编译和运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 编译示例1
gcc -o uname_example1 uname_example1.c
./uname_example1

# 编译示例2
gcc -o uname_example2 uname_example2.c
./uname_example2

# 编译示例3
gcc -o uname_example3 uname_example3.c
./uname_example3

# 编译示例4
gcc -o uname_example4 uname_example4.c
./uname_example4

重要注意事项

结构体大小: utsname结构体的大小在不同系统中可能不同

域名支持: domainname字段不是所有系统都支持

权限要求: 通常不需要特殊权限即可调用

线程安全: uname函数是线程安全的

错误处理: 失败时主要返回EFAULT错误

字符编码: 返回的字符串通常是ASCII编码

缓冲区: 传入的结构体必须由调用者分配

常见应用场景

系统兼容性检测: 程序启动时检查运行环境

日志记录: 在日志中记录系统环境信息

调试信息: 帮助开发者了解运行环境

配置管理: 根据系统类型选择不同配置

性能优化: 根据硬件架构选择优化策略

通过这些示例,你可以理解uname函数在获取系统信息方面的强大功能,它为程序提供了识别和适应不同运行环境的能力。

data-ad-format="auto" data-full-width-responsive="true">