1 /* 2 * Copyright (c) International Business Machines Corp., 2001 3 * 07/2001 Ported by Wayne Boyer 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 Foundation, 17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /* 21 * Testcase to check creat(2) fails with EACCES 22 */ 23 24 #include <stdio.h> 25 #include <errno.h> 26 #include <fcntl.h> 27 #include <pwd.h> 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 31 #include "tst_test.h" 32 33 #define DIRNAME "testdir" 34 #define FILENAME DIRNAME"/file1" 35 36 static uid_t nobody_uid; 37 38 static struct tcase { 39 const char *fname; 40 } tcases[] = { 41 {DIRNAME "/file"}, 42 {FILENAME} 43 }; 44 45 static void child_fn(unsigned int i) 46 { 47 SAFE_SETEUID(nobody_uid); 48 49 TEST(creat(tcases[i].fname, 0444)); 50 51 if (TEST_RETURN != -1) { 52 SAFE_UNLINK(tcases[i].fname); 53 tst_res(TFAIL, "call succeeded unexpectedly"); 54 return; 55 } 56 57 if (TEST_ERRNO != EACCES) { 58 tst_res(TFAIL | TTERRNO, "Expected EACCES"); 59 return; 60 } 61 62 tst_res(TPASS, "call failed with EACCES as expected"); 63 } 64 65 static void verify_creat(unsigned int i) 66 { 67 if (SAFE_FORK() == 0) 68 child_fn(i); 69 } 70 71 void setup(void) 72 { 73 struct passwd *pw; 74 int fd; 75 76 pw = SAFE_GETPWNAM("nobody"); 77 nobody_uid = pw->pw_uid; 78 79 SAFE_MKDIR(DIRNAME, 0700); 80 fd = SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, 0444); 81 SAFE_CLOSE(fd); 82 } 83 84 static struct tst_test test = { 85 .tid = "creat04", 86 .tcnt = ARRAY_SIZE(tcases), 87 .test = verify_creat, 88 .needs_tmpdir = 1, 89 .needs_root = 1, 90 .forks_child = 1, 91 .setup = setup, 92 }; 93