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