1 /*- 2 * Copyright (c) 2013 Jilles Tjoelker 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 29 #include <fcntl.h> 30 #include <stdio.h> 31 #include <unistd.h> 32 33 /* 34 * O_ACCMODE is currently defined incorrectly. This is what it should be. 35 * Various code depends on the incorrect value. 36 */ 37 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC) 38 39 static int testnum; 40 41 static void 42 subtests(const char *path, int omode, const char *omodetext) 43 { 44 int fd, flags1, flags2, flags3; 45 46 fd = open(path, omode); 47 if (fd == -1) 48 printf("not ok %d - open(\"%s\", %s) failed\n", 49 testnum++, path, omodetext); 50 else 51 printf("ok %d - open(\"%s\", %s) succeeded\n", 52 testnum++, path, omodetext); 53 flags1 = fcntl(fd, F_GETFL); 54 if (flags1 == -1) 55 printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++); 56 else if ((flags1 & CORRECT_O_ACCMODE) == omode) 57 printf("ok %d - fcntl(F_GETFL) gave correct result\n", 58 testnum++); 59 else 60 printf("not ok %d - fcntl(F_GETFL) gave incorrect result " 61 "(%#x & %#x != %#x)\n", 62 testnum++, flags1, CORRECT_O_ACCMODE, omode); 63 if (fcntl(fd, F_SETFL, flags1) == -1) 64 printf("not ok %d - fcntl(F_SETFL) same flags failed\n", 65 testnum++); 66 else 67 printf("ok %d - fcntl(F_SETFL) same flags succeeded\n", 68 testnum++); 69 flags2 = fcntl(fd, F_GETFL); 70 if (flags2 == -1) 71 printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++); 72 else if (flags2 == flags1) 73 printf("ok %d - fcntl(F_GETFL) gave same result\n", 74 testnum++); 75 else 76 printf("not ok %d - fcntl(F_SETFL) caused fcntl(F_GETFL) to " 77 "change from %#x to %#x\n", 78 testnum++, flags1, flags2); 79 if (fcntl(fd, F_SETFL, flags2 | O_NONBLOCK) == -1) 80 printf("not ok %d - fcntl(F_SETFL) O_NONBLOCK failed\n", 81 testnum++); 82 else 83 printf("ok %d - fcntl(F_SETFL) O_NONBLOCK succeeded\n", 84 testnum++); 85 flags3 = fcntl(fd, F_GETFL); 86 if (flags3 == -1) 87 printf("not ok %d - fcntl(F_GETFL) failed\n", testnum++); 88 else if (flags3 == (flags2 | O_NONBLOCK)) 89 printf("ok %d - fcntl(F_GETFL) gave expected result\n", 90 testnum++); 91 else 92 printf("not ok %d - fcntl(F_SETFL) gave unexpected result " 93 "(%#x != %#x)\n", 94 testnum++, flags3, flags2 | O_NONBLOCK); 95 (void)close(fd); 96 } 97 98 int 99 main(int argc __unused, char **argv __unused) 100 { 101 printf("1..24\n"); 102 testnum = 1; 103 subtests("/dev/null", O_RDONLY, "O_RDONLY"); 104 subtests("/dev/null", O_WRONLY, "O_WRONLY"); 105 subtests("/dev/null", O_RDWR, "O_RDWR"); 106 subtests("/bin/sh", O_EXEC, "O_EXEC"); 107 return (0); 108 } 109