Skip to content

Commit

Permalink
Improve pledge() and avoid CUDA exit() in server
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Dec 10, 2024
1 parent 59a5d97 commit 8fa1702
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion llama.cpp/ggml-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -2248,7 +2248,7 @@ bool ggml_backend_compare_graph_backend(ggml_backend_t backend1, ggml_backend_t
}

GGML_CALL static void system_exit(int rc) {
exit(rc);
pthread_exit(0);
}

GGML_CALL static void system_free(void *p) {
Expand Down
16 changes: 0 additions & 16 deletions llamafile/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,6 @@ main(int argc, char* argv[])
for (int i = 0; i < FLAG_workers; ++i)
npassert(!g_server->spawn());

// install security
if (!FLAG_unsecure) {
const char* promises;
if (FLAG_www_root) {
promises = "stdio anet rpath";
} else {
promises = "stdio anet";
}
if (pledge(0, 0)) {
SLOG("warning: this OS doesn't support pledge() security");
} else if (pledge("stdio anet", 0)) {
perror("pledge");
exit(1);
}
}

// run server
signals_init();
llama_backend_init();
Expand Down
25 changes: 24 additions & 1 deletion llamafile/server/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
#include "llamafile/server/tokenbucket.h"
#include "llamafile/threadlocal.h"
#include "llamafile/trust.h"
#include <cosmo.h>
#include <atomic>
#include <cassert>
#include <cosmo.h>
#include <exception>
#include <pthread.h>

Expand Down Expand Up @@ -135,6 +136,28 @@ Worker::handle()
void
Worker::run()
{
if (!FLAG_unsecure) {
static std::atomic<bool> once;
if (llamafile_has_gpu()) {
if (!once.exchange(true))
SLOG("warning: gpu mode disables pledge security");
} else {
const char* promises;
if (FLAG_www_root && !startswith(FLAG_www_root, "/zip/")) {
promises = "stdio anet rpath";
} else {
promises = "stdio anet";
}
if (pledge(0, 0)) {
if (!once.exchange(true))
SLOG("warning: this OS doesn't support pledge() security");
} else if (pledge(promises, 0)) {
perror("pledge");
exit(1);
}
}
}

server_->lock();
dll_make_first(&server_->idle_workers, &elem_);
server_->worker_count.fetch_add(1, std::memory_order_acq_rel);
Expand Down

0 comments on commit 8fa1702

Please sign in to comment.