plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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 Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 50 | #ifndef _GNU_SOURCE |
| 51 | #define _GNU_SOURCE |
| 52 | #endif |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 53 | #include <sys/types.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | #include <sys/stat.h> |
| 55 | #include <errno.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 56 | #include <fcntl.h> |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 57 | #include <pwd.h> |
| 58 | #include <stdio.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | void setup(void); |
| 62 | void cleanup(void); |
| 63 | void functest1(void); |
| 64 | void functest2(void); |
| 65 | |
| 66 | char *TCID = "creat01"; |
| 67 | int TST_TOTAL = 2; |
robbiew | e003b8c | 2001-08-31 15:41:20 +0000 | [diff] [blame] | 68 | char nobody_uid[] = "nobody"; |
| 69 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
| 71 | char filename[40]; |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 72 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | #define MODE1 0644 |
| 75 | #define MODE2 0444 |
| 76 | |
| 77 | struct test_case_t { |
| 78 | char *fname; |
| 79 | int mode; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | void (*functest) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | } TC[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | { |
| 83 | filename, MODE1, functest1}, { |
| 84 | filename, MODE2, functest2} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | { |
| 89 | int i; |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 90 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 91 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 93 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 94 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 96 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 100 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | for (i = 0; i < TST_TOTAL; i++) { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 103 | TEST(fd = creat(filename, TC[i].mode)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| 105 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_resm(TFAIL | TTERRNO, "creat failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | continue; |
| 108 | } |
| 109 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 110 | (*TC[i].functest) (); |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 111 | close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 115 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 119 | void functest1(void) |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 120 | { |
| 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 127 | void functest2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | { |
| 129 | struct stat buf; |
| 130 | |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 131 | if (stat(filename, &buf) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_brkm(TBROK | TERRNO, cleanup, "stat failed"); |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 133 | 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 139 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 141 | fd = -1; |
| 142 | |
| 143 | tst_require_root(NULL); |
| 144 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 146 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed"); |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 148 | if (setuid(ltpuser->pw_uid) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | tst_brkm(TBROK | TERRNO, NULL, "setuid failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 152 | |
| 153 | umask(0); |
| 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | TEST_PAUSE; |
| 156 | |
| 157 | tst_tmpdir(); |
| 158 | |
| 159 | sprintf(filename, "creat01.%d", getpid()); |
| 160 | } |
| 161 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 162 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
Garrett Cooper | 83599b9 | 2010-12-18 10:29:05 -0800 | [diff] [blame] | 164 | if (fd != -1) |
| 165 | close(fd); |
subrata_modak | 3fddbf6 | 2007-11-14 15:27:56 +0000 | [diff] [blame] | 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | unlink(filename); |
| 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 170 | } |