static void setExceptionThread(void) { kern_return_t r; // char *nullAddr = NULL; bailOut = FALSE; /* save the old exception port for this task */ r = task_get_exception_port(task_self(), &(ports.old_exc_port)); if (r != KERN_SUCCESS) { mach_error("task_get_exception_port",r); exit(1); } if (!ports.exc_port) { /* create a new exception port for this task */ r = port_allocate(task_self(), &(ports.exc_port)); if (r != KERN_SUCCESS) { mach_error("port_allocate",r); exit(1); } /* Fork the thread that listens to the exception port. */ cthread_detach(cthread_fork((cthread_fn_t)exc_thread,(any_t)&ports)); ports.clear_port = thread_reply(); } /* install the new exception port for this task */ r = task_set_exception_port(task_self(), (ports.exc_port)); if (r != KERN_SUCCESS) { mach_error("task_set_exception_port",r); exit(1); } }
/* These are my mach based versions, untested and probably bad ... */ caddr_t my_mmap(caddr_t addr, size_t len, int prot, int flags, int fildes, off_t off) { kern_return_t ret_val; /* First map ... */ ret_val = map_fd ( fildes, /* fd */ (vm_offset_t) off, /* offset */ (vm_offset_t*)&addr, /* address */ TRUE, /* find_space */ (vm_size_t) len); /* size */ if (ret_val != KERN_SUCCESS) { mach_error("Error calling map_fd() in mmap", ret_val ); return (caddr_t)0; } /* ... then protect (this is probably bad) */ ret_val = vm_protect( task_self(), /* target_task */ (vm_address_t)addr, /* address */ (vm_size_t) len, /* size */ FALSE, /* set_maximum */ (vm_prot_t) prot); /* new_protection */ if (ret_val != KERN_SUCCESS) { mach_error("vm_protect in mmap()", ret_val ); return (caddr_t)0; } return addr; }
kern_return_t task_stats(int pid, struct task_basic_info *info) { kern_return_t status; task_t p_task; unsigned int info_count=TASK_BASIC_INFO_COUNT; /* Get the task pointer for the process. */ status = task_by_unix_pid( task_self(), pid, &p_task); if (status!=KERN_SUCCESS) { #ifdef DEBUG printf("pid = %i\n", pid); mach_error("Error calling task_by_unix_pid()", status); #endif return(status); } status=task_info(p_task, TASK_BASIC_INFO, (task_info_t)info, &info_count); if (status!=KERN_SUCCESS) { #ifdef DEBUG mach_error("Error calling task_info()", status); #endif return(status); } return(KERN_SUCCESS); }
static void *map_it( const char *path, int fd, void *map_at, size_t len ) { kern_return_t rc; vm_offset_t addr; addr = (vm_offset_t)map_at; rc = vm_allocate( task_self(), &addr, len, /* anywhere */ FALSE ); if (rc != KERN_SUCCESS) { mach_error( "vm_allocate", rc ); fprintf( stderr, "%s: could not map at %08lx\n", path, (unsigned long)map_at ); return NULL; } rc = map_fd( fd, 0, &addr, /*find_space*/ FALSE, len ); if (rc != KERN_SUCCESS) { mach_error( "map_fd", rc ); fprintf( stderr, "%s: could not map at %08lx\n", path, (unsigned long)map_at ); return NULL; } return (void *)addr; }
static int send_trigger(int trigger) { mach_port_t serverPort; kern_return_t error; error = host_get_audit_control_port(mach_host_self(), &serverPort); if (error != KERN_SUCCESS) { if (geteuid() != 0) { errno = EPERM; perror("audit requires root privileges"); } else mach_error("Cannot get auditd_control Mach port:", error); return (-1); } error = auditd_control(serverPort, trigger); if (error != KERN_SUCCESS) { mach_error("Error sending trigger: ", error); return (-1); } return (0); }
static void* Pt_Thread(void *p) { CFTimeInterval timerInterval; CFRunLoopTimerContext timerContext; CFRunLoopTimerRef timer; PtThreadParams *params = (PtThreadParams*)p; //CFTimeInterval timeout; /* raise the thread's priority */ kern_return_t error; thread_extended_policy_data_t extendedPolicy; thread_precedence_policy_data_t precedencePolicy; extendedPolicy.timeshare = 0; error = thread_policy_set(mach_thread_self(), THREAD_EXTENDED_POLICY, (thread_policy_t)&extendedPolicy, THREAD_EXTENDED_POLICY_COUNT); if (error != KERN_SUCCESS) { mach_error("Couldn't set thread timeshare policy", error); } precedencePolicy.importance = THREAD_IMPORTANCE; error = thread_policy_set(mach_thread_self(), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&precedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT); if (error != KERN_SUCCESS) { mach_error("Couldn't set thread precedence policy", error); } /* set up the timer context */ timerContext.version = 0; timerContext.info = params; timerContext.retain = NULL; timerContext.release = NULL; timerContext.copyDescription = NULL; /* create a new timer */ timerInterval = (double)params->resolution / 1000.0; timer = CFRunLoopTimerCreate(NULL, startTime+timerInterval, timerInterval, 0, 0, Pt_CFTimerCallback, &timerContext); timerRunLoop = CFRunLoopGetCurrent(); CFRunLoopAddTimer(timerRunLoop, timer, CFSTR("PtTimeMode")); /* run until we're told to stop by Pt_Stop() */ CFRunLoopRunInMode(CFSTR("PtTimeMode"), LONG_TIME, false); CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timer, CFSTR("PtTimeMode")); CFRelease(timer); free(params); return NULL; }
void EstablishDylanExceptionHandlers(void) { if (exception_port == MACH_PORT_NULL) { // Need a port we can receive exceptions on kern_return_t rc = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exception_port); if (rc != KERN_SUCCESS) { mach_error("mach_port_allocate send", rc); abort(); } // Need to be able to send on it too rc = mach_port_insert_right(mach_task_self(), exception_port, exception_port, MACH_MSG_TYPE_MAKE_SEND); if (rc != KERN_SUCCESS) { mach_error("mach_port_insert_right", rc); abort(); } // Spawn a thread to serve exception requests pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_t thread; int prc = pthread_create(&thread, &attr, catcher, NULL); pthread_attr_destroy(&attr); if (prc != 0) { fprintf(stderr, "%s: pthread_create returned %d\n", __func__, prc); abort(); } } // Set this thread's exception port kern_return_t rc = thread_set_exception_ports(mach_thread_self(), EXC_MASK_ARITHMETIC|EXC_MASK_BREAKPOINT, exception_port, EXCEPTION_STATE_IDENTITY|MACH_EXCEPTION_CODES, THREAD_STATE_FLAVOR); if (rc != KERN_SUCCESS) { mach_error("thread_set_exception_ports", rc); abort(); } primitive_reset_float_environment(); }
static int S_remove_service_with_id(mach_port_t server, int argc, char * argv[]) { if_name_t if_name; kern_return_t kret; inline_data_t service_id; unsigned int service_id_len; ipconfig_status_t status = ipconfig_status_success_e; service_id_len = (int)strlen(argv[0]); if (service_id_len > sizeof(service_id)) { service_id_len = sizeof(service_id); } memcpy(service_id, argv[0], service_id_len); if (argc > 1) { strlcpy(if_name, argv[1], sizeof(if_name)); } else { bzero(if_name, sizeof(if_name)); } kret = ipconfig_remove_service_on_interface(server, if_name, service_id, service_id_len, &status); if (kret != KERN_SUCCESS) { mach_error("ipconfig_remove_service_on_interface failed", kret); return (1); } if (status != ipconfig_status_success_e) { fprintf(stderr, "ipconfig_remove_service_on_interface %s failed: %s\n", argv[0], ipconfig_status_string(status)); return (1); } return (0); }
static int S_find_service(mach_port_t server, int argc, char * argv[]) { CFDataRef data = NULL; CFDictionaryRef dict; boolean_t exact = FALSE; char * method_name; if_name_t if_name; kern_return_t kret; inline_data_t service_id; unsigned int service_id_len = sizeof(service_id); ipconfig_status_t status = ipconfig_status_success_e; void * xml_data_ptr = NULL; int xml_data_len = 0; strlcpy(if_name, argv[0], sizeof(if_name)); argv++; argc--; if (argc > 1 && strcasecmp(argv[0], "exact") == 0) { exact = TRUE; argc--; argv++; } method_name = argv[0]; argc--; argv++; dict = ConfigDictCreate(if_name, argc, argv, command_name, method_name, FALSE); if (dict == NULL) { return (1); } data = CFPropertyListCreateData(NULL, dict, kCFPropertyListBinaryFormat_v1_0, 0, NULL); if (data == NULL) { CFRelease(dict); fprintf(stderr, "failed to allocate memory\n"); return (1); } xml_data_ptr = (void *)CFDataGetBytePtr(data); xml_data_len = (int)CFDataGetLength(data); kret = ipconfig_find_service(server, if_name, exact, xml_data_ptr, xml_data_len, service_id, &service_id_len, &status); my_CFRelease(&dict); my_CFRelease(&data); if (kret != KERN_SUCCESS) { mach_error("ipconfig_find_service failed", kret); return (1); } if (status != ipconfig_status_success_e) { fprintf(stderr, "ipconfig_find_service %s %s failed: %s\n", if_name, method_name, ipconfig_status_string(status)); return (1); } printf("%.*s\n", service_id_len, service_id); return (0); }
static void wait_for_servers(void) { int i; int retry_count = 10; mach_port_t bsport, servport; kern_return_t ret; /* find server port */ ret = task_get_bootstrap_port(mach_task_self(), &bsport); if (KERN_SUCCESS != ret) { mach_error("task_get_bootstrap_port(): ", ret); exit(1); } while (retry_count-- > 0) { for (i = 0; i < num_servers; i++) { ret = bootstrap_look_up(bsport, server_port_name[i], &servport); if (ret != KERN_SUCCESS) { break; } } if (ret == KERN_SUCCESS) return; usleep(100 * 1000); /* 100ms */ } fprintf(stderr, "Server(s) failed to register\n"); exit(1); }
int main(int argc, char * argv[]) { const struct command_info * command; mach_port_t server = MACH_PORT_NULL; kern_return_t kret; progname = argv[0]; if (argc < 2) usage(); argv++; argc--; command_name = argv[0]; command = S_lookup_command(command_name, argc - 1); if (command == NULL) { usage(); exit(1); } argv++; argc--; if (command->no_server == 0) { kret = ipconfig_server_port(&server); switch (kret) { case BOOTSTRAP_SUCCESS: break; case BOOTSTRAP_UNKNOWN_SERVICE: fprintf(stderr, "ipconfig server not active\n"); /* start it maybe??? */ exit(1); default: mach_error("ipconfig_server_port failed", kret); exit(1); } } exit ((*command->func)(server, argc, argv)); }
int main(int argc, char** argv) { //定义变量并初始化 kern_return_t kr; vm_task_t target_task=mach_task_self(); vm_address_t *address=NULL; vm_size_t size=vm_page_size; vm_address_t mask;//mask不会设置; boolean_t anywhere=TURE; memory_object_t memory_object=MEMORY_OBJECT_NULL; vm_offset_t offset=0; boolean_t copy=FALSE; vm_prot_t cur_protection=(VM_PORT_READ|VM_PORT_WRITE);//当前保护属性,读写 vm_prot_t max_protection=(VM_PORT_READ|VM_PORT_WRITE);//最大保护属性,读写 vm_inherit_t inheritance=VM_INHERIT_SHARE;//共享,设置共享 kr=0; //建立一个新的内存对象,使用vm_map(); kr=vm_map(target_task,*address,size,mask,anywhere, memory_object,offset,copy,cur_protection, max_protection, inheritance); if(kr) { mach_error("the value of error is",kr); printf("vm_map() is exiting:%d\n",kr); return kr; } printf("vm_map is ok\n"); }
int main(void) { kern_return_t kr; struct stat statbuf; // Initialize a message for use with the Apple System Log (asl) facility. logmsg = asl_new(ASL_TYPE_MSG); asl_set(logmsg, "Facility", "Sleeper Daemon"); // If the shutdown flag file exists, we are destroying the service; // otherwise, we are trying to be a server. if (stat(SERVICE_SHUTDOWN_FILE, &statbuf) == 0) { kr = unregister_bootstrap_service(); MY_ASL_LOG("destroying service %s\n", SERVICE_NAME); } else { kr = register_bootstrap_service(); MY_ASL_LOG("starting up service %s\n", SERVICE_NAME); } if (kr != KERN_SUCCESS) { // NB: When unregistering, we will get here if the unregister succeeded. mach_error("bootstrap_register", kr); exit(kr); } MY_ASL_LOG("server loop ready\n"); while (1) // Dummy server loop. sleep(60); exit(0); }
int my_mprotect(caddr_t addr, size_t len, int prot) { vm_prot_t mach_prot; kern_return_t ret_val; switch (prot) { case PROT_READ: mach_prot = VM_PROT_READ; break; case PROT_WRITE: mach_prot = VM_PROT_WRITE; break; case PROT_EXEC: mach_prot = VM_PROT_EXECUTE; break; case PROT_NONE: mach_prot = VM_PROT_NONE; break; } ret_val = vm_protect(task_self(), /* target_task */ (vm_address_t)addr, /* address */ (vm_size_t) len, /* size */ FALSE, /* set_maximum */ (vm_prot_t) prot); /* new_protection */ if (ret_val != KERN_SUCCESS) { mach_error("vm_protect in mprotect()", ret_val); return -1; } return 0; }
static any_t exc_thread(ports_t *port_p) { kern_return_t r; char *msg_data[2][64]; msg_header_t *imsg = (msg_header_t *)msg_data[0], *omsg = (msg_header_t *)msg_data[1]; /* Wait for exceptions */ while(1) { imsg->msg_size = 64; imsg->msg_local_port = port_p->exc_port; r = msg_receive(imsg, MSG_OPTION_NONE, 0); if (r == RCV_SUCCESS) { /* Give the message to the Mach exception server. */ if (exc_server(imsg,omsg)) { /* send the reply message that exc_serv gave us. */ r = msg_send(omsg, MSG_OPTION_NONE, 0); if (r != SEND_SUCCESS) { mach_error("exc_thread msg_send",r); exit(1); } } else { /* exc_server refused to handle the imsg. */ exit(2); } } else { /* msg_receive returned an error. */ mach_error("exc_thread msg_receive",r); exit(3); } /* Pass the message to the old exception handler, if necessary. */ if (pass_on == TRUE) { imsg->msg_remote_port = port_p->old_exc_port; imsg->msg_local_port = port_p->clear_port; r = msg_send(imsg, MSG_OPTION_NONE, 0); if (r != SEND_SUCCESS) { mach_error("msg_send to old_exc_port",r); exit(4); } } } }
static void restoreExceptionThread(void) { /* install the old port again */ kern_return_t r = task_set_exception_port(task_self(), (ports.old_exc_port)); if (r != KERN_SUCCESS) { mach_error("task_set_exception_port",r); exit(1); } }
msg_return_t send_request( port_t port, enum request_type request, const char * dylib, enum profile_state *profile_state, enum result_code *result_code, char * gmon_file ) { union { struct request_msg request; struct reply_msg reply; } msg; msg_return_t msg_ret; /* * Cons up the header and type structs */ msg.request.hdr.msg_simple = TRUE; msg.request.hdr.msg_size = sizeof(struct request_msg); msg.request.hdr.msg_type = MSG_TYPE_NORMAL; msg.request.hdr.msg_local_port = thread_reply(); msg.request.hdr.msg_remote_port = port; msg.request.hdr.msg_id = PROFILE_REQUEST_ID; msg.request.request_type.msg_type_name = MSG_TYPE_INTEGER_32; msg.request.request_type.msg_type_size = sizeof(enum request_type) * 8; msg.request.request_type.msg_type_number = 1; msg.request.request_type.msg_type_inline = TRUE; msg.request.request_type.msg_type_longform = FALSE; msg.request.request_type.msg_type_deallocate = FALSE; msg.request.dylib_type.msg_type_name = MSG_TYPE_CHAR; msg.request.dylib_type.msg_type_size = sizeof(char) * 8; msg.request.dylib_type.msg_type_number = strlen(dylib) + 1; msg.request.dylib_type.msg_type_inline = TRUE; msg.request.dylib_type.msg_type_longform = FALSE; msg.request.dylib_type.msg_type_deallocate = FALSE; strcpy(msg.request.dylib_file, dylib); msg.request.request = request; /* * Send it off. */ msg_ret = msg_rpc(&msg.request.hdr, MSG_OPTION_NONE, sizeof(msg), (msg_timeout_t)0, (msg_timeout_t)0); if (msg_ret != RPC_SUCCESS) { mach_error("msg_rpc:", msg_ret); return msg_ret; } *profile_state = msg.reply.profile_state; *result_code = msg.reply.result_code; strcpy(gmon_file, msg.reply.gmon_file); return SEND_SUCCESS; }
main(int argc, char **argv) { int c, fd, ofd, filesize; kern_return_t r; char *infile, *outfile, *memfile, *oldstring, *os, *newstring; struct stat statbuf; if (argc != 5) usage(); infile = argv[1]; outfile = argv[2]; fd = open(infile, O_RDONLY); if (fd < 0) { perror("open infile"); exit(1); } if (fstat(fd, &statbuf) < 0) { perror("stat infile"); exit(1); } ofd = open(outfile, O_TRUNC|O_RDWR|O_CREAT, 0644); if (ofd < 0) { perror("open outfile"); exit(1); } filesize = statbuf.st_size; oldstring = strFromQuotedStr(argv[3]); newstring = strFromQuotedStr(argv[4]); if (strlen(newstring) > strlen(oldstring)) { fprintf(stderr, "Warning: new string is bigger than old string.\n"); } r = map_fd(fd, (vm_offset_t)0, (vm_offset_t *)&memfile, TRUE, (vm_size_t)filesize); if (r != KERN_SUCCESS) { mach_error("Error calling map_fd()", r); exit(1); } else { os = (char *)strnstr(memfile, oldstring, filesize); if (os == NULL) { fprintf(stderr, "String not found\n"); exit(1); } while (*newstring) *os++ = *newstring++; *os++ = *newstring++; lseek(fd, 0, 0); c = write(ofd, memfile, filesize); if (c < filesize) { perror("write outfile"); exit(2); } exit(0); } }
/* * Do all the real work. * Send a message to the audit daemon and check the return code. */ void process(int flags) { kern_return_t retcode; retcode = auditd_control(serverPort, flags); if(retcode != KERN_SUCCESS) { mach_error("error doing IPC: ", retcode); exit(1); } printf("Client call successful\n"); }
static void *Pt_CallbackProc(void *p) { pt_callback_parameters *parameters = (pt_callback_parameters *) p; int mytime = 1; kern_return_t error; thread_extended_policy_data_t extendedPolicy; thread_precedence_policy_data_t precedencePolicy; extendedPolicy.timeshare = 0; error = thread_policy_set(mach_thread_self(), THREAD_EXTENDED_POLICY, (thread_policy_t)&extendedPolicy, THREAD_EXTENDED_POLICY_COUNT); if (error != KERN_SUCCESS) { mach_error("Couldn't set thread timeshare policy", error); } precedencePolicy.importance = THREAD_IMPORTANCE; error = thread_policy_set(mach_thread_self(), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&precedencePolicy, THREAD_PRECEDENCE_POLICY_COUNT); if (error != KERN_SUCCESS) { mach_error("Couldn't set thread precedence policy", error); } /* to kill a process, just increment the pt_callback_proc_id */ /* printf("pt_callback_proc_id %d, id %d\n", pt_callback_proc_id, parameters->id); */ while (pt_callback_proc_id == parameters->id) { /* wait for a multiple of resolution ms */ UInt64 wait_time; int delay = mytime++ * parameters->resolution - Pt_Time(); PtTimestamp timestamp; if (delay < 0) delay = 0; wait_time = AudioConvertNanosToHostTime((UInt64)delay * NSEC_PER_MSEC); wait_time += AudioGetCurrentHostTime(); error = mach_wait_until(wait_time); timestamp = Pt_Time(); (*(parameters->callback))(timestamp, parameters->userData); } free(parameters); return NULL; }
void update_cpu_load() { kern_return_t error; natural_t nmpu; processor_info_array_t info; mach_msg_type_number_t cnt; int infosz; int i; int firstrun = 0; error = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &nmpu, &info, &cnt); if (error != KERN_SUCCESS) { mach_error("update_cpu_load1", error); exit(1); } if (cpu_load == NULL) { cpu_count = nmpu; cpu_load = calloc(nmpu, sizeof (cpu_load_t)); firstrun = 1; } infosz = cnt / nmpu; for (i = 0; i < nmpu; i++) { cpu_load_t newload; newload.cl_system = info[CPU_STATE_SYSTEM + i * infosz]; newload.cl_user = info[CPU_STATE_USER + i * infosz]; newload.cl_nice = info[CPU_STATE_NICE + i * infosz]; newload.cl_idle = info[CPU_STATE_IDLE + i * infosz]; if (!firstrun) { int delta_system = newload.cl_system - cpu_load[i].cl_system; int delta_user = newload.cl_user - cpu_load[i].cl_user; int delta_nice = newload.cl_nice - cpu_load[i].cl_nice; int delta_idle = newload.cl_idle - cpu_load[i].cl_idle; int used = delta_system + delta_user + delta_nice; int percent = 100 * used / (used + delta_idle); fprintf(stdout, "%d ", percent); } memcpy(&cpu_load[i], &newload, sizeof (cpu_load_t)); } if (!firstrun) fprintf(stdout, "\n"); vm_deallocate(mach_task_self(), (vm_address_t)info, cnt); }
EXPORT int terminate_(mach_port_t task) { kern_return_t kret; pid_t pid; pid_for_task(task, &pid); kret = kill(pid, PT_KILL); mach_error("[-terminate] kill process status:" , kret); return 1; }
static void protSetupInner(void) { kern_return_t kr; int pr; pthread_t excThread; mach_port_t self; /* Create a port to send and receive exceptions. */ self = mach_task_self(); AVER(MACH_PORT_VALID(self)); kr = mach_port_allocate(self, MACH_PORT_RIGHT_RECEIVE, &protExcPort); AVER(kr == KERN_SUCCESS); if (kr != KERN_SUCCESS) mach_error("ERROR: MPS mach_port_allocate", kr); /* .trans.must */ AVER(MACH_PORT_VALID(protExcPort)); /* Allow me to send exceptions on this port. */ /* TODO: Find out why this is necessary. */ self = mach_task_self(); AVER(MACH_PORT_VALID(self)); kr = mach_port_insert_right(self, protExcPort, protExcPort, MACH_MSG_TYPE_MAKE_SEND); AVER(kr == KERN_SUCCESS); if (kr != KERN_SUCCESS) mach_error("ERROR: MPS mach_port_insert_right", kr); /* .trans.must */ ProtThreadRegister(TRUE); /* Launch the exception handling thread. We use pthread_create because it's much simpler than setting up a thread from scratch using Mach, and that's basically what it does. See [Libc] <http://www.opensource.apple.com/source/Libc/Libc-825.26/pthreads/pthread.c> */ pr = pthread_create(&excThread, NULL, protCatchThread, NULL); AVER(pr == 0); if (pr != 0) fprintf(stderr, "ERROR: MPS pthread_create: %d\n", pr); /* .trans.must */ }
extern void ProtThreadRegister(Bool setup) { kern_return_t kr; mach_msg_type_number_t old_exception_count = 1; exception_mask_t old_exception_masks; exception_behavior_t behavior; mach_port_t old_exception_ports; exception_behavior_t old_behaviors; thread_state_flavor_t old_flavors; mach_port_t self; static mach_port_t setupThread = MACH_PORT_NULL; self = mach_thread_self(); AVER(MACH_PORT_VALID(self)); /* Avoid setting up the exception handler for the thread that calls ProtSetup twice, in the case where the mutator registers that thread explicitly. We need a special case because we don't require thread registration of the sole thread of a single-threaded mutator. */ if (setup) { AVER(setupThread == MACH_PORT_NULL); setupThread = self; } else { AVER(setupThread != MACH_PORT_NULL); if (self == setupThread) return; } /* Ask to receive EXC_BAD_ACCESS exceptions on our port, complete with thread state and identity information in the message. The MACH_EXCEPTION_CODES flag causes the code fields to be passed 64-bits wide, matching protRequestStruct [Fuller_2013]. */ behavior = (exception_behavior_t)(EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES); AVER(MACH_PORT_VALID(protExcPort)); kr = thread_swap_exception_ports(self, EXC_MASK_BAD_ACCESS, protExcPort, behavior, THREAD_STATE_FLAVOR, &old_exception_masks, &old_exception_count, &old_exception_ports, &old_behaviors, &old_flavors); AVER(kr == KERN_SUCCESS); if (kr != KERN_SUCCESS) mach_error("ERROR: MPS thread_swap_exception_ports", kr); /* .trans.must */ AVER(old_exception_masks == EXC_MASK_BAD_ACCESS); AVER(old_exception_count == 1); AVER(old_exception_ports == MACH_PORT_NULL || old_exception_ports == protExcPort); /* .assume.only-port */ }
void unprotect_data_segment( void) { kern_return_t r; if((r = vm_protect(mach_task_self(), data_seg->vmaddr + dyld_image_vmaddr_slide, (vm_size_t)data_seg->vmsize, FALSE, data_seg->initprot)) != KERN_SUCCESS){ mach_error(r, "can't vm_(un)protect data segment of dyld"); link_edit_error(DYLD_MACH_RESOURCE, r, "dyld"); } }
static void protMustSend(mach_msg_header_t *head) { kern_return_t kr; kr = mach_msg(head, MACH_SEND_MSG, head->msgh_size, /* recv_size */ 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); AVER(kr == KERN_SUCCESS); if (kr != KERN_SUCCESS) mach_error("ERROR: MPS mach_msg send", kr); /* .trans.must */ }
int mach_load_avg(void) { kern_return_t status; host_t host; unsigned int info_count; struct processor_set_basic_info info; processor_set_t default_set; status=processor_set_default(host_self(), &default_set); if (status!=KERN_SUCCESS) { mach_error("Error calling processor_set_default", status); exit(1); } info_count=PROCESSOR_SET_BASIC_INFO_COUNT; status=processor_set_info(default_set, PROCESSOR_SET_BASIC_INFO, &host, (processor_set_info_t)&info, &info_count); #ifdef DEBUG if (status != KERN_SUCCESS) mach_error("Error calling processor_set_info", status); #endif return info.load_average; }
static int S_remove_service(mach_port_t server, int argc, char * argv[]) { CFDataRef data = NULL; CFDictionaryRef dict; char * method_name; if_name_t if_name; kern_return_t kret; ipconfig_status_t status = ipconfig_status_success_e; void * xml_data_ptr = NULL; int xml_data_len = 0; strlcpy(if_name, argv[0], sizeof(if_name)); method_name = argv[1]; argv += 2; argc -= 2; dict = ConfigDictCreate(if_name, argc, argv, command_name, method_name, FALSE); if (dict == NULL) { return (1); } data = CFPropertyListCreateData(NULL, dict, kCFPropertyListBinaryFormat_v1_0, 0, NULL); if (data == NULL) { CFRelease(dict); fprintf(stderr, "failed to allocate memory\n"); return (1); } xml_data_ptr = (void *)CFDataGetBytePtr(data); xml_data_len = (int)CFDataGetLength(data); kret = ipconfig_remove_service(server, if_name, xml_data_ptr, xml_data_len, &status); my_CFRelease(&dict); my_CFRelease(&data); if (kret != KERN_SUCCESS) { mach_error("ipconfig_remove_service failed", kret); return (1); } if (status != ipconfig_status_success_e) { fprintf(stderr, "ipconfig_remove_service %s %s failed: %s\n", if_name, method_name, ipconfig_status_string(status)); return (1); } return (0); }
kern_return_t catch_mach_exception_raise(mach_port_t exception_port, mach_port_t thread, mach_port_t task, exception_type_t type, exception_data_t code, mach_msg_type_number_t code_count) { x86_thread_state_t state; kern_return_t kr; mach_msg_type_number_t count = x86_THREAD_STATE_COUNT; int insn_len = 0; if ((kr = thread_get_state(thread, x86_THREAD_STATE, (thread_state_t)&state, &count)) != KERN_SUCCESS) { mach_error("thread_get_state", kr); goto bad; } insn_len = instruction_length_at_address(state.uts.ts64.__rip); if (insn_len == 0 || insn_len == -1) goto bad; printf("Got exception type %d at %#02llx insn_len: %d\n", type, state.uts.ts64.__rip, insn_len); state.uts.ts64.__rip += insn_len; count = x86_THREAD_STATE_COUNT; if ((kr = thread_set_state(thread, x86_THREAD_STATE, (thread_state_t)&state, count)) != KERN_SUCCESS) { mach_error("thread_set_state", kr); goto bad; } return KERN_SUCCESS; bad: return KERN_FAILURE; }
int my_munmap(caddr_t addr, size_t len) { kern_return_t ret_val; ret_val = vm_deallocate(task_self(), (vm_address_t) addr, (vm_size_t) len); if (ret_val != KERN_SUCCESS) { mach_error("vm_deallocate in munmap()", ret_val); return -1; } return 0; }