xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/unlink/unlink05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  * Copyright (c) Linux Test Project, 2006-2024
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Check the basic functionality of the unlink(2):
11  *
12  * - unlink(2) can delete regular file successfully.
13  * - unlink(2) can delete fifo file successfully.
14  */
15 
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include "tst_test.h"
21 
file_create(char * name)22 static void file_create(char *name)
23 {
24 	sprintf(name, "tfile_%d", getpid());
25 	SAFE_TOUCH(name, 0777, NULL);
26 }
27 
fifo_create(char * name)28 static void fifo_create(char *name)
29 {
30 	sprintf(name, "tfifo_%d", getpid());
31 	SAFE_MKFIFO(name, 0777);
32 }
33 
34 static struct test_case_t {
35 	void (*setupfunc)(char *name);
36 	char *desc;
37 } tcases[] = {
38 	{file_create, "file"},
39 	{fifo_create, "fifo"},
40 };
41 
verify_unlink(unsigned int n)42 static void verify_unlink(unsigned int n)
43 {
44 	char fname[255];
45 	struct test_case_t *tc = &tcases[n];
46 
47 	tc->setupfunc(fname);
48 
49 	TEST(unlink(fname));
50 	if (TST_RET == -1) {
51 		tst_res(TFAIL | TTERRNO, "unlink(%s) failed", tc->desc);
52 		return;
53 	}
54 
55 	if (!access(fname, F_OK)) {
56 		tst_res(TFAIL, "unlink(%s) succeeded, but %s still existed",
57 			tc->desc, tc->desc);
58 		return;
59 	}
60 
61 	tst_res(TPASS, "unlink(%s) succeeded", tc->desc);
62 }
63 
64 static struct tst_test test = {
65 	.needs_tmpdir = 1,
66 	.tcnt = ARRAY_SIZE(tcases),
67 	.test = verify_unlink,
68 };
69