File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / proc.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Oct 4 22:37:46 2011 UTC (12 years, 9 months ago) by misho
Branches: misho
CVS tags: start, ansh1_0
ansh ELWIX remote managment

/*************************************************************************
 * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
 *  by Michael Pounov <misho@elwix.org>
 *
 * $Author: misho $
 * $Id: proc.c,v 1.1.1.1 2011/10/04 22:37:46 misho Exp $
 *
 *************************************************************************/
#include "global.h"
#include "anshd.h"
#include "proc.h"


struct tagProc *
InitProc(int h, struct sockaddr *sa, u_short id, int len)
{
	struct tagProc *proc;

	FTRACE(5);

	proc = malloc(sizeof(struct tagProc));
	if (!proc) {
		ERR("Not enough memory #%d - %s", errno, strerror(errno));
		return NULL;
	} else
		memset(proc, 0, sizeof(struct tagProc));
	proc->proc_sock = h;
	proc->proc_id = id;
	if (sa)
		memcpy(&proc->proc_cli, sa, sizeof(struct sockaddr));

	proc->proc_blen = len;
	proc->proc_buf_[0] = malloc(proc->proc_blen);
	if (!proc->proc_buf_[0]) {
		free(proc);
		proc = NULL;
	}
	proc->proc_buf_[1] = malloc(proc->proc_blen);
	if (!proc->proc_buf_[1]) {
		free(proc->proc_buf_[0]);
		free(proc);
		proc = NULL;
	}

	SLIST_INSERT_HEAD(&pH, proc, proc_next);
	return proc;
}

void
FiniProcByID(u_short id)
{
	struct tagProc *proc;

	SLIST_FOREACH(proc, &pH, proc_next)
		if (proc->proc_id == id) {
			SLIST_REMOVE(&pH, proc, tagProc, proc_next);
			FreeProc(&proc);
		}
}

void
FreeProc(struct tagProc ** __restrict proc)
{
	FTRACE(5);

	assert(proc && *proc);
	if (!*proc)
		return;

	if ((*proc)->proc_buf_[1])
		free((*proc)->proc_buf_[1]);
	if ((*proc)->proc_buf_[0])
		free((*proc)->proc_buf_[0]);
	free(*proc);
	*proc = NULL;
}

void
DestroyProc()
{
	struct tagProc *proc;

	while ((proc = SLIST_FIRST(&pH))) {
		SLIST_REMOVE_HEAD(&pH, proc_next);
		FreeProc(&proc);
	}
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>