2 * drivers/misc/logger.c
6 * Copyright (C) 2007-2008 Google, Inc.
8 * Robert Love <rlove@google.com>
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.
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.
20 #define pr_fmt(fmt) "logger: " fmt
22 #include <linux/sched.h>
23 #include <linux/module.h>
25 #include <linux/miscdevice.h>
26 #include <linux/uaccess.h>
27 #include <linux/poll.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/vmalloc.h>
31 #include <linux/aio.h>
34 #include <asm/ioctls.h>
37 * struct logger_log - represents a specific log, such as 'main' or 'radio'
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
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
53 unsigned char *buffer
;
54 struct miscdevice misc
;
56 struct list_head readers
;
61 struct list_head logs
;
64 static LIST_HEAD(log_list
);
68 * struct logger_reader - a logging device open for reading
69 * @log: The associated log
70 * @list: The associated entry in @logger_log's list
71 * @r_off: The current read head offset.
72 * @r_all: Reader can read all entries
73 * @r_ver: Reader ABI version
75 * This object lives from open to release, so we don't need additional
76 * reference counting. The structure is protected by log->mutex.
78 struct logger_reader
{
79 struct logger_log
*log
;
80 struct list_head list
;
86 /* logger_offset - returns index 'n' into the log via (optimized) modulus */
87 static size_t logger_offset(struct logger_log
*log
, size_t n
)
89 return n
& (log
->size
- 1);
94 * file_get_log - Given a file structure, return the associated log
96 * This isn't aesthetic. We have several goals:
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)
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.
107 static inline struct logger_log
*file_get_log(struct file
*file
)
109 if (file
->f_mode
& FMODE_READ
) {
110 struct logger_reader
*reader
= file
->private_data
;
113 return file
->private_data
;
117 * get_entry_header - returns a pointer to the logger_entry header within
118 * 'log' starting at offset 'off'. A temporary logger_entry 'scratch' must
119 * be provided. Typically the return value will be a pointer within
120 * 'logger->buf'. However, a pointer to 'scratch' may be returned if
121 * the log entry spans the end and beginning of the circular buffer.
123 static struct logger_entry
*get_entry_header(struct logger_log
*log
,
124 size_t off
, struct logger_entry
*scratch
)
126 size_t len
= min(sizeof(struct logger_entry
), log
->size
- off
);
127 if (len
!= sizeof(struct logger_entry
)) {
128 memcpy(((void *) scratch
), log
->buffer
+ off
, len
);
129 memcpy(((void *) scratch
) + len
, log
->buffer
,
130 sizeof(struct logger_entry
) - len
);
134 return (struct logger_entry
*) (log
->buffer
+ off
);
138 * get_entry_msg_len - Grabs the length of the message of the entry
139 * starting from from 'off'.
141 * An entry length is 2 bytes (16 bits) in host endian order.
142 * In the log, the length does not include the size of the log entry structure.
143 * This function returns the size including the log entry structure.
145 * Caller needs to hold log->mutex.
147 static __u32
get_entry_msg_len(struct logger_log
*log
, size_t off
)
149 struct logger_entry scratch
;
150 struct logger_entry
*entry
;
152 entry
= get_entry_header(log
, off
, &scratch
);
156 static size_t get_user_hdr_len(int ver
)
159 return sizeof(struct user_logger_entry_compat
);
161 return sizeof(struct logger_entry
);
164 static ssize_t
copy_header_to_user(int ver
, struct logger_entry
*entry
,
169 struct user_logger_entry_compat v1
;
177 v1
.nsec
= entry
->nsec
;
179 hdr_len
= sizeof(struct user_logger_entry_compat
);
182 hdr_len
= sizeof(struct logger_entry
);
185 return copy_to_user(buf
, hdr
, hdr_len
);
189 * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the
190 * user-space buffer 'buf'. Returns 'count' on success.
192 * Caller must hold log->mutex.
194 static ssize_t
do_read_log_to_user(struct logger_log
*log
,
195 struct logger_reader
*reader
,
199 struct logger_entry scratch
;
200 struct logger_entry
*entry
;
205 * First, copy the header to userspace, using the version of
206 * the header requested
208 entry
= get_entry_header(log
, reader
->r_off
, &scratch
);
209 if (copy_header_to_user(reader
->r_ver
, entry
, buf
))
212 count
-= get_user_hdr_len(reader
->r_ver
);
213 buf
+= get_user_hdr_len(reader
->r_ver
);
214 msg_start
= logger_offset(log
,
215 reader
->r_off
+ sizeof(struct logger_entry
));
218 * We read from the msg in two disjoint operations. First, we read from
219 * the current msg head offset up to 'count' bytes or to the end of
220 * the log, whichever comes first.
222 len
= min(count
, log
->size
- msg_start
);
223 if (copy_to_user(buf
, log
->buffer
+ msg_start
, len
))
227 * Second, we read any remaining bytes, starting back at the head of
231 if (copy_to_user(buf
+ len
, log
->buffer
, count
- len
))
234 reader
->r_off
= logger_offset(log
, reader
->r_off
+
235 sizeof(struct logger_entry
) + count
);
237 return count
+ get_user_hdr_len(reader
->r_ver
);
241 * get_next_entry_by_uid - Starting at 'off', returns an offset into
242 * 'log->buffer' which contains the first entry readable by 'euid'
244 static size_t get_next_entry_by_uid(struct logger_log
*log
,
245 size_t off
, uid_t euid
)
247 while (off
!= log
->w_off
) {
248 struct logger_entry
*entry
;
249 struct logger_entry scratch
;
252 entry
= get_entry_header(log
, off
, &scratch
);
254 if (entry
->euid
== euid
)
257 next_len
= sizeof(struct logger_entry
) + entry
->len
;
258 off
= logger_offset(log
, off
+ next_len
);
265 * logger_read - our log's read() method
270 * - If there are no log entries to read, blocks until log is written to
271 * - Atomically reads exactly one log entry
273 * Will set errno to EINVAL if read
274 * buffer is insufficient to hold next entry.
276 static ssize_t
logger_read(struct file
*file
, char __user
*buf
,
277 size_t count
, loff_t
*pos
)
279 struct logger_reader
*reader
= file
->private_data
;
280 struct logger_log
*log
= reader
->log
;
286 mutex_lock(&log
->mutex
);
288 prepare_to_wait(&log
->wq
, &wait
, TASK_INTERRUPTIBLE
);
290 ret
= (log
->w_off
== reader
->r_off
);
291 mutex_unlock(&log
->mutex
);
295 if (file
->f_flags
& O_NONBLOCK
) {
300 if (signal_pending(current
)) {
308 finish_wait(&log
->wq
, &wait
);
312 mutex_lock(&log
->mutex
);
315 reader
->r_off
= get_next_entry_by_uid(log
,
316 reader
->r_off
, current_euid());
318 /* is there still something to read or did we race? */
319 if (unlikely(log
->w_off
== reader
->r_off
)) {
320 mutex_unlock(&log
->mutex
);
324 /* get the size of the next entry */
325 ret
= get_user_hdr_len(reader
->r_ver
) +
326 get_entry_msg_len(log
, reader
->r_off
);
332 /* get exactly one entry from the log */
333 ret
= do_read_log_to_user(log
, reader
, buf
, ret
);
336 mutex_unlock(&log
->mutex
);
342 * get_next_entry - return the offset of the first valid entry at least 'len'
345 * Caller must hold log->mutex.
347 static size_t get_next_entry(struct logger_log
*log
, size_t off
, size_t len
)
352 size_t nr
= sizeof(struct logger_entry
) +
353 get_entry_msg_len(log
, off
);
354 off
= logger_offset(log
, off
+ nr
);
356 } while (count
< len
);
362 * is_between - is a < c < b, accounting for wrapping of a, b, and c
363 * positions in the buffer
365 * That is, if a<b, check for c between a and b
366 * and if a>b, check for c outside (not between) a and b
368 * |------- a xxxxxxxx b --------|
371 * |xxxxx b --------- a xxxxxxxxx|
375 static inline int is_between(size_t a
, size_t b
, size_t c
)
378 /* is c between a and b? */
382 /* is c outside of b through a? */
391 * fix_up_readers - walk the list of all readers and "fix up" any who were
392 * lapped by the writer; also do the same for the default "start head".
393 * We do this by "pulling forward" the readers and start head to the first
394 * entry after the new write head.
396 * The caller needs to hold log->mutex.
398 static void fix_up_readers(struct logger_log
*log
, size_t len
)
400 size_t old
= log
->w_off
;
401 size_t new = logger_offset(log
, old
+ len
);
402 struct logger_reader
*reader
;
404 if (is_between(old
, new, log
->head
))
405 log
->head
= get_next_entry(log
, log
->head
, len
);
407 list_for_each_entry(reader
, &log
->readers
, list
)
408 if (is_between(old
, new, reader
->r_off
))
409 reader
->r_off
= get_next_entry(log
, reader
->r_off
, len
);
413 * do_write_log - writes 'len' bytes from 'buf' to 'log'
415 * The caller needs to hold log->mutex.
417 static void do_write_log(struct logger_log
*log
, const void *buf
, size_t count
)
421 len
= min(count
, log
->size
- log
->w_off
);
422 memcpy(log
->buffer
+ log
->w_off
, buf
, len
);
425 memcpy(log
->buffer
, buf
+ len
, count
- len
);
427 log
->w_off
= logger_offset(log
, log
->w_off
+ count
);
432 * do_write_log_user - writes 'len' bytes from the user-space buffer 'buf' to
435 * The caller needs to hold log->mutex.
437 * Returns 'count' on success, negative error code on failure.
439 static ssize_t
do_write_log_from_user(struct logger_log
*log
,
440 const void __user
*buf
, size_t count
)
444 len
= min(count
, log
->size
- log
->w_off
);
445 if (len
&& copy_from_user(log
->buffer
+ log
->w_off
, buf
, len
))
449 if (copy_from_user(log
->buffer
, buf
+ len
, count
- len
))
451 * Note that by not updating w_off, this abandons the
452 * portion of the new entry that *was* successfully
453 * copied, just above. This is intentional to avoid
454 * message corruption from missing fragments.
458 log
->w_off
= logger_offset(log
, log
->w_off
+ count
);
464 * logger_aio_write - our write method, implementing support for write(),
465 * writev(), and aio_write(). Writes are our fast path, and we try to optimize
466 * them above all else.
468 static ssize_t
logger_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
469 unsigned long nr_segs
, loff_t ppos
)
471 struct logger_log
*log
= file_get_log(iocb
->ki_filp
);
472 size_t orig
= log
->w_off
;
473 struct logger_entry header
;
477 now
= current_kernel_time();
479 header
.pid
= current
->tgid
;
480 header
.tid
= current
->pid
;
481 header
.sec
= now
.tv_sec
;
482 header
.nsec
= now
.tv_nsec
;
483 header
.euid
= current_euid();
484 header
.len
= min_t(size_t, iocb
->ki_left
, LOGGER_ENTRY_MAX_PAYLOAD
);
485 header
.hdr_size
= sizeof(struct logger_entry
);
487 /* null writes succeed, return zero */
488 if (unlikely(!header
.len
))
491 mutex_lock(&log
->mutex
);
494 * Fix up any readers, pulling them forward to the first readable
495 * entry after (what will be) the new write offset. We do this now
496 * because if we partially fail, we can end up with clobbered log
497 * entries that encroach on readable buffer.
499 fix_up_readers(log
, sizeof(struct logger_entry
) + header
.len
);
501 do_write_log(log
, &header
, sizeof(struct logger_entry
));
503 while (nr_segs
-- > 0) {
507 /* figure out how much of this vector we can keep */
508 len
= min_t(size_t, iov
->iov_len
, header
.len
- ret
);
510 /* write out this segment's payload */
511 nr
= do_write_log_from_user(log
, iov
->iov_base
, len
);
512 if (unlikely(nr
< 0)) {
514 mutex_unlock(&log
->mutex
);
522 mutex_unlock(&log
->mutex
);
524 /* wake up any blocked readers */
525 wake_up_interruptible(&log
->wq
);
530 static struct logger_log
*get_log_from_minor(int minor
)
532 struct logger_log
*log
;
534 list_for_each_entry(log
, &log_list
, logs
)
535 if (log
->misc
.minor
== minor
)
541 * logger_open - the log's open() file operation
543 * Note how near a no-op this is in the write-only case. Keep it that way!
545 static int logger_open(struct inode
*inode
, struct file
*file
)
547 struct logger_log
*log
;
550 ret
= nonseekable_open(inode
, file
);
554 log
= get_log_from_minor(MINOR(inode
->i_rdev
));
558 if (file
->f_mode
& FMODE_READ
) {
559 struct logger_reader
*reader
;
561 reader
= kmalloc(sizeof(struct logger_reader
), GFP_KERNEL
);
567 reader
->r_all
= in_egroup_p(inode
->i_gid
) ||
570 INIT_LIST_HEAD(&reader
->list
);
572 mutex_lock(&log
->mutex
);
573 reader
->r_off
= log
->head
;
574 list_add_tail(&reader
->list
, &log
->readers
);
575 mutex_unlock(&log
->mutex
);
577 file
->private_data
= reader
;
579 file
->private_data
= log
;
585 * logger_release - the log's release file operation
587 * Note this is a total no-op in the write-only case. Keep it that way!
589 static int logger_release(struct inode
*ignored
, struct file
*file
)
591 if (file
->f_mode
& FMODE_READ
) {
592 struct logger_reader
*reader
= file
->private_data
;
593 struct logger_log
*log
= reader
->log
;
595 mutex_lock(&log
->mutex
);
596 list_del(&reader
->list
);
597 mutex_unlock(&log
->mutex
);
606 * logger_poll - the log's poll file operation, for poll/select/epoll
608 * Note we always return POLLOUT, because you can always write() to the log.
609 * Note also that, strictly speaking, a return value of POLLIN does not
610 * guarantee that the log is readable without blocking, as there is a small
611 * chance that the writer can lap the reader in the interim between poll()
612 * returning and the read() request.
614 static unsigned int logger_poll(struct file
*file
, poll_table
*wait
)
616 struct logger_reader
*reader
;
617 struct logger_log
*log
;
618 unsigned int ret
= POLLOUT
| POLLWRNORM
;
620 if (!(file
->f_mode
& FMODE_READ
))
623 reader
= file
->private_data
;
626 poll_wait(file
, &log
->wq
, wait
);
628 mutex_lock(&log
->mutex
);
630 reader
->r_off
= get_next_entry_by_uid(log
,
631 reader
->r_off
, current_euid());
633 if (log
->w_off
!= reader
->r_off
)
634 ret
|= POLLIN
| POLLRDNORM
;
635 mutex_unlock(&log
->mutex
);
640 static long logger_set_version(struct logger_reader
*reader
, void __user
*arg
)
643 if (copy_from_user(&version
, arg
, sizeof(int)))
646 if ((version
< 1) || (version
> 2))
649 reader
->r_ver
= version
;
653 static long logger_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
655 struct logger_log
*log
= file_get_log(file
);
656 struct logger_reader
*reader
;
658 void __user
*argp
= (void __user
*) arg
;
660 mutex_lock(&log
->mutex
);
663 case LOGGER_GET_LOG_BUF_SIZE
:
666 case LOGGER_GET_LOG_LEN
:
667 if (!(file
->f_mode
& FMODE_READ
)) {
671 reader
= file
->private_data
;
672 if (log
->w_off
>= reader
->r_off
)
673 ret
= log
->w_off
- reader
->r_off
;
675 ret
= (log
->size
- reader
->r_off
) + log
->w_off
;
677 case LOGGER_GET_NEXT_ENTRY_LEN
:
678 if (!(file
->f_mode
& FMODE_READ
)) {
682 reader
= file
->private_data
;
685 reader
->r_off
= get_next_entry_by_uid(log
,
686 reader
->r_off
, current_euid());
688 if (log
->w_off
!= reader
->r_off
)
689 ret
= get_user_hdr_len(reader
->r_ver
) +
690 get_entry_msg_len(log
, reader
->r_off
);
694 case LOGGER_FLUSH_LOG
:
695 if (!(file
->f_mode
& FMODE_WRITE
)) {
699 if (!(in_egroup_p(file
->f_dentry
->d_inode
->i_gid
) ||
700 capable(CAP_SYSLOG
))) {
704 list_for_each_entry(reader
, &log
->readers
, list
)
705 reader
->r_off
= log
->w_off
;
706 log
->head
= log
->w_off
;
709 case LOGGER_GET_VERSION
:
710 if (!(file
->f_mode
& FMODE_READ
)) {
714 reader
= file
->private_data
;
717 case LOGGER_SET_VERSION
:
718 if (!(file
->f_mode
& FMODE_READ
)) {
722 reader
= file
->private_data
;
723 ret
= logger_set_version(reader
, argp
);
727 mutex_unlock(&log
->mutex
);
732 static const struct file_operations logger_fops
= {
733 .owner
= THIS_MODULE
,
735 .aio_write
= logger_aio_write
,
737 .unlocked_ioctl
= logger_ioctl
,
738 .compat_ioctl
= logger_ioctl
,
740 .release
= logger_release
,
744 * Log size must must be a power of two, and greater than
745 * (LOGGER_ENTRY_MAX_PAYLOAD + sizeof(struct logger_entry)).
747 static int __init
create_log(char *log_name
, int size
)
750 struct logger_log
*log
;
751 unsigned char *buffer
;
753 buffer
= vmalloc(size
);
757 log
= kzalloc(sizeof(struct logger_log
), GFP_KERNEL
);
760 goto out_free_buffer
;
762 log
->buffer
= buffer
;
764 log
->misc
.minor
= MISC_DYNAMIC_MINOR
;
765 log
->misc
.name
= kstrdup(log_name
, GFP_KERNEL
);
766 if (log
->misc
.name
== NULL
) {
771 log
->misc
.fops
= &logger_fops
;
772 log
->misc
.parent
= NULL
;
774 init_waitqueue_head(&log
->wq
);
775 INIT_LIST_HEAD(&log
->readers
);
776 mutex_init(&log
->mutex
);
781 INIT_LIST_HEAD(&log
->logs
);
782 list_add_tail(&log
->logs
, &log_list
);
784 /* finally, initialize the misc device for this log */
785 ret
= misc_register(&log
->misc
);
787 pr_err("failed to register misc device for log '%s'!\n",
792 pr_info("created %luK log '%s'\n",
793 (unsigned long) log
->size
>> 10, log
->misc
.name
);
805 static int __init
logger_init(void)
809 ret
= create_log(LOGGER_LOG_MAIN
, 256*1024);
813 ret
= create_log(LOGGER_LOG_EVENTS
, 256*1024);
817 ret
= create_log(LOGGER_LOG_RADIO
, 256*1024);
821 ret
= create_log(LOGGER_LOG_SYSTEM
, 256*1024);
829 static void __exit
logger_exit(void)
831 struct logger_log
*current_log
, *next_log
;
833 list_for_each_entry_safe(current_log
, next_log
, &log_list
, logs
) {
834 /* we have to delete all the entry inside log_list */
835 misc_deregister(¤t_log
->misc
);
836 vfree(current_log
->buffer
);
837 kfree(current_log
->misc
.name
);
838 list_del(¤t_log
->logs
);
844 device_initcall(logger_init
);
845 module_exit(logger_exit
);
847 MODULE_LICENSE("GPL");
848 MODULE_AUTHOR("Robert Love, <rlove@google.com>");
849 MODULE_DESCRIPTION("Android Logger");