blob: b6391436950ca920ee7bb7e1d8f01b2c65ecdab0 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
nstrazfa31d552002-05-14 16:50:06 +000022 * fcntl18.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Test to check the error conditions in fcntl system call
26 *
27 * USAGE
nstrazfa31d552002-05-14 16:50:06 +000028 * fcntl18
plars865695b2001-08-27 22:15:12 +000029 *
30 * HISTORY
31 * 07/2001 Ported by Wayne Boyer
32 *
33 * RESTRICTIONS
34 * NONE
35 */
36
plars865695b2001-08-27 22:15:12 +000037#include <signal.h>
38#include <errno.h>
plars865695b2001-08-27 22:15:12 +000039#include <pwd.h>
robbiew5aab8a72003-03-26 18:05:30 +000040#include <sys/types.h>
mridgedb639212005-01-04 21:04:11 +000041#include <sys/wait.h>
robbiew5aab8a72003-03-26 18:05:30 +000042#include <sys/stat.h>
43#include <sys/param.h>
mridgedb639212005-01-04 21:04:11 +000044#include <fcntl.h>
45#include <unistd.h>
robbiew5aab8a72003-03-26 18:05:30 +000046#include "test.h"
47#include "usctest.h"
plars865695b2001-08-27 22:15:12 +000048
49#define INVAL_FLAG -1
50#define INVAL_MIN (-2147483647L-1L)
51
52int fd;
53char string[40] = "";
54
nstrazfa31d552002-05-14 16:50:06 +000055char *TCID = "fcntl18";
plars865695b2001-08-27 22:15:12 +000056int TST_TOTAL = 1;
57extern int Tst_count;
58struct passwd *pass;
59
60void setup(void);
61void cleanup(void);
62int fail;
63
robbiew5aab8a72003-03-26 18:05:30 +000064int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000065{
66 int retval;
67 struct flock fl;
68 int pid, status;
69
plars865695b2001-08-27 22:15:12 +000070 char *msg; /* message returned from parse_opts */
71
72 /* parse standard options */
73 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
74 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
75 }
76
77 setup(); /* global setup */
78
mridgedb639212005-01-04 21:04:11 +000079/* //block1: */
vapier7ec19d92006-02-27 04:38:56 +000080#ifndef UCLINUX
81 /* Skip since uClinux does not implement memory protection */
plars865695b2001-08-27 22:15:12 +000082 tst_resm(TINFO, "Enter block 1");
83 fail = 0;
subrata_modake4cf63d2008-01-21 11:16:15 +000084 if ((fd = open("temp.dat", O_CREAT|O_RDWR, 0777)) < 0) { //mode must be specified when O_CREATE is in the flag
plars865695b2001-08-27 22:15:12 +000085 tst_resm(TFAIL, "file opening error");
86 fail = 1;
87 }
88
89 /* Error condition if address is bad */
90 retval = fcntl(fd, F_GETLK, (struct flock *)INVAL_FLAG);
91 if (errno == EFAULT) {
92 tst_resm(TPASS, "Test F_GETLK: for errno EFAULT PASSED");
93 } else {
94 tst_resm(TFAIL, "Test F_GETLK: for errno EFAULT FAILED");
95 fail = 1;
96 }
97 if (fail) {
98 tst_resm(TINFO, "Block 1 FAILED");
99 } else {
100 tst_resm(TINFO, "Block 1 PASSED");
101 }
102 tst_resm(TINFO, "Exit block 1");
vapier7ec19d92006-02-27 04:38:56 +0000103#else
104 tst_resm(TINFO, "Skip block 1 on uClinux");
105#endif
plars865695b2001-08-27 22:15:12 +0000106
mridgedb639212005-01-04 21:04:11 +0000107/* //block2: */
vapier7ec19d92006-02-27 04:38:56 +0000108#ifndef UCLINUX
109 /* Skip since uClinux does not implement memory protection */
robbiewf46d59e2005-08-03 16:53:25 +0000110 tst_resm(TINFO, "Enter block 2");
plars865695b2001-08-27 22:15:12 +0000111 fail = 0;
112 /* Error condition if address is bad */
113 retval = fcntl(fd, F_GETLK64, (struct flock *)INVAL_FLAG);
114 if (errno == EFAULT) {
115 tst_resm(TPASS, "Test F_GETLK64: for errno EFAULT PASSED");
116 } else {
117 tst_resm(TFAIL, "Test F_GETLK64: for errno EFAULT FAILED");
118 fail = 1;
119 }
120 if (fail) {
121 tst_resm(TINFO, "Block 2 FAILED");
122 } else {
123 tst_resm(TINFO, "Block 2 PASSED");
124 }
125 tst_resm(TINFO, "Exit block 2");
vapier7ec19d92006-02-27 04:38:56 +0000126#else
127 tst_resm(TINFO, "Skip block 2 on uClinux");
128#endif
plars865695b2001-08-27 22:15:12 +0000129
mridgedb639212005-01-04 21:04:11 +0000130/* //block3: */
plars865695b2001-08-27 22:15:12 +0000131 tst_resm(TINFO, "Enter block 3");
132 fail = 0;
robbiewd34d5812005-07-11 22:28:09 +0000133 if ((pid = FORK_OR_VFORK()) == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000134 fail = 0;
135 pass = getpwnam("nobody");
136 retval = setreuid(-1, pass->pw_uid);
137 if (retval < 0) {
138 tst_resm(TFAIL, "setreuid to user nobody failed, "
139 "errno: %d", errno);
140 fail = 1;
141 }
142
143 /* Error condition: invalid cmd */
144 retval = fcntl(fd, INVAL_FLAG, &fl);
145 if (errno == EINVAL) {
146 tst_resm(TPASS, "Test for errno EINVAL PASSED");
147 } else {
148 tst_resm(TFAIL, "Test for errno EINVAL FAILED, "
149 "got: %d", errno);
150 fail = 1;
151 }
152 exit(fail);
153 } else { /* parent */
154 waitpid(pid, &status, 0);
155 if (WEXITSTATUS(status) != 0) {
156 tst_resm(TFAIL, "child returned bad exit status");
157 fail = 1;
158 }
159 if (fail) {
160 tst_resm(TINFO, "Block 3 FAILED");
161 } else {
162 tst_resm(TINFO, "Block 3 PASSED");
163 }
164 }
165 tst_resm(TINFO, "Exit block 3");
166
167 cleanup();
subrata_modak43337a32009-02-26 11:43:51 +0000168 return 0;
plars865695b2001-08-27 22:15:12 +0000169}
170
171/*
172 * setup()
173 * performs all ONE TIME setup for this test
174 */
175void
176setup()
177{
178 /* capture signals */
179 tst_sig(FORK, DEF_HANDLER, cleanup);
180
181 umask(0);
182
183 /* Pause if that option was specified */
184 TEST_PAUSE;
185
186 /* make a temp directory and cd to it */
187 tst_tmpdir();
188
nstrazfa31d552002-05-14 16:50:06 +0000189 sprintf(string, "./fcntl18.%d.1", getpid());
plars865695b2001-08-27 22:15:12 +0000190 unlink(string);
191}
192
193/*
194 * cleanup()
195 * performs all the ONE TIME cleanup for this test at completion or
196 * or premature exit.
197 */
198void
199cleanup()
200{
201 /*
202 * print timing status if that option was specified.
203 * print errno log if that option was specified
204 */
mridge22c90442004-08-25 15:26:18 +0000205 close(fd);
206
plars865695b2001-08-27 22:15:12 +0000207 TEST_CLEANUP;
208
209 /* Remove tmp dir and all files in it */
210 tst_rmdir();
211
212 /* exit with return code appropriate for results */
213 unlink("temp.dat");
214 tst_exit();
215}