#define _GNU_SOURCE #include #include #include #include #include #include #include #include #ifndef PATH_MAX #define PATH_MAX 4096 #endif static void fail(const char *message, const char *path) { if (path) fprintf(stderr, "provisioning launcher: %s: %s: %s\n", message, path, strerror(errno)); else fprintf(stderr, "provisioning launcher: %s\n", message); exit(126); } /* launcher.c -- the static launcher every relocatable bundle embeds (relocate.mjs prepareTree replaces each dynamic executable with a copy of this; the payload stays beside it as .provisioning-real-). It finds its _tree via /proc/self/exe, prepends the tree's command dirs to PATH, applies the optional /.provisioning/env (a recipe's `env` lines: KEY=VALUE, KEY+=VALUE, $ROOT/$SELF), then execs the bundled musl loader on the payload -- or, for a shebang script, its interpreter. Nothing absolute is baked in; the bundle can move. Under the bundled loader a payload sees /proc/self/exe as the LOADER: a runtime that re-executes itself (node's fork, a spawn of its own execPath) needs its env lines to point that path back at the launcher (the node recipe sets PROVISIONING_EXE to $SELF and preloads the fix). Build: relocate.mjs ensureLauncher() compiles it (-static -Os) with the folder's first compiler when the binary is missing; only this source ships. */ static int regular_file(const char *path) { struct stat value; return stat(path, &value) == 0 && S_ISREG(value.st_mode); } static char *join2(const char *left, const char *right) { size_t left_length = strlen(left); size_t right_length = strlen(right); int slash = left_length && left[left_length - 1] != '/'; char *result = malloc(left_length + slash + right_length + 1); if (!result) fail("out of memory", NULL); memcpy(result, left, left_length); if (slash) result[left_length++] = '/'; memcpy(result + left_length, right, right_length + 1); return result; } static char *directory_name(const char *path) { char *result = strdup(path); char *slash; if (!result) fail("out of memory", NULL); slash = strrchr(result, '/'); if (!slash) strcpy(result, "."); else if (slash == result) result[1] = '\0'; else *slash = '\0'; return result; } static const char *base_name(const char *path) { const char *slash = strrchr(path, '/'); return slash ? slash + 1 : path; } static char *read_small_file(const char *path) { int descriptor = open(path, O_RDONLY); char *result; ssize_t count; if (descriptor < 0) return NULL; result = malloc(PATH_MAX * 4); if (!result) fail("out of memory", NULL); count = read(descriptor, result, PATH_MAX * 4 - 1); close(descriptor); if (count < 0) { free(result); return NULL; } result[count] = '\0'; while (count > 0 && (result[count - 1] == '\n' || result[count - 1] == '\r')) result[--count] = '\0'; return result; } static char *find_root(const char *self) { char *current = directory_name(self); for (;;) { char *metadata = join2(current, ".provisioning/runtime.json"); int found = regular_file(metadata); free(metadata); if (found) return current; if (!strcmp(current, "/") || !strcmp(current, ".")) break; char *parent = directory_name(current); free(current); current = parent; } free(current); fail("could not locate .provisioning/runtime.json above executable", self); return NULL; } static char *absolute_list(const char *root, const char *relative_list) { size_t capacity = strlen(relative_list) + (strlen(root) + 1) * 64 + 1; char *result = malloc(capacity); const char *cursor = relative_list; size_t used = 0; if (!result) fail("out of memory", NULL); while (*cursor) { const char *end = strchr(cursor, '\n'); size_t length = end ? (size_t)(end - cursor) : strlen(cursor); while (length && (cursor[length - 1] == '\r' || cursor[length - 1] == ' ' || cursor[length - 1] == '\t')) length--; while (length && (*cursor == ' ' || *cursor == '\t')) { cursor++; length--; } if (length) { size_t needed = used + (used ? 1 : 0) + strlen(root) + 1 + length + 1; if (needed > capacity) { capacity = needed * 2; result = realloc(result, capacity); if (!result) fail("out of memory", NULL); } if (used) result[used++] = ':'; memcpy(result + used, root, strlen(root)); used += strlen(root); result[used++] = '/'; memcpy(result + used, cursor, length); used += length; } if (!end) break; cursor = end + 1; } result[used] = '\0'; return result; } static void configure_environment(const char *root) { const char *old_path = getenv("PATH"); size_t root_length = strlen(root); size_t old_length = old_path ? strlen(old_path) : 0; char *path = malloc(root_length * 4 + old_length + 48); char *toolchain_marker = join2(root, ".provisioning/toolchain"); char *prefix = join2(root, "usr/bin:"); if (!path) fail("out of memory", NULL); /* Idempotent: a payload that re-executes itself (node fork chains) re-enters this launcher; PATH must not grow four entries per level. */ if (!(old_path && !strncmp(old_path, prefix, strlen(prefix)))) { snprintf(path, root_length * 4 + old_length + 48, "%s/usr/bin:%s/usr/sbin:%s/bin:%s/sbin%s%s", root, root, root, root, old_length ? ":" : "", old_length ? old_path : ""); setenv("PATH", path, 1); } free(prefix); free(path); if (regular_file(toolchain_marker)) { char *pkgconfig = malloc(root_length * 2 + 48); setenv("PKG_CONFIG_SYSROOT_DIR", root, 1); if (!pkgconfig) fail("out of memory", NULL); snprintf(pkgconfig, root_length * 2 + 48, "%s/usr/lib/pkgconfig:%s/usr/share/pkgconfig", root, root); setenv("PKG_CONFIG_LIBDIR", pkgconfig, 1); unsetenv("PKG_CONFIG_PATH"); free(pkgconfig); } free(toolchain_marker); } /* ---- optional per-bundle environment: /.provisioning/env ------------------------------------ Written by relocate.mjs from a recipe's `env` lines. KEY=VALUE sets; KEY+=VALUE appends " VALUE" unless VALUE already occurs (nested launches must not grow it). "$ROOT" -> bundle root, "$SELF" -> this launcher's resolved path. Absent file = no-op. */ #define ENV_FILE_MAX (PATH_MAX * 4 - 1) /* read_small_file's cap; an env file is a handful of lines */ #define PLACEHOLDER_LENGTH 5 /* strlen("$ROOT") == strlen("$SELF") */ static char *expand_placeholders(const char *value, const char *root, const char *self) { size_t root_length = strlen(root), self_length = strlen(self), needed = 1; const char *cursor; char *result, *out; for (cursor = value; *cursor;) { if (!strncmp(cursor, "$ROOT", PLACEHOLDER_LENGTH)) { needed += root_length; cursor += PLACEHOLDER_LENGTH; } else if (!strncmp(cursor, "$SELF", PLACEHOLDER_LENGTH)) { needed += self_length; cursor += PLACEHOLDER_LENGTH; } else { needed++; cursor++; } } result = out = malloc(needed); if (!result) fail("out of memory", NULL); for (cursor = value; *cursor;) { if (!strncmp(cursor, "$ROOT", PLACEHOLDER_LENGTH)) { memcpy(out, root, root_length); out += root_length; cursor += PLACEHOLDER_LENGTH; } else if (!strncmp(cursor, "$SELF", PLACEHOLDER_LENGTH)) { memcpy(out, self, self_length); out += self_length; cursor += PLACEHOLDER_LENGTH; } else *out++ = *cursor++; } *out = '\0'; return result; } static void apply_environment_line(const char *line, size_t length, const char *root, const char *self) { const char *equals = memchr(line, '=', length); const char *old; int append; size_t key_length; char *key, *raw, *value; if (!equals || equals == line) return; append = equals[-1] == '+'; key_length = (size_t)(equals - line) - (append ? 1 : 0); if (!key_length) return; key = strndup(line, key_length); raw = strndup(equals + 1, length - (size_t)(equals + 1 - line)); if (!key || !raw) fail("out of memory", NULL); value = expand_placeholders(raw, root, self); old = append ? getenv(key) : NULL; if (old && *old) { if (!strstr(old, value)) { char *joined = malloc(strlen(old) + 1 + strlen(value) + 1); if (!joined) fail("out of memory", NULL); sprintf(joined, "%s %s", old, value); setenv(key, joined, 1); free(joined); } } else setenv(key, value, 1); free(key); free(raw); free(value); } static void apply_environment_file(const char *root, const char *self) { char *path = join2(root, ".provisioning/env"); char *content = read_small_file(path); const char *cursor; if (!content) { free(path); return; } if (strlen(content) >= ENV_FILE_MAX) fail("environment file too large", path); free(path); for (cursor = content; *cursor;) { const char *end = strchr(cursor, '\n'); size_t length = end ? (size_t)(end - cursor) : strlen(cursor); if (length && cursor[length - 1] == '\r') length--; if (length && *cursor != '#') apply_environment_line(cursor, length, root, self); if (!end) break; cursor = end + 1; } free(content); } static int line_list_contains(const char *list, const char *value) { size_t value_length = strlen(value); const char *cursor = list; while (*cursor) { const char *end = strchr(cursor, '\n'); size_t length = end ? (size_t)(end - cursor) : strlen(cursor); if (length && cursor[length - 1] == '\r') length--; if (length == value_length && !memcmp(cursor, value, length)) return 1; if (!end) break; cursor = end + 1; } return 0; } static int needs_sysroot(const char *root, const char *self) { char *list_path = join2(root, ".provisioning/sysroot-tools"); char *list = read_small_file(list_path); const char *relative = self; int result = 0; free(list_path); if (!list) return 0; if (!strncmp(self, root, strlen(root)) && self[strlen(root)] == '/') relative = self + strlen(root) + 1; result = line_list_contains(list, relative); free(list); return result; } static int starts_with_shebang(const char *path) { int descriptor = open(path, O_RDONLY); unsigned char bytes[2]; int result = descriptor >= 0 && read(descriptor, bytes, sizeof bytes) == 2 && bytes[0] == '#' && bytes[1] == '!'; if (descriptor >= 0) close(descriptor); return result; } static void execute_script(const char *root, const char *real, int argc, char **argv) { int descriptor = open(real, O_RDONLY); char line[4096]; ssize_t count; char *cursor; char *interpreter; char *optional = NULL; char *root_interpreter = NULL; char **arguments; int output = 0; if (descriptor < 0) fail("cannot open script", real); count = read(descriptor, line, sizeof line - 1); close(descriptor); if (count < 2) fail("invalid script", real); line[count] = '\0'; if ((cursor = strchr(line, '\n'))) *cursor = '\0'; cursor = line + 2; while (*cursor == ' ' || *cursor == '\t') cursor++; interpreter = cursor; while (*cursor && *cursor != ' ' && *cursor != '\t' && *cursor != '\r') cursor++; if (*cursor) { *cursor++ = '\0'; while (*cursor == ' ' || *cursor == '\t') cursor++; if (*cursor) { optional = cursor; cursor += strlen(cursor); while (cursor > optional && (cursor[-1] == '\r' || cursor[-1] == ' ' || cursor[-1] == '\t')) *--cursor = '\0'; } } if (!*interpreter) fail("empty script interpreter", real); if (interpreter[0] == '/') { root_interpreter = join2(root, interpreter + 1); if (access(root_interpreter, X_OK) == 0) interpreter = root_interpreter; } arguments = calloc((size_t)argc + (optional ? 3 : 2), sizeof(char *)); if (!arguments) fail("out of memory", NULL); arguments[output++] = interpreter; if (optional) arguments[output++] = optional; arguments[output++] = (char *)real; for (int index = 1; index < argc; index++) arguments[output++] = argv[index]; arguments[output] = NULL; execv(interpreter, arguments); fail("could not execute script interpreter", interpreter); } int main(int argc, char **argv) { char self[PATH_MAX + 1]; ssize_t self_length = readlink("/proc/self/exe", self, PATH_MAX); char *root; char *self_directory; char *real_name; char *real; char *loader_metadata; char *loader_relative; char *loader; char *library_metadata; char *library_relative; char *library_path; char *sysroot = NULL; char **arguments; int output = 0; if (self_length < 0 || self_length > PATH_MAX) fail("cannot read /proc/self/exe", NULL); self[self_length] = '\0'; root = find_root(self); self_directory = directory_name(self); real_name = malloc(strlen(base_name(self)) + 20); if (!real_name) fail("out of memory", NULL); sprintf(real_name, ".provisioning-real-%s", base_name(self)); real = join2(self_directory, real_name); if (!regular_file(real)) fail("hidden payload is missing", real); configure_environment(root); apply_environment_file(root, self); if (starts_with_shebang(real)) execute_script(root, real, argc, argv); loader_metadata = join2(root, ".provisioning/loader"); loader_relative = read_small_file(loader_metadata); if (!loader_relative || !*loader_relative) fail("dynamic loader metadata is missing", loader_metadata); loader = join2(root, loader_relative); library_metadata = join2(root, ".provisioning/library-path"); library_relative = read_small_file(library_metadata); if (!library_relative) fail("library path metadata is missing", library_metadata); library_path = absolute_list(root, library_relative); if (!*library_path) fail("library path metadata is empty", library_metadata); if (needs_sysroot(root, self)) { sysroot = malloc(strlen(root) + 11); if (!sysroot) fail("out of memory", NULL); sprintf(sysroot, "--sysroot=%s", root); } arguments = calloc((size_t)argc + (sysroot ? 8 : 7), sizeof(char *)); if (!arguments) fail("out of memory", NULL); arguments[output++] = loader; arguments[output++] = "--library-path"; arguments[output++] = library_path; arguments[output++] = "--argv0"; arguments[output++] = argc ? argv[0] : self; arguments[output++] = real; if (sysroot) arguments[output++] = sysroot; for (int index = 1; index < argc; index++) arguments[output++] = argv[index]; arguments[output] = NULL; execv(loader, arguments); fail("could not execute bundled musl loader", loader); return 126; }