C++程序  |  63行  |  1.25 KB

/*
 * Copyright (c) International Business Machines  Corp., 2009
 *
 * Authors:
 * Mimi Zohar <zohar@us.ibm.com>
 *
 * 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, version 2 of the
 * License.
 *
 * File: ima_mmap.c
 *
 * Open and mmap a file and sleep. Another process will open the
 * mmapped file in read mode, resulting in a open_writer violation.
 */

#include "tst_test.h"

#define SLEEP_AFTER_CLOSE 3
#define MMAPSIZE 1024

static char *filename;
static void *file;
static int fd;

static struct tst_option options[] = {
	{"f:", &filename,
	 "-f file  File to mmap"},
	{NULL, NULL, NULL}
};

static void cleanup(void)
{
	if (file)
		SAFE_MUNMAP(file, MMAPSIZE);

	if (fd > 0)
		SAFE_CLOSE(fd);
}

static void run(void)
{
	if (!filename)
		tst_brk(TBROK, "Usage: %s -f filename", TCID);

	fd = SAFE_OPEN(filename, O_CREAT | O_RDWR, S_IRWXU);

	file = SAFE_MMAP(NULL, MMAPSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	SAFE_CLOSE(fd);

	tst_res(TINFO, "sleep %ds", SLEEP_AFTER_CLOSE);
	sleep(SLEEP_AFTER_CLOSE);

	tst_res(TPASS, "test completed");
}

static struct tst_test test = {
	.options = options,
	.test_all = run,
	.cleanup = cleanup,
};