Skip to content

Commit 1862a17

Browse files
llama-router: validate binary before spawn, clean child error handling
1 parent 5782696 commit 1862a17

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

tools/router/router-process.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chrono>
88
#include <cstdio>
99
#include <cerrno>
10+
#include <filesystem>
1011
#include <cstring>
1112
#include <exception>
1213
#include <sstream>
@@ -144,6 +145,13 @@ ProcessHandle spawn_process(const std::vector<std::string> & args) {
144145
return handle;
145146
}
146147

148+
const std::string binary = args[0];
149+
std::error_code ec;
150+
if (!std::filesystem::exists(binary, ec)) {
151+
LOG_ERR("Binary not found: %s\n", binary.c_str());
152+
return handle;
153+
}
154+
147155
#if defined(_WIN32)
148156
std::ostringstream cmdline;
149157
for (size_t i = 0; i < args.size(); ++i) {
@@ -273,29 +281,7 @@ ProcessHandle spawn_process(const std::vector<std::string> & args) {
273281
cargs.push_back(const_cast<char *>(arg.c_str()));
274282
}
275283
cargs.push_back(nullptr);
276-
{
277-
char msg[256];
278-
int n = snprintf(msg, sizeof(msg), "[child %d] preparing to exec: %s\n", static_cast<int>(getpid()), cargs[0]);
279-
if (n > 0) {
280-
write(STDERR_FILENO, msg, static_cast<size_t>(n));
281-
}
282-
}
283284
execvp(cargs[0], cargs.data());
284-
const int err = errno;
285-
{
286-
char err_buf[128];
287-
strerror_r(err, err_buf, sizeof(err_buf));
288-
char msg[256];
289-
int n = snprintf(msg,
290-
sizeof(msg),
291-
"[child %d] execvp failed for %s: %s\n",
292-
static_cast<int>(getpid()),
293-
cargs[0],
294-
err_buf);
295-
if (n > 0) {
296-
write(STDERR_FILENO, msg, static_cast<size_t>(n));
297-
}
298-
}
299285
_exit(1);
300286
} else if (pid > 0) {
301287
close(stdout_pipe[1]);

0 commit comments

Comments
 (0)