[232] | 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/ip.h> |
---|
| 9 | #include <linux/udp.h> |
---|
| 10 | #include <linux/namei.h> |
---|
| 11 | #include <net/udp.h> |
---|
| 12 | #include "ksyslog.h" |
---|
| 13 | |
---|
| 14 | static DEFINE_SPINLOCK(ksyslog_queue_lock); |
---|
| 15 | static DEFINE_SPINLOCK(ksyslog_vfs_lock); |
---|
| 16 | |
---|
| 17 | static struct socket *ksyslog_rcv_sk = NULL; |
---|
| 18 | |
---|
| 19 | static struct delayed_work ksyslog_work; |
---|
| 20 | static struct workqueue_struct *ksyslog_wq = NULL; |
---|
| 21 | |
---|
| 22 | static struct ksyslog_queue ksyslog_queue; |
---|
| 23 | |
---|
| 24 | #ifdef CONFIG_PROC_FS |
---|
| 25 | static struct proc_dir_entry *ksyslog_procdir = NULL; |
---|
| 26 | static struct proc_dir_entry *ksyslog_proc_queue = NULL; |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | static char *ksyslog_host = "0.0.0.0"; |
---|
| 30 | static ushort ksyslog_port = 514; |
---|
| 31 | static char *ksyslog_path = "/var/log/ksyslog.log"; |
---|
| 32 | static uint ksyslog_queue_max = 2048; |
---|
| 33 | static ulong ksyslog_flush_interval = 45; /* milliseconds */ |
---|
| 34 | |
---|
| 35 | module_param(ksyslog_host, charp, 0444); |
---|
| 36 | module_param(ksyslog_port, ushort, 0444); |
---|
| 37 | module_param(ksyslog_path, charp, 0644); |
---|
| 38 | module_param(ksyslog_queue_max, uint, 0644); |
---|
| 39 | module_param(ksyslog_flush_interval, ulong, 0644); |
---|
| 40 | |
---|
| 41 | static int |
---|
| 42 | ksyslog_close(struct file *file) |
---|
| 43 | { |
---|
| 44 | return filp_close(file, NULL); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | static struct file * |
---|
| 48 | ksyslog_open(const char *path) |
---|
| 49 | { |
---|
| 50 | struct file *file; |
---|
| 51 | struct nameidata nd; |
---|
| 52 | mm_segment_t oldfs; |
---|
| 53 | |
---|
| 54 | oldfs = get_fs(); |
---|
| 55 | set_fs(get_ds()); |
---|
| 56 | |
---|
[234] | 57 | if (unlikely(path_lookup(path, LOOKUP_OPEN|LOOKUP_FOLLOW, &nd))) |
---|
[232] | 58 | file = filp_open(path, O_CREAT|O_WRONLY|O_APPEND|O_LARGEFILE, 0600); |
---|
| 59 | else |
---|
| 60 | file = filp_open(path, O_WRONLY|O_APPEND|O_LARGEFILE, 0); |
---|
| 61 | |
---|
[234] | 62 | if (unlikely(IS_ERR(file))) |
---|
[232] | 63 | goto out; |
---|
| 64 | |
---|
[234] | 65 | if (unlikely(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) { |
---|
[232] | 66 | ksyslog_close(file); |
---|
| 67 | file = ERR_PTR(-EISDIR); |
---|
| 68 | goto out; |
---|
| 69 | } |
---|
| 70 | |
---|
[234] | 71 | if (unlikely(file->f_pos < 0)) { |
---|
[232] | 72 | ksyslog_close(file); |
---|
| 73 | file = ERR_PTR(-EIO); |
---|
| 74 | goto out; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | out: |
---|
| 78 | set_fs(oldfs); |
---|
| 79 | return file; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | static int |
---|
| 83 | ksyslog_write(struct file *file, const char *buf, const size_t length) |
---|
| 84 | { |
---|
| 85 | int err; |
---|
| 86 | mm_segment_t oldfs; |
---|
| 87 | |
---|
| 88 | oldfs = get_fs(); |
---|
| 89 | set_fs(get_ds()); |
---|
| 90 | |
---|
| 91 | err = vfs_write(file, (__force void __user *)buf, length, &file->f_pos); |
---|
| 92 | |
---|
| 93 | set_fs(oldfs); |
---|
| 94 | return err; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | static void |
---|
[234] | 98 | ksyslog_drop_warning(const struct ksyslog_entry *entry) |
---|
[232] | 99 | { |
---|
| 100 | pr_warn("dropped: %llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
| 101 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
| 102 | ksyslog_facility_str(entry->facility), |
---|
| 103 | ksyslog_severity_str(entry->severity), |
---|
| 104 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
| 105 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
| 106 | (int)entry->length, entry->data); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | static int |
---|
[234] | 110 | ksyslog_format(char **buf, const struct ksyslog_entry *entry) |
---|
[232] | 111 | { |
---|
| 112 | *buf = kzalloc(54 + entry->length + 2, GFP_ATOMIC); |
---|
[234] | 113 | if (unlikely(*buf == NULL)) |
---|
[232] | 114 | return -ENOMEM; |
---|
| 115 | |
---|
| 116 | return sprintf(*buf, "%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 struct ksyslog_entry * |
---|
[234] | 126 | ksyslog_entry_create(const struct sk_buff *skb, |
---|
| 127 | const struct iphdr *iph, const struct udphdr *udph) |
---|
[232] | 128 | { |
---|
| 129 | struct ksyslog_entry *entry; |
---|
| 130 | unsigned int facility, severity; |
---|
| 131 | unsigned char priority, *start; |
---|
| 132 | int length, i; |
---|
| 133 | |
---|
| 134 | if (sscanf(skb->data, "<%3hhu>", &priority) != 1) |
---|
| 135 | return ERR_PTR(-EINVAL); |
---|
| 136 | |
---|
| 137 | start = memchr(skb->data, '>', 5); |
---|
| 138 | if (start == NULL) |
---|
| 139 | return ERR_PTR(-EINVAL); |
---|
| 140 | start++; |
---|
| 141 | |
---|
| 142 | facility = priority >> 3; |
---|
| 143 | severity = priority & 7; |
---|
| 144 | |
---|
| 145 | if (facility >= __KSYSLOG_F_MAX) |
---|
| 146 | return ERR_PTR(-EINVAL); |
---|
| 147 | if (severity >= __KSYSLOG_S_MAX) |
---|
| 148 | return ERR_PTR(-EINVAL); |
---|
| 149 | |
---|
| 150 | entry = kzalloc(sizeof(*entry), GFP_ATOMIC); |
---|
[234] | 151 | if (unlikely(entry == NULL)) |
---|
[232] | 152 | return ERR_PTR(-ENOMEM); |
---|
| 153 | |
---|
| 154 | length = skb->len - (start - skb->data); |
---|
| 155 | entry->data = kzalloc(length, GFP_ATOMIC); |
---|
[234] | 156 | if (unlikely(entry->data == NULL)) { |
---|
[232] | 157 | kfree(entry); |
---|
| 158 | return ERR_PTR(-ENOMEM); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | if (skb->tstamp.tv64) |
---|
| 162 | entry->tv = ktime_to_timeval(skb->tstamp); |
---|
| 163 | else |
---|
| 164 | do_gettimeofday(&entry->tv); |
---|
| 165 | |
---|
[234] | 166 | entry->facility = facility; |
---|
| 167 | entry->severity = severity; |
---|
| 168 | |
---|
[232] | 169 | entry->daddr.addr32 = iph->daddr; |
---|
| 170 | entry->saddr.addr32 = iph->saddr; |
---|
| 171 | |
---|
| 172 | entry->dport = udph->dest; |
---|
| 173 | entry->sport = udph->source; |
---|
| 174 | |
---|
| 175 | entry->length = length; |
---|
| 176 | memcpy(entry->data, start, length); |
---|
| 177 | |
---|
| 178 | for (i = 0; i < length; i++) |
---|
[234] | 179 | if (unlikely(entry->data[i] == '\n')) |
---|
[232] | 180 | entry->data[i] = ' '; |
---|
| 181 | |
---|
| 182 | return entry; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | static void |
---|
| 186 | ksyslog_entry_free(struct rcu_head *head) |
---|
| 187 | { |
---|
| 188 | struct ksyslog_entry *entry = container_of(head, struct ksyslog_entry, rcu); |
---|
| 189 | kfree(entry->data); |
---|
| 190 | kfree(entry); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | static int |
---|
| 194 | ksyslog_entry_add(struct ksyslog_queue *queue, struct ksyslog_entry *entry) |
---|
| 195 | { |
---|
[234] | 196 | if (unlikely(queue->length >= ksyslog_queue_max)) |
---|
[232] | 197 | return -ENOBUFS; |
---|
| 198 | list_add_tail_rcu(&entry->list, &queue->head); |
---|
| 199 | WARN_ON(++queue->length > ksyslog_queue_max); |
---|
| 200 | return 0; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | static void |
---|
| 204 | ksyslog_entry_del(struct ksyslog_queue *queue, struct ksyslog_entry *entry, bool free) |
---|
| 205 | { |
---|
| 206 | WARN_ON(--queue->length < 0); |
---|
| 207 | list_del_rcu(&entry->list); |
---|
| 208 | if (free) |
---|
| 209 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | static void |
---|
| 213 | ksyslog_entry_destroy(struct ksyslog_queue *queue) |
---|
| 214 | { |
---|
| 215 | struct ksyslog_entry *entry, *next; |
---|
| 216 | |
---|
| 217 | list_for_each_entry_safe(entry, next, &queue->head, list) |
---|
| 218 | ksyslog_entry_del(queue, entry, true); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | static void |
---|
| 222 | ksyslog_entry_migrate(struct ksyslog_queue *from, struct ksyslog_queue *to) |
---|
| 223 | { |
---|
| 224 | struct ksyslog_entry *entry, *next; |
---|
| 225 | |
---|
| 226 | list_for_each_entry_safe(entry, next, &from->head, list) { |
---|
| 227 | ksyslog_entry_del(from, entry, false); |
---|
[234] | 228 | if (unlikely(ksyslog_entry_add(to, entry))) { |
---|
[232] | 229 | ksyslog_drop_warning(entry); |
---|
| 230 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | static void |
---|
| 236 | ksyslog_work_register(unsigned long timer) |
---|
| 237 | { |
---|
| 238 | queue_delayed_work(ksyslog_wq, &ksyslog_work, timer * HZ / 1000); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | static void |
---|
| 242 | ksyslog_work_unregister(void) |
---|
| 243 | { |
---|
| 244 | cancel_delayed_work_sync(&ksyslog_work); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | static void |
---|
| 248 | ksyslog_work_handler(struct work_struct *work) |
---|
| 249 | { |
---|
| 250 | struct file *file = NULL; |
---|
| 251 | struct ksyslog_entry *entry, *next; |
---|
| 252 | struct ksyslog_queue write_queue; |
---|
| 253 | |
---|
| 254 | memset(&write_queue, 0, sizeof(write_queue)); |
---|
| 255 | INIT_LIST_HEAD(&write_queue.head); |
---|
| 256 | |
---|
| 257 | spin_lock_bh(&ksyslog_queue_lock); |
---|
| 258 | ksyslog_entry_migrate(&ksyslog_queue, &write_queue); |
---|
| 259 | spin_unlock_bh(&ksyslog_queue_lock); |
---|
| 260 | |
---|
| 261 | if (write_queue.length <= 0) |
---|
| 262 | goto out; |
---|
| 263 | |
---|
| 264 | spin_lock(&ksyslog_vfs_lock); |
---|
| 265 | |
---|
| 266 | file = ksyslog_open(ksyslog_path); |
---|
[234] | 267 | if (unlikely(IS_ERR(file))) { |
---|
[232] | 268 | spin_unlock(&ksyslog_vfs_lock); |
---|
| 269 | |
---|
| 270 | spin_lock_bh(&ksyslog_queue_lock); |
---|
| 271 | ksyslog_entry_migrate(&write_queue, &ksyslog_queue); |
---|
| 272 | spin_unlock_bh(&ksyslog_queue_lock); |
---|
| 273 | |
---|
| 274 | goto out; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | list_for_each_entry_safe(entry, next, &write_queue.head, list) { |
---|
| 278 | int length; |
---|
| 279 | char *buf; |
---|
| 280 | |
---|
| 281 | ksyslog_entry_del(&write_queue, entry, false); |
---|
| 282 | |
---|
| 283 | length = ksyslog_format(&buf, entry); |
---|
[234] | 284 | if (unlikely(length < 0)) |
---|
[232] | 285 | goto restore; |
---|
| 286 | |
---|
[234] | 287 | if (unlikely(ksyslog_write(file, buf, length) < 0)) { |
---|
[232] | 288 | kfree(buf); |
---|
| 289 | goto restore; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | kfree(buf); |
---|
| 293 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 294 | continue; |
---|
| 295 | |
---|
| 296 | restore: |
---|
| 297 | spin_lock_bh(&ksyslog_queue_lock); |
---|
[234] | 298 | if (unlikely(ksyslog_entry_add(&ksyslog_queue, entry))) { |
---|
[232] | 299 | ksyslog_drop_warning(entry); |
---|
| 300 | call_rcu(&entry->rcu, ksyslog_entry_free); |
---|
| 301 | } |
---|
| 302 | spin_unlock_bh(&ksyslog_queue_lock); |
---|
| 303 | } |
---|
[234] | 304 | |
---|
[232] | 305 | ksyslog_close(file); |
---|
| 306 | spin_unlock(&ksyslog_vfs_lock); |
---|
| 307 | |
---|
| 308 | out: |
---|
| 309 | ksyslog_work_register(ksyslog_flush_interval); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | static int |
---|
| 313 | ksyslog_rcv(struct sock *sk, struct sk_buff *skb) |
---|
| 314 | { |
---|
| 315 | int err; |
---|
| 316 | struct iphdr *iph; |
---|
| 317 | struct udphdr *udph; |
---|
| 318 | struct ksyslog_entry *entry; |
---|
| 319 | |
---|
| 320 | err = skb_linearize(skb); |
---|
[234] | 321 | if (unlikely(err)) |
---|
[232] | 322 | goto out; |
---|
| 323 | |
---|
| 324 | iph = ip_hdr(skb); |
---|
| 325 | udph = udp_hdr(skb); |
---|
| 326 | |
---|
[234] | 327 | if (unlikely(!skb_pull(skb, sizeof(*udph)))) { |
---|
[232] | 328 | err = -EINVAL; |
---|
| 329 | goto out; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | entry = ksyslog_entry_create(skb, iph, udph); |
---|
[234] | 333 | if (unlikely(IS_ERR(entry))) { |
---|
[232] | 334 | err = PTR_ERR(entry); |
---|
| 335 | goto out; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | spin_lock_bh(&ksyslog_queue_lock); |
---|
| 339 | err = ksyslog_entry_add(&ksyslog_queue, entry); |
---|
| 340 | spin_unlock_bh(&ksyslog_queue_lock); |
---|
| 341 | |
---|
[234] | 342 | if (unlikely(err)) |
---|
[232] | 343 | ksyslog_entry_free(&entry->rcu); |
---|
| 344 | |
---|
| 345 | out: |
---|
[234] | 346 | if (unlikely(err)) |
---|
[232] | 347 | UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, IS_UDPLITE(sk)); |
---|
| 348 | |
---|
| 349 | consume_skb(skb); |
---|
| 350 | return 0; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | #ifdef CONFIG_PROC_FS |
---|
| 354 | static void * |
---|
| 355 | ksyslog_queue_seq_start(struct seq_file *seq, loff_t *pos) |
---|
| 356 | { |
---|
| 357 | struct list_head *lh, *head = seq->private; |
---|
| 358 | loff_t ppos = *pos; |
---|
| 359 | |
---|
| 360 | rcu_read_lock(); |
---|
| 361 | |
---|
| 362 | __list_for_each_rcu(lh, head) |
---|
| 363 | if (ppos-- == 0) |
---|
| 364 | return lh; |
---|
| 365 | |
---|
| 366 | return NULL; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | static void * |
---|
| 370 | ksyslog_queue_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
---|
| 371 | { |
---|
| 372 | struct list_head *lh = rcu_dereference(((struct list_head *)v)->next); |
---|
| 373 | ++(*pos); |
---|
| 374 | return lh == seq->private ? NULL : lh; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | static void |
---|
| 378 | ksyslog_queue_seq_stop(struct seq_file *seq, void *v) |
---|
| 379 | { |
---|
| 380 | rcu_read_unlock(); |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | static int |
---|
| 384 | ksyslog_queue_seq_show(struct seq_file *seq, void *v) |
---|
| 385 | { |
---|
| 386 | const struct ksyslog_entry *entry = list_entry_rcu(v, struct ksyslog_entry, list); |
---|
| 387 | |
---|
[233] | 388 | seq_printf(seq, "%llu %s.%s %u.%u.%u.%u %.*s\n", |
---|
[232] | 389 | timeval_to_ns(&entry->tv) / 1000 / 1000 / 1000, |
---|
[233] | 390 | ksyslog_facility_str(entry->facility), |
---|
| 391 | ksyslog_severity_str(entry->severity), |
---|
[232] | 392 | entry->saddr.addr8[0], entry->saddr.addr8[1], |
---|
| 393 | entry->saddr.addr8[2], entry->saddr.addr8[3], |
---|
| 394 | (int)entry->length, entry->data); |
---|
| 395 | |
---|
| 396 | return 0; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | static struct seq_operations ksyslog_queue_seq_ops = { |
---|
| 400 | .start = ksyslog_queue_seq_start, |
---|
| 401 | .next = ksyslog_queue_seq_next, |
---|
| 402 | .stop = ksyslog_queue_seq_stop, |
---|
| 403 | .show = ksyslog_queue_seq_show, |
---|
| 404 | }; |
---|
| 405 | |
---|
| 406 | static int |
---|
| 407 | ksyslog_queue_seq_open(struct inode *inode, struct file *file) |
---|
| 408 | { |
---|
| 409 | int err = seq_open(file, &ksyslog_queue_seq_ops); |
---|
| 410 | |
---|
| 411 | if (!err) |
---|
| 412 | ((struct seq_file *)file->private_data)->private = PDE(inode)->data; |
---|
| 413 | |
---|
| 414 | return err; |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | static struct file_operations ksyslog_queue_fops = { |
---|
| 418 | .owner = THIS_MODULE, |
---|
| 419 | .open = ksyslog_queue_seq_open, |
---|
| 420 | .read = seq_read, |
---|
| 421 | .llseek = seq_lseek, |
---|
| 422 | .release = seq_release, |
---|
| 423 | }; |
---|
| 424 | |
---|
| 425 | static int |
---|
| 426 | ksyslog_proc_init(void) |
---|
| 427 | { |
---|
| 428 | ksyslog_procdir = proc_mkdir("ksyslog", NULL); |
---|
| 429 | if (ksyslog_procdir == NULL) { |
---|
| 430 | pr_err("proc_mkdir failed\n"); |
---|
| 431 | return -ENOMEM; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | ksyslog_proc_queue = proc_create("queue", S_IRUGO, |
---|
| 435 | ksyslog_procdir, &ksyslog_queue_fops); |
---|
| 436 | if (ksyslog_proc_queue == NULL) { |
---|
| 437 | remove_proc_entry(ksyslog_procdir->name, ksyslog_procdir->parent); |
---|
| 438 | pr_err("proc_create failed\n"); |
---|
| 439 | return -ENOMEM; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | ksyslog_proc_queue->data = &ksyslog_queue.head; |
---|
| 443 | return 0; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | static void |
---|
| 447 | ksyslog_proc_destroy(void) |
---|
| 448 | { |
---|
| 449 | if (ksyslog_proc_queue) |
---|
| 450 | remove_proc_entry(ksyslog_proc_queue->name, ksyslog_proc_queue->parent); |
---|
| 451 | ksyslog_proc_queue = NULL; |
---|
| 452 | |
---|
| 453 | if (ksyslog_procdir) |
---|
| 454 | remove_proc_entry(ksyslog_procdir->name, ksyslog_procdir->parent); |
---|
| 455 | ksyslog_procdir = NULL; |
---|
| 456 | } |
---|
| 457 | #endif |
---|
| 458 | |
---|
| 459 | static void |
---|
| 460 | ksyslog_finish(void) |
---|
| 461 | { |
---|
| 462 | if (ksyslog_rcv_sk) |
---|
| 463 | sock_release(ksyslog_rcv_sk); |
---|
| 464 | ksyslog_rcv_sk = NULL; |
---|
| 465 | |
---|
| 466 | if (ksyslog_wq) { |
---|
| 467 | ksyslog_work_unregister(); |
---|
| 468 | destroy_workqueue(ksyslog_wq); |
---|
| 469 | } |
---|
| 470 | ksyslog_wq = NULL; |
---|
| 471 | |
---|
| 472 | #ifdef CONFIG_PROC_FS |
---|
| 473 | ksyslog_proc_destroy(); |
---|
| 474 | #endif |
---|
| 475 | |
---|
| 476 | ksyslog_entry_destroy(&ksyslog_queue); |
---|
| 477 | synchronize_rcu(); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | static int __init |
---|
| 481 | ksyslog_init(void) |
---|
| 482 | { |
---|
| 483 | int err; |
---|
| 484 | struct sockaddr_in sin; |
---|
| 485 | |
---|
| 486 | INIT_LIST_HEAD(&ksyslog_queue.head); |
---|
| 487 | |
---|
| 488 | #ifdef CONFIG_PROC_FS |
---|
| 489 | err = ksyslog_proc_init(); |
---|
| 490 | if (err) |
---|
| 491 | goto err; |
---|
| 492 | #endif |
---|
| 493 | |
---|
| 494 | ksyslog_wq = create_singlethread_workqueue("ksyslog"); |
---|
| 495 | if (ksyslog_wq == NULL) { |
---|
| 496 | pr_err("create_workqueue failed\n"); |
---|
| 497 | err = -ENOMEM; |
---|
| 498 | goto err; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | INIT_DELAYED_WORK(&ksyslog_work, ksyslog_work_handler); |
---|
| 502 | |
---|
| 503 | err = sock_create(AF_INET, SOCK_DGRAM, 0, &ksyslog_rcv_sk); |
---|
| 504 | if (err) { |
---|
| 505 | pr_err("sock_create failed\n"); |
---|
| 506 | goto err; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | sin.sin_family = AF_INET; |
---|
| 510 | sin.sin_addr.s_addr = in_aton(ksyslog_host); |
---|
| 511 | sin.sin_port = htons(ksyslog_port); |
---|
| 512 | |
---|
| 513 | err = kernel_bind(ksyslog_rcv_sk, (struct sockaddr *)&sin, |
---|
| 514 | sizeof(struct sockaddr_in)); |
---|
| 515 | if (err) { |
---|
| 516 | pr_err("kernel_bind failed\n"); |
---|
| 517 | goto err; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | udp_sk(ksyslog_rcv_sk->sk)->encap_type = UDP_ENCAP_KSYSLOG; |
---|
| 521 | udp_sk(ksyslog_rcv_sk->sk)->encap_rcv = ksyslog_rcv; |
---|
| 522 | |
---|
| 523 | ksyslog_work_register(ksyslog_flush_interval); |
---|
| 524 | |
---|
| 525 | return 0; |
---|
| 526 | |
---|
| 527 | err: |
---|
| 528 | ksyslog_finish(); |
---|
| 529 | return err; |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | static void __exit |
---|
| 533 | ksyslog_exit(void) |
---|
| 534 | { |
---|
| 535 | ksyslog_finish(); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | module_init(ksyslog_init); |
---|
| 539 | module_exit(ksyslog_exit); |
---|
| 540 | |
---|
| 541 | MODULE_AUTHOR("Atzm WATANABE"); |
---|
| 542 | MODULE_DESCRIPTION("In-kernel syslog receiver"); |
---|
| 543 | MODULE_LICENSE("GPL"); |
---|