Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#if !UNIX // Not built on Unix
#if UNIX
// On Unix the service cmdlets run through a Unix service-manager facade; the
// aliases below make the cmdlet implementation in this file work unchanged.
using NativeMethods = Microsoft.PowerShell.Commands.UnixServiceNativeMethods;
using ServiceController = Microsoft.PowerShell.Commands.UnixServiceController;
#endif

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -116,6 +121,7 @@ internal void SetServiceSecurityDescriptor(
string securityDescriptorSddl,
NakedWin32Handle hService)
{
#if !UNIX
var rawSecurityDescriptor = new RawSecurityDescriptor(securityDescriptorSddl);
RawAcl rawDiscretionaryAcl = rawSecurityDescriptor.DiscretionaryAcl;
var discretionaryAcl = new DiscretionaryAcl(false, false, rawDiscretionaryAcl);
Expand Down Expand Up @@ -143,6 +149,7 @@ internal void SetServiceSecurityDescriptor(
StringUtil.Format(ServiceResources.CouldNotSetServiceSecurityDescriptorSddl, service.ServiceName, exception.Message),
accessDenied ? ErrorCategory.PermissionDenied : ErrorCategory.InvalidOperation);
}
#endif // !UNIX
}
#endregion Internal
}
Expand Down Expand Up @@ -610,6 +617,7 @@ public string[] Name
/// </summary>
protected override void ProcessRecord()
{
#if !UNIX
nint scManagerHandle = nint.Zero;
if (!DependentServices && !RequiredServices)
{
Expand Down Expand Up @@ -669,12 +677,53 @@ protected override void ProcessRecord()
Diagnostics.Assert(succeeded, "SCManager handle close failed");
}
}
#else
// On Unix the enumeration and status come from the service manager facade.
try
{
foreach (ServiceController service in MatchingServices())
{
if (!DependentServices.IsPresent && !RequiredServices.IsPresent)
{
WriteObject(service);
}
else
{
if (DependentServices.IsPresent)
{
foreach (ServiceController dependantserv in service.DependentServices)
{
WriteObject(dependantserv);
}
}

if (RequiredServices.IsPresent)
{
foreach (ServiceController servicedependedon in service.ServicesDependedOn)
{
WriteObject(servicedependedon);
}
}
}
}
}
catch (InvalidOperationException e) when (e.Data.Contains("NoServiceManagerFound"))
{
ErrorRecord err = new ErrorRecord(
e,
"NoServiceManagerFound",
ErrorCategory.ObjectNotFound,
null);
ThrowTerminatingError(err);
}
#endif
}

#endregion Overrides

#nullable enable

#if !UNIX
/// <summary>
/// Writes a verbose message when a service property query fails.
/// </summary>
Expand Down Expand Up @@ -799,6 +848,7 @@ private PSObject AddProperties(nint scManagerHandle, ServiceController service)

return serviceAsPSObj;
}
#endif // !UNIX
}
#nullable disable
#endregion GetServiceCommand
Expand Down Expand Up @@ -947,6 +997,17 @@ internal bool DoStartService(ServiceController serviceController)
{
serviceController.Start();
}
#if UNIX
catch (InvalidOperationException e) when (e.Data.Contains("SudoRequired"))
{
WriteError(new ErrorRecord(
new ServiceCommandException(e.Message) { ServiceName = serviceController.ServiceName },
"SudoRequired",
ErrorCategory.PermissionDenied,
serviceController));
return false;
}
#endif
catch (Win32Exception e)
{
if (e.NativeErrorCode != NativeMethods.ERROR_SERVICE_ALREADY_RUNNING)
Expand Down Expand Up @@ -1063,6 +1124,18 @@ internal List<ServiceController> DoStopService(ServiceController serviceControll
{
serviceController.Stop();
}
#if UNIX
catch (InvalidOperationException e) when (e.Data.Contains("SudoRequired"))
{
WriteError(new ErrorRecord(
new ServiceCommandException(e.Message) { ServiceName = serviceController.ServiceName },
"SudoRequired",
ErrorCategory.PermissionDenied,
serviceController));
RemoveNotStoppedServices(stoppedServices);
return stoppedServices;
}
#endif
catch (Win32Exception e)
{
if (e.NativeErrorCode != NativeMethods.ERROR_SERVICE_NOT_ACTIVE)
Expand Down Expand Up @@ -1393,6 +1466,7 @@ protected override void ProcessRecord()
}
#endregion StartServiceCommand

#if !UNIX
#region SuspendServiceCommand
/// <summary>
/// This class implements the suspend-service command.
Expand Down Expand Up @@ -1424,7 +1498,9 @@ protected override void ProcessRecord()
}
}
#endregion SuspendServiceCommand
#endif // !UNIX

#if !UNIX
#region ResumeServiceCommand
/// <summary>
/// This class implements the resume-service command.
Expand Down Expand Up @@ -1457,6 +1533,7 @@ protected override void ProcessRecord()
}
}
#endregion ResumeServiceCommand
#endif // !UNIX

#region RestartServiceCommand

Expand Down Expand Up @@ -1512,6 +1589,7 @@ protected override void ProcessRecord()
}
#endregion RestartServiceCommand

#if !UNIX
#region SetServiceCommand

/// <summary>
Expand Down Expand Up @@ -2030,7 +2108,9 @@ protected override void ProcessRecord()

}
#endregion SetServiceCommand
#endif // !UNIX

#if !UNIX
#region NewServiceCommand
/// <summary>
/// This class implements the New-Service command.
Expand Down Expand Up @@ -2396,7 +2476,9 @@ protected override void BeginProcessing()
#endregion Overrides
}
#endregion NewServiceCommand
#endif // !UNIX

#if !UNIX
#region RemoveServiceCommand
/// <summary>
/// This class implements the Remove-Service command.
Expand Down Expand Up @@ -2565,6 +2647,7 @@ protected override void ProcessRecord()
#endregion Overrides
}
#endregion RemoveServiceCommand
#endif // !UNIX

#region ServiceCommandException
/// <summary>
Expand Down Expand Up @@ -2636,6 +2719,7 @@ public string ServiceName
}
#endregion ServiceCommandException

#if !UNIX
#region NativeMethods
internal static class NativeMethods
{
Expand Down Expand Up @@ -2922,6 +3006,7 @@ internal static ServiceStartupType GetServiceStartupType(ServiceStartMode startM
}
}
#endregion NativeMethods
#endif // !UNIX

#region ServiceStartupType
/// <summary>
Expand All @@ -2942,5 +3027,3 @@ public enum ServiceStartupType
}
#endregion ServiceStartupType
}

#endif // Not built on Unix
Loading