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