source: branches/minix3-book/drivers/log/diag.c

Last change on this file was 4, checked in by Mattia Monga, 14 years ago

Importazione sorgenti libro

File size: 3.5 KB
Line 
1/* This file handle diagnostic output that is directly sent to the LOG driver.
2 * This output can either be a kernel message (announced through a SYS_EVENT
3 * with a SIGKMESS in the signal set) or output from another system process
4 * (announced through a DIAGNOSTICS message).
5 *
6 * Changes:
7 * 21 July 2005: Created (Jorrit N. Herder)
8 */
9
10#include <stdio.h>
11#include <fcntl.h>
12
13#include "log.h"
14#include "../../kernel/const.h"
15#include "../../kernel/config.h"
16#include "../../kernel/type.h"
17
18/*==========================================================================*
19 * do_new_kmess *
20 *==========================================================================*/
21PUBLIC int do_new_kmess(m)
22message *m; /* notification message */
23{
24/* Notification for a new kernel message. */
25 struct kmessages kmess; /* entire kmess structure */
26 char print_buf[KMESS_BUF_SIZE]; /* copy new message here */
27 static int prev_next = 0;
28 int bytes;
29 int i, r;
30
31 /* Try to get a fresh copy of the buffer with kernel messages. */
32 if ((r=sys_getkmessages(&kmess)) != OK) {
33 report("LOG","couldn't get copy of kmessages", r);
34 return EDONTREPLY;
35 }
36
37 /* Print only the new part. Determine how many new bytes there are with
38 * help of the current and previous 'next' index. Note that the kernel
39 * buffer is circular. This works fine if less then KMESS_BUF_SIZE bytes
40 * is new data; else we miss % KMESS_BUF_SIZE here.
41 * Check for size being positive, the buffer might as well be emptied!
42 */
43 if (kmess.km_size > 0) {
44 bytes = ((kmess.km_next + KMESS_BUF_SIZE) - prev_next) % KMESS_BUF_SIZE;
45 r=prev_next; /* start at previous old */
46 i=0;
47 while (bytes > 0) {
48 print_buf[i] = kmess.km_buf[(r%KMESS_BUF_SIZE)];
49 bytes --;
50 r ++;
51 i ++;
52 }
53 /* Now terminate the new message and print it. */
54 print_buf[i] = 0;
55 printf("%s", print_buf);
56 log_append(print_buf, i);
57 }
58
59 /* Almost done, store 'next' so that we can determine what part of the
60 * kernel messages buffer to print next time a notification arrives.
61 */
62 prev_next = kmess.km_next;
63 return EDONTREPLY;
64}
65
66/*===========================================================================*
67 * do_diagnostics *
68 *===========================================================================*/
69PUBLIC int do_diagnostics(message *m)
70{
71/* The LOG server handles all diagnostic messages from servers and device
72 * drivers. It forwards the message to the TTY driver to display it to the
73 * user. It also saves a copy in a local buffer so that messages can be
74 * reviewed at a later time.
75 */
76 int result;
77 int proc_nr;
78 vir_bytes src;
79 int count;
80 char c;
81 int i = 0;
82 static char diagbuf[10240];
83
84 /* Forward the message to the TTY driver. Inform the TTY driver about the
85 * original sender, so that it knows where the buffer to be printed is.
86 * The message type, DIAGNOSTICS, remains the same.
87 */
88 if ((proc_nr = m->DIAG_PROC_NR) == SELF)
89 m->DIAG_PROC_NR = proc_nr = m->m_source;
90 result = _sendrec(TTY_PROC_NR, m);
91
92 /* Now also make a copy for the private buffer at the LOG server, so
93 * that the messages can be reviewed at a later time.
94 */
95 src = (vir_bytes) m->DIAG_PRINT_BUF;
96 count = m->DIAG_BUF_COUNT;
97 while (count > 0 && i < sizeof(diagbuf)-1) {
98 if (sys_datacopy(proc_nr, src, SELF, (vir_bytes) &c, 1) != OK)
99 break; /* stop copying on error */
100 src ++;
101 count --;
102 diagbuf[i++] = c;
103 }
104 log_append(diagbuf, i);
105
106 return result;
107}
Note: See TracBrowser for help on using the repository browser.