1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. 4f08c3bdfSopenharmony_ci * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci */ 6f08c3bdfSopenharmony_ci /* 7f08c3bdfSopenharmony_ci * Basic test for fcntl(2) using F_DUPFD argument. 8f08c3bdfSopenharmony_ci */ 9f08c3bdfSopenharmony_ci 10f08c3bdfSopenharmony_ci#include <sys/types.h> 11f08c3bdfSopenharmony_ci#include <sys/stat.h> 12f08c3bdfSopenharmony_ci#include <fcntl.h> 13f08c3bdfSopenharmony_ci#include <unistd.h> 14f08c3bdfSopenharmony_ci#include <errno.h> 15f08c3bdfSopenharmony_ci#include <stdio.h> 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#include "tst_test.h" 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_cistatic int fd; 20f08c3bdfSopenharmony_cistatic char fname[256]; 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_cistatic const int min_fds[] = {0, 1, 2, 3, 10, 100}; 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic void verify_fcntl(unsigned int n) 25f08c3bdfSopenharmony_ci{ 26f08c3bdfSopenharmony_ci int min_fd = min_fds[n]; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci TEST(fcntl(fd, F_DUPFD, min_fd)); 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ci if (TST_RET == -1) { 31f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, "fcntl(%s, F_DUPFD, %i) failed", 32f08c3bdfSopenharmony_ci fname, min_fd); 33f08c3bdfSopenharmony_ci return; 34f08c3bdfSopenharmony_ci } 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci if (TST_RET < min_fd) { 37f08c3bdfSopenharmony_ci tst_res(TFAIL, "fcntl(%s, F_DUPFD, %i) returned %ld < %i", 38f08c3bdfSopenharmony_ci fname, min_fd, TST_RET, min_fd); 39f08c3bdfSopenharmony_ci } 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci tst_res(TPASS, "fcntl(%s, F_DUPFD, %i) returned %ld", 42f08c3bdfSopenharmony_ci fname, min_fd, TST_RET); 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci SAFE_CLOSE(TST_RET); 45f08c3bdfSopenharmony_ci} 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_cistatic void setup(void) 48f08c3bdfSopenharmony_ci{ 49f08c3bdfSopenharmony_ci sprintf(fname, "fcntl02_%d", getpid()); 50f08c3bdfSopenharmony_ci fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0700); 51f08c3bdfSopenharmony_ci} 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_cistatic void cleanup(void) 54f08c3bdfSopenharmony_ci{ 55f08c3bdfSopenharmony_ci if (fd > 0) 56f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 57f08c3bdfSopenharmony_ci} 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cistatic struct tst_test test = { 60f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 61f08c3bdfSopenharmony_ci .test = verify_fcntl, 62f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(min_fds), 63f08c3bdfSopenharmony_ci .setup = setup, 64f08c3bdfSopenharmony_ci .cleanup = cleanup, 65f08c3bdfSopenharmony_ci}; 66