Merge pull request #392 from NextTurn/desc

Set description via advapi32
pull/361/head
Next Turn 2020-03-29 23:55:36 +08:00 committed by GitHub
commit 8f5e3b40dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 33 deletions

View File

@ -17,7 +17,6 @@ using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using Microsoft.Win32;
using winsw.Extensions;
using winsw.Logging;
using winsw.Native;
@ -643,47 +642,30 @@ namespace winsw
password,
descriptor.ServiceDependencies);
// update the description
/* Somehow this doesn't work, even though it doesn't report an error
Win32Service s = svc.Select(d.Id);
s.Description = d.Description;
s.Commit();
*/
// so using a classic method to set the description. Ugly.
Registry.LocalMachine
.OpenSubKey("System")
.OpenSubKey("CurrentControlSet")
.OpenSubKey("Services")
.OpenSubKey(descriptor.Id, true)
.SetValue("Description", descriptor.Description);
var actions = descriptor.FailureActions;
var isDelayedAutoStart = descriptor.StartMode == StartMode.Automatic && descriptor.DelayedAutoStart;
if (actions.Count > 0 || isDelayedAutoStart)
{
using ServiceManager scm = new ServiceManager();
using Service sc = scm.Open(descriptor.Id);
// Delayed auto start
sc.SetDescription(descriptor.Description);
var actions = descriptor.FailureActions;
if (actions.Count > 0)
{
sc.SetFailureActions(descriptor.ResetFailureAfter, actions);
}
var isDelayedAutoStart = descriptor.StartMode == StartMode.Automatic && descriptor.DelayedAutoStart;
if (isDelayedAutoStart)
{
sc.SetDelayedAutoStart(true);
}
// Set the failure actions
if (actions.Count > 0)
{
sc.ChangeConfig(descriptor.ResetFailureAfter, actions);
}
}
if (descriptor.SecurityDescriptor != null)
{
// throws ArgumentException
RawSecurityDescriptor rawSecurityDescriptor = new RawSecurityDescriptor(descriptor.SecurityDescriptor);
byte[] securityDescriptorBytes = new byte[rawSecurityDescriptor.BinaryLength];
rawSecurityDescriptor.GetBinaryForm(securityDescriptorBytes, 0);
Advapi32.SetServiceObjectSecurity(/*TODO*/default, SecurityInfos.DiscretionaryAcl, securityDescriptorBytes);
_ = Advapi32.SetServiceObjectSecurity(sc.Handle, SecurityInfos.DiscretionaryAcl, securityDescriptorBytes);
}
return;

View File

@ -41,16 +41,16 @@ namespace winsw.Native
}
}
public class Service : IDisposable
public struct Service : IDisposable
{
internal IntPtr Handle;
public IntPtr Handle;
internal Service(IntPtr service)
{
Handle = service;
}
public void ChangeConfig(TimeSpan failureResetPeriod, List<SC_ACTION> actions)
public void SetFailureActions(TimeSpan failureResetPeriod, List<SC_ACTION> actions)
{
SERVICE_FAILURE_ACTIONS sfa = new SERVICE_FAILURE_ACTIONS
{
@ -101,6 +101,11 @@ namespace winsw.Native
}
}
public void SetDescription(string description)
{
_ = Advapi32.ChangeServiceConfig2(Handle, SERVICE_CONFIG_INFOLEVEL.SERVICE_CONFIG_DESCRIPTION, new SERVICE_DESCRIPTION { lpDescription = description });
}
public void Dispose()
{
if (Handle != IntPtr.Zero)
@ -267,6 +272,9 @@ namespace winsw.Native
[DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "ChangeServiceConfig2W")]
internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, in SERVICE_DELAYED_AUTO_START lpInfo);
[DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "ChangeServiceConfig2W")]
internal static extern bool ChangeServiceConfig2(IntPtr hService, SERVICE_CONFIG_INFOLEVEL dwInfoLevel, in SERVICE_DESCRIPTION lpInfo);
[DllImport(Advapi32LibraryName, SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "OpenSCManagerW")]
internal static extern IntPtr OpenSCManager(string? lpMachineName, string? lpDatabaseName, uint dwDesiredAccess);
@ -589,4 +597,10 @@ namespace winsw.Native
{
public bool fDelayedAutostart;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SERVICE_DESCRIPTION
{
public string lpDescription;
}
}