• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *    07/2001 Ported by Wayne Boyer
4  * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
5  *
6  * This program is free software;  you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14  * the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;  if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 /*
21  * Fork a process that creates a file and writes a few bytes, and
22  * calls exit WITHOUT calling close(). The parent then reads the
23  * file.  If everything that was written is present in the file, then
24  * the test passes.
25  */
26 
27 #include <stdlib.h>
28 #include "tst_test.h"
29 
30 #define FNAME "test_file"
31 
child_write(void)32 static void child_write(void)
33 {
34 	int fd;
35 
36 	fd = SAFE_CREAT(FNAME, 0666);
37 	SAFE_WRITE(1, fd, FNAME, sizeof(FNAME));
38 	exit(0);
39 }
40 
check_file(void)41 static void check_file(void)
42 {
43 	int fd, len;
44 	char buf[256];
45 
46 	fd = SAFE_OPEN(FNAME, O_RDONLY);
47 	len = SAFE_READ(0, fd, buf, sizeof(buf));
48 
49 	if (len != sizeof(FNAME)) {
50 		tst_res(TFAIL, "Wrong length %i expected %zu", len, sizeof(buf));
51 		goto out;
52 	}
53 
54 	if (memcmp(buf, FNAME, sizeof(FNAME))) {
55 		tst_res(TFAIL, "Wrong data read back");
56 		goto out;
57 	}
58 
59 	tst_res(TPASS, "File written by child read back correctly");
60 out:
61 	SAFE_CLOSE(fd);
62 }
63 
run(void)64 static void run(void)
65 {
66 	int pid;
67 
68 	pid = SAFE_FORK();
69 	if (!pid)
70 		child_write();
71 
72 	tst_reap_children();
73 
74 	check_file();
75 
76 	SAFE_UNLINK(FNAME);
77 }
78 
79 static struct tst_test test = {
80 	.needs_tmpdir = 1,
81 	.forks_child = 1,
82 	.test_all = run,
83 };
84