Skip to content

Commit

Permalink
Improve compatibility with macos
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Oct 16, 2017
1 parent a08a34a commit b5150b8
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 58 deletions.
2 changes: 2 additions & 0 deletions emu/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void mem_retain(struct mem *mem);
void mem_release(struct mem *mem);

#define PAGE_BITS 12
#undef PAGE_SIZE // defined in system headers somewhere
#define PAGE_SIZE (1 << PAGE_BITS)
#define PAGE(addr) ((addr) >> PAGE_BITS)
#define OFFSET(addr) ((addr) & (PAGE_SIZE - 1))
Expand All @@ -43,6 +44,7 @@ struct pt_entry {
// P_READ and P_EXEC are ignored for now
#define P_READ (1 << 0)
#define P_WRITE (1 << 1)
#undef P_EXEC
#define P_EXEC (1 << 2)
#define P_GROWSDOWN (1 << 3)
#define P_COW (1 << 4)
Expand Down
2 changes: 2 additions & 0 deletions fs/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define DEV_H

#include <sys/types.h>
#if __linux__
#include <sys/sysmacros.h>
#endif
#include "kernel/fs.h"

// a dev_t is encoded like this in hex, where M is major and m is minor:
Expand Down
44 changes: 44 additions & 0 deletions fs/ishstat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef FS_ISHSTAT_H
#define FS_ISHSTAT_H

#include "misc.h"

// The values in this structure are stored in an extended attribute on a file,
// because on iOS I can't change the uid or gid of a file.
// TODO the xattr api is a little different on darwin
struct xattr_stat {
dword_t mode;
dword_t uid;
dword_t gid;
dword_t dev;
dword_t rdev;
};
#if defined(__linux__)
#define STAT_XATTR "user.ish.stat"
#elif defined(__APPLE__)
#define STAT_XATTR "com.tbodt.ish.stat"
#endif

#if __linux__
static inline int set_ishstat(const char *path, struct xattr_stat *stat) {
return setxattr(path, "user.ish.stat", stat, sizeof(*stat), 0);
}
static inline int fget_ishstat(int fd, struct xattr_stat *stat) {
return fgetxattr(fd, "user.ish.stat", stat, sizeof(*stat));
}
static inline int lget_ishstat(const char *path, struct xattr_stat *stat) {
return lgetxattr(path, "user.ish.stat", stat, sizeof(*stat));
}
#elif __APPLE__
static inline int set_ishstat(const char *path, struct xattr_stat *stat) {
return setxattr(path, "com.tbodt.ish.stat", stat, sizeof(*stat), 0, 0);
}
static inline int fget_ishstat(int fd, struct xattr_stat *stat) {
return fgetxattr(fd, "com.tbodt.ish.stat", stat, sizeof(*stat), 0, 0);
}
static inline int lget_ishstat(const char *path, struct xattr_stat *stat) {
return getxattr(path, "com.tbodt.ish.stat", stat, sizeof(*stat), 0, XATTR_NOFOLLOW);
}
#endif

#endif
23 changes: 4 additions & 19 deletions fs/real.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "kernel/fs.h"
#include "fs/dev.h"
#include "fs/tty.h"
#include "fs/ishstat.h"

static int getpath(int fd, char *buf) {
#if defined(__linux__)
Expand All @@ -24,7 +25,7 @@ static int getpath(int fd, char *buf) {
buf[size] = '\0';
return size;
#elif defined(__APPLE__)
return fcntl(fd, F_GEPATH, buf);
return fcntl(fd, F_GETPATH, buf);
#endif
}

Expand Down Expand Up @@ -72,22 +73,6 @@ int realfs_close(struct fd *fd) {
return 0;
}

// The values in this structure are stored in an extended attribute on a file,
// because on iOS I can't change the uid or gid of a file.
// TODO the xattr api is a little different on darwin
struct xattr_stat {
dword_t mode;
dword_t uid;
dword_t gid;
dword_t dev;
dword_t rdev;
};
#if defined(__linux__)
#define STAT_XATTR "user.ish.stat"
#elif defined(__APPLE__)
#define STAT_XATTR "com.tbodt.ish.stat"
#endif

static void copy_stat(struct statbuf *fake_stat, struct stat *real_stat) {
fake_stat->dev = dev_fake_from_real(real_stat->st_dev);
fake_stat->inode = real_stat->st_ino;
Expand Down Expand Up @@ -131,7 +116,7 @@ static int realfs_stat(struct mount *mount, char *path, struct statbuf *fake_sta
copy_stat(fake_stat, &real_stat);

struct xattr_stat xstat;
if (lgetxattr(path, STAT_XATTR, &xstat, sizeof(xstat)) == sizeof(xstat))
if (lget_ishstat(path, &xstat) == sizeof(xstat))
copy_xattr_stat(fake_stat, &xstat);
return 0;
}
Expand All @@ -143,7 +128,7 @@ static int realfs_fstat(struct fd *fd, struct statbuf *fake_stat) {
copy_stat(fake_stat, &real_stat);

struct xattr_stat xstat;
if (fgetxattr(fd->real_fd, STAT_XATTR, &xstat, sizeof(xstat)) == sizeof(xstat))
if (fget_ishstat(fd->real_fd, &xstat) == sizeof(xstat))
copy_xattr_stat(fake_stat, &xstat);
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ static inline int err_map(int err) {
ERRCASE(ENAMETOOLONG)
ERRCASE(ENOLCK)
ERRCASE(ENOSYS)
#ifdef ELIBBAD
ERRCASE(ELIBBAD)
#endif
}
#undef ERRCASE
return 1337; // TODO FIXME XXX
Expand Down
1 change: 1 addition & 0 deletions kernel/sockaddr.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
Expand Down
37 changes: 20 additions & 17 deletions kernel/uname.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <strings.h>
#include <string.h>
#if __APPLE__
#include <sys/sysctl.h>
#elif __linux__
#include <sys/sysinfo.h>
#endif
#include "kernel/calls.h"

int sys_uname(struct uname *uts) {
Expand All @@ -22,25 +26,24 @@ dword_t _sys_uname(addr_t uts_addr) {
return res;
}

// TODO portability
#if __APPLE__
static uint64_t get_total_ram() {
uint64_t total_ram;
sysctl((int []) {CTL_DEBUG, HW_PHYSMEM}, 2, &total_ram, NULL, NULL, 0);
return total_ram;
}
#elif __linux__
static uint64_t get_total_ram() {
struct sysinfo info;
sysinfo(&info);
return info.totalram;
}
#endif

dword_t sys_sysinfo(addr_t info_addr) {
struct sys_info info;
struct sysinfo real_info;
sysinfo(&real_info);
info.uptime = real_info.uptime;
info.loads[0] = real_info.loads[0];
info.loads[1] = real_info.loads[1];
info.loads[2] = real_info.loads[2];
info.totalram = real_info.totalram;
info.freeram = real_info.freeram;
info.sharedram = real_info.sharedram;
info.bufferram = real_info.bufferram;
info.totalswap = real_info.totalswap;
info.freeswap = real_info.freeswap;
info.procs = real_info.procs;
info.totalhigh = real_info.totalhigh;
info.freehigh = real_info.freehigh;
info.mem_unit = real_info.mem_unit;
info.totalram = get_total_ram();
// TODO everything else
if (user_put(info_addr, info))
return _EFAULT;
return 0;
Expand Down
27 changes: 15 additions & 12 deletions tools/meson.build
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
transplant_src = [
'vdso-transplant.c',
'ptutil.c',
cified_vdso,
]
executable('ptraceomatic', ['ptraceomatic.c', 'undefined-flags.c', transplant_src], dependencies: [ish, softfloat, threads])
configure_file(input: 'ptraceomatic-gdb.gdb', output: '@PLAINNAME@', configuration: configuration_data())
# these tools are linux-specific
if build_machine.system() == 'linux'
transplant_src = [
'vdso-transplant.c',
'ptutil.c',
cified_vdso,
]
executable('ptraceomatic', ['ptraceomatic.c', 'undefined-flags.c', transplant_src], dependencies: [ish, softfloat, threads])
configure_file(input: 'ptraceomatic-gdb.gdb', output: '@PLAINNAME@', configuration: configuration_data())

executable('xattrify_fs', ['xattrify_fs.c'])
# tools for messing with vdsos
executable('vdso-dump', ['vdso-dump.c'], c_args: ['-m32'], link_args: ['-m32'])
executable('vdso-transplant', ['vdso-transplant-main.c', transplant_src],
include_directories: includes)
endif

# tools for messing with vdsos
executable('vdso-dump', ['vdso-dump.c'], c_args: ['-m32'], link_args: ['-m32'])
executable('vdso-transplant', ['vdso-transplant-main.c', transplant_src],
include_directories: includes)
executable('xattrify_fs', ['xattrify_fs.c'], include_directories: includes)
11 changes: 2 additions & 9 deletions tools/xattrify_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
#include <unistd.h>
#include <sys/xattr.h>
#include <ftw.h>

struct xattr_stat {
uint32_t mode;
uint32_t uid;
uint32_t gid;
uint32_t dev;
uint32_t rdev;
};
#include "fs/ishstat.h"

int xattrify_file(const char *file, const struct stat *stat, int type) {
struct xattr_stat xstat;
Expand All @@ -20,7 +13,7 @@ int xattrify_file(const char *file, const struct stat *stat, int type) {
xstat.gid = stat->st_gid;
xstat.dev = stat->st_dev;
xstat.rdev = stat->st_rdev;
if (setxattr(file, "user.ish.stat", &xstat, sizeof(xstat), 0) < 0)
if (set_ishstat(file, &xstat) < 0)
perror(file);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static inline int xX_main_Xx(int argc, char *const argv[]) {
}
}

char root_realpath[PATH_MAX + 1] = "/";
char root_realpath[MAX_PATH + 1] = "/";
if (has_root && realpath(root, root_realpath) == NULL) {
perror(root); exit(1);
}
Expand Down

0 comments on commit b5150b8

Please sign in to comment.