使用fcntl从stdout创建新文件描述符在文件中失败

时间:2018-03-30 12:49:19

标签: c perl6 openbsd raku nativecall

我有一个简单的测试文件,如下所示:

use v6.c;
use NativeCall;

sub fcntl(int32, int32 --> int32) is native { * }
sub close(int32 --> int32) is native { * }

my $fd := fcntl($*OUT.native-descriptor, 0);
say $fd;
close($fd);

返回的文件描述符是-1,这不是我想要的。但是当我在REPL中运行相同的代码时,我得到了我正在寻找的东西:

> use NativeCall
Nil
> sub fcntl(int32, int32 --> int32) is native { * }
sub fcntl (int32 $, int32 $ --> int32) { #`(Sub+{Callable[int32]}+{NativeCall::Native[Sub+{Callable[int32]},Str]}|17126514527616) ... }
> sub close(int32 --> int32) is native { * }
sub close (int32 $ --> int32) { #`(Sub+{Callable[int32]}+{NativeCall::Native[Sub+{Callable[int32]},Str]}|17126514527904) ... }
> my $fd := fcntl($*OUT.native-descriptor, 0)
15
> say $fd
15
> close($fd)
0

为什么没有fcntl在文件中创建新的文件描述符,就像在REPL中一样?

编辑:我正在运行OpenBSD 6.2,Rakudo 2018.02

1 个答案:

答案 0 :(得分:8)

当使用带有fcntl的F_DUPFD flag(为0)时,OpenBSD要求将原始文件描述符的状态标志传递给新文件描述符。所以这将有效:

use v6.c;
use NativeCall;

constant F_DUPFD = 0;
constant F_GETFD = 1;

sub fcntl(int32, int32, int32 --> int32) is native { * }
sub close(int32 --> int32) is native { * }

sub MAIN() {
    my $out-fd := $*OUT.native-descriptor;
    my $flags  := fcntl($out-fd, F_GETFD, 0);
    my $fd     := fcntl($out-fd, F_DUPFD, $flags);
    say $fd; # 15
    close($fd);
}