chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//            Copyright Keysight Technologies 2012-2025
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
 
namespace OpenTap
{
    
    internal static class ExecutorSubProcess
    {
        public class EnvVarNames
        {
            public static string TpmInteropPipeName = "TPM_PIPE";
            public static string ParentProcessExeDir = "TPM_PARENTPROCESSDIR";
            public static string OpenTapInitDirectory = "OPENTAP_INIT_DIRECTORY";
        }
    }
 
 
    internal static class ExecutorClient
    {
        /// <summary>
        /// Is this process an isolated sub process of tap.exe
        /// </summary>
        public static bool IsRunningIsolated => Environment.GetEnvironmentVariable(ExecutorSubProcess.EnvVarNames.ParentProcessExeDir) != null;
        /// <summary>
        /// Is this process a sub process of tap.exe
        /// </summary>
        public static bool IsExecutorMode => Environment.GetEnvironmentVariable(ExecutorSubProcess.EnvVarNames.TpmInteropPipeName) != null;
        /// <summary>
        /// The directory containing the OpenTAP installation.
        /// This is usually the value of the environment variable OPENTAP_INIT_DIRECTORY set by tap.exe
        /// If this value is not set, use the location of OpenTap.dll instead
        /// In some cases, when running isolated this is that value but from the parent process.
        /// </summary>
        public static string ExeDir
        {
            get
            {
                if (IsRunningIsolated)
                    return Environment.GetEnvironmentVariable(ExecutorSubProcess.EnvVarNames.ParentProcessExeDir);
                else
                {
                    var exePath = Environment.GetEnvironmentVariable(ExecutorSubProcess.EnvVarNames.OpenTapInitDirectory);
                    if (exePath != null)
                        return exePath;
 
                    // Referencing OpenTap.dll causes the file to become locked.
                    // Ensure OpenTap.dll is only loaded if the environment variable is not set.
                    // This should only happen if OpenTAP was not loaded through tap.exe.
                    return GetOpenTapDllLocation();
                }
            }
        }
        
        public static string Dotnet => _dotnet ??= mineDotnet();
        private static string _dotnet = null;
        private static string mineDotnet()
        {
            // Ensure dotnet is always assigned. "dotnet' is used as a fallback, in which case the system
            // will try to resolve it from the current PATH
 
            // Look for dotnet.exe on windows
            var executable = Path.DirectorySeparatorChar == '\\' ? "dotnet.exe" : "dotnet";
            try
            {
                // 1. Try to check which `dotnet` instance launched the current application
                string mainModulePath = Process.GetCurrentProcess().MainModule?.FileName;
                if (!string.IsNullOrWhiteSpace(mainModulePath) &&
                    Path.GetFileName(mainModulePath).Equals(executable, StringComparison.OrdinalIgnoreCase))
                {
                    return mainModulePath;
                }
            }
            catch
            {
                // ignore potential permission errors
            }
 
            try
            {
                // 2. Find dotnet based on runtime information
                var runtime = RuntimeEnvironment.GetRuntimeDirectory();
                if (!string.IsNullOrWhiteSpace(runtime) && Directory.Exists(runtime))
                {
                    var dir = new DirectoryInfo(runtime);
                    while (dir != null)
                    {
                        var candidate = System.IO.Path.Combine(dir.FullName, executable);
                        if (File.Exists(candidate))
                        {
                            return candidate;
                        }
 
                        dir = dir.Parent;
                    }
                }
            }
            catch
            {
                // ignore
            }
 
            return "dotnet";
        } 
 
        [MethodImpl(MethodImplOptions.NoInlining)]
        static string GetOpenTapDllLocation() => Path.GetDirectoryName(typeof(PluginSearcher).Assembly.Location);
    }
}