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#include <linux/config.h>
47#include <linux/mm.h>
48#include <linux/slab.h>
49#include <linux/acct.h>
50#include <linux/file.h>
51#include <linux/tty.h>
52#include <asm/uaccess.h>
53
54
55
56
57
58
59
60
61int acct_parm[3] = {4, 2, 30};
62#define RESUME (acct_parm[0])
63#define SUSPEND (acct_parm[1])
64#define ACCT_TIMEOUT (acct_parm[2])
65
66
67
68
69static void do_acct_process(long, struct file *);
70
71
72
73
74
75
76struct acct_glbs {
77 spinlock_t lock;
78 volatile int active;
79 volatile int needcheck;
80 struct file *file;
81 struct timer_list timer;
82};
83
84static struct acct_glbs acct_globals __cacheline_aligned = {SPIN_LOCK_UNLOCKED};
85
86
87
88
89static void acct_timeout(unsigned long unused)
90{
91 acct_globals.needcheck = 1;
92}
93
94
95
96
97static int check_free_space(struct file *file)
98{
99 struct statfs sbuf;
100 int res;
101 int act;
102
103 spin_lock(&acct_globals.lock);
104 res = acct_globals.active;
105 if (!file || !acct_globals.needcheck)
106 goto out;
107 spin_unlock(&acct_globals.lock);
108
109
110 if (vfs_statfs(file->f_dentry->d_inode->i_sb, &sbuf))
111 return res;
112
113 if (sbuf.f_bavail <= SUSPEND * sbuf.f_blocks / 100)
114 act = -1;
115 else if (sbuf.f_bavail >= RESUME * sbuf.f_blocks / 100)
116 act = 1;
117 else
118 act = 0;
119
120
121
122
123
124 spin_lock(&acct_globals.lock);
125 if (file != acct_globals.file) {
126 if (act)
127 res = act>0;
128 goto out;
129 }
130
131 if (acct_globals.active) {
132 if (act < 0) {
133 acct_globals.active = 0;
134 printk(KERN_INFO "Process accounting paused\n");
135 }
136 } else {
137 if (act > 0) {
138 acct_globals.active = 1;
139 printk(KERN_INFO "Process accounting resumed\n");
140 }
141 }
142
143 del_timer(&acct_globals.timer);
144 acct_globals.needcheck = 0;
145 acct_globals.timer.expires = jiffies + ACCT_TIMEOUT*HZ;
146 add_timer(&acct_globals.timer);
147 res = acct_globals.active;
148out:
149 spin_unlock(&acct_globals.lock);
150 return res;
151}
152
153
154
155
156
157
158
159void acct_file_reopen(struct file *file)
160{
161 struct file *old_acct = NULL;
162
163 if (acct_globals.file) {
164 old_acct = acct_globals.file;
165 del_timer(&acct_globals.timer);
166 acct_globals.active = 0;
167 acct_globals.needcheck = 0;
168 acct_globals.file = NULL;
169 }
170 if (file) {
171 acct_globals.file = file;
172 acct_globals.needcheck = 0;
173 acct_globals.active = 1;
174
175 init_timer(&acct_globals.timer);
176 acct_globals.timer.function = acct_timeout;
177 acct_globals.timer.expires = jiffies + ACCT_TIMEOUT*HZ;
178 add_timer(&acct_globals.timer);
179 }
180 if (old_acct) {
181 spin_unlock(&acct_globals.lock);
182 do_acct_process(0, old_acct);
183 filp_close(old_acct, NULL);
184 spin_lock(&acct_globals.lock);
185 }
186}
187
188
189
190
191
192
193
194asmlinkage long sys_acct(const char *name)
195{
196 struct file *file = NULL;
197 char *tmp;
198 int error;
199
200 if (!capable(CAP_SYS_PACCT))
201 return -EPERM;
202
203 if (name) {
204 tmp = getname(name);
205 if (IS_ERR(tmp)) {
206 return (PTR_ERR(tmp));
207 }
208
209 file = filp_open(tmp, O_WRONLY|O_APPEND, 0);
210 putname(tmp);
211 if (IS_ERR(file)) {
212 return (PTR_ERR(file));
213 }
214 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
215 filp_close(file, NULL);
216 return (-EACCES);
217 }
218
219 if (!file->f_op->write) {
220 filp_close(file, NULL);
221 return (-EIO);
222 }
223 }
224
225 error = security_ops->acct(file);
226 if (error)
227 return error;
228
229 spin_lock(&acct_globals.lock);
230 acct_file_reopen(file);
231 spin_unlock(&acct_globals.lock);
232
233 return (0);
234}
235
236
237
238
239
240void acct_auto_close(struct super_block *sb)
241{
242 spin_lock(&acct_globals.lock);
243 if (acct_globals.file &&
244 acct_globals.file->f_dentry->d_inode->i_sb == sb) {
245 acct_file_reopen((struct file *)NULL);
246 }
247 spin_unlock(&acct_globals.lock);
248}
249
250
251
252
253
254
255
256
257
258#define MANTSIZE 13
259#define EXPSIZE 3
260#define MAXFRACT ((1 << MANTSIZE) - 1)
261
262static comp_t encode_comp_t(unsigned long value)
263{
264 int exp, rnd;
265
266 exp = rnd = 0;
267 while (value > MAXFRACT) {
268 rnd = value & (1 << (EXPSIZE - 1));
269 value >>= EXPSIZE;
270 exp++;
271 }
272
273
274
275
276 if (rnd && (++value > MAXFRACT)) {
277 value >>= EXPSIZE;
278 exp++;
279 }
280
281
282
283
284 exp <<= MANTSIZE;
285 exp += value;
286 return exp;
287}
288
289
290
291
292
293
294
295
296
297
298
299
300
301static void do_acct_process(long exitcode, struct file *file)
302{
303 struct acct ac;
304 mm_segment_t fs;
305 unsigned long vsize;
306 unsigned long flim;
307
308
309
310
311
312 if (!check_free_space(file))
313 return;
314
315
316
317
318
319 memset((caddr_t)&ac, 0, sizeof(struct acct));
320
321 strncpy(ac.ac_comm, current->comm, ACCT_COMM);
322 ac.ac_comm[ACCT_COMM - 1] = '\0';
323
324 ac.ac_btime = CT_TO_SECS(current->start_time) +
325 (xtime.tv_sec - (jiffies / HZ));
326 ac.ac_etime = encode_comp_t(jiffies - current->start_time);
327 ac.ac_utime = encode_comp_t(current->utime);
328 ac.ac_stime = encode_comp_t(current->stime);
329 ac.ac_uid = current->uid;
330 ac.ac_gid = current->gid;
331 ac.ac_tty = (current->tty) ? kdev_t_to_nr(current->tty->device) : 0;
332
333 ac.ac_flag = 0;
334 if (current->flags & PF_FORKNOEXEC)
335 ac.ac_flag |= AFORK;
336 if (current->flags & PF_SUPERPRIV)
337 ac.ac_flag |= ASU;
338 if (current->flags & PF_DUMPCORE)
339 ac.ac_flag |= ACORE;
340 if (current->flags & PF_SIGNALED)
341 ac.ac_flag |= AXSIG;
342
343 vsize = 0;
344 if (current->mm) {
345 struct vm_area_struct *vma;
346 down_read(¤t->mm->mmap_sem);
347 vma = current->mm->mmap;
348 while (vma) {
349 vsize += vma->vm_end - vma->vm_start;
350 vma = vma->vm_next;
351 }
352 up_read(¤t->mm->mmap_sem);
353 }
354 vsize = vsize / 1024;
355 ac.ac_mem = encode_comp_t(vsize);
356 ac.ac_io = encode_comp_t(0 );
357 ac.ac_rw = encode_comp_t(ac.ac_io / 1024);
358 ac.ac_minflt = encode_comp_t(current->min_flt);
359 ac.ac_majflt = encode_comp_t(current->maj_flt);
360 ac.ac_swaps = encode_comp_t(current->nswap);
361 ac.ac_exitcode = exitcode;
362
363
364
365
366
367 fs = get_fs();
368 set_fs(KERNEL_DS);
369
370
371
372 flim = current->rlim[RLIMIT_FSIZE].rlim_cur;
373 current->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
374 file->f_op->write(file, (char *)&ac,
375 sizeof(struct acct), &file->f_pos);
376 current->rlim[RLIMIT_FSIZE].rlim_cur = flim;
377 set_fs(fs);
378}
379
380
381
382
383int acct_process(long exitcode)
384{
385 struct file *file = NULL;
386 spin_lock(&acct_globals.lock);
387 if (acct_globals.file) {
388 file = acct_globals.file;
389 get_file(file);
390 spin_unlock(&acct_globals.lock);
391 do_acct_process(exitcode, file);
392 fput(file);
393 } else
394 spin_unlock(&acct_globals.lock);
395 return 0;
396}
397