Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * The USB Monitor, inspired by Dave Harding's USBMon.
0004  *
0005  * This is the 's' or 'stat' reader which debugs usbmon itself.
0006  * Note that this code blows through locks, so make sure that
0007  * /dbg/usbmon/0s is well protected from non-root users.
0008  *
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/slab.h>
0013 #include <linux/export.h>
0014 #include <linux/usb.h>
0015 #include <linux/fs.h>
0016 #include <linux/uaccess.h>
0017 
0018 #include "usb_mon.h"
0019 
0020 #define STAT_BUF_SIZE  80
0021 
0022 struct snap {
0023     int slen;
0024     char str[STAT_BUF_SIZE];
0025 };
0026 
0027 static int mon_stat_open(struct inode *inode, struct file *file)
0028 {
0029     struct mon_bus *mbus;
0030     struct snap *sp;
0031 
0032     sp = kmalloc(sizeof(struct snap), GFP_KERNEL);
0033     if (sp == NULL)
0034         return -ENOMEM;
0035 
0036     mbus = inode->i_private;
0037 
0038     sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
0039         "nreaders %d events %u text_lost %u\n",
0040         mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
0041 
0042     file->private_data = sp;
0043     return 0;
0044 }
0045 
0046 static ssize_t mon_stat_read(struct file *file, char __user *buf,
0047                 size_t nbytes, loff_t *ppos)
0048 {
0049     struct snap *sp = file->private_data;
0050 
0051     return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
0052 }
0053 
0054 static int mon_stat_release(struct inode *inode, struct file *file)
0055 {
0056     struct snap *sp = file->private_data;
0057     file->private_data = NULL;
0058     kfree(sp);
0059     return 0;
0060 }
0061 
0062 const struct file_operations mon_fops_stat = {
0063     .owner =    THIS_MODULE,
0064     .open =     mon_stat_open,
0065     .llseek =   no_llseek,
0066     .read =     mon_stat_read,
0067     /* .write = mon_stat_write, */
0068     /* .poll =      mon_stat_poll, */
0069     /* .unlocked_ioctl =    mon_stat_ioctl, */
0070     .release =  mon_stat_release,
0071 };