uid_stat.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/stat.h>
  25. #include <linux/uid_stat.h>
  26. #include <net/activity_stats.h>
  27. static DEFINE_SPINLOCK(uid_lock);
  28. static LIST_HEAD(uid_list);
  29. static struct proc_dir_entry *parent;
  30. struct uid_stat {
  31. struct list_head link;
  32. uid_t uid;
  33. atomic_t tcp_rcv;
  34. atomic_t tcp_snd;
  35. };
  36. static struct uid_stat *find_uid_stat(uid_t uid) {
  37. struct uid_stat *entry;
  38. list_for_each_entry(entry, &uid_list, link) {
  39. if (entry->uid == uid) {
  40. return entry;
  41. }
  42. }
  43. return NULL;
  44. }
  45. static int uid_stat_atomic_int_show(struct seq_file *m, void *v)
  46. {
  47. unsigned int bytes;
  48. atomic_t *counter = m->private;
  49. bytes = (unsigned int) (atomic_read(counter) + INT_MIN);
  50. return seq_printf(m, "%u\n", bytes);
  51. }
  52. static int uid_stat_read_atomic_int_open(struct inode *inode, struct file *file)
  53. {
  54. return single_open(file, uid_stat_atomic_int_show, PDE(inode)->data);
  55. }
  56. static const struct file_operations uid_stat_read_atomic_int_fops = {
  57. .open = uid_stat_read_atomic_int_open,
  58. .read = seq_read,
  59. .llseek = seq_lseek,
  60. .release = single_release,
  61. };
  62. /* Create a new entry for tracking the specified uid. */
  63. static struct uid_stat *create_stat(uid_t uid) {
  64. struct uid_stat *new_uid;
  65. /* Create the uid stat struct and append it to the list. */
  66. new_uid = kmalloc(sizeof(struct uid_stat), GFP_ATOMIC);
  67. if (!new_uid)
  68. return NULL;
  69. new_uid->uid = uid;
  70. /* Counters start at INT_MIN, so we can track 4GB of network traffic. */
  71. atomic_set(&new_uid->tcp_rcv, INT_MIN);
  72. atomic_set(&new_uid->tcp_snd, INT_MIN);
  73. list_add_tail(&new_uid->link, &uid_list);
  74. return new_uid;
  75. }
  76. static void create_stat_proc(struct uid_stat *new_uid)
  77. {
  78. char uid_s[32];
  79. struct proc_dir_entry *entry;
  80. sprintf(uid_s, "%d", new_uid->uid);
  81. entry = proc_mkdir(uid_s, parent);
  82. /* Keep reference to uid_stat so we know what uid to read stats from. */
  83. proc_create_data("tcp_snd", S_IRUGO, entry,
  84. &uid_stat_read_atomic_int_fops, &new_uid->tcp_snd);
  85. proc_create_data("tcp_rcv", S_IRUGO, entry,
  86. &uid_stat_read_atomic_int_fops, &new_uid->tcp_rcv);
  87. }
  88. static struct uid_stat *find_or_create_uid_stat(uid_t uid)
  89. {
  90. struct uid_stat *entry;
  91. unsigned long flags;
  92. spin_lock_irqsave(&uid_lock, flags);
  93. entry = find_uid_stat(uid);
  94. if (entry) {
  95. spin_unlock_irqrestore(&uid_lock, flags);
  96. return entry;
  97. }
  98. entry = create_stat(uid);
  99. spin_unlock_irqrestore(&uid_lock, flags);
  100. if (entry)
  101. create_stat_proc(entry);
  102. return entry;
  103. }
  104. int uid_stat_tcp_snd(uid_t uid, int size) {
  105. struct uid_stat *entry;
  106. activity_stats_update();
  107. entry = find_or_create_uid_stat(uid);
  108. if (!entry)
  109. return -1;
  110. atomic_add(size, &entry->tcp_snd);
  111. return 0;
  112. }
  113. int uid_stat_tcp_rcv(uid_t uid, int size) {
  114. struct uid_stat *entry;
  115. activity_stats_update();
  116. entry = find_or_create_uid_stat(uid);
  117. if (!entry)
  118. return -1;
  119. atomic_add(size, &entry->tcp_rcv);
  120. return 0;
  121. }
  122. static int __init uid_stat_init(void)
  123. {
  124. parent = proc_mkdir("uid_stat", NULL);
  125. if (!parent) {
  126. pr_err("uid_stat: failed to create proc entry\n");
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. __initcall(uid_stat_init);