|
| 1 | +#include "tcp_server.h" |
| 2 | + |
| 3 | +#include <stdarg.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdbool.h> |
| 7 | +#include <string.h> |
| 8 | +#include <errno.h> |
| 9 | + |
| 10 | +#include <signal.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <sys/wait.h> |
| 13 | + |
| 14 | +#include <sys/types.h> |
| 15 | +#include <sys/socket.h> |
| 16 | +#include <netdb.h> |
| 17 | + |
| 18 | +void die(const char *msg, ...) |
| 19 | +{ |
| 20 | + va_list ap; |
| 21 | + |
| 22 | + va_start(ap, msg); |
| 23 | + vfprintf(stderr, msg, ap); |
| 24 | + va_end(ap); |
| 25 | + fputc('\n', stderr); |
| 26 | + exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +static char *read_argument(uint8_t **param_buf, size_t *param_buf_size, FILE *instream) { |
| 30 | + bool done = false; |
| 31 | + uint8_t *buf = *param_buf; |
| 32 | + size_t bufsize = *param_buf_size; |
| 33 | + size_t bufpos = 0; |
| 34 | + while (!done) { |
| 35 | + if (bufpos == bufsize) { |
| 36 | + bufsize += 1024; |
| 37 | + buf = (uint8_t *)realloc(buf, bufsize); |
| 38 | + if (!buf) { |
| 39 | + die("failed to allocate memory"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + int c = fgetc(instream); |
| 44 | + if (c == EOF) { |
| 45 | + die("unexpected EOF client socket"); |
| 46 | + } |
| 47 | + buf[bufpos++] = (uint8_t)c; |
| 48 | + if (c == 0) { |
| 49 | + // done reading argument |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + *param_buf = buf; |
| 54 | + *param_buf_size = bufsize; |
| 55 | + return strdup((char *)buf); |
| 56 | +} |
| 57 | + |
| 58 | +static int read_arguments(int argc, char **argv, FILE *instream) { |
| 59 | + int i = 1; |
| 60 | + size_t param_buf_size = 0; |
| 61 | + uint8_t *param_buf = nullptr; |
| 62 | + |
| 63 | + for (i = 1; i < argc; i++) { |
| 64 | + argv[i] = read_argument(¶m_buf, ¶m_buf_size, instream); |
| 65 | + } |
| 66 | + |
| 67 | + free(param_buf); |
| 68 | + return i; |
| 69 | +} |
| 70 | + |
| 71 | +static int serve_model( |
| 72 | + gpt_params params, |
| 73 | + gpt_vocab vocab, |
| 74 | + llama_model model, |
| 75 | + int64_t t_load_us, |
| 76 | + int64_t t_main_start_us, |
| 77 | + int sock_fd) |
| 78 | +{ |
| 79 | + char *response_data; |
| 80 | + int argc; |
| 81 | + char **argv; |
| 82 | + FILE *instream = fdopen(sock_fd, "r"); |
| 83 | + FILE *outstream = fdopen(sock_fd, "w"); |
| 84 | + setvbuf(instream, NULL, _IONBF, 0); |
| 85 | + |
| 86 | + // start by reading the parameter count |
| 87 | + if (fscanf(instream, "%d\n", &argc) != 1) { |
| 88 | + fprintf(outstream, "Error: First line must be character count\n"); |
| 89 | + fflush(outstream); |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + argc += 1; // add one extra argument to emulate the program command line |
| 94 | + argv = (char **)malloc(argc * sizeof *argv); |
| 95 | + argv[0] = nullptr; |
| 96 | + if (read_arguments(argc, argv, instream) != argc) { |
| 97 | + fprintf(outstream, "Error: Failed to read arguments\n"); |
| 98 | + fflush(outstream); |
| 99 | + } |
| 100 | + |
| 101 | + if (gpt_params_parse(argc, argv, params) == false) { |
| 102 | + fprintf(outstream, "Error: Failed to parse parameters\n"); |
| 103 | + fflush(outstream); |
| 104 | + return 1; |
| 105 | + } |
| 106 | + |
| 107 | + for (int i = 1; i < argc; i++) { |
| 108 | + free(argv[i]); |
| 109 | + } |
| 110 | + free(argv); |
| 111 | + |
| 112 | + return llama_main(params, vocab, model, t_load_us, t_main_start_us, instream, outstream, outstream); |
| 113 | +} |
| 114 | + |
| 115 | +int listen_tcp( |
| 116 | + gpt_params params, |
| 117 | + gpt_vocab vocab, |
| 118 | + llama_model model, |
| 119 | + int64_t t_main_start_us, |
| 120 | + int64_t t_load_us) { |
| 121 | + int listen_fd; |
| 122 | + int status; |
| 123 | + pid_t child; |
| 124 | + struct addrinfo hints; |
| 125 | + struct addrinfo *servinfo, *p; |
| 126 | + int yes = 1; |
| 127 | + |
| 128 | + memset(&hints, 0, sizeof hints); |
| 129 | + hints.ai_family = AF_INET; |
| 130 | + hints.ai_socktype = SOCK_STREAM; |
| 131 | + hints.ai_flags = AI_PASSIVE; |
| 132 | + |
| 133 | + // This should only ever listen on a loopback address. Access from outside |
| 134 | + // should be proxied via nginx or similar software |
| 135 | + status = getaddrinfo("127.0.0.1", params.listen_port.c_str(), &hints, &servinfo); |
| 136 | + if (status) { |
| 137 | + die("getaddrinfo error: %s", gai_strerror(status)); |
| 138 | + } |
| 139 | + |
| 140 | + // bind to the first addrinfo we can from the getaddrinfo results |
| 141 | + for (p = servinfo; p != NULL; p = p->ai_next) { |
| 142 | + listen_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); |
| 143 | + if (listen_fd == -1) { |
| 144 | + perror("server: socket"); |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)) { |
| 149 | + die("setsockopt error: %s", params.listen_port.c_str(), strerror(errno)); |
| 150 | + } |
| 151 | + |
| 152 | + if (bind(listen_fd, p->ai_addr, p->ai_addrlen) == 0) { |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + close(listen_fd); |
| 157 | + perror("server: bind"); |
| 158 | + } |
| 159 | + |
| 160 | + freeaddrinfo(servinfo); |
| 161 | + |
| 162 | + if (p == NULL) { |
| 163 | + die("failed to bind: %s", strerror(errno)); |
| 164 | + } |
| 165 | + |
| 166 | + if (listen(listen_fd, 20)) { |
| 167 | + die("listen error: %s", strerror(errno)); |
| 168 | + } |
| 169 | + // Don't track child processes, so ignore SIGCHLD to prevent zombies |
| 170 | + signal(SIGCHLD, SIG_IGN); |
| 171 | + |
| 172 | + for (;;) { |
| 173 | + struct sockaddr_in client_addr = {0}; |
| 174 | + socklen_t client_addr_len = 0; |
| 175 | + |
| 176 | + int sock_fd = accept(listen_fd, |
| 177 | + (struct sockaddr *)&client_addr, |
| 178 | + &client_addr_len); |
| 179 | + if (sock_fd < 0) { |
| 180 | + fprintf(stderr, "accept error: %s\n", strerror(errno)); |
| 181 | + break; |
| 182 | + } |
| 183 | + |
| 184 | + child = fork(); |
| 185 | + if (child == 0) { |
| 186 | + // close the listen_fd since we won't use it in the child |
| 187 | + close(listen_fd); |
| 188 | + int ret = serve_model(params, vocab, model, t_main_start_us, t_load_us, sock_fd); |
| 189 | + close(sock_fd); |
| 190 | + return ret; |
| 191 | + } else { |
| 192 | + // close the client since we won't use it in the server |
| 193 | + close(sock_fd); |
| 194 | + sock_fd = 0; |
| 195 | + } |
| 196 | + } |
| 197 | + close(listen_fd); |
| 198 | + |
| 199 | + // ignore SIGTERM since we'll send it to the group |
| 200 | + signal(SIGTERM, SIG_IGN); |
| 201 | + // tell children to exit |
| 202 | + kill(0, SIGTERM); |
| 203 | + // wait for children to terminate |
| 204 | + wait(&status); |
| 205 | + return 0; |
| 206 | +} |
0 commit comments