Example #1
0
        public static JobObject Enable(this JobObject job, IsolationLimits limits, Action <string, object> onSet = null)
        {
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }

            if (limits == null)
            {
                throw new ArgumentNullException(nameof(limits));
            }

            job.Update(o =>
            {
                if (limits.MaxMemory > 0)
                {
                    o.ProcessMemoryLimit = limits.MaxMemory;
                    onSet?.Invoke(nameof(IsolationLimits.MaxMemory), limits.MaxMemory);
                }

                if (limits.MaxCpuUsage > 0)
                {
                    o.CpuRateLimit = limits.MaxCpuUsage;
                    onSet?.Invoke(nameof(IsolationLimits.MaxCpuUsage), limits.MaxCpuUsage);
                }

                if (limits.AffinityMask != IntPtr.Zero)
                {
                    o.ProcessorAffinity = limits.AffinityMask;
                    onSet?.Invoke(nameof(IsolationLimits.AffinityMask), limits.AffinityMask);
                }
            });

            return(job);
        }
Example #2
0
        /// <summary>
        /// Tries to kill the process and all child processes.
        /// </summary>
        /// <remarks>
        /// It's okay to call this method at any time; however, before process start and after process termination or disposing of
        /// this instance, it does nothing.
        /// </remarks>
        public void Kill(int exitCode)
        {
            // Notify the injected that the process is being killed
            m_processInjector?.OnKilled();
            LogDiagnostics($"Process will be killed with exit code {exitCode}");

            var processHandle = m_processHandle;

            if (processHandle != null && !processHandle.IsInvalid)
            {
                // Ignore result, as there is a race with regular process termination that we cannot do anything about.
                m_killed = true;
                // No job object means that we are on an old OS; let's just terminate this process (we can't reliably terminate all child processes)
                Analysis.IgnoreResult(Native.Processes.ProcessUtilities.TerminateProcess(processHandle, exitCode));
                LogDiagnostics("DetouredProcess is killed using TerminateProcess");
            }

            using (m_queryJobDataLock.AcquireWriteLock())
            {
                JobObject jobObject = m_job;
                if (jobObject != null)
                {
                    // Ignore result, as there is a race with regular shutdown.
                    m_killed = true;
                    Analysis.IgnoreResult(jobObject.Terminate(exitCode));

                    LogDiagnostics("DetouredProcess is killed via JobObject termination");
                }
            }

            LogDiagnostics("Stack trace:");
            LogDiagnostics(Environment.StackTrace);
        }
Example #3
0
    // Constructor
    public static JobObject CreateComponent(GameObject where, JobShopSchedulerObject jsso, int jobNum)
    {
        gameObjectRef = Instantiate(where);
        gameObjectRef.transform.parent = jsso.transform;

        JobObject job = gameObjectRef.AddComponent <JobObject>();

        job.m_JobShopSchedulerObject = jsso;
        job.m_Tasks         = new List <TaskObject>();
        job.m_NumberOfTasks = 0;
        job.m_TotalDuration = 0;
        job.m_StartTime     = 0;
        job.m_EndTime       = 0;
        job.m_JobID         = jobNum;

        gameObjectRef.name = "Job" + job.m_JobID;

        job.ColorRef = new Color(
            Random.Range(.25f, 1f),
            Random.Range(.25f, 1f),
            Random.Range(.25f, 1f)
            );

        return(job);
    }
Example #4
0
 public ProcessTracker(IMessageTransport transport, JobObject jobObject, IProcess hostProcess, ProcessHelper processHelper)
 {
     this.transport     = transport;
     this.jobObject     = jobObject;
     this.hostProcess   = hostProcess;
     this.processHelper = processHelper;
 }
Example #5
0
        private async Task <int> Run(HostCommandContext context,
                                     string pipeName,
                                     long maxMemory,
                                     int affinityMask,
                                     int maxCpu)
        {
            var currentProcess = SDProcess.GetCurrentProcess();

            var logger = context.ServiceProvider.GetService <ILoggerFactory>().CreateLogger <HostProcessImpl>();

            logger.HostProcessStart(currentProcess.Id);

            string name = "pihost." + currentProcess.Id;

            var limits = new IsolationLimits(maxMemory, maxCpu, affinityMask);

            if (limits.IsAnyEnabled)
            {
                s_jobObject = new JobObject(name);
                s_jobObject.AddProcess(currentProcess);
                s_jobObject.Enable(limits, (l, v) => logger.ProcessLimitSet(l, v));
            }

            var host = new IpcServiceHostBuilder(context.ServiceProvider)
                       .AddNamedPipeEndpoint <IHostProcess>(name, pipeName, includeFailureDetailsInResponse: true)
                       .Build();

            logger.IpcServiceHostStarting(pipeName);
            context.SetStartComplete();
            await host.RunAsync(context.Token).ConfigureAwait(false);

            return(0);
        }
        public void GuardDoesNotRetainReferenceToParentJobObject()
        {
            var st = new Stopwatch();

            st.Start();
            while (st.ElapsedMilliseconds < 5 * 1000)
            {
                try
                {
                    using (var gJobObject = new JobObject("Global\\" + username + "-guard", true))
                    {
                        break;
                    }
                }
                catch
                {
                    // retry
                }
            }

            Assert.False(guardProcess.HasExited);

            // Act
            this.jobObject.Dispose();

            // Assert
            guardProcess.WaitForExit(1000);
            Assert.True(guardProcess.HasExited);
        }
    public void CreateObjects()
    {
        m_JobObjects     = new List <JobObject>();
        m_MachineObjects = new List <MachineObject>();

        for (int y = 0; y < jsd.NumMachines; y++)
        {
            m_MachineObjects.Add(MachineObject.CreateComponent(m_MachineObjectRef, this, y));
            Transform machine = m_MachineObjects[y].transform.Find("Machine").GetComponent <Transform>();
            machine.position = machine.position + new Vector3(10, 0, UnityEngine.Random.Range(0, 200.0f));
        }

        for (int j = 0; j < jsd.NumJobs; j++)
        {
            JobObject currentJob = JobObject.CreateComponent(m_JobObjectRef, this, j);
            for (int t = 0; t < jsd.NumMachines; t++)
            {
                int machineIndex = jsd.JobTaskMachines[j][t];
                int duration     = jsd.JobTaskDurations[j][t];

                MachineObject currentMachine = m_MachineObjects[machineIndex];

                currentJob.AddTask(currentMachine, duration);
            }
            m_JobObjects.Add(currentJob);
            currentJob = null;
        }
    }
Example #8
0
        public ActionResult GetAllJobs(string search)
        {
            JobObject model       = new JobObject();
            Jobs      newjobsList = new Jobs();

            using (StreamReader rd = new StreamReader(@"c:\users\gontse.tauyane\documents\visual studio 2012\Projects\FlintnTinderJobApp\FlintnTinderJobApp\Jobs.json"))
            {
                string json = rd.ReadToEnd();
                model = Newtonsoft.Json.JsonConvert.DeserializeObject <JobObject>(json);
            }

            //Attempting to do the search
            if (!String.IsNullOrEmpty(search))
            {
                for (int i = 0; i < model.jobs.Count; i++)
                {
                    if (model.jobs[i].Client.IndexOf(search, StringComparison.OrdinalIgnoreCase) != -1)
                    {
                        newjobsList = model.jobs[i];
                    }
                }
            }
            else
            {
                return(View(model));
            }
            return(View(newjobsList));
        }
Example #9
0
        public async Task RunAsync()
        {
            // setup a soft CPU rate limit of 10% and notification for when it is
            // exceeded..

            var jobLimits        = new JobLimits();
            var jobNotifications = new JobNotifications();

            jobLimits.CpuRate        = new CpuRateLimit(10.0m, false);
            jobNotifications.CpuRate = new RateControl(RateControlInterval.Short, RateControlTolerance.Low);

            using (var jobObject = new JobObject(jobLimits, jobNotifications))
            {
                // assume our working directory is the output path for the project, and that we're in
                // debug configuration..

                jobObject.CpuRateLimitExceeded += (s, e) =>
                {
                    Console.WriteLine("CPU rate exceeded");
                };

                using (var process = jobObject.CreateProcess(new CreateProcessInfo
                {
                    FileName = @"..\..\..\BusyApp\bin\Debug\BusyApp.exe"
                }))
                {
                    await process.Exited;
                }
            }
        }
Example #10
0
        public void Destroy()
        {
            if (!this.isLocked)
            {
                throw new InvalidOperationException("This prison is not locked.");
            }

            Logger.Debug("Destroying prison {0}", this.ID);

            foreach (var cell in prisonCells)
            {
                cell.Destroy(this);
            }

            this.jobObject.TerminateProcesses(-1);

            this.jobObject.Dispose();
            this.jobObject = null;

            // TODO: Should destroy delete the home directory???
            // Directory.CreateDirectory(prisonRules.PrisonHomePath);

            this.TryStopGuard();
            this.UnloadUserProfileUntilReleased();
            this.DeleteUserProfile();
            this.user.Delete();

            SystemVirtualAddressSpaceQuotas.RemoveQuotas(new SecurityIdentifier(this.user.UserSID));

            this.DeletePersistedPrirson();
        }
Example #11
0
        public Container(
            string id,
            string handle,
            IContainerUser user,
            IContainerDirectory directory,
            IContainerPropertyService propertyService,
            ILocalTcpPortManager tcpPortManager,
            JobObject jobObject,
            DiskQuotaControl diskQuotaControl,
            IProcessRunner processRunner,
            IProcessRunner constrainedProcessRunner,
            ProcessHelper processHelper,
            Dictionary <string, string> defaultEnvironment,
            ContainerHostDependencyHelper dependencyHelper
            )
        {
            this.id                       = id;
            this.handle                   = handle;
            this.user                     = user;
            this.directory                = directory;
            this.propertyService          = propertyService;
            this.tcpPortManager           = tcpPortManager;
            this.jobObject                = jobObject;
            this.diskQuotaControl         = diskQuotaControl;
            this.processRunner            = processRunner;
            this.constrainedProcessRunner = constrainedProcessRunner;
            this.processHelper            = processHelper;
            this.dependencyHelper         = dependencyHelper;
            this.defaultEnvironment       = defaultEnvironment ?? new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            this.currentState = ContainerState.Active;
        }
Example #12
0
        private void InitializeJobObject()
        {
            if (this.jobObject != null)
            {
                return;
            }

            // Create the JobObject
            this.jobObject = new JobObject(this.user.Username);

            if (this.Rules.CPUPercentageLimit > 0)
            {
                this.JobObject.CPUPercentageLimit = this.Rules.CPUPercentageLimit;
            }

            if (this.Rules.TotalPrivateMemoryLimitBytes > 0)
            {
                this.JobObject.JobMemoryLimitBytes = this.Rules.TotalPrivateMemoryLimitBytes;
            }

            if (this.Rules.ActiveProcessesLimit > 0)
            {
                this.JobObject.ActiveProcessesLimit = this.Rules.ActiveProcessesLimit;
            }

            if (this.Rules.PriorityClass.HasValue)
            {
                this.JobObject.PriorityClass = this.Rules.PriorityClass.Value;
            }

            this.jobObject.KillProcessesOnJobClose = true;
        }
Example #13
0
 public void IsAssignedToProcess_NotAssociated()
 {
     using (var job = new JobObject())
     {
         Assert.False(job.IsAssignedToProcess(Process.GetCurrentProcess()));
     }
 }
Example #14
0
 public void SetUILimits()
 {
     using (var job = new JobObject())
     {
         job.SetUIRestrictions(Natives.JobObjectUILimit.ReadClipboard);
     }
 }
Example #15
0
        public void KillOnJobClose_ShouldKillProcessOnClose()
        {
            using (var job = new JobObject())
            {
                job.SetLimits(new JobObjectLimits()
                {
                    Flags = JobObjectLimitFlags.DieOnUnhandledException | JobObjectLimitFlags.KillOnJobClose,
                });

                var psi = new ProcessStartInfo
                {
                    FileName        = "ping",
                    Arguments       = "127.0.0.1 -n 100",
                    UseShellExecute = true,
                    CreateNoWindow  = true,
                };

                using (var process = Process.Start(psi))
                {
                    Assert.False(process.WaitForExit(500)); // Ensure process is started

                    job.AssignProcess(process);
                    job.Close();

                    process.WaitForExit();
                }
            }
        }
Example #16
0
            public void ProxiesToJobObject()
            {
                uint processLimit = 8765;

                this.Container.SetActiveProcessLimit(processLimit);
                JobObject.Received(1).SetActiveProcessLimit(processLimit);
            }
Example #17
0
 public IContainer CreateContainer(string id,
                                   string handle,
                                   IContainerUser user,
                                   IContainerDirectory directory,
                                   IContainerPropertyService propertyService,
                                   ILocalTcpPortManager tcpPortManager,
                                   JobObject jobObject,
                                   IContainerDiskQuota containerDiskQuota,
                                   IProcessRunner processRunner,
                                   IProcessRunner constrainedProcessRunner,
                                   ProcessHelper processHelper,
                                   Dictionary <string, string> defaultEnvironment,
                                   ContainerHostDependencyHelper dependencyHelper,
                                   BindMount[] bindMounts)
 {
     return(new Container(
                id,
                handle,
                user,
                directory,
                propertyService,
                tcpPortManager,
                jobObject,
                containerDiskQuota,
                processRunner,
                constrainedProcessRunner,
                processHelper,
                defaultEnvironment,
                dependencyHelper,
                bindMounts
                ));
 }
Example #18
0
        public void Dispose()
        {
            if (!m_disposed)
            {
                using (var semaphoreReleaser = m_syncSemaphore.AcquireSemaphore())
                {
                    StopWaiting(semaphoreReleaser);
                    WaitUntilErrorAndOutputEof(true, semaphoreReleaser).GetAwaiter().GetResult();

                    if (m_processInjector != null)
                    {
                        // We may have already called Stop() in CompletionCallback, but that's okay.
                        m_processInjector.Stop().GetAwaiter().GetResult();
                        m_processInjector.Dispose();
                        m_processInjector = null;
                    }

                    if (m_processHandle != null)
                    {
                        m_processHandle.Dispose();
                        m_processHandle = null;
                    }

                    if (m_job != null)
                    {
                        m_job.Dispose();
                        m_job = null;
                    }
                }

                m_syncSemaphore.Dispose();

                m_disposed = true;
            }
        }
Example #19
0
        /// <summary>
        /// Tries to kill the process and all child processes.
        /// </summary>
        /// <remarks>
        /// It's okay to call this method at any time; however, before process start and after process termination or disposing of
        /// this instance, it does nothing.
        /// </remarks>
        public void Kill(int exitCode)
        {
            // Notify the injected that the process is being killed
            m_processInjector?.OnKilled();

            var processHandle = m_processHandle;

            if (processHandle != null && !processHandle.IsInvalid)
            {
                // Ignore result, as there is a race with regular process termination that we cannot do anything about.
                m_killed = true;

                // No job object means that we are on an old OS; let's just terminate this process (we can't reliably terminate all child processes)
                Analysis.IgnoreResult(Native.Processes.ProcessUtilities.TerminateProcess(processHandle, exitCode));
            }

            JobObject jobObject = m_job;

            if (jobObject != null)
            {
                // Ignore result, as there is a race with regular shutdown.
                m_killed = true;

                Analysis.IgnoreResult(jobObject.Terminate(exitCode));
            }
        }
Example #20
0
            public void ProxiesToJobObject()
            {
                var priority = ProcessPriorityClass.RealTime;

                this.Container.SetPriorityClass(priority);
                JobObject.Received(1).SetPriorityClass(priority);
            }
Example #21
0
            public void ReturnsCpuLimit()
            {
                int weight = 7;

                JobObject.GetJobCpuLimit().Returns(weight);
                Assert.Equal(weight, Container.CurrentCpuLimit());
            }
Example #22
0
            public void ReturnsMemoryLimit()
            {
                ulong limitInBytes = 2048;

                JobObject.GetJobMemoryLimit().Returns(limitInBytes);
                Assert.Equal(limitInBytes, Container.CurrentMemoryLimit());
            }
Example #23
0
            public void WhenManagingMultipleProcesses()
            {
                const long oneProcessPrivateMemory = 1024;
                TimeSpan   expectedTotalKernelTime = TimeSpan.FromSeconds(2);
                TimeSpan   expectedTotalUserTime   = TimeSpan.FromSeconds(2);

                var expectedCpuStats = new CpuStatistics
                {
                    TotalKernelTime = expectedTotalKernelTime,
                    TotalUserTime   = expectedTotalUserTime,
                };

                var firstProcess = Substitute.For <IProcess>();

                firstProcess.Id.Returns(1);
                firstProcess.PrivateMemoryBytes.Returns(oneProcessPrivateMemory);

                var secondProcess = Substitute.For <IProcess>();

                secondProcess.Id.Returns(2);
                secondProcess.PrivateMemoryBytes.Returns(oneProcessPrivateMemory);

                JobObject.GetCpuStatistics().Returns(expectedCpuStats);
                JobObject.GetProcessIds().Returns(new int[] { 1, 2 });

                ProcessHelper.GetProcesses(null).ReturnsForAnyArgs(new[] { firstProcess, secondProcess });

                var metrics = Container.GetMetrics();

                Assert.Equal(expectedTotalKernelTime + expectedTotalUserTime, metrics.CpuStat.TotalProcessorTime);
                Assert.Equal((ulong)firstProcess.PrivateMemoryBytes + (ulong)secondProcess.PrivateMemoryBytes, metrics.MemoryStat.PrivateBytes);
            }
Example #24
0
            public void DisposesJobObject_ThisEnsuresWeCanDeleteTheDirectory()
            {
                Container.Destroy();

                JobObject.Received(1).TerminateProcessesAndWait();
                JobObject.Received(1).Dispose();
            }
Example #25
0
        public async Task <IActionResult> PostNewJob([FromBody] string sourceText)
        {
            ServiceEventSource.Current.ServiceMessage(this.context_, "Orchestrator param: " + sourceText);
            CancellationToken ct = new CancellationToken();

            var jobsDictionary = await this.stateManager_.GetOrAddAsync <IReliableDictionary <Guid, JobObject> >("jobsDictionary");

            var tasksQueue = await this.stateManager_.GetOrAddAsync <IReliableConcurrentQueue <MapTask> >("tasksQueue");

            var allTasks = await this.stateManager_.GetOrAddAsync <IReliableDictionary <Guid, MapTask> >("allTasks");

            using (ITransaction tx = this.stateManager_.CreateTransaction())
            {
                Guid      jobGuid = System.Guid.NewGuid();
                JobObject job     = new JobObject(jobGuid);
                MapTask   task;

                int cursorPos = 0;
                while (cursorPos + TASK_SIZE < sourceText.Length)
                {
                    int nextCursorPos = cursorPos + TASK_SIZE;
                    while (sourceText[nextCursorPos] != ' ')
                    {
                        nextCursorPos--;
                    }

                    task       = new MapTask(jobGuid, sourceText.Substring(cursorPos, nextCursorPos - cursorPos));
                    task.State = MapTask.StateType.InQueue;
                    job.Tasks.Add(task.Uuid, false);

                    await allTasks.AddAsync(tx, task.Uuid, task);

                    await tasksQueue.EnqueueAsync(tx, task, ct);

                    cursorPos = nextCursorPos + 1;
                }
                task       = new MapTask(jobGuid, sourceText.Substring(cursorPos));
                task.State = MapTask.StateType.InQueue;
                job.Tasks.Add(task.Uuid, false);

                await allTasks.AddAsync(tx, task.Uuid, task);

                await tasksQueue.EnqueueAsync(tx, task, ct);

                try
                {
                    await jobsDictionary.AddAsync(tx, job.Uuid, job, TimeSpan.FromSeconds(1), ct);
                }
                catch (Exception e)
                {
                    ServiceEventSource.Current.ServiceMessage(this.context_, e.Message);
                }


                await tx.CommitAsync();

                return(new OkResult());
            }
        }
Example #26
0
    public static Process ExecuteInJob(JobObject jobObject, string command, params object[] args)
    {
        var process = ExecuteWithWait(command, args);

        jobObject.AssignProcessToJob(process);
        ContinueAndWait(process);
        return(process);
    }
    //Confusion, what is this for again?
    void LoadJob(int choice)
    {
        Debug.Log("Loading job.");
        JobObject loadedObjectScript = loadedJob.GetComponent <JobObject> ();

        loadedObjectScript.currentJob = allJobs.jobs [choice];
        loadedObjectScript.jobBoard   = jobBoard;
    }
 public void SetAllJobSelectability(bool newValue)
 {
     foreach (GameObject jobObject in jobObjectList)
     {
         JobObject thisJobObject = jobObject.GetComponent <JobObject> ();
         thisJobObject.SetSelectability(newValue);
         //Debug.Log ("Set accessible.");
     }
 }
Example #29
0
        public void IsAssignedToProcess_Associated()
        {
            using var job = new JobObject();
            var process = Process.GetCurrentProcess();

            job.AssignProcess(process);

            Assert.True(job.IsAssignedToProcess(process));
        }
        public async Task RunAsync()
        {
            using (var jobObject = new JobObject())
            {
                // setup some limits, we do this before starting any processes, but it's not
                // required..

                var jobLimits = new JobLimits();
                jobLimits.CpuRate         = new CpuRateLimit(10.0m, false);
                jobLimits.Options         = JobOptions.TerminateProcessesWhenJobClosed;
                jobLimits.ActiveProcesses = 3;
                jobObject.SetLimits(jobLimits);

                // setup a few event handlers..
                jobObject.ProcessAdded         += (s, e) => Console.WriteLine($"Process {e.ID} added.");
                jobObject.ProcessExited        += (s, e) => Console.WriteLine($"Process {e.ID} exited.");
                jobObject.ProcessLimitExceeded += (s, e) => Console.WriteLine("Process limit exceeded.");
                jobObject.CpuRateLimitExceeded += (s, e) => Console.WriteLine("CPU rate limit exceeded.");

                // configure CPU rate notification..
                var jobNotifications = new JobNotifications();
                jobNotifications.CpuRate = new RateControl(RateControlInterval.Short, RateControlTolerance.Low);

                // start some ping processes...
                var cpiPing = new CreateProcessInfo
                {
                    FileName      = "ping.exe",
                    ArgumentsList = { "8.8.8.8", "-t" }
                };

                // create ping processes, directly through the JobObject, this will create the process
                // and associate the process with the JobObject ..
                for (int iIndex = 0; iIndex < 10; iIndex++)
                {
                    try
                    {
                        using (var process = jobObject.CreateProcess(cpiPing))
                            Console.WriteLine($"Created Process {process.Id}");
                    }
                    catch (System.ComponentModel.Win32Exception ex) when(ex.NativeErrorCode == ERROR_NOT_ENOUGH_QUOTA)
                    {
                        // ERROR_NOT_ENOUGH_QUOTA happens if the process cannot be assigned to the job object
                        // because of the active process limit..

                        Console.WriteLine("JobObject.CreateProcess failed, due to process limit.");
                    }
                }

                Console.WriteLine("[enter] to terminate active processes.");
                Console.ReadLine();
                jobObject.Kill();

                // wait for JobObject to become idle..
                await jobObject.Idle;
            }
        }
Example #31
0
 public JobInfo CreateBulkUpsertJob(JobObject jobObject, string externalIdFieldName)
 {
     var jobInfo = new JobInfo
     {
         ContentType = ContentType.XML,
         Operation = Operation.upsert,
         JobObject = jobObject,
         ExternalIdFieldName = externalIdFieldName
     };
     var jobResponse = CreateJob(jobInfo);
     return jobResponse;
 }
        public void Start(int timeLimit, int memoryLimit)
        {
            try
            {
                this.jobObject = new JobObject();
                this.jobObject.SetExtendedLimitInformation(PrepareJobObject.GetExtendedLimitInformation(timeLimit * 2, memoryLimit * 2));
                this.jobObject.SetBasicUiRestrictions(PrepareJobObject.GetUiRestrictions());
                this.jobObject.AddProcess(this.processInformation.Process);

                NativeMethods.ResumeThread(this.processInformation.Thread);
            }
            catch (Win32Exception)
            {
                this.Kill();
                throw;
            }
        }