示例#1
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;
}
示例#2
0
uint8_t *bootp_get_opt(struct bootphdr *bph, uint8_t opt_type) {
	int ret;
	uint8_t *tag;
	uint8_t *tag_len;

	ret = bootp_valid(bph, BOOTPREPLY);
	if (ret != 0) {
		return err_ptr(-ret);
	}

	tag = &bph->vend[sizeof(dhcp_magic_cookie)];
	while (tag < &bph->vend[sizeof(bph->vend)]) {
		switch(*tag) {
		default:
			if (*tag == opt_type) {
				return tag + 2;
			}
			tag++;
			tag_len = tag;
			tag += (*tag_len + 1);
			break;
		case TAG_PAD:
			tag++;
			break;
		case TAG_END:
			return NULL;
		}
	}

	return NULL;
}
示例#3
0
/*
 * file_operation
 */
static struct idesc *ext3fs_open(struct node *node, struct file_desc *desc, int flags) {
	struct fs_driver *drv;

	if(NULL == (drv = fs_driver_find_drv(EXT2_NAME))) {
		return err_ptr(EINVAL);
	}

	return drv->file_op->open(node, desc, flags);
}
示例#4
0
static struct idesc *uart_fsop_open(struct node *node, struct file_desc *file_desc, int flags)  {
	struct uart *uart;
	struct idesc *idesc;
	int res;

	uart = uart_dev_lookup(node->name);
	if (!uart) {
		return err_ptr(ENOENT);
	}
	idesc = idesc_serial_create(uart, flags);
	if (err(idesc)) {
		return idesc;
	}
	res = uart_open(uart);
	if (res) {
		return err_ptr(-res);
	}

	return idesc;
}
示例#5
0
static struct idesc *uart_fsop_open(struct inode *node, struct idesc *desc) {
	struct device_module *cdev;
	struct idesc *idesc;
	int res;

	cdev = node->i_data;
	idesc = idesc_serial_create(cdev->dev_data, 0);
	if (err(idesc)) {
		return idesc;
	}
	res = uart_open(cdev->dev_data);
	if (res) {
		return err_ptr(-res);
	}

	return idesc;
}
示例#6
0
struct sock *sock_create(int family, int type, int protocol) {
	int ret;
	struct sock *new_sk;
	const struct net_family *nfamily;
	const struct net_family_type *nftype;
	const struct net_sock *nsock;

	nfamily = net_family_lookup(family);
	if (nfamily == NULL) {
		return err_ptr(EAFNOSUPPORT);
	}

	nftype = net_family_type_lookup(nfamily, type);
	if (nftype == NULL) {
		return err_ptr(EPROTOTYPE);
	}

	nsock = net_sock_lookup(family, type, protocol);
	if (nsock == NULL) {
		return err_ptr(EPROTONOSUPPORT);
	}

	new_sk = sock_alloc(nftype->ops, nsock->ops);
	if (new_sk == NULL) {
		return err_ptr(ENOMEM);
	}

	sock_init(new_sk, family, type, nsock->protocol,
			nftype->ops, nsock->ops,
			nfamily->out_ops != NULL ? *nfamily->out_ops : NULL);

	assert(new_sk->f_ops != NULL);
	ret = new_sk->f_ops->init(new_sk);
	if (ret != 0) {
		sock_release(new_sk);
		return err_ptr(-ret);
	}

	assert(new_sk->p_ops != NULL);
	if (new_sk->p_ops->init != NULL) {
		ret = new_sk->p_ops->init(new_sk);
		if (ret != 0) {
			sock_close(new_sk);
			return err_ptr(-ret);
		}
	}

	sock_hash(new_sk);


	return new_sk;
}
示例#7
0
char *dns_set_nameserver(char *nameserver) {
	FILE *file;
	char buf[0x40];
	char *res;

	file = fopen(RESOLV_FILE, "rw");
	if (file == NULL) {
		return err_ptr(errno);
	}
	while(NULL != (res = fgets(buf, sizeof(buf), file))) {
		if (0 == strncmp(buf, "nameserver ", 11)) {
			int i;
			int len;

			/* remove line */
			len = strlen(buf);
			for(i = 0; i < len; i ++) {
				fwrite("\b", 1, 1, file);
			}

			fwrite(buf, 1, strlen(buf), file);

			strncpy(nameserver_ip, nameserver, sizeof(nameserver_ip)-1);
			nameserver_ip[sizeof(nameserver_ip) - 1] = '\0';

			fclose(file);

			return nameserver_ip;
		}
	}
	sprintf(buf, "nameserver %s\n", nameserver);
	fwrite(buf, 1, strlen(buf), file);

	strncpy(nameserver_ip, nameserver, sizeof(nameserver_ip)-1);
	nameserver_ip[sizeof(nameserver_ip) - 1] = '\0';

	fclose(file);

	return nameserver_ip;
}
示例#8
0
static struct idesc *tun_dev_open(struct node *node, struct file_desc *file_desc, int flags) {
	struct net_device *netdev;
	struct tun *tun;

	netdev = tun_netdev_by_node(node);
	if (!netdev) {
		return err_ptr(ENOENT);
	}

	tun = netdev_priv(netdev, struct tun);
	tun_user_lock(tun);

	waitq_init(&tun->wq);

	tun_krnl_lock(tun);
	{
		skb_queue_init(&tun->rx_q);
	}
	tun_krnl_unlock(tun);

	file_desc->file_info = netdev;

	return &file_desc->idesc;
}