diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 63 |
1 files changed, 42 insertions, 21 deletions
@@ -1,14 +1,16 @@ #include <stdio.h> +#include <stdlib.h> #include <err.h> #include <errno.h> #include <string.h> +#include <syslog.h> #include <unistd.h> #include <sys/un.h> #include <sys/socket.h> #include "llm.h" -#define SOCK_PATH "/var/run/lex.sock" +#define SOCK_PATH "/var/run/lexd/sock" #define MODEL_PATH "qwen2.5-3b-instruct-q4_k_m.gguf" static void usage(const char *prog) @@ -27,12 +29,12 @@ static void loop(struct llm_ctx *llm, int lsock) csock = accept(lsock, NULL, NULL); if (csock == -1) { if (errno != EINTR) - warn("accept()"); + syslog(LOG_WARNING, "accept() failed: %m"); continue; } if (!(cfp = fdopen(csock, "r+"))) { - warn("cfp fdopen()"); + syslog(LOG_WARNING, "fdopen() failed for cfp: %m"); close(csock); continue; } @@ -40,7 +42,7 @@ static void loop(struct llm_ctx *llm, int lsock) if (fgets(line, sizeof(line), cfp) != NULL) { len = strcspn(line, "\n"); if (line[len] == '\0' && len == sizeof(line) - 1) { - warnx("request too large"); + syslog(LOG_WARNING, "request too large"); // drain and skip while ((c = fgetc(cfp)) != '\n' && c != EOF) ; @@ -63,8 +65,10 @@ int sock_init(void) struct sockaddr_un addr; sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock == -1) - err(1, "socket()"); + if (sock == -1) { + syslog(LOG_ERR, "socket error: %m"); + exit(1); + } memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; @@ -72,11 +76,15 @@ int sock_init(void) unlink(SOCK_PATH); /* remove stale socket from a previous run */ - if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) - err(1, "bind()"); + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + syslog(LOG_ERR, "bind failed: %m"); + exit(1); + } - if (listen(sock, 1) == -1) /* backlog = 1 */ - err(1, "listen()"); + if (listen(sock, 1) == -1) { /* backlog = 1 */ + syslog(LOG_ERR, "listen(): %m"); + exit(1); + } return sock; } @@ -91,6 +99,8 @@ int main(int argc , char *argv[]) prog = argv[0]; model_path = MODEL_PATH; + openlog("lexd", LOG_PID | LOG_NDELAY, LOG_DAEMON); + while ((opt = getopt(argc, argv, "m:")) != -1) { switch (opt) { case 'm': @@ -106,23 +116,34 @@ int main(int argc , char *argv[]) if (argc != 0) usage(prog); - if (unveil(model_path, "r") == -1) - err(1, "unveil %s failed", model_path); - if (unveil(SOCK_PATH, "rwc") == -1) - err(1, "unveil %s failed", SOCK_PATH); - if (unveil(NULL, NULL) == -1) - err(1, "unveil lock failed"); + if (unveil(model_path, "r") == -1) { + syslog(LOG_ERR, "unveil %s: %m", model_path); + exit(1); + } + + if (unveil(SOCK_PATH, "rwc") == -1) { + syslog(LOG_ERR, "unveil %s: %m", SOCK_PATH); + exit(1); + } + + if (unveil(NULL, NULL) == -1) { + syslog(LOG_ERR, "unveil lock failed: %m"); + exit(1); + } sock = sock_init(); - if (!(llm = llm_init(model_path))) - errx(1, "Failed to load model"); + if (!(llm = llm_init(model_path))) { + syslog(LOG_ERR, "failed to load model"); + exit(1); + } /* llm_init() calls mlock, hence pledge() after that. */ - if (pledge("stdio unix", NULL) == -1) - err(1, "secondary pledge failed"); + if (pledge("stdio unix", NULL) == -1) { + syslog(LOG_ERR, "secondary pledge: %m"); + exit(1); + } loop(llm, sock); /* doesn't return */ - return 0; } |
