{"id":19065459,"url":"https://github.com/menci/simple-sandbox","last_synced_at":"2025-09-03T14:32:02.139Z","repository":{"id":21675330,"uuid":"89884725","full_name":"Menci/simple-sandbox","owner":"Menci","description":"A simple Linux sandbox with Node.js API.","archived":false,"fork":false,"pushed_at":"2024-07-01T05:35:52.000Z","size":159,"stargazers_count":73,"open_issues_count":11,"forks_count":22,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-23T16:12:01.440Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Menci.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-01T00:03:55.000Z","updated_at":"2024-09-13T09:45:16.000Z","dependencies_parsed_at":"2024-08-01T21:53:48.560Z","dependency_job_id":null,"html_url":"https://github.com/Menci/simple-sandbox","commit_stats":null,"previous_names":["t123yh/simple-sandbox"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Menci%2Fsimple-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Menci%2Fsimple-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Menci%2Fsimple-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Menci%2Fsimple-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Menci","download_url":"https://codeload.github.com/Menci/simple-sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231894027,"owners_count":18442090,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T00:50:04.205Z","updated_at":"2024-12-30T17:36:19.380Z","avatar_url":"https://github.com/Menci.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple-sandbox\nA simple linux sandbox with Node.js API. Used by SYZOJ.\n\n## Prerequisites\n### Packages\nYou need to have the `build-essentials` (`g++`, `make`, etc.) and the `fmt` library installed in your system in order to build the C++ part.\n\nThe minimal `g++` version required is `g++-8`. A newer version of `clang++` with C++17 file system support is recommended.\n\nInstall them by (in Ubuntu 18.04):\n```bash\napt install build-essential clang++-9 libfmt-dev\n```\n\n### Kernel\nYou need to have the memory swap account (disabled by default in Debian 8) enabled with your kernel. You can verify this by checking the existence of `/sys/fs/cgroup/memory/memory.memsw.usage_in_bytes` \n\nIf that file does not exist, then you may have to turn on that with your grub.\n\nAdd `swapaccount=1` to `GRUB_CMDLINE_LINUX_DEFAULT` section in `/etc/default/grub`.\n```bash\nGRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash cgroup_enable=memory swapaccount=1\"\n```\n\nSome distro [enables cgroup v2 by default in their new versions](https://rootlesscontaine.rs/getting-started/common/cgroup2/), including Arch Linux (since April 2021), Fedora (since 31) and Debian. If you cannot find the directory `/sys/fs/cgroup/memory/`, this is the case for you. In this case, you also need to add the parameter `systemd.unified_cgroup_hierarchy=0` to enable cgroup v1: \n\n```bash\nGRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash cgroup_enable=memory swapaccount=1 systemd.unified_cgroup_hierarchy=0\"\n```\n\nAnd then run\n```bash\nupdate-grub \u0026\u0026 reboot\n```\nTo enjoy the modified kernel.\n\n## Build\nPull the repository to somewhere on your computer and run\n\n```bash\nCXX=clang++-9 yarn install # Install required packages and compile C++ code with the clang++-9 compiler\nyarn run build # Compile typescript code.\n```\n\nTo build the project. If you don't want to use `yarn`, just change `yarn` to `npm`.\n\nYou can use `yarn run build:watch` to watch for the change of typescript file.\n\n## Use\nThe library is with a simple API.\nTo start the sandbox, use the following code:\n\n```js\nconst sandbox = require('simple-sandbox');\nconst myProcess = await sandbox.startSandbox(parameters);\n```\n\nwhere `parameters` is an instance of the [SandboxParameters interface](src/interfaces.ts). The detail explanation is available in the comments in that file.\n\nThe startSandbox function returns a Promise, from which you can get an instance of the [sandboxProcess](src/sandboxProcess.ts) class, reprensenting your sandboxed Process.\n\nTo terminate the sandboxed process, just use the `stop()` function:\n\n```js\nmyProcess.stop();\n```\n\nTo wait for the sandboxed process to end, use the `waitForStop()` function, which returns a `Promise`:\n\n```js\nmyProcess.waitForStop().then((result) =\u003e {\n    console.log(\"OK! \" + JSON.stringify(result));\n}).catch((err) =\u003e {\n    console.log(\"Whooops!\" + err.toString());\n});\n```\n\nNote that `myProcess` itself is a EventEmitter, so you can register `exit` (indicates that the child process exited), and `error` (indicates that some error happens) event listener on it.\n\n### Note\nWhen a sandbox is started, a event listener for the `exit` event on the `process` object is registered. When Node.js is about to exit, it will kill the sandboxed process.\n\nHowever, the `exit` event won't be called if there are `SIGTERM` or `SIGINT` (Ctrl-C) signals sent to the Node.js process. You should let the `SIGTERM` and `SIGINT` handler to call `process.exit()` on the main process:\n\n```js\nconst terminationHandler = () =\u003e {\n    process.exit(1);\n};\n\nprocess.on('SIGTERM', terminationHandler);\nprocess.on('SIGINT', terminationHandler);\n```\n\n## Example\nA demostration is available in the `demo` directory.\nIn order to get the demostration running for every one, we create the directory `/opt/sandbox-test`.\n\n```bash\nsudo mkdir /opt/sandbox-test\nsudo chown $(whoami) /opt/sandbox-test\nmkdir /opt/sandbox-test/rootfs /opt/sandbox-test/binary /opt/sandbox-test/working\ncurl -L https://nl.alpinelinux.org/alpine/v3.5/releases/x86_64/alpine-minirootfs-3.5.2-x86_64.tar.gz | tar -xzvf - -C /opt/sandbox-test/rootfs\n```\n\n**(to be continued)**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmenci%2Fsimple-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmenci%2Fsimple-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmenci%2Fsimple-sandbox/lists"}