Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Linux-御风守护者
linux
提交
fe17f22d
L
linux
项目概览
Linux-御风守护者
/
linux
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
linux
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fe17f22d
编写于
8月 21, 2012
作者:
A
Al Viro
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
take purely descriptor-related stuff from fcntl.c to file.c
Signed-off-by:
N
Al Viro
<
viro@zeniv.linux.org.uk
>
上级
6a6d27de
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
137 addition
and
128 deletion
+137
-128
fs/fcntl.c
fs/fcntl.c
+3
-128
fs/file.c
fs/file.c
+132
-0
include/linux/file.h
include/linux/file.h
+2
-0
未找到文件。
fs/fcntl.c
浏览文件 @
fe17f22d
...
...
@@ -26,127 +26,6 @@
#include <asm/siginfo.h>
#include <asm/uaccess.h>
void
set_close_on_exec
(
unsigned
int
fd
,
int
flag
)
{
struct
files_struct
*
files
=
current
->
files
;
struct
fdtable
*
fdt
;
spin_lock
(
&
files
->
file_lock
);
fdt
=
files_fdtable
(
files
);
if
(
flag
)
__set_close_on_exec
(
fd
,
fdt
);
else
__clear_close_on_exec
(
fd
,
fdt
);
spin_unlock
(
&
files
->
file_lock
);
}
static
bool
get_close_on_exec
(
unsigned
int
fd
)
{
struct
files_struct
*
files
=
current
->
files
;
struct
fdtable
*
fdt
;
bool
res
;
rcu_read_lock
();
fdt
=
files_fdtable
(
files
);
res
=
close_on_exec
(
fd
,
fdt
);
rcu_read_unlock
();
return
res
;
}
SYSCALL_DEFINE3
(
dup3
,
unsigned
int
,
oldfd
,
unsigned
int
,
newfd
,
int
,
flags
)
{
int
err
=
-
EBADF
;
struct
file
*
file
,
*
tofree
;
struct
files_struct
*
files
=
current
->
files
;
struct
fdtable
*
fdt
;
if
((
flags
&
~
O_CLOEXEC
)
!=
0
)
return
-
EINVAL
;
if
(
unlikely
(
oldfd
==
newfd
))
return
-
EINVAL
;
if
(
newfd
>=
rlimit
(
RLIMIT_NOFILE
))
return
-
EMFILE
;
spin_lock
(
&
files
->
file_lock
);
err
=
expand_files
(
files
,
newfd
);
file
=
fcheck
(
oldfd
);
if
(
unlikely
(
!
file
))
goto
Ebadf
;
if
(
unlikely
(
err
<
0
))
{
if
(
err
==
-
EMFILE
)
goto
Ebadf
;
goto
out_unlock
;
}
/*
* We need to detect attempts to do dup2() over allocated but still
* not finished descriptor. NB: OpenBSD avoids that at the price of
* extra work in their equivalent of fget() - they insert struct
* file immediately after grabbing descriptor, mark it larval if
* more work (e.g. actual opening) is needed and make sure that
* fget() treats larval files as absent. Potentially interesting,
* but while extra work in fget() is trivial, locking implications
* and amount of surgery on open()-related paths in VFS are not.
* FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
* deadlocks in rather amusing ways, AFAICS. All of that is out of
* scope of POSIX or SUS, since neither considers shared descriptor
* tables and this condition does not arise without those.
*/
err
=
-
EBUSY
;
fdt
=
files_fdtable
(
files
);
tofree
=
fdt
->
fd
[
newfd
];
if
(
!
tofree
&&
fd_is_open
(
newfd
,
fdt
))
goto
out_unlock
;
get_file
(
file
);
rcu_assign_pointer
(
fdt
->
fd
[
newfd
],
file
);
__set_open_fd
(
newfd
,
fdt
);
if
(
flags
&
O_CLOEXEC
)
__set_close_on_exec
(
newfd
,
fdt
);
else
__clear_close_on_exec
(
newfd
,
fdt
);
spin_unlock
(
&
files
->
file_lock
);
if
(
tofree
)
filp_close
(
tofree
,
files
);
return
newfd
;
Ebadf:
err
=
-
EBADF
;
out_unlock:
spin_unlock
(
&
files
->
file_lock
);
return
err
;
}
SYSCALL_DEFINE2
(
dup2
,
unsigned
int
,
oldfd
,
unsigned
int
,
newfd
)
{
if
(
unlikely
(
newfd
==
oldfd
))
{
/* corner case */
struct
files_struct
*
files
=
current
->
files
;
int
retval
=
oldfd
;
rcu_read_lock
();
if
(
!
fcheck_files
(
files
,
oldfd
))
retval
=
-
EBADF
;
rcu_read_unlock
();
return
retval
;
}
return
sys_dup3
(
oldfd
,
newfd
,
0
);
}
SYSCALL_DEFINE1
(
dup
,
unsigned
int
,
fildes
)
{
int
ret
=
-
EBADF
;
struct
file
*
file
=
fget_raw
(
fildes
);
if
(
file
)
{
ret
=
get_unused_fd
();
if
(
ret
>=
0
)
fd_install
(
ret
,
file
);
else
fput
(
file
);
}
return
ret
;
}
#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
static
int
setfl
(
int
fd
,
struct
file
*
filp
,
unsigned
long
arg
)
...
...
@@ -376,14 +255,10 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
switch
(
cmd
)
{
case
F_DUPFD
:
case
F_DUPFD_CLOEXEC
:
if
(
arg
>=
rlimit
(
RLIMIT_NOFILE
))
err
=
f_dupfd
(
arg
,
filp
,
0
);
break
;
err
=
alloc_fd
(
arg
,
cmd
==
F_DUPFD_CLOEXEC
?
O_CLOEXEC
:
0
);
if
(
err
>=
0
)
{
get_file
(
filp
);
fd_install
(
err
,
filp
);
}
case
F_DUPFD_CLOEXEC
:
err
=
f_dupfd
(
arg
,
filp
,
FD_CLOEXEC
);
break
;
case
F_GETFD
:
err
=
get_close_on_exec
(
fd
)
?
FD_CLOEXEC
:
0
;
...
...
fs/file.c
浏览文件 @
fe17f22d
...
...
@@ -6,6 +6,7 @@
* Manage the dynamic fd arrays in the process files_struct.
*/
#include <linux/syscalls.h>
#include <linux/export.h>
#include <linux/fs.h>
#include <linux/mm.h>
...
...
@@ -794,3 +795,134 @@ struct file *fget_raw_light(unsigned int fd, int *fput_needed)
return
file
;
}
void
set_close_on_exec
(
unsigned
int
fd
,
int
flag
)
{
struct
files_struct
*
files
=
current
->
files
;
struct
fdtable
*
fdt
;
spin_lock
(
&
files
->
file_lock
);
fdt
=
files_fdtable
(
files
);
if
(
flag
)
__set_close_on_exec
(
fd
,
fdt
);
else
__clear_close_on_exec
(
fd
,
fdt
);
spin_unlock
(
&
files
->
file_lock
);
}
bool
get_close_on_exec
(
unsigned
int
fd
)
{
struct
files_struct
*
files
=
current
->
files
;
struct
fdtable
*
fdt
;
bool
res
;
rcu_read_lock
();
fdt
=
files_fdtable
(
files
);
res
=
close_on_exec
(
fd
,
fdt
);
rcu_read_unlock
();
return
res
;
}
SYSCALL_DEFINE3
(
dup3
,
unsigned
int
,
oldfd
,
unsigned
int
,
newfd
,
int
,
flags
)
{
int
err
=
-
EBADF
;
struct
file
*
file
,
*
tofree
;
struct
files_struct
*
files
=
current
->
files
;
struct
fdtable
*
fdt
;
if
((
flags
&
~
O_CLOEXEC
)
!=
0
)
return
-
EINVAL
;
if
(
newfd
>=
rlimit
(
RLIMIT_NOFILE
))
return
-
EMFILE
;
spin_lock
(
&
files
->
file_lock
);
err
=
expand_files
(
files
,
newfd
);
file
=
fcheck
(
oldfd
);
if
(
unlikely
(
!
file
))
goto
Ebadf
;
if
(
unlikely
(
err
<
0
))
{
if
(
err
==
-
EMFILE
)
goto
Ebadf
;
goto
out_unlock
;
}
/*
* We need to detect attempts to do dup2() over allocated but still
* not finished descriptor. NB: OpenBSD avoids that at the price of
* extra work in their equivalent of fget() - they insert struct
* file immediately after grabbing descriptor, mark it larval if
* more work (e.g. actual opening) is needed and make sure that
* fget() treats larval files as absent. Potentially interesting,
* but while extra work in fget() is trivial, locking implications
* and amount of surgery on open()-related paths in VFS are not.
* FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
* deadlocks in rather amusing ways, AFAICS. All of that is out of
* scope of POSIX or SUS, since neither considers shared descriptor
* tables and this condition does not arise without those.
*/
err
=
-
EBUSY
;
fdt
=
files_fdtable
(
files
);
tofree
=
fdt
->
fd
[
newfd
];
if
(
!
tofree
&&
fd_is_open
(
newfd
,
fdt
))
goto
out_unlock
;
get_file
(
file
);
rcu_assign_pointer
(
fdt
->
fd
[
newfd
],
file
);
__set_open_fd
(
newfd
,
fdt
);
if
(
flags
&
O_CLOEXEC
)
__set_close_on_exec
(
newfd
,
fdt
);
else
__clear_close_on_exec
(
newfd
,
fdt
);
spin_unlock
(
&
files
->
file_lock
);
if
(
tofree
)
filp_close
(
tofree
,
files
);
return
newfd
;
Ebadf:
err
=
-
EBADF
;
out_unlock:
spin_unlock
(
&
files
->
file_lock
);
return
err
;
}
SYSCALL_DEFINE2
(
dup2
,
unsigned
int
,
oldfd
,
unsigned
int
,
newfd
)
{
if
(
unlikely
(
newfd
==
oldfd
))
{
/* corner case */
struct
files_struct
*
files
=
current
->
files
;
int
retval
=
oldfd
;
rcu_read_lock
();
if
(
!
fcheck_files
(
files
,
oldfd
))
retval
=
-
EBADF
;
rcu_read_unlock
();
return
retval
;
}
return
sys_dup3
(
oldfd
,
newfd
,
0
);
}
SYSCALL_DEFINE1
(
dup
,
unsigned
int
,
fildes
)
{
int
ret
=
-
EBADF
;
struct
file
*
file
=
fget_raw
(
fildes
);
if
(
file
)
{
ret
=
get_unused_fd
();
if
(
ret
>=
0
)
fd_install
(
ret
,
file
);
else
fput
(
file
);
}
return
ret
;
}
int
f_dupfd
(
unsigned
int
from
,
struct
file
*
file
,
unsigned
flags
)
{
int
err
;
if
(
from
>=
rlimit
(
RLIMIT_NOFILE
))
return
-
EINVAL
;
err
=
alloc_fd
(
from
,
flags
);
if
(
err
>=
0
)
{
get_file
(
file
);
fd_install
(
err
,
file
);
}
return
err
;
}
include/linux/file.h
浏览文件 @
fe17f22d
...
...
@@ -30,7 +30,9 @@ extern struct file *fget(unsigned int fd);
extern
struct
file
*
fget_light
(
unsigned
int
fd
,
int
*
fput_needed
);
extern
struct
file
*
fget_raw
(
unsigned
int
fd
);
extern
struct
file
*
fget_raw_light
(
unsigned
int
fd
,
int
*
fput_needed
);
extern
int
f_dupfd
(
unsigned
int
from
,
struct
file
*
file
,
unsigned
flags
);
extern
void
set_close_on_exec
(
unsigned
int
fd
,
int
flag
);
extern
bool
get_close_on_exec
(
unsigned
int
fd
);
extern
void
put_filp
(
struct
file
*
);
extern
int
alloc_fd
(
unsigned
start
,
unsigned
flags
);
extern
int
get_unused_fd_flags
(
unsigned
flags
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录