1 /* $OpenBSD: t_umask.c,v 1.1.1.1 2019/11/19 19:57:04 bluhm Exp $ */ 2 /* $NetBSD: t_umask.c,v 1.2 2017/01/13 19:34:19 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 2011 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jukka Ruohonen. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include "macros.h" 34 35 #include <sys/cdefs.h> 36 __RCSID("$NetBSD: t_umask.c,v 1.2 2017/01/13 19:34:19 christos Exp $"); 37 38 #include <sys/stat.h> 39 #include <sys/wait.h> 40 41 #include "atf-c.h" 42 #include <fcntl.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 static const char path[] = "umask"; 48 static const mode_t mask[] = { 49 S_IRWXU, 50 S_IRUSR, 51 S_IWUSR, 52 S_IXUSR, 53 S_IRWXG, 54 S_IRGRP, 55 S_IWGRP, 56 S_IXGRP, 57 S_IRWXO, 58 S_IROTH, 59 S_IWOTH, 60 S_IXOTH 61 }; 62 63 ATF_TC_WITH_CLEANUP(umask_fork); 64 ATF_TC_HEAD(umask_fork, tc) 65 { 66 atf_tc_set_md_var(tc, "descr", "Check that umask(2) is inherited"); 67 } 68 69 ATF_TC_BODY(umask_fork, tc) 70 { 71 mode_t mode; 72 pid_t pid; 73 size_t i; 74 int sta; 75 76 for (i = 0; i < __arraycount(mask) - 1; i++) { 77 78 (void)umask(mask[i] | mask[i + 1]); 79 80 pid = fork(); 81 82 if (pid < 0) 83 continue; 84 85 if (pid == 0) { 86 87 mode = umask(mask[i]); 88 89 if (mode != (mask[i] | mask[i + 1])) 90 _exit(EXIT_FAILURE); 91 92 _exit(EXIT_SUCCESS); 93 } 94 95 (void)wait(&sta); 96 97 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 98 goto fail; 99 } 100 101 return; 102 103 fail: 104 (void)umask(S_IWGRP | S_IWOTH); 105 106 atf_tc_fail("umask(2) was not inherited"); 107 } 108 109 ATF_TC_CLEANUP(umask_fork, tc) 110 { 111 (void)umask(S_IWGRP | S_IWOTH); 112 } 113 114 ATF_TC_WITH_CLEANUP(umask_open); 115 ATF_TC_HEAD(umask_open, tc) 116 { 117 atf_tc_set_md_var(tc, "descr", "A basic test of open(2) and umask(2)"); 118 } 119 120 ATF_TC_BODY(umask_open, tc) 121 { 122 const char *str = NULL; 123 struct stat st; 124 size_t i; 125 int fd; 126 127 for (i = 0; i < __arraycount(mask); i++) { 128 129 (void)umask(mask[i]); 130 131 fd = open(path, O_RDWR | O_CREAT, 0777); 132 133 if (fd < 0) 134 continue; 135 136 (void)close(fd); 137 (void)memset(&st, 0, sizeof(struct stat)); 138 139 if (stat(path, &st) != 0) { 140 str = "failed to stat(2)"; 141 goto out; 142 } 143 144 if ((st.st_mode & mask[i]) != 0) { 145 str = "invalid umask(2)"; 146 goto out; 147 } 148 149 if (unlink(path) != 0) { 150 str = "failed to unlink(2)"; 151 goto out; 152 } 153 154 } 155 156 out: 157 (void)umask(S_IWGRP | S_IWOTH); 158 159 if (str != NULL) 160 atf_tc_fail("%s", str); 161 } 162 163 ATF_TC_CLEANUP(umask_open, tc) 164 { 165 (void)umask(S_IWGRP | S_IWOTH); 166 (void)unlink(path); 167 } 168 169 ATF_TC_WITH_CLEANUP(umask_previous); 170 ATF_TC_HEAD(umask_previous, tc) 171 { 172 atf_tc_set_md_var(tc, "descr", "Test the return value from umask(2)"); 173 } 174 175 ATF_TC_BODY(umask_previous, tc) 176 { 177 mode_t mode; 178 size_t i; 179 180 for (i = 0; i < __arraycount(mask); i++) { 181 182 mode = umask(mask[i]); 183 mode = umask(mask[i]); 184 185 if (mode != mask[i]) 186 goto fail; 187 } 188 189 return; 190 191 fail: 192 (void)umask(S_IWGRP | S_IWOTH); 193 194 atf_tc_fail("umask(2) did not return the previous mask"); 195 } 196 197 ATF_TC_CLEANUP(umask_previous, tc) 198 { 199 (void)umask(S_IWGRP | S_IWOTH); 200 } 201 202 ATF_TP_ADD_TCS(tp) 203 { 204 205 ATF_TP_ADD_TC(tp, umask_fork); 206 ATF_TP_ADD_TC(tp, umask_open); 207 ATF_TP_ADD_TC(tp, umask_previous); 208 209 return atf_no_error(); 210 } 211