1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2021 4f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 5f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Tests basic error handling of the fcntl syscall. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * - EMFILE when cmd is F_DUPFD and the per-process limit on the number of open 14f08c3bdfSopenharmony_ci * file descriptors has been reached. 15f08c3bdfSopenharmony_ci */ 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#include <fcntl.h> 18f08c3bdfSopenharmony_ci#include <sys/types.h> 19f08c3bdfSopenharmony_ci#include <sys/wait.h> 20f08c3bdfSopenharmony_ci#include <stdlib.h> 21f08c3bdfSopenharmony_ci#include <unistd.h> 22f08c3bdfSopenharmony_ci#include "tst_test.h" 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic char fname[20] = "testfile"; 25f08c3bdfSopenharmony_cistatic int fd = -1, max_files; 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_cistatic void verify_fcntl(void) 28f08c3bdfSopenharmony_ci{ 29f08c3bdfSopenharmony_ci pid_t pid; 30f08c3bdfSopenharmony_ci int i; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 33f08c3bdfSopenharmony_ci if (pid == 0) { 34f08c3bdfSopenharmony_ci for (i = 0; i < max_files; i++) { 35f08c3bdfSopenharmony_ci fd = open(fname, O_CREAT | O_RDONLY, 0444); 36f08c3bdfSopenharmony_ci if (fd == -1) 37f08c3bdfSopenharmony_ci break; 38f08c3bdfSopenharmony_ci } 39f08c3bdfSopenharmony_ci TST_EXP_FAIL2(fcntl(1, F_DUPFD, 1), EMFILE, 40f08c3bdfSopenharmony_ci "fcntl(1, F_DUPFD, 1)"); 41f08c3bdfSopenharmony_ci } 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci tst_reap_children(); 44f08c3bdfSopenharmony_ci} 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_cistatic void setup(void) 47f08c3bdfSopenharmony_ci{ 48f08c3bdfSopenharmony_ci max_files = getdtablesize(); 49f08c3bdfSopenharmony_ci} 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_cistatic void cleanup(void) 52f08c3bdfSopenharmony_ci{ 53f08c3bdfSopenharmony_ci if (fd > -1) 54f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci SAFE_UNLINK(fname); 57f08c3bdfSopenharmony_ci} 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cistatic struct tst_test test = { 60f08c3bdfSopenharmony_ci .forks_child = 1, 61f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 62f08c3bdfSopenharmony_ci .setup = setup, 63f08c3bdfSopenharmony_ci .cleanup = cleanup, 64f08c3bdfSopenharmony_ci .test_all = verify_fcntl, 65f08c3bdfSopenharmony_ci}; 66