Blame view

kernel/acct.c 15.5 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  /*
   *  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.
   *
7b7b8a2c9   Randy Dunlap   kernel/: fix repe...
28
   *  Fixed a nasty interaction with sys_umount(). If the accounting
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
32
33
34
35
36
37
38
39
40
41
42
   *  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
c1e8d7c6a   Michel Lespinasse   mmap locking API:...
43
   * ->mmap_lock to walk the vma list of current->mm. Nasty, since it leaks
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
   * a struct file opened for write. Fixed. 2/6/2000, AV.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
  #include <linux/mm.h>
  #include <linux/slab.h>
  #include <linux/acct.h>
c59ede7b7   Randy.Dunlap   [PATCH] move capa...
49
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
54
55
56
  #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...
57
  #include <linux/mount.h>
7153e4027   Paul McQuade   ipc, kernel: use ...
58
  #include <linux/uaccess.h>
32ef5517c   Ingo Molnar   sched/headers: Pr...
59
  #include <linux/sched/cputime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
  #include <asm/div64.h>
  #include <linux/blkdev.h> /* sector_div */
5f7b703fe   Pavel Emelyanov   bsd_acct: using t...
62
  #include <linux/pid_namespace.h>
efb170c22   Al Viro   take fs_pin stuff...
63
  #include <linux/fs_pin.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  
  /*
   * 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
80

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

4d9570158   Oleg Nesterov   kernel/acct.c: fi...
101
  	if (time_is_after_jiffies(acct->needcheck))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
  
  	/* May block */
54a4d58a6   Al Viro   acct: simplify ch...
105
  	if (vfs_statfs(&acct->file->f_path, &sbuf))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107

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

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

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

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

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

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

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

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

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

d8ed45c5d   Michel Lespinasse   mmap locking API:...
526
  		mmap_read_lock(current->mm);
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
527
528
529
530
531
  		vma = current->mm->mmap;
  		while (vma) {
  			vsize += vma->vm_end - vma->vm_start;
  			vma = vma->vm_next;
  		}
d8ed45c5d   Michel Lespinasse   mmap locking API:...
532
  		mmap_read_unlock(current->mm);
0e4648141   KaiGai Kohei   [PATCH] pacct: ad...
533
  	}
77787bfb4   KaiGai Kohei   [PATCH] pacct: no...
534
  	spin_lock_irq(&current->sighand->siglock);
f6ec29a42   KaiGai Kohei   [PATCH] pacct: av...
535
536
537
538
539
540
541
542
543
544
545
546
547
  	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;
d4bc42af7   Frederic Weisbecker   acct: Convert obs...
548
549
  
  	task_cputime(current, &utime, &stime);
6fac4829c   Frederic Weisbecker   cputime: Use acce...
550
551
  	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);
9e251d020   Al Viro   kill pin_put()
563
  			acct_put(acct);
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
  
  /**
b7621ebf8   Randy Dunlap   kernel: acct.c: f...
569
   * acct_process - handles process accounting for an exiting task
7d1e13505   Pavel Emelyanov   bsdacct: account ...
570
571
572
573
   */
  void acct_process(void)
  {
  	struct pid_namespace *ns;
0c18d7a5d   Pavel Emelyanov   bsdacct: fix and ...
574
575
576
577
578
  	/*
  	 * 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...
579
  	for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
b8f00e6be   Al Viro   acct: new lifetim...
580
  		if (ns->bacct)
e25ff11ff   Al Viro   split the slow pa...
581
582
583
584
  			break;
  	}
  	if (unlikely(ns))
  		slow_acct_process(ns);
7d1e13505   Pavel Emelyanov   bsdacct: account ...
585
  }