xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/creat/creat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * 1. creat() a file using 0444 mode, write to the fildes, write
9*49cdfc7eSAndroid Build Coastguard Worker  *    should return a positive count.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * 2. creat() should truncate a file to 0 bytes if it already
12*49cdfc7eSAndroid Build Coastguard Worker  *    exists, and should not fail.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static char filename[40];
24*49cdfc7eSAndroid Build Coastguard Worker static int fd;
25*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)26*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(filename, "creat01.%d", getpid());
29*49cdfc7eSAndroid Build Coastguard Worker }
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
32*49cdfc7eSAndroid Build Coastguard Worker 	int mode;
33*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{0644},
35*49cdfc7eSAndroid Build Coastguard Worker 	{0444}
36*49cdfc7eSAndroid Build Coastguard Worker };
37*49cdfc7eSAndroid Build Coastguard Worker 
verify_creat(unsigned int i)38*49cdfc7eSAndroid Build Coastguard Worker static void verify_creat(unsigned int i)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	struct stat buf;
41*49cdfc7eSAndroid Build Coastguard Worker 	char c;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(filename, tcases[i].mode);
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(filename, &buf);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (buf.st_size != 0)
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "creat() failed to truncate file to 0 bytes");
49*49cdfc7eSAndroid Build Coastguard Worker 	else
50*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "creat() truncated file to 0 bytes");
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fd, "A", 1) != 1)
53*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "write was unsuccessful");
54*49cdfc7eSAndroid Build Coastguard Worker 	else
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "file was created and written to successfully");
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	if (read(fd, &c, 1) != -1)
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "read succeeded unexpectedly");
59*49cdfc7eSAndroid Build Coastguard Worker 	else
60*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TERRNO, "read failed expectedly");
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)65*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
68*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
72*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = 2,
73*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_creat,
74*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
75*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
76*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
77*49cdfc7eSAndroid Build Coastguard Worker };
78