xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fmtmsg/fmtmsg01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /* 01/02/2003	Port to LTP [email protected] */
21*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001	Port to Linux	[email protected] */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker /*
24*49cdfc7eSAndroid Build Coastguard Worker  * fmtmsg(3C) and addseverity(3C)
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
27*49cdfc7eSAndroid Build Coastguard Worker  *      Check basic functionality using various messages and severity levels.
28*49cdfc7eSAndroid Build Coastguard Worker  */
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <ctype.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
35*49cdfc7eSAndroid Build Coastguard Worker #if !defined(__UCLIBC__)
36*49cdfc7eSAndroid Build Coastguard Worker #include <fmtmsg.h>		/* interface definition */
37*49cdfc7eSAndroid Build Coastguard Worker #endif
38*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
44*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
45*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fmtms01";
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
50*49cdfc7eSAndroid Build Coastguard Worker int block_number;
51*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
52*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker int anyfail();
55*49cdfc7eSAndroid Build Coastguard Worker int blenter();
56*49cdfc7eSAndroid Build Coastguard Worker int blexit();
57*49cdfc7eSAndroid Build Coastguard Worker void setup();
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker char ch;
60*49cdfc7eSAndroid Build Coastguard Worker char buf[80];
61*49cdfc7eSAndroid Build Coastguard Worker char *str1 = "LTP:fmtmsg: INFO: LTP fmtmsg() test1 message, NOT an error";
62*49cdfc7eSAndroid Build Coastguard Worker char *str2 = "TO FIX: This is correct output, no action needed  LTP:msg:001";
63*49cdfc7eSAndroid Build Coastguard Worker char *str3 = "LTP:fmtmsg: LTP_TEST: LTP fmtmsg() test2 message, NOT an error";
64*49cdfc7eSAndroid Build Coastguard Worker char *str4 = "TO FIX: This is correct output, no action needed  LTP:msg:002";
65*49cdfc7eSAndroid Build Coastguard Worker 
clearbuf(void)66*49cdfc7eSAndroid Build Coastguard Worker void clearbuf(void)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker 	int i;
69*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 80; i++)
70*49cdfc7eSAndroid Build Coastguard Worker 		buf[i] = '\0';
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker #if !defined(__UCLIBC__)
74*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])75*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker 	int fd, ret_val;
78*49cdfc7eSAndroid Build Coastguard Worker 	FILE *fp;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	setup();
83*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------*/
84*49cdfc7eSAndroid Build Coastguard Worker 	blenter();
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	/* Check that system SEV_LEVEL output is correct */
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	close(2);		/* redirect stderr to file */
89*49cdfc7eSAndroid Build Coastguard Worker 	fd = creat("fmtfile", 0644);
90*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = fmtmsg(MM_PRINT | MM_SOFT, "LTP:fmtmsg", MM_INFO,
91*49cdfc7eSAndroid Build Coastguard Worker 			 "LTP fmtmsg() test1 message, NOT an error",
92*49cdfc7eSAndroid Build Coastguard Worker 			 "This is correct output, no action needed",
93*49cdfc7eSAndroid Build Coastguard Worker 			 "LTP:msg:001");
94*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != 0) {
97*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "fmtmsg returned %d, expected 0\n\n", ret_val);
98*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
99*49cdfc7eSAndroid Build Coastguard Worker 	}
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	fp = fopen("fmtfile", "r");
102*49cdfc7eSAndroid Build Coastguard Worker 	clearbuf();
103*49cdfc7eSAndroid Build Coastguard Worker 	fread(buf, sizeof(buf[0]), strlen(str1), fp);
104*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(str1, buf) != 0) {
105*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "Expected string: %s\n", str1);
106*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "does not match\n");
107*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "received string: %s\n\n", buf);
108*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
109*49cdfc7eSAndroid Build Coastguard Worker 	}
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	/* Read past spaces in output */
112*49cdfc7eSAndroid Build Coastguard Worker 	fread(&ch, sizeof(ch), 1, fp);
113*49cdfc7eSAndroid Build Coastguard Worker 	while (isspace(ch))
114*49cdfc7eSAndroid Build Coastguard Worker 		fread(&ch, sizeof(ch), 1, fp);
115*49cdfc7eSAndroid Build Coastguard Worker 	ungetc(ch, fp);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	clearbuf();
118*49cdfc7eSAndroid Build Coastguard Worker 	fread(buf, sizeof(buf[0]), strlen(str2), fp);
119*49cdfc7eSAndroid Build Coastguard Worker 	fclose(fp);
120*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(str2, buf) != 0) {
121*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "Expected string: %s\n", str2);
122*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "does not match\n");
123*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "received string: %s\n\n", buf);
124*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
125*49cdfc7eSAndroid Build Coastguard Worker 	}
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 	blexit();
128*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------*/
129*49cdfc7eSAndroid Build Coastguard Worker 	blenter();
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	/* Check that a system defined SEV_LEVEL cannot get redefined */
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = addseverity(3, "INVALID");
134*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != MM_NOTOK) {
135*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "addseverity returned %d, expected MM_NOTOK\n",
136*49cdfc7eSAndroid Build Coastguard Worker 			ret_val);
137*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
138*49cdfc7eSAndroid Build Coastguard Worker 	}
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	blexit();
141*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------*/
142*49cdfc7eSAndroid Build Coastguard Worker 	blenter();
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	/* Check that we can define our own */
145*49cdfc7eSAndroid Build Coastguard Worker 	/* SEV_LEVEL and output is correct  */
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = addseverity(5, "LTP_TEST");
148*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != MM_OK) {
149*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "addseverity returned %d, expected MM_OK\n",
150*49cdfc7eSAndroid Build Coastguard Worker 			ret_val);
151*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
152*49cdfc7eSAndroid Build Coastguard Worker 	}
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	close(2);		/* redirect stderr to file */
155*49cdfc7eSAndroid Build Coastguard Worker 	fd = creat("fmtfile", 0644);
156*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = fmtmsg(MM_PRINT | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
157*49cdfc7eSAndroid Build Coastguard Worker 			 "LTP fmtmsg() test2 message, NOT an error",
158*49cdfc7eSAndroid Build Coastguard Worker 			 "This is correct output, no action needed",
159*49cdfc7eSAndroid Build Coastguard Worker 			 "LTP:msg:002");
160*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != 0) {
163*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "fmtmsg returned %d, expected 0\n", ret_val);
164*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
165*49cdfc7eSAndroid Build Coastguard Worker 	}
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	fp = fopen("fmtfile", "r");
168*49cdfc7eSAndroid Build Coastguard Worker 	clearbuf();
169*49cdfc7eSAndroid Build Coastguard Worker 	fread(buf, sizeof(buf[0]), strlen(str3), fp);
170*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(str3, buf) != 0) {
171*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "Expected string: %s\n", str3);
172*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "does not match\n");
173*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "received string: %s\n\n", buf);
174*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
175*49cdfc7eSAndroid Build Coastguard Worker 	}
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 	/* Read past spaces in output */
178*49cdfc7eSAndroid Build Coastguard Worker 	fread(&ch, sizeof(ch), 1, fp);
179*49cdfc7eSAndroid Build Coastguard Worker 	while (isspace(ch))
180*49cdfc7eSAndroid Build Coastguard Worker 		fread(&ch, sizeof(ch), 1, fp);
181*49cdfc7eSAndroid Build Coastguard Worker 	ungetc(ch, fp);
182*49cdfc7eSAndroid Build Coastguard Worker 
183*49cdfc7eSAndroid Build Coastguard Worker 	clearbuf();
184*49cdfc7eSAndroid Build Coastguard Worker 	fread(buf, sizeof(buf[0]), strlen(str4), fp);
185*49cdfc7eSAndroid Build Coastguard Worker 	if (strcmp(str4, buf) != 0) {
186*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "Expected string: %s\n", str4);
187*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "does not match\n");
188*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "received string: %s\n\n", buf);
189*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
190*49cdfc7eSAndroid Build Coastguard Worker 	}
191*49cdfc7eSAndroid Build Coastguard Worker 
192*49cdfc7eSAndroid Build Coastguard Worker 	fclose(fp);
193*49cdfc7eSAndroid Build Coastguard Worker 	remove("fmtfile");
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker 	blexit();
196*49cdfc7eSAndroid Build Coastguard Worker /*--------------------------------------------------------------*/
197*49cdfc7eSAndroid Build Coastguard Worker 	blenter();
198*49cdfc7eSAndroid Build Coastguard Worker 
199*49cdfc7eSAndroid Build Coastguard Worker 	/* Test result of writing to /dev/console */
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	ret_val = fmtmsg(MM_CONSOLE | MM_HARD | MM_OPSYS, "LTP:fmtmsg", 5,
202*49cdfc7eSAndroid Build Coastguard Worker 			 "LTP fmtmsg() test3 message, NOT an error",
203*49cdfc7eSAndroid Build Coastguard Worker 			 "This is correct output, no action needed",
204*49cdfc7eSAndroid Build Coastguard Worker 			 "LTP:msg:003");
205*49cdfc7eSAndroid Build Coastguard Worker 	if (ret_val != MM_OK) {
206*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "fmtmsg returned %d, expected MM_OK\n", ret_val);
207*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(temp, "failed to write to console\n\n");
208*49cdfc7eSAndroid Build Coastguard Worker 		local_flag = FAILED;
209*49cdfc7eSAndroid Build Coastguard Worker 	}
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	blexit();
212*49cdfc7eSAndroid Build Coastguard Worker 	anyfail();
213*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
214*49cdfc7eSAndroid Build Coastguard Worker }
215*49cdfc7eSAndroid Build Coastguard Worker 
anyfail(void)216*49cdfc7eSAndroid Build Coastguard Worker int anyfail(void)
217*49cdfc7eSAndroid Build Coastguard Worker {
218*49cdfc7eSAndroid Build Coastguard Worker 	(local_flag == FAILED) ? tst_resm(TFAIL,
219*49cdfc7eSAndroid Build Coastguard Worker 					  "Test failed") : tst_resm(TPASS,
220*49cdfc7eSAndroid Build Coastguard Worker 								    "Test passed");
221*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
222*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
223*49cdfc7eSAndroid Build Coastguard Worker }
224*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)225*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
226*49cdfc7eSAndroid Build Coastguard Worker {
227*49cdfc7eSAndroid Build Coastguard Worker 	temp = stderr;
228*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
229*49cdfc7eSAndroid Build Coastguard Worker }
230*49cdfc7eSAndroid Build Coastguard Worker 
blenter(void)231*49cdfc7eSAndroid Build Coastguard Worker int blenter(void)
232*49cdfc7eSAndroid Build Coastguard Worker {
233*49cdfc7eSAndroid Build Coastguard Worker 	local_flag = PASSED;
234*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
235*49cdfc7eSAndroid Build Coastguard Worker }
236*49cdfc7eSAndroid Build Coastguard Worker 
blexit(void)237*49cdfc7eSAndroid Build Coastguard Worker int blexit(void)
238*49cdfc7eSAndroid Build Coastguard Worker {
239*49cdfc7eSAndroid Build Coastguard Worker 	(local_flag == FAILED) ? tst_resm(TFAIL,
240*49cdfc7eSAndroid Build Coastguard Worker 					  "Test failed") : tst_resm(TPASS,
241*49cdfc7eSAndroid Build Coastguard Worker 								    "Test passed");
242*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
243*49cdfc7eSAndroid Build Coastguard Worker }
244*49cdfc7eSAndroid Build Coastguard Worker 
245*49cdfc7eSAndroid Build Coastguard Worker #else
246*49cdfc7eSAndroid Build Coastguard Worker 
main(void)247*49cdfc7eSAndroid Build Coastguard Worker int main(void)
248*49cdfc7eSAndroid Build Coastguard Worker {
249*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TCONF, NULL, "test is not available on uClibc");
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker 
252*49cdfc7eSAndroid Build Coastguard Worker #endif /* if !defined(__UCLIBC__) */
253