diff --git a/src/lib.rs b/src/lib.rs index 8a2a508..8745238 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,12 +164,20 @@ impl Pty { } // set the cgroup if specified + #[cfg(target_os = "linux")] if let Some(cgroup_path) = &opts.cgroup_path { let pid = libc::getpid(); let cgroup_path = format!("{}/cgroup.procs", cgroup_path); let mut cgroup_file = File::create(cgroup_path)?; cgroup_file.write_all(format!("{}", pid).as_bytes())?; } + #[cfg(not(target_os = "linux"))] + if opts.cgroup_path.is_some() { + return Err(Error::new( + ErrorKind::Other, + "cgroup_path is only supported on Linux", + )); + } // become the controlling tty for the program let err = libc::ioctl(raw_user_fd, libc::TIOCSCTTY.into(), 0); diff --git a/tests/index.test.ts b/tests/index.test.ts index 277a872..7f5178b 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -11,6 +11,7 @@ const procSelfFd = '/proc/self/fd/'; const IS_DARWIN = process.platform === 'darwin'; const testSkipOnDarwin = IS_DARWIN ? test.skip : test; +const testOnlyOnDarwin = IS_DARWIN ? test : test.skip; type FdRecord = Record; function getOpenFds(): FdRecord { @@ -419,10 +420,8 @@ describe( describe('cgroup opts', () => { testSkipOnDarwin('basic cgroup', async () => { - // create a new cgroup + // create a new cgroup with the right permissions await exec("sudo cgcreate -g 'cpu:/test.slice'") - - // Set permissions for the test user await exec("sudo chown -R $(id -u):$(id -g) /sys/fs/cgroup/cpu/test.slice") return new Promise((done) => { @@ -447,6 +446,20 @@ describe('cgroup opts', () => { }); }); }); + + testOnlyOnDarwin('cgroup is not supported on darwin', () => { + expect(() => { + new Pty({ + command: '/bin/cat', + args: ['/proc/self/cgroup'], + cgroupPath: '/sys/fs/cgroup/cpu/test.slice', + onExit: (err, exitCode) => { + expect(err).toBeNull(); + expect(exitCode).toBe(0); + }, + }) + }).toThrowError(); + }); }); describe('setCloseOnExec', () => {