xref: /aosp_15_r20/external/ltp/lib/tests/trerrno.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2014 Linux Test Project, Inc.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define OUTPUT_FNAME "output"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "trerrno";
29*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
30*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)31*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
32*49cdfc7eSAndroid Build Coastguard Worker {
33*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)36*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
main(void)41*49cdfc7eSAndroid Build Coastguard Worker int main(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	int fd, stdout_fd;
44*49cdfc7eSAndroid Build Coastguard Worker 	char msg[4096], *pos;
45*49cdfc7eSAndroid Build Coastguard Worker 	FILE *f;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	setup();
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	/* redirect stdout to file */
50*49cdfc7eSAndroid Build Coastguard Worker 	stdout_fd = dup(fileno(stdout));
51*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(NULL, OUTPUT_FNAME, O_RDWR | O_CREAT, 0666);
52*49cdfc7eSAndroid Build Coastguard Worker 	TEST(dup2(fd, fileno(stdout)));
53*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == -1)
54*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TTERRNO, cleanup, "dup2");
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	errno = EPERM;
57*49cdfc7eSAndroid Build Coastguard Worker 	TEST_ERRNO = EPERM;
58*49cdfc7eSAndroid Build Coastguard Worker 	TEST_RETURN = EINVAL;
59*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO | TRERRNO, "test");
60*49cdfc7eSAndroid Build Coastguard Worker 	tst_old_flush();
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	/* restore stdout */
63*49cdfc7eSAndroid Build Coastguard Worker 	TEST(dup2(stdout_fd, fileno(stdout)));
64*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN == -1)
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TTERRNO, cleanup, "dup2");
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	/* read file and verify that output is as expected */
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
69*49cdfc7eSAndroid Build Coastguard Worker 	f = fdopen(fd, "r");
70*49cdfc7eSAndroid Build Coastguard Worker 	if (!f)
71*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "fdopen");
72*49cdfc7eSAndroid Build Coastguard Worker 	if (!fgets(msg, sizeof(msg), f))
73*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "fgets");
74*49cdfc7eSAndroid Build Coastguard Worker 	fclose(f);
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	pos = strchr(msg, '\n');
77*49cdfc7eSAndroid Build Coastguard Worker 	if (pos != NULL)
78*49cdfc7eSAndroid Build Coastguard Worker 		*pos = '\0';
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "%s", msg);
81*49cdfc7eSAndroid Build Coastguard Worker 	if (strstr(msg, "EPERM"))
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "EPERM shouldn't be in TRERRNO output");
83*49cdfc7eSAndroid Build Coastguard Worker 	if (strstr(msg, "EINVAL"))
84*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "EINVAL should be in TRERRNO output");
85*49cdfc7eSAndroid Build Coastguard Worker 	else
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "EINVAL not found in TRERRNO output");
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
89*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
90*49cdfc7eSAndroid Build Coastguard Worker }
91