Skip to content

Commit

Permalink
Revert console change, various code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Miller committed Nov 11, 2023
1 parent cd12b23 commit 492fe51
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 81 deletions.
4 changes: 2 additions & 2 deletions app/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ - (intptr_t)boot {
generic_mknodat(AT_PWD, "/dev/tty7", S_IFCHR|0666, dev_make(TTY_CONSOLE_MAJOR, 7));

generic_mknodat(AT_PWD, "/dev/tty", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_TTY_MINOR));
generic_mknodat(AT_PWD, "/dev/console", S_IFCHR|0222, dev_make(136, 0));
// generic_mknodat(AT_PWD, "/dev/console", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_CONSOLE_MINOR));
//generic_mknodat(AT_PWD, "/dev/console", S_IFCHR|0222, dev_make(136, 0));
generic_mknodat(AT_PWD, "/dev/console", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_CONSOLE_MINOR));
generic_mknodat(AT_PWD, "/dev/ptmx", S_IFCHR|0666, dev_make(TTY_ALTERNATE_MAJOR, DEV_PTMX_MINOR));

generic_mknodat(AT_PWD, "/dev/null", S_IFCHR|0777, dev_make(MEM_MAJOR, DEV_NULL_MINOR));
Expand Down
121 changes: 63 additions & 58 deletions app/AppGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,67 +34,72 @@
};

static NSDictionary *AppEntitlements(void) {
static NSDictionary *entitlements;
if (entitlements != nil)
return entitlements;

// Inspired by codesign.c in Darwin sources for Security.framework

const struct mach_header_64 *header = &_mh_execute_header;

// Simulator executables have fake entitlements in the code signature. The real entitlements can be found in an __entitlements section.
size_t entitlements_size;
char *entitlements_data = (char *) getsectiondata(header, "__TEXT", "__entitlements", &entitlements_size);
if (entitlements_data != NULL) {
NSData *data = [NSData dataWithBytesNoCopy:entitlements_data
length:entitlements_size
freeWhenDone:NO];
return entitlements = [NSPropertyListSerialization propertyListWithData:data
options:NSPropertyListImmutable
format:nil
error:nil];
}

// Find the LC_CODE_SIGNATURE
struct load_command *lc = (void *) (header + 1);
struct linkedit_data_command *cs_lc = NULL;
for (uint32_t i = 0; i < header->ncmds; i++) {
if (lc->cmd == LC_CODE_SIGNATURE) {
cs_lc = (void *) lc;
break;
static NSDictionary *entitlements = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
const struct mach_header_64 *header = &_mh_execute_header;

// Check for entitlements in the __entitlements section (common in simulators)
size_t entitlements_size;
char *entitlements_data = (char *) getsectiondata(header, "__TEXT", "__entitlements", &entitlements_size);
if (entitlements_data != NULL) {
NSData *data = [NSData dataWithBytesNoCopy:entitlements_data length:entitlements_size freeWhenDone:NO];
NSError *error = nil;
entitlements = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:nil error:&error];
if (entitlements == nil) {
NSLog(@"Failed to parse entitlements: %@", error);
return;
}
return;
}
lc = (void *) ((char *) lc + lc->cmdsize);
}
if (cs_lc == NULL)
return nil;

// Code for fetching entitlements from the code signature
struct load_command *lc = (void *) (header + 1);
struct linkedit_data_command *cs_lc = NULL;
for (uint32_t i = 0; i < header->ncmds; i++) {
if (lc->cmd == LC_CODE_SIGNATURE) {
cs_lc = (void *) lc;
break;
}
lc = (void *) ((char *) lc + lc->cmdsize);
}
if (cs_lc == NULL)
return;

NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:NSBundle.mainBundle.executableURL error:nil];
if (fileHandle == nil)
return;
[fileHandle seekToFileOffset:cs_lc->dataoff];
NSData *csData = [fileHandle readDataOfLength:cs_lc->datasize];
[fileHandle closeFile];
const struct cs_superblob *cs = csData.bytes;
if (ntohl(cs->magic) != CSMAGIC_EMBEDDED_SIGNATURE)
return;

NSData *entitlementsData = nil;
for (uint32_t i = 0; i < ntohl(cs->count); i++) {
struct cs_entitlements *ents = (void *) ((char *) cs + ntohl(cs->index[i].offset));

// Read the magic number in a way that does not assume alignment
uint32_t magic;
memcpy(&magic, &ents->magic, sizeof(uint32_t));
if (ntohl(ents->magic) == CSMAGIC_EMBEDDED_ENTITLEMENTS) {
entitlementsData = [NSData dataWithBytes:ents->entitlements length:ntohl(ents->length) - offsetof(struct cs_entitlements, entitlements)];
break; // Entitlements found
}
}

if (entitlementsData == nil)
return;

// Read the code signature off disk, as it's apparently not loaded into memory
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:NSBundle.mainBundle.executableURL error:nil];
if (fileHandle == nil)
return nil;
[fileHandle seekToFileOffset:cs_lc->dataoff];
NSData *csData = [fileHandle readDataOfLength:cs_lc->datasize];
[fileHandle closeFile];
const struct cs_superblob *cs = csData.bytes;
if (ntohl(cs->magic) != CSMAGIC_EMBEDDED_SIGNATURE)
return nil;

// Find the entitlements in the code signature
NSData *entitlementsData = nil;
for (uint32_t i = 0; i < ntohl(cs->count); i++) {
struct cs_entitlements *ents = (void *) ((char *) cs + ntohl(cs->index[i].offset));
if (ntohl(ents->magic) == CSMAGIC_EMBEDDED_ENTITLEMENTS) {
entitlementsData = [NSData dataWithBytes:ents->entitlements
length:ntohl(ents->length) - offsetof(struct cs_entitlements, entitlements)];
NSError *serializationError = nil;
entitlements = [NSPropertyListSerialization propertyListWithData:entitlementsData options:NSPropertyListImmutable format:nil error:&serializationError];
if (entitlements == nil) {
NSLog(@"Failed to parse entitlements: %@", serializationError);
return;
}
}
if (entitlementsData == nil)
return nil;

return entitlements = [NSPropertyListSerialization propertyListWithData:entitlementsData
options:NSPropertyListImmutable
format:nil
error:nil];
});
return entitlements;
}

NSArray<NSString *> *CurrentAppGroups(void) {
Expand Down
25 changes: 15 additions & 10 deletions app/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
extern void run_at_boot(void);
#import <Foundation/Foundation.h>
#import <Foundation/NSProcessInfo.h>

void disable_app_nap(void) {
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:reason:)]) {
[[NSProcessInfo processInfo] beginActivityWithOptions:0x00FFFFFF reason:@"Not sleepy and don't want to nap"];
}
}
extern void run_at_boot(void);

int main(int argc, char * argv[]) {
@autoreleasepool {
//disable_app_nap(); // No napping I say. -mke
run_at_boot();
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
int retVal = 0;
@try {
// Your existing setup code here
retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
@catch (NSException *exception) {
// Handle or log the exception
NSLog(@"Uncaught exception: %@", exception.description);
NSLog(@"Stack trace: %@", [exception callStackSymbols]);
}
@finally {
// Perform any final cleanup or logging if necessary
return retVal;
}
}
}
4 changes: 2 additions & 2 deletions fs/proc/root.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ DirectMap2M: 940032 kB
DirectMap1G: 0 kB
*/
struct mem_usage usage = get_mem_usage();
show_kb(buf, "Buffers: ", 0);
show_kb(buf, "Cached: ", usage.cached);
show_kb(buf, "MemTotal: ", usage.total);
show_kb(buf, "MemFree: ", usage.free);
show_kb(buf, "MemAvailable: ", usage.available);
show_kb(buf, "Buffers: ", 0);
show_kb(buf, "Cached: ", usage.cached);
show_kb(buf, "MemShared: ", usage.free);
show_kb(buf, "Active: ", usage.active);
show_kb(buf, "Inactive: ", usage.inactive);
Expand Down
14 changes: 7 additions & 7 deletions kernel/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void signal_delivery_stop(int sig, struct siginfo_ *info) {
lock(&current->sighand->lock, 0);
}

void receive_signals() { // Should this function have a check for critical_region_count? -mke
void receive_signals(void) { // Should this function have a check for critical_region_count? -mke
//nanosleep(&lock_pause, NULL);
lock(&current->group->lock, 0);
bool was_stopped = current->group->stopped;
Expand Down Expand Up @@ -419,7 +419,7 @@ static void restore_sigcontext(struct sigcontext_ *context, struct cpu_state *cp
cpu->eflags = (context->flags & USE_FLAGS) | (cpu->eflags & ~USE_FLAGS);
}

dword_t sys_rt_sigreturn() {
dword_t sys_rt_sigreturn(void) {
struct cpu_state *cpu = &current->cpu;
struct rt_sigframe_ frame;
// esp points past the first field of the frame
Expand All @@ -441,7 +441,7 @@ dword_t sys_rt_sigreturn() {
return cpu->eax;
}

dword_t sys_sigreturn() {
dword_t sys_sigreturn(void) {
struct cpu_state *cpu = &current->cpu;
struct sigframe_ frame;
// esp points past the first two fields of the frame
Expand All @@ -458,7 +458,7 @@ dword_t sys_sigreturn() {
return cpu->eax;
}

struct sighand *sighand_new() {
struct sighand *sighand_new(void) {
struct sighand *sighand = malloc(sizeof(struct sighand));
if (sighand == NULL)
return NULL;
Expand Down Expand Up @@ -560,7 +560,7 @@ dword_t sys_rt_sigprocmask(dword_t how, addr_t set_addr, addr_t oldset_addr, dwo
if (size != sizeof(sigset_t_))
return _EINVAL;

sigset_t_ set;
sigset_t_ set = 0;
if (set_addr != 0)
if (user_get(set_addr, set))
return _EFAULT;
Expand Down Expand Up @@ -661,7 +661,7 @@ int_t sys_rt_sigsuspend(addr_t mask_addr, uint_t size) {
return _EINTR;
}

int_t sys_pause() {
int_t sys_pause(void) {
lock(&current->sighand->lock, 0);
TASK_MAY_BLOCK {
while (wait_for(&current->pause, &current->sighand->lock, NULL) != _EINTR)
Expand Down Expand Up @@ -690,7 +690,7 @@ int_t sys_rt_sigtimedwait(addr_t set_addr, addr_t info_addr, addr_t timeout_addr
lock(&current->sighand->lock, 0);
assert(current->waiting == 0);
current->waiting = set;
int err;
int err = 0;
TASK_MAY_BLOCK {
do {
err = wait_for(&current->pause, &current->sighand->lock, timeout_addr == 0 ? NULL : &timeout);
Expand Down
4 changes: 2 additions & 2 deletions util/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ unsigned critical_region_count(struct task *task) {
return tmp;
}

unsigned critical_region_count_wrapper() { // sync.h can't know about the definition of struct due to recursive include files. -mke
unsigned critical_region_count_wrapper(void) { // sync.h can't know about the definition of struct due to recursive include files. -mke
return(critical_region_count(current));
}

Expand All @@ -234,7 +234,7 @@ unsigned locks_held_count(struct task *task) {
return tmp;
}

unsigned locks_held_count_wrapper() { // sync.h can't know about the definition of struct due to recursive include files. -mke
unsigned locks_held_count_wrapper(void) { // sync.h can't know about the definition of struct due to recursive include files. -mke
if(current != NULL)
return(locks_held_count(current));
return 0;
Expand Down

0 comments on commit 492fe51

Please sign in to comment.