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