]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/fcntl.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | */ | |
7 | ||
8 | #include <linux/syscalls.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/mm.h> | |
29930025 | 11 | #include <linux/sched/task.h> |
1da177e4 LT |
12 | #include <linux/fs.h> |
13 | #include <linux/file.h> | |
9f3acc31 | 14 | #include <linux/fdtable.h> |
16f7e0fe | 15 | #include <linux/capability.h> |
1da177e4 | 16 | #include <linux/dnotify.h> |
1da177e4 LT |
17 | #include <linux/slab.h> |
18 | #include <linux/module.h> | |
35f3d14d | 19 | #include <linux/pipe_fs_i.h> |
1da177e4 LT |
20 | #include <linux/security.h> |
21 | #include <linux/ptrace.h> | |
7ed20e1a | 22 | #include <linux/signal.h> |
ab2af1f5 | 23 | #include <linux/rcupdate.h> |
b488893a | 24 | #include <linux/pid_namespace.h> |
1d151c33 | 25 | #include <linux/user_namespace.h> |
5d752600 | 26 | #include <linux/memfd.h> |
80f0cce6 | 27 | #include <linux/compat.h> |
1da177e4 | 28 | |
cfe39442 | 29 | #include <linux/poll.h> |
1da177e4 | 30 | #include <asm/siginfo.h> |
7c0f6ba6 | 31 | #include <linux/uaccess.h> |
1da177e4 | 32 | |
76398425 | 33 | #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME) |
1da177e4 | 34 | |
e4a3f096 | 35 | int setfl(int fd, struct file *filp, unsigned long arg) |
1da177e4 | 36 | { |
496ad9aa | 37 | struct inode * inode = file_inode(filp); |
1da177e4 LT |
38 | int error = 0; |
39 | ||
7d95c8f2 | 40 | /* |
41 | * O_APPEND cannot be cleared if the file is marked as append-only | |
42 | * and the file is open for write. | |
43 | */ | |
44 | if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode)) | |
1da177e4 LT |
45 | return -EPERM; |
46 | ||
47 | /* O_NOATIME can only be set by the owner or superuser */ | |
48 | if ((arg & O_NOATIME) && !(filp->f_flags & O_NOATIME)) | |
2e149670 | 49 | if (!inode_owner_or_capable(inode)) |
1da177e4 LT |
50 | return -EPERM; |
51 | ||
52 | /* required for strict SunOS emulation */ | |
53 | if (O_NONBLOCK != O_NDELAY) | |
54 | if (arg & O_NDELAY) | |
55 | arg |= O_NONBLOCK; | |
56 | ||
0dbf5f20 | 57 | /* Pipe packetized mode is controlled by O_DIRECT flag */ |
45063097 | 58 | if (!S_ISFIFO(inode->i_mode) && (arg & O_DIRECT)) { |
1da177e4 LT |
59 | if (!filp->f_mapping || !filp->f_mapping->a_ops || |
60 | !filp->f_mapping->a_ops->direct_IO) | |
61 | return -EINVAL; | |
62 | } | |
63 | ||
72c2d531 | 64 | if (filp->f_op->check_flags) |
1da177e4 | 65 | error = filp->f_op->check_flags(arg); |
a3a49a17 SF |
66 | if (!error && filp->f_op->setfl) |
67 | error = filp->f_op->setfl(filp, arg); | |
1da177e4 LT |
68 | if (error) |
69 | return error; | |
70 | ||
218d11a8 | 71 | /* |
76398425 | 72 | * ->fasync() is responsible for setting the FASYNC bit. |
218d11a8 | 73 | */ |
72c2d531 | 74 | if (((arg ^ filp->f_flags) & FASYNC) && filp->f_op->fasync) { |
76398425 JC |
75 | error = filp->f_op->fasync(fd, filp, (arg & FASYNC) != 0); |
76 | if (error < 0) | |
77 | goto out; | |
60aa4924 JC |
78 | if (error > 0) |
79 | error = 0; | |
1da177e4 | 80 | } |
db1dd4d3 | 81 | spin_lock(&filp->f_lock); |
1da177e4 | 82 | filp->f_flags = (arg & SETFL_MASK) | (filp->f_flags & ~SETFL_MASK); |
db1dd4d3 | 83 | spin_unlock(&filp->f_lock); |
76398425 | 84 | |
1da177e4 | 85 | out: |
1da177e4 LT |
86 | return error; |
87 | } | |
a3a49a17 | 88 | EXPORT_SYMBOL_GPL(setfl); |
1da177e4 | 89 | |
609d7fa9 | 90 | static void f_modown(struct file *filp, struct pid *pid, enum pid_type type, |
2f38d70f | 91 | int force) |
1da177e4 | 92 | { |
80e1e823 | 93 | write_lock_irq(&filp->f_owner.lock); |
1da177e4 | 94 | if (force || !filp->f_owner.pid) { |
609d7fa9 EB |
95 | put_pid(filp->f_owner.pid); |
96 | filp->f_owner.pid = get_pid(pid); | |
97 | filp->f_owner.pid_type = type; | |
2f38d70f ON |
98 | |
99 | if (pid) { | |
100 | const struct cred *cred = current_cred(); | |
101 | filp->f_owner.uid = cred->uid; | |
102 | filp->f_owner.euid = cred->euid; | |
103 | } | |
1da177e4 | 104 | } |
80e1e823 | 105 | write_unlock_irq(&filp->f_owner.lock); |
1da177e4 LT |
106 | } |
107 | ||
e0b93edd | 108 | void __f_setown(struct file *filp, struct pid *pid, enum pid_type type, |
609d7fa9 | 109 | int force) |
1da177e4 | 110 | { |
e0b93edd | 111 | security_file_set_fowner(filp); |
2f38d70f | 112 | f_modown(filp, pid, type, force); |
1da177e4 | 113 | } |
609d7fa9 | 114 | EXPORT_SYMBOL(__f_setown); |
1da177e4 | 115 | |
393cc3f5 | 116 | int f_setown(struct file *filp, unsigned long arg, int force) |
609d7fa9 EB |
117 | { |
118 | enum pid_type type; | |
f7312735 JL |
119 | struct pid *pid = NULL; |
120 | int who = arg, ret = 0; | |
121 | ||
01919134 | 122 | type = PIDTYPE_TGID; |
609d7fa9 | 123 | if (who < 0) { |
fc3dc674 JS |
124 | /* avoid overflow below */ |
125 | if (who == INT_MIN) | |
126 | return -EINVAL; | |
127 | ||
609d7fa9 EB |
128 | type = PIDTYPE_PGID; |
129 | who = -who; | |
130 | } | |
f7312735 | 131 | |
609d7fa9 | 132 | rcu_read_lock(); |
f7312735 JL |
133 | if (who) { |
134 | pid = find_vpid(who); | |
135 | if (!pid) | |
136 | ret = -ESRCH; | |
137 | } | |
138 | ||
139 | if (!ret) | |
140 | __f_setown(filp, pid, type, force); | |
609d7fa9 | 141 | rcu_read_unlock(); |
393cc3f5 | 142 | |
f7312735 | 143 | return ret; |
609d7fa9 | 144 | } |
1da177e4 LT |
145 | EXPORT_SYMBOL(f_setown); |
146 | ||
147 | void f_delown(struct file *filp) | |
148 | { | |
01919134 | 149 | f_modown(filp, NULL, PIDTYPE_TGID, 1); |
609d7fa9 EB |
150 | } |
151 | ||
152 | pid_t f_getown(struct file *filp) | |
153 | { | |
154 | pid_t pid; | |
43fa1adb | 155 | read_lock(&filp->f_owner.lock); |
6c5f3e7b | 156 | pid = pid_vnr(filp->f_owner.pid); |
609d7fa9 EB |
157 | if (filp->f_owner.pid_type == PIDTYPE_PGID) |
158 | pid = -pid; | |
43fa1adb | 159 | read_unlock(&filp->f_owner.lock); |
609d7fa9 | 160 | return pid; |
1da177e4 LT |
161 | } |
162 | ||
ba0a6c9f PZ |
163 | static int f_setown_ex(struct file *filp, unsigned long arg) |
164 | { | |
63784dd0 | 165 | struct f_owner_ex __user *owner_p = (void __user *)arg; |
ba0a6c9f PZ |
166 | struct f_owner_ex owner; |
167 | struct pid *pid; | |
168 | int type; | |
169 | int ret; | |
170 | ||
171 | ret = copy_from_user(&owner, owner_p, sizeof(owner)); | |
172 | if (ret) | |
5b54470d | 173 | return -EFAULT; |
ba0a6c9f PZ |
174 | |
175 | switch (owner.type) { | |
176 | case F_OWNER_TID: | |
01919134 | 177 | type = PIDTYPE_PID; |
ba0a6c9f PZ |
178 | break; |
179 | ||
180 | case F_OWNER_PID: | |
01919134 | 181 | type = PIDTYPE_TGID; |
ba0a6c9f PZ |
182 | break; |
183 | ||
978b4053 | 184 | case F_OWNER_PGRP: |
ba0a6c9f PZ |
185 | type = PIDTYPE_PGID; |
186 | break; | |
187 | ||
188 | default: | |
189 | return -EINVAL; | |
190 | } | |
191 | ||
192 | rcu_read_lock(); | |
193 | pid = find_vpid(owner.pid); | |
194 | if (owner.pid && !pid) | |
195 | ret = -ESRCH; | |
196 | else | |
e0b93edd | 197 | __f_setown(filp, pid, type, 1); |
ba0a6c9f PZ |
198 | rcu_read_unlock(); |
199 | ||
200 | return ret; | |
201 | } | |
202 | ||
203 | static int f_getown_ex(struct file *filp, unsigned long arg) | |
204 | { | |
63784dd0 | 205 | struct f_owner_ex __user *owner_p = (void __user *)arg; |
ba0a6c9f PZ |
206 | struct f_owner_ex owner; |
207 | int ret = 0; | |
208 | ||
209 | read_lock(&filp->f_owner.lock); | |
210 | owner.pid = pid_vnr(filp->f_owner.pid); | |
211 | switch (filp->f_owner.pid_type) { | |
01919134 | 212 | case PIDTYPE_PID: |
ba0a6c9f PZ |
213 | owner.type = F_OWNER_TID; |
214 | break; | |
215 | ||
01919134 | 216 | case PIDTYPE_TGID: |
ba0a6c9f PZ |
217 | owner.type = F_OWNER_PID; |
218 | break; | |
219 | ||
220 | case PIDTYPE_PGID: | |
978b4053 | 221 | owner.type = F_OWNER_PGRP; |
ba0a6c9f PZ |
222 | break; |
223 | ||
224 | default: | |
225 | WARN_ON(1); | |
226 | ret = -EINVAL; | |
227 | break; | |
228 | } | |
229 | read_unlock(&filp->f_owner.lock); | |
230 | ||
5b54470d | 231 | if (!ret) { |
ba0a6c9f | 232 | ret = copy_to_user(owner_p, &owner, sizeof(owner)); |
5b54470d DC |
233 | if (ret) |
234 | ret = -EFAULT; | |
235 | } | |
ba0a6c9f PZ |
236 | return ret; |
237 | } | |
238 | ||
1d151c33 CG |
239 | #ifdef CONFIG_CHECKPOINT_RESTORE |
240 | static int f_getowner_uids(struct file *filp, unsigned long arg) | |
241 | { | |
242 | struct user_namespace *user_ns = current_user_ns(); | |
63784dd0 | 243 | uid_t __user *dst = (void __user *)arg; |
1d151c33 CG |
244 | uid_t src[2]; |
245 | int err; | |
246 | ||
247 | read_lock(&filp->f_owner.lock); | |
248 | src[0] = from_kuid(user_ns, filp->f_owner.uid); | |
249 | src[1] = from_kuid(user_ns, filp->f_owner.euid); | |
250 | read_unlock(&filp->f_owner.lock); | |
251 | ||
252 | err = put_user(src[0], &dst[0]); | |
253 | err |= put_user(src[1], &dst[1]); | |
254 | ||
255 | return err; | |
256 | } | |
257 | #else | |
258 | static int f_getowner_uids(struct file *filp, unsigned long arg) | |
259 | { | |
260 | return -EINVAL; | |
261 | } | |
262 | #endif | |
263 | ||
c75b1d94 JA |
264 | static bool rw_hint_valid(enum rw_hint hint) |
265 | { | |
266 | switch (hint) { | |
267 | case RWF_WRITE_LIFE_NOT_SET: | |
268 | case RWH_WRITE_LIFE_NONE: | |
269 | case RWH_WRITE_LIFE_SHORT: | |
270 | case RWH_WRITE_LIFE_MEDIUM: | |
271 | case RWH_WRITE_LIFE_LONG: | |
272 | case RWH_WRITE_LIFE_EXTREME: | |
273 | return true; | |
274 | default: | |
275 | return false; | |
276 | } | |
277 | } | |
278 | ||
279 | static long fcntl_rw_hint(struct file *file, unsigned int cmd, | |
280 | unsigned long arg) | |
281 | { | |
282 | struct inode *inode = file_inode(file); | |
283 | u64 *argp = (u64 __user *)arg; | |
284 | enum rw_hint hint; | |
5657cb07 | 285 | u64 h; |
c75b1d94 JA |
286 | |
287 | switch (cmd) { | |
288 | case F_GET_FILE_RW_HINT: | |
5657cb07 JA |
289 | h = file_write_hint(file); |
290 | if (copy_to_user(argp, &h, sizeof(*argp))) | |
c75b1d94 JA |
291 | return -EFAULT; |
292 | return 0; | |
293 | case F_SET_FILE_RW_HINT: | |
5657cb07 | 294 | if (copy_from_user(&h, argp, sizeof(h))) |
c75b1d94 | 295 | return -EFAULT; |
5657cb07 | 296 | hint = (enum rw_hint) h; |
c75b1d94 JA |
297 | if (!rw_hint_valid(hint)) |
298 | return -EINVAL; | |
299 | ||
300 | spin_lock(&file->f_lock); | |
301 | file->f_write_hint = hint; | |
302 | spin_unlock(&file->f_lock); | |
303 | return 0; | |
304 | case F_GET_RW_HINT: | |
5657cb07 JA |
305 | h = inode->i_write_hint; |
306 | if (copy_to_user(argp, &h, sizeof(*argp))) | |
c75b1d94 JA |
307 | return -EFAULT; |
308 | return 0; | |
309 | case F_SET_RW_HINT: | |
5657cb07 | 310 | if (copy_from_user(&h, argp, sizeof(h))) |
c75b1d94 | 311 | return -EFAULT; |
5657cb07 | 312 | hint = (enum rw_hint) h; |
c75b1d94 JA |
313 | if (!rw_hint_valid(hint)) |
314 | return -EINVAL; | |
315 | ||
316 | inode_lock(inode); | |
317 | inode->i_write_hint = hint; | |
318 | inode_unlock(inode); | |
319 | return 0; | |
320 | default: | |
321 | return -EINVAL; | |
322 | } | |
323 | } | |
324 | ||
1da177e4 LT |
325 | static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, |
326 | struct file *filp) | |
327 | { | |
a75d30c7 CH |
328 | void __user *argp = (void __user *)arg; |
329 | struct flock flock; | |
1da177e4 LT |
330 | long err = -EINVAL; |
331 | ||
332 | switch (cmd) { | |
333 | case F_DUPFD: | |
fe17f22d AV |
334 | err = f_dupfd(arg, filp, 0); |
335 | break; | |
22d2b35b | 336 | case F_DUPFD_CLOEXEC: |
12197718 | 337 | err = f_dupfd(arg, filp, O_CLOEXEC); |
1da177e4 LT |
338 | break; |
339 | case F_GETFD: | |
340 | err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; | |
341 | break; | |
342 | case F_SETFD: | |
343 | err = 0; | |
344 | set_close_on_exec(fd, arg & FD_CLOEXEC); | |
345 | break; | |
346 | case F_GETFL: | |
347 | err = filp->f_flags; | |
348 | break; | |
349 | case F_SETFL: | |
350 | err = setfl(fd, filp, arg); | |
351 | break; | |
5d50ffd7 JL |
352 | #if BITS_PER_LONG != 32 |
353 | /* 32-bit arches must use fcntl64() */ | |
0d3f7a2d | 354 | case F_OFD_GETLK: |
5d50ffd7 | 355 | #endif |
1da177e4 | 356 | case F_GETLK: |
a75d30c7 CH |
357 | if (copy_from_user(&flock, argp, sizeof(flock))) |
358 | return -EFAULT; | |
359 | err = fcntl_getlk(filp, cmd, &flock); | |
360 | if (!err && copy_to_user(argp, &flock, sizeof(flock))) | |
361 | return -EFAULT; | |
1da177e4 | 362 | break; |
5d50ffd7 JL |
363 | #if BITS_PER_LONG != 32 |
364 | /* 32-bit arches must use fcntl64() */ | |
0d3f7a2d JL |
365 | case F_OFD_SETLK: |
366 | case F_OFD_SETLKW: | |
5d50ffd7 JL |
367 | #endif |
368 | /* Fallthrough */ | |
1da177e4 LT |
369 | case F_SETLK: |
370 | case F_SETLKW: | |
a75d30c7 CH |
371 | if (copy_from_user(&flock, argp, sizeof(flock))) |
372 | return -EFAULT; | |
373 | err = fcntl_setlk(fd, filp, cmd, &flock); | |
1da177e4 LT |
374 | break; |
375 | case F_GETOWN: | |
376 | /* | |
377 | * XXX If f_owner is a process group, the | |
378 | * negative return value will get converted | |
379 | * into an error. Oops. If we keep the | |
380 | * current syscall conventions, the only way | |
381 | * to fix this will be in libc. | |
382 | */ | |
609d7fa9 | 383 | err = f_getown(filp); |
1da177e4 LT |
384 | force_successful_syscall_return(); |
385 | break; | |
386 | case F_SETOWN: | |
393cc3f5 | 387 | err = f_setown(filp, arg, 1); |
1da177e4 | 388 | break; |
ba0a6c9f PZ |
389 | case F_GETOWN_EX: |
390 | err = f_getown_ex(filp, arg); | |
391 | break; | |
392 | case F_SETOWN_EX: | |
393 | err = f_setown_ex(filp, arg); | |
394 | break; | |
1d151c33 CG |
395 | case F_GETOWNER_UIDS: |
396 | err = f_getowner_uids(filp, arg); | |
397 | break; | |
1da177e4 LT |
398 | case F_GETSIG: |
399 | err = filp->f_owner.signum; | |
400 | break; | |
401 | case F_SETSIG: | |
402 | /* arg == 0 restores default behaviour. */ | |
7ed20e1a | 403 | if (!valid_signal(arg)) { |
1da177e4 LT |
404 | break; |
405 | } | |
406 | err = 0; | |
407 | filp->f_owner.signum = arg; | |
408 | break; | |
409 | case F_GETLEASE: | |
410 | err = fcntl_getlease(filp); | |
411 | break; | |
412 | case F_SETLEASE: | |
413 | err = fcntl_setlease(fd, filp, arg); | |
414 | break; | |
415 | case F_NOTIFY: | |
416 | err = fcntl_dirnotify(fd, filp, arg); | |
417 | break; | |
35f3d14d JA |
418 | case F_SETPIPE_SZ: |
419 | case F_GETPIPE_SZ: | |
420 | err = pipe_fcntl(filp, cmd, arg); | |
421 | break; | |
40e041a2 DH |
422 | case F_ADD_SEALS: |
423 | case F_GET_SEALS: | |
5aadc431 | 424 | err = memfd_fcntl(filp, cmd, arg); |
40e041a2 | 425 | break; |
c75b1d94 JA |
426 | case F_GET_RW_HINT: |
427 | case F_SET_RW_HINT: | |
428 | case F_GET_FILE_RW_HINT: | |
429 | case F_SET_FILE_RW_HINT: | |
430 | err = fcntl_rw_hint(filp, cmd, arg); | |
431 | break; | |
1da177e4 LT |
432 | default: |
433 | break; | |
434 | } | |
435 | return err; | |
436 | } | |
437 | ||
1abf0c71 AV |
438 | static int check_fcntl_cmd(unsigned cmd) |
439 | { | |
440 | switch (cmd) { | |
441 | case F_DUPFD: | |
442 | case F_DUPFD_CLOEXEC: | |
443 | case F_GETFD: | |
444 | case F_SETFD: | |
445 | case F_GETFL: | |
446 | return 1; | |
447 | } | |
448 | return 0; | |
449 | } | |
450 | ||
a26eab24 | 451 | SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) |
1da177e4 | 452 | { |
2903ff01 | 453 | struct fd f = fdget_raw(fd); |
1da177e4 LT |
454 | long err = -EBADF; |
455 | ||
2903ff01 | 456 | if (!f.file) |
1da177e4 LT |
457 | goto out; |
458 | ||
2903ff01 | 459 | if (unlikely(f.file->f_mode & FMODE_PATH)) { |
545ec2c7 AV |
460 | if (!check_fcntl_cmd(cmd)) |
461 | goto out1; | |
1abf0c71 AV |
462 | } |
463 | ||
2903ff01 | 464 | err = security_file_fcntl(f.file, cmd, arg); |
545ec2c7 | 465 | if (!err) |
2903ff01 | 466 | err = do_fcntl(fd, cmd, arg, f.file); |
1da177e4 | 467 | |
545ec2c7 | 468 | out1: |
2903ff01 | 469 | fdput(f); |
1da177e4 LT |
470 | out: |
471 | return err; | |
472 | } | |
473 | ||
474 | #if BITS_PER_LONG == 32 | |
a26eab24 HC |
475 | SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, |
476 | unsigned long, arg) | |
1da177e4 | 477 | { |
a75d30c7 | 478 | void __user *argp = (void __user *)arg; |
2903ff01 | 479 | struct fd f = fdget_raw(fd); |
a75d30c7 | 480 | struct flock64 flock; |
545ec2c7 | 481 | long err = -EBADF; |
1da177e4 | 482 | |
2903ff01 | 483 | if (!f.file) |
1da177e4 LT |
484 | goto out; |
485 | ||
2903ff01 | 486 | if (unlikely(f.file->f_mode & FMODE_PATH)) { |
545ec2c7 AV |
487 | if (!check_fcntl_cmd(cmd)) |
488 | goto out1; | |
1abf0c71 AV |
489 | } |
490 | ||
2903ff01 | 491 | err = security_file_fcntl(f.file, cmd, arg); |
545ec2c7 AV |
492 | if (err) |
493 | goto out1; | |
1da177e4 LT |
494 | |
495 | switch (cmd) { | |
5d50ffd7 | 496 | case F_GETLK64: |
0d3f7a2d | 497 | case F_OFD_GETLK: |
a75d30c7 CH |
498 | err = -EFAULT; |
499 | if (copy_from_user(&flock, argp, sizeof(flock))) | |
500 | break; | |
501 | err = fcntl_getlk64(f.file, cmd, &flock); | |
502 | if (!err && copy_to_user(argp, &flock, sizeof(flock))) | |
503 | err = -EFAULT; | |
5d50ffd7 JL |
504 | break; |
505 | case F_SETLK64: | |
506 | case F_SETLKW64: | |
0d3f7a2d JL |
507 | case F_OFD_SETLK: |
508 | case F_OFD_SETLKW: | |
a75d30c7 CH |
509 | err = -EFAULT; |
510 | if (copy_from_user(&flock, argp, sizeof(flock))) | |
511 | break; | |
512 | err = fcntl_setlk64(fd, f.file, cmd, &flock); | |
5d50ffd7 JL |
513 | break; |
514 | default: | |
515 | err = do_fcntl(fd, cmd, arg, f.file); | |
516 | break; | |
1da177e4 | 517 | } |
545ec2c7 | 518 | out1: |
2903ff01 | 519 | fdput(f); |
1da177e4 LT |
520 | out: |
521 | return err; | |
522 | } | |
523 | #endif | |
524 | ||
80f0cce6 | 525 | #ifdef CONFIG_COMPAT |
8c6657cb | 526 | /* careful - don't use anywhere else */ |
b59eea55 LT |
527 | #define copy_flock_fields(dst, src) \ |
528 | (dst)->l_type = (src)->l_type; \ | |
529 | (dst)->l_whence = (src)->l_whence; \ | |
530 | (dst)->l_start = (src)->l_start; \ | |
531 | (dst)->l_len = (src)->l_len; \ | |
532 | (dst)->l_pid = (src)->l_pid; | |
533 | ||
534 | static int get_compat_flock(struct flock *kfl, const struct compat_flock __user *ufl) | |
80f0cce6 | 535 | { |
8c6657cb AV |
536 | struct compat_flock fl; |
537 | ||
538 | if (copy_from_user(&fl, ufl, sizeof(struct compat_flock))) | |
80f0cce6 | 539 | return -EFAULT; |
b59eea55 | 540 | copy_flock_fields(kfl, &fl); |
80f0cce6 AV |
541 | return 0; |
542 | } | |
543 | ||
b59eea55 | 544 | static int get_compat_flock64(struct flock *kfl, const struct compat_flock64 __user *ufl) |
80f0cce6 | 545 | { |
8c6657cb AV |
546 | struct compat_flock64 fl; |
547 | ||
548 | if (copy_from_user(&fl, ufl, sizeof(struct compat_flock64))) | |
80f0cce6 | 549 | return -EFAULT; |
b59eea55 | 550 | copy_flock_fields(kfl, &fl); |
80f0cce6 AV |
551 | return 0; |
552 | } | |
553 | ||
b59eea55 | 554 | static int put_compat_flock(const struct flock *kfl, struct compat_flock __user *ufl) |
80f0cce6 | 555 | { |
8c6657cb AV |
556 | struct compat_flock fl; |
557 | ||
558 | memset(&fl, 0, sizeof(struct compat_flock)); | |
b59eea55 | 559 | copy_flock_fields(&fl, kfl); |
8c6657cb | 560 | if (copy_to_user(ufl, &fl, sizeof(struct compat_flock))) |
80f0cce6 AV |
561 | return -EFAULT; |
562 | return 0; | |
563 | } | |
80f0cce6 | 564 | |
b59eea55 | 565 | static int put_compat_flock64(const struct flock *kfl, struct compat_flock64 __user *ufl) |
80f0cce6 | 566 | { |
8c6657cb AV |
567 | struct compat_flock64 fl; |
568 | ||
4d2dc2cc JL |
569 | BUILD_BUG_ON(sizeof(kfl->l_start) > sizeof(ufl->l_start)); |
570 | BUILD_BUG_ON(sizeof(kfl->l_len) > sizeof(ufl->l_len)); | |
571 | ||
8c6657cb | 572 | memset(&fl, 0, sizeof(struct compat_flock64)); |
b59eea55 | 573 | copy_flock_fields(&fl, kfl); |
8c6657cb | 574 | if (copy_to_user(ufl, &fl, sizeof(struct compat_flock64))) |
80f0cce6 AV |
575 | return -EFAULT; |
576 | return 0; | |
577 | } | |
8c6657cb | 578 | #undef copy_flock_fields |
80f0cce6 AV |
579 | |
580 | static unsigned int | |
581 | convert_fcntl_cmd(unsigned int cmd) | |
582 | { | |
583 | switch (cmd) { | |
584 | case F_GETLK64: | |
585 | return F_GETLK; | |
586 | case F_SETLK64: | |
587 | return F_SETLK; | |
588 | case F_SETLKW64: | |
589 | return F_SETLKW; | |
590 | } | |
591 | ||
592 | return cmd; | |
593 | } | |
594 | ||
94073ad7 CH |
595 | /* |
596 | * GETLK was successful and we need to return the data, but it needs to fit in | |
597 | * the compat structure. | |
598 | * l_start shouldn't be too big, unless the original start + end is greater than | |
599 | * COMPAT_OFF_T_MAX, in which case the app was asking for trouble, so we return | |
600 | * -EOVERFLOW in that case. l_len could be too big, in which case we just | |
601 | * truncate it, and only allow the app to see that part of the conflicting lock | |
602 | * that might make sense to it anyway | |
603 | */ | |
604 | static int fixup_compat_flock(struct flock *flock) | |
605 | { | |
606 | if (flock->l_start > COMPAT_OFF_T_MAX) | |
607 | return -EOVERFLOW; | |
608 | if (flock->l_len > COMPAT_OFF_T_MAX) | |
609 | flock->l_len = COMPAT_OFF_T_MAX; | |
610 | return 0; | |
611 | } | |
612 | ||
e02af2ff DB |
613 | static long do_compat_fcntl64(unsigned int fd, unsigned int cmd, |
614 | compat_ulong_t arg) | |
80f0cce6 | 615 | { |
94073ad7 CH |
616 | struct fd f = fdget_raw(fd); |
617 | struct flock flock; | |
618 | long err = -EBADF; | |
619 | ||
620 | if (!f.file) | |
621 | return err; | |
622 | ||
623 | if (unlikely(f.file->f_mode & FMODE_PATH)) { | |
624 | if (!check_fcntl_cmd(cmd)) | |
625 | goto out_put; | |
626 | } | |
627 | ||
628 | err = security_file_fcntl(f.file, cmd, arg); | |
629 | if (err) | |
630 | goto out_put; | |
80f0cce6 AV |
631 | |
632 | switch (cmd) { | |
633 | case F_GETLK: | |
94073ad7 CH |
634 | err = get_compat_flock(&flock, compat_ptr(arg)); |
635 | if (err) | |
636 | break; | |
637 | err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock); | |
638 | if (err) | |
639 | break; | |
640 | err = fixup_compat_flock(&flock); | |
9280a601 JL |
641 | if (!err) |
642 | err = put_compat_flock(&flock, compat_ptr(arg)); | |
94073ad7 CH |
643 | break; |
644 | case F_GETLK64: | |
645 | case F_OFD_GETLK: | |
646 | err = get_compat_flock64(&flock, compat_ptr(arg)); | |
647 | if (err) | |
648 | break; | |
649 | err = fcntl_getlk(f.file, convert_fcntl_cmd(cmd), &flock); | |
4d2dc2cc JL |
650 | if (!err) |
651 | err = put_compat_flock64(&flock, compat_ptr(arg)); | |
94073ad7 | 652 | break; |
80f0cce6 AV |
653 | case F_SETLK: |
654 | case F_SETLKW: | |
94073ad7 CH |
655 | err = get_compat_flock(&flock, compat_ptr(arg)); |
656 | if (err) | |
80f0cce6 | 657 | break; |
94073ad7 | 658 | err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock); |
80f0cce6 | 659 | break; |
80f0cce6 AV |
660 | case F_SETLK64: |
661 | case F_SETLKW64: | |
80f0cce6 AV |
662 | case F_OFD_SETLK: |
663 | case F_OFD_SETLKW: | |
94073ad7 CH |
664 | err = get_compat_flock64(&flock, compat_ptr(arg)); |
665 | if (err) | |
80f0cce6 | 666 | break; |
94073ad7 | 667 | err = fcntl_setlk(fd, f.file, convert_fcntl_cmd(cmd), &flock); |
80f0cce6 | 668 | break; |
80f0cce6 | 669 | default: |
94073ad7 | 670 | err = do_fcntl(fd, cmd, arg, f.file); |
80f0cce6 AV |
671 | break; |
672 | } | |
94073ad7 CH |
673 | out_put: |
674 | fdput(f); | |
675 | return err; | |
80f0cce6 AV |
676 | } |
677 | ||
e02af2ff DB |
678 | COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, |
679 | compat_ulong_t, arg) | |
680 | { | |
681 | return do_compat_fcntl64(fd, cmd, arg); | |
682 | } | |
683 | ||
80f0cce6 AV |
684 | COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, |
685 | compat_ulong_t, arg) | |
686 | { | |
687 | switch (cmd) { | |
688 | case F_GETLK64: | |
689 | case F_SETLK64: | |
690 | case F_SETLKW64: | |
691 | case F_OFD_GETLK: | |
692 | case F_OFD_SETLK: | |
693 | case F_OFD_SETLKW: | |
694 | return -EINVAL; | |
695 | } | |
e02af2ff | 696 | return do_compat_fcntl64(fd, cmd, arg); |
80f0cce6 AV |
697 | } |
698 | #endif | |
699 | ||
1da177e4 LT |
700 | /* Table to convert sigio signal codes into poll band bitmaps */ |
701 | ||
5dc533c6 | 702 | static const __poll_t band_table[NSIGPOLL] = { |
a9a08845 LT |
703 | EPOLLIN | EPOLLRDNORM, /* POLL_IN */ |
704 | EPOLLOUT | EPOLLWRNORM | EPOLLWRBAND, /* POLL_OUT */ | |
705 | EPOLLIN | EPOLLRDNORM | EPOLLMSG, /* POLL_MSG */ | |
706 | EPOLLERR, /* POLL_ERR */ | |
707 | EPOLLPRI | EPOLLRDBAND, /* POLL_PRI */ | |
708 | EPOLLHUP | EPOLLERR /* POLL_HUP */ | |
1da177e4 LT |
709 | }; |
710 | ||
711 | static inline int sigio_perm(struct task_struct *p, | |
712 | struct fown_struct *fown, int sig) | |
713 | { | |
c69e8d9c DH |
714 | const struct cred *cred; |
715 | int ret; | |
716 | ||
717 | rcu_read_lock(); | |
718 | cred = __task_cred(p); | |
8e96e3b7 EB |
719 | ret = ((uid_eq(fown->euid, GLOBAL_ROOT_UID) || |
720 | uid_eq(fown->euid, cred->suid) || uid_eq(fown->euid, cred->uid) || | |
721 | uid_eq(fown->uid, cred->suid) || uid_eq(fown->uid, cred->uid)) && | |
c69e8d9c DH |
722 | !security_file_send_sigiotask(p, fown, sig)); |
723 | rcu_read_unlock(); | |
724 | return ret; | |
1da177e4 LT |
725 | } |
726 | ||
727 | static void send_sigio_to_task(struct task_struct *p, | |
8eeee4e2 | 728 | struct fown_struct *fown, |
9c2db007 | 729 | int fd, int reason, enum pid_type type) |
1da177e4 | 730 | { |
8eeee4e2 ON |
731 | /* |
732 | * F_SETSIG can change ->signum lockless in parallel, make | |
733 | * sure we read it once and use the same value throughout. | |
734 | */ | |
6aa7de05 | 735 | int signum = READ_ONCE(fown->signum); |
8eeee4e2 ON |
736 | |
737 | if (!sigio_perm(p, fown, signum)) | |
1da177e4 LT |
738 | return; |
739 | ||
8eeee4e2 | 740 | switch (signum) { |
ae7795bc | 741 | kernel_siginfo_t si; |
1da177e4 LT |
742 | default: |
743 | /* Queue a rt signal with the appropriate fd as its | |
744 | value. We use SI_SIGIO as the source, not | |
745 | SI_KERNEL, since kernel signals always get | |
746 | delivered even if we can't queue. Failure to | |
747 | queue in this case _should_ be reported; we fall | |
748 | back to SIGIO in that case. --sct */ | |
faf1f22b | 749 | clear_siginfo(&si); |
8eeee4e2 | 750 | si.si_signo = signum; |
1da177e4 LT |
751 | si.si_errno = 0; |
752 | si.si_code = reason; | |
d08477aa EB |
753 | /* |
754 | * Posix definies POLL_IN and friends to be signal | |
755 | * specific si_codes for SIG_POLL. Linux extended | |
756 | * these si_codes to other signals in a way that is | |
757 | * ambiguous if other signals also have signal | |
758 | * specific si_codes. In that case use SI_SIGIO instead | |
759 | * to remove the ambiguity. | |
760 | */ | |
54640d23 | 761 | if ((signum != SIGPOLL) && sig_specific_sicodes(signum)) |
d08477aa EB |
762 | si.si_code = SI_SIGIO; |
763 | ||
1da177e4 LT |
764 | /* Make sure we are called with one of the POLL_* |
765 | reasons, otherwise we could leak kernel stack into | |
766 | userspace. */ | |
d08477aa | 767 | BUG_ON((reason < POLL_IN) || ((reason - POLL_IN) >= NSIGPOLL)); |
1da177e4 LT |
768 | if (reason - POLL_IN >= NSIGPOLL) |
769 | si.si_band = ~0L; | |
770 | else | |
c71d227f | 771 | si.si_band = mangle_poll(band_table[reason - POLL_IN]); |
1da177e4 | 772 | si.si_fd = fd; |
40b3b025 | 773 | if (!do_send_sig_info(signum, &si, p, type)) |
1da177e4 | 774 | break; |
0a4c9265 | 775 | /* fall-through - fall back on the old plain SIGIO signal */ |
1da177e4 | 776 | case 0: |
40b3b025 | 777 | do_send_sig_info(SIGIO, SEND_SIG_PRIV, p, type); |
1da177e4 LT |
778 | } |
779 | } | |
780 | ||
781 | void send_sigio(struct fown_struct *fown, int fd, int band) | |
782 | { | |
783 | struct task_struct *p; | |
609d7fa9 | 784 | enum pid_type type; |
68d705dc | 785 | unsigned long flags; |
609d7fa9 | 786 | struct pid *pid; |
1da177e4 | 787 | |
68d705dc | 788 | read_lock_irqsave(&fown->lock, flags); |
ba0a6c9f | 789 | |
609d7fa9 | 790 | type = fown->pid_type; |
1da177e4 LT |
791 | pid = fown->pid; |
792 | if (!pid) | |
793 | goto out_unlock_fown; | |
01919134 EB |
794 | |
795 | if (type <= PIDTYPE_TGID) { | |
796 | rcu_read_lock(); | |
797 | p = pid_task(pid, PIDTYPE_PID); | |
84fe4cc0 EB |
798 | if (p) |
799 | send_sigio_to_task(p, fown, fd, band, type); | |
01919134 EB |
800 | rcu_read_unlock(); |
801 | } else { | |
802 | read_lock(&tasklist_lock); | |
803 | do_each_pid_task(pid, type, p) { | |
9c2db007 | 804 | send_sigio_to_task(p, fown, fd, band, type); |
01919134 EB |
805 | } while_each_pid_task(pid, type, p); |
806 | read_unlock(&tasklist_lock); | |
807 | } | |
1da177e4 | 808 | out_unlock_fown: |
68d705dc | 809 | read_unlock_irqrestore(&fown->lock, flags); |
1da177e4 LT |
810 | } |
811 | ||
812 | static void send_sigurg_to_task(struct task_struct *p, | |
9c2db007 | 813 | struct fown_struct *fown, enum pid_type type) |
1da177e4 LT |
814 | { |
815 | if (sigio_perm(p, fown, SIGURG)) | |
40b3b025 | 816 | do_send_sig_info(SIGURG, SEND_SIG_PRIV, p, type); |
1da177e4 LT |
817 | } |
818 | ||
819 | int send_sigurg(struct fown_struct *fown) | |
820 | { | |
821 | struct task_struct *p; | |
609d7fa9 EB |
822 | enum pid_type type; |
823 | struct pid *pid; | |
68d705dc | 824 | unsigned long flags; |
609d7fa9 | 825 | int ret = 0; |
1da177e4 | 826 | |
68d705dc | 827 | read_lock_irqsave(&fown->lock, flags); |
ba0a6c9f | 828 | |
609d7fa9 | 829 | type = fown->pid_type; |
1da177e4 LT |
830 | pid = fown->pid; |
831 | if (!pid) | |
832 | goto out_unlock_fown; | |
833 | ||
834 | ret = 1; | |
01919134 EB |
835 | |
836 | if (type <= PIDTYPE_TGID) { | |
837 | rcu_read_lock(); | |
838 | p = pid_task(pid, PIDTYPE_PID); | |
84fe4cc0 EB |
839 | if (p) |
840 | send_sigurg_to_task(p, fown, type); | |
01919134 EB |
841 | rcu_read_unlock(); |
842 | } else { | |
843 | read_lock(&tasklist_lock); | |
844 | do_each_pid_task(pid, type, p) { | |
9c2db007 | 845 | send_sigurg_to_task(p, fown, type); |
01919134 EB |
846 | } while_each_pid_task(pid, type, p); |
847 | read_unlock(&tasklist_lock); | |
848 | } | |
1da177e4 | 849 | out_unlock_fown: |
68d705dc | 850 | read_unlock_irqrestore(&fown->lock, flags); |
1da177e4 LT |
851 | return ret; |
852 | } | |
853 | ||
989a2979 | 854 | static DEFINE_SPINLOCK(fasync_lock); |
e18b890b | 855 | static struct kmem_cache *fasync_cache __read_mostly; |
1da177e4 | 856 | |
989a2979 ED |
857 | static void fasync_free_rcu(struct rcu_head *head) |
858 | { | |
859 | kmem_cache_free(fasync_cache, | |
860 | container_of(head, struct fasync_struct, fa_rcu)); | |
861 | } | |
862 | ||
1da177e4 | 863 | /* |
53281b6d LT |
864 | * Remove a fasync entry. If successfully removed, return |
865 | * positive and clear the FASYNC flag. If no entry exists, | |
866 | * do nothing and return 0. | |
867 | * | |
868 | * NOTE! It is very important that the FASYNC flag always | |
869 | * match the state "is the filp on a fasync list". | |
870 | * | |
1da177e4 | 871 | */ |
f7347ce4 | 872 | int fasync_remove_entry(struct file *filp, struct fasync_struct **fapp) |
1da177e4 LT |
873 | { |
874 | struct fasync_struct *fa, **fp; | |
1da177e4 LT |
875 | int result = 0; |
876 | ||
53281b6d | 877 | spin_lock(&filp->f_lock); |
989a2979 | 878 | spin_lock(&fasync_lock); |
53281b6d LT |
879 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
880 | if (fa->fa_file != filp) | |
881 | continue; | |
989a2979 | 882 | |
7a107c0f | 883 | write_lock_irq(&fa->fa_lock); |
989a2979 | 884 | fa->fa_file = NULL; |
7a107c0f | 885 | write_unlock_irq(&fa->fa_lock); |
989a2979 | 886 | |
53281b6d | 887 | *fp = fa->fa_next; |
989a2979 | 888 | call_rcu(&fa->fa_rcu, fasync_free_rcu); |
53281b6d LT |
889 | filp->f_flags &= ~FASYNC; |
890 | result = 1; | |
891 | break; | |
1da177e4 | 892 | } |
989a2979 | 893 | spin_unlock(&fasync_lock); |
53281b6d LT |
894 | spin_unlock(&filp->f_lock); |
895 | return result; | |
896 | } | |
897 | ||
f7347ce4 LT |
898 | struct fasync_struct *fasync_alloc(void) |
899 | { | |
900 | return kmem_cache_alloc(fasync_cache, GFP_KERNEL); | |
901 | } | |
902 | ||
53281b6d | 903 | /* |
f7347ce4 LT |
904 | * NOTE! This can be used only for unused fasync entries: |
905 | * entries that actually got inserted on the fasync list | |
906 | * need to be released by rcu - see fasync_remove_entry. | |
53281b6d | 907 | */ |
f7347ce4 | 908 | void fasync_free(struct fasync_struct *new) |
53281b6d | 909 | { |
f7347ce4 LT |
910 | kmem_cache_free(fasync_cache, new); |
911 | } | |
53281b6d | 912 | |
f7347ce4 LT |
913 | /* |
914 | * Insert a new entry into the fasync list. Return the pointer to the | |
915 | * old one if we didn't use the new one. | |
55f335a8 LT |
916 | * |
917 | * NOTE! It is very important that the FASYNC flag always | |
918 | * match the state "is the filp on a fasync list". | |
f7347ce4 LT |
919 | */ |
920 | struct fasync_struct *fasync_insert_entry(int fd, struct file *filp, struct fasync_struct **fapp, struct fasync_struct *new) | |
921 | { | |
922 | struct fasync_struct *fa, **fp; | |
4a6a4499 | 923 | |
4a6a4499 | 924 | spin_lock(&filp->f_lock); |
989a2979 | 925 | spin_lock(&fasync_lock); |
1da177e4 | 926 | for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) { |
53281b6d LT |
927 | if (fa->fa_file != filp) |
928 | continue; | |
989a2979 | 929 | |
7a107c0f | 930 | write_lock_irq(&fa->fa_lock); |
53281b6d | 931 | fa->fa_fd = fd; |
7a107c0f | 932 | write_unlock_irq(&fa->fa_lock); |
53281b6d | 933 | goto out; |
1da177e4 LT |
934 | } |
935 | ||
7a107c0f | 936 | rwlock_init(&new->fa_lock); |
53281b6d LT |
937 | new->magic = FASYNC_MAGIC; |
938 | new->fa_file = filp; | |
939 | new->fa_fd = fd; | |
940 | new->fa_next = *fapp; | |
989a2979 | 941 | rcu_assign_pointer(*fapp, new); |
53281b6d LT |
942 | filp->f_flags |= FASYNC; |
943 | ||
1da177e4 | 944 | out: |
989a2979 | 945 | spin_unlock(&fasync_lock); |
4a6a4499 | 946 | spin_unlock(&filp->f_lock); |
f7347ce4 LT |
947 | return fa; |
948 | } | |
949 | ||
950 | /* | |
951 | * Add a fasync entry. Return negative on error, positive if | |
952 | * added, and zero if did nothing but change an existing one. | |
f7347ce4 LT |
953 | */ |
954 | static int fasync_add_entry(int fd, struct file *filp, struct fasync_struct **fapp) | |
955 | { | |
956 | struct fasync_struct *new; | |
957 | ||
958 | new = fasync_alloc(); | |
959 | if (!new) | |
960 | return -ENOMEM; | |
961 | ||
962 | /* | |
963 | * fasync_insert_entry() returns the old (update) entry if | |
964 | * it existed. | |
965 | * | |
966 | * So free the (unused) new entry and return 0 to let the | |
967 | * caller know that we didn't add any new fasync entries. | |
968 | */ | |
969 | if (fasync_insert_entry(fd, filp, fapp, new)) { | |
970 | fasync_free(new); | |
971 | return 0; | |
972 | } | |
973 | ||
974 | return 1; | |
1da177e4 LT |
975 | } |
976 | ||
53281b6d LT |
977 | /* |
978 | * fasync_helper() is used by almost all character device drivers | |
979 | * to set up the fasync queue, and for regular files by the file | |
980 | * lease code. It returns negative on error, 0 if it did no changes | |
981 | * and positive if it added/deleted the entry. | |
982 | */ | |
983 | int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp) | |
984 | { | |
985 | if (!on) | |
986 | return fasync_remove_entry(filp, fapp); | |
987 | return fasync_add_entry(fd, filp, fapp); | |
988 | } | |
989 | ||
1da177e4 LT |
990 | EXPORT_SYMBOL(fasync_helper); |
991 | ||
989a2979 ED |
992 | /* |
993 | * rcu_read_lock() is held | |
994 | */ | |
995 | static void kill_fasync_rcu(struct fasync_struct *fa, int sig, int band) | |
1da177e4 LT |
996 | { |
997 | while (fa) { | |
989a2979 | 998 | struct fown_struct *fown; |
8bc3e4e2 | 999 | unsigned long flags; |
f4985dc7 | 1000 | |
1da177e4 LT |
1001 | if (fa->magic != FASYNC_MAGIC) { |
1002 | printk(KERN_ERR "kill_fasync: bad magic number in " | |
1003 | "fasync_struct!\n"); | |
1004 | return; | |
1005 | } | |
8bc3e4e2 | 1006 | read_lock_irqsave(&fa->fa_lock, flags); |
989a2979 ED |
1007 | if (fa->fa_file) { |
1008 | fown = &fa->fa_file->f_owner; | |
1009 | /* Don't send SIGURG to processes which have not set a | |
1010 | queued signum: SIGURG has its own default signalling | |
1011 | mechanism. */ | |
1012 | if (!(sig == SIGURG && fown->signum == 0)) | |
1013 | send_sigio(fown, fa->fa_fd, band); | |
1014 | } | |
8bc3e4e2 | 1015 | read_unlock_irqrestore(&fa->fa_lock, flags); |
989a2979 | 1016 | fa = rcu_dereference(fa->fa_next); |
1da177e4 LT |
1017 | } |
1018 | } | |
1019 | ||
1da177e4 LT |
1020 | void kill_fasync(struct fasync_struct **fp, int sig, int band) |
1021 | { | |
1022 | /* First a quick test without locking: usually | |
1023 | * the list is empty. | |
1024 | */ | |
1025 | if (*fp) { | |
989a2979 ED |
1026 | rcu_read_lock(); |
1027 | kill_fasync_rcu(rcu_dereference(*fp), sig, band); | |
1028 | rcu_read_unlock(); | |
1da177e4 LT |
1029 | } |
1030 | } | |
1031 | EXPORT_SYMBOL(kill_fasync); | |
1032 | ||
454eedb8 | 1033 | static int __init fcntl_init(void) |
1da177e4 | 1034 | { |
3ab04d5c JB |
1035 | /* |
1036 | * Please add new bits here to ensure allocation uniqueness. | |
1037 | * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY | |
1038 | * is defined as O_NONBLOCK on some platforms and not on others. | |
1039 | */ | |
80f18379 CH |
1040 | BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ != |
1041 | HWEIGHT32( | |
1042 | (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) | | |
1043 | __FMODE_EXEC | __FMODE_NONOTIFY)); | |
454eedb8 | 1044 | |
1da177e4 | 1045 | fasync_cache = kmem_cache_create("fasync_cache", |
20c2df83 | 1046 | sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL); |
1da177e4 LT |
1047 | return 0; |
1048 | } | |
1049 | ||
454eedb8 | 1050 | module_init(fcntl_init) |