blob: 5bdf73a574981506f3776b5f6a6ec966d846d937 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The USB Monitor, inspired by Dave Harding's USBMon.
3 *
4 * This is the 's' or 'stat' reader which debugs usbmon itself.
5 * Note that this code blows through locks, so make sure that
6 * /dbg/usbmon/0s is well protected from non-root users.
7 *
8 */
9
10#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040012#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/usb.h>
Akinobu Mita518386c2008-06-09 16:39:57 -070014#include <linux/fs.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include "usb_mon.h"
18
19#define STAT_BUF_SIZE 80
20
21struct snap {
22 int slen;
23 char str[STAT_BUF_SIZE];
24};
25
26static int mon_stat_open(struct inode *inode, struct file *file)
27{
28 struct mon_bus *mbus;
29 struct snap *sp;
30
Greg Kroah-Hartman4c08ccf2015-04-30 11:32:58 +020031 sp = kmalloc(sizeof(struct snap), GFP_KERNEL);
32 if (sp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return -ENOMEM;
34
Theodore Ts'o8e18e292006-09-27 01:50:46 -070035 mbus = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37 sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
Pete Zaitcev5b1c6742006-06-09 20:10:10 -070038 "nreaders %d events %u text_lost %u\n",
39 mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 file->private_data = sp;
42 return 0;
43}
44
45static ssize_t mon_stat_read(struct file *file, char __user *buf,
46 size_t nbytes, loff_t *ppos)
47{
48 struct snap *sp = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Akinobu Mita518386c2008-06-09 16:39:57 -070050 return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static int mon_stat_release(struct inode *inode, struct file *file)
54{
Ming Leid4062fc2008-04-14 21:27:00 +080055 struct snap *sp = file->private_data;
56 file->private_data = NULL;
57 kfree(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return 0;
59}
60
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -030061const struct file_operations mon_fops_stat = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .owner = THIS_MODULE,
63 .open = mon_stat_open,
64 .llseek = no_llseek,
65 .read = mon_stat_read,
66 /* .write = mon_stat_write, */
67 /* .poll = mon_stat_poll, */
Arnd Bergmann55929332010-04-27 00:24:05 +020068 /* .unlocked_ioctl = mon_stat_ioctl, */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .release = mon_stat_release,
70};