Skip to content

Commit

Permalink
hostname now settable by root, uptime reports the time when the app w…
Browse files Browse the repository at this point in the history
…as started rather than iDevice time, stuff in /proc gets app boot time for ctime and mtime and current time for atime. Minor code cleanup
  • Loading branch information
Mike Miller committed Nov 11, 2022
1 parent 5f0b712 commit b35cea7
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 24 deletions.
4 changes: 4 additions & 0 deletions fs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static unsigned long fd_telldir(struct fd *fd) {

static void fd_seekdir(struct fd *fd, unsigned long off) {
fd->offset = off;

if (fd->ops->seekdir)
fd->ops->seekdir(fd, off);
}
Expand Down Expand Up @@ -72,6 +73,8 @@ int_t sys_getdents_common(fd_t f, addr_t dirents, dword_t count,
int printed = 0;
while (true) {
ptr = fd_telldir(fd);
extern time_t boot_time;
fd->stat.atime = (dword_t)boot_time;
struct dir_entry entry;
err = fd->ops->readdir(fd, &entry);
if (err < 0)
Expand All @@ -81,6 +84,7 @@ int_t sys_getdents_common(fd_t f, addr_t dirents, dword_t count,

size_t max_reclen = sizeof(struct linux_dirent64_) + strlen(entry.name) + 4;
char dirent_data[max_reclen];
dirent_data[0] = 0;
ino_t inode = entry.inode;
off_t_ offset = fd_telldir(fd);
const char *name = entry.name;
Expand Down
3 changes: 3 additions & 0 deletions fs/fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct fd *fd_create(const struct fd_ops *ops) {
fd->flags = 0;
fd->mount = NULL;
fd->offset = 0;
fd->stat.ctime = (dword_t)time(NULL);
list_init(&fd->poll_fds);
lock_init(&fd->poll_lock);
lock_init(&fd->lock);
Expand Down Expand Up @@ -49,6 +50,8 @@ int fd_close(struct fd *fd) {
mount_release(fd->mount);
free(fd);
}
if(fd->inode != NULL)
fd->stat.atime = (dword_t)time(NULL);
return err;
}

Expand Down
1 change: 1 addition & 0 deletions fs/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static fd_t pipe_f_create(int pipe_fd, int flags) {
fd->stat.mode = S_IFIFO | 0660;
fd->stat.uid = current->uid;
fd->stat.gid = current->gid;
fd->stat.ctime = (dword_t)time(NULL);
return f_install(fd, flags);
}

Expand Down
4 changes: 4 additions & 0 deletions fs/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ static int proc_getpath(struct fd *fd, char *buf) {

static int proc_stat(struct mount *UNUSED(mount), const char *path, struct statbuf *stat) {
struct proc_entry entry = {0};
extern time_t boot_time;
int err = proc_lookup(path, &entry);
if (err < 0)
return err;
int ret = proc_entry_stat(&entry, stat);
stat->atime = (dword_t)time(NULL);
stat->mtime = (dword_t)boot_time;
stat->ctime = (dword_t)boot_time;
proc_entry_cleanup(&entry);
return ret;
}
Expand Down
1 change: 0 additions & 1 deletion kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ noreturn void do_exit(int status) {
// sighand must be released below so it can be protected by pids_lock
// since it can be accessed by other threads

//while(critical_region_count(current)) {
while((critical_region_count(current) > 1) ||
(locks_held_count(current)) ||
(current->process_info_being_read) ||
Expand Down
2 changes: 1 addition & 1 deletion kernel/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ dword_t sys_readlinkat(fd_t at_f, addr_t path_addr, addr_t buf_addr, dword_t buf
if (user_write(buf_addr, buf, size))
return _EFAULT;
}
return size;
return (dword_t)size;
}

dword_t sys_readlink(addr_t path_addr, addr_t buf_addr, dword_t bufsize) {
Expand Down
8 changes: 7 additions & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ pthread_mutex_t multicore_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t extra_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t delay_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t atomic_l_lock = PTHREAD_MUTEX_INITIALIZER;
time_t boot_time; // Store the boot time. -mke

bool BOOTING = true;

bool doEnableMulticore; // Enable multicore if toggled, should default to false
bool isGlibC = false; // Try to guess if we're running a non musl distro. -mke
bool doEnableExtraLocking; // Enable extra locking if toggled, should default to false
bool doEnableExtraLocking; // Enable extra locking if toggled, should default to true
unsigned doLockSleepNanoseconds; // How many nanoseconds should __lock() sleep between retries

__thread struct task *current;
Expand Down Expand Up @@ -212,6 +213,11 @@ void run_at_boot(void) { // Stuff we run only once, at boot time.
do_uname(&uts);
unsigned short ncpu = get_cpu_count();
printk("iSH-AOK %s booted on %d emulated %s CPU(s)\n",uts.release, ncpu, uts.arch);
// Get boot time
extern time_t boot_time;

boot_time = time(NULL);
//printk("Seconds since January 1, 1970 = %ld\n", boot_time);
BOOTING = false;

}
Expand Down
29 changes: 20 additions & 9 deletions kernel/uname.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
#endif

const char *uname_version = "iSH-AOK";
const char *uname_hostname_override = NULL;
char *uname_hostname_override = NULL;


void do_uname(struct uname *uts) {
struct utsname real_uname;
uname(&real_uname);
const char *hostname = real_uname.nodename;
char *hostname = real_uname.nodename;
if (uname_hostname_override)
hostname = uname_hostname_override;

Expand Down Expand Up @@ -44,8 +45,18 @@ dword_t sys_uname(addr_t uts_addr) {
return 0;
}

dword_t sys_sethostname(addr_t UNUSED(hostname_addr), dword_t UNUSED(hostname_len)) {
return _EPERM;
dword_t sys_sethostname(addr_t hostname_addr, dword_t hostname_len) {
if(current->uid != 0) {
return _EPERM;
} else {
free(uname_hostname_override);
uname_hostname_override = malloc(hostname_len + 1);
int result = 0;
// user_read(addr, &(var), sizeof(var))
result = user_read(hostname_addr, uname_hostname_override, hostname_len + 1);

return result;
}
}

#if __APPLE__
Expand All @@ -55,7 +66,7 @@ static uint64_t get_total_ram() {
return total_ram;
}
static void sysinfo_specific(struct sys_info *info) {
info->totalram = get_total_ram();
info->totalram = (dword_t)get_total_ram();
// TODO: everything else
}
#elif __linux__
Expand All @@ -77,10 +88,10 @@ static void sysinfo_specific(struct sys_info *info) {
dword_t sys_sysinfo(addr_t info_addr) {
struct sys_info info = {0};
struct uptime_info uptime = get_uptime();
info.uptime = uptime.uptime_ticks;
info.loads[0] = uptime.load_1m;
info.loads[1] = uptime.load_5m;
info.loads[2] = uptime.load_15m;
info.uptime = (dword_t)uptime.uptime_ticks;
info.loads[0] = (dword_t)uptime.load_1m;
info.loads[1] = (dword_t)uptime.load_5m;
info.loads[2] = (dword_t)uptime.load_15m;
sysinfo_specific(&info);

if (user_put(info_addr, info))
Expand Down
11 changes: 10 additions & 1 deletion kernel/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ static int __user_write_task(struct task *task, addr_t addr, const void *buf, si
while (p < addr + count) {
addr_t chunk_end = (PAGE(p) + 1) << PAGE_BITS;
if (chunk_end > addr + count)
chunk_end = addr + count;
chunk_end = addr + (addr_t)count;
char *ptr = mem_ptr(task->mem, p, ptrace ? MEM_WRITE_PTRACE : MEM_WRITE);
if (ptr == NULL)
return 1;
memcpy(ptr, &cbuf[p - addr], chunk_end - p);
/* if(!strcmp(task->comm, "ls")) { // Turns out this code mostly deals with linked libraries, at least in the case of ls. -mke
char foo[500] = {};
memcpy(foo, &cbuf[p - addr], 50);
int a = 0;
printk("INFO: FOO: %s\n", foo);
memcpy(ptr, &cbuf[p - addr], chunk_end - p);
} else {
memcpy(ptr, &cbuf[p - addr], chunk_end - p);
} */
p = chunk_end;
}
return 0;
Expand Down
4 changes: 3 additions & 1 deletion platform/darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ struct uptime_info get_uptime() {
sysctlbyname("kern.boottime", &kern_boottime, &size, NULL, 0);
struct timeval now;
gettimeofday(&now, NULL);
extern time_t boot_time;

struct {
uint32_t ldavg[3];
Expand All @@ -88,7 +89,8 @@ struct uptime_info get_uptime() {

struct uptime_info uptime = {
//.uptime_ticks = now.tv_sec - kern_boottime[0],
.uptime_ticks = getSystemUptime() * 100, // This works but shouldn't. -mke
.uptime_ticks = (now.tv_sec - boot_time) * 100, // Note that busybox uptime doesn't like the multiplier. -mke
//.uptime_ticks = getSystemUptime() * 100, // This works but shouldn't. -mke
.load_1m = vm_loadavg.ldavg[0],
.load_5m = vm_loadavg.ldavg[1],
.load_15m = vm_loadavg.ldavg[2],
Expand Down
29 changes: 23 additions & 6 deletions util/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ int wait_for_ignore_signals(cond_t *cond, lock_t *lock, struct timespec *timeout
unlock(&current->waiting_cond_lock);
}

modify_critical_region_counter(current, 1,__FILE__, __LINE__);
int savepid = current->pid;

modify_critical_region_counter(current, 1, __FILE__, __LINE__);

int rc = 0;
#if LOCK_DEBUG
struct lock_debug lock_tmp = lock->debug;
lock->debug = (struct lock_debug) { .initialized = lock->debug.initialized };
#endif
unsigned attempts = 0;
if (!timeout) {
unsigned save_pid = lock->pid;
if(current->pid <= 20) {
pthread_cond_wait(&cond->cond, &lock->m);
goto SKIP;
Expand Down Expand Up @@ -158,21 +158,31 @@ int wait_for_ignore_signals(cond_t *cond, lock_t *lock, struct timespec *timeout
trigger_time.tv_nsec = 0;

if(lock->pid != -1) {

//pthread_mutex_lock(&lock->m);
//rc = pthread_cond_timedwait_relative_np(&cond->cond, &lock->m, &trigger_time);
lock->pid = -33;

if((pthread_mutex_trylock(&lock->m) == EBUSY)) { // Be sure lock is still held. -mke
rc = pthread_cond_timedwait_relative_np(&cond->cond, &lock->m, &trigger_time);
} else {
printk("ERROR: Locking PID is gone in wait_for_ignore_signals() (%s:%d). Attempting recovery\n", current->comm, current->pid);
lock(&current->waiting_cond_lock, 0);
current->waiting_cond = NULL;
current->waiting_lock = NULL;
unlock(&current->waiting_cond_lock);
modify_critical_region_counter(current, -1,__FILE__, __LINE__);
unlock(lock);
lock->pid = save_pid;
//pthread_mutex_unlock(&lock->m);
return 0; // Process that held lock exited. Bad, but what can you do? -mke
}
//pthread_mutex_unlock(&lock->m);
} else {
lock(&current->waiting_cond_lock, 0);
current->waiting_cond = NULL;
current->waiting_lock = NULL;
unlock(&current->waiting_cond_lock);
modify_critical_region_counter(current, -1,__FILE__, __LINE__);
unlock(lock);
lock->pid = save_pid;
return 0; // More Kludgery. -mke
}

Expand All @@ -181,6 +191,11 @@ int wait_for_ignore_signals(cond_t *cond, lock_t *lock, struct timespec *timeout
if(attempts <= 6) // We are likely deadlocked if more than ten attempts -mke
goto AGAIN;
printk("ERROR: Deadlock in wait_for_ignore_signals() (%s:%d). Attempting recovery\n", current->comm, current->pid);
lock(&current->waiting_cond_lock, 0);
current->waiting_cond = NULL;
current->waiting_lock = NULL;
unlock(&current->waiting_cond_lock);
lock->pid = save_pid;
unlock(lock);
modify_critical_region_counter(current, -1,__FILE__, __LINE__);
return 0;
Expand Down Expand Up @@ -247,6 +262,8 @@ unsigned critical_region_count(struct task *task) {
unsigned tmp = 0;
// pthread_mutex_lock(task->critical_region.lock); // This would make more
tmp = task->critical_region.count;
if(tmp > 1000) // Not likely
tmp = 0;
// pthread_mutex_unlock(task->critical_region.lock);

return tmp;
Expand Down
10 changes: 6 additions & 4 deletions util/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static inline void atomic_l_lockf(unsigned count, __attribute__((unused)) const
pthread_mutex_lock(&atomic_l_lock);
modify_locks_held_count_wrapper(1);
modify_critical_region_counter_wrapper(-1,__FILE__, __LINE__);
STRACE("atomic_l_lockf(%d)\n", count);
//STRACE("atomic_l_lockf(%d)\n", count); // This is too verbose most of the time
}

static inline void atomic_l_unlockf(void) {
Expand Down Expand Up @@ -168,6 +168,11 @@ static inline void __lock(lock_t *lock, int log_lock, __attribute__((unused)) co

static inline void unlock(lock_t *lock) {
//modify_critical_region_counter_wrapper(1, __FILE__, __LINE__);
unsigned count = 0;
while((lock->pid == -33) && (count < 1000)) { // Pending signal, wait. -mke
nanosleep(&lock_pause, NULL);
count++;
}
lock->owner = zero_init(pthread_t);
pthread_mutex_unlock(&lock->m);
lock->pid = -1; //
Expand Down Expand Up @@ -196,13 +201,10 @@ typedef struct {
char comm[16];
} wrlock_t;


static inline void _read_unlock(wrlock_t *lock, const char*, int);
static inline void _write_unlock(wrlock_t *lock, const char*, int);
static inline void write_unlock_and_destroy(wrlock_t *lock);



static inline void loop_lock_read(wrlock_t *lock, __attribute__((unused)) const char *file, __attribute__((unused)) int line) {
modify_critical_region_counter_wrapper(1, __FILE__, __LINE__);
modify_locks_held_count_wrapper(1); // No, it hasn't been granted yet, but since it can take some time, we set it here to avoid problems. -mke
Expand Down

0 comments on commit b35cea7

Please sign in to comment.