xref: /illumos-gate/usr/src/test/libc-tests/tests/closefrom.c (revision b210e77709da8e42dfe621e10ccf4be504206058)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2023 Bill Sommerfeld <sommerfeld@alum.mit.edu>
14  */
15 
16 /*
17  * Test for closefrom().  Test cases were inspired by xapian's
18  * test_closefrom1() and are somewhat incomplete (we don't test
19  * what happens when /proc/self/fd isn't available).
20  */
21 #include <err.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/debug.h>
28 
29 int
30 main(void)
31 {
32 	closefrom(INT_MAX);	/* should be a no-op */
33 
34 	VERIFY3S(dup2(1, 10), ==, 10);
35 	VERIFY3S(dup2(1, 11), ==, 11);
36 	VERIFY3S(dup2(1, 15), ==, 15);
37 
38 	closefrom(11);
39 
40 	VERIFY3S(close(10), ==, 0);
41 	VERIFY3S(close(11), ==, -1);
42 	VERIFY3S(errno, ==, EBADF);
43 	VERIFY3S(close(15), ==, -1);
44 	VERIFY3S(errno, ==, EBADF);
45 
46 	exit(0);
47 }
48