source: mainline/uspace/srv/klog/klog.c@ 368ee04

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 368ee04 was 193d280c, checked in by Martin Decky <martin@…>, 10 years ago

cstyle improvements
replace traditional K&R-style function declarations and definitions

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * Copyright (c) 2013 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup klog KLog
31 * @brief HelenOS KLog
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <stdio.h>
39#include <async.h>
40#include <as.h>
41#include <ddi.h>
42#include <errno.h>
43#include <str_error.h>
44#include <io/klog.h>
45#include <sysinfo.h>
46#include <malloc.h>
47#include <fibril_synch.h>
48#include <adt/list.h>
49#include <adt/prodcons.h>
50#include <io/log.h>
51#include <io/logctl.h>
52
53#define NAME "klog"
54
55typedef size_t __attribute__ ((aligned(1))) unaligned_size_t;
56
57typedef struct {
58 size_t entry_len;
59 uint32_t serial;
60 uint32_t facility;
61 uint32_t level;
62 char message[0];
63
64} __attribute__((__packed__)) log_entry_t;
65
66/* Producer/consumer buffers */
67typedef struct {
68 link_t link;
69 size_t size;
70 log_entry_t *data;
71} item_t;
72
73static prodcons_t pc;
74
75/* Pointer to buffer where kernel stores new entries */
76#define BUFFER_SIZE PAGE_SIZE
77static void *buffer;
78
79/* Notification mutex */
80static FIBRIL_MUTEX_INITIALIZE(mtx);
81
82static log_t kernel_ctx;
83static const char *facility_name[] = {
84 "other",
85 "uspace",
86 "arch"
87};
88
89#define facility_len (sizeof(facility_name) / sizeof(const char *))
90static log_t facility_ctx[facility_len];
91
92/** Klog producer
93 *
94 * Copies the log entries to a producer/consumer queue.
95 *
96 * @param length Number of characters to copy.
97 * @param data Pointer to the kernel klog buffer.
98 *
99 */
100static void producer(void)
101{
102 int read = klog_read(buffer, BUFFER_SIZE);
103
104 if (read < 0) {
105 log_msg(LOG_DEFAULT, LVL_ERROR, "klog_read failed, rc = %d",
106 read);
107 return;
108 }
109
110 size_t len = read;
111 size_t offset = 0;
112 while (offset < len) {
113 size_t entry_len = *((unaligned_size_t *) (buffer + offset));
114
115 if (offset + entry_len > len || entry_len < sizeof(log_entry_t))
116 break;
117
118 log_entry_t *buf = malloc(entry_len + 1);
119 if (buf == NULL)
120 break;
121
122 item_t *item = malloc(sizeof(item_t));
123 if (item == NULL) {
124 free(buf);
125 break;
126 }
127
128 memcpy(buf, buffer + offset, entry_len);
129 *((uint8_t *) buf + entry_len) = 0;
130 link_initialize(&item->link);
131 item->size = entry_len;
132 item->data = buf;
133 prodcons_produce(&pc, &item->link);
134
135 offset += entry_len;
136 }
137}
138
139/** Klog consumer
140 *
141 * Waits in an infinite loop for the log data created by
142 * the producer and logs them to the logger.
143 *
144 * @param data Unused.
145 *
146 * @return Always EOK (unreachable).
147 *
148 */
149static int consumer(void *data)
150{
151
152 while (true) {
153 link_t *link = prodcons_consume(&pc);
154 item_t *item = list_get_instance(link, item_t, link);
155
156 if (item->size < sizeof(log_entry_t)) {
157 free(item->data);
158 free(item);
159 continue;
160 }
161
162 if (item->data->facility == LF_USPACE) {
163 /* Avoid reposting messages */
164 free(item->data);
165 free(item);
166 continue;
167 }
168
169 log_t ctx = kernel_ctx;
170 if (item->data->facility < facility_len) {
171 ctx = facility_ctx[item->data->facility];
172 }
173
174 log_level_t lvl = item->data->level;
175 if (lvl > LVL_LIMIT)
176 lvl = LVL_NOTE;
177
178 log_msg(ctx, lvl, "%s", item->data->message);
179
180 free(item->data);
181 free(item);
182 }
183
184 return EOK;
185}
186
187/** Kernel notification handler
188 *
189 * Receives kernel klog notifications.
190 *
191 * @param callid IPC call ID
192 * @param call IPC call structure
193 * @param arg Local argument
194 *
195 */
196static void klog_notification_received(ipc_callid_t callid, ipc_call_t *call,
197 void *arg)
198{
199 /*
200 * Make sure we process only a single notification
201 * at any time to limit the chance of the consumer
202 * starving.
203 */
204
205 fibril_mutex_lock(&mtx);
206
207 producer();
208
209 async_event_unmask(EVENT_KLOG);
210 fibril_mutex_unlock(&mtx);
211}
212
213int main(int argc, char *argv[])
214{
215 int rc = log_init(NAME);
216 if (rc != EOK) {
217 fprintf(stderr, "%s: Unable to initialize log\n", NAME);
218 return rc;
219 }
220
221 kernel_ctx = log_create("kernel", LOG_NO_PARENT);
222 for (unsigned int i = 0; i < facility_len; i++) {
223 facility_ctx[i] = log_create(facility_name[i], kernel_ctx);
224 }
225
226 buffer = malloc(BUFFER_SIZE);
227 if (buffer == NULL) {
228 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to allocate buffer");
229 return 1;
230 }
231
232 prodcons_initialize(&pc);
233 rc = async_event_subscribe(EVENT_KLOG, klog_notification_received, NULL);
234 if (rc != EOK) {
235 log_msg(LOG_DEFAULT, LVL_ERROR,
236 "Unable to register klog notifications");
237 return rc;
238 }
239
240 fid_t fid = fibril_create(consumer, NULL);
241 if (!fid) {
242 log_msg(LOG_DEFAULT, LVL_ERROR,
243 "Unable to create consumer fibril");
244 return ENOMEM;
245 }
246
247 fibril_add_ready(fid);
248 async_event_unmask(EVENT_KLOG);
249
250 fibril_mutex_lock(&mtx);
251 producer();
252 fibril_mutex_unlock(&mtx);
253
254 task_retval(0);
255 async_manager();
256
257 return 0;
258}
259
260/** @}
261 */
Note: See TracBrowser for help on using the repository browser.