1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * NAME
22*49cdfc7eSAndroid Build Coastguard Worker * fcntl18.c
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker * Test to check the error conditions in fcntl system call
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * USAGE
28*49cdfc7eSAndroid Build Coastguard Worker * fcntl18
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
31*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
32*49cdfc7eSAndroid Build Coastguard Worker *
33*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS
34*49cdfc7eSAndroid Build Coastguard Worker * NONE
35*49cdfc7eSAndroid Build Coastguard Worker */
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/param.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_FLAG -1
49*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_MIN (-2147483647L-1L)
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker int fd;
52*49cdfc7eSAndroid Build Coastguard Worker char string[40] = "";
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fcntl18";
55*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
56*49cdfc7eSAndroid Build Coastguard Worker struct passwd *pass;
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
59*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
60*49cdfc7eSAndroid Build Coastguard Worker int fail;
61*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)62*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker int retval;
65*49cdfc7eSAndroid Build Coastguard Worker struct flock fl;
66*49cdfc7eSAndroid Build Coastguard Worker int pid, status;
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker setup(); /* global setup */
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker /* //block1: */
73*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 1");
74*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
75*49cdfc7eSAndroid Build Coastguard Worker if ((fd = open("temp.dat", O_CREAT | O_RDWR, 0777)) < 0) { //mode must be specified when O_CREATE is in the flag
76*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "file opening error");
77*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker /* Error condition if address is bad */
81*49cdfc7eSAndroid Build Coastguard Worker retval = fcntl(fd, F_GETLK, (struct flock *)INVAL_FLAG);
82*49cdfc7eSAndroid Build Coastguard Worker if (errno == EFAULT) {
83*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "Test F_GETLK: for errno EFAULT PASSED");
84*49cdfc7eSAndroid Build Coastguard Worker } else {
85*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Test F_GETLK: for errno EFAULT FAILED");
86*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
89*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Block 1 FAILED");
90*49cdfc7eSAndroid Build Coastguard Worker } else {
91*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Block 1 PASSED");
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 1");
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker /* //block2: */
96*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 2");
97*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
98*49cdfc7eSAndroid Build Coastguard Worker /* Error condition if address is bad */
99*49cdfc7eSAndroid Build Coastguard Worker retval = fcntl(fd, F_GETLK, (struct flock *)INVAL_FLAG);
100*49cdfc7eSAndroid Build Coastguard Worker if (errno == EFAULT) {
101*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "Test F_GETLK: for errno EFAULT PASSED");
102*49cdfc7eSAndroid Build Coastguard Worker } else {
103*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Test F_GETLK: for errno EFAULT FAILED");
104*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
107*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Block 2 FAILED");
108*49cdfc7eSAndroid Build Coastguard Worker } else {
109*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Block 2 PASSED");
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 2");
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker /* //block3: */
114*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Enter block 3");
115*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
116*49cdfc7eSAndroid Build Coastguard Worker if ((pid = tst_fork()) == 0) { /* child */
117*49cdfc7eSAndroid Build Coastguard Worker fail = 0;
118*49cdfc7eSAndroid Build Coastguard Worker pass = getpwnam("nobody");
119*49cdfc7eSAndroid Build Coastguard Worker retval = setreuid(-1, pass->pw_uid);
120*49cdfc7eSAndroid Build Coastguard Worker if (retval < 0) {
121*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "setreuid to user nobody failed, "
122*49cdfc7eSAndroid Build Coastguard Worker "errno: %d", errno);
123*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker /* Error condition: invalid cmd */
127*49cdfc7eSAndroid Build Coastguard Worker retval = fcntl(fd, INVAL_FLAG, &fl);
128*49cdfc7eSAndroid Build Coastguard Worker if (errno == EINVAL) {
129*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "Test for errno EINVAL PASSED");
130*49cdfc7eSAndroid Build Coastguard Worker } else {
131*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "Test for errno EINVAL FAILED, "
132*49cdfc7eSAndroid Build Coastguard Worker "got: %d", errno);
133*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker exit(fail);
136*49cdfc7eSAndroid Build Coastguard Worker } else { /* parent */
137*49cdfc7eSAndroid Build Coastguard Worker waitpid(pid, &status, 0);
138*49cdfc7eSAndroid Build Coastguard Worker if (WEXITSTATUS(status) != 0) {
139*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "child returned bad exit status");
140*49cdfc7eSAndroid Build Coastguard Worker fail = 1;
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker if (fail) {
143*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Block 3 FAILED");
144*49cdfc7eSAndroid Build Coastguard Worker } else {
145*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Block 3 PASSED");
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "Exit block 3");
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker cleanup();
151*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker /*
156*49cdfc7eSAndroid Build Coastguard Worker * setup()
157*49cdfc7eSAndroid Build Coastguard Worker * performs all ONE TIME setup for this test
158*49cdfc7eSAndroid Build Coastguard Worker */
setup(void)159*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
160*49cdfc7eSAndroid Build Coastguard Worker {
161*49cdfc7eSAndroid Build Coastguard Worker
162*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker tst_require_root();
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker umask(0);
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
169*49cdfc7eSAndroid Build Coastguard Worker
170*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker sprintf(string, "./fcntl18.%d.1", getpid());
173*49cdfc7eSAndroid Build Coastguard Worker unlink(string);
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker /*
177*49cdfc7eSAndroid Build Coastguard Worker * cleanup()
178*49cdfc7eSAndroid Build Coastguard Worker * performs all the ONE TIME cleanup for this test at completion or
179*49cdfc7eSAndroid Build Coastguard Worker * or premature exit.
180*49cdfc7eSAndroid Build Coastguard Worker */
cleanup(void)181*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
182*49cdfc7eSAndroid Build Coastguard Worker {
183*49cdfc7eSAndroid Build Coastguard Worker /*
184*49cdfc7eSAndroid Build Coastguard Worker * print timing status if that option was specified.
185*49cdfc7eSAndroid Build Coastguard Worker * print errno log if that option was specified
186*49cdfc7eSAndroid Build Coastguard Worker */
187*49cdfc7eSAndroid Build Coastguard Worker close(fd);
188*49cdfc7eSAndroid Build Coastguard Worker
189*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
190*49cdfc7eSAndroid Build Coastguard Worker
191*49cdfc7eSAndroid Build Coastguard Worker unlink("temp.dat");
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker }
194