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