linux-user: move uname functions to uname.c

Make syscall.c slightly smaller by moving uname-related
functions to uname.c.

Signed-off-by: Riku Voipio <riku.voipio@linaro.org>
This commit is contained in:
Riku Voipio 2014-02-19 15:35:35 +02:00
parent 18cb008865
commit 6d30db19ca
3 changed files with 110 additions and 102 deletions

View File

@ -60,7 +60,6 @@ int __clone2(int (*fn)(void *), void *child_stack_base,
#include <sys/statfs.h>
#include <utime.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
//#include <sys/user.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
@ -92,7 +91,6 @@ int __clone2(int (*fn)(void *), void *child_stack_base,
#include <linux/termios.h>
#include <linux/unistd.h>
#include <linux/utsname.h>
#include <linux/cdrom.h>
#include <linux/hdreg.h>
#include <linux/soundcard.h>
@ -287,40 +285,6 @@ static bitmask_transtbl fcntl_flags_tbl[] = {
{ 0, 0, 0, 0 }
};
#define COPY_UTSNAME_FIELD(dest, src) \
do { \
/* __NEW_UTS_LEN doesn't include terminating null */ \
(void) strncpy((dest), (src), __NEW_UTS_LEN); \
(dest)[__NEW_UTS_LEN] = '\0'; \
} while (0)
static int sys_uname(struct new_utsname *buf)
{
struct utsname uts_buf;
if (uname(&uts_buf) < 0)
return (-1);
/*
* Just in case these have some differences, we
* translate utsname to new_utsname (which is the
* struct linux kernel uses).
*/
memset(buf, 0, sizeof(*buf));
COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
#ifdef _GNU_SOURCE
COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
#endif
return (0);
#undef COPY_UTSNAME_FIELD
}
static int sys_getcwd1(char *buf, size_t size)
{
if (getcwd(buf, size) == NULL) {
@ -4983,72 +4947,6 @@ int host_to_target_waitstatus(int status)
return status;
}
static int relstr_to_int(const char *s)
{
/* Convert a uname release string like "2.6.18" to an integer
* of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
*/
int i, n, tmp;
tmp = 0;
for (i = 0; i < 3; i++) {
n = 0;
while (*s >= '0' && *s <= '9') {
n *= 10;
n += *s - '0';
s++;
}
tmp = (tmp << 8) + n;
if (*s == '.') {
s++;
}
}
return tmp;
}
int get_osversion(void)
{
static int osversion;
struct new_utsname buf;
const char *s;
if (osversion)
return osversion;
if (qemu_uname_release && *qemu_uname_release) {
s = qemu_uname_release;
} else {
if (sys_uname(&buf))
return 0;
s = buf.release;
}
osversion = relstr_to_int(s);
return osversion;
}
void init_qemu_uname_release(void)
{
/* Initialize qemu_uname_release for later use.
* If the host kernel is too old and the user hasn't asked for
* a specific fake version number, we might want to fake a minimum
* target kernel version.
*/
#ifdef UNAME_MINIMUM_RELEASE
struct new_utsname buf;
if (qemu_uname_release && *qemu_uname_release) {
return;
}
if (sys_uname(&buf)) {
return;
}
if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
qemu_uname_release = UNAME_MINIMUM_RELEASE;
}
#endif
}
static int open_self_maps(void *cpu_env, int fd)
{
#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)

View File

@ -70,3 +70,104 @@ const char *cpu_to_uname_machine(void *cpu_env)
return UNAME_MACHINE;
#endif
}
#define COPY_UTSNAME_FIELD(dest, src) \
do { \
/* __NEW_UTS_LEN doesn't include terminating null */ \
(void) strncpy((dest), (src), __NEW_UTS_LEN); \
(dest)[__NEW_UTS_LEN] = '\0'; \
} while (0)
int sys_uname(struct new_utsname *buf)
{
struct utsname uts_buf;
if (uname(&uts_buf) < 0)
return (-1);
/*
* Just in case these have some differences, we
* translate utsname to new_utsname (which is the
* struct linux kernel uses).
*/
memset(buf, 0, sizeof(*buf));
COPY_UTSNAME_FIELD(buf->sysname, uts_buf.sysname);
COPY_UTSNAME_FIELD(buf->nodename, uts_buf.nodename);
COPY_UTSNAME_FIELD(buf->release, uts_buf.release);
COPY_UTSNAME_FIELD(buf->version, uts_buf.version);
COPY_UTSNAME_FIELD(buf->machine, uts_buf.machine);
#ifdef _GNU_SOURCE
COPY_UTSNAME_FIELD(buf->domainname, uts_buf.domainname);
#endif
return (0);
#undef COPY_UTSNAME_FIELD
}
static int relstr_to_int(const char *s)
{
/* Convert a uname release string like "2.6.18" to an integer
* of the form 0x020612. (Beware that 0x020612 is *not* 2.6.12.)
*/
int i, n, tmp;
tmp = 0;
for (i = 0; i < 3; i++) {
n = 0;
while (*s >= '0' && *s <= '9') {
n *= 10;
n += *s - '0';
s++;
}
tmp = (tmp << 8) + n;
if (*s == '.') {
s++;
}
}
return tmp;
}
int get_osversion(void)
{
static int osversion;
struct new_utsname buf;
const char *s;
if (osversion)
return osversion;
if (qemu_uname_release && *qemu_uname_release) {
s = qemu_uname_release;
} else {
if (sys_uname(&buf))
return 0;
s = buf.release;
}
osversion = relstr_to_int(s);
return osversion;
}
void init_qemu_uname_release(void)
{
/* Initialize qemu_uname_release for later use.
* If the host kernel is too old and the user hasn't asked for
* a specific fake version number, we might want to fake a minimum
* target kernel version.
*/
#ifdef UNAME_MINIMUM_RELEASE
struct new_utsname buf;
if (qemu_uname_release && *qemu_uname_release) {
return;
}
if (sys_uname(&buf)) {
return;
}
if (relstr_to_int(buf.release) < relstr_to_int(UNAME_MINIMUM_RELEASE)) {
qemu_uname_release = UNAME_MINIMUM_RELEASE;
}
#endif
}

View File

@ -1 +1,10 @@
#ifndef UNAME_H
#define UNAME_H 1
#include <sys/utsname.h>
#include <linux/utsname.h>
const char *cpu_to_uname_machine(void *cpu_env);
int sys_uname(struct new_utsname *buf);
#endif /* UNAME _H */