1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21
22#include "qemu.h"
23#include "user-internals.h"
24
25#include "uname.h"
26
27
28
29
30
31
32const char *cpu_to_uname_machine(void *cpu_env)
33{
34#if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
35
36
37
38
39
40
41
42
43
44#ifdef TARGET_WORDS_BIGENDIAN
45#define utsname_suffix "b"
46#else
47#define utsname_suffix "l"
48#endif
49 if (arm_feature(cpu_env, ARM_FEATURE_V7))
50 return "armv7" utsname_suffix;
51 if (arm_feature(cpu_env, ARM_FEATURE_V6))
52 return "armv6" utsname_suffix;
53
54
55 return "armv5te" utsname_suffix;
56#elif defined(TARGET_I386) && !defined(TARGET_X86_64)
57
58 CPUState *cpu = env_cpu((CPUX86State *)cpu_env);
59 int family = object_property_get_int(OBJECT(cpu), "family", NULL);
60 if (family == 4) {
61 return "i486";
62 }
63 if (family == 5) {
64 return "i586";
65 }
66 return "i686";
67#else
68
69 return UNAME_MACHINE;
70#endif
71}
72
73
74#define COPY_UTSNAME_FIELD(dest, src) \
75 do { \
76 memcpy((dest), (src), MIN(sizeof(src), sizeof(dest))); \
77 (dest)[sizeof(dest) - 1] = '\0'; \
78 } while (0)
79
80int sys_uname(struct new_utsname *buf)
81{
82 struct utsname uts_buf;
83
84 if (uname(&uts_buf) < 0)
85 return (-1);
86
87
88
89
90
91
92
93 memset(buf, 0, sizeof(*buf));
94 COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
95 COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
96 COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
97 COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
98 COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
99#ifdef _GNU_SOURCE
100 COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
101#endif
102 return (0);
103
104#undef COPY_UTSNAME_FIELD
105}
106
107static int relstr_to_int(const char *s)
108{
109
110
111
112 int i, n, tmp;
113
114 tmp = 0;
115 for (i = 0; i < 3; i++) {
116 n = 0;
117 while (*s >= '0' && *s <= '9') {
118 n *= 10;
119 n += *s - '0';
120 s++;
121 }
122 tmp = (tmp << 8) + n;
123 if (*s == '.') {
124 s++;
125 }
126 }
127 return tmp;
128}
129
130int get_osversion(void)
131{
132 static int osversion;
133 struct new_utsname buf;
134 const char *s;
135
136 if (osversion)
137 return osversion;
138 if (qemu_uname_release && *qemu_uname_release) {
139 s = qemu_uname_release;
140 } else {
141 if (sys_uname(&buf))
142 return 0;
143 s = buf.release;
144 }
145 osversion = relstr_to_int(s);
146 return osversion;
147}
148
149void init_qemu_uname_release(void)
150{
151
152
153
154
155
156 struct new_utsname buf;
157
158 if (qemu_uname_release && *qemu_uname_release) {
159 return;
160 }
161
162 if (sys_uname(&buf)) {
163 return;
164 }
165
166 if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
167 qemu_uname_release = UNAME_MINIMUM_RELEASE;
168 }
169}
170