Skip to content

Commit

Permalink
add non-linux check
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzha0 committed Aug 26, 2024
1 parent 231d698 commit ae0e06a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 16 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
function getOpenFds(): FdRecord {
Expand Down Expand Up @@ -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<void>((done) => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit ae0e06a

Please sign in to comment.