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