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