Ejemplo n.º 1
0
        private ProcessIsolator(IsolationOptions options)
        {
            m_options = options ?? throw new ArgumentNullException(nameof(options));
            m_options.SanitizeAndSetReadOnly();

            Id         = Interlocked.Increment(ref s_nextId);
            m_pipeName = "pihost." + Id + "." + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
            m_client   = new IpcServiceClientBuilder <IHostProcess>().UseNamedPipe(m_pipeName).Build();
        }
Ejemplo n.º 2
0
        public static async Task <ProcessIsolator> CreateAsync(IsolationOptions options)
        {
            options ??= new IsolationOptions();

            var isolator = new ProcessIsolator(options);

            try
            {
                await isolator.StartAsync().ConfigureAwait(false);
            }
            catch (Exception)
            {
                isolator.Dispose();
                throw;
            }

            return(isolator);
        }
Ejemplo n.º 3
0
        public HostProcessClient(IpcServiceClient <IHostProcess> client, string pipeName, IsolationOptions options)
        {
            m_client  = client;
            PipeName  = pipeName;
            m_process = new Process();

            var arguments = new StringBuilder();

            if (!options.StartWithExeLauncher)
            {
                HostBinary          = Path.GetFileName(s_locations.Value.HostAssemblyPath);
                m_process.StartInfo = new ProcessStartInfo(s_locations.Value.DotNetExecutablePath);
                arguments.Append(s_locations.Value.HostAssemblyPath);
            }
            else
            {
                HostBinary          = Path.GetFileName(s_locations.Value.HostExecutablePath);
                m_process.StartInfo = new ProcessStartInfo(s_locations.Value.HostExecutablePath);
            }

            if (options.CreateJobObject)
            {
                CreateJobObject = true;
                JobName         = PipeName; // For now, simply reuse pipe name
                m_jobObject     = new JobObject(JobName);

                if (options.DieOnCrash)
                {
                    // Normally, we don't want the controlling process to die when a host process
                    // crashes/exits. However, the user might want to bind their faiths together
                    // for easier management.
                    m_jobObject.AddProcess(Process.GetCurrentProcess());
                }
            }

            if (options.Limits != null && options.Limits.IsAnyEnabled)
            {
                if (options.Limits.MaxCpuUsage > 0)
                {
                    arguments.Append(" --max-cpu ").Append(options.Limits.MaxCpuUsage
                                                           .ToString(CultureInfo.InvariantCulture));
                }

                if (options.Limits.MaxMemory > 0)
                {
                    arguments.Append(" --max-memory ").Append(options.Limits.MaxMemory
                                                              .ToString(CultureInfo.InvariantCulture));
                }

                if (options.Limits.AffinityMask != IntPtr.Zero)
                {
                    arguments.Append(" --affinity-mask ").Append(options.Limits.AffinityMask
                                                                 .ToInt32().ToString(CultureInfo.InvariantCulture));
                }
            }

            Dictionary <string, string> environment = null;

            AddDebug(ref environment, options.Debug);
            AddLogLevel(ref environment, options.LogLevel);
            AddListenerThreads(ref environment, options.ListenerThreads);

            if (arguments.Length > 0)
            {
                arguments.Append(" ");
            }

            arguments.Append(pipeName);

            m_process.StartInfo.Arguments = arguments.ToString();
            if (environment != null)
            {
                foreach (var kvp in environment)
                {
                    m_process.StartInfo.Environment.Add(kvp);
                }
            }

            m_process.StartInfo.UseShellExecute        = false;
            m_process.StartInfo.CreateNoWindow         = true;
            m_process.StartInfo.RedirectStandardError  = true;
            m_process.StartInfo.RedirectStandardOutput = true;
            m_process.StartInfo.RedirectStandardInput  = true;

            CommandLine = m_process.StartInfo.FileName + " " + m_process.StartInfo.Arguments;
        }