xref: /aosp_15_r20/external/ltp/testcases/kernel/logging/kmsg/kmsg01.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) 2013 Linux Test Project
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker /*
6*49cdfc7eSAndroid Build Coastguard Worker  * Test /dev/kmsg based on kernel doc: Documentation/ABI/testing/dev-kmsg
7*49cdfc7eSAndroid Build Coastguard Worker  * - read() blocks
8*49cdfc7eSAndroid Build Coastguard Worker  * - non-blocking read() fails with EAGAIN
9*49cdfc7eSAndroid Build Coastguard Worker  * - partial read fails (buffer smaller than message)
10*49cdfc7eSAndroid Build Coastguard Worker  * - can write to /dev/kmsg and message seqno grows
11*49cdfc7eSAndroid Build Coastguard Worker  * - first read() after open() returns same message
12*49cdfc7eSAndroid Build Coastguard Worker  * - if messages get overwritten, read() returns -EPIPE
13*49cdfc7eSAndroid Build Coastguard Worker  * - device supports SEEK_SET, SEEK_END, SEEK_DATA
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #define MSG_PREFIX "LTP kmsg01 "
31*49cdfc7eSAndroid Build Coastguard Worker #define MAX_MSGSIZE 4096
32*49cdfc7eSAndroid Build Coastguard Worker #define NUM_READ_MSGS 3
33*49cdfc7eSAndroid Build Coastguard Worker #define NUM_READ_RETRY 10
34*49cdfc7eSAndroid Build Coastguard Worker #define NUM_OVERWRITE_MSGS 1024
35*49cdfc7eSAndroid Build Coastguard Worker #define PRINTK "/proc/sys/kernel/printk"
36*49cdfc7eSAndroid Build Coastguard Worker #define CONSOLE_LOGLEVEL_QUIET   4
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static int console_loglevel = -1;
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker /*
41*49cdfc7eSAndroid Build Coastguard Worker  * inject_msg - write message to /dev/kmsg
42*49cdfc7eSAndroid Build Coastguard Worker  * @msg: null-terminated message to inject, should end with \n
43*49cdfc7eSAndroid Build Coastguard Worker  *
44*49cdfc7eSAndroid Build Coastguard Worker  * RETURNS:
45*49cdfc7eSAndroid Build Coastguard Worker  *   0 on success
46*49cdfc7eSAndroid Build Coastguard Worker  *  -1 on failure, errno reflects write() errno
47*49cdfc7eSAndroid Build Coastguard Worker  */
inject_msg(const char * msg)48*49cdfc7eSAndroid Build Coastguard Worker static int inject_msg(const char *msg)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	int f;
51*49cdfc7eSAndroid Build Coastguard Worker 	f = SAFE_OPEN("/dev/kmsg", O_WRONLY);
52*49cdfc7eSAndroid Build Coastguard Worker 	TEST(write(f, msg, strlen(msg)));
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(f);
54*49cdfc7eSAndroid Build Coastguard Worker 	errno = TST_ERR;
55*49cdfc7eSAndroid Build Coastguard Worker 	return TST_RET;
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker /*
59*49cdfc7eSAndroid Build Coastguard Worker  * find_msg - find message in kernel ring buffer
60*49cdfc7eSAndroid Build Coastguard Worker  * @fd:           fd to use, if < 0 function opens /dev/kmsg itself
61*49cdfc7eSAndroid Build Coastguard Worker  * @text_to_find: substring to look for in messages
62*49cdfc7eSAndroid Build Coastguard Worker  * @buf:          buf to store found message
63*49cdfc7eSAndroid Build Coastguard Worker  * @bufsize:      size of 'buf'
64*49cdfc7eSAndroid Build Coastguard Worker  * @first:        1 - return first matching message
65*49cdfc7eSAndroid Build Coastguard Worker  *                0 - return last matching message
66*49cdfc7eSAndroid Build Coastguard Worker  * RETURNS:
67*49cdfc7eSAndroid Build Coastguard Worker  *   0 on success
68*49cdfc7eSAndroid Build Coastguard Worker  *  -1 on failure
69*49cdfc7eSAndroid Build Coastguard Worker  */
find_msg(int fd,const char * text_to_find,char * buf,int bufsize,int first)70*49cdfc7eSAndroid Build Coastguard Worker static int find_msg(int fd, const char *text_to_find, char *buf, int bufsize,
71*49cdfc7eSAndroid Build Coastguard Worker 	int first)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker 	int f, msg_found = 0;
74*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE + 1];
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	if (fd < 0) {
77*49cdfc7eSAndroid Build Coastguard Worker 		f = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
78*49cdfc7eSAndroid Build Coastguard Worker 	} else {
79*49cdfc7eSAndroid Build Coastguard Worker 		f = fd;
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
83*49cdfc7eSAndroid Build Coastguard Worker 		TEST(read(f, msg, MAX_MSGSIZE));
84*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET < 0) {
85*49cdfc7eSAndroid Build Coastguard Worker 			if (TST_ERR == EAGAIN)
86*49cdfc7eSAndroid Build Coastguard Worker 				/* there are no more messages */
87*49cdfc7eSAndroid Build Coastguard Worker 				break;
88*49cdfc7eSAndroid Build Coastguard Worker 			else if (TST_ERR == EPIPE)
89*49cdfc7eSAndroid Build Coastguard Worker 				/* current message was overwritten */
90*49cdfc7eSAndroid Build Coastguard Worker 				continue;
91*49cdfc7eSAndroid Build Coastguard Worker 			else
92*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK|TTERRNO, "err reading /dev/kmsg");
93*49cdfc7eSAndroid Build Coastguard Worker 		} else if (TST_RET < bufsize) {
94*49cdfc7eSAndroid Build Coastguard Worker 			/* lines from kmsg are not NULL terminated */
95*49cdfc7eSAndroid Build Coastguard Worker 			msg[TST_RET] = '\0';
96*49cdfc7eSAndroid Build Coastguard Worker 			if (strstr(msg, text_to_find) != NULL) {
97*49cdfc7eSAndroid Build Coastguard Worker 				strncpy(buf, msg, bufsize);
98*49cdfc7eSAndroid Build Coastguard Worker 				msg_found = 1;
99*49cdfc7eSAndroid Build Coastguard Worker 				if (first)
100*49cdfc7eSAndroid Build Coastguard Worker 					break;
101*49cdfc7eSAndroid Build Coastguard Worker 			}
102*49cdfc7eSAndroid Build Coastguard Worker 		}
103*49cdfc7eSAndroid Build Coastguard Worker 	}
104*49cdfc7eSAndroid Build Coastguard Worker 	if (fd < 0)
105*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(f);
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	if (msg_found)
108*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
109*49cdfc7eSAndroid Build Coastguard Worker 	else
110*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker 
get_msg_fields(const char * msg,unsigned long * prio,unsigned long * seqno)113*49cdfc7eSAndroid Build Coastguard Worker static int get_msg_fields(const char *msg, unsigned long *prio,
114*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long *seqno)
115*49cdfc7eSAndroid Build Coastguard Worker {
116*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long s, p;
117*49cdfc7eSAndroid Build Coastguard Worker 	if (sscanf(msg, "%lu,%lu,", &p, &s) == 2) {
118*49cdfc7eSAndroid Build Coastguard Worker 		if (prio)
119*49cdfc7eSAndroid Build Coastguard Worker 			*prio = p;
120*49cdfc7eSAndroid Build Coastguard Worker 		if (seqno)
121*49cdfc7eSAndroid Build Coastguard Worker 			*seqno = s;
122*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
123*49cdfc7eSAndroid Build Coastguard Worker 	} else {
124*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
125*49cdfc7eSAndroid Build Coastguard Worker 	}
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker /*
129*49cdfc7eSAndroid Build Coastguard Worker  * timed_read - if possible reads from fd or times out
130*49cdfc7eSAndroid Build Coastguard Worker  * @fd:           fd to read from
131*49cdfc7eSAndroid Build Coastguard Worker  * @timeout_usec: timeout in useconds
132*49cdfc7eSAndroid Build Coastguard Worker  *
133*49cdfc7eSAndroid Build Coastguard Worker  * RETURNS:
134*49cdfc7eSAndroid Build Coastguard Worker  *   read bytes on successful read
135*49cdfc7eSAndroid Build Coastguard Worker  *  -1 on read() error, errno reflects read() errno
136*49cdfc7eSAndroid Build Coastguard Worker  *  -2 on timeout
137*49cdfc7eSAndroid Build Coastguard Worker  */
timed_read(int fd,long timeout_usec)138*49cdfc7eSAndroid Build Coastguard Worker static int timed_read(int fd, long timeout_usec)
139*49cdfc7eSAndroid Build Coastguard Worker {
140*49cdfc7eSAndroid Build Coastguard Worker 	int ret, tmp;
141*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval timeout;
142*49cdfc7eSAndroid Build Coastguard Worker 	fd_set read_fds;
143*49cdfc7eSAndroid Build Coastguard Worker 
144*49cdfc7eSAndroid Build Coastguard Worker 	FD_ZERO(&read_fds);
145*49cdfc7eSAndroid Build Coastguard Worker 	FD_SET(fd, &read_fds);
146*49cdfc7eSAndroid Build Coastguard Worker 	timeout.tv_sec = timeout_usec / 1000000;
147*49cdfc7eSAndroid Build Coastguard Worker 	timeout.tv_usec = timeout_usec % 1000000;
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	ret = select(fd + 1, &read_fds, 0, 0, &timeout);
150*49cdfc7eSAndroid Build Coastguard Worker 	switch (ret) {
151*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
152*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TERRNO, "select failed");
153*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
154*49cdfc7eSAndroid Build Coastguard Worker 		/* select timed out */
155*49cdfc7eSAndroid Build Coastguard Worker 		return -2;
156*49cdfc7eSAndroid Build Coastguard Worker 	}
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker 	return read(fd, &tmp, 1);
159*49cdfc7eSAndroid Build Coastguard Worker }
160*49cdfc7eSAndroid Build Coastguard Worker 
161*49cdfc7eSAndroid Build Coastguard Worker /*
162*49cdfc7eSAndroid Build Coastguard Worker  * timed_read_kmsg - reads file until it reaches end of file,
163*49cdfc7eSAndroid Build Coastguard Worker  *                   read fails or times out. This ignores any
164*49cdfc7eSAndroid Build Coastguard Worker  *                   EPIPE errors.
165*49cdfc7eSAndroid Build Coastguard Worker  * @fd:           fd to read from
166*49cdfc7eSAndroid Build Coastguard Worker  * @timeout_usec: timeout in useconds for every read attempt
167*49cdfc7eSAndroid Build Coastguard Worker  *
168*49cdfc7eSAndroid Build Coastguard Worker  * RETURNS:
169*49cdfc7eSAndroid Build Coastguard Worker  *     0 on read reaching eof
170*49cdfc7eSAndroid Build Coastguard Worker  *    -1 on read error, errno reflects read() errno
171*49cdfc7eSAndroid Build Coastguard Worker  *    -2 on timeout
172*49cdfc7eSAndroid Build Coastguard Worker  */
timed_read_kmsg(int fd,long timeout_usec)173*49cdfc7eSAndroid Build Coastguard Worker static int timed_read_kmsg(int fd, long timeout_usec)
174*49cdfc7eSAndroid Build Coastguard Worker {
175*49cdfc7eSAndroid Build Coastguard Worker 	int child, status, ret = 0;
176*49cdfc7eSAndroid Build Coastguard Worker 	int pipefd[2];
177*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE];
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker 	if (pipe(pipefd) != 0)
180*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TERRNO, "pipe failed");
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 	child = fork();
183*49cdfc7eSAndroid Build Coastguard Worker 	switch (child) {
184*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
185*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TERRNO, "failed to fork");
186*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
187*49cdfc7eSAndroid Build Coastguard Worker 		/* child does all the reading and keeps writing to
188*49cdfc7eSAndroid Build Coastguard Worker 		 * pipe to let parent know that it didn't block */
189*49cdfc7eSAndroid Build Coastguard Worker 		close(pipefd[0]);
190*49cdfc7eSAndroid Build Coastguard Worker 		while (1) {
191*49cdfc7eSAndroid Build Coastguard Worker 			if (write(pipefd[1], "", 1) == -1)
192*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK|TERRNO, "write to pipe");
193*49cdfc7eSAndroid Build Coastguard Worker 			TEST(read(fd, msg, MAX_MSGSIZE));
194*49cdfc7eSAndroid Build Coastguard Worker 			if (TST_RET == 0)
195*49cdfc7eSAndroid Build Coastguard Worker 				break;
196*49cdfc7eSAndroid Build Coastguard Worker 			if (TST_RET == -1 && TST_ERR != EPIPE) {
197*49cdfc7eSAndroid Build Coastguard Worker 				ret = TST_ERR;
198*49cdfc7eSAndroid Build Coastguard Worker 				break;
199*49cdfc7eSAndroid Build Coastguard Worker 			}
200*49cdfc7eSAndroid Build Coastguard Worker 		}
201*49cdfc7eSAndroid Build Coastguard Worker 
202*49cdfc7eSAndroid Build Coastguard Worker 		close(pipefd[1]);
203*49cdfc7eSAndroid Build Coastguard Worker 		exit(ret);
204*49cdfc7eSAndroid Build Coastguard Worker 	}
205*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipefd[1]);
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 	/* parent reads pipe until it reaches eof or until read times out */
208*49cdfc7eSAndroid Build Coastguard Worker 	do {
209*49cdfc7eSAndroid Build Coastguard Worker 		TEST(timed_read(pipefd[0], timeout_usec));
210*49cdfc7eSAndroid Build Coastguard Worker 	} while (TST_RET > 0);
211*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipefd[0]);
212*49cdfc7eSAndroid Build Coastguard Worker 
213*49cdfc7eSAndroid Build Coastguard Worker 	/* child is blocked, kill it */
214*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -2)
215*49cdfc7eSAndroid Build Coastguard Worker 		kill(child, SIGTERM);
216*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(child, &status, 0);
217*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status)) {
218*49cdfc7eSAndroid Build Coastguard Worker 		if (WEXITSTATUS(status) == 0) {
219*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
220*49cdfc7eSAndroid Build Coastguard Worker 		} else {
221*49cdfc7eSAndroid Build Coastguard Worker 			errno = WEXITSTATUS(status);
222*49cdfc7eSAndroid Build Coastguard Worker 			return -1;
223*49cdfc7eSAndroid Build Coastguard Worker 		}
224*49cdfc7eSAndroid Build Coastguard Worker 	}
225*49cdfc7eSAndroid Build Coastguard Worker 	return -2;
226*49cdfc7eSAndroid Build Coastguard Worker }
227*49cdfc7eSAndroid Build Coastguard Worker 
test_read_nonblock(void)228*49cdfc7eSAndroid Build Coastguard Worker static void test_read_nonblock(void)
229*49cdfc7eSAndroid Build Coastguard Worker {
230*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
231*49cdfc7eSAndroid Build Coastguard Worker 
232*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: nonblock read");
233*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
234*49cdfc7eSAndroid Build Coastguard Worker 
235*49cdfc7eSAndroid Build Coastguard Worker 	TEST(timed_read_kmsg(fd, 5000000));
236*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TST_ERR == EAGAIN)
237*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "non-block read returned EAGAIN");
238*49cdfc7eSAndroid Build Coastguard Worker 	else
239*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TTERRNO, "non-block read returned: %ld",
240*49cdfc7eSAndroid Build Coastguard Worker 			TST_RET);
241*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
242*49cdfc7eSAndroid Build Coastguard Worker }
243*49cdfc7eSAndroid Build Coastguard Worker 
test_read_block(void)244*49cdfc7eSAndroid Build Coastguard Worker static void test_read_block(void)
245*49cdfc7eSAndroid Build Coastguard Worker {
246*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
247*49cdfc7eSAndroid Build Coastguard Worker 
248*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: blocking read");
249*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("/dev/kmsg", O_RDONLY);
250*49cdfc7eSAndroid Build Coastguard Worker 
251*49cdfc7eSAndroid Build Coastguard Worker 	TEST(timed_read_kmsg(fd, 500000));
252*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -2)
253*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "read blocked");
254*49cdfc7eSAndroid Build Coastguard Worker 	else
255*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TTERRNO, "read returned: %ld", TST_RET);
256*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
257*49cdfc7eSAndroid Build Coastguard Worker }
258*49cdfc7eSAndroid Build Coastguard Worker 
test_partial_read(void)259*49cdfc7eSAndroid Build Coastguard Worker static void test_partial_read(void)
260*49cdfc7eSAndroid Build Coastguard Worker {
261*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE];
262*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
263*49cdfc7eSAndroid Build Coastguard Worker 
264*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: partial read");
265*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
266*49cdfc7eSAndroid Build Coastguard Worker 
267*49cdfc7eSAndroid Build Coastguard Worker 	TEST(read(fd, msg, 1));
268*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET < 0)
269*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS|TTERRNO, "read failed as expected");
270*49cdfc7eSAndroid Build Coastguard Worker 	else
271*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "read returned: %ld", TST_RET);
272*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
273*49cdfc7eSAndroid Build Coastguard Worker }
274*49cdfc7eSAndroid Build Coastguard Worker 
test_inject(void)275*49cdfc7eSAndroid Build Coastguard Worker static void test_inject(void)
276*49cdfc7eSAndroid Build Coastguard Worker {
277*49cdfc7eSAndroid Build Coastguard Worker 	char imsg[MAX_MSGSIZE], imsg_prefixed[MAX_MSGSIZE];
278*49cdfc7eSAndroid Build Coastguard Worker 	char tmp[MAX_MSGSIZE];
279*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long prefix, seqno, seqno_last = 0;
280*49cdfc7eSAndroid Build Coastguard Worker 	int i, facility, prio;
281*49cdfc7eSAndroid Build Coastguard Worker 
282*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: injected messages appear in /dev/kmsg");
283*49cdfc7eSAndroid Build Coastguard Worker 
284*49cdfc7eSAndroid Build Coastguard Worker 	srand(time(NULL));
285*49cdfc7eSAndroid Build Coastguard Worker 	/* test all combinations of prio 0-7, facility 0-15 */
286*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 127; i++) {
287*49cdfc7eSAndroid Build Coastguard Worker 		prio = (i & 7);
288*49cdfc7eSAndroid Build Coastguard Worker 		facility = (i >> 3);
289*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(imsg, MSG_PREFIX"TEST MESSAGE %ld prio: %d, "
290*49cdfc7eSAndroid Build Coastguard Worker 			"facility: %d\n", random(), prio, facility);
291*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(imsg_prefixed, "<%d>%s", i, imsg);
292*49cdfc7eSAndroid Build Coastguard Worker 
293*49cdfc7eSAndroid Build Coastguard Worker 		if (inject_msg(imsg_prefixed) == -1) {
294*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL|TERRNO, "inject failed");
295*49cdfc7eSAndroid Build Coastguard Worker 			return;
296*49cdfc7eSAndroid Build Coastguard Worker 		}
297*49cdfc7eSAndroid Build Coastguard Worker 
298*49cdfc7eSAndroid Build Coastguard Worker 		/* check that message appears in log */
299*49cdfc7eSAndroid Build Coastguard Worker 		if (find_msg(-1, imsg, tmp, sizeof(tmp), 0) == -1) {
300*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "failed to find: %s", imsg);
301*49cdfc7eSAndroid Build Coastguard Worker 			return;
302*49cdfc7eSAndroid Build Coastguard Worker 		}
303*49cdfc7eSAndroid Build Coastguard Worker 
304*49cdfc7eSAndroid Build Coastguard Worker 		/* check that facility is not 0 (LOG_KERN). */
305*49cdfc7eSAndroid Build Coastguard Worker 		if (get_msg_fields(tmp, &prefix, &seqno) != 0) {
306*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "failed to parse seqid: %s", tmp);
307*49cdfc7eSAndroid Build Coastguard Worker 			return;
308*49cdfc7eSAndroid Build Coastguard Worker 		}
309*49cdfc7eSAndroid Build Coastguard Worker 		if (prefix >> 3 == 0) {
310*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "facility 0 found: %s", tmp);
311*49cdfc7eSAndroid Build Coastguard Worker 			return;
312*49cdfc7eSAndroid Build Coastguard Worker 		}
313*49cdfc7eSAndroid Build Coastguard Worker 
314*49cdfc7eSAndroid Build Coastguard Worker 		/* check that seq. number grows */
315*49cdfc7eSAndroid Build Coastguard Worker 		if (seqno > seqno_last) {
316*49cdfc7eSAndroid Build Coastguard Worker 			seqno_last = seqno;
317*49cdfc7eSAndroid Build Coastguard Worker 		} else {
318*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "seqno doesn't grow: %lu, "
319*49cdfc7eSAndroid Build Coastguard Worker 				"last: %lu", seqno, seqno_last);
320*49cdfc7eSAndroid Build Coastguard Worker 			return;
321*49cdfc7eSAndroid Build Coastguard Worker 		}
322*49cdfc7eSAndroid Build Coastguard Worker 	}
323*49cdfc7eSAndroid Build Coastguard Worker 
324*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "injected messages found in log");
325*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "sequence numbers grow as expected");
326*49cdfc7eSAndroid Build Coastguard Worker }
327*49cdfc7eSAndroid Build Coastguard Worker 
test_read_returns_first_message(void)328*49cdfc7eSAndroid Build Coastguard Worker static void test_read_returns_first_message(void)
329*49cdfc7eSAndroid Build Coastguard Worker {
330*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long seqno[NUM_READ_MSGS + 1];
331*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE];
332*49cdfc7eSAndroid Build Coastguard Worker 	int msgs_match = 1;
333*49cdfc7eSAndroid Build Coastguard Worker 	int i, fd, j = NUM_READ_RETRY;
334*49cdfc7eSAndroid Build Coastguard Worker 
335*49cdfc7eSAndroid Build Coastguard Worker 	/* Open extra fd, which we try to read after reading NUM_READ_MSGS.
336*49cdfc7eSAndroid Build Coastguard Worker 	 * If this read fails with EPIPE, first message was overwritten and
337*49cdfc7eSAndroid Build Coastguard Worker 	 * we should retry the whole test. If it still fails after
338*49cdfc7eSAndroid Build Coastguard Worker 	 * NUM_READ_RETRY attempts, report TWARN */
339*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: mult. readers will get same first message");
340*49cdfc7eSAndroid Build Coastguard Worker 	while (j) {
341*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
342*49cdfc7eSAndroid Build Coastguard Worker 
343*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < NUM_READ_MSGS; i++) {
344*49cdfc7eSAndroid Build Coastguard Worker 			if (find_msg(-1, "", msg, sizeof(msg), 1) != 0)
345*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TFAIL, "failed to find any message");
346*49cdfc7eSAndroid Build Coastguard Worker 			if (get_msg_fields(msg, NULL, &seqno[i]) != 0)
347*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TFAIL, "failed to parse seqid: %s",
348*49cdfc7eSAndroid Build Coastguard Worker 					msg);
349*49cdfc7eSAndroid Build Coastguard Worker 		}
350*49cdfc7eSAndroid Build Coastguard Worker 
351*49cdfc7eSAndroid Build Coastguard Worker 		TEST(read(fd, msg, sizeof(msg)));
352*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
353*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET != -1)
354*49cdfc7eSAndroid Build Coastguard Worker 			break;
355*49cdfc7eSAndroid Build Coastguard Worker 
356*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_ERR == EPIPE)
357*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "msg overwritten, retrying");
358*49cdfc7eSAndroid Build Coastguard Worker 		else
359*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL|TTERRNO, "read failed");
360*49cdfc7eSAndroid Build Coastguard Worker 
361*49cdfc7eSAndroid Build Coastguard Worker 		/* give a moment to whoever overwrote first message to finish */
362*49cdfc7eSAndroid Build Coastguard Worker 		usleep(100000);
363*49cdfc7eSAndroid Build Coastguard Worker 		j--;
364*49cdfc7eSAndroid Build Coastguard Worker 	}
365*49cdfc7eSAndroid Build Coastguard Worker 
366*49cdfc7eSAndroid Build Coastguard Worker 	if (!j) {
367*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN, "exceeded: %d attempts", NUM_READ_RETRY);
368*49cdfc7eSAndroid Build Coastguard Worker 		return;
369*49cdfc7eSAndroid Build Coastguard Worker 	}
370*49cdfc7eSAndroid Build Coastguard Worker 
371*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < NUM_READ_MSGS - 1; i++)
372*49cdfc7eSAndroid Build Coastguard Worker 		if (seqno[i] != seqno[i + 1])
373*49cdfc7eSAndroid Build Coastguard Worker 			msgs_match = 0;
374*49cdfc7eSAndroid Build Coastguard Worker 	if (msgs_match) {
375*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "all readers got same message on first read");
376*49cdfc7eSAndroid Build Coastguard Worker 	} else {
377*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "readers got different messages");
378*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < NUM_READ_MSGS; i++)
379*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "msg%d: %lu", i, seqno[i]);
380*49cdfc7eSAndroid Build Coastguard Worker 	}
381*49cdfc7eSAndroid Build Coastguard Worker }
382*49cdfc7eSAndroid Build Coastguard Worker 
test_messages_overwritten(void)383*49cdfc7eSAndroid Build Coastguard Worker static void test_messages_overwritten(void)
384*49cdfc7eSAndroid Build Coastguard Worker {
385*49cdfc7eSAndroid Build Coastguard Worker 	int i, fd;
386*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE];
387*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long first_seqno = 0, seqno;
388*49cdfc7eSAndroid Build Coastguard Worker 	char filler_str[] = "<7>"MSG_PREFIX"FILLER MESSAGE TO OVERWRITE OTHERS\n";
389*49cdfc7eSAndroid Build Coastguard Worker 
390*49cdfc7eSAndroid Build Coastguard Worker 	/* Keep injecting messages until we overwrite first one.
391*49cdfc7eSAndroid Build Coastguard Worker 	 * We know first message is overwritten when its seqno changes */
392*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: read returns EPIPE when messages get "
393*49cdfc7eSAndroid Build Coastguard Worker 		"overwritten");
394*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
395*49cdfc7eSAndroid Build Coastguard Worker 
396*49cdfc7eSAndroid Build Coastguard Worker 	if (find_msg(-1, "", msg, sizeof(msg), 1) == 0
397*49cdfc7eSAndroid Build Coastguard Worker 		&& get_msg_fields(msg, NULL, &first_seqno) == 0) {
398*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "first seqno: %lu", first_seqno);
399*49cdfc7eSAndroid Build Coastguard Worker 	} else {
400*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "failed to get first seq. number");
401*49cdfc7eSAndroid Build Coastguard Worker 	}
402*49cdfc7eSAndroid Build Coastguard Worker 
403*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
404*49cdfc7eSAndroid Build Coastguard Worker 		if (find_msg(-1, "", msg, sizeof(msg), 1) != 0
405*49cdfc7eSAndroid Build Coastguard Worker 				|| get_msg_fields(msg, NULL, &seqno) != 0) {
406*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "failed to get first seq. number");
407*49cdfc7eSAndroid Build Coastguard Worker 			break;
408*49cdfc7eSAndroid Build Coastguard Worker 		}
409*49cdfc7eSAndroid Build Coastguard Worker 		if (first_seqno != seqno) {
410*49cdfc7eSAndroid Build Coastguard Worker 			/* first message was overwritten */
411*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO, "first seqno now: %lu", seqno);
412*49cdfc7eSAndroid Build Coastguard Worker 			break;
413*49cdfc7eSAndroid Build Coastguard Worker 		}
414*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < NUM_OVERWRITE_MSGS; i++) {
415*49cdfc7eSAndroid Build Coastguard Worker 			if (inject_msg(filler_str) == -1)
416*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK|TERRNO, "err write to /dev/kmsg");
417*49cdfc7eSAndroid Build Coastguard Worker 		}
418*49cdfc7eSAndroid Build Coastguard Worker 	}
419*49cdfc7eSAndroid Build Coastguard Worker 
420*49cdfc7eSAndroid Build Coastguard Worker 	/* first message is overwritten, so this next read should fail */
421*49cdfc7eSAndroid Build Coastguard Worker 	TEST(read(fd, msg, sizeof(msg)));
422*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TST_ERR == EPIPE)
423*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "read failed with EPIPE as expected");
424*49cdfc7eSAndroid Build Coastguard Worker 	else
425*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TTERRNO, "read returned: %ld", TST_RET);
426*49cdfc7eSAndroid Build Coastguard Worker 
427*49cdfc7eSAndroid Build Coastguard Worker 	/* seek position is updated to the next available record */
428*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: Subsequent reads() will return available "
429*49cdfc7eSAndroid Build Coastguard Worker 		"records again");
430*49cdfc7eSAndroid Build Coastguard Worker 	if (find_msg(fd, "", msg, sizeof(msg), 1) != 0)
431*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TTERRNO, "read returned: %ld", TST_RET);
432*49cdfc7eSAndroid Build Coastguard Worker 	else
433*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "after EPIPE read returned next record");
434*49cdfc7eSAndroid Build Coastguard Worker 
435*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
436*49cdfc7eSAndroid Build Coastguard Worker }
437*49cdfc7eSAndroid Build Coastguard Worker 
read_msg_seqno(int fd,unsigned long * seqno)438*49cdfc7eSAndroid Build Coastguard Worker static int read_msg_seqno(int fd, unsigned long *seqno)
439*49cdfc7eSAndroid Build Coastguard Worker {
440*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE];
441*49cdfc7eSAndroid Build Coastguard Worker 
442*49cdfc7eSAndroid Build Coastguard Worker 	TEST(read(fd, msg, sizeof(msg)));
443*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1 && TST_ERR != EPIPE)
444*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TTERRNO, "failed to read /dev/kmsg");
445*49cdfc7eSAndroid Build Coastguard Worker 
446*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EPIPE)
447*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
448*49cdfc7eSAndroid Build Coastguard Worker 
449*49cdfc7eSAndroid Build Coastguard Worker 	if (get_msg_fields(msg, NULL, seqno) != 0) {
450*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "failed to parse seqid: %s", msg);
451*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
452*49cdfc7eSAndroid Build Coastguard Worker 	}
453*49cdfc7eSAndroid Build Coastguard Worker 
454*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
455*49cdfc7eSAndroid Build Coastguard Worker }
456*49cdfc7eSAndroid Build Coastguard Worker 
test_seek(void)457*49cdfc7eSAndroid Build Coastguard Worker static void test_seek(void)
458*49cdfc7eSAndroid Build Coastguard Worker {
459*49cdfc7eSAndroid Build Coastguard Worker 	int j, fd, fd2;
460*49cdfc7eSAndroid Build Coastguard Worker 	char msg[MAX_MSGSIZE];
461*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long seqno[2], tmp;
462*49cdfc7eSAndroid Build Coastguard Worker 	int ret = 0;
463*49cdfc7eSAndroid Build Coastguard Worker 
464*49cdfc7eSAndroid Build Coastguard Worker 	/* 1. read() after SEEK_SET 0 returns same (first) message */
465*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: seek SEEK_SET 0");
466*49cdfc7eSAndroid Build Coastguard Worker 
467*49cdfc7eSAndroid Build Coastguard Worker 	for (j = 0; j < NUM_READ_RETRY && !ret; j++) {
468*49cdfc7eSAndroid Build Coastguard Worker 		/*
469*49cdfc7eSAndroid Build Coastguard Worker 		 * j > 0 means we are trying again, because we most likely
470*49cdfc7eSAndroid Build Coastguard Worker 		 * failed on read returning EPIPE - first message in buffer
471*49cdfc7eSAndroid Build Coastguard Worker 		 * has been overwrittern. Give a moment to whoever overwrote
472*49cdfc7eSAndroid Build Coastguard Worker 		 * first message to finish.
473*49cdfc7eSAndroid Build Coastguard Worker 		 */
474*49cdfc7eSAndroid Build Coastguard Worker 		if (j)
475*49cdfc7eSAndroid Build Coastguard Worker 			usleep(100000);
476*49cdfc7eSAndroid Build Coastguard Worker 
477*49cdfc7eSAndroid Build Coastguard Worker 		/*
478*49cdfc7eSAndroid Build Coastguard Worker 		 * Open 2 fds. Use fd1 to read seqno1, then seek to
479*49cdfc7eSAndroid Build Coastguard Worker 		 * begininng and read seqno2. Use fd2 to check if
480*49cdfc7eSAndroid Build Coastguard Worker 		 * first entry in buffer got overwritten. If so,
481*49cdfc7eSAndroid Build Coastguard Worker 		 * we'll have to repeat the test.
482*49cdfc7eSAndroid Build Coastguard Worker 		 */
483*49cdfc7eSAndroid Build Coastguard Worker 		fd = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
484*49cdfc7eSAndroid Build Coastguard Worker 		fd2 = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
485*49cdfc7eSAndroid Build Coastguard Worker 
486*49cdfc7eSAndroid Build Coastguard Worker 		if (read_msg_seqno(fd, &seqno[0]))
487*49cdfc7eSAndroid Build Coastguard Worker 			goto close_fds;
488*49cdfc7eSAndroid Build Coastguard Worker 
489*49cdfc7eSAndroid Build Coastguard Worker 		if (lseek(fd, 0, SEEK_SET) == -1) {
490*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL|TERRNO, "SEEK_SET 0 failed");
491*49cdfc7eSAndroid Build Coastguard Worker 			ret = -1;
492*49cdfc7eSAndroid Build Coastguard Worker 			goto close_fds;
493*49cdfc7eSAndroid Build Coastguard Worker 		}
494*49cdfc7eSAndroid Build Coastguard Worker 
495*49cdfc7eSAndroid Build Coastguard Worker 		if (read_msg_seqno(fd, &seqno[1]))
496*49cdfc7eSAndroid Build Coastguard Worker 			goto close_fds;
497*49cdfc7eSAndroid Build Coastguard Worker 
498*49cdfc7eSAndroid Build Coastguard Worker 		if (read_msg_seqno(fd2, &tmp))
499*49cdfc7eSAndroid Build Coastguard Worker 			goto close_fds;
500*49cdfc7eSAndroid Build Coastguard Worker 
501*49cdfc7eSAndroid Build Coastguard Worker 		ret = 1;
502*49cdfc7eSAndroid Build Coastguard Worker close_fds:
503*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
504*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd2);
505*49cdfc7eSAndroid Build Coastguard Worker 	}
506*49cdfc7eSAndroid Build Coastguard Worker 
507*49cdfc7eSAndroid Build Coastguard Worker 	if (j == NUM_READ_RETRY) {
508*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TWARN, "exceeded: %d attempts", NUM_READ_RETRY);
509*49cdfc7eSAndroid Build Coastguard Worker 	} else if (ret > 0) {
510*49cdfc7eSAndroid Build Coastguard Worker 		if (seqno[0] != seqno[1]) {
511*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "SEEK_SET 0, %lu != %lu",
512*49cdfc7eSAndroid Build Coastguard Worker 				seqno[0], seqno[1]);
513*49cdfc7eSAndroid Build Coastguard Worker 		} else {
514*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS, "SEEK_SET 0");
515*49cdfc7eSAndroid Build Coastguard Worker 		}
516*49cdfc7eSAndroid Build Coastguard Worker 	}
517*49cdfc7eSAndroid Build Coastguard Worker 
518*49cdfc7eSAndroid Build Coastguard Worker 	/* 2. messages after SEEK_END 0 shouldn't contain MSG_PREFIX */
519*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("/dev/kmsg", O_RDONLY | O_NONBLOCK);
520*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: seek SEEK_END 0");
521*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fd, 0, SEEK_END) == -1)
522*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, "lseek SEEK_END 0 failed");
523*49cdfc7eSAndroid Build Coastguard Worker 	if (find_msg(fd, MSG_PREFIX, msg, sizeof(msg), 0) != 0)
524*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "SEEK_END 0");
525*49cdfc7eSAndroid Build Coastguard Worker 	else
526*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "SEEK_END 0 found: %s", msg);
527*49cdfc7eSAndroid Build Coastguard Worker 
528*49cdfc7eSAndroid Build Coastguard Worker #ifdef SEEK_DATA
529*49cdfc7eSAndroid Build Coastguard Worker 	/* 3. messages after SEEK_DATA 0 shouldn't contain MSG_PREFIX */
530*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "TEST: seek SEEK_DATA 0");
531*49cdfc7eSAndroid Build Coastguard Worker 
532*49cdfc7eSAndroid Build Coastguard Worker 	/* clear ring buffer */
533*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_syscall(__NR_syslog, 5, NULL, 0) == -1)
534*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK|TERRNO, "syslog clear failed");
535*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fd, 0, SEEK_DATA) == -1)
536*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL|TERRNO, "lseek SEEK_DATA 0 failed");
537*49cdfc7eSAndroid Build Coastguard Worker 	if (find_msg(fd, MSG_PREFIX, msg, sizeof(msg), 0) != 0)
538*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "SEEK_DATA 0");
539*49cdfc7eSAndroid Build Coastguard Worker 	else
540*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "SEEK_DATA 0 found: %s", msg);
541*49cdfc7eSAndroid Build Coastguard Worker #endif
542*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
543*49cdfc7eSAndroid Build Coastguard Worker }
544*49cdfc7eSAndroid Build Coastguard Worker 
test_kmsg(void)545*49cdfc7eSAndroid Build Coastguard Worker static void test_kmsg(void)
546*49cdfc7eSAndroid Build Coastguard Worker {
547*49cdfc7eSAndroid Build Coastguard Worker 	/* run test_inject first so log isn't empty for other tests */
548*49cdfc7eSAndroid Build Coastguard Worker 	test_inject();
549*49cdfc7eSAndroid Build Coastguard Worker 	test_read_nonblock();
550*49cdfc7eSAndroid Build Coastguard Worker 	test_read_block();
551*49cdfc7eSAndroid Build Coastguard Worker 	test_partial_read();
552*49cdfc7eSAndroid Build Coastguard Worker 	test_read_returns_first_message();
553*49cdfc7eSAndroid Build Coastguard Worker 	test_messages_overwritten();
554*49cdfc7eSAndroid Build Coastguard Worker 	test_seek();
555*49cdfc7eSAndroid Build Coastguard Worker }
556*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)557*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
558*49cdfc7eSAndroid Build Coastguard Worker {
559*49cdfc7eSAndroid Build Coastguard Worker 	if (access(PRINTK, F_OK) == 0) {
560*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_SCANF(PRINTK, "%d", &console_loglevel);
561*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(PRINTK, "%d", CONSOLE_LOGLEVEL_QUIET);
562*49cdfc7eSAndroid Build Coastguard Worker 	}
563*49cdfc7eSAndroid Build Coastguard Worker }
564*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)565*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
566*49cdfc7eSAndroid Build Coastguard Worker {
567*49cdfc7eSAndroid Build Coastguard Worker 	if (console_loglevel != -1)
568*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(PRINTK, "%d", console_loglevel);
569*49cdfc7eSAndroid Build Coastguard Worker }
570*49cdfc7eSAndroid Build Coastguard Worker 
571*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
572*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
573*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
574*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
575*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = test_kmsg,
576*49cdfc7eSAndroid Build Coastguard Worker };
577