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/mm.h>
47#include <linux/slab.h>
48#include <linux/acct.h>
49#include <linux/capability.h>
50#include <linux/file.h>
51#include <linux/tty.h>
52#include <linux/security.h>
53#include <linux/vfs.h>
54#include <linux/jiffies.h>
55#include <linux/times.h>
56#include <linux/syscalls.h>
57#include <linux/mount.h>
58#include <asm/uaccess.h>
59#include <asm/div64.h>
60#include <linux/blkdev.h>
61#include <linux/pid_namespace.h>
62
63
64
65
66
67
68
69
70int acct_parm[3] = {4, 2, 30};
71#define RESUME (acct_parm[0])
72#define SUSPEND (acct_parm[1])
73#define ACCT_TIMEOUT (acct_parm[2])
74
75
76
77
78static void do_acct_process(struct bsd_acct_struct *acct,
79 struct pid_namespace *ns, struct file *);
80
81
82
83
84
85
86struct bsd_acct_struct {
87 volatile int active;
88 volatile int needcheck;
89 struct file *file;
90 struct pid_namespace *ns;
91 struct timer_list timer;
92 struct list_head list;
93};
94
95static DEFINE_SPINLOCK(acct_lock);
96static LIST_HEAD(acct_list);
97
98
99
100
101static void acct_timeout(unsigned long x)
102{
103 struct bsd_acct_struct *acct = (struct bsd_acct_struct *)x;
104 acct->needcheck = 1;
105}
106
107
108
109
110static int check_free_space(struct bsd_acct_struct *acct, struct file *file)
111{
112 struct kstatfs sbuf;
113 int res;
114 int act;
115 sector_t resume;
116 sector_t suspend;
117
118 spin_lock(&acct_lock);
119 res = acct->active;
120 if (!file || !acct->needcheck)
121 goto out;
122 spin_unlock(&acct_lock);
123
124
125 if (vfs_statfs(file->f_path.dentry, &sbuf))
126 return res;
127 suspend = sbuf.f_blocks * SUSPEND;
128 resume = sbuf.f_blocks * RESUME;
129
130 sector_div(suspend, 100);
131 sector_div(resume, 100);
132
133 if (sbuf.f_bavail <= suspend)
134 act = -1;
135 else if (sbuf.f_bavail >= resume)
136 act = 1;
137 else
138 act = 0;
139
140
141
142
143
144 spin_lock(&acct_lock);
145 if (file != acct->file) {
146 if (act)
147 res = act>0;
148 goto out;
149 }
150
151 if (acct->active) {
152 if (act < 0) {
153 acct->active = 0;
154 printk(KERN_INFO "Process accounting paused\n");
155 }
156 } else {
157 if (act > 0) {
158 acct->active = 1;
159 printk(KERN_INFO "Process accounting resumed\n");
160 }
161 }
162
163 del_timer(&acct->timer);
164 acct->needcheck = 0;
165 acct->timer.expires = jiffies + ACCT_TIMEOUT*HZ;
166 add_timer(&acct->timer);
167 res = acct->active;
168out:
169 spin_unlock(&acct_lock);
170 return res;
171}
172
173
174
175
176
177
178
179static void acct_file_reopen(struct bsd_acct_struct *acct, struct file *file,
180 struct pid_namespace *ns)
181{
182 struct file *old_acct = NULL;
183 struct pid_namespace *old_ns = NULL;
184
185 if (acct->file) {
186 old_acct = acct->file;
187 old_ns = acct->ns;
188 del_timer(&acct->timer);
189 acct->active = 0;
190 acct->needcheck = 0;
191 acct->file = NULL;
192 acct->ns = NULL;
193 list_del(&acct->list);
194 }
195 if (file) {
196 acct->file = file;
197 acct->ns = ns;
198 acct->needcheck = 0;
199 acct->active = 1;
200 list_add(&acct->list, &acct_list);
201
202 setup_timer(&acct->timer, acct_timeout, (unsigned long)acct);
203 acct->timer.expires = jiffies + ACCT_TIMEOUT*HZ;
204 add_timer(&acct->timer);
205 }
206 if (old_acct) {
207 mnt_unpin(old_acct->f_path.mnt);
208 spin_unlock(&acct_lock);
209 do_acct_process(acct, old_ns, old_acct);
210 filp_close(old_acct, NULL);
211 spin_lock(&acct_lock);
212 }
213}
214
215static int acct_on(char *name)
216{
217 struct file *file;
218 int error;
219 struct pid_namespace *ns;
220 struct bsd_acct_struct *acct = NULL;
221
222
223 file = filp_open(name, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
224 if (IS_ERR(file))
225 return PTR_ERR(file);
226
227 if (!S_ISREG(file->f_path.dentry->d_inode->i_mode)) {
228 filp_close(file, NULL);
229 return -EACCES;
230 }
231
232 if (!file->f_op->write) {
233 filp_close(file, NULL);
234 return -EIO;
235 }
236
237 ns = task_active_pid_ns(current);
238 if (ns->bacct == NULL) {
239 acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
240 if (acct == NULL) {
241 filp_close(file, NULL);
242 return -ENOMEM;
243 }
244 }
245
246 error = security_acct(file);
247 if (error) {
248 kfree(acct);
249 filp_close(file, NULL);
250 return error;
251 }
252
253 spin_lock(&acct_lock);
254 if (ns->bacct == NULL) {
255 ns->bacct = acct;
256 acct = NULL;
257 }
258
259 mnt_pin(file->f_path.mnt);
260 acct_file_reopen(ns->bacct, file, ns);
261 spin_unlock(&acct_lock);
262
263 mntput(file->f_path.mnt);
264 kfree(acct);
265
266 return 0;
267}
268
269
270
271
272
273
274
275
276
277
278
279
280SYSCALL_DEFINE1(acct, const char __user *, name)
281{
282 int error;
283
284 if (!capable(CAP_SYS_PACCT))
285 return -EPERM;
286
287 if (name) {
288 char *tmp = getname(name);
289 if (IS_ERR(tmp))
290 return (PTR_ERR(tmp));
291 error = acct_on(tmp);
292 putname(tmp);
293 } else {
294 struct bsd_acct_struct *acct;
295
296 acct = task_active_pid_ns(current)->bacct;
297 if (acct == NULL)
298 return 0;
299
300 error = security_acct(NULL);
301 if (!error) {
302 spin_lock(&acct_lock);
303 acct_file_reopen(acct, NULL, NULL);
304 spin_unlock(&acct_lock);
305 }
306 }
307 return error;
308}
309
310
311
312
313
314
315
316
317void acct_auto_close_mnt(struct vfsmount *m)
318{
319 struct bsd_acct_struct *acct;
320
321 spin_lock(&acct_lock);
322restart:
323 list_for_each_entry(acct, &acct_list, list)
324 if (acct->file && acct->file->f_path.mnt == m) {
325 acct_file_reopen(acct, NULL, NULL);
326 goto restart;
327 }
328 spin_unlock(&acct_lock);
329}
330
331
332
333
334
335
336
337
338void acct_auto_close(struct super_block *sb)
339{
340 struct bsd_acct_struct *acct;
341
342 spin_lock(&acct_lock);
343restart:
344 list_for_each_entry(acct, &acct_list, list)
345 if (acct->file && acct->file->f_path.mnt->mnt_sb == sb) {
346 acct_file_reopen(acct, NULL, NULL);
347 goto restart;
348 }
349 spin_unlock(&acct_lock);
350}
351
352void acct_exit_ns(struct pid_namespace *ns)
353{
354 struct bsd_acct_struct *acct;
355
356 spin_lock(&acct_lock);
357 acct = ns->bacct;
358 if (acct != NULL) {
359 if (acct->file != NULL)
360 acct_file_reopen(acct, NULL, NULL);
361
362 kfree(acct);
363 }
364 spin_unlock(&acct_lock);
365}
366
367
368
369
370
371
372
373
374
375#define MANTSIZE 13
376#define EXPSIZE 3
377#define MAXFRACT ((1 << MANTSIZE) - 1)
378
379static comp_t encode_comp_t(unsigned long value)
380{
381 int exp, rnd;
382
383 exp = rnd = 0;
384 while (value > MAXFRACT) {
385 rnd = value & (1 << (EXPSIZE - 1));
386 value >>= EXPSIZE;
387 exp++;
388 }
389
390
391
392
393 if (rnd && (++value > MAXFRACT)) {
394 value >>= EXPSIZE;
395 exp++;
396 }
397
398
399
400
401 exp <<= MANTSIZE;
402 exp += value;
403 return exp;
404}
405
406#if ACCT_VERSION==1 || ACCT_VERSION==2
407
408
409
410
411
412
413
414
415
416#define MANTSIZE2 20
417#define EXPSIZE2 5
418#define MAXFRACT2 ((1ul << MANTSIZE2) - 1)
419#define MAXEXP2 ((1 <<EXPSIZE2) - 1)
420
421static comp2_t encode_comp2_t(u64 value)
422{
423 int exp, rnd;
424
425 exp = (value > (MAXFRACT2>>1));
426 rnd = 0;
427 while (value > MAXFRACT2) {
428 rnd = value & 1;
429 value >>= 1;
430 exp++;
431 }
432
433
434
435
436 if (rnd && (++value > MAXFRACT2)) {
437 value >>= 1;
438 exp++;
439 }
440
441 if (exp > MAXEXP2) {
442
443 return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
444 } else {
445 return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
446 }
447}
448#endif
449
450#if ACCT_VERSION==3
451
452
453
454static u32 encode_float(u64 value)
455{
456 unsigned exp = 190;
457 unsigned u;
458
459 if (value==0) return 0;
460 while ((s64)value > 0){
461 value <<= 1;
462 exp--;
463 }
464 u = (u32)(value >> 40) & 0x7fffffu;
465 return u | (exp << 23);
466}
467#endif
468
469
470
471
472
473
474
475
476
477
478
479
480
481static void do_acct_process(struct bsd_acct_struct *acct,
482 struct pid_namespace *ns, struct file *file)
483{
484 struct pacct_struct *pacct = ¤t->signal->pacct;
485 acct_t ac;
486 mm_segment_t fs;
487 unsigned long flim;
488 u64 elapsed;
489 u64 run_time;
490 struct timespec uptime;
491 struct tty_struct *tty;
492
493
494
495
496
497 if (!check_free_space(acct, file))
498 return;
499
500
501
502
503
504 memset((caddr_t)&ac, 0, sizeof(acct_t));
505
506 ac.ac_version = ACCT_VERSION | ACCT_BYTEORDER;
507 strlcpy(ac.ac_comm, current->comm, sizeof(ac.ac_comm));
508
509
510 do_posix_clock_monotonic_gettime(&uptime);
511 run_time = (u64)uptime.tv_sec*NSEC_PER_SEC + uptime.tv_nsec;
512 run_time -= (u64)current->group_leader->start_time.tv_sec * NSEC_PER_SEC
513 + current->group_leader->start_time.tv_nsec;
514
515 elapsed = nsec_to_AHZ(run_time);
516#if ACCT_VERSION==3
517 ac.ac_etime = encode_float(elapsed);
518#else
519 ac.ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
520 (unsigned long) elapsed : (unsigned long) -1l);
521#endif
522#if ACCT_VERSION==1 || ACCT_VERSION==2
523 {
524
525 comp2_t etime = encode_comp2_t(elapsed);
526 ac.ac_etime_hi = etime >> 16;
527 ac.ac_etime_lo = (u16) etime;
528 }
529#endif
530 do_div(elapsed, AHZ);
531 ac.ac_btime = get_seconds() - elapsed;
532
533 current_uid_gid(&ac.ac_uid, &ac.ac_gid);
534#if ACCT_VERSION==2
535 ac.ac_ahz = AHZ;
536#endif
537#if ACCT_VERSION==1 || ACCT_VERSION==2
538
539 ac.ac_uid16 = ac.ac_uid;
540 ac.ac_gid16 = ac.ac_gid;
541#endif
542#if ACCT_VERSION==3
543 ac.ac_pid = task_tgid_nr_ns(current, ns);
544 rcu_read_lock();
545 ac.ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
546 rcu_read_unlock();
547#endif
548
549 spin_lock_irq(¤t->sighand->siglock);
550 tty = current->signal->tty;
551 ac.ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
552 ac.ac_utime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_utime)));
553 ac.ac_stime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_stime)));
554 ac.ac_flag = pacct->ac_flag;
555 ac.ac_mem = encode_comp_t(pacct->ac_mem);
556 ac.ac_minflt = encode_comp_t(pacct->ac_minflt);
557 ac.ac_majflt = encode_comp_t(pacct->ac_majflt);
558 ac.ac_exitcode = pacct->ac_exitcode;
559 spin_unlock_irq(¤t->sighand->siglock);
560 ac.ac_io = encode_comp_t(0 );
561 ac.ac_rw = encode_comp_t(ac.ac_io / 1024);
562 ac.ac_swaps = encode_comp_t(0);
563
564
565
566
567
568 fs = get_fs();
569 set_fs(KERNEL_DS);
570
571
572
573 flim = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
574 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
575 file->f_op->write(file, (char *)&ac,
576 sizeof(acct_t), &file->f_pos);
577 current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
578 set_fs(fs);
579}
580
581
582
583
584
585void acct_init_pacct(struct pacct_struct *pacct)
586{
587 memset(pacct, 0, sizeof(struct pacct_struct));
588 pacct->ac_utime = pacct->ac_stime = cputime_zero;
589}
590
591
592
593
594
595
596void acct_collect(long exitcode, int group_dead)
597{
598 struct pacct_struct *pacct = ¤t->signal->pacct;
599 unsigned long vsize = 0;
600
601 if (group_dead && current->mm) {
602 struct vm_area_struct *vma;
603 down_read(¤t->mm->mmap_sem);
604 vma = current->mm->mmap;
605 while (vma) {
606 vsize += vma->vm_end - vma->vm_start;
607 vma = vma->vm_next;
608 }
609 up_read(¤t->mm->mmap_sem);
610 }
611
612 spin_lock_irq(¤t->sighand->siglock);
613 if (group_dead)
614 pacct->ac_mem = vsize / 1024;
615 if (thread_group_leader(current)) {
616 pacct->ac_exitcode = exitcode;
617 if (current->flags & PF_FORKNOEXEC)
618 pacct->ac_flag |= AFORK;
619 }
620 if (current->flags & PF_SUPERPRIV)
621 pacct->ac_flag |= ASU;
622 if (current->flags & PF_DUMPCORE)
623 pacct->ac_flag |= ACORE;
624 if (current->flags & PF_SIGNALED)
625 pacct->ac_flag |= AXSIG;
626 pacct->ac_utime = cputime_add(pacct->ac_utime, current->utime);
627 pacct->ac_stime = cputime_add(pacct->ac_stime, current->stime);
628 pacct->ac_minflt += current->min_flt;
629 pacct->ac_majflt += current->maj_flt;
630 spin_unlock_irq(¤t->sighand->siglock);
631}
632
633static void acct_process_in_ns(struct pid_namespace *ns)
634{
635 struct file *file = NULL;
636 struct bsd_acct_struct *acct;
637
638 acct = ns->bacct;
639
640
641
642 if (!acct || !acct->file)
643 return;
644
645 spin_lock(&acct_lock);
646 file = acct->file;
647 if (unlikely(!file)) {
648 spin_unlock(&acct_lock);
649 return;
650 }
651 get_file(file);
652 spin_unlock(&acct_lock);
653
654 do_acct_process(acct, ns, file);
655 fput(file);
656}
657
658
659
660
661
662
663
664void acct_process(void)
665{
666 struct pid_namespace *ns;
667
668
669
670
671
672
673 for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent)
674 acct_process_in_ns(ns);
675}
676