1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker *
8*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker * or the like. Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker * otherwise, applies only to this software file. Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24*49cdfc7eSAndroid Build Coastguard Worker * Mountain View, CA 94043, or:
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * http://www.sgi.com
27*49cdfc7eSAndroid Build Coastguard Worker *
28*49cdfc7eSAndroid Build Coastguard Worker * For further information regarding this notice, see:
29*49cdfc7eSAndroid Build Coastguard Worker *
30*49cdfc7eSAndroid Build Coastguard Worker * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31*49cdfc7eSAndroid Build Coastguard Worker *
32*49cdfc7eSAndroid Build Coastguard Worker */
33*49cdfc7eSAndroid Build Coastguard Worker /* $Id: fcntl10.c,v 1.8 2009/10/26 14:55:46 subrata_modak Exp $ */
34*49cdfc7eSAndroid Build Coastguard Worker /**********************************************************
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker * OS Test - Silicon Graphics, Inc.
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * TEST IDENTIFIER : fcntl10
39*49cdfc7eSAndroid Build Coastguard Worker *
40*49cdfc7eSAndroid Build Coastguard Worker * EXECUTED BY : anyone
41*49cdfc7eSAndroid Build Coastguard Worker *
42*49cdfc7eSAndroid Build Coastguard Worker * TEST TITLE : Basic test for fcntl(2) using F_SETLKW argument.
43*49cdfc7eSAndroid Build Coastguard Worker *
44*49cdfc7eSAndroid Build Coastguard Worker * PARENT DOCUMENT : usctpl01
45*49cdfc7eSAndroid Build Coastguard Worker *
46*49cdfc7eSAndroid Build Coastguard Worker * TEST CASE TOTAL : 2
47*49cdfc7eSAndroid Build Coastguard Worker *
48*49cdfc7eSAndroid Build Coastguard Worker * WALL CLOCK TIME : 1
49*49cdfc7eSAndroid Build Coastguard Worker *
50*49cdfc7eSAndroid Build Coastguard Worker * CPU TYPES : ALL
51*49cdfc7eSAndroid Build Coastguard Worker *
52*49cdfc7eSAndroid Build Coastguard Worker * AUTHOR : William Roske
53*49cdfc7eSAndroid Build Coastguard Worker *
54*49cdfc7eSAndroid Build Coastguard Worker * CO-PILOT : Dave Fenner
55*49cdfc7eSAndroid Build Coastguard Worker *
56*49cdfc7eSAndroid Build Coastguard Worker * DATE STARTED : 03/30/92
57*49cdfc7eSAndroid Build Coastguard Worker *
58*49cdfc7eSAndroid Build Coastguard Worker * INITIAL RELEASE : UNICOS 7.0
59*49cdfc7eSAndroid Build Coastguard Worker *
60*49cdfc7eSAndroid Build Coastguard Worker * TEST CASES
61*49cdfc7eSAndroid Build Coastguard Worker *
62*49cdfc7eSAndroid Build Coastguard Worker * 1.) fcntl(2) returns...(See Description)
63*49cdfc7eSAndroid Build Coastguard Worker *
64*49cdfc7eSAndroid Build Coastguard Worker * INPUT SPECIFICATIONS
65*49cdfc7eSAndroid Build Coastguard Worker * The standard options for system call tests are accepted.
66*49cdfc7eSAndroid Build Coastguard Worker * (See the parse_opts(3) man page).
67*49cdfc7eSAndroid Build Coastguard Worker *
68*49cdfc7eSAndroid Build Coastguard Worker * OUTPUT SPECIFICATIONS
69*49cdfc7eSAndroid Build Coastguard Worker *
70*49cdfc7eSAndroid Build Coastguard Worker * DURATION
71*49cdfc7eSAndroid Build Coastguard Worker * Terminates - with frequency and infinite modes.
72*49cdfc7eSAndroid Build Coastguard Worker *
73*49cdfc7eSAndroid Build Coastguard Worker * SIGNALS
74*49cdfc7eSAndroid Build Coastguard Worker * Uses SIGUSR1 to pause before test if option set.
75*49cdfc7eSAndroid Build Coastguard Worker * (See the parse_opts(3) man page).
76*49cdfc7eSAndroid Build Coastguard Worker *
77*49cdfc7eSAndroid Build Coastguard Worker * RESOURCES
78*49cdfc7eSAndroid Build Coastguard Worker * None
79*49cdfc7eSAndroid Build Coastguard Worker *
80*49cdfc7eSAndroid Build Coastguard Worker * ENVIRONMENTAL NEEDS
81*49cdfc7eSAndroid Build Coastguard Worker * No run-time environmental needs.
82*49cdfc7eSAndroid Build Coastguard Worker *
83*49cdfc7eSAndroid Build Coastguard Worker * SPECIAL PROCEDURAL REQUIREMENTS
84*49cdfc7eSAndroid Build Coastguard Worker * None
85*49cdfc7eSAndroid Build Coastguard Worker *
86*49cdfc7eSAndroid Build Coastguard Worker * INTERCASE DEPENDENCIES
87*49cdfc7eSAndroid Build Coastguard Worker * None
88*49cdfc7eSAndroid Build Coastguard Worker *
89*49cdfc7eSAndroid Build Coastguard Worker * DETAILED DESCRIPTION
90*49cdfc7eSAndroid Build Coastguard Worker * This is a Phase I test for the fcntl(2) system call. It is intended
91*49cdfc7eSAndroid Build Coastguard Worker * to provide a limited exposure of the system call, for now. It
92*49cdfc7eSAndroid Build Coastguard Worker * should/will be extended when full functional tests are written for
93*49cdfc7eSAndroid Build Coastguard Worker * fcntl(2).
94*49cdfc7eSAndroid Build Coastguard Worker *
95*49cdfc7eSAndroid Build Coastguard Worker * Setup:
96*49cdfc7eSAndroid Build Coastguard Worker * Setup signal handling.
97*49cdfc7eSAndroid Build Coastguard Worker * Pause for SIGUSR1 if option specified.
98*49cdfc7eSAndroid Build Coastguard Worker *
99*49cdfc7eSAndroid Build Coastguard Worker * Test:
100*49cdfc7eSAndroid Build Coastguard Worker * Loop if the proper options are given.
101*49cdfc7eSAndroid Build Coastguard Worker * Execute system call
102*49cdfc7eSAndroid Build Coastguard Worker * Check return code, if system call failed (return=-1)
103*49cdfc7eSAndroid Build Coastguard Worker * Log the errno and Issue a FAIL message.
104*49cdfc7eSAndroid Build Coastguard Worker * Otherwise, Issue a PASS message.
105*49cdfc7eSAndroid Build Coastguard Worker *
106*49cdfc7eSAndroid Build Coastguard Worker * Cleanup:
107*49cdfc7eSAndroid Build Coastguard Worker * Print errno log and/or timing stats if options given
108*49cdfc7eSAndroid Build Coastguard Worker *
109*49cdfc7eSAndroid Build Coastguard Worker *
110*49cdfc7eSAndroid Build Coastguard Worker *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
113*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
114*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
115*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
116*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
117*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
118*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
119*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker void setup();
122*49cdfc7eSAndroid Build Coastguard Worker void cleanup();
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fcntl10";
125*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 2;
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker char fname[255];
128*49cdfc7eSAndroid Build Coastguard Worker int fd;
129*49cdfc7eSAndroid Build Coastguard Worker struct flock flocks;
130*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)131*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
132*49cdfc7eSAndroid Build Coastguard Worker {
133*49cdfc7eSAndroid Build Coastguard Worker int lc;
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
136*49cdfc7eSAndroid Build Coastguard Worker * parse standard options
137*49cdfc7eSAndroid Build Coastguard Worker ***************************************************************/
138*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
141*49cdfc7eSAndroid Build Coastguard Worker * perform global setup for test
142*49cdfc7eSAndroid Build Coastguard Worker ***************************************************************/
143*49cdfc7eSAndroid Build Coastguard Worker setup();
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
146*49cdfc7eSAndroid Build Coastguard Worker * check looping state if -c option given
147*49cdfc7eSAndroid Build Coastguard Worker ***************************************************************/
148*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
149*49cdfc7eSAndroid Build Coastguard Worker int type;
150*49cdfc7eSAndroid Build Coastguard Worker for (type = 0; type < 2; type++) {
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker flocks.l_type = type ? F_RDLCK : F_WRLCK;
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker /*
157*49cdfc7eSAndroid Build Coastguard Worker * Call fcntl(2) with F_SETLKW flocks.l_type = F_UNLCK argument on fname
158*49cdfc7eSAndroid Build Coastguard Worker */
159*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_SETLKW, &flocks));
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker /* check return code */
162*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
163*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
164*49cdfc7eSAndroid Build Coastguard Worker "fcntl(%s, F_SETLKW, &flocks) flocks.l_type = %s Failed, errno=%d : %s",
165*49cdfc7eSAndroid Build Coastguard Worker fname, type ? "F_RDLCK" : "F_WRLCK",
166*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO, strerror(TEST_ERRNO));
167*49cdfc7eSAndroid Build Coastguard Worker } else {
168*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS,
169*49cdfc7eSAndroid Build Coastguard Worker "fcntl(%s, F_SETLKW, &flocks) flocks.l_type = %s returned %ld",
170*49cdfc7eSAndroid Build Coastguard Worker fname, type ? "F_RDLCK" : "F_WRLCK",
171*49cdfc7eSAndroid Build Coastguard Worker TEST_RETURN);
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker flocks.l_type = F_UNLCK;
175*49cdfc7eSAndroid Build Coastguard Worker /*
176*49cdfc7eSAndroid Build Coastguard Worker * Call fcntl(2) with F_SETLKW flocks.l_type = F_UNLCK argument on fname
177*49cdfc7eSAndroid Build Coastguard Worker */
178*49cdfc7eSAndroid Build Coastguard Worker TEST(fcntl(fd, F_SETLKW, &flocks));
179*49cdfc7eSAndroid Build Coastguard Worker
180*49cdfc7eSAndroid Build Coastguard Worker /* check return code */
181*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
182*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
183*49cdfc7eSAndroid Build Coastguard Worker "fcntl(%s, F_SETLKW, &flocks) flocks.l_type = F_UNLCK Failed, errno=%d : %s",
184*49cdfc7eSAndroid Build Coastguard Worker fname, TEST_ERRNO,
185*49cdfc7eSAndroid Build Coastguard Worker strerror(TEST_ERRNO));
186*49cdfc7eSAndroid Build Coastguard Worker } else {
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS,
189*49cdfc7eSAndroid Build Coastguard Worker "fcntl(%s, F_SETLKW, &flocks) flocks.l_type = F_UNLCK returned %ld",
190*49cdfc7eSAndroid Build Coastguard Worker fname, TEST_RETURN);
191*49cdfc7eSAndroid Build Coastguard Worker }
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker
196*49cdfc7eSAndroid Build Coastguard Worker cleanup();
197*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
198*49cdfc7eSAndroid Build Coastguard Worker }
199*49cdfc7eSAndroid Build Coastguard Worker
200*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
201*49cdfc7eSAndroid Build Coastguard Worker * setup() - performs all ONE TIME setup for this test.
202*49cdfc7eSAndroid Build Coastguard Worker ***************************************************************/
setup(void)203*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
204*49cdfc7eSAndroid Build Coastguard Worker {
205*49cdfc7eSAndroid Build Coastguard Worker
206*49cdfc7eSAndroid Build Coastguard Worker tst_sig(NOFORK, DEF_HANDLER, cleanup);
207*49cdfc7eSAndroid Build Coastguard Worker
208*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
209*49cdfc7eSAndroid Build Coastguard Worker
210*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
211*49cdfc7eSAndroid Build Coastguard Worker
212*49cdfc7eSAndroid Build Coastguard Worker sprintf(fname, "tfile_%d", getpid());
213*49cdfc7eSAndroid Build Coastguard Worker if ((fd = creat(fname, 0644)) == -1) {
214*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
215*49cdfc7eSAndroid Build Coastguard Worker "creat(%s, 0644) Failed, errno=%d : %s", fname, errno,
216*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
217*49cdfc7eSAndroid Build Coastguard Worker } else if (close(fd) == -1) {
218*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s",
219*49cdfc7eSAndroid Build Coastguard Worker fname, errno, strerror(errno));
220*49cdfc7eSAndroid Build Coastguard Worker } else if ((fd = open(fname, O_RDWR, 0700)) == -1) {
221*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
222*49cdfc7eSAndroid Build Coastguard Worker "open(%s, O_RDWR,0700) Failed, errno=%d : %s", fname,
223*49cdfc7eSAndroid Build Coastguard Worker errno, strerror(errno));
224*49cdfc7eSAndroid Build Coastguard Worker }
225*49cdfc7eSAndroid Build Coastguard Worker
226*49cdfc7eSAndroid Build Coastguard Worker /* set needed fields in the flocks structure */
227*49cdfc7eSAndroid Build Coastguard Worker flocks.l_whence = 1;
228*49cdfc7eSAndroid Build Coastguard Worker flocks.l_start = 0;
229*49cdfc7eSAndroid Build Coastguard Worker flocks.l_len = 0;
230*49cdfc7eSAndroid Build Coastguard Worker flocks.l_pid = getpid();
231*49cdfc7eSAndroid Build Coastguard Worker }
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Worker /***************************************************************
234*49cdfc7eSAndroid Build Coastguard Worker * cleanup() - performs all ONE TIME cleanup for this test at
235*49cdfc7eSAndroid Build Coastguard Worker * completion or premature exit.
236*49cdfc7eSAndroid Build Coastguard Worker ***************************************************************/
cleanup(void)237*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
238*49cdfc7eSAndroid Build Coastguard Worker {
239*49cdfc7eSAndroid Build Coastguard Worker
240*49cdfc7eSAndroid Build Coastguard Worker if (close(fd) == -1) {
241*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", fname, errno,
242*49cdfc7eSAndroid Build Coastguard Worker strerror(errno));
243*49cdfc7eSAndroid Build Coastguard Worker } else if (unlink(fname) == -1) {
244*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN, "unlink(%s) Failed, errno=%d : %s", fname,
245*49cdfc7eSAndroid Build Coastguard Worker errno, strerror(errno));
246*49cdfc7eSAndroid Build Coastguard Worker }
247*49cdfc7eSAndroid Build Coastguard Worker
248*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
249*49cdfc7eSAndroid Build Coastguard Worker
250*49cdfc7eSAndroid Build Coastguard Worker }
251