mon_stat.c 1.5 KB

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