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
32static void do_child(void);
33
34static 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
55static 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
66static struct tst_test test = {
67	.test_all = verify_execve,
68	.forks_child = 1,
69	.child_needs_reinit = 1,
70	.needs_checkpoints = 1,
71	.resource_files = (const char *const []) {
72		TEST_APP,
73		NULL
74	}
75};
76