Skip to content

Commit

Permalink
Fix ldd bug on MUSL.
Browse files Browse the repository at this point in the history
  • Loading branch information
ww898 committed Jun 14, 2024
1 parent 548c24e commit 71b3f69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
36 changes: 22 additions & 14 deletions JetBrains.HabitatDetector/src/Impl/Linux/LinuxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal static JetArchitecture ConvertToArchitecture(ELFCLASS eiClass, ELFDATA

internal record struct ElfInfo(JetLinuxLibC LinuxLibC, JetArchitecture ProcessArchitecture);

private static string? RunLddVersion()
private static string? RunLddVersion(bool shouldFail)
{
#if NETSTANDARD1_1 || NETSTANDARD1_2 || NETSTANDARD1_3 || NETSTANDARD1_4 || NETSTANDARD1_5 || NETSTANDARD1_6
return null;
Expand All @@ -123,31 +123,39 @@ internal record struct ElfInfo(JetLinuxLibC LinuxLibC, JetArchitecture ProcessAr
return null;

var builder = new StringBuilder();

void OnDataReceived(string? str)
{
if (str != null)
lock (builder)
builder.AppendLine(str);
}

using (var process = new Process())
{
// Note(ww898): MUSL doesn't support `--version` argument, so /usr/bin/ldd dumps version and fail with exit code 1, see https://github.com/kraj/musl/blob/007997299248b8682dcbb73595c53dfe86071c83/ldso/dynlink.c#L1895-L1901
process.StartInfo = new ProcessStartInfo(ldd, "--version")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
RedirectStandardError = shouldFail
};

void OnDataReceived(string? str)
{
if (str != null)
lock (builder)
builder.AppendLine(str);
}

process.ErrorDataReceived += (_, args) => OnDataReceived(args.Data);
process.OutputDataReceived += (_, args) => OnDataReceived(args.Data);
if (shouldFail)
process.ErrorDataReceived += (_, args) => OnDataReceived(args.Data);

if (!process.Start())
throw new InvalidOperationException($"Failed to start {ldd} process");
process.BeginErrorReadLine();

process.BeginOutputReadLine();
if (shouldFail)
process.BeginErrorReadLine();

process.WaitForExit();
if (process.ExitCode != 0)

if (process.ExitCode != 0 && (!shouldFail || process.ExitCode != 1))
throw new InvalidOperationException($"The {ldd} process failed with exit code {process.ExitCode}");
}
return builder.ToString();
Expand All @@ -158,13 +166,13 @@ void OnDataReceived(string? str)

internal static Version? GetGlibcLddVersion()
{
var output = RunLddVersion();
var output = RunLddVersion(false);
return output != null ? ParseGlibcLddOutput(output) : null;
}

internal static Version? GetMuslLddVersion()
{
var output = RunLddVersion();
var output = RunLddVersion(true);
return output != null ? ParseMuslLddOutput(output) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Supported frameworks:
<PackageIcon>icon.png</PackageIcon>
<PackageTags>jetbrains windows linux macos freebsd netstandard netframework</PackageTags>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Version>1.4.0</Version>
<Version>1.4.1</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.FormatRipper" Version="2.2.1" />
Expand Down

0 comments on commit 71b3f69

Please sign in to comment.