1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 5f08c3bdfSopenharmony_ci * 07/2002 Limited forking and split "infinite forks" testcase to fork12.c by 6f08c3bdfSopenharmony_ci * Nate Straz 7f08c3bdfSopenharmony_ci * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com> 8f08c3bdfSopenharmony_ci */ 9f08c3bdfSopenharmony_ci 10f08c3bdfSopenharmony_ci/*\ 11f08c3bdfSopenharmony_ci * [Description] 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * Check that all children inherit parent's file descriptor. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * Parent opens a file and forks children. Each child reads a byte and checks 16f08c3bdfSopenharmony_ci * that the value is correct. Parent checks that correct number of bytes was 17f08c3bdfSopenharmony_ci * consumed from the file. 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci#include <stdlib.h> 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#define NFORKS 100 25f08c3bdfSopenharmony_ci#define TESTFILE "testfile_fork07" 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic int fd; 28f08c3bdfSopenharmony_cistatic char buf; 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_cistatic void run(void) 31f08c3bdfSopenharmony_ci{ 32f08c3bdfSopenharmony_ci int ret, i; 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDONLY); 35f08c3bdfSopenharmony_ci for (i = 0; i < NFORKS; ++i) { 36f08c3bdfSopenharmony_ci if (!SAFE_FORK()) { 37f08c3bdfSopenharmony_ci SAFE_READ(1, fd, &buf, 1); 38f08c3bdfSopenharmony_ci if (buf != 'a') 39f08c3bdfSopenharmony_ci tst_res(TFAIL, "%6d: read '%c' instead of 'a'", 40f08c3bdfSopenharmony_ci getpid(), buf); 41f08c3bdfSopenharmony_ci exit(0); 42f08c3bdfSopenharmony_ci } 43f08c3bdfSopenharmony_ci } 44f08c3bdfSopenharmony_ci tst_reap_children(); 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci ret = read(fd, &buf, 1); 47f08c3bdfSopenharmony_ci if (ret == 0) 48f08c3bdfSopenharmony_ci tst_res(TPASS, "read the end of file correctly"); 49f08c3bdfSopenharmony_ci else 50f08c3bdfSopenharmony_ci tst_res(TFAIL, "read() returns %d, expected 0", ret); 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 53f08c3bdfSopenharmony_ci} 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void setup(void) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci tst_fill_file(TESTFILE, 'a', NFORKS, 1); 58f08c3bdfSopenharmony_ci} 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_cistatic void cleanup(void) 61f08c3bdfSopenharmony_ci{ 62f08c3bdfSopenharmony_ci if (fd > 0) 63f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 64f08c3bdfSopenharmony_ci} 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_cistatic struct tst_test test = { 67f08c3bdfSopenharmony_ci .forks_child = 1, 68f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 69f08c3bdfSopenharmony_ci .cleanup = cleanup, 70f08c3bdfSopenharmony_ci .setup = setup, 71f08c3bdfSopenharmony_ci .test_all = run, 72f08c3bdfSopenharmony_ci}; 73