uid_stat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* drivers/misc/uid_stat.c
  2. *
  3. * Copyright (C) 2008 - 2009 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <asm/atomic.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/stat.h>
  24. #include <linux/uid_stat.h>
  25. #include <net/activity_stats.h>
  26. static DEFINE_SPINLOCK(uid_lock);
  27. static LIST_HEAD(uid_list);
  28. static struct proc_dir_entry *parent;
  29. struct uid_stat {
  30. struct list_head link;
  31. uid_t uid;
  32. atomic_t tcp_rcv;
  33. atomic_t tcp_snd;
  34. };
  35. static struct uid_stat *find_uid_stat(uid_t uid) {
  36. unsigned long flags;
  37. struct uid_stat *entry;
  38. spin_lock_irqsave(&uid_lock, flags);
  39. list_for_each_entry(entry, &uid_list, link) {
  40. if (entry->uid == uid) {
  41. spin_unlock_irqrestore(&uid_lock, flags);
  42. return entry;
  43. }
  44. }
  45. spin_unlock_irqrestore(&uid_lock, flags);
  46. return NULL;
  47. }
  48. static int tcp_snd_read_proc(char *page, char **start, off_t off,
  49. int count, int *eof, void *data)
  50. {
  51. int len;
  52. unsigned int bytes;
  53. char *p = page;
  54. struct uid_stat *uid_entry = (struct uid_stat *) data;
  55. if (!data)
  56. return 0;
  57. bytes = (unsigned int) (atomic_read(&uid_entry->tcp_snd) + INT_MIN);
  58. p += sprintf(p, "%u\n", bytes);
  59. len = (p - page) - off;
  60. *eof = (len <= count) ? 1 : 0;
  61. *start = page + off;
  62. return len;
  63. }
  64. static int tcp_rcv_read_proc(char *page, char **start, off_t off,
  65. int count, int *eof, void *data)
  66. {
  67. int len;
  68. unsigned int bytes;
  69. char *p = page;
  70. struct uid_stat *uid_entry = (struct uid_stat *) data;
  71. if (!data)
  72. return 0;
  73. bytes = (unsigned int) (atomic_read(&uid_entry->tcp_rcv) + INT_MIN);
  74. p += sprintf(p, "%u\n", bytes);
  75. len = (p - page) - off;
  76. *eof = (len <= count) ? 1 : 0;
  77. *start = page + off;
  78. return len;
  79. }
  80. /* Create a new entry for tracking the specified uid. */
  81. static struct uid_stat *create_stat(uid_t uid) {
  82. unsigned long flags;
  83. char uid_s[32];
  84. struct uid_stat *new_uid;
  85. struct proc_dir_entry *entry;
  86. /* Create the uid stat struct and append it to the list. */
  87. if ((new_uid = kmalloc(sizeof(struct uid_stat), GFP_KERNEL)) == NULL)
  88. return NULL;
  89. new_uid->uid = uid;
  90. /* Counters start at INT_MIN, so we can track 4GB of network traffic. */
  91. atomic_set(&new_uid->tcp_rcv, INT_MIN);
  92. atomic_set(&new_uid->tcp_snd, INT_MIN);
  93. spin_lock_irqsave(&uid_lock, flags);
  94. list_add_tail(&new_uid->link, &uid_list);
  95. spin_unlock_irqrestore(&uid_lock, flags);
  96. sprintf(uid_s, "%d", uid);
  97. entry = proc_mkdir(uid_s, parent);
  98. /* Keep reference to uid_stat so we know what uid to read stats from. */
  99. create_proc_read_entry("tcp_snd", S_IRUGO, entry , tcp_snd_read_proc,
  100. (void *) new_uid);
  101. create_proc_read_entry("tcp_rcv", S_IRUGO, entry, tcp_rcv_read_proc,
  102. (void *) new_uid);
  103. return new_uid;
  104. }
  105. int uid_stat_tcp_snd(uid_t uid, int size) {
  106. struct uid_stat *entry;
  107. activity_stats_update();
  108. if ((entry = find_uid_stat(uid)) == NULL &&
  109. ((entry = create_stat(uid)) == NULL)) {
  110. return -1;
  111. }
  112. atomic_add(size, &entry->tcp_snd);
  113. return 0;
  114. }
  115. int uid_stat_tcp_rcv(uid_t uid, int size) {
  116. struct uid_stat *entry;
  117. activity_stats_update();
  118. if ((entry = find_uid_stat(uid)) == NULL &&
  119. ((entry = create_stat(uid)) == NULL)) {
  120. return -1;
  121. }
  122. atomic_add(size, &entry->tcp_rcv);
  123. return 0;
  124. }
  125. static int __init uid_stat_init(void)
  126. {
  127. parent = proc_mkdir("uid_stat", NULL);
  128. if (!parent) {
  129. pr_err("uid_stat: failed to create proc entry\n");
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. __initcall(uid_stat_init);