| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /* Shared runtime setup for browser-host and host-curl. */
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "host.h"
- #include "fs.h"
- struct host_options host_opts;
- char host_root[1024];
- char host_repo[1024];
- char host_curl_bin[1024];
- static void dirname_in_place(char *p)
- {
- char *slash = strrchr(p, '/');
- if (!slash) {
- strcpy(p, ".");
- return;
- }
- if (slash == p)
- slash[1] = '\0';
- else
- *slash = '\0';
- }
- static void exe_dir(char *out, size_t cap, const char *argv0)
- {
- ssize_t n = readlink("/proc/self/exe", out, cap - 1);
- if (n > 0) {
- out[n] = '\0';
- dirname_in_place(out);
- return;
- }
- if (argv0 && argv0[0]) {
- snprintf(out, cap, "%s", argv0);
- dirname_in_place(out);
- return;
- }
- out[0] = '\0';
- }
- int host_runtime_init(const char *argv0, const char *root)
- {
- const char *env_root = getenv("NEBOS_HOST_ROOT");
- const char *env_curl = getenv("NEBOS_HOST_CURL");
- char exe[1024];
- if (!root || !root[0])
- root = env_root && env_root[0] ? env_root :
- "/tmp/opencode/nebos-browser-root";
- snprintf(host_root, sizeof(host_root), "%s", root);
- /* Drop a trailing slash except for "/". */
- if (strlen(host_root) > 1 && host_root[strlen(host_root) - 1] == '/')
- host_root[strlen(host_root) - 1] = '\0';
- exe_dir(exe, sizeof(exe), argv0);
- snprintf(host_repo, sizeof(host_repo), "%s", exe);
- if (env_curl && env_curl[0])
- snprintf(host_curl_bin, sizeof(host_curl_bin), "%s", env_curl);
- else
- snprintf(host_curl_bin, sizeof(host_curl_bin), "%s/host-curl",
- exe);
- setenv("NEBOS_HOST_ROOT", host_root, 1);
- setenv("NEBOS_HOST_CURL", host_curl_bin, 1);
- if (host_fs_init(host_root) != 0) {
- fprintf(stderr, "browser-host: cannot create profile root %s\n",
- host_root);
- return -1;
- }
- return 0;
- }
|