1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1998 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 /* 31 * Portions of this source code were derived from Berkeley 4.3 BSD 32 * under license from the Regents of the University of California. 33 */ 34 35 /* 36 * Copyright 2026 Oxide Computer Company 37 */ 38 39 #include <sys/param.h> 40 #include <sys/isa_defs.h> 41 #include <sys/types.h> 42 #include <sys/sysmacros.h> 43 #include <sys/systm.h> 44 #include <sys/errno.h> 45 #include <sys/fcntl.h> 46 #include <sys/file.h> 47 #include <sys/unistd.h> 48 #include <sys/debug.h> 49 50 /* 51 * Close a file. 52 */ 53 54 int 55 close(int fdes) 56 { 57 int error; 58 59 if ((error = closeandsetf(fdes, NULL)) != 0) 60 return (set_errno(error)); 61 return (0); 62 } 63 64 /* 65 * Close, or set descriptor flags on, every open file descriptor in the 66 * range [low, high]. The range may extend beyond the highest open descriptor, 67 * and errors from closing individual descriptors are ignored. 68 */ 69 int 70 close_range(uint_t low, uint_t high, int flags) 71 { 72 int fdflags = 0; 73 74 if (low > high) 75 return (set_errno(EINVAL)); 76 if ((flags & ~(CLOSE_RANGE_CLOEXEC | CLOSE_RANGE_CLOFORK)) != 0) 77 return (set_errno(EINVAL)); 78 79 if (flags & CLOSE_RANGE_CLOEXEC) 80 fdflags |= FD_CLOEXEC; 81 if (flags & CLOSE_RANGE_CLOFORK) 82 fdflags |= FD_CLOFORK; 83 84 fdcloserange((int)MIN(low, INT_MAX), (int)MIN(high, INT_MAX), 85 fdflags); 86 87 return (0); 88 } 89