aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-25 16:05:12 -0500
committerJP Dillingham <jp@dillingham.me>2026-10-25 16:05:12 -0500
commit6de3be313ae26aac832d15dffef5400333200e09 (patch)
treeaf06bb9f40db4f8f6b314f7cd6c59779f4bb2523 /src/slskd/Common
parentb51f789bc3f15d5d8f9424f58f50014fbc58391a (diff)
parenta4cce04d38544ad6d4c1506b9facb32407a79509 (diff)
downloadslskd-6de3be313ae26aac832d15dffef5400333200e09.tar.gz
Merge remote-tracking branch 'origin' into auth
Diffstat (limited to 'src/slskd/Common')
-rw-r--r--src/slskd/Common/Dumper.cs181
1 files changed, 0 insertions, 181 deletions
diff --git a/src/slskd/Common/Dumper.cs b/src/slskd/Common/Dumper.cs
deleted file mode 100644
index 8797d040..00000000
--- a/src/slskd/Common/Dumper.cs
+++ /dev/null
@@ -1,181 +0,0 @@
-// <copyright file="Dumper.cs" company="JP Dillingham">
-// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
-// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
-// █__ --█ █__ --█ ◄█ - █
-// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
-// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
-// │ Copyright (c) JP Dillingham.
-// │
-// │ This program is free software: you can redistribute it and/or modify
-// │ it under the terms of the GNU Affero General Public License as published
-// │ by the Free Software Foundation, version 3.
-// │
-// │ This program is distributed in the hope that it will be useful,
-// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
-// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// │ GNU Affero General Public License for more details.
-// │
-// │ You should have received a copy of the GNU Affero General Public License
-// │ along with this program. If not, see https://www.gnu.org/licenses/.
-// │
-// │ This program is distributed with Additional Terms pursuant to Section 7
-// │ of the AGPLv3. See the LICENSE file in the root directory of this
-// │ project for the complete terms and conditions.
-// │
-// │ https://slskd.org
-// │
-// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
-// │ SPDX-FileCopyrightText: JP Dillingham
-// │ SPDX-License-Identifier: AGPL-3.0-only
-// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
-// </copyright>
-
-namespace slskd
-{
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Http;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
-
- /// <summary>
- /// Dumps the contents of the application's memory to a .dmp file using dotnet-dump.
- /// </summary>
- /// <remarks>
- /// <para>
- /// Monitor https://docs.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dump for tool updates. Currently supported
- /// runtime IDs:
- /// </para>
- /// <list type="bullet">
- /// <listheader>Currently supported runtime IDs:</listheader>
- /// <item>https://aka.ms/dotnet-dump/win-x86</item>
- /// <item>https://aka.ms/dotnet-dump/win-x64</item>
- /// <item>https://aka.ms/dotnet-dump/win-arm</item>
- /// <item>https://aka.ms/dotnet-dump/win-arm64</item>
- /// <item>https://aka.ms/dotnet-dump/osx-x64</item>
- /// <item>https://aka.ms/dotnet-dump/linux-x64</item>
- /// <item>https://aka.ms/dotnet-dump/linux-arm</item>
- /// <item>https://aka.ms/dotnet-dump/linux-arm64</item>
- /// <item>https://aka.ms/dotnet-dump/linux-musl-x64</item>
- /// <item>https://aka.ms/dotnet-dump/linux-musl-arm64</item>
- /// <item></item>
- /// </list>
- /// </remarks>
- public class Dumper : IDisposable
- {
- private static readonly string URLTemplate = "https://aka.ms/dotnet-dump/$RID";
-
- private string BinFile { get; set; }
- private bool Disposed { get; set; }
-
- public void Dispose()
- {
- Dispose(disposing: true);
- GC.SuppressFinalize(this);
- }
-
- public async Task<string> DumpAsync()
- {
- BinFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
- var outputFile = Path.Combine(Path.GetTempPath(), $"slskd_{Path.GetRandomFileName()}.dmp");
-
- var url = URLTemplate.Replace("$RID", GetRID());
-
- await Download(url, BinFile);
-
- if (!System.OperatingSystem.IsWindows())
- {
- await ExecAsync("/bin/chmod", $"+x \"{BinFile}\"");
- }
-
- await ExecAsync(BinFile, $"collect --process-id {Environment.ProcessId} --type full --output \"{outputFile}\"");
-
- return outputFile;
- }
-
- protected virtual void Dispose(bool disposing)
- {
- if (!Disposed)
- {
- if (disposing)
- {
- TryDelete(BinFile);
- }
-
- Disposed = true;
- }
- }
-
- private async Task Download(string url, string destination)
- {
- using var http = new HttpClient();
-
- using var localStream = new FileStream(destination, FileMode.OpenOrCreate);
- using var remoteStream = await http.GetStreamAsync(url);
-
- await remoteStream.CopyToAsync(localStream);
- }
-
- private async Task ExecAsync(string bin, string args)
- {
- using var process = new Process();
- process.StartInfo.FileName = bin;
- process.StartInfo.Arguments = args;
- process.Start();
- await process.WaitForExitAsync();
-
- if (process.ExitCode != 0)
- {
- throw new SlskdException($"'{bin}' exited with code {process.ExitCode}");
- }
- }
-
- private string GetRID()
- {
- // one of: x86, x64, arm, arm64, wasm, s390x
- var arch = RuntimeInformation.ProcessArchitecture.ToString().ToLower();
-
- // .RuntimeIdentifier returns a very specific (e.g. win10-x64) RID, and we need a generic one (e.g. win-x64). rather
- // than trying to hack this up to derive a generic RID, only inspect this to see if this build targeted musl libc (we
- // can't get this any other way)
- var isMusl = RuntimeInformation.RuntimeIdentifier.ToLower().Contains("musl");
-
- string os = default;
-
- // seems like there should be a way to just retrieve this, but there is not as of .NET 6
- if (System.OperatingSystem.IsLinux())
- {
- os = "linux";
- }
- else if (System.OperatingSystem.IsWindows())
- {
- os = "win";
- }
- else if (System.OperatingSystem.IsMacOS() || System.OperatingSystem.IsFreeBSD())
- {
- os = "osx";
- }
-
- if (os == default)
- {
- throw new PlatformNotSupportedException($"Unable to determine operating system. RID is {RuntimeInformation.RuntimeIdentifier}; did someone forget to update Dumper.cs to reflect .NET targeting changes?");
- }
-
- return $"{os}-{(isMusl ? "musl-" : string.Empty)}{arch}";
- }
-
- private bool TryDelete(string file)
- {
- try
- {
- File.Delete(file);
- return true;
- }
- catch (FileNotFoundException)
- {
- return false;
- }
- }
- }
-} \ No newline at end of file