summaryrefslogtreecommitdiff
path: root/drivers/misc/uid_stat.c
blob: f5fd7038779d067df340c06bd853c3b332579519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* drivers/misc/uid_stat.c
 *
 * Copyright (C) 2008 - 2009 Google, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <asm/atomic.h>

#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stat.h>
#include <linux/uid_stat.h>
#include <net/activity_stats.h>

static DEFINE_SPINLOCK(uid_lock);
static LIST_HEAD(uid_list);
static struct proc_dir_entry *parent;

struct uid_stat {
	struct list_head link;
	uid_t uid;
	atomic_t tcp_rcv;
	atomic_t tcp_snd;
	atomic_t tcp_rcv_pkt;
	atomic_t tcp_snd_pkt;
	atomic_t udp_rcv;
	atomic_t udp_snd;
	atomic_t udp_rcv_pkt;
	atomic_t udp_snd_pkt;
};

static int read_proc_entry(char *page, char **start, off_t off,
			int count, int *eof, void *data)
{
	int len;
	unsigned int value;
	char *p = page;
	atomic_t *uid_entry = (atomic_t *) data;
	if (!data)
		return 0;

	value = (unsigned int) (atomic_read(uid_entry) + INT_MIN);
	p += sprintf(p, "%u\n", value);
	len = (p - page) - off;
	*eof = (len <= count) ? 1 : 0;
	*start = page + off;
	return len;
}

/* Find or create a new entry for tracking the specified uid. */
static struct uid_stat *get_uid_stat(uid_t uid) {
	unsigned long flags;
	struct uid_stat *uid_entry;
	struct uid_stat *new_uid;
	struct proc_dir_entry *proc_entry;
	char uid_s[32];

	spin_lock_irqsave(&uid_lock, flags);
	list_for_each_entry(uid_entry, &uid_list, link) {
		if (uid_entry->uid == uid) {
			spin_unlock_irqrestore(&uid_lock, flags);
			return uid_entry;
		}
	}
	spin_unlock_irqrestore(&uid_lock, flags);

	/* Create a new entry for tracking the specified uid. */
	if ((new_uid = kmalloc(sizeof(struct uid_stat), GFP_KERNEL)) == NULL)
		return NULL;

	new_uid->uid = uid;
	/* Counters start at INT_MIN, so we can track 4GB of network traffic. */
	atomic_set(&new_uid->tcp_rcv, INT_MIN);
	atomic_set(&new_uid->tcp_snd, INT_MIN);
	atomic_set(&new_uid->tcp_snd_pkt, INT_MIN);
	atomic_set(&new_uid->tcp_rcv_pkt, INT_MIN);
	atomic_set(&new_uid->udp_rcv, INT_MIN);
	atomic_set(&new_uid->udp_snd, INT_MIN);
	atomic_set(&new_uid->udp_snd_pkt, INT_MIN);
	atomic_set(&new_uid->udp_rcv_pkt, INT_MIN);

	/* Append the newly created uid stat struct to the list. */
	spin_lock_irqsave(&uid_lock, flags);
	list_add_tail(&new_uid->link, &uid_list);
	spin_unlock_irqrestore(&uid_lock, flags);

	sprintf(uid_s, "%d", uid);
	proc_entry = proc_mkdir(uid_s, parent);

	/* Keep reference to uid_stat so we know what uid to read stats from. */
	create_proc_read_entry("tcp_snd", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->tcp_snd);

	create_proc_read_entry("tcp_rcv", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->tcp_rcv);

	create_proc_read_entry("tcp_snd_pkt", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->tcp_snd_pkt);

	create_proc_read_entry("tcp_rcv_pkt", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->tcp_rcv_pkt);

	create_proc_read_entry("udp_snd", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->udp_snd);

	create_proc_read_entry("udp_rcv", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->udp_rcv);

	create_proc_read_entry("udp_snd_pkt", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->udp_snd_pkt);

	create_proc_read_entry("udp_rcv_pkt", S_IRUGO, proc_entry, read_proc_entry,
			(void *) &new_uid->udp_rcv_pkt);

	return new_uid;
}

int uid_stat_tcp_snd(uid_t uid, int size) {
	struct uid_stat *entry;
	activity_stats_update();
	if ((entry = get_uid_stat(uid)) == NULL) {
		return -1;
	}
	atomic_add(size, &entry->tcp_snd);
	atomic_inc(&entry->tcp_snd_pkt);
	return 0;
}

int uid_stat_tcp_rcv(uid_t uid, int size) {
	struct uid_stat *entry;
	activity_stats_update();
	if ((entry = get_uid_stat(uid)) == NULL) {
		return -1;
	}
	atomic_add(size, &entry->tcp_rcv);
	atomic_inc(&entry->tcp_rcv_pkt);
	return 0;
}

int uid_stat_udp_snd(uid_t uid, int size) {
	struct uid_stat *entry;
	activity_stats_update();
	if ((entry = get_uid_stat(uid)) == NULL) {
		return -1;
	}
	atomic_add(size, &entry->udp_snd);
	atomic_inc(&entry->udp_snd_pkt);
	return 0;
}

int uid_stat_udp_rcv(uid_t uid, int size) {
	struct uid_stat *entry;
	activity_stats_update();
	if ((entry = get_uid_stat(uid)) == NULL) {
		return -1;
	}
	atomic_add(size, &entry->udp_rcv);
	atomic_inc(&entry->udp_rcv_pkt);
	return 0;
}

static int __init uid_stat_init(void)
{
	parent = proc_mkdir("uid_stat", NULL);
	if (!parent) {
		pr_err("uid_stat: failed to create proc entry\n");
		return -1;
	}
	return 0;
}

__initcall(uid_stat_init);