Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * drivers/misc/logger.c |
| 3 | * |
| 4 | * A Logging Subsystem |
| 5 | * |
| 6 | * Copyright (C) 2007-2008 Google, Inc. |
| 7 | * |
| 8 | * Robert Love <rlove@google.com> |
| 9 | * |
| 10 | * This software is licensed under the terms of the GNU General Public |
| 11 | * License version 2, as published by the Free Software Foundation, and |
| 12 | * may be copied, distributed, and modified under those terms. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | */ |
| 19 | |
Sachin Kamat | c810a39 | 2012-06-05 16:40:10 +0530 | [diff] [blame] | 20 | #define pr_fmt(fmt) "logger: " fmt |
| 21 | |
Corentin Chary | 23687af | 2009-11-28 09:45:14 +0100 | [diff] [blame] | 22 | #include <linux/sched.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/miscdevice.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/poll.h> |
Colin Cross | c11a166 | 2010-04-15 15:21:51 -0700 | [diff] [blame] | 28 | #include <linux/slab.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 29 | #include <linux/time.h> |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 31 | #include <linux/aio.h> |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 32 | #include "logger.h" |
| 33 | |
| 34 | #include <asm/ioctls.h> |
| 35 | |
Cruz Julian Bishop | 0edc6c6 | 2012-08-01 14:54:18 +1000 | [diff] [blame] | 36 | /** |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 37 | * struct logger_log - represents a specific log, such as 'main' or 'radio' |
Cruz Julian Bishop | 0edc6c6 | 2012-08-01 14:54:18 +1000 | [diff] [blame] | 38 | * @buffer: The actual ring buffer |
| 39 | * @misc: The "misc" device representing the log |
| 40 | * @wq: The wait queue for @readers |
| 41 | * @readers: This log's readers |
| 42 | * @mutex: The mutex that protects the @buffer |
| 43 | * @w_off: The current write head offset |
| 44 | * @head: The head, or location that readers start reading at. |
| 45 | * @size: The size of the log |
| 46 | * @logs: The list of log channels |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 47 | * |
| 48 | * This structure lives from module insertion until module removal, so it does |
| 49 | * not need additional reference counting. The structure is protected by the |
| 50 | * mutex 'mutex'. |
| 51 | */ |
| 52 | struct logger_log { |
Cruz Julian Bishop | 0edc6c6 | 2012-08-01 14:54:18 +1000 | [diff] [blame] | 53 | unsigned char *buffer; |
| 54 | struct miscdevice misc; |
| 55 | wait_queue_head_t wq; |
| 56 | struct list_head readers; |
| 57 | struct mutex mutex; |
| 58 | size_t w_off; |
| 59 | size_t head; |
| 60 | size_t size; |
| 61 | struct list_head logs; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 62 | }; |
| 63 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 64 | static LIST_HEAD(log_list); |
| 65 | |
| 66 | |
Cruz Julian Bishop | 0edc6c6 | 2012-08-01 14:54:18 +1000 | [diff] [blame] | 67 | /** |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 68 | * struct logger_reader - a logging device open for reading |
Cruz Julian Bishop | 0edc6c6 | 2012-08-01 14:54:18 +1000 | [diff] [blame] | 69 | * @log: The associated log |
| 70 | * @list: The associated entry in @logger_log's list |
| 71 | * @r_off: The current read head offset. |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 72 | * @r_all: Reader can read all entries |
| 73 | * @r_ver: Reader ABI version |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 74 | * |
| 75 | * This object lives from open to release, so we don't need additional |
| 76 | * reference counting. The structure is protected by log->mutex. |
| 77 | */ |
| 78 | struct logger_reader { |
Cruz Julian Bishop | 0edc6c6 | 2012-08-01 14:54:18 +1000 | [diff] [blame] | 79 | struct logger_log *log; |
| 80 | struct list_head list; |
| 81 | size_t r_off; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 82 | bool r_all; |
| 83 | int r_ver; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* logger_offset - returns index 'n' into the log via (optimized) modulus */ |
Tim Bird | e7f3eb0 | 2012-05-10 14:22:59 -0700 | [diff] [blame] | 87 | static size_t logger_offset(struct logger_log *log, size_t n) |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 88 | { |
Tim Bird | e7f3eb0 | 2012-05-10 14:22:59 -0700 | [diff] [blame] | 89 | return n & (log->size - 1); |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * file_get_log - Given a file structure, return the associated log |
| 95 | * |
| 96 | * This isn't aesthetic. We have several goals: |
| 97 | * |
Marco Navarra | 277cdd0 | 2011-12-15 17:57:48 +0100 | [diff] [blame] | 98 | * 1) Need to quickly obtain the associated log during an I/O operation |
| 99 | * 2) Readers need to maintain state (logger_reader) |
| 100 | * 3) Writers need to be very fast (open() should be a near no-op) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 101 | * |
| 102 | * In the reader case, we can trivially go file->logger_reader->logger_log. |
| 103 | * For a writer, we don't want to maintain a logger_reader, so we just go |
| 104 | * file->logger_log. Thus what file->private_data points at depends on whether |
| 105 | * or not the file was opened for reading. This function hides that dirtiness. |
| 106 | */ |
| 107 | static inline struct logger_log *file_get_log(struct file *file) |
| 108 | { |
| 109 | if (file->f_mode & FMODE_READ) { |
| 110 | struct logger_reader *reader = file->private_data; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 111 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 112 | return reader->log; |
Jerry Stralko | 48d5bb4 | 2014-07-01 07:13:18 -0400 | [diff] [blame] | 113 | } |
| 114 | return file->private_data; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 118 | * get_entry_header - returns a pointer to the logger_entry header within |
| 119 | * 'log' starting at offset 'off'. A temporary logger_entry 'scratch' must |
| 120 | * be provided. Typically the return value will be a pointer within |
| 121 | * 'logger->buf'. However, a pointer to 'scratch' may be returned if |
| 122 | * the log entry spans the end and beginning of the circular buffer. |
| 123 | */ |
| 124 | static struct logger_entry *get_entry_header(struct logger_log *log, |
| 125 | size_t off, struct logger_entry *scratch) |
| 126 | { |
| 127 | size_t len = min(sizeof(struct logger_entry), log->size - off); |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 128 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 129 | if (len != sizeof(struct logger_entry)) { |
| 130 | memcpy(((void *) scratch), log->buffer + off, len); |
| 131 | memcpy(((void *) scratch) + len, log->buffer, |
| 132 | sizeof(struct logger_entry) - len); |
| 133 | return scratch; |
| 134 | } |
| 135 | |
| 136 | return (struct logger_entry *) (log->buffer + off); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * get_entry_msg_len - Grabs the length of the message of the entry |
| 141 | * starting from from 'off'. |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 142 | * |
Tim Bird | 3bcfa43 | 2012-02-08 10:37:57 -0800 | [diff] [blame] | 143 | * An entry length is 2 bytes (16 bits) in host endian order. |
| 144 | * In the log, the length does not include the size of the log entry structure. |
| 145 | * This function returns the size including the log entry structure. |
| 146 | * |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 147 | * Caller needs to hold log->mutex. |
| 148 | */ |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 149 | static __u32 get_entry_msg_len(struct logger_log *log, size_t off) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 150 | { |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 151 | struct logger_entry scratch; |
| 152 | struct logger_entry *entry; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 153 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 154 | entry = get_entry_header(log, off, &scratch); |
| 155 | return entry->len; |
| 156 | } |
Tim Bird | 3bcfa43 | 2012-02-08 10:37:57 -0800 | [diff] [blame] | 157 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 158 | static size_t get_user_hdr_len(int ver) |
| 159 | { |
| 160 | if (ver < 2) |
| 161 | return sizeof(struct user_logger_entry_compat); |
Jerry Stralko | 48d5bb4 | 2014-07-01 07:13:18 -0400 | [diff] [blame] | 162 | return sizeof(struct logger_entry); |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 163 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 164 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 165 | static ssize_t copy_header_to_user(int ver, struct logger_entry *entry, |
| 166 | char __user *buf) |
| 167 | { |
| 168 | void *hdr; |
| 169 | size_t hdr_len; |
| 170 | struct user_logger_entry_compat v1; |
| 171 | |
| 172 | if (ver < 2) { |
| 173 | v1.len = entry->len; |
| 174 | v1.__pad = 0; |
| 175 | v1.pid = entry->pid; |
| 176 | v1.tid = entry->tid; |
| 177 | v1.sec = entry->sec; |
| 178 | v1.nsec = entry->nsec; |
| 179 | hdr = &v1; |
| 180 | hdr_len = sizeof(struct user_logger_entry_compat); |
| 181 | } else { |
| 182 | hdr = entry; |
| 183 | hdr_len = sizeof(struct logger_entry); |
| 184 | } |
| 185 | |
| 186 | return copy_to_user(buf, hdr, hdr_len); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the |
| 191 | * user-space buffer 'buf'. Returns 'count' on success. |
| 192 | * |
| 193 | * Caller must hold log->mutex. |
| 194 | */ |
| 195 | static ssize_t do_read_log_to_user(struct logger_log *log, |
| 196 | struct logger_reader *reader, |
| 197 | char __user *buf, |
| 198 | size_t count) |
| 199 | { |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 200 | struct logger_entry scratch; |
| 201 | struct logger_entry *entry; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 202 | size_t len; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 203 | size_t msg_start; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 204 | |
| 205 | /* |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 206 | * First, copy the header to userspace, using the version of |
| 207 | * the header requested |
| 208 | */ |
| 209 | entry = get_entry_header(log, reader->r_off, &scratch); |
| 210 | if (copy_header_to_user(reader->r_ver, entry, buf)) |
| 211 | return -EFAULT; |
| 212 | |
| 213 | count -= get_user_hdr_len(reader->r_ver); |
| 214 | buf += get_user_hdr_len(reader->r_ver); |
| 215 | msg_start = logger_offset(log, |
| 216 | reader->r_off + sizeof(struct logger_entry)); |
| 217 | |
| 218 | /* |
| 219 | * We read from the msg in two disjoint operations. First, we read from |
| 220 | * the current msg head offset up to 'count' bytes or to the end of |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 221 | * the log, whichever comes first. |
| 222 | */ |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 223 | len = min(count, log->size - msg_start); |
| 224 | if (copy_to_user(buf, log->buffer + msg_start, len)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 225 | return -EFAULT; |
| 226 | |
| 227 | /* |
| 228 | * Second, we read any remaining bytes, starting back at the head of |
| 229 | * the log. |
| 230 | */ |
| 231 | if (count != len) |
| 232 | if (copy_to_user(buf + len, log->buffer, count - len)) |
| 233 | return -EFAULT; |
| 234 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 235 | reader->r_off = logger_offset(log, reader->r_off + |
| 236 | sizeof(struct logger_entry) + count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 237 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 238 | return count + get_user_hdr_len(reader->r_ver); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * get_next_entry_by_uid - Starting at 'off', returns an offset into |
| 243 | * 'log->buffer' which contains the first entry readable by 'euid' |
| 244 | */ |
| 245 | static size_t get_next_entry_by_uid(struct logger_log *log, |
Xiong Zhou | bd47125 | 2013-05-08 18:52:48 +0800 | [diff] [blame] | 246 | size_t off, kuid_t euid) |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 247 | { |
| 248 | while (off != log->w_off) { |
| 249 | struct logger_entry *entry; |
| 250 | struct logger_entry scratch; |
| 251 | size_t next_len; |
| 252 | |
| 253 | entry = get_entry_header(log, off, &scratch); |
| 254 | |
Xiong Zhou | bd47125 | 2013-05-08 18:52:48 +0800 | [diff] [blame] | 255 | if (uid_eq(entry->euid, euid)) |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 256 | return off; |
| 257 | |
| 258 | next_len = sizeof(struct logger_entry) + entry->len; |
| 259 | off = logger_offset(log, off + next_len); |
| 260 | } |
| 261 | |
| 262 | return off; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /* |
| 266 | * logger_read - our log's read() method |
| 267 | * |
| 268 | * Behavior: |
| 269 | * |
Marco Navarra | 277cdd0 | 2011-12-15 17:57:48 +0100 | [diff] [blame] | 270 | * - O_NONBLOCK works |
| 271 | * - If there are no log entries to read, blocks until log is written to |
| 272 | * - Atomically reads exactly one log entry |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 273 | * |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 274 | * Will set errno to EINVAL if read |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 275 | * buffer is insufficient to hold next entry. |
| 276 | */ |
| 277 | static ssize_t logger_read(struct file *file, char __user *buf, |
| 278 | size_t count, loff_t *pos) |
| 279 | { |
| 280 | struct logger_reader *reader = file->private_data; |
| 281 | struct logger_log *log = reader->log; |
| 282 | ssize_t ret; |
| 283 | DEFINE_WAIT(wait); |
| 284 | |
| 285 | start: |
| 286 | while (1) { |
Tim Bird | c76c7ca | 2012-02-07 18:30:09 -0800 | [diff] [blame] | 287 | mutex_lock(&log->mutex); |
| 288 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 289 | prepare_to_wait(&log->wq, &wait, TASK_INTERRUPTIBLE); |
| 290 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 291 | ret = (log->w_off == reader->r_off); |
| 292 | mutex_unlock(&log->mutex); |
| 293 | if (!ret) |
| 294 | break; |
| 295 | |
| 296 | if (file->f_flags & O_NONBLOCK) { |
| 297 | ret = -EAGAIN; |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | if (signal_pending(current)) { |
| 302 | ret = -EINTR; |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | schedule(); |
| 307 | } |
| 308 | |
| 309 | finish_wait(&log->wq, &wait); |
| 310 | if (ret) |
| 311 | return ret; |
| 312 | |
| 313 | mutex_lock(&log->mutex); |
| 314 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 315 | if (!reader->r_all) |
| 316 | reader->r_off = get_next_entry_by_uid(log, |
| 317 | reader->r_off, current_euid()); |
| 318 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 319 | /* is there still something to read or did we race? */ |
| 320 | if (unlikely(log->w_off == reader->r_off)) { |
| 321 | mutex_unlock(&log->mutex); |
| 322 | goto start; |
| 323 | } |
| 324 | |
| 325 | /* get the size of the next entry */ |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 326 | ret = get_user_hdr_len(reader->r_ver) + |
| 327 | get_entry_msg_len(log, reader->r_off); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 328 | if (count < ret) { |
| 329 | ret = -EINVAL; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | /* get exactly one entry from the log */ |
| 334 | ret = do_read_log_to_user(log, reader, buf, ret); |
| 335 | |
| 336 | out: |
| 337 | mutex_unlock(&log->mutex); |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * get_next_entry - return the offset of the first valid entry at least 'len' |
| 344 | * bytes after 'off'. |
| 345 | * |
| 346 | * Caller must hold log->mutex. |
| 347 | */ |
| 348 | static size_t get_next_entry(struct logger_log *log, size_t off, size_t len) |
| 349 | { |
| 350 | size_t count = 0; |
| 351 | |
| 352 | do { |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 353 | size_t nr = sizeof(struct logger_entry) + |
| 354 | get_entry_msg_len(log, off); |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 355 | off = logger_offset(log, off + nr); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 356 | count += nr; |
| 357 | } while (count < len); |
| 358 | |
| 359 | return off; |
| 360 | } |
| 361 | |
| 362 | /* |
Tim Bird | 169c843 | 2012-02-07 18:32:02 -0800 | [diff] [blame] | 363 | * is_between - is a < c < b, accounting for wrapping of a, b, and c |
| 364 | * positions in the buffer |
| 365 | * |
| 366 | * That is, if a<b, check for c between a and b |
| 367 | * and if a>b, check for c outside (not between) a and b |
| 368 | * |
| 369 | * |------- a xxxxxxxx b --------| |
| 370 | * c^ |
| 371 | * |
| 372 | * |xxxxx b --------- a xxxxxxxxx| |
| 373 | * c^ |
| 374 | * or c^ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 375 | */ |
Tim Bird | 169c843 | 2012-02-07 18:32:02 -0800 | [diff] [blame] | 376 | static inline int is_between(size_t a, size_t b, size_t c) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 377 | { |
Tim Bird | 169c843 | 2012-02-07 18:32:02 -0800 | [diff] [blame] | 378 | if (a < b) { |
| 379 | /* is c between a and b? */ |
| 380 | if (a < c && c <= b) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 381 | return 1; |
| 382 | } else { |
Tim Bird | 169c843 | 2012-02-07 18:32:02 -0800 | [diff] [blame] | 383 | /* is c outside of b through a? */ |
| 384 | if (c <= b || a < c) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * fix_up_readers - walk the list of all readers and "fix up" any who were |
| 393 | * lapped by the writer; also do the same for the default "start head". |
| 394 | * We do this by "pulling forward" the readers and start head to the first |
| 395 | * entry after the new write head. |
| 396 | * |
| 397 | * The caller needs to hold log->mutex. |
| 398 | */ |
| 399 | static void fix_up_readers(struct logger_log *log, size_t len) |
| 400 | { |
| 401 | size_t old = log->w_off; |
Tim Bird | c626224 | 2012-02-07 18:26:38 -0800 | [diff] [blame] | 402 | size_t new = logger_offset(log, old + len); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 403 | struct logger_reader *reader; |
| 404 | |
Tim Bird | 169c843 | 2012-02-07 18:32:02 -0800 | [diff] [blame] | 405 | if (is_between(old, new, log->head)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 406 | log->head = get_next_entry(log, log->head, len); |
| 407 | |
| 408 | list_for_each_entry(reader, &log->readers, list) |
Tim Bird | 169c843 | 2012-02-07 18:32:02 -0800 | [diff] [blame] | 409 | if (is_between(old, new, reader->r_off)) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 410 | reader->r_off = get_next_entry(log, reader->r_off, len); |
| 411 | } |
| 412 | |
| 413 | /* |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 414 | * logger_write_iter - our write method, implementing support for write(), |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 415 | * writev(), and aio_write(). Writes are our fast path, and we try to optimize |
| 416 | * them above all else. |
| 417 | */ |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 418 | static ssize_t logger_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 419 | { |
| 420 | struct logger_log *log = file_get_log(iocb->ki_filp); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 421 | struct logger_entry header; |
| 422 | struct timespec now; |
Daniel Thompson | 5fecf3a | 2014-10-27 18:51:43 +0000 | [diff] [blame] | 423 | size_t len, count, w_off; |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 424 | |
| 425 | count = min_t(size_t, iocb->ki_nbytes, LOGGER_ENTRY_MAX_PAYLOAD); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 426 | |
| 427 | now = current_kernel_time(); |
| 428 | |
| 429 | header.pid = current->tgid; |
| 430 | header.tid = current->pid; |
| 431 | header.sec = now.tv_sec; |
| 432 | header.nsec = now.tv_nsec; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 433 | header.euid = current_euid(); |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 434 | header.len = count; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 435 | header.hdr_size = sizeof(struct logger_entry); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 436 | |
| 437 | /* null writes succeed, return zero */ |
| 438 | if (unlikely(!header.len)) |
| 439 | return 0; |
| 440 | |
| 441 | mutex_lock(&log->mutex); |
| 442 | |
| 443 | /* |
| 444 | * Fix up any readers, pulling them forward to the first readable |
| 445 | * entry after (what will be) the new write offset. We do this now |
| 446 | * because if we partially fail, we can end up with clobbered log |
| 447 | * entries that encroach on readable buffer. |
| 448 | */ |
| 449 | fix_up_readers(log, sizeof(struct logger_entry) + header.len); |
| 450 | |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 451 | len = min(sizeof(header), log->size - log->w_off); |
| 452 | memcpy(log->buffer + log->w_off, &header, len); |
| 453 | memcpy(log->buffer, (char *)&header + len, sizeof(header) - len); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 454 | |
Daniel Thompson | 5fecf3a | 2014-10-27 18:51:43 +0000 | [diff] [blame] | 455 | /* Work with a copy until we are ready to commit the whole entry */ |
| 456 | w_off = logger_offset(log, log->w_off + sizeof(struct logger_entry)); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 457 | |
Daniel Thompson | 5fecf3a | 2014-10-27 18:51:43 +0000 | [diff] [blame] | 458 | len = min(count, log->size - w_off); |
| 459 | |
| 460 | if (copy_from_iter(log->buffer + w_off, len, from) != len) { |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 461 | /* |
Daniel Thompson | 5fecf3a | 2014-10-27 18:51:43 +0000 | [diff] [blame] | 462 | * Note that by not updating log->w_off, this abandons the |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 463 | * portion of the new entry that *was* successfully |
| 464 | * copied, just above. This is intentional to avoid |
| 465 | * message corruption from missing fragments. |
| 466 | */ |
| 467 | mutex_unlock(&log->mutex); |
| 468 | return -EFAULT; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 469 | } |
| 470 | |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 471 | if (copy_from_iter(log->buffer, count - len, from) != count - len) { |
| 472 | mutex_unlock(&log->mutex); |
| 473 | return -EFAULT; |
| 474 | } |
| 475 | |
Daniel Thompson | 5fecf3a | 2014-10-27 18:51:43 +0000 | [diff] [blame] | 476 | log->w_off = logger_offset(log, w_off + count); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 477 | mutex_unlock(&log->mutex); |
| 478 | |
| 479 | /* wake up any blocked readers */ |
| 480 | wake_up_interruptible(&log->wq); |
| 481 | |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 482 | return len; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 483 | } |
| 484 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 485 | static struct logger_log *get_log_from_minor(int minor) |
| 486 | { |
| 487 | struct logger_log *log; |
| 488 | |
| 489 | list_for_each_entry(log, &log_list, logs) |
| 490 | if (log->misc.minor == minor) |
| 491 | return log; |
| 492 | return NULL; |
| 493 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * logger_open - the log's open() file operation |
| 497 | * |
| 498 | * Note how near a no-op this is in the write-only case. Keep it that way! |
| 499 | */ |
| 500 | static int logger_open(struct inode *inode, struct file *file) |
| 501 | { |
| 502 | struct logger_log *log; |
| 503 | int ret; |
| 504 | |
| 505 | ret = nonseekable_open(inode, file); |
| 506 | if (ret) |
| 507 | return ret; |
| 508 | |
| 509 | log = get_log_from_minor(MINOR(inode->i_rdev)); |
| 510 | if (!log) |
| 511 | return -ENODEV; |
| 512 | |
| 513 | if (file->f_mode & FMODE_READ) { |
| 514 | struct logger_reader *reader; |
| 515 | |
| 516 | reader = kmalloc(sizeof(struct logger_reader), GFP_KERNEL); |
| 517 | if (!reader) |
| 518 | return -ENOMEM; |
| 519 | |
| 520 | reader->log = log; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 521 | reader->r_ver = 1; |
| 522 | reader->r_all = in_egroup_p(inode->i_gid) || |
| 523 | capable(CAP_SYSLOG); |
| 524 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 525 | INIT_LIST_HEAD(&reader->list); |
| 526 | |
| 527 | mutex_lock(&log->mutex); |
| 528 | reader->r_off = log->head; |
| 529 | list_add_tail(&reader->list, &log->readers); |
| 530 | mutex_unlock(&log->mutex); |
| 531 | |
| 532 | file->private_data = reader; |
| 533 | } else |
| 534 | file->private_data = log; |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * logger_release - the log's release file operation |
| 541 | * |
| 542 | * Note this is a total no-op in the write-only case. Keep it that way! |
| 543 | */ |
| 544 | static int logger_release(struct inode *ignored, struct file *file) |
| 545 | { |
| 546 | if (file->f_mode & FMODE_READ) { |
| 547 | struct logger_reader *reader = file->private_data; |
Rabin Vincent | 47de87a | 2012-02-22 15:58:00 +0530 | [diff] [blame] | 548 | struct logger_log *log = reader->log; |
| 549 | |
| 550 | mutex_lock(&log->mutex); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 551 | list_del(&reader->list); |
Rabin Vincent | 47de87a | 2012-02-22 15:58:00 +0530 | [diff] [blame] | 552 | mutex_unlock(&log->mutex); |
| 553 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 554 | kfree(reader); |
| 555 | } |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * logger_poll - the log's poll file operation, for poll/select/epoll |
| 562 | * |
| 563 | * Note we always return POLLOUT, because you can always write() to the log. |
| 564 | * Note also that, strictly speaking, a return value of POLLIN does not |
| 565 | * guarantee that the log is readable without blocking, as there is a small |
| 566 | * chance that the writer can lap the reader in the interim between poll() |
| 567 | * returning and the read() request. |
| 568 | */ |
| 569 | static unsigned int logger_poll(struct file *file, poll_table *wait) |
| 570 | { |
| 571 | struct logger_reader *reader; |
| 572 | struct logger_log *log; |
| 573 | unsigned int ret = POLLOUT | POLLWRNORM; |
| 574 | |
| 575 | if (!(file->f_mode & FMODE_READ)) |
| 576 | return ret; |
| 577 | |
| 578 | reader = file->private_data; |
| 579 | log = reader->log; |
| 580 | |
| 581 | poll_wait(file, &log->wq, wait); |
| 582 | |
| 583 | mutex_lock(&log->mutex); |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 584 | if (!reader->r_all) |
| 585 | reader->r_off = get_next_entry_by_uid(log, |
| 586 | reader->r_off, current_euid()); |
| 587 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 588 | if (log->w_off != reader->r_off) |
| 589 | ret |= POLLIN | POLLRDNORM; |
| 590 | mutex_unlock(&log->mutex); |
| 591 | |
| 592 | return ret; |
| 593 | } |
| 594 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 595 | static long logger_set_version(struct logger_reader *reader, void __user *arg) |
| 596 | { |
| 597 | int version; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 598 | |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 599 | if (copy_from_user(&version, arg, sizeof(int))) |
| 600 | return -EFAULT; |
| 601 | |
| 602 | if ((version < 1) || (version > 2)) |
| 603 | return -EINVAL; |
| 604 | |
| 605 | reader->r_ver = version; |
| 606 | return 0; |
| 607 | } |
| 608 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 609 | static long logger_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 610 | { |
| 611 | struct logger_log *log = file_get_log(file); |
| 612 | struct logger_reader *reader; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 613 | long ret = -EINVAL; |
| 614 | void __user *argp = (void __user *) arg; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 615 | |
| 616 | mutex_lock(&log->mutex); |
| 617 | |
| 618 | switch (cmd) { |
| 619 | case LOGGER_GET_LOG_BUF_SIZE: |
| 620 | ret = log->size; |
| 621 | break; |
| 622 | case LOGGER_GET_LOG_LEN: |
| 623 | if (!(file->f_mode & FMODE_READ)) { |
| 624 | ret = -EBADF; |
| 625 | break; |
| 626 | } |
| 627 | reader = file->private_data; |
| 628 | if (log->w_off >= reader->r_off) |
| 629 | ret = log->w_off - reader->r_off; |
| 630 | else |
| 631 | ret = (log->size - reader->r_off) + log->w_off; |
| 632 | break; |
| 633 | case LOGGER_GET_NEXT_ENTRY_LEN: |
| 634 | if (!(file->f_mode & FMODE_READ)) { |
| 635 | ret = -EBADF; |
| 636 | break; |
| 637 | } |
| 638 | reader = file->private_data; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 639 | |
| 640 | if (!reader->r_all) |
| 641 | reader->r_off = get_next_entry_by_uid(log, |
| 642 | reader->r_off, current_euid()); |
| 643 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 644 | if (log->w_off != reader->r_off) |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 645 | ret = get_user_hdr_len(reader->r_ver) + |
| 646 | get_entry_msg_len(log, reader->r_off); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 647 | else |
| 648 | ret = 0; |
| 649 | break; |
| 650 | case LOGGER_FLUSH_LOG: |
| 651 | if (!(file->f_mode & FMODE_WRITE)) { |
| 652 | ret = -EBADF; |
| 653 | break; |
| 654 | } |
Al Viro | e77e430 | 2013-06-16 17:25:12 +0400 | [diff] [blame] | 655 | if (!(in_egroup_p(file_inode(file)->i_gid) || |
Charndeep Grewal | 1e70bd4 | 2013-02-26 22:07:38 -0800 | [diff] [blame] | 656 | capable(CAP_SYSLOG))) { |
| 657 | ret = -EPERM; |
| 658 | break; |
| 659 | } |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 660 | list_for_each_entry(reader, &log->readers, list) |
| 661 | reader->r_off = log->w_off; |
| 662 | log->head = log->w_off; |
| 663 | ret = 0; |
| 664 | break; |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 665 | case LOGGER_GET_VERSION: |
| 666 | if (!(file->f_mode & FMODE_READ)) { |
| 667 | ret = -EBADF; |
| 668 | break; |
| 669 | } |
| 670 | reader = file->private_data; |
| 671 | ret = reader->r_ver; |
| 672 | break; |
| 673 | case LOGGER_SET_VERSION: |
| 674 | if (!(file->f_mode & FMODE_READ)) { |
| 675 | ret = -EBADF; |
| 676 | break; |
| 677 | } |
| 678 | reader = file->private_data; |
| 679 | ret = logger_set_version(reader, argp); |
| 680 | break; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | mutex_unlock(&log->mutex); |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | static const struct file_operations logger_fops = { |
| 689 | .owner = THIS_MODULE, |
| 690 | .read = logger_read, |
Al Viro | cd678fc | 2014-08-23 12:20:37 -0400 | [diff] [blame] | 691 | .write_iter = logger_write_iter, |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 692 | .poll = logger_poll, |
| 693 | .unlocked_ioctl = logger_ioctl, |
| 694 | .compat_ioctl = logger_ioctl, |
| 695 | .open = logger_open, |
| 696 | .release = logger_release, |
| 697 | }; |
| 698 | |
| 699 | /* |
Nick Kralevich | 0441bcf | 2013-02-26 22:07:37 -0800 | [diff] [blame] | 700 | * Log size must must be a power of two, and greater than |
| 701 | * (LOGGER_ENTRY_MAX_PAYLOAD + sizeof(struct logger_entry)). |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 702 | */ |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 703 | static int __init create_log(char *log_name, int size) |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 704 | { |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 705 | int ret = 0; |
| 706 | struct logger_log *log; |
| 707 | unsigned char *buffer; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 708 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 709 | buffer = vmalloc(size); |
| 710 | if (buffer == NULL) |
| 711 | return -ENOMEM; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 712 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 713 | log = kzalloc(sizeof(struct logger_log), GFP_KERNEL); |
| 714 | if (log == NULL) { |
| 715 | ret = -ENOMEM; |
| 716 | goto out_free_buffer; |
| 717 | } |
| 718 | log->buffer = buffer; |
| 719 | |
| 720 | log->misc.minor = MISC_DYNAMIC_MINOR; |
| 721 | log->misc.name = kstrdup(log_name, GFP_KERNEL); |
| 722 | if (log->misc.name == NULL) { |
| 723 | ret = -ENOMEM; |
| 724 | goto out_free_log; |
| 725 | } |
| 726 | |
| 727 | log->misc.fops = &logger_fops; |
| 728 | log->misc.parent = NULL; |
| 729 | |
| 730 | init_waitqueue_head(&log->wq); |
| 731 | INIT_LIST_HEAD(&log->readers); |
| 732 | mutex_init(&log->mutex); |
| 733 | log->w_off = 0; |
| 734 | log->head = 0; |
| 735 | log->size = size; |
| 736 | |
| 737 | INIT_LIST_HEAD(&log->logs); |
| 738 | list_add_tail(&log->logs, &log_list); |
| 739 | |
| 740 | /* finally, initialize the misc device for this log */ |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 741 | ret = misc_register(&log->misc); |
| 742 | if (unlikely(ret)) { |
Sachin Kamat | c810a39 | 2012-06-05 16:40:10 +0530 | [diff] [blame] | 743 | pr_err("failed to register misc device for log '%s'!\n", |
| 744 | log->misc.name); |
Seunghun Lee | eb29835 | 2014-08-14 02:02:48 +0900 | [diff] [blame] | 745 | goto out_free_misc_name; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 746 | } |
| 747 | |
Sachin Kamat | c810a39 | 2012-06-05 16:40:10 +0530 | [diff] [blame] | 748 | pr_info("created %luK log '%s'\n", |
| 749 | (unsigned long) log->size >> 10, log->misc.name); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 750 | |
| 751 | return 0; |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 752 | |
Seunghun Lee | eb29835 | 2014-08-14 02:02:48 +0900 | [diff] [blame] | 753 | out_free_misc_name: |
| 754 | kfree(log->misc.name); |
| 755 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 756 | out_free_log: |
| 757 | kfree(log); |
| 758 | |
| 759 | out_free_buffer: |
| 760 | vfree(buffer); |
| 761 | return ret; |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static int __init logger_init(void) |
| 765 | { |
| 766 | int ret; |
| 767 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 768 | ret = create_log(LOGGER_LOG_MAIN, 256*1024); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 769 | if (unlikely(ret)) |
| 770 | goto out; |
| 771 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 772 | ret = create_log(LOGGER_LOG_EVENTS, 256*1024); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 773 | if (unlikely(ret)) |
| 774 | goto out; |
| 775 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 776 | ret = create_log(LOGGER_LOG_RADIO, 256*1024); |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 777 | if (unlikely(ret)) |
| 778 | goto out; |
| 779 | |
Tim Bird | 10b2419 | 2012-05-10 15:11:36 -0700 | [diff] [blame] | 780 | ret = create_log(LOGGER_LOG_SYSTEM, 256*1024); |
San Mehat | 3537cda | 2010-02-23 16:09:47 -0800 | [diff] [blame] | 781 | if (unlikely(ret)) |
| 782 | goto out; |
| 783 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 784 | out: |
| 785 | return ret; |
| 786 | } |
Luca Clementi | a3b41b7 | 2012-11-21 19:43:17 -0800 | [diff] [blame] | 787 | |
| 788 | static void __exit logger_exit(void) |
| 789 | { |
| 790 | struct logger_log *current_log, *next_log; |
| 791 | |
| 792 | list_for_each_entry_safe(current_log, next_log, &log_list, logs) { |
| 793 | /* we have to delete all the entry inside log_list */ |
| 794 | misc_deregister(¤t_log->misc); |
| 795 | vfree(current_log->buffer); |
| 796 | kfree(current_log->misc.name); |
| 797 | list_del(¤t_log->logs); |
| 798 | kfree(current_log); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | |
Greg Kroah-Hartman | 355b050 | 2011-11-30 20:18:14 +0900 | [diff] [blame] | 803 | device_initcall(logger_init); |
Luca Clementi | a3b41b7 | 2012-11-21 19:43:17 -0800 | [diff] [blame] | 804 | module_exit(logger_exit); |
| 805 | |
| 806 | MODULE_LICENSE("GPL"); |
| 807 | MODULE_AUTHOR("Robert Love, <rlove@google.com>"); |
| 808 | MODULE_DESCRIPTION("Android Logger"); |