1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 07/2001 Ported by Wayne Boyer 5 * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com> 6 */ 7 8/*\ 9 * [Description] 10 * 11 * Check that the parent's file descriptors will not be affected by being 12 * closed in the child. 13 */ 14 15#include <stdlib.h> 16 17#include "tst_test.h" 18 19#define TESTFILE "testfile_fork08" 20 21static int fd; 22static char buf; 23 24static void run(void) 25{ 26 int ret; 27 28 fd = SAFE_OPEN(TESTFILE, O_RDONLY); 29 if (!SAFE_FORK()) { 30 SAFE_CLOSE(fd); 31 exit(0); 32 } 33 34 if (!SAFE_FORK()) { 35 SAFE_READ(1, fd, &buf, 1); 36 if (buf != 'a') 37 tst_res(TFAIL, "%6d: read '%c' instead of 'a'", 38 getpid(), buf); 39 SAFE_CLOSE(fd); 40 exit(0); 41 } 42 tst_reap_children(); 43 44 ret = read(fd, &buf, 1); 45 if (ret == 0) 46 tst_res(TPASS, "read the end of file correctly"); 47 else 48 tst_res(TFAIL | TERRNO, "read() returns %d, expected 0", ret); 49 50 SAFE_CLOSE(fd); 51} 52 53static void setup(void) 54{ 55 tst_fill_file(TESTFILE, 'a', 1, 1); 56} 57 58static void cleanup(void) 59{ 60 if (fd > 0) 61 SAFE_CLOSE(fd); 62} 63 64static struct tst_test test = { 65 .forks_child = 1, 66 .needs_tmpdir = 1, 67 .cleanup = cleanup, 68 .setup = setup, 69 .test_all = run, 70}; 71