mon_stat.c 1.5 KB

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