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