mon_stat.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  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. #include <linux/kernel.h>
  10. #include <linux/usb.h>
  11. #include <linux/fs.h>
  12. #include <asm/uaccess.h>
  13. #include "usb_mon.h"
  14. #define STAT_BUF_SIZE 80
  15. struct snap {
  16. int slen;
  17. char str[STAT_BUF_SIZE];
  18. };
  19. static int mon_stat_open(struct inode *inode, struct file *file)
  20. {
  21. struct mon_bus *mbus;
  22. struct snap *sp;
  23. if ((sp = kmalloc(sizeof(struct snap), GFP_KERNEL)) == NULL)
  24. return -ENOMEM;
  25. mbus = inode->i_private;
  26. sp->slen = snprintf(sp->str, STAT_BUF_SIZE,
  27. "nreaders %d events %u text_lost %u\n",
  28. mbus->nreaders, mbus->cnt_events, mbus->cnt_text_lost);
  29. file->private_data = sp;
  30. return 0;
  31. }
  32. static ssize_t mon_stat_read(struct file *file, char __user *buf,
  33. size_t nbytes, loff_t *ppos)
  34. {
  35. struct snap *sp = file->private_data;
  36. return simple_read_from_buffer(buf, nbytes, ppos, sp->str, sp->slen);
  37. }
  38. static int mon_stat_release(struct inode *inode, struct file *file)
  39. {
  40. struct snap *sp = file->private_data;
  41. file->private_data = NULL;
  42. kfree(sp);
  43. return 0;
  44. }
  45. const struct file_operations mon_fops_stat = {
  46. .owner = THIS_MODULE,
  47. .open = mon_stat_open,
  48. .llseek = no_llseek,
  49. .read = mon_stat_read,
  50. /* .write = mon_stat_write, */
  51. /* .poll = mon_stat_poll, */
  52. /* .ioctl = mon_stat_ioctl, */
  53. .release = mon_stat_release,
  54. };