Codebergdnkl

foot

master
ply5 git calls25 ms32 KiB3 cachedprotocol v2
573 lines15 KiBRaw
1#include "slave.h"
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <ctype.h>
7#include <unistd.h>
8#include <errno.h>
9#include <signal.h>
10#include <termios.h>
12#include <sys/stat.h>
13#include <sys/ioctl.h>
14#include <fcntl.h>
16#define LOG_MODULE "slave"
17#define LOG_ENABLE_DBG 0
18#include "log.h"
20#include "debug.h"
21#include "macros.h"
22#include "tokenize.h"
23#include "util.h"
24#include "xmalloc.h"
26extern char **environ;
28struct environ {
29 size_t count;
30 char **envp;
31};
33#if !defined(EXECVPE)
34static char *
35find_file_in_path(const char *file)
37 if (strchr(file, '/') != NULL)
38 return xstrdup(file);
40 const char *env_path = getenv("PATH");
41 char *path_list = NULL;
43 if (env_path != NULL && env_path[0] != '\0')
44 path_list = xstrdup(env_path);
45 else {
46 size_t sc_path_len = confstr(_CS_PATH, NULL, 0);
47 if (sc_path_len > 0) {
48 path_list = xmalloc(sc_path_len);
49 confstr(_CS_PATH, path_list, sc_path_len);
50 } else
51 return xstrdup(file);
52 }
54 for (const char *path = strtok(path_list, ":");
55 path != NULL;
56 path = strtok(NULL, ":"))
57 {
58 char *full = xstrjoin3(path, "/", file);
59 if (access(full, F_OK) == 0) {
60 free(path_list);
61 return full;
62 }
64 free(full);
65 }
67 free(path_list);
68 return xstrdup(file);
71static int
72foot_execvpe(const char *file, char *const argv[], char *const envp[])
74 char *path = find_file_in_path(file);
75 int ret = execve(path, argv, envp);
77 /*
78 * Getting here is an error
79 */
80 free(path);
81 return ret;
84#else /* EXECVPE */
86#define foot_execvpe(file, argv, envp) execvpe(file, argv, envp)
88#endif /* EXECVPE */
90static bool
91is_valid_shell(const char *shell)
93 FILE *f = fopen("/etc/shells", "r");
94 if (f == NULL)
95 goto err;
97 char *_line = NULL;
98 size_t count = 0;
100 while (true) {
101 errno = 0;
102 ssize_t ret = getline(&_line, &count, f);
104 if (ret < 0) {
105 free(_line);
106 break;
107 }
109 char *line = _line;
110 {
111 while (isspace(*line))
112 line++;
113 if (line[0] != '\0') {
114 char *end = line + strlen(line) - 1;
115 while (isspace(*end))
116 end--;
117 *(end + 1) = '\0';
118 }
119 }
121 if (line[0] == '#')
122 continue;
124 if (streq(line, shell)) {
125 fclose(f);
126 return true;
127 }
128 }
130err:
131 if (f != NULL)
132 fclose(f);
133 return false;
136enum user_notification_ret_t {UN_OK, UN_NO_MORE, UN_FAIL};
138static enum user_notification_ret_t
139emit_one_notification(int fd, const struct user_notification *notif)
141 const char *prefix = NULL;
142 const char *postfix = "\033[m\n";
144 switch (notif->kind) {
145 case USER_NOTIFICATION_DEPRECATED:
146 prefix = "\033[33;1mdeprecated\033[39;22m: ";
147 break;
149 case USER_NOTIFICATION_WARNING:
150 prefix = "\033[33;1mwarning\033[39;22m: ";
151 break;
153 case USER_NOTIFICATION_ERROR:
154 prefix = "\033[31;1merror\033[39;22m: ";
155 break;
156 }
158 xassert(prefix != NULL);
160 if (write(fd, prefix, strlen(prefix)) < 0 ||
161 write(fd, "foot: ", 6) < 0 ||
162 write(fd, notif->text, strlen(notif->text)) < 0 ||
163 write(fd, postfix, strlen(postfix)) < 0)
164 {
165 /*
166 * The main process is blocking and waiting for us to close
167 * the error pipe. Thus, pts data will *not* be processed
168 * until we've exec:d. This means we cannot write anymore once
169 * the kernel buffer is full. Don't treat this as a fatal
170 * error.
171 */
172 if (errno == EWOULDBLOCK || errno == EAGAIN)
173 return UN_NO_MORE;
174 else {
175 LOG_ERRNO("failed to write user-notification");
176 return UN_FAIL;
177 }
178 }
180 return UN_OK;
183static bool
184emit_notifications_of_kind(int fd,
185 const user_notifications_t *notifications,
186 enum user_notification_kind kind)
188 tll_foreach(*notifications, it) {
189 if (it->item.kind == kind) {
190 switch (emit_one_notification(fd, &it->item)) {
191 case UN_OK:
192 break;
193 case UN_NO_MORE:
194 return true;
195 case UN_FAIL:
196 return false;
197 }
198 }
199 }
201 return true;
204static bool
205emit_notifications(int fd, const user_notifications_t *notifications)
207 return
208 emit_notifications_of_kind(fd, notifications, USER_NOTIFICATION_ERROR) &&
209 emit_notifications_of_kind(fd, notifications, USER_NOTIFICATION_WARNING) &&
210 emit_notifications_of_kind(fd, notifications, USER_NOTIFICATION_DEPRECATED);
213static noreturn void
214slave_exec(int ptmx, char *argv[], char *const envp[], int err_fd,
215 bool login_shell, const user_notifications_t *notifications)
217 int pts = -1;
218 const char *pts_name = ptsname(ptmx);
220 if (grantpt(ptmx) == -1) {
221 LOG_ERRNO("failed to grantpt()");
222 goto err;
223 }
224 if (unlockpt(ptmx) == -1) {
225 LOG_ERRNO("failed to unlockpt()");
226 goto err;
227 }
229 close(ptmx);
230 ptmx = -1;
232 if (setsid() == -1) {
233 LOG_ERRNO("failed to setsid()");
234 goto err;
235 }
237 pts = open(pts_name, O_RDWR);
238 if (pts == -1) {
239 LOG_ERRNO("failed to open pseudo terminal slave device");
240 goto err;
241 }
243 if (ioctl(pts, TIOCSCTTY, 0) < 0) {
244 LOG_ERRNO("failed to configure controlling terminal");
245 goto err;
246 }
248#ifdef IUTF8
249 {
250 struct termios flags;
251 if (tcgetattr(pts, &flags) < 0) {
252 LOG_ERRNO("failed to get terminal attributes");
253 goto err;
254 }
256 flags.c_iflag |= IUTF8;
257 if (tcsetattr(pts, TCSANOW, &flags) < 0) {
258 LOG_ERRNO("failed to set IUTF8 terminal attribute");
259 goto err;
260 }
261 }
262#endif
264 if (tll_length(*notifications) > 0) {
265 int flags = fcntl(pts, F_GETFL);
266 if (flags < 0)
267 goto err;
268 if (fcntl(pts, F_SETFL, flags | O_NONBLOCK) < 0)
269 goto err;
271 if (!emit_notifications(pts, notifications))
272 goto err;
274 fcntl(pts, F_SETFL, flags);
275 }
277 if (dup2(pts, STDIN_FILENO) == -1 ||
278 dup2(pts, STDOUT_FILENO) == -1 ||
279 dup2(pts, STDERR_FILENO) == -1)
280 {
281 LOG_ERRNO("failed to dup stdin/stdout/stderr");
282 goto err;
283 }
285 close(pts);
286 pts = -1;
288 const char *file;
289 if (login_shell) {
290 file = xstrdup(argv[0]);
292 char *arg0 = xmalloc(strlen(argv[0]) + 1 + 1);
293 arg0[0] = '-';
294 arg0[1] = '\0';
295 strcat(arg0, argv[0]);
297 argv[0] = arg0;
298 } else
299 file = argv[0];
301 foot_execvpe(file, argv, envp);
303err:
304 (void)!write(err_fd, &errno, sizeof(errno));
305 if (pts != -1)
306 close(pts);
307 if (ptmx != -1)
308 close(ptmx);
309 close(err_fd);
310 _exit(errno);
313static bool
314env_matches_var_name(const char *e, const char *name)
316 const size_t e_len = strlen(e);
317 const size_t name_len = strlen(name);
319 if (e_len <= name_len)
320 return false;
321 if (memcmp(e, name, name_len) != 0)
322 return false;
323 if (e[name_len] != '=')
324 return false;
325 return true;
328static void
329add_to_env(struct environ *env, const char *name, const char *value)
331 if (env->envp == NULL)
332 setenv(name, value, 1);
333 else {
334 char *e = xstrjoin3(name, "=", value);
336 /* Search for existing variable. If found, replace it with the
337 new value */
338 for (size_t i = 0; i < env->count; i++) {
339 if (env_matches_var_name(env->envp[i], name)) {
340 free(env->envp[i]);
341 env->envp[i] = e;
342 return;
343 }
344 }
346 /* If the variable does not already exist, add it */
347 env->envp = xrealloc(env->envp, (env->count + 2) * sizeof(env->envp[0]));
348 env->envp[env->count++] = e;
349 env->envp[env->count] = NULL;
350 }
353static void
354del_from_env(struct environ *env, const char *name)
356 if (env->envp == NULL)
357 unsetenv(name);
358 else {
359 for (size_t i = 0; i < env->count; i++) {
360 if (env_matches_var_name(env->envp[i], name)) {
361 free(env->envp[i]);
362 memmove(&env->envp[i],
363 &env->envp[i + 1],
364 (env->count - i) * sizeof(env->envp[0]));
365 env->count--;
366 xassert(env->envp[env->count] == NULL);
367 break;
368 }
369 }
370 }
373pid_t
374slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv,
375 const char *const *envp, const env_var_list_t *extra_env_vars,
376 const char *term_env, const char *conf_shell, bool login_shell,
377 const user_notifications_t *notifications)
379 int fork_pipe[2];
380 if (pipe2(fork_pipe, O_CLOEXEC) < 0) {
381 LOG_ERRNO("failed to create pipe");
382 return -1;
383 }
385 pid_t pid = fork();
386 switch (pid) {
387 case -1:
388 LOG_ERRNO("failed to fork");
389 close(fork_pipe[0]);
390 close(fork_pipe[1]);
391 return -1;
393 case 0:
394 /* Child */
395 close(fork_pipe[0]); /* Close read end */
397 if (chdir(cwd) < 0) {
398 const int errno_copy = errno;
399 LOG_ERRNO("failed to change working directory to %s", cwd);
400 (void)!write(fork_pipe[1], &errno_copy, sizeof(errno_copy));
401 _exit(errno_copy);
402 }
404 /* Restore signal mask, and SIG_IGN'd signals */
405 struct sigaction dfl = {.sa_handler = SIG_DFL};
406 sigemptyset(&dfl.sa_mask);
407 sigset_t mask;
408 sigemptyset(&mask);
410 if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0 ||
411 sigaction(SIGHUP, &dfl, NULL) < 0 ||
412 sigaction(SIGPIPE, &dfl, NULL) < 0)
413 {
414 const int errno_copy = errno;
415 LOG_ERRNO_P(errno, "failed to restore signals");
416 (void)!write(fork_pipe[1], &errno_copy, sizeof(errno_copy));
417 _exit(errno_copy);
418 }
420 /* Create a mutable copy of the environment */
421 struct environ custom_env = {0};
422 if (envp != NULL) {
423 for (const char *const *e = envp; *e != NULL; e++)
424 custom_env.count++;
426 custom_env.envp = xcalloc(
427 custom_env.count + 1, sizeof(custom_env.envp[0]));
429 size_t i = 0;
430 for (const char *const *e = envp; *e != NULL; e++, i++)
431 custom_env.envp[i] = xstrdup(*e);
432 xassert(custom_env.envp[custom_env.count] == NULL);
433 }
435 add_to_env(&custom_env, "TERM", term_env);
436 add_to_env(&custom_env, "COLORTERM", "truecolor");
437 add_to_env(&custom_env, "PWD", cwd);
439 del_from_env(&custom_env, "TERM_PROGRAM"); /* Wezterm, Ghostty */
440 del_from_env(&custom_env, "TERM_PROGRAM_VERSION"); /* Wezterm, Ghostty */
441 del_from_env(&custom_env, "TERMINAL_NAME"); /* Contour */
442 del_from_env(&custom_env, "TERMINAL_VERSION_STRING"); /* Contour */
443 del_from_env(&custom_env, "TERMINAL_VERSION_TRIPLE"); /* Contour */
445 /* XTerm specific */
446 del_from_env(&custom_env, "XTERM_SHELL");
447 del_from_env(&custom_env, "XTERM_VERSION");
448 del_from_env(&custom_env, "XTERM_LOCALE");
450 /* Mlterm specific */
451 del_from_env(&custom_env, "MLTERM");
453 /* Zutty specific */
454 del_from_env(&custom_env, "ZUTTY_VERSION");
456 /* Ghostty specific */
457 del_from_env(&custom_env, "GHOSTTY_BIN_DIR");
458 del_from_env(&custom_env, "GHOSTTY_SHELL_INTEGRATION_NO_SUDO");
459 del_from_env(&custom_env, "GHOSTTY_RESOURCES_DIR");
461 /* Kitty specific */
462 del_from_env(&custom_env, "KITTY_WINDOW_ID");
463 del_from_env(&custom_env, "KITTY_PID");
464 del_from_env(&custom_env, "KITTY_PUBLIC_KEY");
465 del_from_env(&custom_env, "KITTY_INSTALLATION_DIR");
467 /* Contour specific */
468 del_from_env(&custom_env, "CONTOUR_PROFILE");
470 /* Wezterm specific */
471 del_from_env(&custom_env, "WEZTERM_PANE");
472 del_from_env(&custom_env, "WEZTERM_EXECUTABLE");
473 del_from_env(&custom_env, "WEZTERM_CONFIG_FILE");
474 del_from_env(&custom_env, "WEZTERM_EXECUTABLE_DIR");
475 del_from_env(&custom_env, "WEZTERM_UNIX_SOCKET");
476 del_from_env(&custom_env, "WEZTERM_CONFIG_DIR");
478 /* Alacritty specific */
479 del_from_env(&custom_env, "ALACRITTY_LOG");
480 del_from_env(&custom_env, "ALACRITTY_WINDOW_ID");
481 del_from_env(&custom_env, "ALACRITTY_SOCKET");
483 /* VTE, gnome-terminal, kgx etc */
484 del_from_env(&custom_env, "VTE_VERSION");
485 del_from_env(&custom_env, "GNOME_TERMINAL_SERVICE");
486 del_from_env(&custom_env, "GNOME_TERMINAL_SCREEN");
488#if defined(FOOT_TERMINFO_PATH)
489 add_to_env(&custom_env, "TERMINFO", FOOT_TERMINFO_PATH);
490#endif
492 if (extra_env_vars != NULL) {
493 tll_foreach(*extra_env_vars, it) {
494 const char *name = it->item.name;
495 const char *value = it->item.value;
497 if (strlen(value) == 0)
498 del_from_env(&custom_env, name);
499 else
500 add_to_env(&custom_env, name, value);
501 }
502 }
504 char **_shell_argv = NULL;
505 char **shell_argv = NULL;
507 if (argc == 0) {
508 if (!tokenize_cmdline(conf_shell, &_shell_argv)) {
509 (void)!write(fork_pipe[1], &errno, sizeof(errno));
510 _exit(0);
511 }
512 shell_argv = _shell_argv;
513 } else {
514 size_t count = 0;
515 for (; argv[count] != NULL; count++)
516 ;
517 shell_argv = xmalloc((count + 1) * sizeof(shell_argv[0]));
518 for (size_t i = 0; i < count; i++)
519 shell_argv[i] = argv[i];
520 shell_argv[count] = NULL;
521 }
523 if (is_valid_shell(shell_argv[0]))
524 add_to_env(&custom_env, "SHELL", shell_argv[0]);
526 slave_exec(ptmx, shell_argv,
527 custom_env.envp != NULL ? custom_env.envp : environ,
528 fork_pipe[1], login_shell, notifications);
529 BUG("Unexpected return from slave_exec()");
530 break;
532 default: {
534 /*
535 * Don't stay in CWD, since it may be an ephemeral path. For
536 * example, it may be a mount point of, say, a thumb drive. Us
537 * keeping it open will prevent the user from unmounting it.
538 */
539 (void)!!chdir("/");
541 close(fork_pipe[1]); /* Close write end */
542 LOG_DBG("slave has PID %d", pid);
544 int errno_copy;
545 static_assert(sizeof(errno) == sizeof(errno_copy), "errno size mismatch");
547 ssize_t ret = read(fork_pipe[0], &errno_copy, sizeof(errno_copy));
548 close(fork_pipe[0]);
550 if (ret < 0) {
551 LOG_ERRNO("failed to read from pipe");
552 return -1;
553 } else if (ret == sizeof(errno_copy)) {
554 LOG_ERRNO_P(
555 errno_copy, "%s: failed to execute",
556 argc == 0 ? conf_shell : argv[0]);
557 return -1;
558 } else
559 LOG_DBG("%s: successfully started", conf_shell);
561 int fd_flags;
562 if ((fd_flags = fcntl(ptmx, F_GETFD)) < 0 ||
563 fcntl(ptmx, F_SETFD, fd_flags | FD_CLOEXEC) < 0)
564 {
565 LOG_ERRNO("failed to set FD_CLOEXEC on ptmx");
566 return -1;
567 }
568 break;
569 }
570 }
572 return pid;
Diagnostics5 git calls · 25 ms
CallStatusSizeTime025 ms
discover200178 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=10322
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch2004.3 KiB · hit19 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=19
git.response.bytes=4360
git.response.cache=HIT
git.response.duration_ms=19
git.fetch.wants=6289937212b39755da824754abf44d756609dbfd
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=4360
git.pkt.payload.max=4333
git.sideband.band1.bytes=4333
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e27fe4f9c10555
Request
16c1d217…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format