blob: 36e12c4634d6547543cad9ec27ba421e9fe98b45 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * fcntl18.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000025 * Test to check the error conditions in fcntl system call
plars865695b2001-08-27 22:15:12 +000026 *
27 * USAGE
subrata_modak56207ce2009-03-23 13:35:39 +000028 * fcntl18
plars865695b2001-08-27 22:15:12 +000029 *
30 * HISTORY
31 * 07/2001 Ported by Wayne Boyer
32 *
33 * RESTRICTIONS
subrata_modak56207ce2009-03-23 13:35:39 +000034 * NONE
plars865695b2001-08-27 22:15:12 +000035 */
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"
plars865695b2001-08-27 22:15:12 +000047
48#define INVAL_FLAG -1
49#define INVAL_MIN (-2147483647L-1L)
50
51int fd;
52char string[40] = "";
53
nstrazfa31d552002-05-14 16:50:06 +000054char *TCID = "fcntl18";
plars865695b2001-08-27 22:15:12 +000055int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000056struct passwd *pass;
57
58void setup(void);
59void cleanup(void);
60int fail;
61
robbiew5aab8a72003-03-26 18:05:30 +000062int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000063{
64 int retval;
65 struct flock fl;
66 int pid, status;
67
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020068 const char *msg;
plars865695b2001-08-27 22:15:12 +000069
Garrett Cooper45e285d2010-11-22 12:19:25 -080070 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080071 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000072 }
73
subrata_modak56207ce2009-03-23 13:35:39 +000074 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000075
mridgedb639212005-01-04 21:04:11 +000076/* //block1: */
vapier7ec19d92006-02-27 04:38:56 +000077#ifndef UCLINUX
78 /* Skip since uClinux does not implement memory protection */
plars865695b2001-08-27 22:15:12 +000079 tst_resm(TINFO, "Enter block 1");
80 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +000081 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 +000082 tst_resm(TFAIL, "file opening error");
83 fail = 1;
84 }
85
86 /* Error condition if address is bad */
87 retval = fcntl(fd, F_GETLK, (struct flock *)INVAL_FLAG);
88 if (errno == EFAULT) {
89 tst_resm(TPASS, "Test F_GETLK: for errno EFAULT PASSED");
90 } else {
91 tst_resm(TFAIL, "Test F_GETLK: for errno EFAULT FAILED");
92 fail = 1;
93 }
94 if (fail) {
95 tst_resm(TINFO, "Block 1 FAILED");
96 } else {
97 tst_resm(TINFO, "Block 1 PASSED");
98 }
99 tst_resm(TINFO, "Exit block 1");
vapier7ec19d92006-02-27 04:38:56 +0000100#else
101 tst_resm(TINFO, "Skip block 1 on uClinux");
102#endif
plars865695b2001-08-27 22:15:12 +0000103
mridgedb639212005-01-04 21:04:11 +0000104/* //block2: */
vapier7ec19d92006-02-27 04:38:56 +0000105#ifndef UCLINUX
106 /* Skip since uClinux does not implement memory protection */
robbiewf46d59e2005-08-03 16:53:25 +0000107 tst_resm(TINFO, "Enter block 2");
plars865695b2001-08-27 22:15:12 +0000108 fail = 0;
109 /* Error condition if address is bad */
110 retval = fcntl(fd, F_GETLK64, (struct flock *)INVAL_FLAG);
111 if (errno == EFAULT) {
112 tst_resm(TPASS, "Test F_GETLK64: for errno EFAULT PASSED");
113 } else {
114 tst_resm(TFAIL, "Test F_GETLK64: for errno EFAULT FAILED");
115 fail = 1;
116 }
117 if (fail) {
118 tst_resm(TINFO, "Block 2 FAILED");
119 } else {
120 tst_resm(TINFO, "Block 2 PASSED");
121 }
122 tst_resm(TINFO, "Exit block 2");
vapier7ec19d92006-02-27 04:38:56 +0000123#else
124 tst_resm(TINFO, "Skip block 2 on uClinux");
125#endif
plars865695b2001-08-27 22:15:12 +0000126
mridgedb639212005-01-04 21:04:11 +0000127/* //block3: */
plars865695b2001-08-27 22:15:12 +0000128 tst_resm(TINFO, "Enter block 3");
129 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000130 if ((pid = FORK_OR_VFORK()) == 0) { /* child */
plars865695b2001-08-27 22:15:12 +0000131 fail = 0;
132 pass = getpwnam("nobody");
133 retval = setreuid(-1, pass->pw_uid);
134 if (retval < 0) {
135 tst_resm(TFAIL, "setreuid to user nobody failed, "
136 "errno: %d", errno);
137 fail = 1;
138 }
139
140 /* Error condition: invalid cmd */
141 retval = fcntl(fd, INVAL_FLAG, &fl);
142 if (errno == EINVAL) {
143 tst_resm(TPASS, "Test for errno EINVAL PASSED");
144 } else {
145 tst_resm(TFAIL, "Test for errno EINVAL FAILED, "
146 "got: %d", errno);
147 fail = 1;
148 }
149 exit(fail);
subrata_modak56207ce2009-03-23 13:35:39 +0000150 } else { /* parent */
plars865695b2001-08-27 22:15:12 +0000151 waitpid(pid, &status, 0);
152 if (WEXITSTATUS(status) != 0) {
153 tst_resm(TFAIL, "child returned bad exit status");
154 fail = 1;
155 }
156 if (fail) {
157 tst_resm(TINFO, "Block 3 FAILED");
158 } else {
159 tst_resm(TINFO, "Block 3 PASSED");
160 }
161 }
162 tst_resm(TINFO, "Exit block 3");
163
164 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800165 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800166
plars865695b2001-08-27 22:15:12 +0000167}
168
169/*
170 * setup()
subrata_modak56207ce2009-03-23 13:35:39 +0000171 * performs all ONE TIME setup for this test
plars865695b2001-08-27 22:15:12 +0000172 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400173void setup(void)
plars865695b2001-08-27 22:15:12 +0000174{
Garrett Cooper2c282152010-12-16 00:55:50 -0800175
plars865695b2001-08-27 22:15:12 +0000176 tst_sig(FORK, DEF_HANDLER, cleanup);
177
178 umask(0);
179
plars865695b2001-08-27 22:15:12 +0000180 TEST_PAUSE;
181
plars865695b2001-08-27 22:15:12 +0000182 tst_tmpdir();
183
nstrazfa31d552002-05-14 16:50:06 +0000184 sprintf(string, "./fcntl18.%d.1", getpid());
plars865695b2001-08-27 22:15:12 +0000185 unlink(string);
186}
187
188/*
189 * cleanup()
subrata_modak56207ce2009-03-23 13:35:39 +0000190 * performs all the ONE TIME cleanup for this test at completion or
plars865695b2001-08-27 22:15:12 +0000191 * or premature exit.
192 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400193void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000194{
195 /*
196 * print timing status if that option was specified.
197 * print errno log if that option was specified
198 */
subrata_modak56207ce2009-03-23 13:35:39 +0000199 close(fd);
mridge22c90442004-08-25 15:26:18 +0000200
plars865695b2001-08-27 22:15:12 +0000201 tst_rmdir();
202
plars865695b2001-08-27 22:15:12 +0000203 unlink("temp.dat");
Garrett Cooper2c282152010-12-16 00:55:50 -0800204
Chris Dearmanec6edca2012-10-17 19:54:01 -0700205}