blob: fa587adb5c3f800a211859fd6db0fce7d4701b3a [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
22 * creat01.c
23 *
24 * DESCRIPTION
25 * Testcase to check the basic functionality of the creat(2) system call.
26 *
27 * ALGORITHM
28 * 1. creat() a file using 0444 mode, write to the fildes, write
29 * should return a positive count.
30 *
31 * 2. creat() should truncate a file to 0 bytes if it already
32 * exists, and should not fail.
33 *
34 * USAGE: <for command-line>
35 * creat01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -f : Turn off functionality Testing.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * HISTORY
44 * 07/2001 Ported by Wayne Boyer
45 *
46 * RESTRICTIONS
47 * None
48 */
49
Garrett Cooper87bc45c2010-12-17 01:56:08 -080050#ifndef _GNU_SOURCE
51#define _GNU_SOURCE
52#endif
plars74948ad2002-11-14 16:16:14 +000053#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000054#include <sys/stat.h>
55#include <errno.h>
robbiew2c945242002-11-11 19:01:25 +000056#include <fcntl.h>
Garrett Cooper87bc45c2010-12-17 01:56:08 -080057#include <pwd.h>
58#include <stdio.h>
plars865695b2001-08-27 22:15:12 +000059#include "test.h"
plars865695b2001-08-27 22:15:12 +000060
61void setup(void);
62void cleanup(void);
63void functest1(void);
64void functest2(void);
65
66char *TCID = "creat01";
67int TST_TOTAL = 2;
robbiewe003b8c2001-08-31 15:41:20 +000068char nobody_uid[] = "nobody";
69struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000070
71char filename[40];
Garrett Cooper83599b92010-12-18 10:29:05 -080072int fd;
plars865695b2001-08-27 22:15:12 +000073
74#define MODE1 0644
75#define MODE2 0444
76
77struct test_case_t {
78 char *fname;
79 int mode;
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 void (*functest) ();
plars865695b2001-08-27 22:15:12 +000081} TC[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 {
83 filename, MODE1, functest1}, {
84 filename, MODE2, functest2}
plars865695b2001-08-27 22:15:12 +000085};
86
subrata_modak56207ce2009-03-23 13:35:39 +000087int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000088{
89 int i;
Garrett Cooper83599b92010-12-18 10:29:05 -080090 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020091 const char *msg;
plars865695b2001-08-27 22:15:12 +000092
Garrett Cooper83599b92010-12-18 10:29:05 -080093 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080094 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000095
Garrett Cooper83599b92010-12-18 10:29:05 -080096 setup();
plars865695b2001-08-27 22:15:12 +000097
plars865695b2001-08-27 22:15:12 +000098 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
Caspar Zhangd59a6592013-03-07 14:59:12 +0800100 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000101
subrata_modak56207ce2009-03-23 13:35:39 +0000102 for (i = 0; i < TST_TOTAL; i++) {
Garrett Cooper83599b92010-12-18 10:29:05 -0800103 TEST(fd = creat(filename, TC[i].mode));
plars865695b2001-08-27 22:15:12 +0000104
105 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_resm(TFAIL | TTERRNO, "creat failed");
plars865695b2001-08-27 22:15:12 +0000107 continue;
108 }
109
Cyril Hrubise38b9612014-06-02 17:20:57 +0200110 (*TC[i].functest) ();
Garrett Cooper83599b92010-12-18 10:29:05 -0800111 close(fd);
plars865695b2001-08-27 22:15:12 +0000112 }
113 }
plars865695b2001-08-27 22:15:12 +0000114
Cyril Hrubise38b9612014-06-02 17:20:57 +0200115 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800116 tst_exit();
plars865695b2001-08-27 22:15:12 +0000117}
118
Mike Frysingerc57fba52014-04-09 18:56:30 -0400119void functest1(void)
Garrett Cooper83599b92010-12-18 10:29:05 -0800120{
121 if (write(TEST_RETURN, "A", 1) != 1)
122 tst_resm(TFAIL, "write was unsuccessful");
123 else
124 tst_resm(TPASS, "file was created and written to successfully");
125}
126
Mike Frysingerc57fba52014-04-09 18:56:30 -0400127void functest2(void)
plars865695b2001-08-27 22:15:12 +0000128{
129 struct stat buf;
130
Garrett Cooper83599b92010-12-18 10:29:05 -0800131 if (stat(filename, &buf) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TBROK | TERRNO, cleanup, "stat failed");
Garrett Cooper83599b92010-12-18 10:29:05 -0800133 if (buf.st_size == 0)
134 tst_resm(TPASS, "creat truncated existing file to 0 bytes");
135 else
136 tst_resm(TFAIL, "creat FAILED to truncate file to 0 bytes");
plars865695b2001-08-27 22:15:12 +0000137}
138
Mike Frysingerc57fba52014-04-09 18:56:30 -0400139void setup(void)
plars865695b2001-08-27 22:15:12 +0000140{
Garrett Cooper83599b92010-12-18 10:29:05 -0800141 fd = -1;
142
143 tst_require_root(NULL);
144
subrata_modak56207ce2009-03-23 13:35:39 +0000145 ltpuser = getpwnam(nobody_uid);
Garrett Cooper83599b92010-12-18 10:29:05 -0800146 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
vapierdac68732009-08-28 12:15:15 +0000148 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 tst_brkm(TBROK | TERRNO, NULL, "setuid failed");
plars865695b2001-08-27 22:15:12 +0000150
plars865695b2001-08-27 22:15:12 +0000151 tst_sig(NOFORK, DEF_HANDLER, cleanup);
152
153 umask(0);
154
plars865695b2001-08-27 22:15:12 +0000155 TEST_PAUSE;
156
157 tst_tmpdir();
158
159 sprintf(filename, "creat01.%d", getpid());
160}
161
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000163{
Garrett Cooper83599b92010-12-18 10:29:05 -0800164 if (fd != -1)
165 close(fd);
subrata_modak3fddbf62007-11-14 15:27:56 +0000166
plars865695b2001-08-27 22:15:12 +0000167 unlink(filename);
168
plars865695b2001-08-27 22:15:12 +0000169 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700170}