.NET Core:允许其他用户写入命名管道

问题描述 投票:0回答:1

使用

NamedPipeServerStream
读取数据并使用
NamedPipeClientStream
写入数据,当两个进程由同一用户运行时一切正常,但如果我由不同的用户运行客户端进程,我会得到
Permission denied /tmp/CoreFxPipe_my_pipe
。管道“文件”确实没有其他用户的写入权限。

PipeOptions
有一个成员
CurrentUserOnly
,但我没有设置它(我什至尝试明确设置
None
)。如何允许其他用户写入管道?

linux .net-core named-pipes
1个回答
0
投票

/tmp
目录默认设置了粘性位,这意味着除非明确授予权限,否则其他用户无法写入。

不幸的是

NamedPipeServerStream
使用的文件不能放在其他地方,所以我们必须使用SetUnixFileMode自行设置适用的权限。

const string pipeName = "my_pipe";
NamedPipeServerStream pipe = new(pipeName, PipeDirection.In /* other args */);

if (Environment.OSVersion.Platform == PlatformID.Unix)
{
    System.IO.File.SetUnixFileMode(
        $"/tmp/CoreFxPipe_{pipeName}",
        System.IO.UnixFileMode.OtherWrite);
}

pipe.WaitForConnection();

如果您的客户也需要阅读,请添加

System.IO.UnixFileMode.OtherRead
模式。

© www.soinside.com 2019 - 2024. All rights reserved.