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