Commit 1c265edd authored by Pavel Emelyanov's avatar Pavel Emelyanov

zdtm: A bunch of test for various flavors of unlinked/opened/maped files

Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 18a5c90c
......@@ -61,6 +61,9 @@ static/selfexe00
static/unlink_fstat00
static/unlink_fstat02
static/unlink_fstat03
static/unlink_mmap00
static/unlink_mmap01
static/unlink_mmap02
static/eventfs00
static/signalfd00
static/inotify00
......
......@@ -128,6 +128,9 @@ TST_FILE = \
fifo_wronly \
unlink_fifo \
unlink_fifo_wronly \
unlink_mmap00 \
unlink_mmap01 \
unlink_mmap02 \
file_shared \
file_append \
cow01 \
......
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include "zdtmtst.h"
const char *test_doc = "Test mmaped and unlinked files";
char *filename;
TEST_OPTION(filename, string, "file name", 1);
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
static void touch_file_page(int fd, unsigned long off, char c)
{
if (lseek(fd, off, SEEK_SET) != off) {
err("Lseek fail");
exit(1);
}
if (write(fd, &c, 1) != 1) {
err("Write fail");
exit(1);
}
}
int main(int argc, char ** argv)
{
int fd;
char *mem_a, *mem_b;
test_init(argc, argv);
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
err("can't open file");
exit(1);
}
touch_file_page(fd, 0, 'a');
touch_file_page(fd, PAGE_SIZE, 'b');
touch_file_page(fd, 2 * PAGE_SIZE - 1, 'c'); /* for aligned file */
/* map with different prots to create 2 regions */
mem_a = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0);
mem_b = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, PAGE_SIZE);
if (mem_a == MAP_FAILED || mem_b == MAP_FAILED) {
err("can't map file");
exit(1);
}
if (unlink(filename) < 0) {
err("can't unlink file");
exit(1);
}
close(fd);
test_daemon();
test_waitsig();
if (mem_a[0] != 'a')
fail("1st region fail");
else if (mem_b[0] != 'b' || mem_b[PAGE_SIZE - 1] != 'c')
fail("2nd regin fail");
else
pass();
return 0;
}
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include "zdtmtst.h"
const char *test_doc = "Test mmaped and unlinked files (2, with hard links)";
char *filename;
TEST_OPTION(filename, string, "file name", 1);
static char linkname[4096];
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
static void touch_file_page(int fd, unsigned long off, char c)
{
if (lseek(fd, off, SEEK_SET) != off) {
err("Lseek fail");
exit(1);
}
if (write(fd, &c, 1) != 1) {
err("Write fail");
exit(1);
}
}
int main(int argc, char ** argv)
{
int fd;
char *mem_a, *mem_b;
test_init(argc, argv);
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
err("can't open file");
exit(1);
}
touch_file_page(fd, 0, 'a');
touch_file_page(fd, PAGE_SIZE - 1, 'b');/* for aligned file */
mem_a = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0);
if (mem_a == MAP_FAILED) {
err("can't map file");
exit(1);
}
sprintf(linkname, "%s.lnk", filename);
if (link(filename, linkname)) {
err("can't link file");
exit(1);
}
if (unlink(filename) < 0) {
err("can't unlink file");
exit(1);
}
close(fd);
fd = open(linkname, O_RDWR);
if (fd < 0) {
err("can't open link");
exit(1);
}
mem_b = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0);
if (mem_b == MAP_FAILED) {
err("can't map link");
exit(1);
}
if (unlink(linkname) < 0) {
err("can't unlink link");
exit(1);
}
close(fd);
test_daemon();
test_waitsig();
if (mem_a[0] != 'a' || mem_a[PAGE_SIZE - 1] != 'b')
fail("1st region fail");
else if (mem_b[0] != 'a' || mem_b[PAGE_SIZE - 1] != 'b')
fail("2nd regin fail");
else
pass();
return 0;
}
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include "zdtmtst.h"
const char *test_doc = "Test mmaped, opened and unlinked files";
char *filename;
TEST_OPTION(filename, string, "file name", 1);
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif
static void touch_file_page(int fd, unsigned long off, char c)
{
if (lseek(fd, off, SEEK_SET) != off) {
err("Lseek fail");
exit(1);
}
if (write(fd, &c, 1) != 1) {
err("Write fail");
exit(1);
}
}
int main(int argc, char ** argv)
{
int fd;
char *mem_a, *mem_b;
test_init(argc, argv);
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
err("can't open file");
exit(1);
}
touch_file_page(fd, 2 * PAGE_SIZE - 1, 'c'); /* for aligned file */
/* map with different prots to create 2 regions */
mem_a = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0);
mem_b = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, PAGE_SIZE);
if (mem_a == MAP_FAILED || mem_b == MAP_FAILED) {
err("can't map file");
exit(1);
}
if (unlink(filename) < 0) {
err("can't unlink file");
exit(1);
}
test_daemon();
test_waitsig();
touch_file_page(fd, 0, 'a');
touch_file_page(fd, PAGE_SIZE, 'b');
if (mem_a[0] != 'a')
fail("1st region fail");
else if (mem_b[0] != 'b' || mem_b[PAGE_SIZE - 1] != 'c')
fail("2nd regin fail");
else
pass();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment