2023-03-30 20:07:07 +00:00
|
|
|
using Microsoft.Win32.SafeHandles;
|
2023-03-04 13:43:08 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2023-02-25 15:07:23 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2023-03-04 13:43:08 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2023-02-25 15:07:23 +00:00
|
|
|
using System.Runtime.Versioning;
|
|
|
|
using System.Threading;
|
2023-03-30 20:07:07 +00:00
|
|
|
using System.Threading.Tasks;
|
2023-02-25 15:07:23 +00:00
|
|
|
|
|
|
|
namespace Ryujinx.Common.SystemInterop
|
|
|
|
{
|
|
|
|
public partial class StdErrAdapter : IDisposable
|
|
|
|
{
|
|
|
|
private bool _disposable = false;
|
2023-03-30 20:07:07 +00:00
|
|
|
private Stream _pipeReader;
|
|
|
|
private Stream _pipeWriter;
|
|
|
|
private CancellationTokenSource _cancellationTokenSource;
|
|
|
|
private Task _worker;
|
2023-02-25 15:07:23 +00:00
|
|
|
|
|
|
|
public StdErrAdapter()
|
|
|
|
{
|
|
|
|
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
|
|
|
{
|
|
|
|
RegisterPosix();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[SupportedOSPlatform("linux")]
|
|
|
|
[SupportedOSPlatform("macos")]
|
|
|
|
private void RegisterPosix()
|
|
|
|
{
|
|
|
|
const int stdErrFileno = 2;
|
|
|
|
|
|
|
|
(int readFd, int writeFd) = MakePipe();
|
|
|
|
dup2(writeFd, stdErrFileno);
|
|
|
|
|
2023-03-30 20:07:07 +00:00
|
|
|
_pipeReader = CreateFileDescriptorStream(readFd);
|
|
|
|
_pipeWriter = CreateFileDescriptorStream(writeFd);
|
2023-02-25 15:07:23 +00:00
|
|
|
|
2023-03-30 20:07:07 +00:00
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
_worker = Task.Run(async () => await EventWorkerAsync(_cancellationTokenSource.Token), _cancellationTokenSource.Token);
|
2023-02-25 15:07:23 +00:00
|
|
|
_disposable = true;
|
|
|
|
}
|
2023-03-30 20:07:07 +00:00
|
|
|
|
2023-02-25 15:07:23 +00:00
|
|
|
[SupportedOSPlatform("linux")]
|
|
|
|
[SupportedOSPlatform("macos")]
|
2023-03-30 20:07:07 +00:00
|
|
|
private async Task EventWorkerAsync(CancellationToken cancellationToken)
|
2023-02-25 15:07:23 +00:00
|
|
|
{
|
2023-03-30 20:07:07 +00:00
|
|
|
using TextReader reader = new StreamReader(_pipeReader, leaveOpen: true);
|
2023-02-25 15:07:23 +00:00
|
|
|
string line;
|
2023-03-30 20:07:07 +00:00
|
|
|
while (cancellationToken.IsCancellationRequested == false && (line = await reader.ReadLineAsync(cancellationToken)) != null)
|
2023-02-25 15:07:23 +00:00
|
|
|
{
|
|
|
|
Logger.Error?.PrintRawMsg(line);
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 20:07:07 +00:00
|
|
|
|
2023-02-25 15:07:23 +00:00
|
|
|
private void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (_disposable)
|
|
|
|
{
|
2023-03-30 20:07:07 +00:00
|
|
|
_disposable = false;
|
|
|
|
|
2023-02-25 15:07:23 +00:00
|
|
|
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
|
|
|
|
{
|
2023-03-30 20:07:07 +00:00
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
_worker.Wait(0);
|
2023-02-25 15:07:23 +00:00
|
|
|
_pipeReader?.Close();
|
|
|
|
_pipeWriter?.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
[LibraryImport("libc", SetLastError = true)]
|
|
|
|
private static partial int dup2(int fd, int fd2);
|
|
|
|
|
|
|
|
[LibraryImport("libc", SetLastError = true)]
|
2023-03-30 20:07:07 +00:00
|
|
|
private static partial int pipe(Span<int> pipefd);
|
2023-02-25 15:07:23 +00:00
|
|
|
|
2023-03-30 20:07:07 +00:00
|
|
|
private static (int, int) MakePipe()
|
2023-02-25 15:07:23 +00:00
|
|
|
{
|
2023-03-30 20:07:07 +00:00
|
|
|
Span<int> pipefd = stackalloc int[2];
|
2023-02-25 15:07:23 +00:00
|
|
|
|
|
|
|
if (pipe(pipefd) == 0)
|
|
|
|
{
|
|
|
|
return (pipefd[0], pipefd[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new();
|
|
|
|
}
|
|
|
|
}
|
2023-03-30 20:07:07 +00:00
|
|
|
|
|
|
|
[SupportedOSPlatform("linux")]
|
|
|
|
[SupportedOSPlatform("macos")]
|
|
|
|
private static Stream CreateFileDescriptorStream(int fd)
|
|
|
|
{
|
|
|
|
return new FileStream(
|
|
|
|
new SafeFileHandle((IntPtr)fd, ownsHandle: true),
|
|
|
|
FileAccess.ReadWrite
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-25 15:07:23 +00:00
|
|
|
}
|
|
|
|
}
|