linux.net-corenamed-pipes

.NET Core on Linux: Allow writing to a named pipe by another user


Using NamedPipeServerStream to read and NamedPipeClientStream to write data, everything works fine when both processes are run by the same user, but if I run the client process by a different user I get Permission denied /tmp/CoreFxPipe_my_pipe. The pipe "file" indeed doesn't have write permissions for other users.

PipeOptions has a member CurrentUserOnly, but I didn't set it (I even tried setting None explicitly). How can I allow other users to write to the pipe?


Solution

  • The /tmp directory has the sticky bit set by default, meaning other users can't write unless permission is explicitly granted.

    Unfortunately the file used by NamedPipeServerStream can't be placed elsewhere, so we have to set the applicable permissions ourselves with 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();
    

    If your client also needs to read, include the System.IO.UnixFileMode.OtherRead mode.