1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Linux Test Project
4 * Copyright (c) 2015 Cyril Hrubis <[email protected]>
5 * Copyright (c) International Business Machines Corp., 2001
6 *
7 * Ported to LTP: Wayne Boyer
8 * 04/2008 Roy Lee <[email protected]>
9 */
10
11 /*\
12 * [Description]
13 *
14 * Attempt to execve(2) a file which is being opened by another process for
15 * writing fails with ETXTBSY.
16 */
17
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <libgen.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29
30 #include "tst_test.h"
31
32 #define TEST_APP "execve_child"
33
34 static void do_child(void);
35
verify_execve(void)36 static void verify_execve(void)
37 {
38 pid_t pid;
39 char *argv[2] = {TEST_APP, NULL};
40
41 pid = SAFE_FORK();
42 if (pid == 0)
43 do_child();
44
45 TST_CHECKPOINT_WAIT(0);
46
47 TEST(execve(TEST_APP, argv, environ));
48
49 if (TST_ERR != ETXTBSY)
50 tst_res(TFAIL | TTERRNO, "execve succeeded, expected failure");
51 else
52 tst_res(TPASS | TTERRNO, "execve failed as expected");
53
54 TST_CHECKPOINT_WAKE(0);
55 }
56
do_child(void)57 static void do_child(void)
58 {
59 int fd = SAFE_OPEN(TEST_APP, O_WRONLY);
60
61 TST_CHECKPOINT_WAKE_AND_WAIT(0);
62
63 SAFE_CLOSE(fd);
64
65 exit(0);
66 }
67
setup(void)68 static void setup(void)
69 {
70 if ((tst_kvercmp(6, 11, 0)) >= 0) {
71 tst_brk(TCONF, "Skipping test, write to executed file is "
72 "allowed since 6.11-rc1.\n"
73 "2a010c412853 (\"fs: don't block i_writecount during exec\")");
74 }
75 }
76
77 static struct tst_test test = {
78 .setup = setup,
79 .test_all = verify_execve,
80 .forks_child = 1,
81 .child_needs_reinit = 1,
82 .needs_checkpoints = 1,
83 .resource_files = (const char *const []) {
84 TEST_APP,
85 NULL
86 }
87 };
88