Skip to content

Commit f36afb1

Browse files
[release/6.0-maui] Backport MacCatalyst process start support (#62520)
This PR adds support for Process.Start() and Process.Kill() for Mac Catalyst. I also added a way to run Mac Catalyst tests in App Sandbox and I resolved the problems with libproc in App Sandbox (the app is unable to obtain a list of running processes). Closes #61295, closes #61504.
1 parent a9d5ccd commit f36afb1

25 files changed

Lines changed: 188 additions & 44 deletions

eng/pipelines/runtime-staging.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,51 @@ jobs:
9999
eq(variables['monoContainsChange'], true),
100100
eq(variables['isFullMatrix'], true))
101101
102+
#
103+
# MacCatalyst interp - requires AOT Compilation and Interp flags
104+
# Build the whole product using Mono and run libraries tests
105+
# The test app is built with the App Sandbox entitlement
106+
#
107+
- template: /eng/pipelines/common/platform-matrix.yml
108+
parameters:
109+
jobTemplate: /eng/pipelines/common/global-build-job.yml
110+
helixQueuesTemplate: /eng/pipelines/libraries/helix-queues-setup.yml
111+
buildConfig: Release
112+
runtimeFlavor: mono
113+
platforms:
114+
- MacCatalyst_x64
115+
# don't run tests on arm64 PRs until we can get significantly more devices
116+
- ${{ if eq(variables['isFullMatrix'], true) }}:
117+
- MacCatalyst_arm64
118+
variables:
119+
# map dependencies variables to local variables
120+
- name: librariesContainsChange
121+
value: $[ dependencies.evaluate_paths.outputs['SetPathVars_libraries.containsChange'] ]
122+
- name: monoContainsChange
123+
value: $[ dependencies.evaluate_paths.outputs['SetPathVars_mono.containsChange'] ]
124+
jobParameters:
125+
testGroup: innerloop
126+
nameSuffix: AllSubsets_Mono_AppSandbox
127+
buildArgs: -s mono+libs+host+packs+libs.tests -c $(_BuildConfig) /p:ArchiveTests=true /p:DevTeamProvisioning=adhoc /p:RunAOTCompilation=true /p:MonoForceInterpreter=true /p:BuildDarwinFrameworks=true /p:EnableAppSandbox=true
128+
timeoutInMinutes: 180
129+
condition: >-
130+
or(
131+
eq(dependencies.evaluate_paths.outputs['SetPathVars_libraries.containsChange'], true),
132+
eq(dependencies.evaluate_paths.outputs['SetPathVars_mono.containsChange'], true),
133+
eq(dependencies.evaluate_paths.outputs['SetPathVars_installer.containsChange'], true),
134+
eq(variables['isFullMatrix'], true))
135+
# extra steps, run tests
136+
extraStepsTemplate: /eng/pipelines/libraries/helix.yml
137+
extraStepsParameters:
138+
creator: dotnet-bot
139+
interpreter: true
140+
testRunNamePrefixSuffix: Mono_$(_BuildConfig)
141+
condition: >-
142+
or(
143+
eq(variables['librariesContainsChange'], true),
144+
eq(variables['monoContainsChange'], true),
145+
eq(variables['isFullMatrix'], true))
146+
102147
#
103148
# MacCatalyst interp - requires AOT Compilation and Interp flags
104149
# Build the whole product using Mono and run libraries tests

eng/testing/tests.mobile.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
MainLibraryFileName="$(MainLibraryFileName)"
220220
ForceAOT="$(RunAOTCompilation)"
221221
ForceInterpreter="$(MonoForceInterpreter)"
222+
EnableAppSandbox="$(EnableAppSandbox)"
222223
InvariantGlobalization="$(InvariantGlobalization)"
223224
UseConsoleUITemplate="True"
224225
GenerateXcodeProject="$(GenerateXcodeProject)"

src/libraries/Common/src/Interop/OSX/Interop.libproc.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ internal static partial class libproc
2525
// Constants from sys\resource.h
2626
private const int RUSAGE_INFO_V3 = 3;
2727

28+
// Constants from sys/errno.h
29+
private const int EPERM = 1;
30+
2831
// Defines from proc_info.h
2932
internal enum ThreadRunState
3033
{
@@ -120,7 +123,14 @@ internal static unsafe int[] proc_listallpids()
120123
{
121124
// Get the number of processes currently running to know how much data to allocate
122125
int numProcesses = proc_listallpids(null, 0);
123-
if (numProcesses <= 0)
126+
if (numProcesses == 0 && Marshal.GetLastPInvokeError() == EPERM)
127+
{
128+
// An app running in App Sandbox does not have permissions to list other running processes
129+
// and so the `proc_listallpids` function returns 0 and sets errno to 1. As a fallback
130+
// we return at least an array with the PID of the current process which we always know.
131+
return new[] { Environment.ProcessId };
132+
}
133+
else if (numProcesses <= 0)
124134
{
125135
throw new Win32Exception(SR.CantGetAllPids);
126136
}

src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public static partial class PlatformDetection
4141
public static bool IsMacOsCatalinaOrHigher => IsOSX && Environment.OSVersion.Version >= new Version(10, 15);
4242
public static bool IsMacOsAppleSilicon => IsOSX && IsArm64Process;
4343
public static bool IsNotMacOsAppleSilicon => !IsMacOsAppleSilicon;
44+
public static bool IsAppSandbox => Environment.GetEnvironmentVariable("APP_SANDBOX_CONTAINER_ID") != null;
45+
public static bool IsNotAppSandbox => !IsAppSandbox;
4446

4547
// RedHat family covers RedHat and CentOS
4648
public static bool IsRedHatFamily => IsRedHatFamilyAndVersion();

src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public Process() { }
4343
public System.Diagnostics.ProcessModule? MainModule { get { throw null; } }
4444
public System.IntPtr MainWindowHandle { get { throw null; } }
4545
public string MainWindowTitle { get { throw null; } }
46-
public System.IntPtr MaxWorkingSet { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("freebsd"), System.Runtime.Versioning.SupportedOSPlatformAttribute("macos"), System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
47-
public System.IntPtr MinWorkingSet { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("freebsd"), System.Runtime.Versioning.SupportedOSPlatformAttribute("macos"), System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
46+
public System.IntPtr MaxWorkingSet { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos"), System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("freebsd"), System.Runtime.Versioning.SupportedOSPlatformAttribute("macos"), System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst"), System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
47+
public System.IntPtr MinWorkingSet { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos"), System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("freebsd"), System.Runtime.Versioning.SupportedOSPlatformAttribute("macos"), System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst"), System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
4848
public System.Diagnostics.ProcessModuleCollection Modules { get { throw null; } }
4949
[System.ObsoleteAttribute("Process.NonpagedSystemMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.NonpagedSystemMemorySize64 instead.")]
5050
public int NonpagedSystemMemorySize { get { throw null; } }
@@ -86,9 +86,11 @@ public Process() { }
8686
public System.Diagnostics.ProcessThreadCollection Threads { get { throw null; } }
8787
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
8888
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
89+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
8990
public System.TimeSpan TotalProcessorTime { get { throw null; } }
9091
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
9192
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
93+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
9294
public System.TimeSpan UserProcessorTime { get { throw null; } }
9395
[System.ObsoleteAttribute("Process.VirtualMemorySize has been deprecated because the type of the property can't represent all valid results. Use System.Diagnostics.Process.VirtualMemorySize64 instead.")]
9496
public int VirtualMemorySize { get { throw null; } }
@@ -112,39 +114,50 @@ public static void EnterDebugMode() { }
112114
public static System.Diagnostics.Process GetProcessById(int processId, string machineName) { throw null; }
113115
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
114116
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
117+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
115118
public static System.Diagnostics.Process[] GetProcesses() { throw null; }
116119
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
117120
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
121+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
118122
public static System.Diagnostics.Process[] GetProcesses(string machineName) { throw null; }
119123
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
120124
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
125+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
121126
public static System.Diagnostics.Process[] GetProcessesByName(string? processName) { throw null; }
122127
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
123128
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
129+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
124130
public static System.Diagnostics.Process[] GetProcessesByName(string? processName, string machineName) { throw null; }
125131
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
126132
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
133+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
127134
public void Kill() { }
128135
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
129136
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
137+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
130138
public void Kill(bool entireProcessTree) { }
131139
public static void LeaveDebugMode() { }
132140
protected void OnExited() { }
133141
public void Refresh() { }
134142
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
135143
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
144+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
136145
public bool Start() { throw null; }
137146
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
138147
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
148+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
139149
public static System.Diagnostics.Process? Start(System.Diagnostics.ProcessStartInfo startInfo) { throw null; }
140150
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
141151
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
152+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
142153
public static System.Diagnostics.Process Start(string fileName) { throw null; }
143154
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
144155
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
156+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
145157
public static System.Diagnostics.Process Start(string fileName, string arguments) { throw null; }
146158
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
147159
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
160+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
148161
public static System.Diagnostics.Process Start(string fileName, System.Collections.Generic.IEnumerable<string> arguments) { throw null; }
149162
[System.CLSCompliantAttribute(false)]
150163
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
@@ -247,6 +260,7 @@ public int IdealProcessor { set { } }
247260
public System.Diagnostics.ThreadPriorityLevel PriorityLevel { [System.Runtime.Versioning.SupportedOSPlatform("windows")] [System.Runtime.Versioning.SupportedOSPlatform("linux")] [System.Runtime.Versioning.SupportedOSPlatform("freebsd")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
248261
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
249262
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
263+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
250264
public System.TimeSpan PrivilegedProcessorTime { get { throw null; } }
251265
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
252266
public System.IntPtr ProcessorAffinity { set { } }
@@ -257,9 +271,11 @@ public System.IntPtr ProcessorAffinity { set { } }
257271
public System.Diagnostics.ThreadState ThreadState { get { throw null; } }
258272
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
259273
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
274+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
260275
public System.TimeSpan TotalProcessorTime { get { throw null; } }
261276
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
262277
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
278+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("maccatalyst")]
263279
public System.TimeSpan UserProcessorTime { get { throw null; } }
264280
public System.Diagnostics.ThreadWaitReason WaitReason { get { throw null; } }
265281
public void ResetIdealProcessor() { }

src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<DefineConstants>$(DefineConstants);FEATURE_REGISTRY</DefineConstants>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5-
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent);$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS</TargetFrameworks>
5+
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent);$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-MacCatalyst;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS</TargetFrameworks>
66
<Nullable>enable</Nullable>
77
<!-- Suppress unused field warnings when using PlatformNotSupportedException stubs -->
88
<NoWarn Condition=" '$(TargetsFreeBSD)' == 'true' or '$(TargetsUnknownUnix)' == 'true' ">$(NoWarn);0649</NoWarn>
@@ -309,7 +309,7 @@
309309
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.SchedGetSetAffinity.cs"
310310
Link="Common\Interop\Linux\Interop.SchedGetSetAffinity.cs" />
311311
</ItemGroup>
312-
<ItemGroup Condition=" '$(TargetsOSX)' == 'true'">
312+
<ItemGroup Condition=" '$(TargetsOSX)' == 'true' or '$(TargetsMacCatalyst)' == 'true'">
313313
<Compile Include="System\Diagnostics\Process.BSD.cs" />
314314
<Compile Include="System\Diagnostics\Process.OSX.cs" />
315315
<Compile Include="System\Diagnostics\ProcessManager.BSD.cs" />

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.BSD.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public partial class Process
1515
/// </summary>
1616
[UnsupportedOSPlatform("ios")]
1717
[UnsupportedOSPlatform("tvos")]
18+
[SupportedOSPlatform("maccatalyst")]
1819
public static Process[] GetProcessesByName(string? processName, string machineName)
1920
{
2021
if (processName == null)

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Linux.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public partial class Process : IDisposable
2020
/// </summary>
2121
[UnsupportedOSPlatform("ios")]
2222
[UnsupportedOSPlatform("tvos")]
23+
[SupportedOSPlatform("maccatalyst")]
2324
public static Process[] GetProcessesByName(string? processName, string machineName)
2425
{
2526
ProcessManager.ThrowIfRemoteMachine(machineName);

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.NonUap.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public partial class Process : IDisposable
1111
{
1212
[UnsupportedOSPlatform("ios")]
1313
[UnsupportedOSPlatform("tvos")]
14+
[SupportedOSPlatform("maccatalyst")]
1415
public void Kill(bool entireProcessTree)
1516
{
1617
if (!entireProcessTree)

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public static Process Start(string fileName, string arguments, string userName,
5858
/// <summary>Terminates the associated process immediately.</summary>
5959
[UnsupportedOSPlatform("ios")]
6060
[UnsupportedOSPlatform("tvos")]
61+
[SupportedOSPlatform("maccatalyst")]
6162
public void Kill()
6263
{
63-
if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS())
64+
if (PlatformDoesNotSupportProcessStartAndKill)
6465
{
6566
throw new PlatformNotSupportedException();
6667
}
@@ -372,7 +373,7 @@ private SafeProcessHandle GetProcessHandle()
372373
/// <param name="startInfo">The start info with which to start the process.</param>
373374
private bool StartCore(ProcessStartInfo startInfo)
374375
{
375-
if (OperatingSystem.IsIOS() || OperatingSystem.IsTvOS())
376+
if (PlatformDoesNotSupportProcessStartAndKill)
376377
{
377378
throw new PlatformNotSupportedException();
378379
}
@@ -1105,5 +1106,8 @@ private static int OnSigChild(int reapAll, int configureConsole)
11051106
s_processStartLock.ExitWriteLock();
11061107
}
11071108
}
1109+
1110+
private static bool PlatformDoesNotSupportProcessStartAndKill
1111+
=> (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst()) || OperatingSystem.IsTvOS();
11081112
}
11091113
}

0 commit comments

Comments
 (0)