1 | /* |
---|
2 | * ksyslog: In-kernel syslog receiver |
---|
3 | * Copyright(C) 2013 Atzm WATANABE All rights reserved |
---|
4 | * Distributed under the GPL |
---|
5 | */ |
---|
6 | |
---|
7 | #include <linux/version.h> |
---|
8 | #include <linux/module.h> |
---|
9 | #include <linux/inet.h> |
---|
10 | #include <linux/ip.h> |
---|
11 | #include <linux/udp.h> |
---|
12 | #include <linux/namei.h> |
---|
13 | #include <linux/fsnotify.h> |
---|
14 | #include <linux/proc_fs.h> |
---|
15 | #include <linux/u64_stats_sync.h> |
---|
16 | #include <linux/percpu.h> |
---|
17 | #include <net/udp.h> |
---|
18 | #include "compat.h" |
---|
19 | #include "ksyslog.h" |
---|
20 | |
---|
21 | static struct ksyslog_queue ksyslog_queue; |
---|
22 | static struct socket *ksyslog_rcv_sk = NULL; |
---|
23 | |
---|
24 | static struct delayed_work ksyslog_work; |
---|
25 | static struct workqueue_struct *ksyslog_wq = NULL; |
---|
26 | |
---|
27 | #ifdef CONFIG_PROC_FS |
---|
28 | static struct proc_dir_entry *ksyslog_procdir = NULL; |
---|
29 | static struct proc_dir_entry *ksyslog_proc_queue = NULL; |
---|
30 | static struct proc_dir_entry *ksyslog_proc_size = NULL; |
---|
31 | static struct proc_dir_entry *ksyslog_proc_stats = NULL; |
---|
32 | #endif |
---|
33 | |
---|
34 | static char *ksyslog_host = "0.0.0.0"; |
---|
35 | static ushort ksyslog_port = 514; |
---|
36 | static char *ksyslog_path = "/var/log/ksyslog.log"; |
---|
37 | static ulong ksyslog_queue_size_max = 4096; |
---|
38 | static ulong ksyslog_flush_interval = 45; /* milliseconds */ |
---|
39 | |
---|
40 | module_param(ksyslog_host, charp, 0444); |
---|
41 | module_param(ksyslog_port, ushort, 0444); |
---|
42 | module_param(ksyslog_path, charp, 0644); |
---|
43 | module_param(ksyslog_queue_size_max, ulong, 0644); |
---|
44 | module_param(ksyslog_flush_interval, ulong, 0644); |
---|
45 | |
---|
46 | static int |
---|
47 | ksyslog_queue_init(struct ksyslog_queue *queue) |
---|
48 | { |
---|
49 | memset(queue, 0, sizeof(*queue)); |
---|
50 | INIT_LIST_HEAD(&queue->head); |
---|
51 | spin_lock_init(&queue->lock); |
---|
52 | atomic64_set(&queue->size, 0); |
---|
53 | queue->stats = alloc_percpu(struct ksyslog_stats); |
---|
54 | if (unlikely(queue->stats == NULL)) |
---|
55 | return -ENOMEM; |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | static void |
---|
60 | ksyslog_queue_uninit(struct ksyslog_queue *queue) |
---|
61 | { |
---|
62 | if (likely(queue->stats)) |
---|
63 | free_percpu(queue->stats); |
---|
64 | queue->stats = NULL; |
---|
65 | } |
---|
66 | |
---|
67 | static int |
---|
68 | ksyslog_close(struct file *file) |
---|
69 | { |
---|
70 | int err; |
---|
71 | mm_segment_t oldfs; |
---|
72 | |
---|
73 | oldfs = get_fs(); |
---|
74 | set_fs(get_ds()); |
---|
75 | |
---|
76 | err = filp_close(file, NULL); |
---|
77 | |
---|
78 | set_fs(oldfs); |
---|
79 | return err; |
---|
80 | } |
---|
81 | |
---|
82 | static struct file * |
---|
83 | ksyslog_open(const char *path) |
---|
84 | { |
---|
85 | struct file *file; |
---|
86 | struct path ppath; |
---|
87 | mm_segment_t oldfs; |
---|
88 | |
---|
89 | oldfs = get_fs(); |
---|
90 | set_fs(get_ds()); |
---|
91 | |
---|
92 | if (unlikely(kern_path(path, LOOKUP_OPEN|LOOKUP_FOLLOW, &ppath))) |
---|
93 | file = filp_open(path, O_CREAT|O_WRONLY|O_APPEND|O_LARGEFILE, 0600); |
---|
94 | else |
---|
95 | file = filp_open(path, O_WRONLY|O_APPEND|O_LARGEFILE, 0); |
---|
96 | |
---|
97 | if (unlikely(IS_ERR(file))) |
---|
98 | goto out; |
---|
99 | |
---|
100 | compat_fsnotify_open(file); |
---|
101 | |
---|
102 | if (unlikely(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) { |
---|
103 | ksyslog_close(file); |
---|
104 | file = ERR_PTR(-EISDIR); |
---|
105 | goto out; |
---|
106 | } |
---|
107 | |
---|
108 | if (unlikely(file->f_pos < 0)) { |
---|
109 | ksyslog_close(file); |
---|
110 | file = ERR_PTR(-EIO); |
---|
111 | goto out; |
---|
112 | } |
---|
113 | |
---|
114 | out: |
---|
115 | set_fs(oldfs); |
---|
116 | return file; |
---|
117 | } |
---|
118 | |
---|
119 | static int |
---|
120 | ksyslog_write(struct file *file, const char *buf, const size_t length) |
---|
121 | { |
---|
122 | int err; |
---|
123 | mm_segment_t oldfs; |
---|
124 | |
---|
125 | oldfs = get_fs(); |
---|
126 | set_fs(get_ds()); |
---|
127 | |
---|
128 | err = vfs_write(file, (__force void __user *)buf, length, &file->f_pos); |
---|
129 | |
---|
130 | set_fs(oldfs); |
---|
131 | return err; |
---|
132 | } |
---|
133 | |
---|
134 | static void |
---|
135 | ksyslog_drop_warning(const struct ksyslog_entry *entry) |
---|
136 | { |
---|
137 | pr_warn("ksyslog: dropped: %llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
138 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
139 | ksyslog_facility_str(entry->facility), |
---|
140 | ksyslog_severity_str(entry->severity), |
---|
141 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
142 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
143 | (int)entry->length, entry->data); |
---|
144 | } |
---|
145 | |
---|
146 | static int |
---|
147 | ksyslog_format(char **buf, const struct ksyslog_entry *entry) |
---|
148 | { |
---|
149 | *buf = kzalloc(54 + entry->length + 2, GFP_ATOMIC); |
---|
150 | if (unlikely(*buf == NULL)) |
---|
151 | return -ENOMEM; |
---|
152 | |
---|
153 | return sprintf(*buf, "%llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
154 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
155 | ksyslog_facility_str(entry->facility), |
---|
156 | ksyslog_severity_str(entry->severity), |
---|
157 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
158 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
159 | (int)entry->length, entry->data); |
---|
160 | } |
---|
161 | |
---|
162 | static struct ksyslog_entry * |
---|
163 | ksyslog_entry_create(const struct sk_buff *skb, |
---|
164 | const struct iphdr *iph, const struct udphdr *udph) |
---|
165 | { |
---|
166 | struct ksyslog_entry *entry; |
---|
167 | unsigned int priority, facility, severity, month, day, hour, minute, second; |
---|
168 | unsigned char *start, month_s[4]; |
---|
169 | struct tm tm; |
---|
170 | int length, i; |
---|
171 | |
---|
172 | if (sscanf(skb->data, "<%3u>%3s %2u %2u:%2u:%2u ", |
---|
173 | &priority, month_s, &day, &hour, &minute, &second) != 6) |
---|
174 | return ERR_PTR(-EINVAL); |
---|
175 | |
---|
176 | start = memchr(skb->data, '>', 5); |
---|
177 | if (start == NULL) |
---|
178 | return ERR_PTR(-EINVAL); |
---|
179 | start++; |
---|
180 | |
---|
181 | facility = priority >> 3; |
---|
182 | severity = priority & 7; |
---|
183 | |
---|
184 | if (facility >= __KSYSLOG_F_MAX) |
---|
185 | return ERR_PTR(-EINVAL); |
---|
186 | if (severity >= __KSYSLOG_S_MAX) |
---|
187 | return ERR_PTR(-EINVAL); |
---|
188 | |
---|
189 | month = ksyslog_month_num(month_s); |
---|
190 | if (!month) |
---|
191 | return ERR_PTR(-EINVAL); |
---|
192 | if (day > 31) |
---|
193 | return ERR_PTR(-EINVAL); |
---|
194 | if (hour > 23) |
---|
195 | return ERR_PTR(-EINVAL); |
---|
196 | if (minute > 59) |
---|
197 | return ERR_PTR(-EINVAL); |
---|
198 | if (second > 59) |
---|
199 | return ERR_PTR(-EINVAL); |
---|
200 | |
---|
201 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
---|
202 | if (unlikely(entry == NULL)) |
---|
203 | return ERR_PTR(-ENOMEM); |
---|
204 | |
---|
205 | length = skb->len - (start - skb->data); |
---|
206 | entry->data = kzalloc(length, GFP_ATOMIC); |
---|
207 | if (unlikely(entry->data == NULL)) { |
---|
208 | kfree(entry); |
---|
209 | return ERR_PTR(-ENOMEM); |
---|
210 | } |
---|
211 | |
---|
212 | if (skb->tstamp.tv64) |
---|
213 | entry->tv = ktime_to_timeval(skb->tstamp); |
---|
214 | else |
---|
215 | do_gettimeofday(&entry->tv); |
---|
216 | |
---|
217 | time_to_tm(entry->tv.tv_sec, 0, &tm); |
---|
218 | entry->time = mktime(tm.tm_year + 1900, month, day, hour, minute, second); |
---|
219 | |
---|
220 | entry->priority = priority; |
---|
221 | entry->facility = facility; |
---|
222 | entry->severity = severity; |
---|
223 | |
---|
224 | entry->daddr.addr32 = iph->daddr; |
---|
225 | entry->saddr.addr32 = iph->saddr; |
---|
226 | |
---|
227 | entry->dport = udph->dest; |
---|
228 | entry->sport = udph->source; |
---|
229 | |
---|
230 | entry->length = length; |
---|
231 | memcpy(entry->data, start, length); |
---|
232 | |
---|
233 | for (i = 0; i < length; i++) |
---|
234 | if (unlikely(entry->data[i] == '\n')) |
---|
235 | entry->data[i] = ' '; |
---|
236 | |
---|
237 | return entry; |
---|
238 | } |
---|
239 | |
---|
240 | static void |
---|
241 | ksyslog_entry_free(struct rcu_head *head) |
---|
242 | { |
---|
243 | struct ksyslog_entry *entry = container_of(head, struct ksyslog_entry, rcu); |
---|
244 | kfree(entry->data); |
---|
245 | kfree(entry); |
---|
246 | } |
---|
247 | |
---|
248 | static int |
---|
249 | ksyslog_entry_add(struct ksyslog_queue *queue, struct ksyslog_entry *entry) |
---|
250 | { |
---|
251 | if (unlikely(atomic64_read(&queue->size) >= ksyslog_queue_size_max)) |
---|
252 | return -ENOBUFS; |
---|
253 | list_add_tail_rcu(&entry->list, &queue->head); |
---|
254 | WARN_ON(atomic64_inc_return(&queue->size) > ksyslog_queue_size_max); |
---|
255 | return 0; |
---|
256 | } |
---|
257 | |
---|
258 | static void |
---|
259 | ksyslog_entry_del(struct ksyslog_queue *queue, struct ksyslog_entry *entry, bool free) |
---|
260 | { |
---|
261 | WARN_ON(atomic64_dec_return(&queue->size) < 0); |
---|
262 | list_del_rcu(&entry->list); |
---|
263 | if (free) |
---|
264 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
265 | } |
---|
266 | |
---|
267 | static void |
---|
268 | ksyslog_entry_destroy(struct ksyslog_queue *queue) |
---|
269 | { |
---|
270 | struct ksyslog_entry *entry, *next; |
---|
271 | |
---|
272 | list_for_each_entry_safe(entry, next, &queue->head, list) |
---|
273 | ksyslog_entry_del(queue, entry, true); |
---|
274 | } |
---|
275 | |
---|
276 | static void |
---|
277 | ksyslog_entry_migrate(struct ksyslog_queue *from, struct ksyslog_queue *to) |
---|
278 | { |
---|
279 | struct ksyslog_entry *entry, *next; |
---|
280 | |
---|
281 | list_for_each_entry_safe(entry, next, &from->head, list) { |
---|
282 | ksyslog_entry_del(from, entry, false); |
---|
283 | if (unlikely(ksyslog_entry_add(to, entry))) { |
---|
284 | ksyslog_stats_add_drop(from, entry->length); |
---|
285 | ksyslog_stats_add_drop(to, entry->length); |
---|
286 | ksyslog_drop_warning(entry); |
---|
287 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | static void |
---|
293 | ksyslog_work_register(unsigned long timer) |
---|
294 | { |
---|
295 | queue_delayed_work(ksyslog_wq, &ksyslog_work, timer * HZ / 1000); |
---|
296 | } |
---|
297 | |
---|
298 | static void |
---|
299 | ksyslog_work_unregister(void) |
---|
300 | { |
---|
301 | cancel_delayed_work_sync(&ksyslog_work); |
---|
302 | } |
---|
303 | |
---|
304 | static void |
---|
305 | ksyslog_work_handler(struct work_struct *work) |
---|
306 | { |
---|
307 | struct file *file = NULL; |
---|
308 | struct ksyslog_entry *entry, *next; |
---|
309 | struct ksyslog_queue write_queue; |
---|
310 | |
---|
311 | if (ksyslog_queue_init(&write_queue)) |
---|
312 | goto out; |
---|
313 | |
---|
314 | spin_lock_bh(&ksyslog_queue.lock); |
---|
315 | ksyslog_entry_migrate(&ksyslog_queue, &write_queue); |
---|
316 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
317 | |
---|
318 | if (atomic64_read(&write_queue.size) <= 0) |
---|
319 | goto out; |
---|
320 | |
---|
321 | file = ksyslog_open(ksyslog_path); |
---|
322 | if (unlikely(IS_ERR(file))) { |
---|
323 | spin_lock_bh(&ksyslog_queue.lock); |
---|
324 | ksyslog_entry_migrate(&write_queue, &ksyslog_queue); |
---|
325 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
326 | goto out; |
---|
327 | } |
---|
328 | |
---|
329 | list_for_each_entry_safe(entry, next, &write_queue.head, list) { |
---|
330 | int length; |
---|
331 | char *buf; |
---|
332 | |
---|
333 | ksyslog_entry_del(&write_queue, entry, false); |
---|
334 | |
---|
335 | length = ksyslog_format(&buf, entry); |
---|
336 | if (unlikely(length < 0)) |
---|
337 | goto restore; |
---|
338 | |
---|
339 | if (unlikely(ksyslog_write(file, buf, length) != length)) { |
---|
340 | kfree(buf); |
---|
341 | goto restore; |
---|
342 | } |
---|
343 | |
---|
344 | ksyslog_stats_add_write(&ksyslog_queue, entry->length); |
---|
345 | kfree(buf); |
---|
346 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
347 | continue; |
---|
348 | |
---|
349 | restore: |
---|
350 | spin_lock_bh(&ksyslog_queue.lock); |
---|
351 | if (unlikely(ksyslog_entry_add(&ksyslog_queue, entry))) { |
---|
352 | ksyslog_stats_add_drop(&ksyslog_queue, entry->length); |
---|
353 | ksyslog_drop_warning(entry); |
---|
354 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
355 | } |
---|
356 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
357 | } |
---|
358 | |
---|
359 | ksyslog_close(file); |
---|
360 | |
---|
361 | out: |
---|
362 | ksyslog_queue_uninit(&write_queue); |
---|
363 | ksyslog_work_register(ksyslog_flush_interval); |
---|
364 | } |
---|
365 | |
---|
366 | static int |
---|
367 | ksyslog_rcv(struct sock *sk, struct sk_buff *skb) |
---|
368 | { |
---|
369 | int err; |
---|
370 | struct iphdr *iph; |
---|
371 | struct udphdr *udph; |
---|
372 | struct ksyslog_entry *entry; |
---|
373 | |
---|
374 | if (unlikely(skb_linearize(skb))) { |
---|
375 | ksyslog_stats_add_drop(&ksyslog_queue, skb->len); |
---|
376 | goto out; |
---|
377 | } |
---|
378 | |
---|
379 | iph = ip_hdr(skb); |
---|
380 | udph = udp_hdr(skb); |
---|
381 | |
---|
382 | if (unlikely(!skb_pull(skb, sizeof(*udph)))) { |
---|
383 | ksyslog_stats_add_drop(&ksyslog_queue, skb->len); |
---|
384 | goto out; |
---|
385 | } |
---|
386 | |
---|
387 | entry = ksyslog_entry_create(skb, iph, udph); |
---|
388 | if (unlikely(IS_ERR(entry))) { |
---|
389 | if (PTR_ERR(entry) == -EINVAL) { |
---|
390 | ksyslog_stats_add_discard(&ksyslog_queue, skb->len); |
---|
391 | goto out; |
---|
392 | } |
---|
393 | |
---|
394 | ksyslog_stats_add_drop(&ksyslog_queue, skb->len); |
---|
395 | goto out; |
---|
396 | } |
---|
397 | |
---|
398 | spin_lock_bh(&ksyslog_queue.lock); |
---|
399 | err = ksyslog_entry_add(&ksyslog_queue, entry); |
---|
400 | spin_unlock_bh(&ksyslog_queue.lock); |
---|
401 | |
---|
402 | if (unlikely(err)) { |
---|
403 | ksyslog_stats_add_drop(&ksyslog_queue, entry->length); |
---|
404 | ksyslog_drop_warning(entry); |
---|
405 | ksyslog_entry_free(&entry->rcu); |
---|
406 | goto out; |
---|
407 | } |
---|
408 | |
---|
409 | out: |
---|
410 | consume_skb(skb); |
---|
411 | return 0; |
---|
412 | } |
---|
413 | |
---|
414 | #ifdef CONFIG_PROC_FS |
---|
415 | static void * |
---|
416 | ksyslog_rculist_seq_start(struct seq_file *seq, loff_t *pos) |
---|
417 | { |
---|
418 | struct list_head *lh, *head = seq->private; |
---|
419 | loff_t ppos = *pos; |
---|
420 | |
---|
421 | rcu_read_lock(); |
---|
422 | |
---|
423 | __list_for_each_rcu(lh, head) |
---|
424 | if (ppos-- == 0) |
---|
425 | return lh; |
---|
426 | |
---|
427 | return NULL; |
---|
428 | } |
---|
429 | |
---|
430 | static void * |
---|
431 | ksyslog_rculist_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
---|
432 | { |
---|
433 | struct list_head *lh = rcu_dereference(((struct list_head *)v)->next); |
---|
434 | ++(*pos); |
---|
435 | return lh == seq->private ? NULL : lh; |
---|
436 | } |
---|
437 | |
---|
438 | static void |
---|
439 | ksyslog_rculist_seq_stop(struct seq_file *seq, void *v) |
---|
440 | { |
---|
441 | rcu_read_unlock(); |
---|
442 | } |
---|
443 | |
---|
444 | static int |
---|
445 | ksyslog_queue_seq_show(struct seq_file *seq, void *v) |
---|
446 | { |
---|
447 | const struct ksyslog_entry *entry = list_entry_rcu(v, struct ksyslog_entry, list); |
---|
448 | |
---|
449 | seq_printf(seq, "%llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
450 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
451 | ksyslog_facility_str(entry->facility), |
---|
452 | ksyslog_severity_str(entry->severity), |
---|
453 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
454 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
455 | (int)entry->length, entry->data); |
---|
456 | |
---|
457 | return 0; |
---|
458 | } |
---|
459 | |
---|
460 | static struct seq_operations ksyslog_queue_seq_ops = { |
---|
461 | .start = ksyslog_rculist_seq_start, |
---|
462 | .next = ksyslog_rculist_seq_next, |
---|
463 | .stop = ksyslog_rculist_seq_stop, |
---|
464 | .show = ksyslog_queue_seq_show, |
---|
465 | }; |
---|
466 | |
---|
467 | static int |
---|
468 | ksyslog_queue_seq_open(struct inode *inode, struct file *file) |
---|
469 | { |
---|
470 | int err = seq_open(file, &ksyslog_queue_seq_ops); |
---|
471 | |
---|
472 | if (!err) |
---|
473 | ((struct seq_file *)file->private_data)->private = PDE_DATA(inode); |
---|
474 | |
---|
475 | return err; |
---|
476 | } |
---|
477 | |
---|
478 | static struct file_operations ksyslog_queue_fops = { |
---|
479 | .owner = THIS_MODULE, |
---|
480 | .open = ksyslog_queue_seq_open, |
---|
481 | .read = seq_read, |
---|
482 | .llseek = seq_lseek, |
---|
483 | .release = seq_release, |
---|
484 | }; |
---|
485 | |
---|
486 | static int |
---|
487 | ksyslog_size_seq_show(struct seq_file *seq, void *v) |
---|
488 | { |
---|
489 | seq_printf(seq, "%lu\n", atomic64_read(&ksyslog_queue.size)); |
---|
490 | return 0; |
---|
491 | } |
---|
492 | |
---|
493 | static int |
---|
494 | ksyslog_size_seq_open(struct inode *inode, struct file *file) |
---|
495 | { |
---|
496 | return single_open(file, ksyslog_size_seq_show, PDE_DATA(inode)); |
---|
497 | } |
---|
498 | |
---|
499 | static int |
---|
500 | ksyslog_stats_seq_show(struct seq_file *seq, void *v) |
---|
501 | { |
---|
502 | int i; |
---|
503 | struct ksyslog_stats stats; |
---|
504 | |
---|
505 | memset(&stats, 0, sizeof(stats)); |
---|
506 | |
---|
507 | for_each_possible_cpu(i) { |
---|
508 | const struct ksyslog_stats *percpu_stats; |
---|
509 | struct ksyslog_stats local_stats; |
---|
510 | unsigned int start; |
---|
511 | |
---|
512 | percpu_stats = per_cpu_ptr(ksyslog_queue.stats, i); |
---|
513 | |
---|
514 | do { |
---|
515 | start = u64_stats_fetch_begin_bh(&percpu_stats->sync); |
---|
516 | local_stats = *percpu_stats; |
---|
517 | } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); |
---|
518 | |
---|
519 | stats.write_bytes += local_stats.write_bytes; |
---|
520 | stats.write_packets += local_stats.write_packets; |
---|
521 | stats.drop_bytes += local_stats.drop_bytes; |
---|
522 | stats.drop_packets += local_stats.drop_packets; |
---|
523 | stats.discard_bytes += local_stats.discard_bytes; |
---|
524 | stats.discard_packets += local_stats.discard_packets; |
---|
525 | } |
---|
526 | |
---|
527 | seq_puts(seq, "{\n"); |
---|
528 | seq_puts(seq, " \"write\": {\n"); |
---|
529 | seq_printf(seq, " \"bytes\": \"%llu\",\n", stats.write_bytes); |
---|
530 | seq_printf(seq, " \"packets\": \"%llu\"\n", stats.write_packets); |
---|
531 | seq_puts(seq, " },\n"); |
---|
532 | seq_puts(seq, " \"drop\": {\n"); |
---|
533 | seq_printf(seq, " \"bytes\": \"%llu\",\n", stats.drop_bytes); |
---|
534 | seq_printf(seq, " \"packets\": \"%llu\"\n", stats.drop_packets); |
---|
535 | seq_puts(seq, " },\n"); |
---|
536 | seq_puts(seq, " \"discard\": {\n"); |
---|
537 | seq_printf(seq, " \"bytes\": \"%llu\",\n", stats.discard_bytes); |
---|
538 | seq_printf(seq, " \"packets\": \"%llu\"\n", stats.discard_packets); |
---|
539 | seq_puts(seq, " }\n"); |
---|
540 | seq_puts(seq, "}\n"); |
---|
541 | |
---|
542 | return 0; |
---|
543 | } |
---|
544 | |
---|
545 | static int |
---|
546 | ksyslog_stats_seq_open(struct inode *inode, struct file *file) |
---|
547 | { |
---|
548 | return single_open(file, ksyslog_stats_seq_show, PDE_DATA(inode)); |
---|
549 | } |
---|
550 | |
---|
551 | static struct file_operations ksyslog_size_fops = { |
---|
552 | .owner = THIS_MODULE, |
---|
553 | .open = ksyslog_size_seq_open, |
---|
554 | .read = seq_read, |
---|
555 | .llseek = seq_lseek, |
---|
556 | .release = single_release, |
---|
557 | }; |
---|
558 | |
---|
559 | static struct file_operations ksyslog_stats_fops = { |
---|
560 | .owner = THIS_MODULE, |
---|
561 | .open = ksyslog_stats_seq_open, |
---|
562 | .read = seq_read, |
---|
563 | .llseek = seq_lseek, |
---|
564 | .release = single_release, |
---|
565 | }; |
---|
566 | |
---|
567 | static void |
---|
568 | ksyslog_proc_destroy(void) |
---|
569 | { |
---|
570 | if (ksyslog_proc_queue) |
---|
571 | remove_proc_entry("queue", ksyslog_procdir); |
---|
572 | ksyslog_proc_queue = NULL; |
---|
573 | |
---|
574 | if (ksyslog_proc_size) |
---|
575 | remove_proc_entry("size", ksyslog_procdir); |
---|
576 | ksyslog_proc_size = NULL; |
---|
577 | |
---|
578 | if (ksyslog_proc_stats) |
---|
579 | remove_proc_entry("stats", ksyslog_procdir); |
---|
580 | ksyslog_proc_stats = NULL; |
---|
581 | |
---|
582 | if (ksyslog_procdir) |
---|
583 | remove_proc_entry("ksyslog", NULL); |
---|
584 | ksyslog_procdir = NULL; |
---|
585 | } |
---|
586 | |
---|
587 | static int |
---|
588 | ksyslog_proc_init(void) |
---|
589 | { |
---|
590 | ksyslog_procdir = proc_mkdir("ksyslog", NULL); |
---|
591 | if (ksyslog_procdir == NULL) { |
---|
592 | pr_err("ksyslog: proc_mkdir failed\n"); |
---|
593 | goto err; |
---|
594 | } |
---|
595 | |
---|
596 | ksyslog_proc_queue = proc_create_data("queue", S_IRUGO, ksyslog_procdir, |
---|
597 | &ksyslog_queue_fops, &ksyslog_queue.head); |
---|
598 | if (ksyslog_proc_queue == NULL) { |
---|
599 | pr_err("ksyslog: proc_create(queue) failed\n"); |
---|
600 | goto err; |
---|
601 | } |
---|
602 | |
---|
603 | ksyslog_proc_size = proc_create("size", S_IRUGO, ksyslog_procdir, |
---|
604 | &ksyslog_size_fops); |
---|
605 | if (ksyslog_proc_size == NULL) { |
---|
606 | pr_err("ksyslog: proc_create(size) failed\n"); |
---|
607 | goto err; |
---|
608 | } |
---|
609 | |
---|
610 | ksyslog_proc_stats = proc_create("stats", S_IRUGO, ksyslog_procdir, |
---|
611 | &ksyslog_stats_fops); |
---|
612 | if (ksyslog_proc_stats == NULL) { |
---|
613 | pr_err("ksyslog: proc_create(stats) failed\n"); |
---|
614 | goto err; |
---|
615 | } |
---|
616 | |
---|
617 | return 0; |
---|
618 | |
---|
619 | err: |
---|
620 | ksyslog_proc_destroy(); |
---|
621 | return -ENOMEM; |
---|
622 | } |
---|
623 | #endif |
---|
624 | |
---|
625 | static void |
---|
626 | ksyslog_finish(void) |
---|
627 | { |
---|
628 | if (ksyslog_rcv_sk) |
---|
629 | sock_release(ksyslog_rcv_sk); |
---|
630 | ksyslog_rcv_sk = NULL; |
---|
631 | |
---|
632 | if (ksyslog_wq) { |
---|
633 | ksyslog_work_unregister(); |
---|
634 | destroy_workqueue(ksyslog_wq); |
---|
635 | } |
---|
636 | ksyslog_wq = NULL; |
---|
637 | |
---|
638 | #ifdef CONFIG_PROC_FS |
---|
639 | ksyslog_proc_destroy(); |
---|
640 | #endif |
---|
641 | |
---|
642 | ksyslog_entry_destroy(&ksyslog_queue); |
---|
643 | rcu_barrier(); |
---|
644 | |
---|
645 | ksyslog_queue_uninit(&ksyslog_queue); |
---|
646 | } |
---|
647 | |
---|
648 | static int __init |
---|
649 | ksyslog_init(void) |
---|
650 | { |
---|
651 | int err; |
---|
652 | struct sockaddr_in sin; |
---|
653 | |
---|
654 | err = ksyslog_queue_init(&ksyslog_queue); |
---|
655 | if (err) |
---|
656 | goto err; |
---|
657 | |
---|
658 | #ifdef CONFIG_PROC_FS |
---|
659 | err = ksyslog_proc_init(); |
---|
660 | if (err) |
---|
661 | goto err; |
---|
662 | #endif |
---|
663 | |
---|
664 | ksyslog_wq = create_singlethread_workqueue("ksyslog"); |
---|
665 | if (ksyslog_wq == NULL) { |
---|
666 | pr_err("ksyslog: create_workqueue failed\n"); |
---|
667 | err = -ENOMEM; |
---|
668 | goto err; |
---|
669 | } |
---|
670 | |
---|
671 | INIT_DELAYED_WORK(&ksyslog_work, ksyslog_work_handler); |
---|
672 | |
---|
673 | err = sock_create(AF_INET, SOCK_DGRAM, 0, &ksyslog_rcv_sk); |
---|
674 | if (err) { |
---|
675 | pr_err("ksyslog: sock_create failed\n"); |
---|
676 | goto err; |
---|
677 | } |
---|
678 | |
---|
679 | sin.sin_family = AF_INET; |
---|
680 | sin.sin_addr.s_addr = in_aton(ksyslog_host); |
---|
681 | sin.sin_port = htons(ksyslog_port); |
---|
682 | |
---|
683 | err = kernel_bind(ksyslog_rcv_sk, (struct sockaddr *)&sin, |
---|
684 | sizeof(struct sockaddr_in)); |
---|
685 | if (err) { |
---|
686 | pr_err("ksyslog: kernel_bind failed\n"); |
---|
687 | goto err; |
---|
688 | } |
---|
689 | |
---|
690 | ksyslog_work_register(ksyslog_flush_interval); |
---|
691 | |
---|
692 | udp_sk(ksyslog_rcv_sk->sk)->encap_type = UDP_ENCAP_KSYSLOG; |
---|
693 | udp_sk(ksyslog_rcv_sk->sk)->encap_rcv = ksyslog_rcv; |
---|
694 | udp_encap_enable(); |
---|
695 | |
---|
696 | return 0; |
---|
697 | |
---|
698 | err: |
---|
699 | ksyslog_finish(); |
---|
700 | return err; |
---|
701 | } |
---|
702 | |
---|
703 | static void __exit |
---|
704 | ksyslog_exit(void) |
---|
705 | { |
---|
706 | ksyslog_finish(); |
---|
707 | } |
---|
708 | |
---|
709 | module_init(ksyslog_init); |
---|
710 | module_exit(ksyslog_exit); |
---|
711 | |
---|
712 | MODULE_AUTHOR("Atzm WATANABE"); |
---|
713 | MODULE_DESCRIPTION("In-kernel syslog receiver"); |
---|
714 | MODULE_LICENSE("GPL"); |
---|