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.
   */
b8f00e6be   Al Viro   acct: new lifetim...
78
  static void do_acct_process(struct bsd_acct_struct *acct);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79

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

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

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

496ad9aa8   Al Viro   new helper: file_...
208
  	if (!S_ISREG(file_inode(file)->i_mode)) {
b8f00e6be   Al Viro   acct: new lifetim...
209
  		kfree(acct);
7b7b1ace2   Al Viro   [PATCH] saner han...
210
211
212
213
214
  		filp_close(file, NULL);
  		return -EACCES;
  	}
  
  	if (!file->f_op->write) {
b8f00e6be   Al Viro   acct: new lifetim...
215
  		kfree(acct);
7b7b1ace2   Al Viro   [PATCH] saner han...
216
217
218
  		filp_close(file, NULL);
  		return -EIO;
  	}
3064c3563   Al Viro   death to mnt_pinned
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  	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...
234

1629d0eb3   Al Viro   start carving bsd...
235
  	atomic_long_set(&acct->pin.count, 1);
efb170c22   Al Viro   take fs_pin stuff...
236
  	acct->pin.kill = acct_pin_kill;
b8f00e6be   Al Viro   acct: new lifetim...
237
238
239
240
  	acct->file = file;
  	acct->needcheck = jiffies;
  	acct->ns = ns;
  	mutex_init(&acct->lock);
efb170c22   Al Viro   take fs_pin stuff...
241
242
  	mutex_lock_nested(&acct->lock, 1);	/* nobody has seen it yet */
  	pin_insert(&acct->pin, mnt);
0b6b030fc   Pavel Emelyanov   bsdacct: switch f...
243

215752fce   Al Viro   acct: get rid of ...
244
  	old = acct_get(ns);
efb170c22   Al Viro   take fs_pin stuff...
245
  	if (old)
b8f00e6be   Al Viro   acct: new lifetim...
246
  		acct_kill(old, acct);
efb170c22   Al Viro   take fs_pin stuff...
247
  	else
0b6b030fc   Pavel Emelyanov   bsdacct: switch f...
248
  		ns->bacct = acct;
efb170c22   Al Viro   take fs_pin stuff...
249
  	mutex_unlock(&acct->lock);
3064c3563   Al Viro   death to mnt_pinned
250
251
  	mnt_drop_write(mnt);
  	mntput(mnt);
7b7b1ace2   Al Viro   [PATCH] saner han...
252
253
  	return 0;
  }
9df7fa16e   Al Viro   acct: serialize a...
254
  static DEFINE_MUTEX(acct_on_mutex);
417ef5314   Randy Dunlap   [PATCH] kernel/ac...
255
256
257
258
259
260
261
262
263
264
  /**
   * 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
265
   */
b290ebe2c   Heiko Carstens   [CVE-2009-0029] S...
266
  SYSCALL_DEFINE1(acct, const char __user *, name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  {
05b90496f   Eric Paris   security: remove ...
268
  	int error = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
273
  
  	if (!capable(CAP_SYS_PACCT))
  		return -EPERM;
  
  	if (name) {
91a27b2a7   Jeff Layton   vfs: define struc...
274
  		struct filename *tmp = getname(name);
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
275

7b7b1ace2   Al Viro   [PATCH] saner han...
276
  		if (IS_ERR(tmp))
46c0a8ca3   Paul McQuade   ipc, kernel: clea...
277
  			return PTR_ERR(tmp);
9df7fa16e   Al Viro   acct: serialize a...
278
  		mutex_lock(&acct_on_mutex);
669abf4e5   Jeff Layton   vfs: make path_op...
279
  		error = acct_on(tmp);
9df7fa16e   Al Viro   acct: serialize a...
280
  		mutex_unlock(&acct_on_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  		putname(tmp);
7b7b1ace2   Al Viro   [PATCH] saner han...
282
  	} else {
215752fce   Al Viro   acct: get rid of ...
283
  		acct_kill(acct_get(task_active_pid_ns(current)), NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  	}
05b90496f   Eric Paris   security: remove ...
285

7b7b1ace2   Al Viro   [PATCH] saner han...
286
287
  	return error;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288

0b6b030fc   Pavel Emelyanov   bsdacct: switch f...
289
290
  void acct_exit_ns(struct pid_namespace *ns)
  {
215752fce   Al Viro   acct: get rid of ...
291
  	acct_kill(acct_get(ns), NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  }
  
  /*
   *  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:...
318
319
  	 * If we need to round up, do it (and handle overflow correctly).
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
324
325
  	if (rnd && (++value > MAXFRACT)) {
  		value >>= EXPSIZE;
  		exp++;
  	}
  
  	/*
6ae965cd6   Daniel Walker   whitespace fixes:...
326
327
  	 * Clean it up and polish it off.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
  	exp <<= MANTSIZE;		/* Shift the exponent into place */
  	exp += value;			/* and add on the mantissa. */
  	return exp;
  }
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
332
  #if ACCT_VERSION == 1 || ACCT_VERSION == 2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
334
335
336
337
338
339
340
341
342
343
344
  /*
   * 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...
345
  #define MAXEXP2         ((1 << EXPSIZE2) - 1)    /* Maximum exponent. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
  
  static comp2_t encode_comp2_t(u64 value)
  {
6ae965cd6   Daniel Walker   whitespace fixes:...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  	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
373
374
  }
  #endif
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
375
  #if ACCT_VERSION == 3
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
379
380
381
382
  /*
   * 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...
383
384
385
  	if (value == 0)
  		return 0;
  	while ((s64)value > 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  		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...
400
   *  do_exit() or when switching to a different output file.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
   */
cdd37e230   Al Viro   separate namespac...
402
  static void fill_ac(acct_t *ac)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
  {
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
404
  	struct pacct_struct *pacct = &current->signal->pacct;
ccbf62d8a   Thomas Gleixner   sched: Make task-...
405
  	u64 elapsed, run_time;
24ec839c4   Peter Zijlstra   [PATCH] tty: ->si...
406
  	struct tty_struct *tty;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
410
411
  
  	/*
  	 * Fill the accounting struct with the needed info as recorded
  	 * by the different kernel functions.
  	 */
cdd37e230   Al Viro   separate namespac...
412
  	memset(ac, 0, sizeof(acct_t));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413

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

cdd37e230   Al Viro   separate namespac...
433
434
  		ac->ac_etime_hi = etime >> 16;
  		ac->ac_etime_lo = (u16) etime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
437
  	}
  #endif
  	do_div(elapsed, AHZ);
cdd37e230   Al Viro   separate namespac...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  	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...
458
  static void do_acct_process(struct bsd_acct_struct *acct)
cdd37e230   Al Viro   separate namespac...
459
460
461
462
  {
  	acct_t ac;
  	unsigned long flim;
  	const struct cred *orig_cred;
b8f00e6be   Al Viro   acct: new lifetim...
463
  	struct file *file = acct->file;
cdd37e230   Al Viro   separate namespac...
464
465
466
467
468
469
470
471
472
473
474
475
476
  
  	/*
  	 * 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...
477
  	if (!check_free_space(acct))
cdd37e230   Al Viro   separate namespac...
478
479
480
  		goto out;
  
  	fill_ac(&ac);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
  	/* we really need to bite the bullet and change layout */
f8f3d4de2   Eric W. Biederman   userns: Convert b...
482
483
  	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...
484
  #if ACCT_VERSION == 1 || ACCT_VERSION == 2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
485
  	/* backward-compatible 16 bit fields */
76aac0e9a   David Howells   CRED: Wrap task c...
486
487
  	ac.ac_uid16 = ac.ac_uid;
  	ac.ac_gid16 = ac.ac_gid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488
  #endif
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
489
  #if ACCT_VERSION == 3
067b722fa   Ying Xue   acct: eliminate c...
490
491
492
493
494
495
496
497
498
  	{
  		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
499
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  	/*
5ae98f158   Jan Kara   fs: Fix hang with...
501
502
503
  	 * 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 _...
504
505
506
507
508
509
  	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...
510
  out:
ed44724b7   Al Viro   acct: switch to _...
511
  	current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
d8e180dcd   Michal Schmidt   bsdacct: switch c...
512
  	revert_creds(orig_cred);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  }
417ef5314   Randy Dunlap   [PATCH] kernel/ac...
514
  /**
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
515
   * acct_collect - collect accounting information into pacct_struct
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
516
517
   * @exitcode: task exit code
   * @group_dead: not 0, if this thread is the last one in the process.
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
518
   */
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
519
  void acct_collect(long exitcode, int group_dead)
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
520
521
  {
  	struct pacct_struct *pacct = &current->signal->pacct;
6fac4829c   Frederic Weisbecker   cputime: Use acce...
522
  	cputime_t utime, stime;
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
523
  	unsigned long vsize = 0;
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
524
  	if (group_dead && current->mm) {
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
525
  		struct vm_area_struct *vma;
2577d92eb   Ionut Alexa   kernel/acct.c: fi...
526

0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
527
528
529
530
531
532
533
534
  		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...
535
  	spin_lock_irq(&current->sighand->siglock);
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
536
537
538
539
540
541
542
543
544
545
546
547
548
  	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...
549
550
551
  	task_cputime(current, &utime, &stime);
  	pacct->ac_utime += utime;
  	pacct->ac_stime += stime;
77787bfb4   KaiGai Kohei   [PATCH] pacct: no...
552
553
554
  	pacct->ac_minflt += current->min_flt;
  	pacct->ac_majflt += current->maj_flt;
  	spin_unlock_irq(&current->sighand->siglock);
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
555
  }
e25ff11ff   Al Viro   split the slow pa...
556
  static void slow_acct_process(struct pid_namespace *ns)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
  {
e25ff11ff   Al Viro   split the slow pa...
558
  	for ( ; ns; ns = ns->parent) {
215752fce   Al Viro   acct: get rid of ...
559
  		struct bsd_acct_struct *acct = acct_get(ns);
b8f00e6be   Al Viro   acct: new lifetim...
560
561
562
  		if (acct) {
  			do_acct_process(acct);
  			mutex_unlock(&acct->lock);
1629d0eb3   Al Viro   start carving bsd...
563
  			pin_put(&acct->pin);
e25ff11ff   Al Viro   split the slow pa...
564
  		}
e25ff11ff   Al Viro   split the slow pa...
565
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
  }
7d1e13505   Pavel Emelyanov   bsdacct: account ...
567
568
  
  /**
e25ff11ff   Al Viro   split the slow pa...
569
   * acct_process
7d1e13505   Pavel Emelyanov   bsdacct: account ...
570
571
572
573
574
575
   *
   * handles process accounting for an exiting task
   */
  void acct_process(void)
  {
  	struct pid_namespace *ns;
0c18d7a5d   Pavel Emelyanov   bsdacct: fix and ...
576
577
578
579
580
  	/*
  	 * 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...
581
  	for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
b8f00e6be   Al Viro   acct: new lifetim...
582
  		if (ns->bacct)
e25ff11ff   Al Viro   split the slow pa...
583
584
585
586
  			break;
  	}
  	if (unlikely(ns))
  		slow_acct_process(ns);
7d1e13505   Pavel Emelyanov   bsdacct: account ...
587
  }