AF:
NF:0
PS:10
SRH:1
SFN:
DSR:
MID:<20070521141800.1107b3ea@ripper.onstor.net>
CFG:
PT:0
S:andy.sharp@onstor.com
RQ:
SSV:onstor-exch02.onstor.net
NSV:
SSH:
R:<tim.gardner@onstor.com>
MAID:1
X-Sylpheed-Privacy-System:
X-Sylpheed-Sign:0
SCF:#mh/Mailbox/sent
X-Sylpheed-End-Special-Headers: 1
Date: Mon, 21 May 2007 14:19:02 -0700
From: Andrew Sharp <andy.sharp@onstor.com>
To: Tim Gardner <tim.gardner@onstor.com>
Subject: example use of do_system function
Message-ID: <20070521141902.087b7844@ripper.onstor.net>
Organization: Onstor
X-Mailer: Sylpheed-Claws 2.6.0 (GTK+ 2.8.20; x86_64-pc-linux-gnu)
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=MP_WJFMXDnSXEjWnsBhT2VB0e.

--MP_WJFMXDnSXEjWnsBhT2VB0e.
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Tim,

As per our conversation, here is an example of how the do_system can be
used to replace fork/execl calls which are clumsy and use up a lot of
space.  I took the file ssc-vsd/vs-ipm-openbsd.c, and modified the
function vsd_addKernRt() to use do_system() instead of that other
stuff.  As you can see, it collapsed it down to 1 line from many more.
I even went so far as to wrap the call to do_system in a macro to show
Wencheng how to do that, because it's a common thing to do.  You can
find the file ssc-vsd/vs-ipm-openbsd.c in the cougar branch.  Max
basically copied the original vm-ipm.c to vs-ipm-openbsd.c and created a
vs-ipm-linux.c with all the fuctions empty.  He forgot to leave a
vs-ipm.c around but that's no problem, we'll create a new one.

vs-ipm.c example attached.  Hope it's helpful.

Cheers,

a

--MP_WJFMXDnSXEjWnsBhT2VB0e.
Content-Type: text/x-csrc; name=vs-ipm.c
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=vs-ipm.c

#include OS_INCL

/*-----------------------------------------------------------------
 * Routine     : vsd_addKernRt
 *
 * Description: Directly interact to kernel to add a route.
 *
 *-----------------------------------------------------------------
 */
static int32
vsd_addKernRt(uint32 ip, uint32 mask, uint32 nhop)
{
    int     pid;
    char    ip_str[IPM_STR_SIZE];
    char    mask_str[IPM_STR_SIZE];
    char    nhop_str[IPM_STR_SIZE];
    int     status;

    strncpy(ip_str, inet_ntoa(*(struct in_addr*)&ip), IPM_STR_SIZE);
    strncpy(mask_str, inet_ntoa(*(struct in_addr*)&mask), IPM_STR_SIZE);
    strncpy(nhop_str, inet_ntoa(*(struct in_addr*)&nhop), IPM_STR_SIZE);

	status = OS_ADD_ROUTE(ip_str, mask, mask_str, nhop_str);

	VSD_DBG_PRT(("%s[%u] : Add route completed with status=0x%x(%u)",
			 __FUNCTION__, __LINE__, status, WEXITSTATUS(status)));

	if (WEXITSTATUS(status)) {
		return NFX_ERR;
	}

	if (!ip && !mask) {  /* It's the default route */
		modify_readonly_file("/etc/mygate", string_to_file, nhop_str);
	}

    return NFX_OK;
}


vs-ipm-linux.h:

#define OS_ADD_ROUTE(A, B, C, D) \
	status = do_system("/sbin/route add %s %s %s %s", A, ntohl(B) & 1 ? "" : "-netmask", ntohl(B) & 1 ? "" : C, D);


vs-ipm-openbsd.h:

#define OS_ADD_ROUTE(A, B, C, D) \
	status = do_system("/sbin/route foo %s %s %s %s", A, ntohl(B) & 1 ? "" : "-netmask", ntohl(B) & 1 ? "" : C, D);

--MP_WJFMXDnSXEjWnsBhT2VB0e.--
