forget difference between big and small commands - obsolete with vm.
[minix.git] / drivers / log / diag.c
blob27e4cfcf3e305718cbc9fd7c99ac4dae9be80481
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).
6 * Changes:
7 * 21 July 2005: Created (Jorrit N. Herder)
8 */
10 #include <stdio.h>
11 #include <minix/type.h>
12 #include <minix/safecopies.h>
13 #include <minix/sys_config.h>
15 #include "log.h"
17 /*==========================================================================*
18 * do_new_kmess *
19 *==========================================================================*/
20 PUBLIC int do_new_kmess(from)
21 endpoint_t from; /* who sent this message? */
23 /* Notification for a new kernel message. */
24 static struct kmessages kmess; /* entire kmess structure */
25 static char print_buf[_KMESS_BUF_SIZE]; /* copy new message here */
26 int bytes;
27 int i, r;
28 int *prev_nextp;
30 static int kernel_prev_next = 0;
31 static int tty_prev_next = 0;
33 if (from == TTY_PROC_NR)
35 cp_grant_id_t gid;
36 message mess;
38 prev_nextp= &tty_prev_next;
39 gid= cpf_grant_direct(TTY_PROC_NR, (vir_bytes)&kmess, sizeof(kmess),
40 CPF_WRITE);
41 if (gid == -1)
43 #if 0
44 report("LOG","cpf_grant_direct failed for TTY", errno);
45 #endif
46 return EDONTREPLY;
49 /* Ask TTY driver for log output */
50 mess.GETKM_GRANT= gid;
51 mess.m_type = GET_KMESS_S;
52 r= sendrec(TTY_PROC_NR, &mess);
53 cpf_revoke(gid);
55 if (r == OK) r= mess.m_type;
56 if (r != OK)
58 report("LOG","couldn't get copy of kmessages from TTY", r);
59 return EDONTREPLY;
62 else
64 /* Try to get a fresh copy of the buffer with kernel messages. */
65 if ((r=sys_getkmessages(&kmess)) != OK) {
66 report("LOG","couldn't get copy of kmessages", r);
67 return EDONTREPLY;
69 prev_nextp= &kernel_prev_next;
72 /* Print only the new part. Determine how many new bytes there are with
73 * help of the current and previous 'next' index. Note that the kernel
74 * buffer is circular. This works fine if less then KMESS_BUF_SIZE bytes
75 * is new data; else we miss % KMESS_BUF_SIZE here.
76 * Check for size being positive, the buffer might as well be emptied!
78 if (kmess.km_size > 0) {
79 bytes = ((kmess.km_next + _KMESS_BUF_SIZE) - (*prev_nextp)) %
80 _KMESS_BUF_SIZE;
81 r= *prev_nextp; /* start at previous old */
82 i=0;
83 while (bytes > 0) {
84 print_buf[i] = kmess.km_buf[(r%_KMESS_BUF_SIZE)];
85 bytes --;
86 r ++;
87 i ++;
89 /* Now terminate the new message and save it in the log. */
90 print_buf[i] = 0;
91 log_append(print_buf, i);
94 /* Almost done, store 'next' so that we can determine what part of the
95 * kernel messages buffer to print next time a notification arrives.
97 *prev_nextp = kmess.km_next;
98 return EDONTREPLY;
101 /*===========================================================================*
102 * do_diagnostics *
103 *===========================================================================*/
104 PUBLIC int do_diagnostics(message *m, int safe)
106 /* The LOG server handles all diagnostic messages from servers and device
107 * drivers. It forwards the message to the TTY driver to display it to the
108 * user. It also saves a copy in a local buffer so that messages can be
109 * reviewed at a later time.
111 vir_bytes src;
112 int count;
113 char c;
114 int i = 0, offset = 0;
115 static char diagbuf[10240];
117 /* Also make a copy for the private buffer at the LOG server, so
118 * that the messages can be reviewed at a later time.
120 src = (vir_bytes) m->DIAG_PRINT_BUF_G;
121 count = m->DIAG_BUF_COUNT;
122 while (count > 0 && i < sizeof(diagbuf)-1) {
123 int r;
124 if(safe) {
125 r = sys_safecopyfrom(m->m_source, src, offset, (vir_bytes) &c, 1, D);
126 } else {
127 r = sys_datacopy(m->m_source, src+offset, SELF, (vir_bytes) &c, 1);
129 if(r != OK) break;
130 offset ++;
131 count --;
132 diagbuf[i++] = c;
134 log_append(diagbuf, i);
136 if(m->m_type == ASYN_DIAGNOSTICS_OLD) return EDONTREPLY;
138 return OK;