1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Fork a process that creates a file and writes a few bytes, and
9f08c3bdfSopenharmony_ci * calls exit WITHOUT calling close(). The parent then reads the
10f08c3bdfSopenharmony_ci * file.  If everything that was written is present in the file, then
11f08c3bdfSopenharmony_ci * the test passes.
12f08c3bdfSopenharmony_ci */
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <stdlib.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define FNAME "test_file"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic void child_write(void)
20f08c3bdfSopenharmony_ci{
21f08c3bdfSopenharmony_ci	int fd;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci	fd = SAFE_CREAT(FNAME, 0666);
24f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, fd, FNAME, sizeof(FNAME));
25f08c3bdfSopenharmony_ci	exit(0);
26f08c3bdfSopenharmony_ci}
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void check_file(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	int fd, len;
31f08c3bdfSopenharmony_ci	char buf[256];
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FNAME, O_RDONLY);
34f08c3bdfSopenharmony_ci	len = SAFE_READ(0, fd, buf, sizeof(buf));
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	if (len != sizeof(FNAME)) {
37f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Wrong length %i expected %zu", len, sizeof(buf));
38f08c3bdfSopenharmony_ci		goto out;
39f08c3bdfSopenharmony_ci	}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	if (memcmp(buf, FNAME, sizeof(FNAME))) {
42f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Wrong data read back");
43f08c3bdfSopenharmony_ci		goto out;
44f08c3bdfSopenharmony_ci	}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	tst_res(TPASS, "File written by child read back correctly");
47f08c3bdfSopenharmony_ciout:
48f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void run(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	int pid;
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	pid = SAFE_FORK();
56f08c3bdfSopenharmony_ci	if (!pid)
57f08c3bdfSopenharmony_ci		child_write();
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	tst_reap_children();
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	check_file();
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	SAFE_UNLINK(FNAME);
64f08c3bdfSopenharmony_ci}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_cistatic struct tst_test test = {
67f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
68f08c3bdfSopenharmony_ci	.forks_child = 1,
69f08c3bdfSopenharmony_ci	.test_all = run,
70f08c3bdfSopenharmony_ci};
71