xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/creat/creat07.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2012-2016 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Testcase to check creat(2) sets ETXTBSY correctly.
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define TEST_APP "creat07_child"
21*49cdfc7eSAndroid Build Coastguard Worker 
verify_creat(void)22*49cdfc7eSAndroid Build Coastguard Worker static void verify_creat(void)
23*49cdfc7eSAndroid Build Coastguard Worker {
24*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
27*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
28*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_EXECL(TEST_APP, TEST_APP, NULL);
29*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
30*49cdfc7eSAndroid Build Coastguard Worker 	}
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	TEST(creat(TEST_APP, O_WRONLY));
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "creat() succeeded unexpectedly");
38*49cdfc7eSAndroid Build Coastguard Worker 		return;
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == ETXTBSY)
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "creat() received EXTBSY");
43*49cdfc7eSAndroid Build Coastguard Worker 	else
44*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "creat() failed unexpectedly");
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_KILL(pid, SIGKILL);
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(pid, NULL, 0);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)50*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	if ((tst_kvercmp(6, 11, 0)) >= 0) {
53*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "Skipping test, write to executed file is "
54*49cdfc7eSAndroid Build Coastguard Worker 			"allowed since 6.11-rc1.\n"
55*49cdfc7eSAndroid Build Coastguard Worker 			"2a010c412853 (\"fs: don't block i_writecount during exec\")");
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
60*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
61*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_creat,
62*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
63*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 	.resource_files = (const char *const []) {
65*49cdfc7eSAndroid Build Coastguard Worker 		TEST_APP,
66*49cdfc7eSAndroid Build Coastguard Worker 		NULL
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker };
69