mon_stat.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The USB Monitor, inspired by Dave Harding's USBMon.
  4. *
  5. * This is the 's' or 'stat' reader which debugs usbmon itself.
  6. * Note that this code blows through locks, so make sure that
  7. * /dbg/usbmon/0s is well protected from non-root users.
  8. *
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/export.h>
  13. #include <linux/usb.h>
  14. #include <linux/fs.h>
  15. #include <linux/uaccess.h>
  16. #include "usb_mon.h"
  17. #define STAT_BUF_SIZE 80
  18. struct snap {
  19. int slen;
  20. char str[STAT_BUF_SIZE];
  21. };
  22. static int mon_stat_open(struct inode *inode, struct file *file)
  23. {
  24. struct mon_bus *mbus;
  25. struct snap *sp;
  26. sp = kmalloc(sizeof(struct snap), GFP_KERNEL);
  27. if (sp == NULL)
  28. return -ENOMEM;
  29. mbus = inode->i_private;
  30. sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
  31. "nreaders %d events %u text_lost %u\n",
  32. mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
  33. file->private_data = sp;
  34. return 0;
  35. }
  36. static ssize_t mon_stat_read(struct file *file, char __user *buf,
  37. size_t nbytes, loff_t *ppos)
  38. {
  39. struct snap *sp = file->private_data;
  40. return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
  41. }
  42. static int mon_stat_release(struct inode *inode, struct file *file)
  43. {
  44. struct snap *sp = file->private_data;
  45. file->private_data = NULL;
  46. kfree(sp);
  47. return 0;
  48. }
  49. const struct file_operations mon_fops_stat = {
  50. .owner = THIS_MODULE,
  51. .open = mon_stat_open,
  52. .llseek = no_llseek,
  53. .read = mon_stat_read,
  54. /* .write = mon_stat_write, */
  55. /* .poll = mon_stat_poll, */
  56. /* .unlocked_ioctl = mon_stat_ioctl, */
  57. .release = mon_stat_release,
  58. };