Codebergdnkl
foot
master
1#include "spawn.h"
3#include <string.h>
4#include <unistd.h>
5#include <errno.h>
6#include <signal.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
12#define LOG_MODULE "spawn"
13#define LOG_ENABLE_DBG 0
14#include "log.h"
15#include "debug.h"
16#include "xmalloc.h"
18pid_t
19spawn(struct reaper *reaper, const char *cwd, char *const argv[],
20 int stdin_fd, int stdout_fd, int stderr_fd, reaper_cb cb, void *cb_data,
21 const char *xdg_activation_token)
22{
23 int pipe_fds[2] = {-1, -1};
24 if (pipe2(pipe_fds, O_CLOEXEC) < 0) {
25 LOG_ERRNO("failed to create pipe");
26 goto err;
27 }
29 pid_t pid = fork();
30 if (pid < 0) {
31 LOG_ERRNO("failed to fork");
32 goto err;
33 }
35 if (pid == 0) {
36 /* Child */
37 close(pipe_fds[0]);
39 if (setsid() < 0)
40 goto child_err;
42 /* Clear signal mask */
43 sigset_t mask;
44 sigemptyset(&mask);
45 if (sigprocmask(SIG_SETMASK, &mask, NULL) < 0)
46 goto child_err;
48 /* Restore ignored (SIG_IGN) signals */
49 struct sigaction dfl = {.sa_handler = SIG_DFL};
50 sigemptyset(&dfl.sa_mask);
51 if (sigaction(SIGHUP, &dfl, NULL) < 0 ||
52 sigaction(SIGPIPE, &dfl, NULL) < 0)
53 {
54 goto child_err;
55 }
57 if (cwd != NULL) {
58 setenv("PWD", cwd, 1);
59 if (chdir(cwd) < 0) {
60 LOG_WARN("failed to change working directory to %s: %s",
61 cwd, strerror(errno));
62 }
63 }
65 if (xdg_activation_token != NULL) {
66 setenv("XDG_ACTIVATION_TOKEN", xdg_activation_token, 1);
68 if (getenv("DISPLAY") != NULL)
69 setenv("DESKTOP_STARTUP_ID", xdg_activation_token, 1);
70 }
72 bool close_stderr = stderr_fd >= 0;
73 bool close_stdout = stdout_fd >= 0 && stdout_fd != stderr_fd;
74 bool close_stdin = stdin_fd >= 0 && stdin_fd != stdout_fd && stdin_fd != stderr_fd;
76 if ((stdin_fd >= 0 && (dup2(stdin_fd, STDIN_FILENO) < 0
77 || (close_stdin && close(stdin_fd) < 0))) ||
78 (stdout_fd >= 0 && (dup2(stdout_fd, STDOUT_FILENO) < 0
79 || (close_stdout && close(stdout_fd) < 0))) ||
80 (stderr_fd >= 0 && (dup2(stderr_fd, STDERR_FILENO) < 0
81 || (close_stderr && close(stderr_fd) < 0))) ||
82 execvp(argv[0], argv) < 0)
83 {
84 goto child_err;
85 }
87 xassert(false);
88 _exit(errno);
90 child_err:
91 ;
92 const int errno_copy = errno;
93 (void)!write(pipe_fds[1], &errno_copy, sizeof(errno_copy));
94 _exit(errno_copy);
95 }
97 /* Parent */
98 close(pipe_fds[1]);
100 int errno_copy;
101 static_assert(sizeof(errno_copy) == sizeof(errno), "errno size mismatch");
103 ssize_t ret = read(pipe_fds[0], &errno_copy, sizeof(errno_copy));
104 close(pipe_fds[0]);
106 if (ret == 0) {
107 reaper_add(reaper, pid, cb, cb_data);
108 return pid;
109 } else if (ret < 0) {
110 LOG_ERRNO("failed to read from pipe");
111 return -1;
112 } else {
113 LOG_ERRNO_P(errno_copy, "%s: failed to spawn", argv[0]);
114 errno = errno_copy;
115 waitpid(pid, NULL, 0);
116 return -1;
117 }
119err:
120 if (pipe_fds[0] != -1)
121 close(pipe_fds[0]);
122 if (pipe_fds[1] != -1)
123 close(pipe_fds[1]);
124 return -1;
125}
127bool
128spawn_expand_template(const struct config_spawn_template *template,
129 size_t key_count,
130 const char *key_names[static key_count],
131 const char *key_values[static key_count],
132 size_t *argc, char ***argv)
133{
134 *argc = 0;
135 *argv = NULL;
137 for (; template->argv.args[*argc] != NULL; (*argc)++)
138 ;
140#define append(s, n) \
141 do { \
142 expanded = xrealloc(expanded, len + (n) + 1); \
143 memcpy(&expanded[len], s, n); \
144 len += n; \
145 expanded[len] = '\0'; \
146 } while (0)
148 *argv = xmalloc((*argc + 1) * sizeof((*argv)[0]));
150 /* Expand the provided keys */
151 for (size_t i = 0; i < *argc; i++) {
152 size_t len = 0;
153 char *expanded = NULL;
155 char *start = NULL;
156 char *last_end = template->argv.args[i];
158 while ((start = strstr(last_end, "${")) != NULL) {
159 /* Append everything from the last template's end to this
160 * one's beginning */
161 append(last_end, start - last_end);
163 /* Find end of template */
164 start += 2;
165 char *end = strstr(start, "}");
167 if (end == NULL) {
168 /* Ensure final append() copies the unclosed '${' */
169 last_end = start - 2;
170 LOG_WARN("notify: unclosed template: %s", last_end);
171 break;
172 }
174 /* Expand template */
175 bool valid_key = false;
176 for (size_t j = 0; j < key_count; j++) {
177 if (strncmp(start, key_names[j], end - start) != 0)
178 continue;
180 append(key_values[j], strlen(key_values[j]));
181 valid_key = true;
182 break;
183 }
185 if (!valid_key) {
186 /* Unrecognized template - append it as-is */
187 start -= 2;
188 append(start, end + 1 - start);
189 LOG_WARN("notify: unrecognized template: %.*s",
190 (int)(end + 1 - start), start);
191 }
193 last_end = end + 1;
194 }
196 append(
197 last_end,
198 template->argv.args[i] + strlen(template->argv.args[i]) - last_end);
199 (*argv)[i] = expanded;
200 }
201 (*argv)[*argc] = NULL;
203#undef append
204 return true;
205}
Diagnostics5 git calls · 1.8 s
CallStatusSizeTime01.8 s
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 KiB686 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=662 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=686 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
fetchtree:0 · deepen 102009.3 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=2 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=10Open pack map
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
fetch2001.9 KiB1.1 s
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1106 git.response.bytes=1950 git.response.cache=MISS git.response.duration_ms=1106 git.fetch.wants=17c821b52e29b910ac789844ce7f01f58bb3c8c9 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=1950 git.pkt.payload.max=1923 git.sideband.band1.bytes=1923 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
- a5e198a4be85b86e
- Request
- f22ac151…
- 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