コード例 #1
0
ファイル: pack_alone.c プロジェクト: AleksandraButrova/embox
/**
 * @brief Alloc pnet simple packet with content
 *
 * @param data Content pointer to store to packet
 * @param len Content lenght
 *
 * @return Pointer to allocated packet
 */
static struct pnet_pack *pnet_pack_alloc(void *data, size_t len) {
	struct pnet_pack *pack;
	struct pnet_pack_data *pack_data;

	if (len > PNET_PACK_MAX_DATA_LEN) {
		return NULL;
	}

	if (!(pack = objalloc(&net_packs))) {
		return NULL;
	}

	if (!(pack_data = objalloc(&net_packs_data))) {
		objfree(&net_packs,pack);
		return NULL;
	}

	pack->data = pack_data;

	pack->dir = PNET_PACK_DIRECTION_RX; //TODO varios packet types

	if (data) {
		memcpy(pack_data->buff,data,len);
	}

	pack_data->len = len;

	pack->type = PNET_PACK_TYPE_SINGLE;

	pack->priority = 0;

	return pack;
}
コード例 #2
0
ファイル: thread.c プロジェクト: Distrotech/dtsapplib
/** @brief Initialise the threadlist and start manager thread
  *
  * @note There is no need to call this as it will start when first thread starts.
  * @returns 1 On success 0 on failure.*/
extern int startthreads(void) {
	struct threadcontainer *tc;

	tc = (objref(threads)) ? threads : NULL;

	if (tc) {
		objunref(tc);
		return 1;
	}

	if (!(tc = objalloc(sizeof(*threads), close_threads))) {
		return 0;
	}

	if (!tc->list && !(tc->list = create_bucketlist(4, hash_thread))) {
		objunref(tc);
		return 0;
	}

	threads = tc;
	if (!(tc->manager = framework_mkthread(managethread, manage_clean, manager_sig, NULL, THREAD_OPTION_JOINABLE | THREAD_OPTION_RETURN))) {
		objunref(tc);
		return 0;
	}

	return 1;
}
コード例 #3
0
ファイル: openldap.c プロジェクト: Distrotech/dtsapplib
/** @brief Bind to the connection with simple bind requireing a distingushed name and password.
  * @param ld LDAP connection to bind to.
  * @param dn Distinguished name to bind with.
  * @param passwd Password for dn.
  * @returns -1 on error.*/
extern int ldap_simplebind(struct ldap_conn *ld, const char *dn, const char *passwd) {
	struct ldap_simple *simple;
	struct berval *cred;
	int res, len = 0;

	if (!objref(ld)) {
		return LDAP_UNAVAILABLE;
	}

	if (passwd) {
		len = strlen(passwd);
	}
	simple = objalloc(sizeof(*simple), free_simple);
	cred = calloc(sizeof(*cred), 1);
	cred->bv_val = malloc(len);
	memcpy(cred->bv_val, passwd, len);
	cred->bv_len=len;
	simple->cred = cred;
	simple->dn = strdup(dn);

	objlock(ld);
	if (ld->simple) {
		objunref(ld->simple);
	}
	ld->simple = simple;
	res = ldap_sasl_bind_s(ld->ldap, simple->dn, LDAP_SASL_SIMPLE, simple->cred, ld->sctrlsp, NULL, NULL);
	objunlock(ld);
	objunref(ld);
	return res;
}
コード例 #4
0
ファイル: thread.c プロジェクト: Distrotech/dtsapplib
/** @brief create a thread result must be unreferenced
  *
  * @note If the manager thread has not yet started this will start the manager thread.
  * @warning @ref THREAD_OPTION_RETURN flag controls the return of this function.
  * @warning Threads should periodically check the result of framework_threadok() and cleanup or use @ref THREAD_OPTION_CANCEL
  * @param func Function to run thread on.
  * @param cleanup Cleanup function to run.
  * @param sig_handler Thread signal handler.
  * @param data Data to pass to callbacks.
  * @param flags Options of @ref thread_option_flags passed
  * @returns a thread structure that must be un referencend OR NULL depending on flags.*/
extern struct thread_pvt *framework_mkthread(threadfunc func, threadcleanup cleanup, threadsighandler sig_handler, void *data, int flags) {
	struct thread_pvt *thread;
	struct threadcontainer *tc = NULL;

	/*Grab a reference for threads in this scope start up if we can*/
	if (!(tc = (objref(threads)) ? threads : NULL)) {
		if (!thread_can_start) {
			return NULL;
		} else if (!startthreads()) {
			return NULL;
		}
		if (!(tc = (objref(threads)) ? threads : NULL)) {
			return NULL;
		}
	}

	objlock(tc);
	/* dont allow threads if no manager or it not started*/
	if ((!tc->manager || !func) && (func != managethread)) {
		/*im shuting down*/
		objunlock(tc);
		objunref(tc);
		return NULL;
	} else if (!(thread = objalloc(sizeof(*thread), free_thread))) {
		/* could not create*/
		objunlock(tc);
		objunref(tc);
		return NULL;
	}

	thread->data = (objref(data)) ? data : NULL;
	thread->flags = flags << 16;
	thread->cleanup = cleanup;
	thread->sighandler = sig_handler;
	thread->func = func;
	objunlock(tc);

	/* start thread and check it*/
	if (pthread_create(&thread->thr, NULL, threadwrap, thread) || pthread_kill(thread->thr, 0)) {
		objunref(thread);
		objunref(tc);
		return NULL;
	}

	/*Activate the thread it needs to be flaged to run or it will die*/
	objlock(tc);
	addtobucket(tc->list, thread);
	setflag(thread, TL_THREAD_RUN);
	objunlock(tc);
	objunref(tc);

	if (testflag(thread, TL_THREAD_RETURN)) {
		return thread;
	} else {
		objunref(thread);
		return NULL;
	}
}
コード例 #5
0
ファイル: node.c プロジェクト: AleksandraButrova/embox
net_node_t pnet_node_alloc(net_addr_t addr, pnet_proto_t proto) {
	net_node_t node;
	if (NULL == proto || NULL == proto->actions.alloc) {
		node = (net_node_t) objalloc(&net_nodes);
	} else {
		node = proto->actions.alloc();
	}

	pnet_node_init(node, proto);

	return node;

}
コード例 #6
0
ファイル: openldap.c プロジェクト: Distrotech/dtsapplib
/** @brief Bind to the server with SASL
  * @param ld Reference to LDAP connection.
  * @param mech SASL mechanisim.
  * @param realm SASL realm.
  * @param authcid SASL auth id.
  * @param passwd Password for authid.
  * @param authzid Proxy authid.
  * @returns -1 on error.*/
extern int ldap_saslbind(struct ldap_conn *ld, const char *mech, const char *realm, const char *authcid, const char *passwd, const char *authzid ) {
	struct sasl_defaults *sasl;
	int res, sasl_flags = LDAP_SASL_AUTOMATIC | LDAP_SASL_QUIET;

	if (!objref(ld)) {
		return LDAP_UNAVAILABLE;
	}

	if (!(sasl = objalloc(sizeof(*sasl), free_sasl))) {
		return LDAP_NO_MEMORY;
	}

	ALLOC_CONST(sasl->passwd, passwd);

	if (mech) {
		ALLOC_CONST(sasl->mech, mech);
	} else {
		ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_MECH, &sasl->mech);
	}

	if (realm) {
		ALLOC_CONST(sasl->realm, realm);
	} else {
		ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_REALM, &sasl->realm );
	}

	if (authcid) {
		ALLOC_CONST(sasl->authcid, authcid);
	} else {
		ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_AUTHCID, &sasl->authcid );
	}

	if (authzid) {
		ALLOC_CONST(sasl->authzid, authzid);
	} else {
		ldap_get_option(ld->ldap, LDAP_OPT_X_SASL_AUTHZID, &sasl->authzid );
	}

	objlock(ld);
	if (ld->sasl) {
		objunref(ld->sasl);
	}
	ld->sasl = sasl;
	res = ldap_sasl_interactive_bind_s(ld->ldap, NULL, sasl->mech, ld->sctrlsp , NULL, sasl_flags, dts_sasl_interact, sasl);
	objunlock(ld);
	objunref(ld);
	return res;
}
コード例 #7
0
ファイル: openldap.c プロジェクト: Distrotech/dtsapplib
/** @brief Connect to a LDAP server.
  * @param uri Server to connect too.
  * @param starttls Starttls flags to disallow,allow or enforce SSL.
  * @param timelimit Query timelimit.
  * @param limit Results limit.
  * @param debug Set LDAP_OPT_DEBUG_LEVEL and LBER_OPT_DEBUG_LEVEL to this level.
  * @param err Pointer to a int that will contain the ldap error on failure.
  * @returns Reference to LDAP connection if its NULL the error is returned in err.*/
extern struct ldap_conn *ldap_connect(const char *uri, enum ldap_starttls starttls, int timelimit, int limit, int debug, int *err) {
	struct ldap_conn *ld;
	int version = 3;
	int res, sslres;
	struct timeval timeout;

	if (!(ld = objalloc(sizeof(*ld), free_ldapconn))) {
		return NULL;
	}

	ld->uri = strdup(uri);
	ld->sctrlsp = NULL;
	ld->timelim = timelimit;
	ld->limit = limit;
	ld->sasl = NULL;

	if ((res = ldap_initialize(&ld->ldap, ld->uri) != LDAP_SUCCESS)) {
		objunref(ld);
		ld = NULL;
	} else {
		if (debug) {
			ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &debug);
			ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &debug);
		}
		if (timelimit) {
			timeout.tv_sec = timelimit;
			timeout.tv_usec = 0;
			ldap_set_option(ld->ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *)&timeout);
		}
		ldap_set_option(ld->ldap, LDAP_OPT_PROTOCOL_VERSION, &version);
		ldap_set_option(ld->ldap, LDAP_OPT_REFERRALS, (void *)LDAP_OPT_ON);
		ldap_set_rebind_proc(ld->ldap, ldap_rebind_proc, ld);

		if ((starttls != LDAP_STARTTLS_NONE) & !ldap_tls_inplace(ld->ldap) && (sslres = ldap_start_tls_s(ld->ldap, ld->sctrlsp, NULL))) {
			if (starttls == LDAP_STARTTLS_ENFORCE) {
				objunref(ld);
				ld = NULL;
				res = sslres;
			}
		}
	}
	*err = res;
	return ld;
}
コード例 #8
0
ファイル: ext3.c プロジェクト: kandeshvari/embox
static int ext3fs_mount(void *dev, void *dir) {
	struct fs_driver *drv;
	struct ext2fs_dinode *dip = sysmalloc(sizeof(struct ext2fs_dinode));
	char buf[SECTOR_SIZE * 2];
	struct ext2_fs_info *fsi;
	int inode_sector, ret, rsize;
	struct node *dev_node = dev;
	struct nas *dir_nas = ((struct node *)dir)->nas;
	journal_t *jp = NULL;
	ext3_journal_specific_t *ext3_spec;
	journal_fs_specific_t spec = {
			.bmap = ext3_journal_bmap,
			.commit = ext3_journal_commit,
			.update = ext3_journal_update,
			.trans_freespace = ext3_journal_trans_freespace
	};

	if (NULL == (drv = fs_driver_find_drv(EXT2_NAME))) {
		return -1;
	}

	if ((ret = drv->fsop->mount(dev, dir)) < 0) {
		return ret;
	}

	if (NULL == (ext3_spec = objalloc(&ext3_journal_cache))) {
		return -1;
	}

	spec.data = ext3_spec;

	if (NULL == (jp = journal_create(&spec))) {
		objfree(&ext3_journal_cache, ext3_spec);
		return -1;
	}

	/* Getting first block for inode number EXT3_JOURNAL_SUPERBLOCK_INODE */
	dir_nas = ((struct node *)dir)->nas;
	fsi = dir_nas->fs->fsi;

	inode_sector = ino_to_fsba(fsi, EXT3_JOURNAL_SUPERBLOCK_INODE);

	rsize = ext2_read_sector(dir_nas, buf, 1, inode_sector);
	if (rsize * fsi->s_block_size != fsi->s_block_size) {
		return -EIO;
	}

	/* set pointer to inode struct in read buffer */
	memcpy(dip, (buf
			+ EXT2_DINODE_SIZE(fsi) * ino_to_fsbo(fsi, EXT3_JOURNAL_SUPERBLOCK_INODE)),
			sizeof(struct ext2fs_dinode));

	/* XXX Hack to use ext2 functions */
	dir_nas->fs->drv = &ext3fs_driver;
	ext3_spec->ext3_journal_inode = dip;
	if (0 > ext3_journal_load(jp, (struct block_dev *) dev_node->nas->fi->privdata,
			fsbtodb(fsi, dip->i_block[0]))) {
		return -EIO;
	}
	/*
	 * FIXME Now journal supports block size only equal to filesystem block size
	 * It is not critical but not flexible enough
	 */
	assert(jp->j_blocksize == fsi->s_block_size);
	fsi->journal = jp;

	return 0;
}