Blame view

kernel/acct.c 15.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  /*
   *  linux/kernel/acct.c
   *
   *  BSD Process Accounting for Linux
   *
   *  Author: Marco van Wieringen <mvw@planets.elm.net>
   *
   *  Some code based on ideas and code from:
   *  Thomas K. Dyas <tdyas@eden.rutgers.edu>
   *
   *  This file implements BSD-style process accounting. Whenever any
   *  process exits, an accounting record of type "struct acct" is
   *  written to the file specified with the acct() system call. It is
   *  up to user-level programs to do useful things with the accounting
   *  log. The kernel just provides the raw accounting information.
   *
   * (C) Copyright 1995 - 1997 Marco van Wieringen - ELM Consultancy B.V.
   *
   *  Plugged two leaks. 1) It didn't return acct_file into the free_filps if
   *  the file happened to be read-only. 2) If the accounting was suspended
   *  due to the lack of space it happily allowed to reopen it and completely
   *  lost the old acct_file. 3/10/98, Al Viro.
   *
   *  Now we silently close acct_file on attempt to reopen. Cleaned sys_acct().
   *  XTerms and EMACS are manifestations of pure evil. 21/10/98, AV.
   *
   *  Fixed a nasty interaction with with sys_umount(). If the accointing
   *  was suspeneded we failed to stop it on umount(). Messy.
   *  Another one: remount to readonly didn't stop accounting.
   *	Question: what should we do if we have CAP_SYS_ADMIN but not
   *  CAP_SYS_PACCT? Current code does the following: umount returns -EBUSY
   *  unless we are messing with the root. In that case we are getting a
   *  real mess with do_remount_sb(). 9/11/98, AV.
   *
   *  Fixed a bunch of races (and pair of leaks). Probably not the best way,
   *  but this one obviously doesn't introduce deadlocks. Later. BTW, found
   *  one race (and leak) in BSD implementation.
   *  OK, that's better. ANOTHER race and leak in BSD variant. There always
   *  is one more bug... 10/11/98, AV.
   *
   *	Oh, fsck... Oopsable SMP race in do_process_acct() - we must hold
   * ->mmap_sem to walk the vma list of current->mm. Nasty, since it leaks
   * a struct file opened for write. Fixed. 2/6/2000, AV.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
  #include <linux/mm.h>
  #include <linux/slab.h>
  #include <linux/acct.h>
c59ede7b7   Randy.Dunlap   [PATCH] move capa...
48
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
55
  #include <linux/file.h>
  #include <linux/tty.h>
  #include <linux/security.h>
  #include <linux/vfs.h>
  #include <linux/jiffies.h>
  #include <linux/times.h>
  #include <linux/syscalls.h>
7b7b1ace2   Al Viro   [PATCH] saner han...
56
  #include <linux/mount.h>
7153e4027   Paul McQuade   ipc, kernel: use ...
57
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
  #include <asm/div64.h>
  #include <linux/blkdev.h> /* sector_div */
5f7b703fe   Pavel Emelyanov   bsd_acct: using t...
60
  #include <linux/pid_namespace.h>
efb170c22   Al Viro   take fs_pin stuff...
61
  #include <linux/fs_pin.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  
  /*
   * These constants control the amount of freespace that suspend and
   * resume the process accounting system, and the time delay between
   * each check.
   * Turned into sysctl-controllable parameters. AV, 12/11/98
   */
  
  int acct_parm[3] = {4, 2, 30};
  #define RESUME		(acct_parm[0])	/* >foo% free space - resume */
  #define SUSPEND		(acct_parm[1])	/* <foo% free space - suspend */
  #define ACCT_TIMEOUT	(acct_parm[2])	/* foo second timeout between checks */
  
  /*
   * External references and all of the globals.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78

1629d0eb3   Al Viro   start carving bsd...
79
80
  struct bsd_acct_struct {
  	struct fs_pin		pin;
34cece2e8   Al Viro   take count and rc...
81
82
  	atomic_long_t		count;
  	struct rcu_head		rcu;
b8f00e6be   Al Viro   acct: new lifetim...
83
  	struct mutex		lock;
32dc73086   Al Viro   get rid of timer ...
84
85
  	int			active;
  	unsigned long		needcheck;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  	struct file		*file;
5f7b703fe   Pavel Emelyanov   bsd_acct: using t...
87
  	struct pid_namespace	*ns;
17c0a5aaf   Al Viro   make acct_kill() ...
88
89
  	struct work_struct	work;
  	struct completion	done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
  };
59eda0e07   Al Viro   new fs_pin killin...
91
  static void do_acct_process(struct bsd_acct_struct *acct);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
   * Check the amount of free space and suspend/resume accordingly.
   */
54a4d58a6   Al Viro   acct: simplify ch...
95
  static int check_free_space(struct bsd_acct_struct *acct)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
  {
  	struct kstatfs sbuf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98

54a4d58a6   Al Viro   acct: simplify ch...
99
  	if (time_is_before_jiffies(acct->needcheck))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  
  	/* May block */
54a4d58a6   Al Viro   acct: simplify ch...
103
  	if (vfs_statfs(&acct->file->f_path, &sbuf))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105

6248b1b34   Pavel Emelyanov   bsdacct: make int...
106
  	if (acct->active) {
54a4d58a6   Al Viro   acct: simplify ch...
107
108
109
  		u64 suspend = sbuf.f_blocks * SUSPEND;
  		do_div(suspend, 100);
  		if (sbuf.f_bavail <= suspend) {
6248b1b34   Pavel Emelyanov   bsdacct: make int...
110
  			acct->active = 0;
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
111
112
  			pr_info("Process accounting paused
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
  		}
  	} else {
54a4d58a6   Al Viro   acct: simplify ch...
115
116
117
  		u64 resume = sbuf.f_blocks * RESUME;
  		do_div(resume, 100);
  		if (sbuf.f_bavail >= resume) {
6248b1b34   Pavel Emelyanov   bsdacct: make int...
118
  			acct->active = 1;
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
119
120
  			pr_info("Process accounting resumed
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
  		}
  	}
32dc73086   Al Viro   get rid of timer ...
123
  	acct->needcheck = jiffies + ACCT_TIMEOUT*HZ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  out:
54a4d58a6   Al Viro   acct: simplify ch...
125
  	return acct->active;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  }
9e251d020   Al Viro   kill pin_put()
127
128
  static void acct_put(struct bsd_acct_struct *p)
  {
34cece2e8   Al Viro   take count and rc...
129
130
  	if (atomic_long_dec_and_test(&p->count))
  		kfree_rcu(p, rcu);
9e251d020   Al Viro   kill pin_put()
131
  }
59eda0e07   Al Viro   new fs_pin killin...
132
133
134
135
  static inline struct bsd_acct_struct *to_acct(struct fs_pin *p)
  {
  	return p ? container_of(p, struct bsd_acct_struct, pin) : NULL;
  }
215752fce   Al Viro   acct: get rid of ...
136
  static struct bsd_acct_struct *acct_get(struct pid_namespace *ns)
b8f00e6be   Al Viro   acct: new lifetim...
137
138
  {
  	struct bsd_acct_struct *res;
b8f00e6be   Al Viro   acct: new lifetim...
139
  again:
2798d4ce6   Al Viro   acct: get rid of ...
140
141
  	smp_rmb();
  	rcu_read_lock();
59eda0e07   Al Viro   new fs_pin killin...
142
  	res = to_acct(ACCESS_ONCE(ns->bacct));
2798d4ce6   Al Viro   acct: get rid of ...
143
144
  	if (!res) {
  		rcu_read_unlock();
215752fce   Al Viro   acct: get rid of ...
145
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  	}
34cece2e8   Al Viro   take count and rc...
147
  	if (!atomic_long_inc_not_zero(&res->count)) {
efb170c22   Al Viro   take fs_pin stuff...
148
149
  		rcu_read_unlock();
  		cpu_relax();
215752fce   Al Viro   acct: get rid of ...
150
  		goto again;
efb170c22   Al Viro   take fs_pin stuff...
151
152
153
  	}
  	rcu_read_unlock();
  	mutex_lock(&res->lock);
59eda0e07   Al Viro   new fs_pin killin...
154
  	if (res != to_acct(ACCESS_ONCE(ns->bacct))) {
efb170c22   Al Viro   take fs_pin stuff...
155
  		mutex_unlock(&res->lock);
9e251d020   Al Viro   kill pin_put()
156
  		acct_put(res);
efb170c22   Al Viro   take fs_pin stuff...
157
158
  		goto again;
  	}
b8f00e6be   Al Viro   acct: new lifetim...
159
160
  	return res;
  }
59eda0e07   Al Viro   new fs_pin killin...
161
162
163
164
165
166
167
168
169
170
171
172
  static void acct_pin_kill(struct fs_pin *pin)
  {
  	struct bsd_acct_struct *acct = to_acct(pin);
  	mutex_lock(&acct->lock);
  	do_acct_process(acct);
  	schedule_work(&acct->work);
  	wait_for_completion(&acct->done);
  	cmpxchg(&acct->ns->bacct, pin, NULL);
  	mutex_unlock(&acct->lock);
  	pin_remove(pin);
  	acct_put(acct);
  }
17c0a5aaf   Al Viro   make acct_kill() ...
173
174
175
176
  static void close_work(struct work_struct *work)
  {
  	struct bsd_acct_struct *acct = container_of(work, struct bsd_acct_struct, work);
  	struct file *file = acct->file;
17c0a5aaf   Al Viro   make acct_kill() ...
177
178
179
180
181
  	if (file->f_op->flush)
  		file->f_op->flush(file, NULL);
  	__fput_sync(file);
  	complete(&acct->done);
  }
669abf4e5   Jeff Layton   vfs: make path_op...
182
  static int acct_on(struct filename *pathname)
7b7b1ace2   Al Viro   [PATCH] saner han...
183
184
  {
  	struct file *file;
3064c3563   Al Viro   death to mnt_pinned
185
  	struct vfsmount *mnt, *internal;
b8f00e6be   Al Viro   acct: new lifetim...
186
  	struct pid_namespace *ns = task_active_pid_ns(current);
59eda0e07   Al Viro   new fs_pin killin...
187
188
  	struct bsd_acct_struct *acct;
  	struct fs_pin *old;
3064c3563   Al Viro   death to mnt_pinned
189
  	int err;
b8f00e6be   Al Viro   acct: new lifetim...
190
191
192
193
  
  	acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
  	if (!acct)
  		return -ENOMEM;
7b7b1ace2   Al Viro   [PATCH] saner han...
194
195
  
  	/* Difference from BSD - they don't do O_APPEND */
669abf4e5   Jeff Layton   vfs: make path_op...
196
  	file = file_open_name(pathname, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
b8f00e6be   Al Viro   acct: new lifetim...
197
198
  	if (IS_ERR(file)) {
  		kfree(acct);
7b7b1ace2   Al Viro   [PATCH] saner han...
199
  		return PTR_ERR(file);
b8f00e6be   Al Viro   acct: new lifetim...
200
  	}
7b7b1ace2   Al Viro   [PATCH] saner han...
201

496ad9aa8   Al Viro   new helper: file_...
202
  	if (!S_ISREG(file_inode(file)->i_mode)) {
b8f00e6be   Al Viro   acct: new lifetim...
203
  		kfree(acct);
7b7b1ace2   Al Viro   [PATCH] saner han...
204
205
206
  		filp_close(file, NULL);
  		return -EACCES;
  	}
d0f88f8d5   Al Viro   acct: check FMODE...
207
  	if (!(file->f_mode & FMODE_CAN_WRITE)) {
b8f00e6be   Al Viro   acct: new lifetim...
208
  		kfree(acct);
7b7b1ace2   Al Viro   [PATCH] saner han...
209
210
211
  		filp_close(file, NULL);
  		return -EIO;
  	}
3064c3563   Al Viro   death to mnt_pinned
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	internal = mnt_clone_internal(&file->f_path);
  	if (IS_ERR(internal)) {
  		kfree(acct);
  		filp_close(file, NULL);
  		return PTR_ERR(internal);
  	}
  	err = mnt_want_write(internal);
  	if (err) {
  		mntput(internal);
  		kfree(acct);
  		filp_close(file, NULL);
  		return err;
  	}
  	mnt = file->f_path.mnt;
  	file->f_path.mnt = internal;
7b7b1ace2   Al Viro   [PATCH] saner han...
227

34cece2e8   Al Viro   take count and rc...
228
  	atomic_long_set(&acct->count, 1);
59eda0e07   Al Viro   new fs_pin killin...
229
  	init_fs_pin(&acct->pin, acct_pin_kill);
b8f00e6be   Al Viro   acct: new lifetim...
230
231
232
233
  	acct->file = file;
  	acct->needcheck = jiffies;
  	acct->ns = ns;
  	mutex_init(&acct->lock);
59eda0e07   Al Viro   new fs_pin killin...
234
235
  	INIT_WORK(&acct->work, close_work);
  	init_completion(&acct->done);
efb170c22   Al Viro   take fs_pin stuff...
236
237
  	mutex_lock_nested(&acct->lock, 1);	/* nobody has seen it yet */
  	pin_insert(&acct->pin, mnt);
0b6b030fc   Pavel Emelyanov   bsdacct: switch f...
238

59eda0e07   Al Viro   new fs_pin killin...
239
240
  	rcu_read_lock();
  	old = xchg(&ns->bacct, &acct->pin);
efb170c22   Al Viro   take fs_pin stuff...
241
  	mutex_unlock(&acct->lock);
59eda0e07   Al Viro   new fs_pin killin...
242
  	pin_kill(old);
3064c3563   Al Viro   death to mnt_pinned
243
244
  	mnt_drop_write(mnt);
  	mntput(mnt);
7b7b1ace2   Al Viro   [PATCH] saner han...
245
246
  	return 0;
  }
9df7fa16e   Al Viro   acct: serialize a...
247
  static DEFINE_MUTEX(acct_on_mutex);
417ef5314   Randy Dunlap   [PATCH] kernel/ac...
248
249
250
251
252
253
254
255
256
257
  /**
   * sys_acct - enable/disable process accounting
   * @name: file name for accounting records or NULL to shutdown accounting
   *
   * Returns 0 for success or negative errno values for failure.
   *
   * sys_acct() is the only system call needed to implement process
   * accounting. It takes the name of the file where accounting records
   * should be written. If the filename is NULL, accounting will be
   * shutdown.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
   */
b290ebe2c   Heiko Carstens   [CVE-2009-0029] S...
259
  SYSCALL_DEFINE1(acct, const char __user *, name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  {
05b90496f   Eric Paris   security: remove ...
261
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
  
  	if (!capable(CAP_SYS_PACCT))
  		return -EPERM;
  
  	if (name) {
91a27b2a7   Jeff Layton   vfs: define struc...
267
  		struct filename *tmp = getname(name);
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
268

7b7b1ace2   Al Viro   [PATCH] saner han...
269
  		if (IS_ERR(tmp))
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
270
  			return PTR_ERR(tmp);
9df7fa16e   Al Viro   acct: serialize a...
271
  		mutex_lock(&acct_on_mutex);
669abf4e5   Jeff Layton   vfs: make path_op...
272
  		error = acct_on(tmp);
9df7fa16e   Al Viro   acct: serialize a...
273
  		mutex_unlock(&acct_on_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
  		putname(tmp);
7b7b1ace2   Al Viro   [PATCH] saner han...
275
  	} else {
59eda0e07   Al Viro   new fs_pin killin...
276
277
  		rcu_read_lock();
  		pin_kill(task_active_pid_ns(current)->bacct);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
  	}
05b90496f   Eric Paris   security: remove ...
279

7b7b1ace2   Al Viro   [PATCH] saner han...
280
281
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282

0b6b030fc   Pavel Emelyanov   bsdacct: switch f...
283
284
  void acct_exit_ns(struct pid_namespace *ns)
  {
59eda0e07   Al Viro   new fs_pin killin...
285
286
  	rcu_read_lock();
  	pin_kill(ns->bacct);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  }
  
  /*
   *  encode an unsigned long into a comp_t
   *
   *  This routine has been adopted from the encode_comp_t() function in
   *  the kern_acct.c file of the FreeBSD operating system. The encoding
   *  is a 13-bit fraction with a 3-bit (base 8) exponent.
   */
  
  #define	MANTSIZE	13			/* 13 bit mantissa. */
  #define	EXPSIZE		3			/* Base 8 (3 bit) exponent. */
  #define	MAXFRACT	((1 << MANTSIZE) - 1)	/* Maximum fractional value. */
  
  static comp_t encode_comp_t(unsigned long value)
  {
  	int exp, rnd;
  
  	exp = rnd = 0;
  	while (value > MAXFRACT) {
  		rnd = value & (1 << (EXPSIZE - 1));	/* Round up? */
  		value >>= EXPSIZE;	/* Base 8 exponent == 3 bit shift. */
  		exp++;
  	}
  
  	/*
6ae965cd6   Daniel Walker   whitespace fixes:...
313
314
  	 * If we need to round up, do it (and handle overflow correctly).
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
316
317
318
319
320
  	if (rnd && (++value > MAXFRACT)) {
  		value >>= EXPSIZE;
  		exp++;
  	}
  
  	/*
6ae965cd6   Daniel Walker   whitespace fixes:...
321
322
  	 * Clean it up and polish it off.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
326
  	exp <<= MANTSIZE;		/* Shift the exponent into place */
  	exp += value;			/* and add on the mantissa. */
  	return exp;
  }
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
327
  #if ACCT_VERSION == 1 || ACCT_VERSION == 2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
333
334
335
336
337
338
339
  /*
   * encode an u64 into a comp2_t (24 bits)
   *
   * Format: 5 bit base 2 exponent, 20 bits mantissa.
   * The leading bit of the mantissa is not stored, but implied for
   * non-zero exponents.
   * Largest encodable value is 50 bits.
   */
  
  #define MANTSIZE2       20                      /* 20 bit mantissa. */
  #define EXPSIZE2        5                       /* 5 bit base 2 exponent. */
  #define MAXFRACT2       ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
340
  #define MAXEXP2         ((1 << EXPSIZE2) - 1)    /* Maximum exponent. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
342
343
  
  static comp2_t encode_comp2_t(u64 value)
  {
6ae965cd6   Daniel Walker   whitespace fixes:...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  	int exp, rnd;
  
  	exp = (value > (MAXFRACT2>>1));
  	rnd = 0;
  	while (value > MAXFRACT2) {
  		rnd = value & 1;
  		value >>= 1;
  		exp++;
  	}
  
  	/*
  	 * If we need to round up, do it (and handle overflow correctly).
  	 */
  	if (rnd && (++value > MAXFRACT2)) {
  		value >>= 1;
  		exp++;
  	}
  
  	if (exp > MAXEXP2) {
  		/* Overflow. Return largest representable number instead. */
  		return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
  	} else {
  		return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
  }
  #endif
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
370
  #if ACCT_VERSION == 3
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
372
373
374
375
376
377
  /*
   * encode an u64 into a 32 bit IEEE float
   */
  static u32 encode_float(u64 value)
  {
  	unsigned exp = 190;
  	unsigned u;
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
378
379
380
  	if (value == 0)
  		return 0;
  	while ((s64)value > 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  		value <<= 1;
  		exp--;
  	}
  	u = (u32)(value >> 40) & 0x7fffffu;
  	return u | (exp << 23);
  }
  #endif
  
  /*
   *  Write an accounting entry for an exiting process
   *
   *  The acct_process() call is the workhorse of the process
   *  accounting system. The struct acct is built here and then written
   *  into the accounting file. This function should only be called from
bcbe4a076   Ingo Molnar   sched: fix kernel...
395
   *  do_exit() or when switching to a different output file.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
   */
cdd37e230   Al Viro   separate namespac...
397
  static void fill_ac(acct_t *ac)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398
  {
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
399
  	struct pacct_struct *pacct = &current->signal->pacct;
ccbf62d8a   Thomas Gleixner   sched: Make task-...
400
  	u64 elapsed, run_time;
24ec839c4   Peter Zijlstra   [PATCH] tty: ->si...
401
  	struct tty_struct *tty;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
405
406
  
  	/*
  	 * Fill the accounting struct with the needed info as recorded
  	 * by the different kernel functions.
  	 */
cdd37e230   Al Viro   separate namespac...
407
  	memset(ac, 0, sizeof(acct_t));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408

cdd37e230   Al Viro   separate namespac...
409
410
  	ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER;
  	strlcpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
412
  
  	/* calculate run_time in nsec*/
ccbf62d8a   Thomas Gleixner   sched: Make task-...
413
414
  	run_time = ktime_get_ns();
  	run_time -= current->group_leader->start_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
  	/* convert nsec -> AHZ */
  	elapsed = nsec_to_AHZ(run_time);
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
417
  #if ACCT_VERSION == 3
cdd37e230   Al Viro   separate namespac...
418
  	ac->ac_etime = encode_float(elapsed);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
  #else
cdd37e230   Al Viro   separate namespac...
420
  	ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
421
  				(unsigned long) elapsed : (unsigned long) -1l);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
  #endif
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
423
  #if ACCT_VERSION == 1 || ACCT_VERSION == 2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424
425
426
  	{
  		/* new enlarged etime field */
  		comp2_t etime = encode_comp2_t(elapsed);
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
427

cdd37e230   Al Viro   separate namespac...
428
429
  		ac->ac_etime_hi = etime >> 16;
  		ac->ac_etime_lo = (u16) etime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
431
432
  	}
  #endif
  	do_div(elapsed, AHZ);
cdd37e230   Al Viro   separate namespac...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
  	ac->ac_btime = get_seconds() - elapsed;
  #if ACCT_VERSION==2
  	ac->ac_ahz = AHZ;
  #endif
  
  	spin_lock_irq(&current->sighand->siglock);
  	tty = current->signal->tty;	/* Safe as we hold the siglock */
  	ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
  	ac->ac_utime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_utime)));
  	ac->ac_stime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_stime)));
  	ac->ac_flag = pacct->ac_flag;
  	ac->ac_mem = encode_comp_t(pacct->ac_mem);
  	ac->ac_minflt = encode_comp_t(pacct->ac_minflt);
  	ac->ac_majflt = encode_comp_t(pacct->ac_majflt);
  	ac->ac_exitcode = pacct->ac_exitcode;
  	spin_unlock_irq(&current->sighand->siglock);
  }
  /*
   *  do_acct_process does all actual work. Caller holds the reference to file.
   */
b8f00e6be   Al Viro   acct: new lifetim...
453
  static void do_acct_process(struct bsd_acct_struct *acct)
cdd37e230   Al Viro   separate namespac...
454
455
456
457
  {
  	acct_t ac;
  	unsigned long flim;
  	const struct cred *orig_cred;
b8f00e6be   Al Viro   acct: new lifetim...
458
  	struct file *file = acct->file;
cdd37e230   Al Viro   separate namespac...
459
460
461
462
463
464
465
466
467
468
469
470
471
  
  	/*
  	 * Accounting records are not subject to resource limits.
  	 */
  	flim = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  	current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
  	/* Perform file operations on behalf of whoever enabled accounting */
  	orig_cred = override_creds(file->f_cred);
  
  	/*
  	 * First check to see if there is enough free_space to continue
  	 * the process accounting system.
  	 */
54a4d58a6   Al Viro   acct: simplify ch...
472
  	if (!check_free_space(acct))
cdd37e230   Al Viro   separate namespac...
473
474
475
  		goto out;
  
  	fill_ac(&ac);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
  	/* we really need to bite the bullet and change layout */
f8f3d4de2   Eric W. Biederman   userns: Convert b...
477
478
  	ac.ac_uid = from_kuid_munged(file->f_cred->user_ns, orig_cred->uid);
  	ac.ac_gid = from_kgid_munged(file->f_cred->user_ns, orig_cred->gid);
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
479
  #if ACCT_VERSION == 1 || ACCT_VERSION == 2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  	/* backward-compatible 16 bit fields */
76aac0e9a   David Howells   CRED: Wrap task c...
481
482
  	ac.ac_uid16 = ac.ac_uid;
  	ac.ac_gid16 = ac.ac_gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
  #endif
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
484
  #if ACCT_VERSION == 3
067b722fa   Ying Xue   acct: eliminate c...
485
486
487
488
489
490
491
492
493
  	{
  		struct pid_namespace *ns = acct->ns;
  
  		ac.ac_pid = task_tgid_nr_ns(current, ns);
  		rcu_read_lock();
  		ac.ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent),
  					     ns);
  		rcu_read_unlock();
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
495
  	/*
5ae98f158   Jan Kara   fs: Fix hang with...
496
497
498
  	 * Get freeze protection. If the fs is frozen, just skip the write
  	 * as we could deadlock the system otherwise.
  	 */
ed44724b7   Al Viro   acct: switch to _...
499
500
501
502
503
504
  	if (file_start_write_trylock(file)) {
  		/* it's been opened O_APPEND, so position is irrelevant */
  		loff_t pos = 0;
  		__kernel_write(file, (char *)&ac, sizeof(acct_t), &pos);
  		file_end_write(file);
  	}
d8e180dcd   Michal Schmidt   bsdacct: switch c...
505
  out:
ed44724b7   Al Viro   acct: switch to _...
506
  	current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
d8e180dcd   Michal Schmidt   bsdacct: switch c...
507
  	revert_creds(orig_cred);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
508
  }
417ef5314   Randy Dunlap   [PATCH] kernel/ac...
509
  /**
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
510
   * acct_collect - collect accounting information into pacct_struct
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
511
512
   * @exitcode: task exit code
   * @group_dead: not 0, if this thread is the last one in the process.
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
513
   */
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
514
  void acct_collect(long exitcode, int group_dead)
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
515
516
  {
  	struct pacct_struct *pacct = &current->signal->pacct;
6fac4829c   Frederic Weisbecker   cputime: Use acce...
517
  	cputime_t utime, stime;
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
518
  	unsigned long vsize = 0;
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
519
  	if (group_dead && current->mm) {
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
520
  		struct vm_area_struct *vma;
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
521

0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
522
523
524
525
526
527
528
529
  		down_read(&current->mm->mmap_sem);
  		vma = current->mm->mmap;
  		while (vma) {
  			vsize += vma->vm_end - vma->vm_start;
  			vma = vma->vm_next;
  		}
  		up_read(&current->mm->mmap_sem);
  	}
77787bfb4   KaiGai Kohei   [PATCH] pacct: no...
530
  	spin_lock_irq(&current->sighand->siglock);
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
531
532
533
534
535
536
537
538
539
540
541
542
543
  	if (group_dead)
  		pacct->ac_mem = vsize / 1024;
  	if (thread_group_leader(current)) {
  		pacct->ac_exitcode = exitcode;
  		if (current->flags & PF_FORKNOEXEC)
  			pacct->ac_flag |= AFORK;
  	}
  	if (current->flags & PF_SUPERPRIV)
  		pacct->ac_flag |= ASU;
  	if (current->flags & PF_DUMPCORE)
  		pacct->ac_flag |= ACORE;
  	if (current->flags & PF_SIGNALED)
  		pacct->ac_flag |= AXSIG;
6fac4829c   Frederic Weisbecker   cputime: Use acce...
544
545
546
  	task_cputime(current, &utime, &stime);
  	pacct->ac_utime += utime;
  	pacct->ac_stime += stime;
77787bfb4   KaiGai Kohei   [PATCH] pacct: no...
547
548
549
  	pacct->ac_minflt += current->min_flt;
  	pacct->ac_majflt += current->maj_flt;
  	spin_unlock_irq(&current->sighand->siglock);
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
550
  }
e25ff11ff   Al Viro   split the slow pa...
551
  static void slow_acct_process(struct pid_namespace *ns)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
  {
e25ff11ff   Al Viro   split the slow pa...
553
  	for ( ; ns; ns = ns->parent) {
215752fce   Al Viro   acct: get rid of ...
554
  		struct bsd_acct_struct *acct = acct_get(ns);
b8f00e6be   Al Viro   acct: new lifetim...
555
556
557
  		if (acct) {
  			do_acct_process(acct);
  			mutex_unlock(&acct->lock);
9e251d020   Al Viro   kill pin_put()
558
  			acct_put(acct);
e25ff11ff   Al Viro   split the slow pa...
559
  		}
e25ff11ff   Al Viro   split the slow pa...
560
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
  }
7d1e13505   Pavel Emelyanov   bsdacct: account ...
562
563
  
  /**
e25ff11ff   Al Viro   split the slow pa...
564
   * acct_process
7d1e13505   Pavel Emelyanov   bsdacct: account ...
565
566
567
568
569
570
   *
   * handles process accounting for an exiting task
   */
  void acct_process(void)
  {
  	struct pid_namespace *ns;
0c18d7a5d   Pavel Emelyanov   bsdacct: fix and ...
571
572
573
574
575
  	/*
  	 * This loop is safe lockless, since current is still
  	 * alive and holds its namespace, which in turn holds
  	 * its parent.
  	 */
e25ff11ff   Al Viro   split the slow pa...
576
  	for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
b8f00e6be   Al Viro   acct: new lifetim...
577
  		if (ns->bacct)
e25ff11ff   Al Viro   split the slow pa...
578
579
580
581
  			break;
  	}
  	if (unlikely(ns))
  		slow_acct_process(ns);
7d1e13505   Pavel Emelyanov   bsdacct: account ...
582
  }