|
| 1 | +/* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | +/* |
| 3 | + * Copyright (C) 2022 Micron Technology, Inc. All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <assert.h> |
| 7 | +#include <errno.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stddef.h> |
| 10 | +#include <sys/socket.h> |
| 11 | +#include <sys/un.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +#include <bsd/string.h> |
| 15 | +#include <microhttpd.h> |
| 16 | + |
| 17 | +#include <rest/rest.h> |
| 18 | + |
| 19 | +#include <hse_util/assert.h> |
| 20 | +#include <hse_util/compiler.h> |
| 21 | +#include <hse_util/event_counter.h> |
| 22 | +#include <hse_util/hse_err.h> |
| 23 | +#include <hse_util/logging.h> |
| 24 | +#include <hse_util/table.h> |
| 25 | + |
| 26 | +struct server { |
| 27 | + struct MHD_Daemon *daemon; |
| 28 | + bool initialized; |
| 29 | + int socket_fd; |
| 30 | + char socket_path[sizeof(((struct sockaddr_un *)NULL)->sun_path)]; |
| 31 | +}; |
| 32 | + |
| 33 | +struct context { |
| 34 | + struct MHD_Connection *connection; |
| 35 | + enum rest_method method; |
| 36 | +}; |
| 37 | + |
| 38 | +static struct server server; |
| 39 | + |
| 40 | +static struct table *route_map; |
| 41 | + |
| 42 | +merr_t |
| 43 | +rest_register_routes(size_t routec, const struct rest_route *routev) |
| 44 | +{ |
| 45 | + if (routec == 0) |
| 46 | + return 0; |
| 47 | + |
| 48 | + if (routec > 0 && !routev) |
| 49 | + return merr(EINVAL); |
| 50 | + |
| 51 | + for (size_t i = 0; i < routec; i++) { |
| 52 | + const void *elem = table_append_object(route_map, (void *)(routev + i)); |
| 53 | + if (ev(!elem)) |
| 54 | + return merr(ENOMEM); |
| 55 | + } |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static merr_t |
| 61 | +create_socket(const char *const socket_path, int *const socket_fd) |
| 62 | +{ |
| 63 | + struct sockaddr_un sock; |
| 64 | + int tmp_fd; |
| 65 | + int rc; |
| 66 | + size_t n; |
| 67 | + |
| 68 | + INVARIANT(socket_path); |
| 69 | + INVARIANT(socket_fd); |
| 70 | + |
| 71 | + /* In case the program exited inadvertently on the last run, |
| 72 | + * remove the socket. |
| 73 | + */ |
| 74 | + unlink(socket_path); |
| 75 | + |
| 76 | + tmp_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 77 | + if (tmp_fd == -1) |
| 78 | + return merr(errno); |
| 79 | + |
| 80 | + sock.sun_family = AF_UNIX; |
| 81 | + n = strlcpy(sock.sun_path, socket_path, sizeof(sock.sun_path)); |
| 82 | + if (n >= sizeof(sock.sun_path)) |
| 83 | + return merr(EINVAL); |
| 84 | + |
| 85 | + rc = bind(tmp_fd, (const struct sockaddr *)&sock, sizeof(sock)); |
| 86 | + if (rc == -1) { |
| 87 | + close(tmp_fd); |
| 88 | + |
| 89 | + return merr(errno); |
| 90 | + } |
| 91 | + |
| 92 | + rc = listen(tmp_fd, 1); |
| 93 | + if (rc == -1) { |
| 94 | + close(tmp_fd); |
| 95 | + |
| 96 | + return merr(errno); |
| 97 | + } |
| 98 | + |
| 99 | + *socket_fd = tmp_fd; |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +static enum MHD_Result |
| 105 | +response( |
| 106 | + void *const cls, |
| 107 | + struct MHD_Connection *const connection, |
| 108 | + const char *const url, |
| 109 | + const char *const method, |
| 110 | + const char *const version, |
| 111 | + const char *const upload_data, |
| 112 | + unsigned long *const upload_data_size, |
| 113 | + void **const con_cls) |
| 114 | +{ |
| 115 | + struct context *ctx; |
| 116 | + |
| 117 | + log_debug("REST request received: %s %s %s", version, method, url); |
| 118 | + |
| 119 | + if (!*con_cls) { |
| 120 | + ctx = calloc(1, sizeof(*ctx)); |
| 121 | + if (ev(!ctx)) |
| 122 | + return MHD_NO; |
| 123 | + |
| 124 | + ctx->connection = connection; |
| 125 | + |
| 126 | + if (strcmp(method, "GET") == 0) { |
| 127 | + ctx->method = REST_METHOD_GET; |
| 128 | + } else if (strcmp(method, "PUT") == 0) { |
| 129 | + ctx->method = REST_METHOD_PUT; |
| 130 | + } |
| 131 | + |
| 132 | + *con_cls = ctx; |
| 133 | + |
| 134 | + return MHD_YES; |
| 135 | + } |
| 136 | + |
| 137 | + ctx = *con_cls; |
| 138 | + |
| 139 | + switch (ctx->method) { |
| 140 | + case REST_METHOD_GET: |
| 141 | + break; |
| 142 | + case REST_METHOD_PUT: |
| 143 | + break; |
| 144 | + default: |
| 145 | + abort(); |
| 146 | + } |
| 147 | + |
| 148 | + return MHD_NO; |
| 149 | +} |
| 150 | + |
| 151 | +static void * |
| 152 | +completed( |
| 153 | + void *const cls, |
| 154 | + struct MHD_Connection *const connection, |
| 155 | + void **const con_cls, |
| 156 | + const enum MHD_RequestTerminationCode toe) |
| 157 | +{ |
| 158 | + struct context *ctx = *con_cls; |
| 159 | + |
| 160 | + free(ctx); |
| 161 | + |
| 162 | + return NULL; |
| 163 | +} |
| 164 | + |
| 165 | +static void |
| 166 | +panic( |
| 167 | + void *const cls, |
| 168 | + const char *const file, |
| 169 | + const unsigned int line, |
| 170 | + const char *const reason) |
| 171 | +{ |
| 172 | + log_err("REST server paniced at %s:%u: %s", file, line, reason); |
| 173 | +} |
| 174 | + |
| 175 | +merr_t |
| 176 | +rest_server_start(const char *socket_path) |
| 177 | +{ |
| 178 | + merr_t err; |
| 179 | + size_t n HSE_MAYBE_UNUSED; |
| 180 | + |
| 181 | + if (server.initialized) |
| 182 | + return 0; |
| 183 | + |
| 184 | + route_map = table_create(8, sizeof(struct rest_route), false); |
| 185 | + if (ev(!route_map)) |
| 186 | + return merr(ENOMEM); |
| 187 | + |
| 188 | + /* Keep a copy of the socket path so we can unlink it when closing the |
| 189 | + * rest server. |
| 190 | + */ |
| 191 | + n = strlcpy(server.socket_path, socket_path, sizeof(server.socket_path)); |
| 192 | + assert(n < sizeof(server.socket_path)); |
| 193 | + |
| 194 | + err = create_socket(socket_path, &server.socket_fd); |
| 195 | + if (ev(err)) |
| 196 | + goto out; |
| 197 | + |
| 198 | + /* The REST server panicing should not abort the entire program */ |
| 199 | + MHD_set_panic_func(panic, NULL); |
| 200 | + |
| 201 | + server.daemon = MHD_start_daemon( |
| 202 | + MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_DEBUG, |
| 203 | + 0, |
| 204 | + NULL, |
| 205 | + NULL, |
| 206 | + response, |
| 207 | + NULL, |
| 208 | + MHD_OPTION_LISTEN_SOCKET, |
| 209 | + server.socket_fd, |
| 210 | + MHD_OPTION_CONNECTION_LIMIT, |
| 211 | + 1, |
| 212 | + MHD_OPTION_CONNECTION_TIMEOUT, |
| 213 | + 120, |
| 214 | + MHD_OPTION_NOTIFY_COMPLETED, |
| 215 | + completed, |
| 216 | + NULL, |
| 217 | + MHD_OPTION_END); |
| 218 | + if (ev(!server.daemon)) { |
| 219 | + err = merr(ENOANO); |
| 220 | + goto out; |
| 221 | + } |
| 222 | + |
| 223 | + server.initialized = true; |
| 224 | + |
| 225 | +out: |
| 226 | + if (err) { |
| 227 | + table_destroy(route_map); |
| 228 | + if (server.socket_fd) |
| 229 | + unlink(server.socket_path); |
| 230 | + } |
| 231 | + |
| 232 | + return 0; |
| 233 | +} |
| 234 | + |
| 235 | +void |
| 236 | +rest_server_stop(void) |
| 237 | +{ |
| 238 | + if (!server.initialized) |
| 239 | + return; |
| 240 | + |
| 241 | + unlink(server.socket_path); |
| 242 | + table_destroy(route_map); |
| 243 | + MHD_stop_daemon(server.daemon); |
| 244 | + |
| 245 | + memset(&server, 0, sizeof(server)); |
| 246 | +} |
0 commit comments