xref: /minix3/minix/drivers/system/log/diag.c (revision 594df55e53732746ac76b15ad87a3eac02ec1619)
1433d6423SLionel Sambuc /* This file handle diagnostic output that is sent to the LOG driver. Output
2433d6423SLionel Sambuc  * can be either from the kernel, or from other system processes. Output from
3433d6423SLionel Sambuc  * system processes is also routed through the kernel. The kernel notifies
4433d6423SLionel Sambuc  * this driver with a SIGKMESS signal if any messages are available.
5433d6423SLionel Sambuc  *
6433d6423SLionel Sambuc  * Changes:
7433d6423SLionel Sambuc  *	21 July 2005:	Created  (Jorrit N. Herder)
8433d6423SLionel Sambuc  */
9433d6423SLionel Sambuc 
10433d6423SLionel Sambuc #include "log.h"
11433d6423SLionel Sambuc 
12433d6423SLionel Sambuc #include <assert.h>
13433d6423SLionel Sambuc 
14433d6423SLionel Sambuc /*==========================================================================*
15433d6423SLionel Sambuc  *				do_new_kmess				    *
16433d6423SLionel Sambuc  *==========================================================================*/
do_new_kmess(void)17433d6423SLionel Sambuc void do_new_kmess(void)
18433d6423SLionel Sambuc {
19433d6423SLionel Sambuc /* Notification for a new kernel message. */
20433d6423SLionel Sambuc   static struct kmessages *kmess;		/* entire kmess structure */
21433d6423SLionel Sambuc   static char print_buf[_KMESS_BUF_SIZE];	/* copy new message here */
22433d6423SLionel Sambuc   int i, r, next, bytes;
23433d6423SLionel Sambuc   static int prev_next = 0;
24433d6423SLionel Sambuc 
25*594df55eSDavid van Moolenbroek   kmess = get_minix_kerninfo()->kmessages;
26433d6423SLionel Sambuc 
27433d6423SLionel Sambuc   /* Print only the new part. Determine how many new bytes there are with
28433d6423SLionel Sambuc    * help of the current and previous 'next' index. Note that the kernel
29433d6423SLionel Sambuc    * buffer is circular. This works fine if less than KMESS_BUF_SIZE bytes
30433d6423SLionel Sambuc    * are new data; else we miss % KMESS_BUF_SIZE here. Obtain 'next' only
31433d6423SLionel Sambuc    * once, since we are operating on shared memory here.
32433d6423SLionel Sambuc    * Check for size being positive, the buffer might as well be emptied!
33433d6423SLionel Sambuc    */
34433d6423SLionel Sambuc   next = kmess->km_next;
35433d6423SLionel Sambuc   bytes = ((next + _KMESS_BUF_SIZE) - prev_next) % _KMESS_BUF_SIZE;
36433d6423SLionel Sambuc   if (bytes > 0) {
37433d6423SLionel Sambuc       r= prev_next;				/* start at previous old */
38433d6423SLionel Sambuc       i=0;
39433d6423SLionel Sambuc       while (bytes > 0) {
40433d6423SLionel Sambuc           print_buf[i] = kmess->km_buf[(r%_KMESS_BUF_SIZE)];
41433d6423SLionel Sambuc           bytes --;
42433d6423SLionel Sambuc           r ++;
43433d6423SLionel Sambuc           i ++;
44433d6423SLionel Sambuc       }
45433d6423SLionel Sambuc       /* Now terminate the new message and save it in the log. */
46433d6423SLionel Sambuc       print_buf[i] = 0;
47433d6423SLionel Sambuc       log_append(print_buf, i);
48433d6423SLionel Sambuc   }
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc   /* Almost done, store 'next' so that we can determine what part of the
51433d6423SLionel Sambuc    * kernel messages buffer to print next time a notification arrives.
52433d6423SLionel Sambuc    */
53433d6423SLionel Sambuc   prev_next = next;
54433d6423SLionel Sambuc }
55