1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#!/usr/bin/env bun
import { cstr, libc, localeQuotedEscapedDiagnostic, parseOptions } from "../shared/common.js";
import { UsageError } from "../shared/diagnostics.js";
import { ensureSpecialFileCreatable, mknodDiagnosticName, normalizeSpecialFileLongOptions, parseSpecialFileCreationMode, selinuxCreationOptions, setFileMode, specialFileMetaOption, specialFileQuotedName, withSelinuxCreationContext } from "../shared/filesystem.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export async function mknodCmd(args) {
args = normalizeSpecialFileLongOptions(args);
const { opts, operands } = parseOptions(args, { short: { m: "value", Z: false }, long: { mode: "value", context: "optional-value", help: false, version: false } });
const securityContext = selinuxCreationOptions("mknod", opts);
const explicitMode = opts.m ?? opts.mode;
const perm = explicitMode ? parseSpecialFileCreationMode(explicitMode) : 0o666;
if (!operands.length) throw new UsageError("missing operand", true);
if (operands.length === 1) throw new UsageError(`missing operand after '${operands[0]}'`, true);
const [path, type, major, minor] = operands;
if (type === "p") {
if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true);
await withSelinuxCreationContext("mknod", securityContext, async (created) => {
await ensureSpecialFileCreatable("mknod", path, "fifo");
if (libc.symbols.mkfifo(cstr(path), perm) !== 0) throw new UsageError(`cannot create fifo ${specialFileQuotedName(path)}: Operation not permitted`);
if (explicitMode) await setFileMode(path, perm);
await created(path);
});
return 0;
}
if (operands.length > 4) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[4])}`, true);
if (major == null) throw new UsageError(`missing operand after '${type}'\nSpecial files require major and minor device numbers.`, true);
if (minor == null) throw new UsageError(`missing operand after '${major}'`, true);
if (type === "b" || type === "c" || type === "u") {
const majorNumber = parseMknodDeviceNumber(major, "major");
const minorNumber = parseMknodDeviceNumber(minor, "minor");
await withSelinuxCreationContext("mknod", securityContext, async (created) => {
await ensureSpecialFileCreatable("mknod", path, "special file");
const modeType = type === "b" ? 0o060000 : 0o020000;
if (libc.symbols.mknod(cstr(path), modeType | perm, makedev(majorNumber, minorNumber)) !== 0) throw new UsageError(`${mknodDiagnosticName(path)}: Operation not permitted`);
await created(path);
});
return 0;
}
throw new UsageError(`invalid device type ${localeQuotedEscapedDiagnostic(type)}`, true);
}
export function parseMknodDeviceNumber(value, kind) {
const text = String(value);
let digits;
let base;
if (/^0[xX][0-9A-Fa-f]+$/.test(text)) {
digits = text.slice(2);
base = 16n;
} else if (/^0[0-7]*$/.test(text)) {
digits = text;
base = 8n;
} else if (/^[1-9][0-9]*$/.test(text)) {
digits = text;
base = 10n;
} else {
throw new UsageError(`invalid ${kind} device number ${localeQuotedEscapedDiagnostic(value)}`);
}
let number = 0n;
for (const ch of digits) {
number = number * base + BigInt(Number.parseInt(ch, Number(base)));
if (number > 0xffffffffn) throw new UsageError(`invalid ${kind} device number ${localeQuotedEscapedDiagnostic(value)}`);
}
return number;
}
export function makedev(major, minor) {
return Number(((major & 0xfffn) << 8n) | (minor & 0xffn) | ((minor & ~0xffn) << 12n) | ((major & ~0xfffn) << 32n));
}
const singleCall = defineCommand("mknod", mknodCmd, (args) => specialFileMetaOption("mknod", args));
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|