/*ARGSUSED*/
int
main(int argc, char *argv[])
{
	caddr_t mmapaddr, munmap_begin;
	long pagesize = sysconf(_SC_PAGE_SIZE);
	int i;
	time_t t;

	(void)time(&t);
	//(void)printf("%s: Started %s", argv[0], ctime(&t));
	if (sbrk(pagesize - ((u_long)sbrk(0)%(u_long)pagesize))==(char *)-1) {
		ERROR("couldn't round up brk to a page boundary");
                local_flag = FAILED;
                anyfail();
	}
	/* The brk is now at the begining of a page. */

	if ((munmap_begin = mmapaddr = (caddr_t)sbrk(0)) == (caddr_t)-1) {
		ERROR("couldn't find top of brk");
                local_flag = FAILED;
                anyfail();
	}
	mmapaddr = 0;
	/* burn level 2 ptes by spacing mmaps 4Meg apart */
	/* This should switch to large anonymous swap space granularity */
	for (i = 0; i < GRAN_NUMBER; i++) {
		if (mmap(mmapaddr, pagesize, PROT_READ|PROT_WRITE,
			MAP_ANONYMOUS|MAP_PRIVATE, 0, 0)==(caddr_t)-1)
		{
			ERROR("mmap failed");
                	local_flag = FAILED;
                	anyfail();
		}
		mmapaddr += NPTEPG*pagesize;
	}
	/* Free bizillion level2 ptes to switch to small granularity */
	if (munmap(munmap_begin, (size_t)(mmapaddr-munmap_begin))) {
		ERROR("munmap failed");
                local_flag = FAILED;
                anyfail();
	}
	(void)time(&t);
	//(void)printf("%s: Finished %s", argv[0], ctime(&t));
	ok_exit();
	tst_exit();
}
Exemple #2
0
int main(int argc, char *argv[])
{
	caddr_t mmapaddr;
	size_t pagesize = sysconf(_SC_PAGE_SIZE);
	time_t t;
	int sleep_time = 0;

	if (!argc) {
		(void)fprintf(stderr, "argc == 0\n");
		anyfail();
	}
	if (argc != 2 || !(sleep_time = atoi(argv[1]))) {
		(void)fprintf(stderr, "usage: %s sleep_time\n", argv[0]);
		anyfail();
	}
	(void)time(&t);
//      (void)printf("%s: Started %s", argv[0], ctime(&t));  LTP Port
	if (sbrk(pagesize - ((unsigned long) sbrk(0) & (pagesize - 1))) == (char *)-1) {
		ERROR("couldn't round up brk");
		anyfail();
	}
	if ((mmapaddr = sbrk(0)) == (char *)-1) {
		ERROR("couldn't find top of brk");
		anyfail();
	}
	/* mmapaddr is now on a page boundary after the brk segment */
	if (mmap
	    (mmapaddr, (ANON_GRAN_PAGES_MAX * NMFPTEPG + 1) * pagesize,
	     PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1,
	     0) == (caddr_t) - 1) {
		ERROR("large mmap failed");
		printf("for this test to run, it needs a mmap space of\n");
		printf("%d pages\n", (ANON_GRAN_PAGES_MAX * NMFPTEPG + 1));
		return 1;
	}
	(void)sleep(sleep_time);
	(void)time(&t);
//      (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
	ok_exit();
	tst_exit();
}
Exemple #3
0
int
main(int argc, char *argv[])
{
    char *progname;
    unsigned c;
    extern char *optarg;
    unsigned nprocs = 0;
    unsigned procno;
    pid_t *pidarray=NULL;
    pid_t pid;
    uchar_t *buf, *ptr;
    unsigned int seed;
    int alarmtime = 0;
    struct sigaction sa;
    unsigned i, j;
    uchar_t data;
    int no_prob = 0;
    time_t t;
    int wait_stat;

    progname = *argv;
    pagesize = sysconf(_SC_PAGE_SIZE);

    if (argc < 2) {
        (void)fprintf(stderr, "usage: %s %s\n", progname, usage);
        anyfail();
    }

    while ((c = getopt(argc, argv, "mdrp:t:s:")) != -1) {
        switch (c) {
        case 'd':
            debug = 1;
            break;
        case 't':
            alarmtime = atoi(optarg) * 60;
            break;
        case 'p':
            nprocs = atoi(optarg);
            break;
        case 'm':
            dosync = 1;
            break;
        case 's':
            mapsize = atoi(optarg);
            if (mapsize < 0) {
                (void)fprintf(stderr, "error: negative "
                              "mapsize\n");
                anyfail();
            }
            break;
        case 'r':
            randloops = 1;
            break;
        default:
            (void)fprintf(stderr, "usage: %s %s\n", progname,
                          usage);
            anyfail();
        }
    }

    /* nprocs is unsigned */
    if (nprocs > 255) {
        (void)fprintf(stderr, "invalid nprocs %d - (range 0-255)\n",
                      nprocs);
        anyfail();
    }
    (void)time(&t);
//	(void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port

    seed = initrand();
    pattern = seed & 0xff;

    if (debug) {
        (void)printf("%s mapsize %d bytes, pattern %d\n",
                     progname, mapsize, pattern);
        if (alarmtime)
            (void)printf("running for %d minutes\n", alarmtime/60);
        else
            (void)printf("running with no time limit\n");
    }

    if ((mapaddr = mmap(0, mapsize, PROT_READ|PROT_WRITE,
                        MAP_ANONYMOUS|MAP_SHARED, 0, 0))
            == (caddr_t)-1) {
        perror("mmap error");
        anyfail();
    }

    if ((buf = (uchar_t *)malloc(pagesize)) == NULL
            || (pidarray = (pid_t *)malloc(nprocs*sizeof(pid_t))) == NULL) {
        perror("malloc error");
        anyfail();
    }

    for (i = 0; i < nprocs; i++)
        *(pidarray+i) = 0;

    /*
     * Initialize page compare buffer, then initialize map.
     */

    for (i = 0, data = 0; i < pagesize; i++) {
        *(buf+i) = (data + pattern) & 0xff;
        if (++data == nprocs)
            data = 0;
    }

    mappages = roundup(mapsize, pagesize)/pagesize;
    ptr = (uchar_t *)mapaddr;

    for (i = 0; i < mappages; i++) {
        for (j = 0; j < pagesize; j++)
            *ptr++ = *(buf+j);
    }

    /*
     *  Fork off mmap children.
     */
    for (procno = 0; procno < nprocs; procno++) {
        switch (pid = fork()) {

        case -1:
            perror("fork error");
            goto cleanup;

        case 0:
            child_mapper(procno, nprocs);
            exit(0);

        default:
            pidarray[procno] = pid;
        }
    }

    /*
     *  Plan for death by signal.  User may have specified
     *  a time limit, in which set an alarm and catch SIGALRM.
     *  Also catch and cleanup with SIGINT.
     */
    sa.sa_handler = finish;
    sa.sa_flags = 0;
    if (sigemptyset(&sa.sa_mask)) {
        perror("sigemptyset error");
        goto cleanup;
    }

    if (sigaction(SIGINT, &sa, 0) == -1) {
        perror("sigaction error");
        goto cleanup;
    }

    if (alarmtime) {
        if (sigaction(SIGALRM, &sa, 0) == -1) {
            perror("sigaction error");
            goto cleanup;
        }
        (void)alarm(alarmtime);
    }

    /*
     *  Now wait for children and refork them as needed.
     */

    while (!finished) {
        do {
            pid = wait(&wait_stat);
        } while (pid == -1 && errno == EINTR);
        /*
         *  Block signals while processing child exit.
         */

        if (sighold(SIGALRM) || sighold(SIGINT)) {
            perror("sighold error");
            goto cleanup;
        }

        if (pid != -1) {
            /*
             *  Check exit status, then refork with the
             *  appropriate procno.
             */
            if (!WIFEXITED(wait_stat)
                    || WEXITSTATUS(wait_stat) != 0) {
                (void)fprintf(stderr, "child exit with err "
                              "<x%x>\n", wait_stat);
                goto cleanup;
            }
            for (i = 0; i < nprocs; i++)
                if (pid == pidarray[i])
                    break;
            if (i == nprocs) {
                (void)fprintf(stderr,
                              "unknown child pid %d, <x%x>\n",
                              pid, wait_stat);
                goto cleanup;
            }

            if ((pid = fork()) == -1) {
                perror("fork error");
                pidarray[i] = 0;
                goto cleanup;
            } else if (pid == 0) {		/* child */
                child_mapper(i, nprocs);
                exit(0);
            } else
                pidarray[i] = pid;
        } else {
            /*
             *  wait returned an error.  If EINTR, then
             *  normal finish, else it's an unexpected
             *  error...
             */
            if (errno != EINTR || !finished) {
                perror("unexpected wait error");
                goto cleanup;
            }
        }
        if (sigrelse(SIGALRM) || sigrelse(SIGINT)) {
            perror("sigrelse error");
            goto cleanup;
        }
    }

    /*
     *  Finished!  Check the map for sanity, then kill all
     *  the children and done!.
     */

    if (sighold(SIGALRM)) {
        perror("sighold error");
        goto cleanup;
    }
    (void)alarm(0);
    no_prob = 1;

cleanup:
    for (i = 0; i < nprocs; i++)
        (void)kill(pidarray[i], SIGKILL);	/* failure? oh well. */

    while (wait(&wait_stat) != -1 || errno != ECHILD)
        continue;

    if (no_prob) {		/* only check file if no errors */
        if (!mapokay(buf)) {
            (void)fprintf(stderr, "map data incorrect!\n");
            anyfail();
        }
        else
            (void)printf("map data okay\n");
    }

    (void)time(&t);
//	(void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP POrt
    ok_exit();
    return(0);
}
/*ARGSUSED*/
int
main(int argc, char *argv[])
{
	char *brk_max_addr, *hole_addr, *brk_start, *hole_start;
	size_t pagesize = (size_t)sysconf(_SC_PAGE_SIZE);
	time_t	t;

	progname = argv[0];

	(void)time(&t);
//	(void)printf("%s: Started %s", argv[0], ctime(&t));
	if ((brk_start = sbrk(0)) == NEG1) {
		ERROR("initial sbrk failed");
		anyfail();
	}
	if ((u_long)brk_start % (u_long)pagesize) {
		if (sbrk(pagesize - ((u_long)brk_start % (u_long)pagesize))
			== NEG1)
		{
			ERROR("couldn't round up brk to a page boundary");
        	        anyfail();
		}
	}
	/* The brk is now at the beginning of a page. */

	if ((hole_addr = hole_start = sbrk(NUM_SEGS * 2 * pagesize)) == NEG1) {
		ERROR("couldn't brk large space for segments");
                anyfail();
	}
	if ((brk_max_addr = sbrk(0)) == NEG1) {
		ERROR("couldn't find top of brk");
                anyfail();
	}
	do_test((caddr_t)brk_max_addr, pagesize);

	/* now make holes and repeat test */
	while (hole_addr + pagesize < brk_max_addr) {
		if (munmap(hole_addr, pagesize) == -1) {
			ERROR("failed to munmap odd hole in brk segment");
	                anyfail();
		}
		hole_addr += 2 * pagesize;
	}

	if (brk_max_addr != sbrk(0)) {
		ERROR("do_test should leave the top of brk where it began");
                anyfail();
	}
	do_test((caddr_t)brk_max_addr, pagesize);

	/* Shrink brk */
	if (sbrk(-NUM_SEGS * pagesize) == NEG1) {
		ERROR("couldn't brk back over holes");
                anyfail();
	}
	if ((brk_max_addr = sbrk(0)) == NEG1) {
		ERROR("couldn't find top of break again");
                anyfail();
	}
	/* sbrked over about half the holes */

	hole_addr = hole_start + pagesize; /* munmap the other pages */
	while (hole_addr + pagesize < brk_max_addr) {
		if (munmap(hole_addr, pagesize) == -1) {
			ERROR("failed to munmap even hole in brk segment");
        	        anyfail();
		}
		hole_addr += 2 * pagesize;
	}
	/* munmaped the rest of the brk except a little at the beginning */

	if (brk(brk_start) == -1) {
		ERROR("failed to completely remove brk");
                anyfail();
	}
	if (sbrk(pagesize) == NEG1 || sbrk(-pagesize) == NEG1) {
		ERROR("failed to fiddle with brk at the end");
                anyfail();
	}
	/* Ask for a ridiculously large mmap region at a high address */
	if (mmap((caddr_t)(1UL << (POINTER_SIZE  - 1)) - pagesize,
		(size_t)((1UL << (POINTER_SIZE - 1)) - pagesize),
		PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, 0, 0)
		!= (caddr_t)-1)
	{
		ERROR("really large mmap didn't fail");
                anyfail();
	}
	if (errno != ENOMEM && errno != EINVAL) {
		ERROR("really large mmap didn't set errno = ENOMEM nor EINVAL");
                anyfail();
	}
	(void)time(&t);
//	(void)printf("%s: Finished %s", argv[0], ctime(&t));
	ok_exit();
	tst_exit();
}
Exemple #5
0
int
main(int argc, char *argv[])
{
	char *progname;
	int fd;
	int c;
	extern char *optarg;
	unsigned nprocs = 0;
	unsigned procno;
	pid_t *pidarray=NULL;
	pid_t pid;
	pid_t wr_pid = 0;
	uchar_t *buf=NULL;
	unsigned int seed;
	int pagesize = sysconf(_SC_PAGE_SIZE);
	int alarmtime = 0;
	struct sigaction sa;
	unsigned i;
	int write_cnt;
	uchar_t data;
	int no_prob = 0;
	int wait_stat;
	time_t t;
#ifdef LARGE_FILE
	off64_t bytes_left;
#else /* LARGE_FILE */
	off_t bytes_left;
#endif /* LARGE_FILE */

	progname = *argv;
	tst_tmpdir();
	if (argc < 2) {
		(void)fprintf(stderr, "usage: %s %s\n", progname, usage);
		exit(1);
	}

	while ((c = getopt(argc, argv, "S:omdlrf:p:t:w:s:")) != -1) {
		switch (c) {
		case 'd':
			debug = 1;
			break;
		case 't':
			alarmtime = atoi(optarg) * 60;
			break;
		case 'p':
			nprocs = atoi(optarg);
			break;
		case 'l':
			leavefile = 1;
			break;
		case 's':
			sleeptime = atoi(optarg);
			if (sleeptime < 0) {
				(void)fprintf(stderr, "error: negative "
					"sleeptime\n");
                	        anyfail();
			}
			break;
		case 'w':
			growsize = atoi(optarg);
			if (growsize < 0) {
				(void)fprintf(stderr, "error: negative write "
					"size\n");
        	                anyfail();
			}
			break;
		case 'f':
#ifdef LARGE_FILE
			filesize = atoll(optarg);
#else /* LARGE_FILE */
			filesize = atoi(optarg);
#endif /* LARGE_FILE */
			if (filesize < 0) {
				(void)fprintf(stderr, "error: negative "
					"filesize\n");
                	        anyfail();
			}
			break;
		case 'r':
			randloops = 1;
			break;
		case 'm':
			dosync = 1;
			break;
		case 'o':
			do_offset = 1;
			break;
		case 'S':
#ifdef LARGE_FILE
			sparseoffset = atoll(optarg);
#else /* LARGE_FILE */
			sparseoffset = atoi(optarg);
#endif /* LARGE_FILE */
			if (sparseoffset % pagesize != 0) {
				fprintf(stderr, 
				   "sparseoffset must be pagesize multiple\n");
        	                anyfail();
			}
			break;
		default:
			(void)fprintf(stderr, "usage: %s %s\n", progname,
				usage);
			anyfail();
		}
	}

	if (nprocs > 255) {
		(void)fprintf(stderr, "invalid nprocs %d - (range 0-255)\n",
			nprocs);
                anyfail();
	}
	(void)time(&t);
	//(void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port

	(void)sprintf(filename, "%sout.%d", progname, getpid());
	seed = initrand();
	pattern = seed & 0xff;

	if (debug) {
#ifdef LARGE_FILE
		(void)printf("creating file <%s> with %Ld bytes, pattern %d\n", 
			filename, filesize, pattern);
#else /* LARGE_FILE */
		(void)printf("creating file <%s> with %ld bytes, pattern %d\n", 
			filename, filesize, pattern);
#endif /* LARGE_FILE */
		if (alarmtime)
			(void)printf("running for %d minutes\n", alarmtime/60);
		else
			(void)printf("running with no time limit\n");
	}

	/*
	 *  Plan for death by signal.  User may have specified
	 *  a time limit, in which case set an alarm and catch SIGALRM.
	 *  Also catch and cleanup with SIGINT, SIGQUIT, and SIGTERM.
	 */
	sa.sa_handler = finish;
	sa.sa_flags = 0;
	if (sigemptyset(&sa.sa_mask)) {
		perror("sigempty error");
		goto cleanup;
	}

	if (sigaction(SIGINT, &sa, 0) == -1) {
		perror("sigaction error SIGINT");
		goto cleanup;
	}
	if (alarmtime) {
		if (sigaction(SIGALRM, &sa, 0) == -1) {
			perror("sigaction error");
			goto cleanup;
		}
		(void)alarm(alarmtime);
	}
	/* If we get a SIGQUIT or SIGTERM, clean up and exit immediately. */
	sa.sa_handler = clean_up_file;
	if (sigaction(SIGQUIT, &sa, 0) == -1) {
		perror("sigaction error SIGQUIT");
		goto cleanup;
	}
	if (sigaction(SIGTERM, &sa, 0) == -1) {
		perror("sigaction error SIGTERM");
		goto cleanup;
	}

#ifdef LARGE_FILE
	if ((fd = open64(filename, O_CREAT|O_TRUNC|O_RDWR, 0664)) == -1) {
#else /* LARGE_FILE */
	if ((fd = open(filename, O_CREAT|O_TRUNC|O_RDWR, 0664)) == -1) {
#endif /* LARGE_FILE */
		perror("open error");
                anyfail();
	}

	if ((buf = (uchar_t *)malloc(pagesize+growsize)) == NULL
	    || (pidarray = (pid_t *)malloc(nprocs*sizeof(pid_t))) == NULL) {
		perror("malloc error");
                anyfail();
	}

	for (i = 0; i < nprocs; i++)
		*(pidarray+i) = 0;

	for (i = 0, data = 0; i < pagesize; i++) {
		*(buf+i) = (data + pattern) & 0xff;
		if (++data == nprocs)
			data = 0;
	}
	for (data = 0; i < pagesize+growsize; i++) {
		*(buf+i) = (data + pattern) & 0xff;
		if (++data == nprocs)
			data = 0;
	}

#ifdef LARGE_FILE
	if (lseek64(fd, sparseoffset, SEEK_SET) < 0) {
#else /* LARGE_FILE */
	if (lseek(fd, sparseoffset, SEEK_SET) < 0) {
#endif /* LARGE_FILE */
		perror("lseek");
               anyfail();
	}

	for (bytes_left = filesize; bytes_left; bytes_left -= c) {
		write_cnt = min(pagesize, bytes_left);
		if ((c = write(fd, (char *)buf, write_cnt)) != write_cnt) {
			if (c == -1) {
				perror("write error");
			} else {
				(void)fprintf(stderr, "write: wrote %d of %d "
					"bytes\n", c, write_cnt);
			}
			(void)close(fd);
			(void)unlink(filename);
			anyfail();
		}
	}

	(void)close(fd);

	/*
	 *  Fork off mmap children.
	 */
	for (procno = 0; procno < nprocs; procno++) {
		switch (pid = fork()) {

		case -1:
			perror("fork error");
			goto cleanup;

		case 0:
			child_mapper(filename, procno, nprocs);
			exit(0);

		default:
			pidarray[procno] = pid;
		}
	}

	/*
	 *  Now fork off an additional process to continually
	 *  write to (and grow) the file.
	 */
	if ((wr_pid = fork()) == -1) {
		perror("fork error");
		goto cleanup;
	} else if (wr_pid == 0) {	/* child */
		child_writer(filename, buf);
		exit(0);
	}

	/*
	 *  Now wait for children and refork them as needed.
	 */
	
	while (!finished) {
		pid = wait(&wait_stat);
		/*
		 *  Block signals while processing child exit.
		 */

		if (sighold(SIGALRM) || sighold(SIGINT)) {
			perror("sighold error");
			goto cleanup;
		}

		if (pid != -1) {
			/*
			 *  Check exit status, then refork with the
			 *  appropriate procno.
			 */
			if (!WIFEXITED(wait_stat) 
			    || WEXITSTATUS(wait_stat) != 0) {
				(void)fprintf(stderr, "child exit with err "
					"<x%x>\n", wait_stat);
				goto cleanup;
			}
			for (i = 0; i < nprocs; i++)
				if (pid == pidarray[i])
					break;
			if (i == nprocs) {
				if (pid == wr_pid) {
					(void)fprintf(stderr, 
					"writer child unexpected exit <x%x>\n",
						wait_stat);
					wr_pid = 0;
				} else
					(void)fprintf(stderr, "unknown child "
						"pid %d, <x%x>\n",
						pid, wait_stat);
				goto cleanup;
			}

			if ((pid = fork()) == -1) {
				perror("fork error");
				pidarray[i] = 0;
				goto cleanup;
			} else if (pid == 0) {		/* child */
				child_mapper(filename, i, nprocs);
				exit(0);
			} else
				pidarray[i] = pid;
		} else {
			/*
			 *  wait returned an error.  If EINTR, then
			 *  normal finish, else it's an unexpected
			 *  error...
			 */
			if (errno != EINTR || !finished) {
				perror("unexpected wait error");
				goto cleanup;
			}
		}
		if (sigrelse(SIGALRM) || sigrelse(SIGINT)) {
			perror("sigrelse error");
			goto cleanup;
		}
	}
	
	/*
	 *  Finished!  Check the file for sanity, then kill all
	 *  the children and done!.
	 */

	(void)alarm(0);
	no_prob = 1;

cleanup:
	for (i = 0; i < nprocs; i++)
		(void)kill(pidarray[i], SIGKILL);
	(void)kill(wr_pid, SIGKILL);

	while (wait(&wait_stat) != -1 || errno != ECHILD)
		continue;

	if (no_prob) {		/* only check file if no errors */
		if (!fileokay(filename, buf)) {
			(void)fprintf(stderr, "file data incorrect!\n");
			(void)printf("  leaving file <%s>\n", filename);
                        anyfail();

		} else {
			(void)printf("file data okay\n");
			if (!leavefile)
				(void)unlink(filename);
		}
	} else
		(void)printf("  leaving file <%s>\n", filename);
	
	(void)time(&t);
//	(void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
	ok_exit();
	return 0;
}


/*
 *  Child process that reads/writes map.  The child stats the file
 *  to determine the size, maps the size of the file, then reads/writes
 *  its own locations on random pages of the map (its locations being
 *  determined based on nprocs & procno).  After a specific number of
 *  iterations, it exits.
 */
void
child_mapper(char *file, unsigned procno, unsigned nprocs)
{
#ifdef LARGE_FILE
	struct stat64 statbuf;
	off64_t filesize;
	off64_t offset;
#else /* LARGE_FILE */
	struct stat statbuf;
	off_t filesize;
	off_t offset;
#endif /* LARGE_FILE */
	size_t validsize;
	size_t mapsize;
	caddr_t maddr, paddr;
	int fd;
	int pagesize = sysconf(_SC_PAGE_SIZE);
	unsigned randpage;
	unsigned int seed;
	unsigned loopcnt;
	unsigned nloops;
	unsigned mappages; 
	unsigned mapflags;
	unsigned i;

	mapflags = MAP_SHARED;

	seed = initrand();		/* initialize random seed */


#ifdef LARGE_FILE
	if ((fd = open64(file, O_RDWR)) == -1) {
#else /* LARGE_FILE */
	if ((fd = open(file, O_RDWR)) == -1) {
#endif /* LARGE_FILE */
		perror("open error");
                anyfail();
	}

#ifdef LARGE_FILE
	if (fstat64(fd, &statbuf) == -1) {
#else /* LARGE_FILE */
	if (fstat(fd, &statbuf) == -1) {
#endif /* LARGE_FILE */
		perror("stat error");
                anyfail();
	}
	filesize = statbuf.st_size;

	if (statbuf.st_size - sparseoffset > SIZE_MAX) {
		fprintf(stderr, "size_t overflow when setting up map\n");
                anyfail();
	}
	mapsize = (size_t)(statbuf.st_size - sparseoffset);
	mappages = roundup(mapsize, pagesize) / pagesize;
	offset = sparseoffset;
	if (do_offset) {
		int pageoffset = lrand48() % mappages;
		int byteoffset = pageoffset * pagesize;
		offset += byteoffset;
		mapsize -= byteoffset;
		mappages -= pageoffset;
	}

#ifdef LARGE_FILE
	if ((maddr = mmap64(0, mapsize, PROT_READ|PROT_WRITE, 
			mapflags, fd, offset)) == (caddr_t)-1) {
#else /* LARGE_FILE */
	if ((maddr = mmap(0, mapsize, PROT_READ|PROT_WRITE, 
			mapflags, fd, offset)) == (caddr_t)-1) {
#endif /* LARGE_FILE */
		perror("mmap error");
                anyfail();
	}

	(void)close(fd);

	nloops = (randloops) ? (lrand48() % MAXLOOPS) : MAXLOOPS;

	if (debug) {
#ifdef LARGE_FILE
		(void)printf("child %d (pid %ld): seed %d, fsize %Ld, "
			"mapsize %d, off %Ld, loop %d\n",
			procno, getpid(), seed, filesize, mapsize,
			offset/pagesize, nloops);
#else /* LARGE_FILE */
		(void)printf("child %d (pid %d): seed %d, fsize %ld, "
			"mapsize %ld, off %ld, loop %d\n",
			procno, getpid(), seed, filesize, (long)mapsize,
			offset/pagesize, nloops);
#endif /* LARGE_FILE */
	}

	/*
	 *  Now loop read/writing random pages.
	 */
	for (loopcnt = 0; loopcnt < nloops; loopcnt++) {
		randpage = lrand48() % mappages;
		paddr = maddr + (randpage * pagesize);	 /* page address */

		if (randpage < mappages - 1
		    || !(mapsize % pagesize))
			validsize = pagesize;
		else
			validsize = mapsize % pagesize;

		/*
		 * Because one child is mapping file in extend mode,
		 * it may be padded with zeros at end.  So we can't
		 * do an exact check -- accept known pattern OR zeros.
		 */
		for (i = procno; i < validsize; i += nprocs) {
			if (*((unsigned char *)(paddr+i)) 
			    != ((procno + pattern) & 0xff)
			    && *((unsigned char *)(paddr+i)) != 0) {
				(void)fprintf(stderr, "child %d: invalid data "
				"<x%x>", procno, *((unsigned char *)(paddr+i)));
				(void)fprintf(stderr, " at pg %d off %d, exp "
					"<x%x>\n", randpage, i,
					(procno+pattern)&0xff);
                        anyfail();
			}
			/*
			 *  Now write it.
			 */

			*(paddr+i) = (procno + pattern) & 0xff;
		}
	}
	if (dosync) {
		/*
		 * Exercise msync() as well!
		 */
		randpage = lrand48() % mappages;
		paddr = maddr + (randpage * pagesize);	 /* page address */
		if (msync(paddr, (mappages - randpage)*pagesize, 
		    MS_SYNC) == -1) {
			perror("msync error");
                        anyfail();
		}
	}

	exit(0);
}

/*
 *  child_writer
 * 	The child process that continually (and slowly!!) grows
 *	the file.  The purpose of this is to exercise the code
 *	supporting mapping of fragments.  The map children are 
 *	constantly reforking and will pick up the map changes, etc.
 *	This process executes until signalled (i.e. has no exit!)
 *	unless error.	
 */
void
child_writer(char *file, uchar_t *buf)	/* buf already set up in main */
{
	int fd;
#ifdef LARGE_FILE
	struct stat64 statbuf;
	off64_t off;
#else /* LARGE_FILE */
	struct stat statbuf;
	off_t off;
#endif /* LARGE_FILE */
	int pagesize = sysconf(_SC_PAGE_SIZE);
	uchar_t *p;
	int cnt;

#ifdef LARGE_FILE
	if ((fd = open64(file, O_RDWR)) == -1) {
#else /* LARGE_FILE */
	if ((fd = open(file, O_RDWR)) == -1) {
#endif /* LARGE_FILE */
		perror("open error");
                anyfail();
	}

#ifdef LARGE_FILE
	if ((off = lseek64(fd, 0, SEEK_END)) == -1) {
#else /* LARGE_FILE */
	if ((off = lseek(fd, 0, SEEK_END)) == -1) {
#endif /* LARGE_FILE */
		perror("lseek error");
                anyfail();
	}


	for (;;) {
#ifdef LARGE_FILE
		if (fstat64(fd, &statbuf) == -1) {
#else /* LARGE_FILE */
		if (fstat(fd, &statbuf) == -1) {
#endif /* LARGE_FILE */
			perror("fstat error");
                        anyfail();
		}
#ifdef LARGE_FILE
		if (debug)
			(void)printf("writer %d bytes at off %Ld, size %Ld\n", 
				growsize, off, statbuf.st_size); 
#else /* LARGE_FILE */
		if (debug)
			(void)printf("writer %d bytes at off %ld, size %ld\n", 
				growsize, off, statbuf.st_size); 
#endif /* LARGE_FILE */

		/*
		 *  Write some number of bytes, then sleep some
		 *  number of seconds...
		 *  Need to keep track of our offset so write the
		 *  right bytes.
		 */

		p = buf + (off % pagesize);

		if ((cnt = write(fd, p, growsize)) != growsize) {
			if (cnt == -1)
				perror("write error");
			else
				(void)fprintf(stderr, "wrote %d of %d bytes\n",
					cnt, growsize);
                        anyfail();
		}

		off += growsize;

		(void)sleep(sleeptime);
		if (dosync) {
			if (fsync(fd) == -1) {
				perror("fsync error");
                                anyfail();
			}
		}
	}
}


/*
 *  Make sure file has all the correct data.

 */
int
fileokay(char *file, uchar_t *expbuf)
{
#ifdef LARGE_FILE
	struct stat64 statbuf;
#else /* LARGE_FILE */
	struct stat statbuf;
#endif /* LARGE_FILE */
	size_t mapsize;
	uchar_t *readbuf;
	unsigned mappages;
	unsigned pagesize = sysconf(_SC_PAGE_SIZE);
	int fd;
	int cnt;
	unsigned i, j;

#ifdef LARGE_FILE
	if ((fd = open64(file, O_RDONLY)) == -1) {
#else /* LARGE_FILE */
	if ((fd = open(file, O_RDONLY)) == -1) {
#endif /* LARGE_FILE */
		perror("open error");
                anyfail();
	}
#ifdef LARGE_FILE
	if (fstat64(fd, &statbuf) == -1) {
#else /* LARGE_FILE */
	if (fstat(fd, &statbuf) == -1) {
#endif /* LARGE_FILE */
		perror("stat error");
                anyfail();
	}
#ifdef LARGE_FILE
	if (lseek64(fd, sparseoffset, SEEK_SET) < 0) {
#else /* LARGE_FILE */
	if (lseek(fd, sparseoffset, SEEK_SET) < 0) {
#endif /* LARGE_FILE */
		perror("lseek");
		exit(1);
	}

	readbuf = (uchar_t *)malloc(pagesize);

	if (statbuf.st_size - sparseoffset > SIZE_MAX) {
		fprintf(stderr, "size_t overflow when setting up map\n");
		exit(1);
	}
	mapsize = (size_t)(statbuf.st_size - sparseoffset);
	mappages = roundup(mapsize, pagesize) / pagesize;

	for (i = 0; i < mappages; i++) {
		cnt = read(fd, (char *)readbuf, pagesize);
		if (cnt == -1) {
			perror("read error");
			return(0);	
		} else if (cnt != pagesize) {
			/*
			 *  Okay if at last page in file... 
			 */
			if ((i * pagesize) + cnt != mapsize) {
				(void)fprintf(stderr, "read %d of %ld bytes\n",
					(i*pagesize)+cnt, (long)mapsize);
				return(0);
			}
		}
		/*
		 *  Compare read bytes of data.
		 *  May have zeros from map extend...
		 */
		for (j = 0; j < cnt; j++) {
			if (expbuf[j] != readbuf[j] && readbuf[j] != 0) {
				(void)fprintf(stderr, 
					"read bad data: exp %c got %c",
					expbuf[j], readbuf[j]);
#ifdef LARGE_FILE
				(void)fprintf(stderr, ", pg %d off %d, "
					"(fsize %Ld)\n", i, j, statbuf.st_size);
#else /* LARGE_FILE */
				(void)fprintf(stderr, ", pg %d off %d, "
					"(fsize %ld)\n", i, j, statbuf.st_size);
#endif /* LARGE_FILE */
				return(0);
			}
		}
	}
					
	return(1);
}

/*ARGSUSED*/
void
finish(int sig)
{
	finished++;
	/* finish nicely and check the file contents */
}

/*ARGSUSED*/
void
clean_up_file(int sig)
{
	if (!leavefile)
		(void)unlink(filename);
	exit(1);
}

unsigned int
initrand(void)
{
	unsigned int seed;

	/*
	 *  Initialize random seed...  Got this from a test written
	 *  by scooter:
	 *	Use srand/rand to diffuse the information from the
	 *	time and pid.  If you start several processes, then
	 *	the time and pid information don't provide much
	 *	variation.
	 */
	srand((unsigned int)getpid());
	seed = rand();
	srand((unsigned int)time((time_t *)0));
	seed = (seed ^ rand()) % 100000;
	srand48((long int)seed);
	return (seed);
}


/*****  LTP Port        *****/
void ok_exit()
{
        tst_resm(TPASS, "Test passed\n");
	tst_rmdir();
	tst_exit();
}


int anyfail()
{
  tst_resm(TFAIL, "Test failed\n");
  tst_rmdir();
  tst_exit();
  return(0);
}
Exemple #6
0
int
main(int argc, char *argv[]) {
	caddr_t			mmapaddr;
	size_t			pagesize = sysconf(_SC_PAGE_SIZE);
	time_t			t;
	int			i;
        struct sigaction        sa;

	tst_tmpdir();
	if (!argc) {
		(void)fprintf(stderr, "argc == 0\n");
		return 1;
	}
	if (argc != 1) {
		(void)fprintf(stderr, "usage: %s\n", argv[0]);
		return 1;
	}
	(void)time(&t);
	if ((fd = mkstemp(tmpname)) == -1) {
		ERROR("mkstemp failed");
		anyfail();
	}
        sa.sa_handler = cleanup;
        sa.sa_flags = 0;
        if (sigemptyset(&sa.sa_mask)) {
                ERROR("sigemptyset failed");
		anyfail();
        }
        CATCH_SIG(SIGINT);
        CATCH_SIG(SIGQUIT);
        CATCH_SIG(SIGTERM);
	if (sbrk(2*pagesize - ((ulong)sbrk(0) & (pagesize-1))) == (char *)-1) {
		CLEANERROR("couldn't round up brk");
		anyfail();
	}
	if ((mmapaddr = sbrk(0)) == (caddr_t)-1) {
		CLEANERROR("couldn't find top of brk");
		anyfail();
	}
	/* Write a page of garbage into the file, so we can mmap it without
	 * asking for PROT_WRITE.
	 */
	for (i = pagesize; i; i--)
		*(mmapaddr-i) = 'a';
	if (write(fd, (char *)mmapaddr-pagesize, pagesize) != pagesize) {
		CLEANERROR("write failed");
		anyfail();
	}
	if (mmap(mmapaddr, pagesize, PROT_NONE,
		MAP_FIXED|MAP_PRIVATE|MAP_FILE, fd, 0) != mmapaddr)
	{
		CLEANERROR("couldn't mmap file");
		anyfail();
	}
	/*
	 * Since the file is mmapped, mmreg_new and uiomove_phys handle all
	 * I/O
	 */
	if (lseek(fd, 0, SEEK_SET) != 0) {
		CLEANERROR("lseek failed");
		anyfail();
	}
	if (read(fd, (char *)mmapaddr, pagesize) != -1) {
		CLEANERROR("read succeded");
		anyfail();
	}
	if (errno != EFAULT) {
		CLEANERROR("read didn't set errno = EFAULT");
		anyfail();
	}
	if (write(fd, (char *)mmapaddr, pagesize) != -1) {
		CLEANERROR("write succeded");
		anyfail();
	}
	if (errno != EFAULT) {
		CLEANERROR("write didn't set errno = EFAULT");
		anyfail();
	}
	if (close(fd) == -1) {
		CLEANERROR("close failed");
		anyfail();
	}
	if (munmap(mmapaddr, pagesize) == -1) {
		CLEANERROR("munmap failed");
		anyfail();
	}
	if (unlink(tmpname) == -1) {
		ERROR("unlink failed");
		anyfail();
	}
	(void)time(&t);
//	(void)printf("%s: Finished %s", argv[0], ctime(&t));
	ok_exit(); /* LTP Port */
	tst_exit();
}
Exemple #7
0
/*--------------------------------------------------------------*/
int main(int argc, char *argv[])
{
	register int i;
	int status, count, child, kidpid;
	int core, sig, ex;
	char *msg;

	if ((msg =
	     parse_opts(argc, argv, (option_t *) NULL, NULL)) != (char *)NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}
#ifdef UCLINUX
	maybe_run_child(&do_child, "");
#endif

	setup();		/* temp file is now open */
/*--------------------------------------------------------------*/

	for (i = 0; i < ITER; i++) {

		if ((kidpid = FORK_OR_VFORK()) == 0) {
#ifdef UCLINUX
			if (self_exec(argv[0], "")) {
				terror("self_exec failed (may be OK if under stress)");
				if (instress())
					ok_exit();
				forkfail();
			}
#else
			do_child();
#endif
		}
		if (kidpid < 0) {
			terror("Fork failed (may be OK if under stress)");
			if (instress())
				ok_exit();
			forkfail();
		}
		count = 0;
		while ((child = wait(&status)) > 0) {
			count++;
		}
		if (count != 1) {
			fprintf(temp, "\twrong # children waited on.\n");
			fprintf(temp, "\tgot %d, expected %d\n", count, 1);
			fail_exit();
		}
		/*
		   sig = status & 0177;
		   core = status & 0200;
		   ex = (status & 0xFF00) >> 8;
		 */
		/*****	LTP Port	*****/
		sig = WTERMSIG(status);
#ifdef WCOREDUMP
		core = WCOREDUMP(status);
#endif
		ex = WIFEXITED(status);
		/**************/
		if (!core) {
			fprintf(temp, "\tChild did not return core bit set!\n");
			fprintf(temp, "\t  iteration %d, exit stat = 0x%x\n",
				i, status);
			fprintf(temp, "\tCore = %d, sig = %d, ex = %d\n",
				core, sig, ex);
			local_flag = FAILED;
		}
		if (sig != SIGIOT) {
			fprintf(temp, "\tChild did not exit with SIGIOT (%d)\n",
				SIGIOT);
			fprintf(temp, "\t  iteration %d, exit stat = 0x%x\n",
				i, status);
			fprintf(temp, "\tCore = %d, sig = %d, ex = %d\n",
				core, sig, ex);
			local_flag = FAILED;
		}
		if (local_flag == FAILED)
			break;
	}

/*--------------------------------------------------------------*/
/* Clean up any files created by test before call to anyfail.	*/

	unlink("core");
	anyfail();		/* THIS CALL DOES NOT RETURN - EXITS!!  */
	return 0;
}