• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkgxdev / pkgx / 10947034918

19 Sep 2024 06:28PM UTC coverage: 92.72% (-0.02%) from 92.742%
10947034918

push

github

mxcl
Pass `--unstable-process`

Fixes #1038

397 of 427 branches covered (92.97%)

1488 of 1606 relevant lines covered (92.65%)

162.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.51
/src/utils/execve.ts
1
import { ProgrammerError } from "./error.ts"
6✔
2
import { utils, Path, PkgxError } from "pkgx"
6✔
3
const { host } = utils
6✔
4

5
export default function({cmd: args, env}: {cmd: string[], env: Record<string, string>}): never {
6✔
6
  /// we need to forward any other env, some vars like HOME are super important
24✔
7
  /// but otherwise the user has set stuff so we should use it
24✔
8
  for (const [key, value] of Object.entries(Deno.env.toObject())) {
24✔
9
    env[key] ??= value
2,166✔
10
  }
2,166✔
11

12
  find_in_PATH(args, env.PATH, Path.abs(env.HOME))
24✔
13

14
  const path = cstr(args[0])
24✔
15
  const argv = new CStringArray(args)
24✔
16
  const envp = new CStringArray(Object.entries(env).map(([key, value]) => `${key}=${value}`))
24✔
17

18
  const errno = (() => {
24✔
19
    let tries = 10;
42✔
20
    while (true) {
42✔
21
      const errno = _internals.execve(
42✔
22
        Deno.UnsafePointer.of(path),
42✔
23
        Deno.UnsafePointer.of(argv),
42✔
24
        Deno.UnsafePointer.of(envp)
42✔
25
      );
26
      if (!tries--) return errno;
×
27
      // 11 = EAGAIN = Try again
42✔
28
      if (errno == 11) continue;
×
29
      return errno;
42✔
30
    }
42✔
31
  })();
24✔
32

33
  switch (errno) {
24✔
34
    case 2:   //ENOENT:
24✔
35
    case 316: //FIXME ALERT! ALERT! BUG! SOMETHING IS WRONG WITH OUR USE OR ERRNO AND THIS SOMETIMES RESULTS, USUALLY ON MACOS :/
24✔
36
      // yes: strange behavior from execve here indeed
32✔
37
      if (parse_Path(args[0])?.exists()) {
32✔
38
        throw new Deno.errors.PermissionDenied()
36✔
39
      } else {
36✔
40
        throw new Deno.errors.NotFound()
36✔
41
      }
36✔
42
    case 13:
24✔
43
      throw new Deno.errors.PermissionDenied()
26✔
44
    case 63: //ENAMETOOLONG:
12✔
45
    case 7:  //E2BIG:
12✔
46
    case 14: //EFAULT:
12✔
47
    case 5:  //EIO:
12✔
48
    case 62: //ELOOP:
12✔
49
    case 8:  //ENOEXEC:
12✔
50
    case 12: //ENOMEM:
12✔
51
    case 20: //ENOTDIR:
12✔
52
    case 26: //ETXTBSY:
12✔
53
      throw new PkgxError(`execve (${errno})`)
13✔
54
  }
24✔
55

56
  throw new ProgrammerError(`execve (${errno})`)
31✔
57
}
24✔
58

59
export function parse_Path(input: string) {
6✔
60
  const p = Path.abs(input)
14✔
61
  if (p) return p
14✔
62
  try {
18✔
63
    return _internals.getcwd().join(input)
18✔
64
  } catch {
14✔
65
    return
16✔
66
  }
16✔
67
}
14✔
68

69
function find_in_PATH(cmd: string[], PATH: string | undefined, HOME: Path | undefined) {
24✔
70
  if (cmd[0].startsWith("/")) {
24✔
71
    return
30✔
72
  }
30✔
73
  if (cmd[0].includes("/")) {
24✔
74
    cmd[0] = _internals.getcwd().join(cmd[0]).string
26✔
75
    return
26✔
76
  }
26✔
77

78
  PATH ??= "/usr/bin:/bin"  // see manpage for execvp(3)
34✔
79

80
  for (const part of PATH.split(':')) {
24✔
81
    const path = (() => {
192✔
82
      if (part == '.') return _internals.getcwd()
360✔
83
      if (part == '~') return HOME
360✔
84
      if (part.startsWith('~/')) return HOME?.join(part.slice(2))
360✔
85
      //FIXME: not handled: ~user/...
522✔
86
      return new Path(part)
522✔
87
    })()?.join(cmd[0])
192✔
88
    if (path?.isExecutableFile()) {
192✔
89
      cmd[0] = path.string
194✔
90
      return
194✔
91
    }
194✔
92
  }
192✔
93
}
24✔
94

95

96
////// COPY PASTA
2✔
97
// actually minor mods to add trailing null
2✔
98
// https://github.com/aapoalas/libclang_deno/blob/main/lib/utils.ts
2✔
99

100
const ENCODER = new TextEncoder();
6✔
101

102
export class CStringArray extends Uint8Array {
6✔
103
  constructor(strings: string[]) {
6✔
104
    let stringsLength = 0;
42✔
105
    for (const string of strings) {
42✔
106
      // maximum bytes for a utf8 string is 4×length
2,281✔
107
      stringsLength += string.length * 4 + 1;
2,281✔
108
    }
2,281✔
109
    super(8 * (strings.length + 1) + stringsLength);
42✔
110
    const pointerBuffer = new BigUint64Array(this.buffer, 0, strings.length + 1);
42✔
111
    const stringsBuffer = new Uint8Array(this.buffer).subarray(
42✔
112
      (strings.length + 1) * 8,
42✔
113
    );
114
    const basePointer = BigInt(
42✔
115
      Deno.UnsafePointer.value(Deno.UnsafePointer.of(stringsBuffer)),
42✔
116
    );
117
    let index = 0;
42✔
118
    let offset = 0;
42✔
119
    for (const string of strings) {
42✔
120
      const start = offset;
2,281✔
121
      const result = ENCODER.encodeInto(
2,281✔
122
        string,
2,281✔
123
        stringsBuffer.subarray(start),
2,281✔
124
      );
125
      offset = start + result.written + 1; // Leave null byte
2,281✔
126
      pointerBuffer[index++] = basePointer + BigInt(start);
2,281✔
127
    }
2,281✔
128
  }
42✔
129
}
6✔
130

131
export const cstr = (string: string): Uint8Array =>
6✔
132
  ENCODER.encode(`${string}\0`);
6✔
133

134
function execve(arg0: Deno.PointerValue, args: Deno.PointerValue, env: Deno.PointerValue) {
18✔
135
  const filename = host().platform == 'darwin' ? '/usr/lib/libSystem.dylib' : 'libc.so.6'
×
136

137
  const libc = Deno.dlopen(
18✔
138
    filename, {
18✔
139
      execve: {
18✔
140
        parameters: ["pointer", "pointer", "pointer"],
90✔
141
        result: "i32"
18✔
142
      },
18✔
143
      errno: {
18✔
144
        type: "i32"
18✔
145
      }
18✔
146
    }
18✔
147
  )
148

149
  try {
18✔
150
    libc.symbols.execve(arg0, args, env)
18✔
151
    return libc.symbols.errno
18✔
152
  } finally {
18✔
153
    libc.close()
18✔
154
  }
18✔
155
}
18✔
156

157
export const _internals = {
6✔
158
  execve,
6✔
159
  getcwd: Path.cwd
6✔
160
}
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc