blob: f37c7f01a9f5a361c2d2bffafadb643fb6217bb7 [file] [log] [blame]
Andrew Lunn5beef3c2009-11-09 21:20:10 +01001/*
2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "log.h"
24
25#define LOG_BUF_MASK (log_buf_len-1)
26#define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
27
28static char log_buf[LOG_BUF_LEN];
29static int log_buf_len = LOG_BUF_LEN;
30static unsigned long log_start;
31static unsigned long log_end;
32uint8_t log_level;
33
34static DEFINE_SPINLOCK(logbuf_lock);
35
36const struct file_operations proc_log_operations = {
37 .open = log_open,
38 .release = log_release,
39 .read = log_read,
40 .write = log_write,
41 .poll = log_poll,
42};
43
44static DECLARE_WAIT_QUEUE_HEAD(log_wait);
45
46static void emit_log_char(char c)
47{
48 LOG_BUF(log_end) = c;
49 log_end++;
50
51 if (log_end - log_start > log_buf_len)
52 log_start = log_end - log_buf_len;
53}
54
55static int fdebug_log(char *fmt, ...)
56{
57 int printed_len;
58 char *p;
59 va_list args;
60 static char debug_log_buf[256];
61 unsigned long flags;
62
63 spin_lock_irqsave(&logbuf_lock, flags);
64 va_start(args, fmt);
65 printed_len = vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt,
66 args);
67 va_end(args);
68
69 for (p = debug_log_buf; *p != 0; p++)
70 emit_log_char(*p);
71
72 spin_unlock_irqrestore(&logbuf_lock, flags);
73
74 wake_up(&log_wait);
75
76 return 0;
77}
78
79int debug_log(int type, char *fmt, ...)
80{
81 va_list args;
82 int retval = 0;
83 char tmp_log_buf[256];
84
85 /* only critical information get into the official kernel log */
86 if (type == LOG_TYPE_CRIT) {
87 va_start(args, fmt);
88 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
89 printk(KERN_ERR "batman-adv: %s", tmp_log_buf);
90 va_end(args);
91 }
92
93 if ((type == LOG_TYPE_CRIT) || (log_level & type)) {
94 va_start(args, fmt);
95 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
96 fdebug_log("[%10u] %s", (jiffies / HZ), tmp_log_buf);
97 va_end(args);
98 }
99
100 return retval;
101}
102
103int log_open(struct inode *inode, struct file *file)
104{
105 inc_module_count();
106 return 0;
107}
108
109int log_release(struct inode *inode, struct file *file)
110{
111 dec_module_count();
112 return 0;
113}
114
115ssize_t log_read(struct file *file, char __user *buf, size_t count,
116 loff_t *ppos)
117{
118 int error, i = 0;
119 char c;
120 unsigned long flags;
121
122 if ((file->f_flags & O_NONBLOCK) && !(log_end - log_start))
123 return -EAGAIN;
124
125 if ((!buf) || (count < 0))
126 return -EINVAL;
127
128 if (count == 0)
129 return 0;
130
131 if (!access_ok(VERIFY_WRITE, buf, count))
132 return -EFAULT;
133
134 error = wait_event_interruptible(log_wait, (log_start - log_end));
135
136 if (error)
137 return error;
138
139 spin_lock_irqsave(&logbuf_lock, flags);
140
141 while ((!error) && (log_start != log_end) && (i < count)) {
142 c = LOG_BUF(log_start);
143
144 log_start++;
145
146 spin_unlock_irqrestore(&logbuf_lock, flags);
147
148 error = __put_user(c, buf);
149
150 spin_lock_irqsave(&logbuf_lock, flags);
151
152 buf++;
153 i++;
154
155 }
156
157 spin_unlock_irqrestore(&logbuf_lock, flags);
158
159 if (!error)
160 return i;
161
162 return error;
163}
164
165ssize_t log_write(struct file *file, const char __user *buf, size_t count,
166 loff_t *ppos)
167{
168 return count;
169}
170
171unsigned int log_poll(struct file *file, poll_table *wait)
172{
173 poll_wait(file, &log_wait, wait);
174
175 if (log_end - log_start)
176 return POLLIN | POLLRDNORM;
177
178 return 0;
179}