aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Program.cs
diff options
context:
space:
mode:
authorCody Robibero <cody@robibe.ro>2022-03-26 07:24:06 -0600
committerGitHub <noreply@github.com>2022-03-26 07:24:06 -0600
commit4fa7f49ffc6cec2a5be3cf9e5ff0fb727815e6b1 (patch)
tree45d420ad09d3a270d186a1c586a3d1ae20209c12 /Jellyfin.Server/Program.cs
parentd80c4ee3811546d6fe7e5bc03f6c8d5034d32797 (diff)
parent0a068b924f0be67c8f9602288e545e39dce7314c (diff)
Merge pull request #7202 from knackebrot/unix-perms
Diffstat (limited to 'Jellyfin.Server/Program.cs')
-rw-r--r--Jellyfin.Server/Program.cs67
1 files changed, 53 insertions, 14 deletions
diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs
index fc871f064..d699102ff 100644
--- a/Jellyfin.Server/Program.cs
+++ b/Jellyfin.Server/Program.cs
@@ -5,6 +5,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
+using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -197,6 +198,13 @@ namespace Jellyfin.Server
try
{
await webHost.StartAsync(_tokenSource.Token).ConfigureAwait(false);
+
+ if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
+ {
+ var socketPath = GetUnixSocketPath(startupConfig, appPaths);
+
+ SetUnixSocketPermissions(startupConfig, socketPath);
+ }
}
catch (Exception ex) when (ex is not TaskCanceledException)
{
@@ -326,20 +334,7 @@ namespace Jellyfin.Server
// Bind to unix socket (only on unix systems)
if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
{
- var socketPath = startupConfig.GetUnixSocketPath();
- if (string.IsNullOrEmpty(socketPath))
- {
- var xdgRuntimeDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
- if (xdgRuntimeDir == null)
- {
- // Fall back to config dir
- socketPath = Path.Join(appPaths.ConfigurationDirectoryPath, "socket.sock");
- }
- else
- {
- socketPath = Path.Join(xdgRuntimeDir, "jellyfin-socket");
- }
- }
+ var socketPath = GetUnixSocketPath(startupConfig, appPaths);
// Workaround for https://github.com/aspnet/AspNetCore/issues/14134
if (File.Exists(socketPath))
@@ -663,5 +658,49 @@ namespace Jellyfin.Server
return "\"" + arg + "\"";
}
+
+ private static string GetUnixSocketPath(IConfiguration startupConfig, IApplicationPaths appPaths)
+ {
+ var socketPath = startupConfig.GetUnixSocketPath();
+
+ if (string.IsNullOrEmpty(socketPath))
+ {
+ var xdgRuntimeDir = Environment.GetEnvironmentVariable("XDG_RUNTIME_DIR");
+ var socketFile = "jellyfin.sock";
+ if (xdgRuntimeDir == null)
+ {
+ // Fall back to config dir
+ socketPath = Path.Join(appPaths.ConfigurationDirectoryPath, socketFile);
+ }
+ else
+ {
+ socketPath = Path.Join(xdgRuntimeDir, socketFile);
+ }
+ }
+
+ return socketPath;
+ }
+
+ private static void SetUnixSocketPermissions(IConfiguration startupConfig, string socketPath)
+ {
+ var socketPerms = startupConfig.GetUnixSocketPermissions();
+
+ if (!string.IsNullOrEmpty(socketPerms))
+ {
+ [DllImport("libc")]
+ static extern int Chmod(string pathname, int mode);
+
+ var exitCode = Chmod(socketPath, Convert.ToInt32(socketPerms, 8));
+
+ if (exitCode < 0)
+ {
+ _logger.LogError("Failed to set Kestrel unix socket permissions to {SocketPerms}, return code: {ExitCode}", socketPerms, exitCode);
+ }
+ else
+ {
+ _logger.LogInformation("Kestrel unix socket permissions set to {SocketPerms}", socketPerms);
+ }
+ }
+ }
}
}