public void Attach(ProcessPrisonCreateInfo createInfo) { if (createInfo.Id == null) { throw new ArgumentException("Id from createInfo is null", "createInfo"); } if (createInfo.WindowsPassword == null) { throw new ArgumentException("WindowsPassword from createInfo is null", "createInfo"); } this.Id = createInfo.Id; this.createInfo = createInfo; // The Job Object will disapear after a reboot or if all job's processes exit. // It is fine if it is created again with the same name id if the Job doesn't exist. try { // try only to attach and fail if it doesn't exist this.jobObject = JobObject.Attach(JobObjectNamespace() + this.Id); } catch (Win32Exception) { // try to create the job Id; this.jobObject = new JobObject(JobObjectNamespace() + this.Id); } this.WindowsPassword = this.createInfo.WindowsPassword; this.WindowsUsername = GenerateDecoratedUsername(this.Id); if (this.createInfo.DiskQuotaBytes > -1) { userQuota = DiskQuotaManager.GetDiskQuotaUser(DiskQuotaManager.GetVolumeRootFromPath(this.createInfo.DiskQuotaPath), this.WindowsUsername); } this.Created = true; }
public void Create(ProcessPrisonCreateInfo createInfo) { if (createInfo.Id == null) { this.Id = GenerateSecureGuid().ToString(); } else { this.Id = createInfo.Id; } string[] keys = new string[] { "ALLUSERSPROFILE", "APPDATA", "CommonProgramFiles", "CommonProgramFiles(x86)", "CommonProgramW6432", "COMPUTERNAME", "HOMEDRIVE", "LOCALAPPDATA", "NUMBER_OF_PROCESSORS", "OS", "Path", "PROCESSOR_ARCHITECTURE", "PROCESSOR_IDENTIFIER", "PROCESSOR_LEVEL", "PROCESSOR_REVISION", "ProgramData", "ProgramFiles", "ProgramFiles(x86)", "ProgramW6432", "PROMPT", "PSModulePath", "SystemDrive", "SystemRoot", "windir" }; this.myenvvars["HOMEPATH"] = createInfo.DiskQuotaPath; this.myenvvars["TEMP"] = Path.Combine(createInfo.DiskQuotaPath, "tmp"); this.myenvvars["TMP"] = Path.Combine(createInfo.DiskQuotaPath, "tmp"); foreach (string key in keys) { this.myenvvars[key] = Environment.GetEnvironmentVariable(key); } this.createInfo = createInfo; this.jobObject = new JobObject(JobObjectNamespace() + this.Id); this.jobObject.ActiveProcessesLimit = this.createInfo.RunningProcessesLimit; this.jobObject.JobMemoryLimitBytes = this.createInfo.TotalPrivateMemoryLimitBytes; this.jobObject.KillProcessesOnJobClose = this.createInfo.KillProcessesrOnPrisonClose; if (this.createInfo.WindowsPassword == null) { this.WindowsPassword = GenerateSecurePassword(40); } else { this.WindowsPassword = this.createInfo.WindowsPassword; } this.WindowsUsername = CreateDecoratedUser(this.Id, this.WindowsPassword); if (this.createInfo.DiskQuotaBytes > -1) { if (string.IsNullOrEmpty(this.createInfo.DiskQuotaPath)) { // set this.createInfo.DiskQuotaPath to the output of GetUserProfileDirectory throw new NotImplementedException(); } // Set the disk quota to 0 for all disks, exept disk quota path var volumesQuotas = DiskQuotaManager.GetDisksQuotaUser(this.WindowsUsername); foreach (var volumeQuota in volumesQuotas) { volumeQuota.QuotaLimit = 0; } userQuota = DiskQuotaManager.GetDiskQuotaUser(DiskQuotaManager.GetVolumeRootFromPath(this.createInfo.DiskQuotaPath), this.WindowsUsername); userQuota.QuotaLimit = this.createInfo.DiskQuotaBytes; } if (this.createInfo.UrlPortAccess > 0) { UrlsAcl.AddPortAccess(this.createInfo.UrlPortAccess, this.WindowsUsername); } if (this.createInfo.NetworkOutboundRateLimitBitsPerSecond > 0) { NetworkQos.CreateOutboundThrottlePolicy(this.WindowsUsername, this.WindowsUsername, this.createInfo.NetworkOutboundRateLimitBitsPerSecond); if (this.createInfo.UrlPortAccess > 0) { NetworkQos.CreateOutboundThrottlePolicy(this.createInfo.UrlPortAccess.ToString(), this.createInfo.UrlPortAccess, this.createInfo.NetworkOutboundRateLimitBitsPerSecond); } } this.Created = true; }