uid_stat.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. struct uid_stat *entry;
  37. list_for_each_entry(entry, &uid_list, link) {
  38. if (entry->uid == uid) {
  39. return entry;
  40. }
  41. }
  42. return NULL;
  43. }
  44. static int tcp_snd_read_proc(char *page, char **start, off_t off,
  45. int count, int *eof, void *data)
  46. {
  47. int len;
  48. unsigned int bytes;
  49. char *p = page;
  50. struct uid_stat *uid_entry = (struct uid_stat *) data;
  51. if (!data)
  52. return 0;
  53. bytes = (unsigned int) (atomic_read(&uid_entry->tcp_snd) + INT_MIN);
  54. p += sprintf(p, "%u\n", bytes);
  55. len = (p - page) - off;
  56. *eof = (len <= count) ? 1 : 0;
  57. *start = page + off;
  58. return len;
  59. }
  60. static int tcp_rcv_read_proc(char *page, char **start, off_t off,
  61. int count, int *eof, void *data)
  62. {
  63. int len;
  64. unsigned int bytes;
  65. char *p = page;
  66. struct uid_stat *uid_entry = (struct uid_stat *) data;
  67. if (!data)
  68. return 0;
  69. bytes = (unsigned int) (atomic_read(&uid_entry->tcp_rcv) + INT_MIN);
  70. p += sprintf(p, "%u\n", bytes);
  71. len = (p - page) - off;
  72. *eof = (len <= count) ? 1 : 0;
  73. *start = page + off;
  74. return len;
  75. }
  76. /* Create a new entry for tracking the specified uid. */
  77. static struct uid_stat *create_stat(uid_t uid) {
  78. struct uid_stat *new_uid;
  79. /* Create the uid stat struct and append it to the list. */
  80. new_uid = kmalloc(sizeof(struct uid_stat), GFP_ATOMIC);
  81. if (!new_uid)
  82. return NULL;
  83. new_uid->uid = uid;
  84. /* Counters start at INT_MIN, so we can track 4GB of network traffic. */
  85. atomic_set(&new_uid->tcp_rcv, INT_MIN);
  86. atomic_set(&new_uid->tcp_snd, INT_MIN);
  87. list_add_tail(&new_uid->link, &uid_list);
  88. return new_uid;
  89. }
  90. static void create_stat_proc(struct uid_stat *new_uid)
  91. {
  92. char uid_s[32];
  93. struct proc_dir_entry *entry;
  94. sprintf(uid_s, "%d", new_uid->uid);
  95. entry = proc_mkdir(uid_s, parent);
  96. /* Keep reference to uid_stat so we know what uid to read stats from. */
  97. create_proc_read_entry("tcp_snd", S_IRUGO, entry , tcp_snd_read_proc,
  98. (void *) new_uid);
  99. create_proc_read_entry("tcp_rcv", S_IRUGO, entry, tcp_rcv_read_proc,
  100. (void *) new_uid);
  101. }
  102. static struct uid_stat *find_or_create_uid_stat(uid_t uid)
  103. {
  104. struct uid_stat *entry;
  105. unsigned long flags;
  106. spin_lock_irqsave(&uid_lock, flags);
  107. entry = find_uid_stat(uid);
  108. if (entry) {
  109. spin_unlock_irqrestore(&uid_lock, flags);
  110. return entry;
  111. }
  112. entry = create_stat(uid);
  113. spin_unlock_irqrestore(&uid_lock, flags);
  114. if (entry)
  115. create_stat_proc(entry);
  116. return entry;
  117. }
  118. int uid_stat_tcp_snd(uid_t uid, int size) {
  119. struct uid_stat *entry;
  120. activity_stats_update();
  121. entry = find_or_create_uid_stat(uid);
  122. if (!entry)
  123. return -1;
  124. atomic_add(size, &entry->tcp_snd);
  125. return 0;
  126. }
  127. int uid_stat_tcp_rcv(uid_t uid, int size) {
  128. struct uid_stat *entry;
  129. activity_stats_update();
  130. entry = find_or_create_uid_stat(uid);
  131. if (!entry)
  132. return -1;
  133. atomic_add(size, &entry->tcp_rcv);
  134. return 0;
  135. }
  136. static int __init uid_stat_init(void)
  137. {
  138. parent = proc_mkdir("uid_stat", NULL);
  139. if (!parent) {
  140. pr_err("uid_stat: failed to create proc entry\n");
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. __initcall(uid_stat_init);