gecko-dev/toolkit/modules/subprocess/subprocess_shared_unix.js
Jed Davis 996eb87d1d Bug 1276388 - Use IPC process launching for Subprocess.jsm on Unix. r=kmag,nika,barret
Currently, Subprocess.jsm on Unix uses js-ctypes to call `posix_spawn`.
This has some issues, primarily that file descriptors are inherited by
the child process unless explicitly opted-out, which unfortunately a lot
of code doesn't do.  This patch changes it to use IPC process launching,
where fd inheritance is opt-in, by:

1. Extending `base::LaunchApp` to handle a few features that Subprocess
   needs (setting the process's working directory, specifying the full
   environment, and the macOS `disclaim` flag)

2. Adding a WebIDL method to `IOUtils` to expose that function to JS
   (currently Unix-only; Windows could also be supported but it would
   probably want to use a different IDL type for strings, given that the
   OS APIs use 16-bit code units).

3. Replacing the part of Subprocess that invokes `posix_spawn` (and
   related functions) by calling that method; the rest of Subprocess's
   machinery to manage pipes and I/O is unchanged.  (The Windows backend
   is also unchanged; I'm not aware of any functional issues there.)
   This results in some dead code, which is removed.

Differential Revision: https://phabricator.services.mozilla.com/D152336
2022-08-09 17:32:22 +00:00

113 lines
2.4 KiB
JavaScript

/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
/* exported LIBC, libc */
// This file is loaded into the same scope as subprocess_unix.jsm
/* import-globals-from subprocess_shared.js */
/* import-globals-from subprocess_unix.jsm */
var LIBC = OS.Constants.libc;
const LIBC_CHOICES = ["libc.so", "libSystem.B.dylib", "a.out"];
const unix = {
pid_t: ctypes.int32_t,
pollfd: new ctypes.StructType("pollfd", [
{ fd: ctypes.int },
{ events: ctypes.short },
{ revents: ctypes.short },
]),
WEXITSTATUS(status) {
return (status >> 8) & 0xff;
},
WTERMSIG(status) {
return status & 0x7f;
},
};
var libc = new Library("libc", LIBC_CHOICES, {
environ: [ctypes.char.ptr.ptr],
// Darwin-only.
_NSGetEnviron: [ctypes.default_abi, ctypes.char.ptr.ptr.ptr],
setenv: [
ctypes.default_abi,
ctypes.int,
ctypes.char.ptr,
ctypes.char.ptr,
ctypes.int,
],
chdir: [ctypes.default_abi, ctypes.int, ctypes.char.ptr /* path */],
close: [ctypes.default_abi, ctypes.int, ctypes.int /* fildes */],
fcntl: [
ctypes.default_abi,
ctypes.int,
ctypes.int /* fildes */,
ctypes.int /* cmd */,
ctypes.int /* ... */,
],
getcwd: [
ctypes.default_abi,
ctypes.char.ptr,
ctypes.char.ptr /* buf */,
ctypes.size_t /* size */,
],
kill: [
ctypes.default_abi,
ctypes.int,
unix.pid_t /* pid */,
ctypes.int /* signal */,
],
pipe: [ctypes.default_abi, ctypes.int, ctypes.int.array(2) /* pipefd */],
poll: [
ctypes.default_abi,
ctypes.int,
unix.pollfd.array() /* fds */,
ctypes.unsigned_int /* nfds */,
ctypes.int /* timeout */,
],
read: [
ctypes.default_abi,
ctypes.ssize_t,
ctypes.int /* fildes */,
ctypes.char.ptr /* buf */,
ctypes.size_t /* nbyte */,
],
waitpid: [
ctypes.default_abi,
unix.pid_t,
unix.pid_t /* pid */,
ctypes.int.ptr /* status */,
ctypes.int /* options */,
],
write: [
ctypes.default_abi,
ctypes.ssize_t,
ctypes.int /* fildes */,
ctypes.char.ptr /* buf */,
ctypes.size_t /* nbyte */,
],
});
unix.Fd = function(fd) {
return ctypes.CDataFinalizer(ctypes.int(fd), libc.close);
};