[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ltp-cvs
Subject:    [Ltp-cvs] ltp/testcases/kernel/syscalls/sendfile sendfile04.c, NONE,
From:       Subrata <subrata_modak () users ! sourceforge ! net>
Date:       2007-11-28 11:20:06
Message-ID: E1IxKxc-0000pU-3v () mail ! sourceforge ! net
[Download RAW message or body]

Update of /cvsroot/ltp/ltp/testcases/kernel/syscalls/sendfile
In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv24572/ltp/testcases/kernel/syscalls/sendfile


Modified Files:
	Makefile sendfile02.c sendfile03.c 
Added Files:
	sendfile04.c 
Log Message:
Added sendfile64 support and new testcase sendfile04.c in this regard, by Masatake \
YAMATO <yamato@redhat.com>


--- NEW FILE: sendfile04.c ---
/*
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *   Copyright (c) Red Hat Inc., 2007
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * NAME
 *	sendfile04.c
 *
 * DESCRIPTION
 *	Testcase to test that sendfile(2) system call returns EFAULT
 *	when passing wrong buffer.
 *
 * ALGORITHM
 *     Given wrong address or protected buffer as OFFSET argument to sendfile.
 *     A wrong address is created by munmap a buffer allocated by mmap.
 *     A protected buffer is created by mmap with specifying protection.
 *
 * USAGE:  <for command-line>
 *  sendfile03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
 *     where,  
 *             -f   : Turn off functionality Testing.
 *             -i n : Execute test n times.
 *             -I x : Execute test for x seconds.
 *             -P x : Pause for x seconds between iterations.
 *             -t   : Turn on syscall timing.
 *
 * HISTORY
 *	11/2007 Copyed from sendfile02.c by Masatake YAMATO
 *
 * RESTRICTIONS
 *	NONE
 */
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "usctest.h"
#include "test.h"

#ifndef OFF_T
#define OFF_T off_t
#endif /* Not def: OFF_T */


char *TCID = "sendfile04";
extern int Tst_count;

char in_file[100];
char out_file[100];
int out_fd;
pid_t child_pid;
static int sockfd, s;
static struct sockaddr_in sin1; /* shared between do_child and create_server */

void cleanup(void);
void do_child(void);
void setup(void);
int create_server(void);

#define PASS_MAPPED_BUFFER 0
#define PASS_UNMAPPED_BUFFER 1

struct test_case_t {
	int  protection;
	int  pass_unmapped_buffer;
} testcases[] = {
	{ PROT_NONE,            PASS_MAPPED_BUFFER   },
	{ PROT_READ,            PASS_MAPPED_BUFFER   },
	{ PROT_EXEC,            PASS_MAPPED_BUFFER   },
	{ PROT_EXEC|PROT_READ,  PASS_MAPPED_BUFFER   },
	{ PROT_READ|PROT_WRITE, PASS_UNMAPPED_BUFFER },
};

int TST_TOTAL = sizeof(testcases)/sizeof(testcases[0]);



#ifdef UCLINUX
static char* argv0;
#endif

void do_sendfile(int prot, int pass_unmapped_buffer)
{
	OFF_T *protected_buffer;
	int in_fd;
	struct stat sb;


	protected_buffer = mmap(NULL, 
			       sizeof(*protected_buffer), 
			       prot, 
			       MAP_PRIVATE|MAP_ANONYMOUS, 
			       -1, 0);
	if (protected_buffer == MAP_FAILED) {
		tst_brkm(TBROK, cleanup, "mmap failed: %d", errno);
		/*NOTREACHED*/
	}

	out_fd = create_server();

	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
		/*NOTREACHED*/
	}
	if (stat(in_file, &sb) < 0) {
		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
		/*NOTREACHED*/
	}


	if (pass_unmapped_buffer) {
		if (munmap(protected_buffer, sizeof(*protected_buffer)) < 0) {
			tst_brkm(TBROK, cleanup, "munmap failed: %d", errno);
			/*NOTREACHED*/
		}
	}

	TEST(sendfile(out_fd, in_fd, protected_buffer, sb.st_size));

	if (TEST_RETURN != -1) {
		tst_resm(TFAIL, "call succeeded unexpectedly");
	} else {
		TEST_ERROR_LOG(TEST_ERRNO);

		if (TEST_ERRNO != EFAULT) {
			tst_resm(TFAIL, "sendfile returned unexpected"
				 "errno, expected: %d, got: %d",
				 EFAULT, TEST_ERRNO);
		} else {
			tst_resm(TPASS, "sendfile() returned %d : %s",
				 TEST_ERRNO, strerror(TEST_ERRNO));
		}
	}

	shutdown(sockfd, SHUT_RDWR);
	shutdown(s, SHUT_RDWR);
	kill(child_pid, SIGKILL);
	close(in_fd);

	if (!pass_unmapped_buffer) {
		/* Not unmapped yet. So do it now.*/
		munmap(protected_buffer, sizeof(*protected_buffer));
	}
}

/*
 * do_child
 */
void
do_child()
{
	int lc;
	socklen_t length;
	char rbuf[4096];

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		length = sizeof(sin1);
		recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr*)&sin1, &length);
	}
	exit(0);
}

/*
 * setup() - performs all ONE TIME setup for this test.
 */
void
setup()
{
	int fd;
	char buf[100];

	/* capture signals */
	tst_sig(FORK, DEF_HANDLER, cleanup);

	/* Pause if that option was specified */
	TEST_PAUSE;

	/* make a temporary directory and cd to it */
	tst_tmpdir();
	sprintf(in_file, "in.%d", getpid());
	if ((fd = creat(in_file, 00700)) < 0) {
		tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
			 errno);
		/*NOTREACHED*/
	}
	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
	if (write(fd, buf, strlen(buf)) < 0) {
		tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
		/*NOTREACHED*/
	}
	close(fd);
	sprintf(out_file, "out.%d", getpid());
}

/*
 * cleanup() - performs all ONE TIME cleanup for this test at
 *	       completion or premature exit.
 */
void
cleanup()
{
	/*
	 * print timing stats if that option was specified.
	 * print errno log if that option was specified.
	 */
	TEST_CLEANUP;

	close(out_fd);
	/* delete the test directory created in setup() */
	tst_rmdir();

	/* exit with return code appropriate for results */
	tst_exit();
}

int create_server(void) {
	static int count=0;

	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
	if(sockfd < 0) {
		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
			strerror(errno));
		return -1;
	}
	sin1.sin_family = AF_INET;
	sin1.sin_port = htons((getpid() % 32768) + 11000 + count);
	sin1.sin_addr.s_addr = INADDR_ANY;
	count++;
	if(bind(sockfd, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) {
		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
			strerror(errno));
		return -1;
	}
	child_pid = FORK_OR_VFORK();
	if(child_pid < 0) {
		tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
			strerror(errno));
		return -1;
	}
	if(!child_pid) { /* child */
#ifdef UCLINUX
		if(self_exec(argv0, "") < 0) {
			tst_brkm(TBROK, cleanup, "self_exec failed");
			return -1;
			
		}
#else
		do_child();
#endif
	}

	s = socket(PF_INET, SOCK_DGRAM, 0);
	inet_aton("127.0.0.1", &sin1.sin_addr);
	if(s < 0) {
		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
			strerror(errno));
		return -1;
	}
	if (connect(s, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) {
		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
			strerror(errno));
	}
	return s;

}

int main(int ac, char **av)
{
	int i;
	int lc;				/* loop counter */
	char *msg;			/* parse_opts() return message */

	if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
		/*NOTREACHED*/
	}

#ifdef UCLINUX
	argv0 = av[0];
	maybe_run_child(&do_child, "");
#endif

	setup();

	/*
	 * The following loop checks looping state if -c option given
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {
		Tst_count = 0;

		for (i = 0; i < TST_TOTAL; ++i) {
			do_sendfile(testcases[i].protection,
				    testcases[i].pass_unmapped_buffer);
		}
	}
	cleanup();

	/*NOTREACHED*/
	return(0);
}


Index: sendfile03.c
===================================================================
RCS file: /cvsroot/ltp/ltp/testcases/kernel/syscalls/sendfile/sendfile03.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** sendfile03.c	28 Aug 2007 06:45:50 -0000	1.7
--- sendfile03.c	28 Nov 2007 11:20:04 -0000	1.8
***************
*** 56,59 ****
--- 56,64 ----
  #define FAILED 1
  
+ #ifndef OFF_T
+ #define OFF_T off_t
+ #endif /* Not def: OFF_T */
+ 
+ 
  char *TCID = "sendfile03";
  int TST_TOTAL = 3;
***************
*** 72,76 ****
  	int out_fd;
  	int in_fd;
! 	off_t *offset;
  	int count;
  	int exp_errno;
--- 77,81 ----
  	int out_fd;
  	int in_fd;
! 	OFF_T *offset;
  	int count;
  	int exp_errno;

Index: Makefile
===================================================================
RCS file: /cvsroot/ltp/ltp/testcases/kernel/syscalls/sendfile/Makefile,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Makefile	21 Aug 2006 06:58:44 -0000	1.10
--- Makefile	28 Nov 2007 11:20:04 -0000	1.11
***************
*** 20,25 ****
  LDLIBS += -L../../../../lib -lltp
  
! SRCS    = $(wildcard *.c)
! TARGETS = $(patsubst %.c,%,$(SRCS))
  
  all: $(TARGETS)
--- 20,30 ----
  LDLIBS += -L../../../../lib -lltp
  
! SRCS    = $(wildcard sendfile[0-9][0-9].c)
! TARGETS = $(patsubst %.c,%,$(SRCS)) $(patsubst %.c,%_64,$(SRCS))
! 
! %_64.c: %.c Makefile
! 	echo "#define _FILE_OFFSET_BITS 64" >  $@
! 	echo "#define OFF_T __off64_t"      >> $@
! 	cat  $< >> $@
  
  all: $(TARGETS)

Index: sendfile02.c
===================================================================
RCS file: /cvsroot/ltp/ltp/testcases/kernel/syscalls/sendfile/sendfile02.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** sendfile02.c	26 May 2006 06:35:44 -0000	1.10
--- sendfile02.c	28 Nov 2007 11:20:04 -0000	1.11
***************
*** 58,61 ****
--- 58,66 ----
  #include "test.h"
  
+ #ifndef OFF_T
+ #define OFF_T off_t
+ #endif /* Not def: OFF_T */
+ 
+ 
  char *TCID = "sendfile02";
  int TST_TOTAL = 4;
***************
*** 89,93 ****
  #endif
  
! void do_sendfile(off_t offset, int i)
  {
  	int in_fd;
--- 94,98 ----
  #endif
  
! void do_sendfile(OFF_T offset, int i)
  {
  	int in_fd;


-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Ltp-cvs mailing list
Ltp-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-cvs


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic