Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

67 lines (53 sloc) 1.82 KB
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Net.Sockets;
namespace Microsoft.Extensions.Hosting.Systemd
{
public class SystemdNotifier : ISystemdNotifier
{
private const string NOTIFY_SOCKET = "NOTIFY_SOCKET";
private readonly string _socketPath;
public SystemdNotifier() :
this(GetNotifySocketPath())
{ }
// For testing
internal SystemdNotifier(string socketPath)
{
_socketPath = socketPath;
}
/// <inheritdoc />
public bool IsEnabled => _socketPath != null;
/// <inheritdoc />
public void Notify(ServiceState state)
{
if (!IsEnabled)
{
return;
}
using (var socket = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified))
{
var endPoint = new UnixDomainSocketEndPoint(_socketPath);
socket.Connect(endPoint);
// It's safe to do a non-blocking call here: messages sent here are much
// smaller than kernel buffers so we won't get blocked.
socket.Send(state.GetData());
}
}
private static string GetNotifySocketPath()
{
string socketPath = Environment.GetEnvironmentVariable(NOTIFY_SOCKET);
if (string.IsNullOrEmpty(socketPath))
{
return null;
}
// Support abstract socket paths.
if (socketPath[0] == '@')
{
socketPath = "\0" + socketPath.Substring(1);
}
return socketPath;
}
}
}
You can’t perform that action at this time.