Example #1
50
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);
    }
    
}
Example #2
0
static void
info_mach_threads_command (char *args, int from_tty)
{
  thread_array_t threads;
  unsigned int thread_count;
  kern_return_t result;
  task_t task;
  int i;

  task = get_task_from_args (args);
  if (task == TASK_NULL)
    return;

  result = task_threads (task, &threads, &thread_count);
  MACH_CHECK_ERROR (result);

  printf_unfiltered (_("Threads in task %#x:\n"), task);
  for (i = 0; i < thread_count; ++i)
    {
      printf_unfiltered (_("    %#x\n"), threads[i]);
      mach_port_deallocate (task_self (), threads[i]);
    }

  vm_deallocate (task_self (), (vm_address_t) threads,
                 (thread_count * sizeof (thread_t)));
}
Example #3
0
static void
info_mach_ports_command (char *args, int from_tty)
{
  port_name_array_t port_names_data;
  port_type_array_t port_types_data;
  unsigned int name_count, type_count;
  kern_return_t result;
  int index;
  task_t task;

  CHECK_ARGS ("Task", args);
  sscanf (args, "0x%x", &task);

  result = port_names (task,
                       &port_names_data,
                       &name_count, &port_types_data, &type_count);
  MACH_CHECK_ERROR (result);

  CHECK_FATAL (name_count == type_count);

  printf_unfiltered ("Ports for task %#x:\n", task);
  for (index = 0; index < name_count; ++index)
    {
      printf_unfiltered ("port name: %#x, type %#x\n",
                         port_names_data[index], port_types_data[index]);
    }

  vm_deallocate (task_self (), (vm_address_t) port_names_data,
                 (name_count * sizeof (mach_port_t)));
  vm_deallocate (task_self (), (vm_address_t) port_types_data,
                 (type_count * sizeof (mach_port_type_t)));
}
Example #4
0
int
main(int argc, char *argv[])
{
	int pid, fd1, fd2, len;
	char buf1[256], buf2[256];

	printf("FIFO test program\n");

	if (mknod("/mnt/fifo/test", (mode_t)(S_IFIFO | 0666), 0) == -1) {
		perror("mkfifo");
		exit(1);
	}
	pid = vfork();
	if (pid == -1) {
		perror("fork");
		exit(1);
	}
	if (pid == 0) {
		/* child */
		printf("child: task=%x\n", (int)task_self());

		fd1 = open("/mnt/fifo/test", O_RDONLY);
		if (fd1 == -1) {
			perror("open");
			exit(1);
		}
		for (;;) {
			printf("child: reading data from FIFO\n");
			len = read(fd1, buf1, sizeof(buf1) - 1);
			if (len == 0)
				break;
			buf1[len] = '\0';
			printf("child: length=%d data=%s\n", len, buf1);
		}
		close(fd1);
		printf("child: exit\n");
		exit(0);
	}
	printf("parent: task=%x\n", (int)task_self());

	fd2 = open("/mnt/fifo/test", O_WRONLY);
	if (fd2 == -1) {
		perror("open");
		exit(1);
	}
	for (;;) {
		printf("parent: please input string...\n");
		fgets(buf2, sizeof(buf2) - 1, stdin);
		if (feof(stdin))
			break;
		printf("parent: writing to FIFO\n");
		write(fd2, buf2, strlen(buf2));
	}
	close(fd2);
	printf("parent: exit\n");
	exit(0);
	return 0;
}
Example #5
0
File: main.c Project: uiv/Lerdu
int
main(int argc, char *argv[])
{
	char *prog, *cmd;
	struct cmdentry const *entry;
	int shcmd = 0;

	prog = basename(argv[0]);
	cmd = prog;

	/*
	 * Alias:
	 * 'sh'            => sh
	 * 'cmdbox'        => sh
	 * 'cmdbox sh'     => sh
	 * 'cmdbox cmd'    => cmd
	 * 'cmd' (symlink) => cmd
	 */
	if (!strcmp(prog, "sh"))
		shcmd = 1;
	else if (!strcmp(prog, "cmdbox")) {
		if (argc == 1)
			shcmd = 1;
		else {
			if (!strcmp(argv[1], "sh"))
				shcmd = 1;
			else
				cmd = argv[1];
			argv++;
			argc--;
		}
	}

	if (shcmd) {
		task_setname(task_self(), "sh");
		exit(sh_main(argc, argv));
	}

	entry = builtin_cmds;
	while (entry->cmd != NULL) {
		if (!strcmp(cmd, entry->cmd)) {
			task_setname(task_self(), cmd);
			exit(entry->func(argc, argv));
		}
		entry++;
	}
	fprintf(stderr, "No such command: %s\n", cmd);
	return 0;
}
Example #6
0
void
_exit(int status)
{

	for (;;)
		task_terminate(task_self());
}
Example #7
0
int exec_call(void) {
	int ecode;
	struct task *task = task_self();
	const char *path = task_resource_argv_path(task);
	const char *cmd_name = exec_cmd_name(path);
	const struct shell *sh = shell_lookup(cmd_name);
	int c;
	char **v;

	if (strcmp(cmd_name, path))
		task_resource_argv_insert(task, cmd_name, 0);

	c = task_resource_argv_argc(task);
	v = task_resource_argv_argv(task);

	/* FIXME pass argv to shell_exec */
	if (sh) {
		ecode = shell_run(sh);
	} else {
		const struct cmd *cmd;

		cmd = cmd_lookup(cmd_name);

		if (cmd) {
			task_self_module_ptr_set(cmd2mod(cmd));
			ecode = cmd_exec(cmd, c, v);
		} else {
			ecode = ENOENT;
		}
	}

	return ecode;
}
static void exec_trampoline(void) {
	sched_unlock();

	kill(task_get_id(task_get_parent(task_self())), SIGCHLD);

	_exit(exec_call());
}
Example #9
0
struct uio_mem *
uio_map_iomem(struct uio_handle *handle, const char *name,
	      paddr_t phys, size_t size)
{
	struct uio_mem *mem;
	int ret;

	mem = malloc(sizeof(struct uio_mem));
	if (!mem)
		return NULL;

	mem->phys_addr = phys;
	mem->size = size;
	strncpy(mem->name, name, sizeof(name));
	ret = vm_map_phys(phys, size, &mem->addr);
	if (ret < 0)
		goto out_free_mem;

	ret = vm_attribute(task_self(), mem->addr, PROT_IO);
	if (ret < 0)
		goto out_free_mem;

	list_init(&mem->link);
	list_insert(&handle->mem, &mem->link);
	return mem;
out_free_mem:
	free(mem);
	return NULL;
}
Example #10
0
static bool mutex_callback_unlock (sync_object_handle_t _handle,
    error_t *_p_ecode)
{
    mutex_handle_t p_mutex = (mutex_handle_t) _handle;
    task_handle_t p_task = task_self ();

    // grab the mutex
    if (is_in_interrupt () || is_invalid_task (p_task)) {
        return ERROR_T (ERROR_MUTEX_INVCONTEXT);
    }
    if (p_mutex->owner_ != p_task) {
        *_p_ecode = ERROR_T (ERROR_MUTEX_NOTOWNER);
        return false;
    }
    if (p_mutex->reference_ > 0) {
        p_mutex->reference_ --;
        return true;
    }
    // restore owner's original priority
    if (p_mutex->inherited_) {
        p_mutex->inherited_ = false;
        task_priority_change (p_mutex->owner_, p_mutex->original_);
    }
    if (task_bitmap_is_empty (&p_mutex->object_.pending_bitmap_)) {
        // no task is pending on this mutex
        p_mutex->owner_ = null;
        return true;
    }
    return false;
}
Example #11
0
struct thread *thread_create(unsigned int flags, void *(*run)(void *), void *arg) {
	struct thread *t;
	int priority;

	/* check mutually exclusive flags */
	if ((flags & THREAD_FLAG_PRIORITY_LOWER)
			&& (flags & THREAD_FLAG_PRIORITY_HIGHER)) {
		return err_ptr(EINVAL);
	}

	if((flags & THREAD_FLAG_NOTASK) && !(flags & THREAD_FLAG_SUSPENDED)) {
		return err_ptr(EINVAL);
	}

	/* check correct executive function */
	if (!run) {
		return err_ptr(EINVAL);
	}

	/* calculate current thread priority. It can be change later with
	 * thread_set_priority () function
	 */
	priority = thread_priority_by_flags(flags);

	/* below we will work with thread's instances and therefore we need to
	 * lock scheduler (disable scheduling) to our structures is not be
	 * corrupted
	 */
	sched_lock();
	{
		/* allocate memory */
		if (!(t = thread_alloc())) {
			t = err_ptr(ENOMEM);
			goto out_unlock;
		}

		/* initialize internal thread structure */
		thread_init(t, priority, run, arg);

		/* link with task if needed */
		if (!(flags & THREAD_FLAG_NOTASK)) {
			task_thread_register(task_self(), t);
		}

		thread_cancel_init(t);

		if (!(flags & THREAD_FLAG_SUSPENDED)) {
			thread_launch(t);
		}

		if (flags & THREAD_FLAG_DETACHED) {
			thread_detach(t);
		}

	}
out_unlock:
	sched_unlock();

	return t;
}
Example #12
0
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);
}
Example #13
0
/*	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;
}
Example #14
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;
}
Example #15
0
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;
}
Example #16
0
File: fork.c Project: AndrewD/prex
void
vfork_end(struct proc *p)
{
	void *stack;
	task_t self = task_self();

	DPRINTF(("vfork_end: org=%x saved=%x\n", p->p_stackbase,
		 p->p_stacksaved));
	/*
	 * Restore parent's stack
	 */
	if (vm_map(p->p_task, p->p_stackbase, USTACK_SIZE, &stack) != 0)
		return;

	memcpy(stack, p->p_stacksaved, USTACK_SIZE);

	vm_free(self, p->p_stacksaved);
	vm_free(self, stack);

	/*
	 * Resume parent
	 */
	p->p_vforked = 0;
	task_resume(p->p_task);
}
Example #17
0
int
uio_unmap_iomem(struct uio_handle *handle, struct uio_mem *mem)
{
	list_remove(&mem->link);
	vm_free(task_self(), (void *)mem->phys_addr);
	free(mem);
	return 0;
}
Example #18
0
static int get_index(struct sock *sk) {
	struct idesc_table *it;

	it = task_resource_idesc_table(task_self());
	assert(it);

	return idesc_table_add(it, (struct idesc *)sk, 0);
}
Example #19
0
void mapf_close( void )
{
    if (image_file >= 0)
    {
	vm_deallocate( task_self(), image_mapping_addr, image_mapping_size );
	close( image_file );
    }
}
static void
my_free(
	char *buf,
	vm_size_t size
	)
{
	vm_deallocate(task_self(), (vm_address_t)buf, size);
}
Example #21
0
void
attack(void)
{
	object_t *objp	= (object_t *)random();
	object_t obj	= (object_t)random();
	char *name	= (char *)random();
	void *msg	= (void *)random();
	size_t size	= (size_t)random();
	task_t self	= task_self();
	void *addr	= (void *)random();
	int attr	= random() & 7;
	thread_t t	= (thread_t)random();
	thread_t *tp	= (thread_t *)random();

	object_create(NULL, NULL);
	object_create(NULL, objp);
	object_create(name, NULL);
	object_create(name, objp);

	object_destroy(0);
	object_destroy(obj);

	object_lookup(NULL, objp);
	object_lookup(name, NULL);
	object_lookup(name, objp);

	msg_send(0, msg, size);
	msg_send(obj, NULL, size);
	msg_send(obj, msg, 0);
	msg_send(0, msg, 0);
	msg_send(0, NULL, size);
	msg_send(obj, msg, size);

	msg_receive(0, msg, size);
	msg_receive(obj, NULL, size);
	msg_receive(obj, msg, 0);
	msg_receive(0, msg, 0);
	msg_receive(0, NULL, size);
	msg_receive(obj, msg, size);

	msg_reply(0, msg, size);
	msg_reply(obj, NULL, size);
	msg_reply(obj, msg, 0);
	msg_reply(0, msg, 0);
	msg_reply(0, NULL, size);
	msg_reply(obj, msg, size);

	vm_allocate(self, addr, size, 1);
	vm_allocate(self, &addr, size, 1);

	vm_free(self, addr);
	vm_attribute(self, addr, attr);
	vm_map(self, addr, size, &addr);

	thread_create(self, tp);
	thread_suspend(t);
	thread_terminate(t);
}
Example #22
0
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);
    }
}
Example #23
0
void zombie(int sig, int code, struct sigcontext *scp) {
    memcpy(&OldContext, scp, (int)sizeof(struct sigcontext));
    LogMsg(0, 0, LogFile,  "****** INTERRUPTED BY SIGNAL %d CODE %d ******", sig, code);
    LogMsg(0, 0, LogFile,  "****** Aborting outstanding transactions, stand by...");
    
    LogMsg(0, 0, LogFile, "To debug via gdb: attach %d, setcontext OldContext", getpid());
    LogMsg(0, 0, LogFile, "Becoming a zombie now ........");
    task_suspend(task_self());
}
Example #24
0
static int xetpriority_match(int which, id_t who, struct task *task) {
	struct task_u_area *task_u_area;
	id_t real_who;
	int is_match;

	if (who != 0) {
		real_who = who;
	} else {
		struct task *self = task_self();
		struct task_u_area *self_u = task_resource_u_area(self);

		switch(which) {
		case PRIO_PROCESS:
			real_who = task_get_id(task_self());
			break;
		case PRIO_PGRP:
			real_who = self_u->regid;
			break;
		case PRIO_USER:
			real_who = self_u->reuid;
			break;
		default:
			assert(0);
		}
	}

	task_u_area = task_resource_u_area(task);

	switch (which) {
	case PRIO_PROCESS:
		is_match = task_get_id(task) == real_who;
		break;
	case PRIO_PGRP:
		is_match = task_u_area->regid == real_who;
		break;
	case PRIO_USER:
		is_match = task_u_area->reuid == real_who;
		break;
	default:
		assert(0);
	}

	return is_match;
}
Example #25
0
int
main(int argc, char *argv[])
{
	struct timerinfo info;
	task_t task;
	char stack[16];
	u_long start, end;
	int i, pri, error;

	printf("Benchmark to create/terminate %d threads\n", NR_THREADS);

	sys_info(INFO_TIMER, &info);
	if (info.hz == 0)
		panic("can not get timer tick rate");

	thread_getpri(thread_self(), &pri);
	thread_setpri(thread_self(), pri - 1);

	task = task_self();
	error = vm_allocate(task, (void **)&thread,
			    sizeof(thread_t) * NR_THREADS, 1);
	if (error)
		panic("vm_allocate is failed");

	sys_time(&start);

	/*
	 * Create threads
	 */
	for (i = 0; i < NR_THREADS; i++) {
		if (thread_create(task, &thread[i]) != 0)
			panic("thread_create is failed");

		if (thread_load(thread[i], null_thread, &stack) != 0)
			panic("thread_load is failed");

		if (thread_resume(thread[i]) != 0)
			panic("thread_resume is failed");
	}

	/*
	 * Teminate threads
	 */
	for (i = 0; i < NR_THREADS; i++)
		thread_terminate(thread[i]);

	sys_time(&end);

	vm_free(task, thread);

	printf("Complete. The score is %d msec (%d ticks).\n",
	       (int)((end - start) * 1000 / info.hz),
	       (int)(end - start));

	return 0;
}
kern_return_t SetServerAttribute(AttributeType attrib, UInt32 /*tagSize*/,
                                            void* /*tags*/, unsigned int bufSize, void* buffer)
{
    if ((attrib.attribClass != kServerAttr) || (attrib.version != kCurrentVersion))
    {
        //because the buffer is being passed out of line, make sure to free it up
        vm_deallocate(task_self(), (unsigned int)buffer, bufSize);
        return SCNoError;
    }

    kern_return_t theError = SCNoError;
        
    switch (attrib.attribKind)
    {
        case kRefuseConnectionsAttr:
        {
            if ((buffer != NULL) && (bufSize == sizeof(QTSRefuseConnectionsRec)))
                theError = SetRefuseConnections((QTSRefuseConnectionsRec*)buffer);
            else
                theError = SCBufferToSmall;
            break;
        }
        case kRereadPreferencesAttr:
        {
            theError = RereadPreferences();
            break;
        }
        case kLogRollAttr:
        {
            if ((buffer != NULL) && (bufSize == sizeof(QTSLogRollRec)))
                theError = RollLogNow((QTSLogRollRec*)buffer);
            else
                theError = SCBufferToSmall;
            break;
        }
        default:
            theError = SCUnsupportedAttrib;
    }

    vm_deallocate(task_self(), (unsigned int)buffer, bufSize);
    return theError;    
}
Example #27
0
static int
ramfs_write(vnode_t vp, file_t fp, void *buf, size_t size, size_t *result)
{
	struct ramfs_node *np;
	off_t file_pos, end_pos;
	void *new_buf;
	size_t new_size;
	task_t task;

	*result = 0;
	if (vp->v_type == VFIFO)
		return ramfs_write_fifo(vp, fp, buf, size, result);
	if (vp->v_type == VDIR)
		return EISDIR;
	if (vp->v_type != VREG)
		return EINVAL;

	np = vp->v_data;
	/* Check if the file position exceeds the end of file. */
	end_pos = vp->v_size;
	file_pos = (fp->f_flags & O_APPEND) ? end_pos : fp->f_offset;
	if (file_pos + size > (size_t)end_pos) {
		/* Expand the file size before writing to it */
		end_pos = file_pos + size;
		if (end_pos > (off_t)np->rn_bufsize) {
			task = task_self();
			/*
			 * We allocate the data buffer in page boundary.
			 * So that we can reduce the memory allocation unless
			 * the file size exceeds next page boundary.
			 * This will prevent the memory fragmentation by
			 * many malloc/free calls.
			 */
			new_size = PAGE_ALIGN(end_pos);
			if (vm_allocate(task, &new_buf, new_size, 1))
				return EIO;
			if (np->rn_size != 0) {
				memcpy(new_buf, np->rn_buf, vp->v_size);
				vm_free(task, np->rn_buf);
			}
			if (vp->v_size < (size_t)file_pos) /* sparse file */
				memset((char *)new_buf + vp->v_size, 0,
				       file_pos - vp->v_size);
			np->rn_buf = new_buf;
			np->rn_bufsize = new_size;
		}
		np->rn_size = end_pos;
		vp->v_size = end_pos;
	}
	memcpy(np->rn_buf + file_pos, buf, size);
	fp->f_offset += size;
	*result = size;
	return 0;
}
Example #28
0
int diag_fd(void) {
	struct idesc_table *idesc_table;

	idesc_table = task_resource_idesc_table(task_self());
	if (!idesc_table) {
		return -ENOSYS;
	}

	idesc_init(&diag_idesc, &diag_idx_ops, S_IROTH | S_IWOTH);

	return idesc_table_add(idesc_table, &diag_idesc, 0);
}
static char *
my_malloc(
	  vm_size_t size
	  )
{
	vm_address_t address;

	if (vm_allocate(task_self(), &address, size, TRUE) != KERN_SUCCESS) {
		return (NULL);
	}
	return ((char *)address);
}
Example #30
0
int diag_fd(void) {
	struct idesc_table *idesc_table;

	idesc_table = task_resource_idesc_table(task_self());
	if (!idesc_table) {
		return -ENOSYS;
	}

	idesc_init(&diag_idesc, &diag_idx_ops, FS_MAY_READ | FS_MAY_WRITE);

	return idesc_table_add(idesc_table, &diag_idesc, 0);
}