| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2026 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | #include <ctype.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #ifdef WIN32 |
| 28 | # define GROONGA_MAIN |
| 29 | #endif /* WIN32 */ |
| 30 | #include <grn.h> |
| 31 | |
| 32 | #include <grn_com.h> |
| 33 | #include <grn_ctx_impl.h> |
| 34 | #include <grn_proc.h> |
| 35 | #include <grn_db.h> |
| 36 | #include <grn_util.h> |
| 37 | #include <grn_error.h> |
| 38 | |
| 39 | #ifdef HAVE_SYS_WAIT_H |
| 40 | # include <sys/wait.h> |
| 41 | #endif /* HAVE_SYS_WAIT_H */ |
| 42 | #ifdef HAVE_SYS_SOCKET_H |
| 43 | # include <sys/socket.h> |
| 44 | #endif /* HAVE_SYS_SOCKET_H */ |
| 45 | #ifndef WIN32 |
| 46 | # include <netinet/in.h> |
| 47 | #endif /* WIN32 */ |
| 48 | |
| 49 | #ifdef HAVE_SYS_RESOURCE_H |
| 50 | # include <sys/resource.h> |
| 51 | #endif /* HAVE_SYS_RESOURCE_H */ |
| 52 | |
| 53 | #ifdef WIN32 |
| 54 | # include <io.h> |
| 55 | # include <direct.h> |
| 56 | #else /* WIN32 */ |
| 57 | # include <sys/uio.h> |
| 58 | #endif /* WIN32 */ |
| 59 | |
| 60 | #if !defined(WIN32) && defined(HAVE_SIGNAL_H) |
| 61 | # define ENABLE_LOG_REOPEN_BY_SIGNAL |
| 62 | #endif |
| 63 | |
| 64 | #ifdef ENABLE_LOG_REOPEN_BY_SIGNAL |
| 65 | # include <signal.h> |
| 66 | #endif |
| 67 | |
| 68 | #ifndef USE_MSG_NOSIGNAL |
| 69 | # ifdef MSG_NOSIGNAL |
| 70 | # undef MSG_NOSIGNAL |
| 71 | # endif |
| 72 | # define MSG_NOSIGNAL 0 |
| 73 | #endif /* USE_MSG_NOSIGNAL */ |
| 74 | |
| 75 | #ifndef STDIN_FILENO |
| 76 | # define STDIN_FILENO 0 |
| 77 | #endif /* STDIN_FILENO */ |
| 78 | #ifndef STDOUT_FILENO |
| 79 | # define STDOUT_FILENO 1 |
| 80 | #endif /* STDOUT_FILENO */ |
| 81 | #ifndef STDERR_FILENO |
| 82 | # define STDERR_FILENO 2 |
| 83 | #endif /* STDERR_FILENO */ |
| 84 | |
| 85 | #define DEFAULT_HTTP_PORT 10041 |
| 86 | #define DEFAULT_GQTP_PORT 10043 |
| 87 | #define DEFAULT_DEST "localhost" |
| 88 | #define DEFAULT_MAX_N_FLOATING_THREADS 8 |
| 89 | #define MAX_CON 0x10000 |
| 90 | |
| 91 | #define RLIMIT_NOFILE_MINIMUM 4096 |
| 92 | |
| 93 | static char bind_address[HOST_NAME_MAX + 1]; |
| 94 | static char hostname[HOST_NAME_MAX + 1]; |
| 95 | static int port = DEFAULT_GQTP_PORT; |
| 96 | static bool batchmode = false; |
| 97 | static int number_of_lines = 0; |
| 98 | static int newdb; |
| 99 | static bool is_daemon_mode = false; |
| 100 | static int listen_backlog = GRN_COM_EVENT_LISTEN_BACKLOG_DEFAULT; |
| 101 | static int (*do_client)(int argc, char **argv); |
| 102 | static int (*do_server)(char *path); |
| 103 | static const char *pid_file_path = NULL; |
| 104 | static const char *input_path = NULL; |
| 105 | static grn_file_reader *input_reader = NULL; |
| 106 | static FILE *output = NULL; |
| 107 | static bool is_memcached_mode = false; |
| 108 | static const char *memcached_column_name = NULL; |
| 109 | |
| 110 | static int ready_notify_pipe[2]; |
| 111 | #define PIPE_READ 0 |
| 112 | #define PIPE_WRITE 1 |
| 113 | |
| 114 | static grn_encoding encoding; |
| 115 | static const char *windows_event_source_name = "Groonga"; |
| 116 | static bool use_windows_event_log = false; |
| 117 | static grn_obj http_response_server_line; |
| 118 | static grn_wal_role wal_role = GRN_WAL_ROLE_NONE; |
| 119 | static grn_wal_role worker_wal_role = GRN_WAL_ROLE_NONE; |
| 120 | |
| 121 | static bool running_event_loop = false; |
| 122 | |
| 123 | #ifdef ENABLE_LOG_REOPEN_BY_SIGNAL |
| 124 | static bool usr1_received = false; |
| 125 | |
| 126 | static void |
| 127 | usr1_handler(int signal_number, siginfo_t *info, void *context) |
| 128 | { |
| 129 | usr1_received = true; |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | set_usr1_handler(void) |
| 134 | { |
| 135 | struct sigaction action; |
| 136 | sigemptyset(&action.sa_mask); |
| 137 | action.sa_sigaction = usr1_handler; |
| 138 | action.sa_flags = SA_SIGINFO | SA_ONSTACK; |
| 139 | sigaction(SIGUSR1, &action, NULL); |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | reopen_log_by_signal(grn_ctx *ctx) |
| 144 | { |
| 145 | if (!usr1_received) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | usr1_received = false; |
| 150 | grn_log_reopen(ctx); |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | static int |
| 155 | grn_rc_to_exit_code(grn_rc rc) |
| 156 | { |
| 157 | if (rc == GRN_SUCCESS) { |
| 158 | return EXIT_SUCCESS; |
| 159 | } else { |
| 160 | return EXIT_FAILURE; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | break_accept_event_loop(grn_ctx *ctx) |
| 166 | { |
| 167 | grn_com *client; |
| 168 | const char *address; |
| 169 | |
| 170 | if (strcmp(bind_address, "0.0.0.0") == 0) { |
| 171 | address = "127.0.0.1"; |
| 172 | } else if (strcmp(bind_address, "::") == 0) { |
| 173 | address = "::1"; |
| 174 | } else { |
| 175 | address = bind_address; |
| 176 | } |
| 177 | client = grn_com_copen(ctx, NULL, address, port); |
| 178 | if (client) { |
| 179 | grn_com_close(ctx, client); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | #ifdef GRN_WITH_LIBEDIT |
| 184 | # include <locale.h> |
| 185 | # include <histedit.h> |
| 186 | static EditLine *line_editor = NULL; |
| 187 | static HistoryW *line_editor_history = NULL; |
| 188 | static HistEventW line_editor_history_event; |
| 189 | static char line_editor_history_path[PATH_MAX] = ""; |
| 190 | |
| 191 | static const wchar_t * |
| 192 | line_editor_prompt(EditLine *e __attribute__((unused))) |
| 193 | { |
| 194 | return L"> "; |
| 195 | } |
| 196 | static const wchar_t *const line_editor_editor = L"emacs"; |
| 197 | |
| 198 | static void |
| 199 | line_editor_init(int argc __attribute__((unused)), char *argv[]) |
| 200 | { |
| 201 | const char *const HOME_PATH = getenv("HOME"); |
| 202 | const char *const HISTORY_PATH = "/.groonga-history"; |
| 203 | |
| 204 | setlocale(LC_ALL, ""); |
| 205 | |
| 206 | if (strlen(HOME_PATH) + strlen(HISTORY_PATH) < PATH_MAX) { |
| 207 | grn_strcpy(line_editor_history_path, PATH_MAX, HOME_PATH); |
| 208 | grn_strcat(line_editor_history_path, PATH_MAX, HISTORY_PATH); |
| 209 | } else { |
| 210 | line_editor_history_path[0] = '\0'; |
| 211 | } |
| 212 | |
| 213 | line_editor_history = history_winit(); |
| 214 | history_w(line_editor_history, &line_editor_history_event, H_SETSIZE, 200); |
| 215 | if (line_editor_history_path[0]) { |
| 216 | history_w(line_editor_history, |
| 217 | &line_editor_history_event, |
| 218 | H_LOAD, |
| 219 | line_editor_history_path); |
| 220 | } |
| 221 | |
| 222 | line_editor = el_init(argv[0], stdin, stdout, stderr); |
| 223 | el_wset(line_editor, EL_PROMPT, &line_editor_prompt); |
| 224 | el_wset(line_editor, EL_EDITOR, line_editor_editor); |
| 225 | el_wset(line_editor, EL_HIST, history_w, line_editor_history); |
| 226 | el_source(line_editor, NULL); |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | line_editor_fin(void) |
| 231 | { |
| 232 | if (line_editor) { |
| 233 | el_end(line_editor); |
| 234 | if (line_editor_history) { |
| 235 | if (line_editor_history_path[0]) { |
| 236 | history_w(line_editor_history, |
| 237 | &line_editor_history_event, |
| 238 | H_SAVE, |
| 239 | line_editor_history_path); |
| 240 | } |
| 241 | history_wend(line_editor_history); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | static grn_rc |
| 247 | line_editor_fgets(grn_ctx *ctx, grn_obj *buf) |
| 248 | { |
| 249 | grn_rc rc = GRN_SUCCESS; |
| 250 | const wchar_t *line; |
| 251 | int nchar; |
| 252 | line = el_wgets(line_editor, &nchar); |
| 253 | if (nchar > 0) { |
| 254 | int i; |
| 255 | char multibyte_buf[MB_CUR_MAX]; |
| 256 | size_t multibyte_len; |
| 257 | mbstate_t ps; |
| 258 | history_w(line_editor_history, &line_editor_history_event, H_ENTER, line); |
| 259 | memset(&ps, 0, sizeof(ps)); |
| 260 | for (i = 0; i < nchar; i++) { |
| 261 | multibyte_len = wcrtomb(multibyte_buf, line[i], &ps); |
| 262 | if (multibyte_len == (size_t)-1) { |
| 263 | GRN_LOG(ctx, |
| 264 | GRN_LOG_WARNING, |
| 265 | "[prompt][libedit] failed to read input: %s", |
| 266 | strerror(errno)); |
| 267 | rc = GRN_INVALID_ARGUMENT; |
| 268 | } else { |
| 269 | GRN_TEXT_PUT(ctx, buf, multibyte_buf, multibyte_len); |
| 270 | } |
| 271 | } |
| 272 | } else { |
| 273 | rc = GRN_END_OF_DATA; |
| 274 | } |
| 275 | return rc; |
| 276 | } |
| 277 | #endif /* GRN_WITH_LIBEDIT */ |
| 278 | |
| 279 | static inline grn_rc |
| 280 | read_next_line(grn_ctx *ctx, grn_obj *buf) |
| 281 | { |
| 282 | static bool the_first_read = true; |
| 283 | grn_rc rc = GRN_SUCCESS; |
| 284 | if (!batchmode) { |
| 285 | #ifdef GRN_WITH_LIBEDIT |
| 286 | rc = line_editor_fgets(ctx, buf); |
| 287 | #else |
| 288 | fprintf(stderr, "> "); |
| 289 | fflush(stderr); |
| 290 | rc = grn_file_reader_read_line(ctx, input_reader, buf); |
| 291 | #endif |
| 292 | } else { |
| 293 | rc = grn_file_reader_read_line(ctx, input_reader, buf); |
| 294 | if (rc != GRN_END_OF_DATA) { |
| 295 | number_of_lines++; |
| 296 | } |
| 297 | } |
| 298 | if (the_first_read && GRN_TEXT_LEN(buf) > 0) { |
| 299 | const char bom[] = {0xef, 0xbb, 0xbf}; |
| 300 | if (GRN_CTX_GET_ENCODING(ctx) == GRN_ENC_UTF8 && GRN_TEXT_LEN(buf) > 3 && |
| 301 | !memcmp(GRN_TEXT_VALUE(buf), bom, 3)) { |
| 302 | grn_obj buf_without_bom; |
| 303 | GRN_TEXT_INIT(&buf_without_bom, 0); |
| 304 | GRN_TEXT_PUT(ctx, |
| 305 | &buf_without_bom, |
| 306 | GRN_TEXT_VALUE(buf) + 3, |
| 307 | GRN_TEXT_LEN(buf) - 3); |
| 308 | GRN_TEXT_SET(ctx, |
| 309 | buf, |
| 310 | GRN_TEXT_VALUE(&buf_without_bom), |
| 311 | GRN_TEXT_LEN(&buf_without_bom)); |
| 312 | grn_obj_unlink(ctx, &buf_without_bom); |
| 313 | } |
| 314 | the_first_read = false; |
| 315 | } |
| 316 | if (GRN_TEXT_LEN(buf) > 0 && |
| 317 | GRN_TEXT_VALUE(buf)[GRN_TEXT_LEN(buf) - 1] == '\n') { |
| 318 | grn_bulk_truncate(ctx, buf, GRN_TEXT_LEN(buf) - 1); |
| 319 | } |
| 320 | if (GRN_TEXT_LEN(buf) > 0 && |
| 321 | GRN_TEXT_VALUE(buf)[GRN_TEXT_LEN(buf) - 1] == '\r') { |
| 322 | grn_bulk_truncate(ctx, buf, GRN_TEXT_LEN(buf) - 1); |
| 323 | } |
| 324 | return rc; |
| 325 | } |
| 326 | |
| 327 | static inline grn_rc |
| 328 | prompt(grn_ctx *ctx, grn_obj *buf) |
| 329 | { |
| 330 | grn_rc rc = GRN_SUCCESS; |
| 331 | GRN_BULK_REWIND(buf); |
| 332 | while (rc == GRN_SUCCESS) { |
| 333 | rc = read_next_line(ctx, buf); |
| 334 | if (rc == GRN_SUCCESS && GRN_TEXT_LEN(buf) > 0 && |
| 335 | GRN_TEXT_VALUE(buf)[GRN_TEXT_LEN(buf) - 1] == '\\') { |
| 336 | rc = grn_bulk_truncate(ctx, buf, GRN_TEXT_LEN(buf) - 1); |
| 337 | } else { |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | return rc; |
| 342 | } |
| 343 | |
| 344 | static void |
| 345 | output_envelope( |
| 346 | grn_ctx *ctx, grn_rc rc, grn_obj *head, grn_obj *body, grn_obj *foot) |
| 347 | { |
| 348 | grn_output_envelope(ctx, rc, head, body, foot, input_path, number_of_lines); |
| 349 | } |
| 350 | |
| 351 | static void |
| 352 | ensure_write(grn_ctx *ctx, FILE *output, const char *data, size_t size) |
| 353 | { |
| 354 | const char *rest_data = data; |
| 355 | size_t rest_size = size; |
| 356 | while (rest_size > 0) { |
| 357 | errno = 0; |
| 358 | size_t written = fwrite(rest_data, 1, rest_size, output); |
| 359 | if (!(errno == 0 || errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 360 | SERR("fwrite"); |
| 361 | break; |
| 362 | } |
| 363 | rest_data += written; |
| 364 | rest_size -= written; |
| 365 | if (rest_size > 0) { |
| 366 | fflush(output); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | typedef struct { |
| 372 | FILE *output; |
| 373 | bool is_outputting; |
| 374 | } standalone_output_context; |
| 375 | |
| 376 | static void |
| 377 | s_output_raw(grn_ctx *ctx, int flags, standalone_output_context *output_context) |
| 378 | { |
| 379 | FILE *stream = output_context->output; |
| 380 | char *chunk = NULL; |
| 381 | unsigned int chunk_size = 0; |
| 382 | int recv_flags; |
| 383 | size_t written = 0; |
| 384 | |
| 385 | grn_ctx_recv(ctx, &chunk, &chunk_size, &recv_flags); |
| 386 | if (chunk_size > 0) { |
| 387 | ensure_write(ctx, stream, chunk, chunk_size); |
| 388 | written += chunk_size; |
| 389 | } |
| 390 | |
| 391 | bool is_last_message = (flags & GRN_CTX_TAIL); |
| 392 | if (is_last_message) { |
| 393 | grn_obj *command; |
| 394 | |
| 395 | if (grn_ctx_get_output_type(ctx) == GRN_CONTENT_GROONGA_COMMAND_LIST && |
| 396 | chunk_size > 0 && chunk[chunk_size - 1] != '\n') { |
| 397 | ensure_write(ctx, stream, "\n", 1); |
| 398 | written++; |
| 399 | } |
| 400 | fflush(stream); |
| 401 | |
| 402 | command = GRN_CTX_USER_DATA(ctx)->ptr; |
| 403 | GRN_BULK_REWIND(command); |
| 404 | } |
| 405 | |
| 406 | GRN_QUERY_LOG(ctx, |
| 407 | GRN_QUERY_LOG_SIZE, |
| 408 | ":", |
| 409 | "send(%" GRN_FMT_SIZE ")", |
| 410 | written); |
| 411 | } |
| 412 | |
| 413 | static void |
| 414 | s_output_typed(grn_ctx *ctx, |
| 415 | int flags, |
| 416 | standalone_output_context *output_context) |
| 417 | { |
| 418 | if (!ctx) { |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | if (!ctx->impl) { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | FILE *stream = output_context->output; |
| 427 | char *chunk = NULL; |
| 428 | unsigned int chunk_size = 0; |
| 429 | int recv_flags; |
| 430 | grn_obj body; |
| 431 | grn_obj *command; |
| 432 | bool is_last_message = (flags & GRN_CTX_TAIL); |
| 433 | |
| 434 | GRN_TEXT_INIT(&body, GRN_OBJ_DO_SHALLOW_COPY); |
| 435 | grn_ctx_recv(ctx, &chunk, &chunk_size, &recv_flags); |
| 436 | GRN_TEXT_SET(ctx, &body, chunk, chunk_size); |
| 437 | |
| 438 | if (GRN_TEXT_LEN(&body) == 0 && ctx->rc == GRN_SUCCESS) { |
| 439 | GRN_OBJ_FIN(ctx, &body); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | grn_obj head, foot; |
| 444 | GRN_TEXT_INIT(&head, 0); |
| 445 | GRN_TEXT_INIT(&foot, 0); |
| 446 | if (!output_context->is_outputting) { |
| 447 | if (is_last_message) { |
| 448 | output_envelope(ctx, ctx->rc, &head, &body, &foot); |
| 449 | } else { |
| 450 | grn_output_envelope_open(ctx, &head); |
| 451 | } |
| 452 | output_context->is_outputting = true; |
| 453 | } else { |
| 454 | if (is_last_message) { |
| 455 | grn_output_envelope_close(ctx, |
| 456 | &foot, |
| 457 | ctx->rc, |
| 458 | input_path, |
| 459 | number_of_lines); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | ensure_write(ctx, stream, GRN_TEXT_VALUE(&head), GRN_TEXT_LEN(&head)); |
| 464 | ensure_write(ctx, stream, GRN_TEXT_VALUE(&body), GRN_TEXT_LEN(&body)); |
| 465 | ensure_write(ctx, stream, GRN_TEXT_VALUE(&foot), GRN_TEXT_LEN(&foot)); |
| 466 | size_t content_size = |
| 467 | GRN_TEXT_LEN(&head) + GRN_TEXT_LEN(&body) + GRN_TEXT_LEN(&foot); |
| 468 | if (is_last_message && content_size > 0) { |
| 469 | ensure_write(ctx, stream, "\n", 1); |
| 470 | content_size++; |
| 471 | } |
| 472 | fflush(stream); |
| 473 | if (content_size > 0) { |
| 474 | GRN_QUERY_LOG(ctx, |
| 475 | GRN_QUERY_LOG_SIZE, |
| 476 | ":", |
| 477 | "send(%" GRN_FMT_SIZE ")", |
| 478 | content_size); |
| 479 | } |
| 480 | GRN_OBJ_FIN(ctx, &head); |
| 481 | GRN_OBJ_FIN(ctx, &foot); |
| 482 | |
| 483 | if (is_last_message) { |
| 484 | command = GRN_CTX_USER_DATA(ctx)->ptr; |
| 485 | GRN_BULK_REWIND(command); |
| 486 | |
| 487 | output_context->is_outputting = false; |
| 488 | } |
| 489 | |
| 490 | GRN_OBJ_FIN(ctx, &body); |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | s_output(grn_ctx *ctx, int flags, void *arg) |
| 495 | { |
| 496 | standalone_output_context *output_context = arg; |
| 497 | |
| 498 | switch (grn_ctx_get_output_type(ctx)) { |
| 499 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 500 | case GRN_CONTENT_NONE: |
| 501 | s_output_raw(ctx, flags, output_context); |
| 502 | break; |
| 503 | default: |
| 504 | s_output_typed(ctx, flags, output_context); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | create_pid_file(void) |
| 511 | { |
| 512 | FILE *pid_file = NULL; |
| 513 | |
| 514 | if (!pid_file_path) { |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | pid_file = fopen(pid_file_path, "w"); |
| 519 | if (!pid_file) { |
| 520 | fprintf(stderr, |
| 521 | "Failed to open PID file: <%s>: <%s>\n", |
| 522 | pid_file_path, |
| 523 | grn_strerror(errno)); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | { |
| 528 | #ifdef WIN32 |
| 529 | DWORD pid; |
| 530 | pid = GetCurrentProcessId(); |
| 531 | fprintf(pid_file, "%" GRN_FMT_DWORD "\n", pid); |
| 532 | #else /* WIN32 */ |
| 533 | pid_t pid; |
| 534 | pid = grn_getpid(); |
| 535 | fprintf(pid_file, "%d\n", pid); |
| 536 | #endif /* WIN32 */ |
| 537 | } |
| 538 | fclose(pid_file); |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | clean_pid_file(void) |
| 543 | { |
| 544 | if (pid_file_path) { |
| 545 | grn_unlink(pid_file_path); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | static bool |
| 550 | do_alone_allow_db_reopen(grn_ctx *ctx, |
| 551 | grn_obj *text, |
| 552 | grn_obj **db, |
| 553 | const char *db_path) |
| 554 | { |
| 555 | bool is_close = false; |
| 556 | bool is_reopen = false; |
| 557 | if (GRN_TEXT_EQUAL_CSTRING(text, "_database_close")) { |
| 558 | is_close = true; |
| 559 | } else if (GRN_TEXT_EQUAL_CSTRING(text, "_database_reopen")) { |
| 560 | is_reopen = true; |
| 561 | } else { |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | GRN_QUERY_LOG(ctx, |
| 566 | GRN_QUERY_LOG_COMMAND, |
| 567 | ">", |
| 568 | "%.*s", |
| 569 | (int)GRN_TEXT_LEN(text), |
| 570 | GRN_TEXT_VALUE(text)); |
| 571 | if (is_close) { |
| 572 | grn_obj_close(ctx, *db); |
| 573 | *db = NULL; |
| 574 | } else if (is_reopen) { |
| 575 | if (*db) { |
| 576 | grn_obj_close(ctx, *db); |
| 577 | } |
| 578 | if (db_path) { |
| 579 | *db = grn_db_open(ctx, db_path); |
| 580 | } else { |
| 581 | *db = grn_db_create(ctx, NULL, NULL); |
| 582 | } |
| 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | static int |
| 588 | do_alone(int argc, char **argv) |
| 589 | { |
| 590 | int exit_code = EXIT_FAILURE; |
| 591 | char *path = NULL; |
| 592 | grn_obj *db; |
| 593 | grn_ctx ctx_, *ctx = &ctx_; |
| 594 | create_pid_file(); |
| 595 | grn_ctx_init(ctx, 0); |
| 596 | grn_ctx_set_wal_role(ctx, wal_role); |
| 597 | if (argc > 0 && argv) { |
| 598 | path = *argv++; |
| 599 | argc--; |
| 600 | } |
| 601 | db = |
| 602 | (newdb || !path) ? grn_db_create(ctx, path, NULL) : grn_db_open(ctx, path); |
| 603 | if (db) { |
| 604 | const char *allow_db_reopen_env = getenv("GROONGA_ALLOW_DATABASE_REOPEN"); |
| 605 | bool allow_db_reopen = |
| 606 | (allow_db_reopen_env && (strcmp(allow_db_reopen_env, "yes") == 0)); |
| 607 | grn_obj command; |
| 608 | GRN_TEXT_INIT(&command, 0); |
| 609 | GRN_CTX_USER_DATA(ctx)->ptr = &command; |
| 610 | standalone_output_context output_context; |
| 611 | output_context.output = output; |
| 612 | output_context.is_outputting = false; |
| 613 | grn_ctx_recv_handler_set(ctx, s_output, &output_context); |
| 614 | grn_obj *load_command = grn_ctx_get(ctx, "load", -1); |
| 615 | if (!argc) { |
| 616 | grn_obj text; |
| 617 | GRN_TEXT_INIT(&text, 0); |
| 618 | while (prompt(ctx, &text) == GRN_SUCCESS) { |
| 619 | const bool is_loading = |
| 620 | (ctx->impl->command.keep.command == load_command); |
| 621 | if (!is_loading) { |
| 622 | GRN_TEXT_PUT(ctx, |
| 623 | &command, |
| 624 | GRN_TEXT_VALUE(&text), |
| 625 | GRN_TEXT_LEN(&text)); |
| 626 | if (allow_db_reopen) { |
| 627 | bool processed = do_alone_allow_db_reopen(ctx, &text, &db, path); |
| 628 | if (processed) { |
| 629 | ctx->impl->curr_expr = NULL; |
| 630 | grn_ctx_set_output_type(ctx, GRN_CONTENT_JSON); |
| 631 | grn_ctx_output_bool(ctx, ctx->rc == GRN_SUCCESS); |
| 632 | grn_ctx_output_flush(ctx, GRN_CTX_TAIL); |
| 633 | GRN_QUERY_LOG(ctx, |
| 634 | GRN_QUERY_LOG_RESULT_CODE, |
| 635 | "<", |
| 636 | "rc=%d", |
| 637 | ctx->rc); |
| 638 | if (ctx->rc == GRN_SUCCESS) { |
| 639 | continue; |
| 640 | } else { |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | grn_ctx_send(ctx, GRN_TEXT_VALUE(&text), GRN_TEXT_LEN(&text), 0); |
| 647 | if (ctx->stat == GRN_CTX_QUIT) { |
| 648 | break; |
| 649 | } |
| 650 | if (!is_loading) { |
| 651 | ERRCLR(ctx); |
| 652 | if (ctx->rc == GRN_CANCEL) { |
| 653 | ctx->rc = GRN_SUCCESS; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | exit_code = grn_rc_to_exit_code(ctx->rc); |
| 658 | grn_obj_unlink(ctx, &text); |
| 659 | } else { |
| 660 | grn_rc rc; |
| 661 | rc = grn_ctx_sendv(ctx, argc, argv, 0); |
| 662 | exit_code = grn_rc_to_exit_code(rc); |
| 663 | } |
| 664 | GRN_OBJ_FIN(ctx, &command); |
| 665 | grn_obj_close(ctx, db); |
| 666 | } else { |
| 667 | fprintf(stderr, "db open failed (%s): %s\n", path, ctx->errbuf); |
| 668 | } |
| 669 | grn_ctx_fin(ctx); |
| 670 | clean_pid_file(); |
| 671 | return exit_code; |
| 672 | } |
| 673 | |
| 674 | static int |
| 675 | c_output(grn_ctx *ctx) |
| 676 | { |
| 677 | int flags; |
| 678 | char *str; |
| 679 | unsigned int str_len; |
| 680 | do { |
| 681 | grn_ctx_recv(ctx, &str, &str_len, &flags); |
| 682 | /* |
| 683 | if (ctx->rc) { |
| 684 | fprintf(stderr, "grn_ctx_recv failed\n"); |
| 685 | return -1; |
| 686 | } |
| 687 | */ |
| 688 | if (str_len || ctx->rc) { |
| 689 | grn_obj head, body, foot; |
| 690 | GRN_TEXT_INIT(&head, 0); |
| 691 | GRN_TEXT_INIT(&body, GRN_OBJ_DO_SHALLOW_COPY); |
| 692 | GRN_TEXT_INIT(&foot, 0); |
| 693 | if (ctx->rc == GRN_SUCCESS) { |
| 694 | GRN_TEXT_SET(ctx, &body, str, str_len); |
| 695 | } else { |
| 696 | ERR(ctx->rc, "%.*s", str_len, str); |
| 697 | } |
| 698 | output_envelope(ctx, ctx->rc, &head, &body, &foot); |
| 699 | fwrite(GRN_TEXT_VALUE(&head), 1, GRN_TEXT_LEN(&head), output); |
| 700 | fwrite(GRN_TEXT_VALUE(&body), 1, GRN_TEXT_LEN(&body), output); |
| 701 | fwrite(GRN_TEXT_VALUE(&foot), 1, GRN_TEXT_LEN(&foot), output); |
| 702 | fputc('\n', output); |
| 703 | fflush(output); |
| 704 | GRN_OBJ_FIN(ctx, &head); |
| 705 | GRN_OBJ_FIN(ctx, &body); |
| 706 | GRN_OBJ_FIN(ctx, &foot); |
| 707 | } |
| 708 | } while ((flags & GRN_CTX_MORE)); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int |
| 713 | g_client(int argc, char **argv) |
| 714 | { |
| 715 | int exit_code = EXIT_FAILURE; |
| 716 | grn_ctx ctx_, *ctx = &ctx_; |
| 717 | const char *hostname = DEFAULT_DEST; |
| 718 | if (argc > 0 && argv) { |
| 719 | hostname = *argv++; |
| 720 | argc--; |
| 721 | } |
| 722 | grn_ctx_init(ctx, 0); |
| 723 | if (!grn_ctx_connect(ctx, hostname, port, 0)) { |
| 724 | if (!argc) { |
| 725 | grn_obj text; |
| 726 | GRN_TEXT_INIT(&text, 0); |
| 727 | while (prompt(ctx, &text) == GRN_SUCCESS) { |
| 728 | grn_ctx_send(ctx, GRN_TEXT_VALUE(&text), GRN_TEXT_LEN(&text), 0); |
| 729 | exit_code = grn_rc_to_exit_code(ctx->rc); |
| 730 | if (ctx->rc != GRN_SUCCESS) { |
| 731 | break; |
| 732 | } |
| 733 | if (c_output(ctx)) { |
| 734 | goto exit; |
| 735 | } |
| 736 | if (ctx->stat == GRN_CTX_QUIT) { |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | grn_obj_unlink(ctx, &text); |
| 741 | } else { |
| 742 | grn_rc rc; |
| 743 | rc = grn_ctx_sendv(ctx, argc, argv, 0); |
| 744 | exit_code = grn_rc_to_exit_code(rc); |
| 745 | if (c_output(ctx)) { |
| 746 | goto exit; |
| 747 | } |
| 748 | } |
| 749 | } else { |
| 750 | fprintf(stderr, "grn_ctx_connect failed (%s:%d)\n", hostname, port); |
| 751 | } |
| 752 | exit: |
| 753 | grn_ctx_fin(ctx); |
| 754 | return exit_code; |
| 755 | } |
| 756 | |
| 757 | /* server */ |
| 758 | |
| 759 | typedef void (*grn_edge_dispatcher_func)(grn_ctx *ctx, grn_edge *edge); |
| 760 | typedef void (*grn_handler_func)(grn_ctx *ctx, grn_obj *msg); |
| 761 | |
| 762 | static grn_com_queue ctx_new; |
| 763 | static grn_com_queue ctx_old; |
| 764 | static grn_critical_section q_critical_section; |
| 765 | static grn_cond q_cond; |
| 766 | static uint32_t n_running_threads = 0; |
| 767 | static uint32_t n_floating_threads = 0; |
| 768 | static uint32_t max_n_floating_threads; |
| 769 | |
| 770 | static uint32_t |
| 771 | groonga_get_thread_limit(void *data) |
| 772 | { |
| 773 | return max_n_floating_threads; |
| 774 | } |
| 775 | |
| 776 | static void |
| 777 | groonga_set_thread_limit_with_ctx(grn_ctx *ctx, uint32_t new_limit, void *data) |
| 778 | { |
| 779 | uint32_t i; |
| 780 | uint32_t current_n_floating_threads; |
| 781 | uint32_t current_max_n_floating_threads; |
| 782 | static uint32_t n_changing_threads = 0; |
| 783 | uint32_t prev_n_changing_threads; |
| 784 | |
| 785 | GRN_ATOMIC_ADD_EX(&n_changing_threads, 1, prev_n_changing_threads); |
| 786 | |
| 787 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 788 | current_n_floating_threads = n_floating_threads; |
| 789 | current_max_n_floating_threads = max_n_floating_threads; |
| 790 | max_n_floating_threads = new_limit; |
| 791 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 792 | |
| 793 | if (prev_n_changing_threads > 0) { |
| 794 | GRN_ATOMIC_ADD_EX(&n_changing_threads, -1, prev_n_changing_threads); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | if (current_n_floating_threads > new_limit) { |
| 799 | for (i = 0; i < current_n_floating_threads; i++) { |
| 800 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 801 | COND_SIGNAL(q_cond); |
| 802 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | while (true) { |
| 807 | bool is_reduced; |
| 808 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 809 | is_reduced = (n_running_threads <= max_n_floating_threads); |
| 810 | if (!is_reduced && n_floating_threads > 0) { |
| 811 | COND_SIGNAL(q_cond); |
| 812 | } |
| 813 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 814 | if (is_reduced) { |
| 815 | break; |
| 816 | } |
| 817 | if (ctx && ctx->rc == GRN_CANCEL) { |
| 818 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 819 | max_n_floating_threads = current_max_n_floating_threads; |
| 820 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 821 | break; |
| 822 | } |
| 823 | grn_nanosleep(1000000); |
| 824 | } |
| 825 | |
| 826 | GRN_ATOMIC_ADD_EX(&n_changing_threads, -1, prev_n_changing_threads); |
| 827 | } |
| 828 | |
| 829 | static void |
| 830 | groonga_set_thread_limit(uint32_t new_limit, void *data) |
| 831 | { |
| 832 | groonga_set_thread_limit_with_ctx(NULL, new_limit, data); |
| 833 | } |
| 834 | |
| 835 | typedef struct { |
| 836 | grn_mutex mutex; |
| 837 | grn_ctx ctx; |
| 838 | grn_pat *entries; |
| 839 | uint64_t earliest_unix_time_msec; |
| 840 | } request_timer_data; |
| 841 | static request_timer_data the_request_timer_data; |
| 842 | |
| 843 | static void * |
| 844 | request_timer_register(const char *request_id, |
| 845 | unsigned int request_id_size, |
| 846 | double timeout, |
| 847 | void *user_data) |
| 848 | { |
| 849 | request_timer_data *data = user_data; |
| 850 | grn_id id = GRN_ID_NIL; |
| 851 | |
| 852 | if (MUTEX_LOCK_CHECK(data->mutex)) { |
| 853 | grn_ctx *ctx = &(data->ctx); |
| 854 | bool is_first_timer; |
| 855 | grn_timeval tv; |
| 856 | uint64_t timeout_unix_time_msec; |
| 857 | void *value; |
| 858 | |
| 859 | is_first_timer = (grn_pat_size(ctx, data->entries) == 0); |
| 860 | grn_timeval_now(ctx, &tv); |
| 861 | timeout_unix_time_msec = GRN_TIMEVAL_TO_MSEC(&tv) + (timeout * 1000); |
| 862 | while (true) { |
| 863 | int added; |
| 864 | id = grn_pat_add(ctx, |
| 865 | data->entries, |
| 866 | &timeout_unix_time_msec, |
| 867 | sizeof(uint64_t), |
| 868 | &value, |
| 869 | &added); |
| 870 | if (added != 0) { |
| 871 | break; |
| 872 | } |
| 873 | timeout_unix_time_msec++; |
| 874 | } |
| 875 | grn_memcpy(value, &request_id_size, sizeof(unsigned int)); |
| 876 | grn_memcpy(((uint8_t *)value) + sizeof(unsigned int), |
| 877 | request_id, |
| 878 | request_id_size); |
| 879 | if (data->earliest_unix_time_msec == 0 || |
| 880 | data->earliest_unix_time_msec > timeout_unix_time_msec) { |
| 881 | data->earliest_unix_time_msec = timeout_unix_time_msec; |
| 882 | } |
| 883 | if (is_first_timer) { |
| 884 | break_accept_event_loop(ctx); |
| 885 | } |
| 886 | MUTEX_UNLOCK(data->mutex); |
| 887 | } |
| 888 | |
| 889 | return (void *)(uint64_t)id; |
| 890 | } |
| 891 | |
| 892 | static void |
| 893 | request_timer_unregister(void *timer_id, void *user_data) |
| 894 | { |
| 895 | request_timer_data *data = user_data; |
| 896 | grn_id id = (grn_id)(uint64_t)timer_id; |
| 897 | |
| 898 | if (MUTEX_LOCK_CHECK(data->mutex)) { |
| 899 | grn_ctx *ctx = &(data->ctx); |
| 900 | uint64_t timeout_unix_time_msec; |
| 901 | int key_size; |
| 902 | |
| 903 | key_size = grn_pat_get_key(ctx, |
| 904 | data->entries, |
| 905 | id, |
| 906 | &timeout_unix_time_msec, |
| 907 | sizeof(uint64_t)); |
| 908 | if (key_size > 0) { |
| 909 | grn_pat_delete_by_id(ctx, data->entries, id, NULL); |
| 910 | if (data->earliest_unix_time_msec >= timeout_unix_time_msec) { |
| 911 | data->earliest_unix_time_msec = 0; |
| 912 | } |
| 913 | } |
| 914 | MUTEX_UNLOCK(data->mutex); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | request_timer_fin(void *user_data) |
| 920 | { |
| 921 | request_timer_data *data = user_data; |
| 922 | |
| 923 | { |
| 924 | grn_ctx *ctx = &(data->ctx); |
| 925 | grn_pat_close(ctx, data->entries); |
| 926 | grn_ctx_fin(ctx); |
| 927 | MUTEX_FIN(data->mutex); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | static void |
| 932 | request_timer_init(void) |
| 933 | { |
| 934 | static grn_request_timer timer; |
| 935 | request_timer_data *data = &the_request_timer_data; |
| 936 | grn_ctx *ctx; |
| 937 | |
| 938 | MUTEX_INIT(data->mutex); |
| 939 | ctx = &(data->ctx); |
| 940 | grn_ctx_init(ctx, 0); |
| 941 | data->entries = grn_pat_create(ctx, |
| 942 | NULL, |
| 943 | sizeof(uint64_t), |
| 944 | GRN_TABLE_MAX_KEY_SIZE, |
| 945 | GRN_OBJ_KEY_UINT); |
| 946 | data->earliest_unix_time_msec = 0; |
| 947 | |
| 948 | timer.user_data = data; |
| 949 | timer.register_func = request_timer_register; |
| 950 | timer.unregister_func = request_timer_unregister; |
| 951 | timer.fin_func = request_timer_fin; |
| 952 | |
| 953 | grn_request_timer_set(&timer); |
| 954 | } |
| 955 | |
| 956 | static bool |
| 957 | request_timer_ensure_earliest_unix_time_msec(void) |
| 958 | { |
| 959 | request_timer_data *data = &the_request_timer_data; |
| 960 | grn_ctx *ctx; |
| 961 | grn_pat_cursor *cursor; |
| 962 | |
| 963 | if (data->earliest_unix_time_msec > 0) { |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | ctx = &(data->ctx); |
| 968 | cursor = grn_pat_cursor_open(ctx, |
| 969 | data->entries, |
| 970 | NULL, |
| 971 | 0, |
| 972 | NULL, |
| 973 | 0, |
| 974 | 0, |
| 975 | 1, |
| 976 | GRN_CURSOR_ASCENDING); |
| 977 | if (!cursor) { |
| 978 | return false; |
| 979 | } |
| 980 | while (grn_pat_cursor_next(ctx, cursor) != GRN_ID_NIL) { |
| 981 | void *key; |
| 982 | uint64_t timeout_unix_time_msec; |
| 983 | |
| 984 | grn_pat_cursor_get_key(ctx, cursor, &key); |
| 985 | timeout_unix_time_msec = *(uint64_t *)key; |
| 986 | data->earliest_unix_time_msec = timeout_unix_time_msec; |
| 987 | break; |
| 988 | } |
| 989 | grn_pat_cursor_close(ctx, cursor); |
| 990 | |
| 991 | return data->earliest_unix_time_msec > 0; |
| 992 | } |
| 993 | |
| 994 | static int |
| 995 | request_timer_get_poll_timeout(void) |
| 996 | { |
| 997 | request_timer_data *data = &the_request_timer_data; |
| 998 | int timeout = 1000; |
| 999 | grn_ctx *ctx; |
| 1000 | grn_timeval tv; |
| 1001 | |
| 1002 | if (!MUTEX_LOCK_CHECK(data->mutex)) { |
| 1003 | return timeout; |
| 1004 | } |
| 1005 | |
| 1006 | ctx = &(data->ctx); |
| 1007 | if (grn_pat_size(ctx, data->entries) == 0) { |
| 1008 | goto exit; |
| 1009 | } |
| 1010 | |
| 1011 | if (!request_timer_ensure_earliest_unix_time_msec()) { |
| 1012 | goto exit; |
| 1013 | } |
| 1014 | |
| 1015 | grn_timeval_now(ctx, &tv); |
| 1016 | timeout = data->earliest_unix_time_msec - GRN_TIMEVAL_TO_MSEC(&tv); |
| 1017 | if (timeout < 0) { |
| 1018 | timeout = 0; |
| 1019 | } else if (timeout > 1000) { |
| 1020 | timeout = 1000; |
| 1021 | } |
| 1022 | |
| 1023 | exit: |
| 1024 | MUTEX_UNLOCK(data->mutex); |
| 1025 | |
| 1026 | return timeout; |
| 1027 | } |
| 1028 | |
| 1029 | static void |
| 1030 | request_timer_process_timeout(void) |
| 1031 | { |
| 1032 | request_timer_data *data = &the_request_timer_data; |
| 1033 | grn_ctx *ctx; |
| 1034 | grn_timeval tv; |
| 1035 | uint64_t max; |
| 1036 | grn_pat_cursor *cursor; |
| 1037 | |
| 1038 | ctx = &(data->ctx); |
| 1039 | if (grn_pat_size(ctx, data->entries) == 0) { |
| 1040 | return; |
| 1041 | } |
| 1042 | |
| 1043 | grn_timeval_now(ctx, &tv); |
| 1044 | max = GRN_TIMEVAL_TO_MSEC(&tv); |
| 1045 | cursor = grn_pat_cursor_open(ctx, |
| 1046 | data->entries, |
| 1047 | NULL, |
| 1048 | 0, |
| 1049 | &max, |
| 1050 | sizeof(uint64_t), |
| 1051 | 0, |
| 1052 | -1, |
| 1053 | GRN_CURSOR_ASCENDING); |
| 1054 | if (!cursor) { |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | grn_id id; |
| 1059 | while ((id = grn_pat_cursor_next(ctx, cursor)) != GRN_ID_NIL) { |
| 1060 | void *value; |
| 1061 | const char *request_id; |
| 1062 | unsigned int request_id_size; |
| 1063 | |
| 1064 | grn_pat_cursor_get_value(ctx, cursor, &value); |
| 1065 | request_id_size = *((unsigned int *)value); |
| 1066 | request_id = (const char *)(((uint8_t *)value) + sizeof(unsigned int)); |
| 1067 | grn_request_canceler_cancel(request_id, request_id_size); |
| 1068 | } |
| 1069 | grn_pat_cursor_close(ctx, cursor); |
| 1070 | } |
| 1071 | |
| 1072 | static void |
| 1073 | reset_ready_notify_pipe(void) |
| 1074 | { |
| 1075 | ready_notify_pipe[PIPE_READ] = 0; |
| 1076 | ready_notify_pipe[PIPE_WRITE] = 0; |
| 1077 | } |
| 1078 | |
| 1079 | static void |
| 1080 | close_ready_notify_pipe(void) |
| 1081 | { |
| 1082 | if (ready_notify_pipe[PIPE_READ] > 0) { |
| 1083 | close(ready_notify_pipe[PIPE_READ]); |
| 1084 | } |
| 1085 | if (ready_notify_pipe[PIPE_WRITE] > 0) { |
| 1086 | close(ready_notify_pipe[PIPE_WRITE]); |
| 1087 | } |
| 1088 | reset_ready_notify_pipe(); |
| 1089 | } |
| 1090 | |
| 1091 | /* FIXME: callers ignore the return value of send_ready_notify. */ |
| 1092 | static bool |
| 1093 | send_ready_notify(void) |
| 1094 | { |
| 1095 | if (ready_notify_pipe[PIPE_WRITE] > 0) { |
| 1096 | const char *ready_notify_message = "ready"; |
| 1097 | const size_t ready_notify_message_len = strlen(ready_notify_message); |
| 1098 | size_t n = 0; |
| 1099 | do { |
| 1100 | ssize_t m = write(ready_notify_pipe[PIPE_WRITE], |
| 1101 | ready_notify_message + n, |
| 1102 | ready_notify_message_len - n); |
| 1103 | if (m == -1) { |
| 1104 | close_ready_notify_pipe(); |
| 1105 | return false; |
| 1106 | } |
| 1107 | n += m; |
| 1108 | } while (n < ready_notify_message_len); |
| 1109 | } |
| 1110 | close_ready_notify_pipe(); |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
| 1114 | static int |
| 1115 | daemonize(void) |
| 1116 | { |
| 1117 | int exit_code = EXIT_SUCCESS; |
| 1118 | #ifndef WIN32 |
| 1119 | |
| 1120 | if (pipe(ready_notify_pipe) == -1) { |
| 1121 | reset_ready_notify_pipe(); |
| 1122 | } |
| 1123 | |
| 1124 | switch (fork()) { |
| 1125 | case 0: |
| 1126 | break; |
| 1127 | case -1: |
| 1128 | perror("fork"); |
| 1129 | return EXIT_FAILURE; |
| 1130 | default: |
| 1131 | wait(NULL); |
| 1132 | if (ready_notify_pipe[PIPE_READ] > 0) { |
| 1133 | int max_fd; |
| 1134 | fd_set read_fds; |
| 1135 | FD_ZERO(&read_fds); |
| 1136 | FD_SET(ready_notify_pipe[PIPE_READ], &read_fds); |
| 1137 | max_fd = ready_notify_pipe[PIPE_READ] + 1; |
| 1138 | select(max_fd, &read_fds, NULL, NULL, NULL); |
| 1139 | } |
| 1140 | close_ready_notify_pipe(); |
| 1141 | _exit(EXIT_SUCCESS); |
| 1142 | } |
| 1143 | switch (fork()) { |
| 1144 | case 0: |
| 1145 | if (pid_file_path) { |
| 1146 | create_pid_file(); |
| 1147 | } else { |
| 1148 | pid_t pid; |
| 1149 | pid = grn_getpid(); |
| 1150 | fprintf(stderr, "%d\n", pid); |
| 1151 | } |
| 1152 | break; |
| 1153 | case -1: |
| 1154 | perror("fork"); |
| 1155 | return EXIT_FAILURE; |
| 1156 | default: |
| 1157 | close_ready_notify_pipe(); |
| 1158 | _exit(EXIT_SUCCESS); |
| 1159 | } |
| 1160 | { |
| 1161 | int null_fd; |
| 1162 | grn_open(null_fd, "/dev/null", O_RDWR); |
| 1163 | if (null_fd != -1) { |
| 1164 | dup2(null_fd, STDIN_FILENO); |
| 1165 | dup2(null_fd, STDOUT_FILENO); |
| 1166 | dup2(null_fd, STDERR_FILENO); |
| 1167 | if (null_fd > STDERR_FILENO) { |
| 1168 | grn_close(null_fd); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | #endif /* WIN32 */ |
| 1173 | return exit_code; |
| 1174 | } |
| 1175 | |
| 1176 | static void |
| 1177 | run_server_loop(grn_ctx *ctx, grn_com_event *ev) |
| 1178 | { |
| 1179 | request_timer_init(); |
| 1180 | running_event_loop = true; |
| 1181 | while (!grn_com_event_poll(ctx, ev, request_timer_get_poll_timeout()) && |
| 1182 | grn_gctx.stat != GRN_CTX_QUIT) { |
| 1183 | #ifdef ENABLE_LOG_REOPEN_BY_SIGNAL |
| 1184 | reopen_log_by_signal(ctx); |
| 1185 | #endif |
| 1186 | grn_edge *edge; |
| 1187 | while ((edge = (grn_edge *)grn_com_queue_deque(ctx, &ctx_old))) { |
| 1188 | grn_obj *msg; |
| 1189 | while ((msg = (grn_obj *)grn_com_queue_deque(ctx, &edge->send_old))) { |
| 1190 | grn_msg_close(&edge->ctx, msg); |
| 1191 | } |
| 1192 | while ((msg = (grn_obj *)grn_com_queue_deque(ctx, &edge->recv_new))) { |
| 1193 | grn_msg_close(ctx, msg); |
| 1194 | } |
| 1195 | grn_ctx_fin(&edge->ctx); |
| 1196 | if (edge->com->has_sid && edge->com->opaque == edge) { |
| 1197 | grn_com_close(ctx, edge->com); |
| 1198 | } |
| 1199 | grn_edges_delete(ctx, edge); |
| 1200 | } |
| 1201 | request_timer_process_timeout(); |
| 1202 | /* todo : log stat */ |
| 1203 | } |
| 1204 | running_event_loop = false; |
| 1205 | for (;;) { |
| 1206 | uint32_t i; |
| 1207 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 1208 | for (i = 0; i < n_floating_threads; i++) { |
| 1209 | COND_SIGNAL(q_cond); |
| 1210 | } |
| 1211 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 1212 | if (n_running_threads == 0) { |
| 1213 | break; |
| 1214 | } |
| 1215 | grn_nanosleep(1000000); |
| 1216 | } |
| 1217 | { |
| 1218 | grn_edge *edge; |
| 1219 | GRN_HASH_EACH(ctx, grn_edges, id, NULL, NULL, &edge, { |
| 1220 | grn_obj *obj; |
| 1221 | while ((obj = (grn_obj *)grn_com_queue_deque(ctx, &edge->send_old))) { |
| 1222 | grn_msg_close(&edge->ctx, obj); |
| 1223 | } |
| 1224 | while ((obj = (grn_obj *)grn_com_queue_deque(ctx, &edge->recv_new))) { |
| 1225 | grn_msg_close(ctx, obj); |
| 1226 | } |
| 1227 | grn_ctx_fin(&edge->ctx); |
| 1228 | if (edge->com->has_sid) { |
| 1229 | grn_com_close(ctx, edge->com); |
| 1230 | } |
| 1231 | grn_edges_delete(ctx, edge); |
| 1232 | }); |
| 1233 | } |
| 1234 | { |
| 1235 | grn_com *com; |
| 1236 | GRN_HASH_EACH(ctx, ev->hash, id, NULL, NULL, &com, { |
| 1237 | grn_com_close(ctx, com); |
| 1238 | }); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | static int |
| 1243 | run_server(grn_ctx *ctx, |
| 1244 | grn_obj *db, |
| 1245 | grn_com_event *ev, |
| 1246 | grn_edge_dispatcher_func dispatcher, |
| 1247 | grn_handler_func handler) |
| 1248 | { |
| 1249 | int exit_code = EXIT_SUCCESS; |
| 1250 | struct hostent *he; |
| 1251 | if (!(he = gethostbyname(hostname))) { |
| 1252 | send_ready_notify(); |
| 1253 | SOERR("gethostbyname"); |
| 1254 | } else { |
| 1255 | ev->opaque = db; |
| 1256 | grn_edges_init(ctx, dispatcher); |
| 1257 | grn_job_queue_current_set(ctx, &ctx_new); |
| 1258 | if (!grn_com_sopen(ctx, ev, bind_address, port, handler, he)) { |
| 1259 | send_ready_notify(); |
| 1260 | run_server_loop(ctx, ev); |
| 1261 | exit_code = EXIT_SUCCESS; |
| 1262 | } else { |
| 1263 | send_ready_notify(); |
| 1264 | fprintf(stderr, |
| 1265 | "grn_com_sopen failed (%s:%d): %s\n", |
| 1266 | bind_address, |
| 1267 | port, |
| 1268 | ctx->errbuf); |
| 1269 | } |
| 1270 | grn_job_queue_current_set(ctx, NULL); |
| 1271 | grn_edges_fin(ctx); |
| 1272 | } |
| 1273 | return exit_code; |
| 1274 | } |
| 1275 | |
| 1276 | static bool |
| 1277 | memcached_init(grn_ctx *ctx); |
| 1278 | |
| 1279 | static int |
| 1280 | start_service(grn_ctx *ctx, |
| 1281 | const char *db_path, |
| 1282 | grn_edge_dispatcher_func dispatcher, |
| 1283 | grn_handler_func handler) |
| 1284 | { |
| 1285 | int exit_code = EXIT_SUCCESS; |
| 1286 | grn_com_event ev; |
| 1287 | |
| 1288 | if (is_daemon_mode) { |
| 1289 | exit_code = daemonize(); |
| 1290 | if (exit_code != EXIT_SUCCESS) { |
| 1291 | return exit_code; |
| 1292 | } |
| 1293 | } else { |
| 1294 | create_pid_file(); |
| 1295 | } |
| 1296 | |
| 1297 | if (!grn_com_event_init(ctx, &ev, MAX_CON, sizeof(grn_com))) { |
| 1298 | grn_obj *db; |
| 1299 | db = (newdb || !db_path) ? grn_db_create(ctx, db_path, NULL) |
| 1300 | : grn_db_open(ctx, db_path); |
| 1301 | if (db) { |
| 1302 | if (is_memcached_mode) { |
| 1303 | if (!memcached_init(ctx)) { |
| 1304 | fprintf(stderr, |
| 1305 | "failed to initialize memcached mode: %s\n", |
| 1306 | ctx->errbuf); |
| 1307 | exit_code = EXIT_FAILURE; |
| 1308 | send_ready_notify(); |
| 1309 | } |
| 1310 | } |
| 1311 | if (exit_code == EXIT_SUCCESS) { |
| 1312 | grn_com_event_set_listen_backlog(ctx, &ev, listen_backlog); |
| 1313 | exit_code = run_server(ctx, db, &ev, dispatcher, handler); |
| 1314 | } |
| 1315 | grn_obj_close(ctx, db); |
| 1316 | } else { |
| 1317 | fprintf(stderr, "db open failed (%s): %s\n", db_path, ctx->errbuf); |
| 1318 | exit_code = EXIT_FAILURE; |
| 1319 | send_ready_notify(); |
| 1320 | } |
| 1321 | grn_com_event_fin(ctx, &ev); |
| 1322 | } else { |
| 1323 | fprintf(stderr, "grn_com_event_init failed\n"); |
| 1324 | exit_code = EXIT_FAILURE; |
| 1325 | send_ready_notify(); |
| 1326 | } |
| 1327 | |
| 1328 | clean_pid_file(); |
| 1329 | |
| 1330 | return exit_code; |
| 1331 | } |
| 1332 | |
| 1333 | typedef struct { |
| 1334 | grn_msg *msg; |
| 1335 | bool in_body; |
| 1336 | bool is_chunked; |
| 1337 | } ht_context; |
| 1338 | |
| 1339 | static void |
| 1340 | h_output_set_header(grn_ctx *ctx, |
| 1341 | grn_obj *header, |
| 1342 | grn_rc rc, |
| 1343 | int64_t content_length, |
| 1344 | grn_obj *foot) |
| 1345 | { |
| 1346 | switch (rc) { |
| 1347 | case GRN_SUCCESS: |
| 1348 | GRN_TEXT_SETS(ctx, header, "HTTP/1.1 200 OK\r\n"); |
| 1349 | break; |
| 1350 | case GRN_INVALID_ARGUMENT: |
| 1351 | case GRN_FUNCTION_NOT_IMPLEMENTED: |
| 1352 | case GRN_SYNTAX_ERROR: |
| 1353 | GRN_TEXT_SETS(ctx, header, "HTTP/1.1 400 Bad Request\r\n"); |
| 1354 | break; |
| 1355 | case GRN_NO_SUCH_FILE_OR_DIRECTORY: |
| 1356 | GRN_TEXT_SETS(ctx, header, "HTTP/1.1 404 Not Found\r\n"); |
| 1357 | break; |
| 1358 | case GRN_CANCEL: |
| 1359 | GRN_TEXT_SETS(ctx, header, "HTTP/1.1 408 Request Timeout\r\n"); |
| 1360 | break; |
| 1361 | default: |
| 1362 | GRN_TEXT_SETS(ctx, header, "HTTP/1.1 500 Internal Server Error\r\n"); |
| 1363 | break; |
| 1364 | } |
| 1365 | GRN_TEXT_PUT(ctx, |
| 1366 | header, |
| 1367 | GRN_TEXT_VALUE(&http_response_server_line), |
| 1368 | GRN_TEXT_LEN(&http_response_server_line)); |
| 1369 | GRN_TEXT_PUTS(ctx, header, "Content-Type: "); |
| 1370 | if (grn_ctx_get_output_type(ctx) == GRN_CONTENT_JSON && foot && |
| 1371 | GRN_TEXT_LEN(foot) > 0 && |
| 1372 | GRN_TEXT_VALUE(foot)[GRN_TEXT_LEN(foot) - 1] == ';') { |
| 1373 | GRN_TEXT_PUTS(ctx, header, "application/javascript"); |
| 1374 | } else { |
| 1375 | GRN_TEXT_PUTS(ctx, header, grn_ctx_get_mime_type(ctx)); |
| 1376 | } |
| 1377 | GRN_TEXT_PUTS(ctx, header, "\r\n"); |
| 1378 | GRN_TEXT_PUTS(ctx, header, "Connection: close\r\n"); |
| 1379 | if (content_length >= 0) { |
| 1380 | GRN_TEXT_PUTS(ctx, header, "Content-Length: "); |
| 1381 | grn_text_lltoa(ctx, header, content_length); |
| 1382 | GRN_TEXT_PUTS(ctx, header, "\r\n"); |
| 1383 | } else { |
| 1384 | GRN_TEXT_PUTS(ctx, header, "Transfer-Encoding: chunked\r\n"); |
| 1385 | } |
| 1386 | GRN_TEXT_PUTS(ctx, header, "\r\n"); |
| 1387 | } |
| 1388 | |
| 1389 | static void |
| 1390 | h_output_send(grn_ctx *ctx, |
| 1391 | grn_sock fd, |
| 1392 | grn_obj *header, |
| 1393 | grn_obj *head, |
| 1394 | grn_obj *body, |
| 1395 | grn_obj *foot) |
| 1396 | { |
| 1397 | ssize_t ret; |
| 1398 | ssize_t len = 0; |
| 1399 | #ifdef WIN32 |
| 1400 | int n_buffers = 0; |
| 1401 | WSABUF wsabufs[4]; |
| 1402 | if (header) { |
| 1403 | wsabufs[n_buffers].buf = GRN_TEXT_VALUE(header); |
| 1404 | wsabufs[n_buffers].len = GRN_TEXT_LEN(header); |
| 1405 | len += GRN_TEXT_LEN(header); |
| 1406 | n_buffers++; |
| 1407 | } |
| 1408 | if (head) { |
| 1409 | wsabufs[n_buffers].buf = GRN_TEXT_VALUE(head); |
| 1410 | wsabufs[n_buffers].len = GRN_TEXT_LEN(head); |
| 1411 | len += GRN_TEXT_LEN(head); |
| 1412 | n_buffers++; |
| 1413 | } |
| 1414 | if (body) { |
| 1415 | wsabufs[n_buffers].buf = GRN_TEXT_VALUE(body); |
| 1416 | wsabufs[n_buffers].len = GRN_TEXT_LEN(body); |
| 1417 | len += GRN_TEXT_LEN(body); |
| 1418 | n_buffers++; |
| 1419 | } |
| 1420 | if (foot) { |
| 1421 | wsabufs[n_buffers].buf = GRN_TEXT_VALUE(foot); |
| 1422 | wsabufs[n_buffers].len = GRN_TEXT_LEN(foot); |
| 1423 | len += GRN_TEXT_LEN(foot); |
| 1424 | n_buffers++; |
| 1425 | } |
| 1426 | { |
| 1427 | DWORD sent = 0; |
| 1428 | if (WSASend(fd, wsabufs, n_buffers, &sent, 0, NULL, NULL) == SOCKET_ERROR) { |
| 1429 | SOERR("WSASend"); |
| 1430 | } |
| 1431 | ret = sent; |
| 1432 | } |
| 1433 | #else /* WIN32 */ |
| 1434 | struct iovec msg_iov[4]; |
| 1435 | struct msghdr msg; |
| 1436 | msg.msg_name = NULL; |
| 1437 | msg.msg_namelen = 0; |
| 1438 | msg.msg_iov = msg_iov; |
| 1439 | msg.msg_iovlen = 0; |
| 1440 | msg.msg_control = NULL; |
| 1441 | msg.msg_controllen = 0; |
| 1442 | msg.msg_flags = 0; |
| 1443 | |
| 1444 | if (header) { |
| 1445 | msg_iov[msg.msg_iovlen].iov_base = GRN_TEXT_VALUE(header); |
| 1446 | msg_iov[msg.msg_iovlen].iov_len = GRN_TEXT_LEN(header); |
| 1447 | len += GRN_TEXT_LEN(header); |
| 1448 | msg.msg_iovlen++; |
| 1449 | } |
| 1450 | if (head) { |
| 1451 | msg_iov[msg.msg_iovlen].iov_base = GRN_TEXT_VALUE(head); |
| 1452 | msg_iov[msg.msg_iovlen].iov_len = GRN_TEXT_LEN(head); |
| 1453 | len += GRN_TEXT_LEN(head); |
| 1454 | msg.msg_iovlen++; |
| 1455 | } |
| 1456 | if (body) { |
| 1457 | msg_iov[msg.msg_iovlen].iov_base = GRN_TEXT_VALUE(body); |
| 1458 | msg_iov[msg.msg_iovlen].iov_len = GRN_TEXT_LEN(body); |
| 1459 | len += GRN_TEXT_LEN(body); |
| 1460 | msg.msg_iovlen++; |
| 1461 | } |
| 1462 | if (foot) { |
| 1463 | msg_iov[msg.msg_iovlen].iov_base = GRN_TEXT_VALUE(foot); |
| 1464 | msg_iov[msg.msg_iovlen].iov_len = GRN_TEXT_LEN(foot); |
| 1465 | len += GRN_TEXT_LEN(foot); |
| 1466 | msg.msg_iovlen++; |
| 1467 | } |
| 1468 | if ((ret = sendmsg(fd, &msg, MSG_NOSIGNAL)) == -1) { |
| 1469 | SOERR("sendmsg"); |
| 1470 | } |
| 1471 | #endif /* WIN32 */ |
| 1472 | if (ret != len) { |
| 1473 | GRN_LOG(&grn_gctx, |
| 1474 | GRN_LOG_NOTICE, |
| 1475 | "couldn't send all data (%" GRN_FMT_SSIZE "/%" GRN_FMT_SSIZE ")", |
| 1476 | ret, |
| 1477 | len); |
| 1478 | } |
| 1479 | GRN_QUERY_LOG(ctx, |
| 1480 | GRN_QUERY_LOG_SIZE, |
| 1481 | ":", |
| 1482 | "send(%" GRN_FMT_SSIZE "): %" GRN_FMT_SSIZE "/%" GRN_FMT_SSIZE, |
| 1483 | len, |
| 1484 | ret, |
| 1485 | len); |
| 1486 | } |
| 1487 | |
| 1488 | static void |
| 1489 | h_output_raw(grn_ctx *ctx, int flags, ht_context *hc) |
| 1490 | { |
| 1491 | grn_rc expr_rc = ctx->rc; |
| 1492 | grn_sock fd = hc->msg->u.fd; |
| 1493 | grn_obj header_; |
| 1494 | grn_obj head_; |
| 1495 | grn_obj body_; |
| 1496 | grn_obj foot_; |
| 1497 | grn_obj *header = NULL; |
| 1498 | grn_obj *head = NULL; |
| 1499 | grn_obj *body = NULL; |
| 1500 | grn_obj *foot = NULL; |
| 1501 | char *chunk = NULL; |
| 1502 | unsigned int chunk_size = 0; |
| 1503 | int recv_flags; |
| 1504 | bool is_last_message = (flags & GRN_CTX_TAIL); |
| 1505 | |
| 1506 | GRN_TEXT_INIT(&header_, 0); |
| 1507 | GRN_TEXT_INIT(&head_, 0); |
| 1508 | GRN_TEXT_INIT(&body_, GRN_OBJ_DO_SHALLOW_COPY); |
| 1509 | GRN_TEXT_INIT(&foot_, 0); |
| 1510 | |
| 1511 | grn_ctx_recv(ctx, &chunk, &chunk_size, &recv_flags); |
| 1512 | GRN_TEXT_SET(ctx, &body_, chunk, chunk_size); |
| 1513 | |
| 1514 | if (!hc->in_body) { |
| 1515 | if (is_last_message) { |
| 1516 | h_output_set_header(ctx, &header_, expr_rc, GRN_TEXT_LEN(&body_), NULL); |
| 1517 | hc->is_chunked = false; |
| 1518 | } else { |
| 1519 | h_output_set_header(ctx, &header_, expr_rc, -1, NULL); |
| 1520 | hc->is_chunked = true; |
| 1521 | } |
| 1522 | header = &header_; |
| 1523 | hc->in_body = true; |
| 1524 | } |
| 1525 | |
| 1526 | if (GRN_TEXT_LEN(&body_) > 0) { |
| 1527 | if (hc->is_chunked) { |
| 1528 | grn_text_printf(ctx, |
| 1529 | &head_, |
| 1530 | "%x\r\n", |
| 1531 | (unsigned int)GRN_TEXT_LEN(&body_)); |
| 1532 | head = &head_; |
| 1533 | GRN_TEXT_PUTS(ctx, &foot_, "\r\n"); |
| 1534 | foot = &foot_; |
| 1535 | } |
| 1536 | body = &body_; |
| 1537 | } |
| 1538 | |
| 1539 | if (is_last_message) { |
| 1540 | if (hc->is_chunked) { |
| 1541 | GRN_TEXT_PUTS(ctx, &foot_, "0\r\n"); |
| 1542 | GRN_TEXT_PUTS(ctx, &foot_, "\r\n"); |
| 1543 | foot = &foot_; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | h_output_send(ctx, fd, header, head, body, foot); |
| 1548 | |
| 1549 | GRN_OBJ_FIN(ctx, &foot_); |
| 1550 | GRN_OBJ_FIN(ctx, &body_); |
| 1551 | GRN_OBJ_FIN(ctx, &head_); |
| 1552 | GRN_OBJ_FIN(ctx, &header_); |
| 1553 | } |
| 1554 | |
| 1555 | static void |
| 1556 | h_output_typed(grn_ctx *ctx, int flags, ht_context *hc) |
| 1557 | { |
| 1558 | grn_rc expr_rc = ctx->rc; |
| 1559 | grn_sock fd = hc->msg->u.fd; |
| 1560 | grn_obj header_; |
| 1561 | grn_obj head_; |
| 1562 | grn_obj body_; |
| 1563 | grn_obj foot_; |
| 1564 | grn_obj *header = NULL; |
| 1565 | grn_obj *head = NULL; |
| 1566 | grn_obj *body = NULL; |
| 1567 | grn_obj *foot = NULL; |
| 1568 | char *chunk = NULL; |
| 1569 | unsigned int chunk_size = 0; |
| 1570 | int recv_flags; |
| 1571 | bool should_return_body; |
| 1572 | bool is_last_message = (flags & GRN_CTX_TAIL); |
| 1573 | |
| 1574 | switch (hc->msg->header.qtype) { |
| 1575 | case 'G': |
| 1576 | case 'P': |
| 1577 | should_return_body = true; |
| 1578 | break; |
| 1579 | default: |
| 1580 | should_return_body = false; |
| 1581 | break; |
| 1582 | } |
| 1583 | |
| 1584 | GRN_TEXT_INIT(&header_, 0); |
| 1585 | GRN_TEXT_INIT(&head_, 0); |
| 1586 | GRN_TEXT_INIT(&body_, GRN_OBJ_DO_SHALLOW_COPY); |
| 1587 | GRN_TEXT_INIT(&foot_, 0); |
| 1588 | |
| 1589 | grn_ctx_recv(ctx, &chunk, &chunk_size, &recv_flags); |
| 1590 | GRN_TEXT_SET(ctx, &body_, chunk, chunk_size); |
| 1591 | |
| 1592 | if (!hc->in_body) { |
| 1593 | if (is_last_message) { |
| 1594 | output_envelope(ctx, expr_rc, &head_, &body_, &foot_); |
| 1595 | h_output_set_header(ctx, |
| 1596 | &header_, |
| 1597 | expr_rc, |
| 1598 | GRN_TEXT_LEN(&head_) + GRN_TEXT_LEN(&body_) + |
| 1599 | GRN_TEXT_LEN(&foot_), |
| 1600 | &foot_); |
| 1601 | head = &head_; |
| 1602 | body = &body_; |
| 1603 | foot = &foot_; |
| 1604 | hc->is_chunked = false; |
| 1605 | } else { |
| 1606 | grn_obj first_output; |
| 1607 | GRN_TEXT_INIT(&first_output, 0); |
| 1608 | grn_output_envelope_open(ctx, &first_output); |
| 1609 | size_t first_output_length = GRN_TEXT_LEN(&first_output); |
| 1610 | if (first_output_length > 0) { |
| 1611 | grn_text_printf(ctx, |
| 1612 | &head_, |
| 1613 | "%x\r\n", |
| 1614 | (unsigned int)first_output_length); |
| 1615 | GRN_TEXT_PUT(ctx, |
| 1616 | &head_, |
| 1617 | GRN_TEXT_VALUE(&first_output), |
| 1618 | first_output_length); |
| 1619 | GRN_TEXT_PUTS(ctx, &head_, "\r\n"); |
| 1620 | head = &head_; |
| 1621 | } |
| 1622 | GRN_OBJ_FIN(ctx, &first_output); |
| 1623 | |
| 1624 | h_output_set_header(ctx, &header_, expr_rc, -1, NULL); |
| 1625 | hc->is_chunked = true; |
| 1626 | } |
| 1627 | header = &header_; |
| 1628 | hc->in_body = true; |
| 1629 | } |
| 1630 | |
| 1631 | if (!should_return_body) { |
| 1632 | h_output_send(ctx, fd, header, NULL, NULL, NULL); |
| 1633 | goto exit; |
| 1634 | } |
| 1635 | |
| 1636 | if (!hc->is_chunked) { |
| 1637 | if (GRN_TEXT_LEN(&body_) > 0) { |
| 1638 | body = &body_; |
| 1639 | } |
| 1640 | h_output_send(ctx, fd, header, head, body, foot); |
| 1641 | goto exit; |
| 1642 | } |
| 1643 | |
| 1644 | grn_obj chunk_; |
| 1645 | GRN_TEXT_INIT(&chunk_, GRN_OBJ_DO_SHALLOW_COPY); |
| 1646 | const size_t max_chunk_size = 64 * 1024 * 1024; |
| 1647 | size_t offset = 0; |
| 1648 | for (offset = 0; GRN_TEXT_LEN(&body_) - offset > max_chunk_size; |
| 1649 | offset += max_chunk_size) { |
| 1650 | GRN_TEXT_SET(ctx, &chunk_, GRN_TEXT_VALUE(&body_) + offset, max_chunk_size); |
| 1651 | grn_text_printf(ctx, &head_, "%x\r\n", (unsigned int)GRN_TEXT_LEN(&chunk_)); |
| 1652 | GRN_TEXT_PUTS(ctx, &foot_, "\r\n"); |
| 1653 | h_output_send(ctx, fd, header, &head_, &chunk_, &foot_); |
| 1654 | GRN_BULK_REWIND(&head_); |
| 1655 | GRN_BULK_REWIND(&foot_); |
| 1656 | header = NULL; |
| 1657 | head = NULL; |
| 1658 | foot = NULL; |
| 1659 | } |
| 1660 | if (GRN_TEXT_LEN(&body_) - offset > 0) { |
| 1661 | GRN_TEXT_SET(ctx, |
| 1662 | &chunk_, |
| 1663 | GRN_TEXT_VALUE(&body_) + offset, |
| 1664 | GRN_TEXT_LEN(&body_) - offset); |
| 1665 | grn_text_printf(ctx, &head_, "%x\r\n", (unsigned int)GRN_TEXT_LEN(&chunk_)); |
| 1666 | head = &head_; |
| 1667 | GRN_TEXT_PUTS(ctx, &foot_, "\r\n"); |
| 1668 | foot = &foot_; |
| 1669 | body = &chunk_; |
| 1670 | } |
| 1671 | if (is_last_message) { |
| 1672 | grn_obj last_output; |
| 1673 | GRN_TEXT_INIT(&last_output, 0); |
| 1674 | grn_output_envelope_close(ctx, |
| 1675 | &last_output, |
| 1676 | expr_rc, |
| 1677 | input_path, |
| 1678 | number_of_lines); |
| 1679 | size_t last_output_length = GRN_TEXT_LEN(&last_output); |
| 1680 | grn_text_printf(ctx, &foot_, "%x\r\n", (unsigned int)last_output_length); |
| 1681 | GRN_TEXT_PUT(ctx, &foot_, GRN_TEXT_VALUE(&last_output), last_output_length); |
| 1682 | GRN_TEXT_PUTS(ctx, &foot_, "\r\n"); |
| 1683 | GRN_OBJ_FIN(ctx, &last_output); |
| 1684 | |
| 1685 | GRN_TEXT_PUTS(ctx, &foot_, "0\r\n"); |
| 1686 | GRN_TEXT_PUTS(ctx, &foot_, "\r\n"); |
| 1687 | foot = &foot_; |
| 1688 | } |
| 1689 | h_output_send(ctx, fd, header, head, body, foot); |
| 1690 | GRN_OBJ_FIN(ctx, &chunk_); |
| 1691 | |
| 1692 | exit: |
| 1693 | GRN_OBJ_FIN(ctx, &foot_); |
| 1694 | GRN_OBJ_FIN(ctx, &body_); |
| 1695 | GRN_OBJ_FIN(ctx, &head_); |
| 1696 | GRN_OBJ_FIN(ctx, &header_); |
| 1697 | } |
| 1698 | |
| 1699 | static void |
| 1700 | h_output(grn_ctx *ctx, int flags, void *arg) |
| 1701 | { |
| 1702 | ht_context *hc = (ht_context *)arg; |
| 1703 | |
| 1704 | switch (grn_ctx_get_output_type(ctx)) { |
| 1705 | case GRN_CONTENT_GROONGA_COMMAND_LIST: |
| 1706 | case GRN_CONTENT_NONE: |
| 1707 | h_output_raw(ctx, flags, hc); |
| 1708 | break; |
| 1709 | default: |
| 1710 | h_output_typed(ctx, flags, hc); |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | typedef enum { |
| 1716 | HTTP_CONTENT_TYPE_UNKNOWN, |
| 1717 | HTTP_CONTENT_TYPE_WWW_FORM_URLENCODED, |
| 1718 | HTTP_CONTENT_TYPE_JSON, |
| 1719 | HTTP_CONTENT_TYPE_APACHE_ARROW_STREAMING, |
| 1720 | } http_content_type; |
| 1721 | |
| 1722 | typedef struct { |
| 1723 | const char *method_start; |
| 1724 | int method_length; |
| 1725 | const char *path_start; |
| 1726 | int path_length; |
| 1727 | http_content_type content_type; |
| 1728 | const char *content_type_raw; |
| 1729 | long long int content_type_raw_length; |
| 1730 | long long int content_length; |
| 1731 | bool have_100_continue; |
| 1732 | bool is_chunked_request; |
| 1733 | const char *body_start; |
| 1734 | const char *body_end; |
| 1735 | } h_header; |
| 1736 | |
| 1737 | #define STRING_EQUAL(string, string_length, constant_string) \ |
| 1738 | (string_length == strlen(constant_string) && \ |
| 1739 | strncmp(string, constant_string, string_length) == 0) |
| 1740 | |
| 1741 | #define STRING_EQUAL_CI(string, string_length, constant_string) \ |
| 1742 | (string_length == strlen(constant_string) && \ |
| 1743 | grn_strncasecmp(string, constant_string, string_length) == 0) |
| 1744 | |
| 1745 | static const char * |
| 1746 | h_parse_header_request_line(grn_ctx *ctx, |
| 1747 | const char *start, |
| 1748 | const char *end, |
| 1749 | h_header *header) |
| 1750 | { |
| 1751 | const char *current; |
| 1752 | |
| 1753 | { |
| 1754 | header->method_start = start; |
| 1755 | for (current = header->method_start; current < end; current++) { |
| 1756 | if (current[0] == '\n') { |
| 1757 | return NULL; |
| 1758 | } |
| 1759 | if (current[0] == ' ') { |
| 1760 | header->method_length = current - header->method_start; |
| 1761 | current++; |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | if (header->method_length == -1) { |
| 1766 | return NULL; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | { |
| 1771 | header->path_start = current; |
| 1772 | header->path_length = -1; |
| 1773 | for (; current < end; current++) { |
| 1774 | if (current[0] == '\n') { |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | if (current[0] == ' ') { |
| 1778 | header->path_length = current - header->path_start; |
| 1779 | current++; |
| 1780 | break; |
| 1781 | } |
| 1782 | } |
| 1783 | if (header->path_length == -1) { |
| 1784 | return NULL; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | { |
| 1789 | const char *http_version_start = current; |
| 1790 | int http_version_length = -1; |
| 1791 | for (; current < end; current++) { |
| 1792 | if (current[0] == '\n') { |
| 1793 | http_version_length = current - http_version_start; |
| 1794 | if (http_version_length > 0 && |
| 1795 | http_version_start[http_version_length - 1] == '\r') { |
| 1796 | http_version_length--; |
| 1797 | } |
| 1798 | current++; |
| 1799 | break; |
| 1800 | } |
| 1801 | } |
| 1802 | if (http_version_length == -1) { |
| 1803 | return NULL; |
| 1804 | } |
| 1805 | if (!(STRING_EQUAL_CI(http_version_start, |
| 1806 | http_version_length, |
| 1807 | "HTTP/1.0") || |
| 1808 | STRING_EQUAL_CI(http_version_start, |
| 1809 | http_version_length, |
| 1810 | "HTTP/1.1"))) { |
| 1811 | return NULL; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | return current; |
| 1816 | } |
| 1817 | |
| 1818 | static const char * |
| 1819 | h_parse_header_values(grn_ctx *ctx, |
| 1820 | const char *start, |
| 1821 | const char *end, |
| 1822 | h_header *header) |
| 1823 | { |
| 1824 | const char *current; |
| 1825 | const char *name = start; |
| 1826 | int name_length = -1; |
| 1827 | const char *value = NULL; |
| 1828 | int value_length = -1; |
| 1829 | |
| 1830 | for (current = start; current < end; current++) { |
| 1831 | switch (current[0]) { |
| 1832 | case '\n': |
| 1833 | if (name_length == -1) { |
| 1834 | if (current - name == 1 && current[-1] == '\r') { |
| 1835 | return current + 1; |
| 1836 | } else { |
| 1837 | /* No ":" header line. TODO: report error. */ |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | } else { |
| 1841 | while (value < current && value[0] == ' ') { |
| 1842 | value++; |
| 1843 | } |
| 1844 | value_length = current - value; |
| 1845 | if (value_length > 0 && value[value_length - 1] == '\r') { |
| 1846 | value_length--; |
| 1847 | } |
| 1848 | if (STRING_EQUAL_CI(name, name_length, "Content-Type")) { |
| 1849 | header->content_type_raw = value; |
| 1850 | header->content_type_raw_length = value_length; |
| 1851 | const char *content_type = value; |
| 1852 | int content_type_length = value_length; |
| 1853 | int i; |
| 1854 | for (i = 0; i < content_type_length; i++) { |
| 1855 | if (content_type[i] == ';') { |
| 1856 | content_type_length = i; |
| 1857 | break; |
| 1858 | } |
| 1859 | } |
| 1860 | if (STRING_EQUAL_CI(content_type, |
| 1861 | content_type_length, |
| 1862 | "application/x-www-form-urlencoded")) { |
| 1863 | header->content_type = HTTP_CONTENT_TYPE_WWW_FORM_URLENCODED; |
| 1864 | } else if (STRING_EQUAL_CI(content_type, |
| 1865 | content_type_length, |
| 1866 | "application/json")) { |
| 1867 | header->content_type = HTTP_CONTENT_TYPE_JSON; |
| 1868 | } else if (STRING_EQUAL_CI(content_type, |
| 1869 | content_type_length, |
| 1870 | "application/vnd.apache.arrow.stream")) { |
| 1871 | header->content_type = HTTP_CONTENT_TYPE_APACHE_ARROW_STREAMING; |
| 1872 | } else if (STRING_EQUAL_CI(content_type, |
| 1873 | content_type_length, |
| 1874 | "application/x-apache-arrow-streaming")) { |
| 1875 | header->content_type = HTTP_CONTENT_TYPE_APACHE_ARROW_STREAMING; |
| 1876 | } else { |
| 1877 | header->content_type = HTTP_CONTENT_TYPE_UNKNOWN; |
| 1878 | } |
| 1879 | } else if (STRING_EQUAL_CI(name, name_length, "Content-Length")) { |
| 1880 | const char *rest; |
| 1881 | header->content_length = |
| 1882 | grn_atoll(value, value + value_length, &rest); |
| 1883 | if (rest != value + value_length) { |
| 1884 | /* Invalid Content-Length value. TODO: report error. */ |
| 1885 | header->content_length = -1; |
| 1886 | } |
| 1887 | } else if (STRING_EQUAL_CI(name, name_length, "Expect")) { |
| 1888 | if (STRING_EQUAL(value, value_length, "100-continue")) { |
| 1889 | header->have_100_continue = true; |
| 1890 | } |
| 1891 | } else if (STRING_EQUAL_CI(name, name_length, "Transfer-Encoding")) { |
| 1892 | if (STRING_EQUAL(value, value_length, "chunked")) { |
| 1893 | header->is_chunked_request = true; |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | name = current + 1; |
| 1898 | name_length = -1; |
| 1899 | value = NULL; |
| 1900 | value_length = -1; |
| 1901 | break; |
| 1902 | case ':': |
| 1903 | if (name_length == -1) { |
| 1904 | name_length = current - name; |
| 1905 | value = current + 1; |
| 1906 | } |
| 1907 | break; |
| 1908 | default: |
| 1909 | break; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | return NULL; |
| 1914 | } |
| 1915 | |
| 1916 | static bool |
| 1917 | h_parse_header(grn_ctx *ctx, |
| 1918 | const char *start, |
| 1919 | const char *end, |
| 1920 | h_header *header) |
| 1921 | { |
| 1922 | const char *current; |
| 1923 | |
| 1924 | current = h_parse_header_request_line(ctx, start, end, header); |
| 1925 | if (!current) { |
| 1926 | return false; |
| 1927 | } |
| 1928 | current = h_parse_header_values(ctx, current, end, header); |
| 1929 | if (!current) { |
| 1930 | return false; |
| 1931 | } |
| 1932 | |
| 1933 | if (current == end) { |
| 1934 | header->body_start = NULL; |
| 1935 | } else { |
| 1936 | header->body_start = current; |
| 1937 | } |
| 1938 | |
| 1939 | return true; |
| 1940 | } |
| 1941 | |
| 1942 | static void |
| 1943 | do_htreq_get(grn_ctx *ctx, ht_context *hc) |
| 1944 | { |
| 1945 | grn_msg *msg = hc->msg; |
| 1946 | h_header header; |
| 1947 | header.method_start = NULL; |
| 1948 | header.method_length = -1; |
| 1949 | header.path_start = NULL; |
| 1950 | header.path_length = -1; |
| 1951 | if (!h_parse_header_request_line(ctx, |
| 1952 | GRN_BULK_HEAD((grn_obj *)msg), |
| 1953 | GRN_BULK_CURR((grn_obj *)msg), |
| 1954 | &header)) { |
| 1955 | return; |
| 1956 | } |
| 1957 | grn_ctx_send(ctx, header.path_start, header.path_length, GRN_CTX_TAIL); |
| 1958 | } |
| 1959 | |
| 1960 | typedef void (*grn_htreq_post_process_body_chunk_func)(grn_ctx *ctx, |
| 1961 | const char *chunk, |
| 1962 | size_t chunk_size, |
| 1963 | void *user_data); |
| 1964 | |
| 1965 | /* #define GRN_HTTP_POST_DEBUG */ |
| 1966 | #ifdef GRN_HTTP_POST_DEBUG |
| 1967 | # define grn_http_post_p(...) printf(__VA_ARGS__) |
| 1968 | #else |
| 1969 | # define grn_http_post_p(...) |
| 1970 | #endif |
| 1971 | |
| 1972 | static const char * |
| 1973 | do_htreq_post_process_body_chunked_read_chunk_size(grn_ctx *ctx, |
| 1974 | const char *data, |
| 1975 | size_t data_size, |
| 1976 | size_t *chunk_size, |
| 1977 | size_t total_body_size) |
| 1978 | { |
| 1979 | const char *current = data; |
| 1980 | const char *data_end = data + data_size; |
| 1981 | while (data_end - current >= 2) { |
| 1982 | switch (current[0]) { |
| 1983 | case '0': |
| 1984 | case '1': |
| 1985 | case '2': |
| 1986 | case '3': |
| 1987 | case '4': |
| 1988 | case '5': |
| 1989 | case '6': |
| 1990 | case '7': |
| 1991 | case '8': |
| 1992 | case '9': |
| 1993 | case 'a': |
| 1994 | case 'b': |
| 1995 | case 'c': |
| 1996 | case 'd': |
| 1997 | case 'e': |
| 1998 | case 'f': |
| 1999 | case 'A': |
| 2000 | case 'B': |
| 2001 | case 'C': |
| 2002 | case 'D': |
| 2003 | case 'E': |
| 2004 | case 'F': |
| 2005 | /* OK */ |
| 2006 | break; |
| 2007 | case '\r': |
| 2008 | if (current[1] == '\n') { |
| 2009 | if (data == current) { |
| 2010 | ERR(GRN_INVALID_ARGUMENT, |
| 2011 | "[http][post][%" GRN_FMT_SIZE "] " |
| 2012 | "chunked request size line doesn't have content", |
| 2013 | total_body_size); |
| 2014 | return NULL; |
| 2015 | } |
| 2016 | *chunk_size = grn_htoui(data, current, NULL); |
| 2017 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] " |
| 2018 | "read chunk size: %" GRN_FMT_SIZE ": <%.*s>\n", |
| 2019 | total_body_size, |
| 2020 | *chunk_size, |
| 2021 | (int)(current - data), |
| 2022 | data); |
| 2023 | return current + 2; |
| 2024 | } else { |
| 2025 | ERR(GRN_INVALID_ARGUMENT, |
| 2026 | "[http][post][%" GRN_FMT_SIZE "] " |
| 2027 | "chunked request size line isn't ended by CRLF: " |
| 2028 | "<%.*s>", |
| 2029 | total_body_size, |
| 2030 | (int)((current + 2) - data), |
| 2031 | data); |
| 2032 | return NULL; |
| 2033 | } |
| 2034 | break; |
| 2035 | default: |
| 2036 | ERR(GRN_INVALID_ARGUMENT, |
| 2037 | "[http][post][%" GRN_FMT_SIZE "] " |
| 2038 | "chunked request size line has garbage: " |
| 2039 | "<%.*s>", |
| 2040 | total_body_size, |
| 2041 | (int)((current + 1) - data), |
| 2042 | data); |
| 2043 | return NULL; |
| 2044 | } |
| 2045 | current++; |
| 2046 | } |
| 2047 | return NULL; |
| 2048 | } |
| 2049 | |
| 2050 | static void |
| 2051 | do_htreq_post_process_body_chunked(grn_ctx *ctx, |
| 2052 | h_header *header, |
| 2053 | grn_sock fd, |
| 2054 | grn_htreq_post_process_body_chunk_func func, |
| 2055 | void *user_data) |
| 2056 | { |
| 2057 | grn_obj buffer; |
| 2058 | GRN_TEXT_INIT(&buffer, 0); |
| 2059 | size_t buffer_offset = 0; |
| 2060 | const char *data = NULL; |
| 2061 | size_t data_size = 0; |
| 2062 | bool need_chunk_size = true; |
| 2063 | bool need_chunk_end_cr = false; |
| 2064 | bool need_chunk_end_lf = false; |
| 2065 | size_t chunk_size = 0; |
| 2066 | size_t total_body_size = 0; |
| 2067 | |
| 2068 | if (header->body_start) { |
| 2069 | data = header->body_start; |
| 2070 | data_size = header->body_end - header->body_start; |
| 2071 | } |
| 2072 | |
| 2073 | while (true) { |
| 2074 | if (!data) { |
| 2075 | const size_t buffer_size = 8192; |
| 2076 | if (GRN_BULK_REST(&buffer) < buffer_size) { |
| 2077 | grn_bulk_reserve(ctx, &buffer, buffer_size); |
| 2078 | } |
| 2079 | int recv_flags = 0; |
| 2080 | ssize_t recv_length = |
| 2081 | recv(fd, GRN_BULK_CURR(&buffer), GRN_BULK_REST(&buffer), recv_flags); |
| 2082 | if (recv_length == 0) { |
| 2083 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] no data\n", |
| 2084 | total_body_size); |
| 2085 | break; |
| 2086 | } |
| 2087 | if (recv_length == -1) { |
| 2088 | SOERR("recv"); |
| 2089 | break; |
| 2090 | } |
| 2091 | GRN_BULK_INCR_LEN(&buffer, recv_length); |
| 2092 | data = GRN_BULK_HEAD(&buffer) + buffer_offset; |
| 2093 | data_size = GRN_BULK_VSIZE(&buffer) - buffer_offset; |
| 2094 | } |
| 2095 | |
| 2096 | if (need_chunk_end_cr) { |
| 2097 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] need chunk end CR\n", |
| 2098 | total_body_size); |
| 2099 | if (data[0] != '\r') { |
| 2100 | ERR(GRN_INVALID_ARGUMENT, |
| 2101 | "[http][post][%" GRN_FMT_SIZE "] " |
| 2102 | "chunk end CR doesn't exit: <%c>", |
| 2103 | total_body_size, |
| 2104 | data[0]); |
| 2105 | break; |
| 2106 | } |
| 2107 | need_chunk_end_cr = false; |
| 2108 | if (data_size == 1) { |
| 2109 | data = NULL; |
| 2110 | data_size = 0; |
| 2111 | GRN_BULK_REWIND(&buffer); |
| 2112 | buffer_offset = 0; |
| 2113 | continue; |
| 2114 | } |
| 2115 | data++; |
| 2116 | data_size--; |
| 2117 | } |
| 2118 | |
| 2119 | if (need_chunk_end_lf) { |
| 2120 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] need chunk end LF\n", |
| 2121 | total_body_size); |
| 2122 | if (data[0] != '\n') { |
| 2123 | ERR(GRN_INVALID_ARGUMENT, |
| 2124 | "[http][post][%" GRN_FMT_SIZE "] chunk end LF doesn't exit: <%c>", |
| 2125 | total_body_size, |
| 2126 | data[0]); |
| 2127 | break; |
| 2128 | } |
| 2129 | need_chunk_end_lf = false; |
| 2130 | if (data_size == 1) { |
| 2131 | data = NULL; |
| 2132 | data_size = 0; |
| 2133 | GRN_BULK_REWIND(&buffer); |
| 2134 | buffer_offset = 0; |
| 2135 | continue; |
| 2136 | } |
| 2137 | data++; |
| 2138 | data_size--; |
| 2139 | } |
| 2140 | |
| 2141 | if (need_chunk_size) { |
| 2142 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] need chunk size\n", |
| 2143 | total_body_size); |
| 2144 | const char *chunk_start = |
| 2145 | do_htreq_post_process_body_chunked_read_chunk_size(ctx, |
| 2146 | data, |
| 2147 | data_size, |
| 2148 | &chunk_size, |
| 2149 | total_body_size); |
| 2150 | if (ctx->rc != GRN_SUCCESS) { |
| 2151 | break; |
| 2152 | } |
| 2153 | if (chunk_size == 0) { |
| 2154 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] " |
| 2155 | "zero chunk_size: <%.*s>\n", |
| 2156 | total_body_size, |
| 2157 | (int)((data + data_size) - chunk_start), |
| 2158 | chunk_start); |
| 2159 | break; |
| 2160 | } |
| 2161 | if (!chunk_start) { |
| 2162 | /* TODO: Test this */ |
| 2163 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] " |
| 2164 | "no chunk start: %p:%p:%p\n", |
| 2165 | total_body_size, |
| 2166 | data, |
| 2167 | GRN_BULK_HEAD(&buffer), |
| 2168 | GRN_BULK_CURR(&buffer)); |
| 2169 | if (GRN_BULK_HEAD(&buffer) <= data && data < GRN_BULK_CURR(&buffer)) { |
| 2170 | buffer_offset = data - GRN_BULK_HEAD(&buffer); |
| 2171 | } else { |
| 2172 | GRN_TEXT_PUT(ctx, &buffer, data, data_size); |
| 2173 | } |
| 2174 | data = NULL; |
| 2175 | data_size = 0; |
| 2176 | continue; |
| 2177 | } |
| 2178 | size_t read_chunk_size = (data + data_size) - chunk_start; |
| 2179 | data = chunk_start; |
| 2180 | data_size = read_chunk_size; |
| 2181 | } |
| 2182 | |
| 2183 | grn_http_post_p("[http][post][%" GRN_FMT_SIZE "] " |
| 2184 | "data: %" GRN_FMT_SIZE ":%" GRN_FMT_SIZE "\n", |
| 2185 | total_body_size, |
| 2186 | data_size, |
| 2187 | chunk_size); |
| 2188 | if (data_size >= chunk_size) { |
| 2189 | func(ctx, data, chunk_size, user_data); |
| 2190 | if (ctx->rc != GRN_SUCCESS) { |
| 2191 | break; |
| 2192 | } |
| 2193 | total_body_size += chunk_size; |
| 2194 | if (data_size == chunk_size) { |
| 2195 | data = NULL; |
| 2196 | data_size = 0; |
| 2197 | GRN_BULK_REWIND(&buffer); |
| 2198 | buffer_offset = 0; |
| 2199 | } else { |
| 2200 | data += chunk_size; |
| 2201 | data_size -= chunk_size; |
| 2202 | } |
| 2203 | need_chunk_end_cr = true; |
| 2204 | need_chunk_end_lf = true; |
| 2205 | need_chunk_size = true; |
| 2206 | } else { |
| 2207 | func(ctx, data, data_size, user_data); |
| 2208 | if (ctx->rc != GRN_SUCCESS) { |
| 2209 | break; |
| 2210 | } |
| 2211 | total_body_size += data_size; |
| 2212 | chunk_size -= data_size; |
| 2213 | data = NULL; |
| 2214 | data_size = 0; |
| 2215 | GRN_BULK_REWIND(&buffer); |
| 2216 | buffer_offset = 0; |
| 2217 | need_chunk_size = false; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | GRN_OBJ_FIN(ctx, &buffer); |
| 2222 | } |
| 2223 | |
| 2224 | static void |
| 2225 | do_htreq_post_process_body_raw(grn_ctx *ctx, |
| 2226 | h_header *header, |
| 2227 | grn_sock fd, |
| 2228 | grn_htreq_post_process_body_chunk_func func, |
| 2229 | void *user_data) |
| 2230 | { |
| 2231 | long long int read_content_length = 0; |
| 2232 | if (header->body_start) { |
| 2233 | size_t read_body_length = header->body_end - header->body_start; |
| 2234 | read_content_length += read_body_length; |
| 2235 | func(ctx, header->body_start, read_body_length, user_data); |
| 2236 | if (ctx->rc != GRN_SUCCESS) { |
| 2237 | return; |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | while (read_content_length < header->content_length) { |
| 2242 | #define POST_BUFFER_SIZE 8192 |
| 2243 | char buffer[POST_BUFFER_SIZE]; |
| 2244 | int recv_flags = 0; |
| 2245 | ssize_t recv_length = recv(fd, buffer, POST_BUFFER_SIZE, recv_flags); |
| 2246 | #undef POST_BUFFER_SIZE |
| 2247 | if (recv_length == 0) { |
| 2248 | break; |
| 2249 | } |
| 2250 | if (recv_length == -1) { |
| 2251 | SOERR("recv"); |
| 2252 | break; |
| 2253 | } |
| 2254 | func(ctx, buffer, recv_length, user_data); |
| 2255 | if (ctx->rc != GRN_SUCCESS) { |
| 2256 | break; |
| 2257 | } |
| 2258 | read_content_length += recv_length; |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | static void |
| 2263 | do_htreq_post_process_body(grn_ctx *ctx, |
| 2264 | h_header *header, |
| 2265 | grn_sock fd, |
| 2266 | grn_htreq_post_process_body_chunk_func func, |
| 2267 | void *user_data) |
| 2268 | { |
| 2269 | if (header->is_chunked_request) { |
| 2270 | do_htreq_post_process_body_chunked(ctx, header, fd, func, user_data); |
| 2271 | } else { |
| 2272 | do_htreq_post_process_body_raw(ctx, header, fd, func, user_data); |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | typedef struct { |
| 2277 | grn_obj command; |
| 2278 | bool have_question; |
| 2279 | bool is_first_chunk; |
| 2280 | } http_post_www_form_urlencoded_data; |
| 2281 | |
| 2282 | static void |
| 2283 | do_htreq_post_process_body_chunks_www_form_urlencoded(grn_ctx *ctx, |
| 2284 | const char *chunk, |
| 2285 | size_t chunk_size, |
| 2286 | void *user_data) |
| 2287 | { |
| 2288 | http_post_www_form_urlencoded_data *data = user_data; |
| 2289 | |
| 2290 | if (data->is_first_chunk) { |
| 2291 | if (data->have_question) { |
| 2292 | GRN_TEXT_PUTC(ctx, &(data->command), '&'); |
| 2293 | } else { |
| 2294 | GRN_TEXT_PUTC(ctx, &(data->command), '?'); |
| 2295 | } |
| 2296 | data->is_first_chunk = false; |
| 2297 | } |
| 2298 | GRN_TEXT_PUT(ctx, &(data->command), chunk, chunk_size); |
| 2299 | } |
| 2300 | |
| 2301 | static void |
| 2302 | do_htreq_post_process_body_www_form_urlencoded(grn_ctx *ctx, |
| 2303 | h_header *header, |
| 2304 | grn_sock fd) |
| 2305 | { |
| 2306 | http_post_www_form_urlencoded_data data; |
| 2307 | GRN_TEXT_INIT(&(data.command), 0); |
| 2308 | GRN_TEXT_PUT(ctx, &(data.command), header->path_start, header->path_length); |
| 2309 | data.have_question = false; |
| 2310 | { |
| 2311 | int i; |
| 2312 | for (i = 0; i < header->path_length; i++) { |
| 2313 | if (header->path_start[i] == '?') { |
| 2314 | data.have_question = true; |
| 2315 | break; |
| 2316 | } |
| 2317 | } |
| 2318 | } |
| 2319 | data.is_first_chunk = true; |
| 2320 | |
| 2321 | do_htreq_post_process_body( |
| 2322 | ctx, |
| 2323 | header, |
| 2324 | fd, |
| 2325 | do_htreq_post_process_body_chunks_www_form_urlencoded, |
| 2326 | &data); |
| 2327 | if (ctx->rc == GRN_SUCCESS) { |
| 2328 | grn_ctx_send(ctx, |
| 2329 | GRN_TEXT_VALUE(&(data.command)), |
| 2330 | GRN_TEXT_LEN(&(data.command)), |
| 2331 | GRN_CTX_TAIL); |
| 2332 | } |
| 2333 | GRN_OBJ_FIN(ctx, &(data.command)); |
| 2334 | } |
| 2335 | |
| 2336 | typedef struct { |
| 2337 | grn_obj chunk_buffer1; |
| 2338 | grn_obj chunk_buffer2; |
| 2339 | grn_obj *chunk_buffer_old; |
| 2340 | grn_obj *chunk_buffer_current; |
| 2341 | } http_post_load_data; |
| 2342 | |
| 2343 | static void |
| 2344 | http_post_load_data_init(grn_ctx *ctx, http_post_load_data *data) |
| 2345 | { |
| 2346 | GRN_TEXT_INIT(&(data->chunk_buffer1), 0); |
| 2347 | GRN_TEXT_INIT(&(data->chunk_buffer2), 0); |
| 2348 | data->chunk_buffer_old = &(data->chunk_buffer1); |
| 2349 | data->chunk_buffer_current = &(data->chunk_buffer2); |
| 2350 | } |
| 2351 | |
| 2352 | static void |
| 2353 | http_post_load_data_fin(grn_ctx *ctx, http_post_load_data *data) |
| 2354 | { |
| 2355 | if (ctx->rc == GRN_SUCCESS && |
| 2356 | (GRN_TEXT_LEN(data->chunk_buffer_old) > 0 || |
| 2357 | GRN_TEXT_LEN(data->chunk_buffer_current) > 0)) { |
| 2358 | if (GRN_TEXT_LEN(data->chunk_buffer_old) > 0) { |
| 2359 | int flags; |
| 2360 | if (GRN_TEXT_LEN(data->chunk_buffer_current) > 0) { |
| 2361 | flags = GRN_CTX_MORE; |
| 2362 | } else { |
| 2363 | flags = GRN_CTX_TAIL; |
| 2364 | } |
| 2365 | grn_ctx_send(ctx, |
| 2366 | GRN_TEXT_VALUE(data->chunk_buffer_old), |
| 2367 | GRN_TEXT_LEN(data->chunk_buffer_old), |
| 2368 | flags); |
| 2369 | } |
| 2370 | if (GRN_TEXT_LEN(data->chunk_buffer_current) > 0) { |
| 2371 | grn_ctx_send(ctx, |
| 2372 | GRN_TEXT_VALUE(data->chunk_buffer_current), |
| 2373 | GRN_TEXT_LEN(data->chunk_buffer_current), |
| 2374 | GRN_CTX_TAIL); |
| 2375 | } |
| 2376 | } else { |
| 2377 | grn_ctx_send(ctx, NULL, 0, GRN_CTX_TAIL); |
| 2378 | } |
| 2379 | GRN_OBJ_FIN(ctx, &(data->chunk_buffer1)); |
| 2380 | GRN_OBJ_FIN(ctx, &(data->chunk_buffer2)); |
| 2381 | } |
| 2382 | |
| 2383 | static void |
| 2384 | http_post_load_data_flush(grn_ctx *ctx, http_post_load_data *data) |
| 2385 | { |
| 2386 | if (GRN_TEXT_LEN(data->chunk_buffer_old) > 0) { |
| 2387 | grn_ctx_send(ctx, |
| 2388 | GRN_TEXT_VALUE(data->chunk_buffer_old), |
| 2389 | GRN_TEXT_LEN(data->chunk_buffer_old), |
| 2390 | GRN_CTX_MORE); |
| 2391 | GRN_BULK_REWIND(data->chunk_buffer_old); |
| 2392 | } |
| 2393 | |
| 2394 | grn_obj *tmp = data->chunk_buffer_current; |
| 2395 | data->chunk_buffer_current = data->chunk_buffer_old; |
| 2396 | data->chunk_buffer_old = tmp; |
| 2397 | } |
| 2398 | |
| 2399 | static void |
| 2400 | do_htreq_post_process_body_load_chunks_json(grn_ctx *ctx, |
| 2401 | const char *chunk, |
| 2402 | size_t chunk_size, |
| 2403 | void *user_data) |
| 2404 | { |
| 2405 | http_post_load_data *data = user_data; |
| 2406 | |
| 2407 | const char *chunk_start = chunk; |
| 2408 | const char *chunk_end = chunk + chunk_size; |
| 2409 | const char *chunk_current; |
| 2410 | ; |
| 2411 | for (chunk_current = chunk_end - 1; chunk_current > chunk; chunk_current--) { |
| 2412 | bool is_separator; |
| 2413 | switch (chunk_current[0]) { |
| 2414 | case '\n': |
| 2415 | case ',': |
| 2416 | is_separator = true; |
| 2417 | break; |
| 2418 | default: |
| 2419 | is_separator = false; |
| 2420 | break; |
| 2421 | } |
| 2422 | if (!is_separator) { |
| 2423 | continue; |
| 2424 | } |
| 2425 | |
| 2426 | GRN_TEXT_PUT(ctx, |
| 2427 | data->chunk_buffer_current, |
| 2428 | chunk, |
| 2429 | chunk_current + 1 - chunk); |
| 2430 | http_post_load_data_flush(ctx, data); |
| 2431 | chunk_start = chunk_current + 1; |
| 2432 | break; |
| 2433 | } |
| 2434 | |
| 2435 | if (chunk_end > chunk_start) { |
| 2436 | GRN_TEXT_PUT(ctx, |
| 2437 | data->chunk_buffer_current, |
| 2438 | chunk_start, |
| 2439 | chunk_end - chunk_start); |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | static void |
| 2444 | do_htreq_post_process_body_load_json(grn_ctx *ctx, |
| 2445 | h_header *header, |
| 2446 | grn_sock fd) |
| 2447 | { |
| 2448 | http_post_load_data data; |
| 2449 | http_post_load_data_init(ctx, &data); |
| 2450 | do_htreq_post_process_body(ctx, |
| 2451 | header, |
| 2452 | fd, |
| 2453 | do_htreq_post_process_body_load_chunks_json, |
| 2454 | &data); |
| 2455 | http_post_load_data_fin(ctx, &data); |
| 2456 | } |
| 2457 | |
| 2458 | static void |
| 2459 | do_htreq_post_process_body_load_chunks_generic(grn_ctx *ctx, |
| 2460 | const char *chunk, |
| 2461 | size_t chunk_size, |
| 2462 | void *user_data) |
| 2463 | { |
| 2464 | http_post_load_data *data = user_data; |
| 2465 | GRN_TEXT_PUT(ctx, data->chunk_buffer_current, chunk, chunk_size); |
| 2466 | http_post_load_data_flush(ctx, data); |
| 2467 | } |
| 2468 | |
| 2469 | static void |
| 2470 | do_htreq_post_process_body_load_generic(grn_ctx *ctx, |
| 2471 | h_header *header, |
| 2472 | grn_sock fd) |
| 2473 | { |
| 2474 | http_post_load_data data; |
| 2475 | http_post_load_data_init(ctx, &data); |
| 2476 | do_htreq_post_process_body(ctx, |
| 2477 | header, |
| 2478 | fd, |
| 2479 | do_htreq_post_process_body_load_chunks_generic, |
| 2480 | &data); |
| 2481 | http_post_load_data_fin(ctx, &data); |
| 2482 | } |
| 2483 | |
| 2484 | static void |
| 2485 | do_htreq_post(grn_ctx *ctx, ht_context *hc) |
| 2486 | { |
| 2487 | grn_msg *msg = hc->msg; |
| 2488 | grn_sock fd = msg->u.fd; |
| 2489 | h_header header; |
| 2490 | |
| 2491 | header.method_start = NULL; |
| 2492 | header.method_length = -1; |
| 2493 | header.path_start = NULL; |
| 2494 | header.path_length = -1; |
| 2495 | header.content_type = HTTP_CONTENT_TYPE_UNKNOWN; |
| 2496 | header.content_type_raw = NULL; |
| 2497 | header.content_type_raw_length = -1; |
| 2498 | header.content_length = -1; |
| 2499 | header.body_start = NULL; |
| 2500 | header.body_end = GRN_BULK_CURR((grn_obj *)msg); |
| 2501 | header.have_100_continue = false; |
| 2502 | header.is_chunked_request = false; |
| 2503 | |
| 2504 | if (!h_parse_header(ctx, |
| 2505 | GRN_BULK_HEAD((grn_obj *)msg), |
| 2506 | header.body_end, |
| 2507 | &header)) { |
| 2508 | return; |
| 2509 | } |
| 2510 | if (!STRING_EQUAL(header.method_start, header.method_length, "POST")) { |
| 2511 | return; |
| 2512 | } |
| 2513 | |
| 2514 | if (header.content_type == HTTP_CONTENT_TYPE_UNKNOWN) { |
| 2515 | const char *specified_content_type = header.content_type_raw; |
| 2516 | int specified_content_type_length = header.content_type_raw_length; |
| 2517 | if (!specified_content_type) { |
| 2518 | specified_content_type = ""; |
| 2519 | specified_content_type_length = 0; |
| 2520 | } |
| 2521 | ERR(GRN_INVALID_ARGUMENT, |
| 2522 | "[http][post] Content-Type must be one of " |
| 2523 | "application/x-www-form-urlencoded, " |
| 2524 | "application/json or " |
| 2525 | "application/vnd.apache.arrow.stream " |
| 2526 | "(application/x-apache-arrow-streaming): " |
| 2527 | "<%.*s>", |
| 2528 | specified_content_type_length, |
| 2529 | specified_content_type); |
| 2530 | ht_context context; |
| 2531 | context.msg = msg; |
| 2532 | context.in_body = false; |
| 2533 | context.is_chunked = false; |
| 2534 | grn_ctx_set_output_type(ctx, GRN_CONTENT_JSON); |
| 2535 | h_output(ctx, GRN_CTX_TAIL, &context); |
| 2536 | return; |
| 2537 | } |
| 2538 | |
| 2539 | if (header.have_100_continue) { |
| 2540 | const char *continue_message = "HTTP/1.1 100 Continue\r\n\r\n"; |
| 2541 | ssize_t send_size; |
| 2542 | int send_flags = MSG_NOSIGNAL; |
| 2543 | send_size = |
| 2544 | send(fd, continue_message, strlen(continue_message), send_flags); |
| 2545 | if (send_size == -1) { |
| 2546 | SOERR("send"); |
| 2547 | return; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | if (header.content_type == HTTP_CONTENT_TYPE_WWW_FORM_URLENCODED) { |
| 2552 | do_htreq_post_process_body_www_form_urlencoded(ctx, &header, fd); |
| 2553 | } else { |
| 2554 | grn_ctx_send(ctx, header.path_start, header.path_length, GRN_CTX_MORE); |
| 2555 | if (ctx->rc != GRN_SUCCESS) { |
| 2556 | ht_context context; |
| 2557 | context.msg = msg; |
| 2558 | context.in_body = false; |
| 2559 | context.is_chunked = false; |
| 2560 | h_output(ctx, GRN_CTX_TAIL, &context); |
| 2561 | return; |
| 2562 | } |
| 2563 | |
| 2564 | if (header.content_type == HTTP_CONTENT_TYPE_JSON) { |
| 2565 | do_htreq_post_process_body_load_json(ctx, &header, fd); |
| 2566 | } else { |
| 2567 | do_htreq_post_process_body_load_generic(ctx, &header, fd); |
| 2568 | } |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | static void |
| 2573 | do_htreq(grn_ctx *ctx, ht_context *hc) |
| 2574 | { |
| 2575 | grn_msg *msg = hc->msg; |
| 2576 | grn_com_header *header = &msg->header; |
| 2577 | switch (header->qtype) { |
| 2578 | case 'G': /* GET */ |
| 2579 | case 'H': /* HEAD */ |
| 2580 | do_htreq_get(ctx, hc); |
| 2581 | break; |
| 2582 | case 'P': /* POST */ |
| 2583 | do_htreq_post(ctx, hc); |
| 2584 | break; |
| 2585 | } |
| 2586 | ERRCLR(ctx); |
| 2587 | if (ctx->rc == GRN_CANCEL) { |
| 2588 | ctx->rc = GRN_SUCCESS; |
| 2589 | } |
| 2590 | /* if (ctx->rc != GRN_OPERATION_WOULD_BLOCK) {...} */ |
| 2591 | grn_msg_close(ctx, (grn_obj *)msg); |
| 2592 | /* if not keep alive connection */ |
| 2593 | grn_sock_close(msg->u.fd); |
| 2594 | grn_com_event_start_accept(ctx, msg->acceptor->ev); |
| 2595 | } |
| 2596 | |
| 2597 | enum { |
| 2598 | MBRES_SUCCESS = 0x00, |
| 2599 | MBRES_KEY_ENOENT = 0x01, |
| 2600 | MBRES_KEY_EEXISTS = 0x02, |
| 2601 | MBRES_E2BIG = 0x03, |
| 2602 | MBRES_EINVAL = 0x04, |
| 2603 | MBRES_NOT_STORED = 0x05, |
| 2604 | MBRES_UNKNOWN_COMMAND = 0x81, |
| 2605 | MBRES_ENOMEM = 0x82, |
| 2606 | }; |
| 2607 | |
| 2608 | enum { |
| 2609 | MBCMD_GET = 0x00, |
| 2610 | MBCMD_SET = 0x01, |
| 2611 | MBCMD_ADD = 0x02, |
| 2612 | MBCMD_REPLACE = 0x03, |
| 2613 | MBCMD_DELETE = 0x04, |
| 2614 | MBCMD_INCREMENT = 0x05, |
| 2615 | MBCMD_DECREMENT = 0x06, |
| 2616 | MBCMD_QUIT = 0x07, |
| 2617 | MBCMD_FLUSH = 0x08, |
| 2618 | MBCMD_GETQ = 0x09, |
| 2619 | MBCMD_NOOP = 0x0a, |
| 2620 | MBCMD_VERSION = 0x0b, |
| 2621 | MBCMD_GETK = 0x0c, |
| 2622 | MBCMD_GETKQ = 0x0d, |
| 2623 | MBCMD_APPEND = 0x0e, |
| 2624 | MBCMD_PREPEND = 0x0f, |
| 2625 | MBCMD_STAT = 0x10, |
| 2626 | MBCMD_SETQ = 0x11, |
| 2627 | MBCMD_ADDQ = 0x12, |
| 2628 | MBCMD_REPLACEQ = 0x13, |
| 2629 | MBCMD_DELETEQ = 0x14, |
| 2630 | MBCMD_INCREMENTQ = 0x15, |
| 2631 | MBCMD_DECREMENTQ = 0x16, |
| 2632 | MBCMD_QUITQ = 0x17, |
| 2633 | MBCMD_FLUSHQ = 0x18, |
| 2634 | MBCMD_APPENDQ = 0x19, |
| 2635 | MBCMD_PREPENDQ = 0x1a |
| 2636 | }; |
| 2637 | |
| 2638 | static grn_obj *cache_table = NULL; |
| 2639 | static grn_obj *cache_value = NULL; |
| 2640 | static grn_obj *cache_flags = NULL; |
| 2641 | static grn_obj *cache_expire = NULL; |
| 2642 | static grn_obj *cache_cas = NULL; |
| 2643 | |
| 2644 | #define CTX_GET(name) (grn_ctx_get(ctx, (name), strlen(name))) |
| 2645 | |
| 2646 | static bool |
| 2647 | memcached_setup_flags_column(grn_ctx *ctx, const char *name) |
| 2648 | { |
| 2649 | cache_flags = grn_obj_column(ctx, cache_table, name, strlen(name)); |
| 2650 | if (cache_flags) { |
| 2651 | return true; |
| 2652 | } |
| 2653 | |
| 2654 | cache_flags = grn_column_create(ctx, |
| 2655 | cache_table, |
| 2656 | name, |
| 2657 | strlen(name), |
| 2658 | NULL, |
| 2659 | GRN_OBJ_COLUMN_SCALAR | GRN_OBJ_PERSISTENT, |
| 2660 | grn_ctx_at(ctx, GRN_DB_UINT32)); |
| 2661 | if (!cache_flags) { |
| 2662 | return false; |
| 2663 | } |
| 2664 | |
| 2665 | return true; |
| 2666 | } |
| 2667 | |
| 2668 | static bool |
| 2669 | memcached_setup_expire_column(grn_ctx *ctx, const char *name) |
| 2670 | { |
| 2671 | cache_expire = grn_obj_column(ctx, cache_table, name, strlen(name)); |
| 2672 | if (cache_expire) { |
| 2673 | return true; |
| 2674 | } |
| 2675 | |
| 2676 | cache_expire = grn_column_create(ctx, |
| 2677 | cache_table, |
| 2678 | name, |
| 2679 | strlen(name), |
| 2680 | NULL, |
| 2681 | GRN_OBJ_COLUMN_SCALAR | GRN_OBJ_PERSISTENT, |
| 2682 | grn_ctx_at(ctx, GRN_DB_UINT32)); |
| 2683 | if (!cache_expire) { |
| 2684 | return false; |
| 2685 | } |
| 2686 | |
| 2687 | return true; |
| 2688 | } |
| 2689 | |
| 2690 | static bool |
| 2691 | memcached_setup_cas_column(grn_ctx *ctx, const char *name) |
| 2692 | { |
| 2693 | cache_cas = grn_obj_column(ctx, cache_table, name, strlen(name)); |
| 2694 | if (cache_cas) { |
| 2695 | return true; |
| 2696 | } |
| 2697 | |
| 2698 | cache_cas = grn_column_create(ctx, |
| 2699 | cache_table, |
| 2700 | name, |
| 2701 | strlen(name), |
| 2702 | NULL, |
| 2703 | GRN_OBJ_COLUMN_SCALAR | GRN_OBJ_PERSISTENT, |
| 2704 | grn_ctx_at(ctx, GRN_DB_UINT64)); |
| 2705 | if (!cache_cas) { |
| 2706 | return false; |
| 2707 | } |
| 2708 | |
| 2709 | return true; |
| 2710 | } |
| 2711 | |
| 2712 | static bool |
| 2713 | memcached_init(grn_ctx *ctx) |
| 2714 | { |
| 2715 | if (memcached_column_name) { |
| 2716 | cache_value = CTX_GET(memcached_column_name); |
| 2717 | if (!cache_value) { |
| 2718 | ERR(GRN_INVALID_ARGUMENT, |
| 2719 | "memcached column doesn't exist: <%s>", |
| 2720 | memcached_column_name); |
| 2721 | return false; |
| 2722 | } |
| 2723 | if (!(grn_obj_is_column(ctx, cache_value) && |
| 2724 | ((cache_value->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) == |
| 2725 | GRN_OBJ_COLUMN_SCALAR))) { |
| 2726 | grn_obj inspected; |
| 2727 | GRN_TEXT_INIT(&inspected, 0); |
| 2728 | grn_inspect(ctx, &inspected, cache_value); |
| 2729 | ERR(GRN_INVALID_ARGUMENT, |
| 2730 | "memcached column must be scalar column: <%.*s>", |
| 2731 | (int)GRN_TEXT_LEN(&inspected), |
| 2732 | GRN_TEXT_VALUE(&inspected)); |
| 2733 | GRN_OBJ_FIN(ctx, &inspected); |
| 2734 | return false; |
| 2735 | } |
| 2736 | if (!(GRN_DB_SHORT_TEXT <= grn_obj_get_range(ctx, cache_value) && |
| 2737 | grn_obj_get_range(ctx, cache_value) <= GRN_DB_LONG_TEXT)) { |
| 2738 | grn_obj inspected; |
| 2739 | GRN_TEXT_INIT(&inspected, 0); |
| 2740 | grn_inspect(ctx, &inspected, cache_value); |
| 2741 | ERR(GRN_INVALID_ARGUMENT, |
| 2742 | "memcached column must be text column: <%.*s>", |
| 2743 | (int)GRN_TEXT_LEN(&inspected), |
| 2744 | GRN_TEXT_VALUE(&inspected)); |
| 2745 | GRN_OBJ_FIN(ctx, &inspected); |
| 2746 | return false; |
| 2747 | } |
| 2748 | |
| 2749 | cache_table = grn_ctx_at(ctx, cache_value->header.domain); |
| 2750 | if (cache_table->header.type == GRN_TABLE_NO_KEY) { |
| 2751 | grn_obj inspected; |
| 2752 | GRN_TEXT_INIT(&inspected, 0); |
| 2753 | grn_inspect(ctx, &inspected, cache_table); |
| 2754 | ERR( |
| 2755 | GRN_INVALID_ARGUMENT, |
| 2756 | "memcached column's table must be HASH_KEY, PAT_KEY or DAT_KEY table: " |
| 2757 | "<%.*s>", |
| 2758 | (int)GRN_TEXT_LEN(&inspected), |
| 2759 | GRN_TEXT_VALUE(&inspected)); |
| 2760 | GRN_OBJ_FIN(ctx, &inspected); |
| 2761 | return false; |
| 2762 | } |
| 2763 | |
| 2764 | { |
| 2765 | char column_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 2766 | char value_column_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 2767 | int value_column_name_size; |
| 2768 | |
| 2769 | value_column_name_size = grn_column_name(ctx, |
| 2770 | cache_value, |
| 2771 | value_column_name, |
| 2772 | GRN_TABLE_MAX_KEY_SIZE); |
| 2773 | grn_snprintf(column_name, |
| 2774 | GRN_TABLE_MAX_KEY_SIZE, |
| 2775 | GRN_TABLE_MAX_KEY_SIZE, |
| 2776 | "%.*s_memcached_flags", |
| 2777 | value_column_name_size, |
| 2778 | value_column_name); |
| 2779 | if (!memcached_setup_flags_column(ctx, column_name)) { |
| 2780 | return false; |
| 2781 | } |
| 2782 | grn_snprintf(column_name, |
| 2783 | GRN_TABLE_MAX_KEY_SIZE, |
| 2784 | GRN_TABLE_MAX_KEY_SIZE, |
| 2785 | "%.*s_memcached_expire", |
| 2786 | value_column_name_size, |
| 2787 | value_column_name); |
| 2788 | if (!memcached_setup_expire_column(ctx, column_name)) { |
| 2789 | return false; |
| 2790 | } |
| 2791 | grn_snprintf(column_name, |
| 2792 | GRN_TABLE_MAX_KEY_SIZE, |
| 2793 | GRN_TABLE_MAX_KEY_SIZE, |
| 2794 | "%.*s_memcached_cas", |
| 2795 | value_column_name_size, |
| 2796 | value_column_name); |
| 2797 | if (!memcached_setup_cas_column(ctx, column_name)) { |
| 2798 | return false; |
| 2799 | } |
| 2800 | } |
| 2801 | } else { |
| 2802 | const char *table_name = "Memcache"; |
| 2803 | const char *value_column_name = "value"; |
| 2804 | |
| 2805 | cache_table = CTX_GET(table_name); |
| 2806 | if (!cache_table) { |
| 2807 | cache_table = grn_table_create(ctx, |
| 2808 | table_name, |
| 2809 | strlen(table_name), |
| 2810 | NULL, |
| 2811 | GRN_OBJ_TABLE_PAT_KEY | GRN_OBJ_PERSISTENT, |
| 2812 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT), |
| 2813 | NULL); |
| 2814 | if (!cache_table) { |
| 2815 | return false; |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | cache_value = grn_obj_column(ctx, |
| 2820 | cache_table, |
| 2821 | value_column_name, |
| 2822 | strlen(value_column_name)); |
| 2823 | if (!cache_value) { |
| 2824 | cache_value = |
| 2825 | grn_column_create(ctx, |
| 2826 | cache_table, |
| 2827 | value_column_name, |
| 2828 | strlen(value_column_name), |
| 2829 | NULL, |
| 2830 | GRN_OBJ_COLUMN_SCALAR | GRN_OBJ_PERSISTENT, |
| 2831 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT)); |
| 2832 | if (!cache_value) { |
| 2833 | return false; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | if (!memcached_setup_flags_column(ctx, "flags")) { |
| 2838 | return false; |
| 2839 | } |
| 2840 | if (!memcached_setup_expire_column(ctx, "expire")) { |
| 2841 | return false; |
| 2842 | } |
| 2843 | if (!memcached_setup_cas_column(ctx, "cas")) { |
| 2844 | return false; |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | return true; |
| 2849 | } |
| 2850 | |
| 2851 | #define RELATIVE_TIME_THRESH 1000000000 |
| 2852 | |
| 2853 | #define MBRES(ctx, re, status, key_len, extra_len, flags) \ |
| 2854 | do { \ |
| 2855 | grn_msg_set_property((ctx), (re), (status), (key_len), (extra_len)); \ |
| 2856 | grn_msg_send((ctx), (re), (flags)); \ |
| 2857 | } while (0) |
| 2858 | |
| 2859 | #define GRN_MSG_MBRES(block) \ |
| 2860 | do { \ |
| 2861 | if (!quiet) { \ |
| 2862 | grn_obj *re = \ |
| 2863 | grn_msg_open_for_reply(ctx, (grn_obj *)msg, &edge->send_old); \ |
| 2864 | ((grn_msg *)re)->header.qtype = header->qtype; \ |
| 2865 | block \ |
| 2866 | } \ |
| 2867 | } while (0) |
| 2868 | |
| 2869 | static uint64_t |
| 2870 | get_mbreq_cas_id() |
| 2871 | { |
| 2872 | static uint64_t cas_id = 0; |
| 2873 | /* FIXME: use GRN_ATOMIC_ADD_EX_64, but it is not implemented */ |
| 2874 | return ++cas_id; |
| 2875 | } |
| 2876 | |
| 2877 | static void |
| 2878 | do_mbreq(grn_ctx *ctx, grn_edge *edge) |
| 2879 | { |
| 2880 | int quiet = 0; |
| 2881 | int flags = 0; |
| 2882 | grn_msg *msg = edge->msg; |
| 2883 | grn_com_header *header = &msg->header; |
| 2884 | |
| 2885 | switch (header->qtype) { |
| 2886 | case MBCMD_GETQ: |
| 2887 | flags = GRN_CTX_MORE; |
| 2888 | /* fallthru */ |
| 2889 | case MBCMD_GET: |
| 2890 | { |
| 2891 | grn_id rid; |
| 2892 | uint16_t keylen = ntohs(header->keylen); |
| 2893 | char *key = GRN_BULK_HEAD((grn_obj *)msg); |
| 2894 | rid = grn_table_get(ctx, cache_table, key, keylen); |
| 2895 | if (!rid) { |
| 2896 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 2897 | } else { |
| 2898 | grn_timeval tv; |
| 2899 | uint32_t expire; |
| 2900 | { |
| 2901 | grn_obj expire_buf; |
| 2902 | GRN_UINT32_INIT(&expire_buf, 0); |
| 2903 | grn_obj_get_value(ctx, cache_expire, rid, &expire_buf); |
| 2904 | expire = GRN_UINT32_VALUE(&expire_buf); |
| 2905 | grn_obj_close(ctx, &expire_buf); |
| 2906 | } |
| 2907 | grn_timeval_now(ctx, &tv); |
| 2908 | if (expire && expire < tv.tv_sec) { |
| 2909 | grn_table_delete_by_id(ctx, cache_table, rid); |
| 2910 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 2911 | } else { |
| 2912 | grn_obj cas_buf; |
| 2913 | GRN_UINT64_INIT(&cas_buf, 0); |
| 2914 | grn_obj_get_value(ctx, cache_cas, rid, &cas_buf); |
| 2915 | GRN_MSG_MBRES({ |
| 2916 | grn_obj_get_value(ctx, cache_flags, rid, re); |
| 2917 | grn_obj_get_value(ctx, cache_value, rid, re); |
| 2918 | ((grn_msg *)re)->header.cas = GRN_UINT64_VALUE(&cas_buf); |
| 2919 | MBRES(ctx, re, MBRES_SUCCESS, 0, 4, flags); |
| 2920 | }); |
| 2921 | grn_obj_close(ctx, &cas_buf); |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | break; |
| 2926 | case MBCMD_SETQ: |
| 2927 | case MBCMD_ADDQ: |
| 2928 | case MBCMD_REPLACEQ: |
| 2929 | quiet = 1; |
| 2930 | /* fallthru */ |
| 2931 | case MBCMD_SET: |
| 2932 | case MBCMD_ADD: |
| 2933 | case MBCMD_REPLACE: |
| 2934 | { |
| 2935 | grn_id rid; |
| 2936 | uint32_t size = ntohl(header->size); |
| 2937 | uint16_t keylen = ntohs(header->keylen); |
| 2938 | uint8_t extralen = header->level; |
| 2939 | char *body = GRN_BULK_HEAD((grn_obj *)msg); |
| 2940 | uint32_t flags = *((uint32_t *)body); |
| 2941 | uint32_t expire = ntohl(*((uint32_t *)(body + 4))); |
| 2942 | uint32_t valuelen = size - keylen - extralen; |
| 2943 | char *key = body + 8; |
| 2944 | char *value = key + keylen; |
| 2945 | int added = 0; |
| 2946 | int f = |
| 2947 | (header->qtype == MBCMD_REPLACE || header->qtype == MBCMD_REPLACEQ) |
| 2948 | ? 0 |
| 2949 | : GRN_TABLE_ADD; |
| 2950 | GRN_ASSERT(extralen == 8); |
| 2951 | if (header->qtype == MBCMD_REPLACE || header->qtype == MBCMD_REPLACEQ) { |
| 2952 | rid = grn_table_get(ctx, cache_table, key, keylen); |
| 2953 | } else { |
| 2954 | rid = grn_table_add(ctx, cache_table, key, keylen, &added); |
| 2955 | } |
| 2956 | if (!rid) { |
| 2957 | GRN_MSG_MBRES({ |
| 2958 | MBRES(ctx, |
| 2959 | re, |
| 2960 | (f & GRN_TABLE_ADD) ? MBRES_ENOMEM : MBRES_NOT_STORED, |
| 2961 | 0, |
| 2962 | 0, |
| 2963 | 0); |
| 2964 | }); |
| 2965 | } else { |
| 2966 | if (added) { |
| 2967 | if (header->cas) { |
| 2968 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_EINVAL, 0, 0, 0); }); |
| 2969 | } else { |
| 2970 | grn_obj text_buf, uint32_buf; |
| 2971 | GRN_TEXT_INIT(&text_buf, GRN_OBJ_DO_SHALLOW_COPY); |
| 2972 | GRN_TEXT_SET_REF(&text_buf, value, valuelen); |
| 2973 | grn_obj_set_value(ctx, cache_value, rid, &text_buf, GRN_OBJ_SET); |
| 2974 | GRN_UINT32_INIT(&uint32_buf, 0); |
| 2975 | GRN_UINT32_SET(ctx, &uint32_buf, flags); |
| 2976 | grn_obj_set_value(ctx, cache_flags, rid, &uint32_buf, GRN_OBJ_SET); |
| 2977 | if (expire && expire < RELATIVE_TIME_THRESH) { |
| 2978 | grn_timeval tv; |
| 2979 | grn_timeval_now(ctx, &tv); |
| 2980 | expire += tv.tv_sec; |
| 2981 | } |
| 2982 | GRN_UINT32_SET(ctx, &uint32_buf, expire); |
| 2983 | grn_obj_set_value(ctx, cache_expire, rid, &uint32_buf, GRN_OBJ_SET); |
| 2984 | grn_obj_close(ctx, &uint32_buf); |
| 2985 | { |
| 2986 | grn_obj cas_buf; |
| 2987 | uint64_t cas_id = get_mbreq_cas_id(); |
| 2988 | GRN_UINT64_INIT(&cas_buf, 0); |
| 2989 | GRN_UINT64_SET(ctx, &cas_buf, cas_id); |
| 2990 | grn_obj_set_value(ctx, cache_cas, rid, &cas_buf, GRN_OBJ_SET); |
| 2991 | grn_obj_close(ctx, &cas_buf); |
| 2992 | GRN_MSG_MBRES({ |
| 2993 | ((grn_msg *)re)->header.cas = cas_id; |
| 2994 | MBRES(ctx, re, MBRES_SUCCESS, 0, 0, 0); |
| 2995 | }); |
| 2996 | } |
| 2997 | } |
| 2998 | } else { |
| 2999 | if (header->qtype != MBCMD_SET && header->qtype != MBCMD_SETQ) { |
| 3000 | grn_obj uint32_buf; |
| 3001 | grn_timeval tv; |
| 3002 | uint32_t oexpire; |
| 3003 | |
| 3004 | GRN_UINT32_INIT(&uint32_buf, 0); |
| 3005 | grn_obj_get_value(ctx, cache_expire, rid, &uint32_buf); |
| 3006 | oexpire = GRN_UINT32_VALUE(&uint32_buf); |
| 3007 | grn_timeval_now(ctx, &tv); |
| 3008 | |
| 3009 | if (oexpire && oexpire < tv.tv_sec) { |
| 3010 | if (header->qtype == MBCMD_REPLACE || |
| 3011 | header->qtype == MBCMD_REPLACEQ) { |
| 3012 | grn_table_delete_by_id(ctx, cache_table, rid); |
| 3013 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_NOT_STORED, 0, 0, 0); }); |
| 3014 | break; |
| 3015 | } |
| 3016 | } else if (header->qtype == MBCMD_ADD || |
| 3017 | header->qtype == MBCMD_ADDQ) { |
| 3018 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_NOT_STORED, 0, 0, 0); }); |
| 3019 | break; |
| 3020 | } |
| 3021 | } |
| 3022 | { |
| 3023 | if (header->cas) { |
| 3024 | grn_obj cas_buf; |
| 3025 | GRN_UINT64_INIT(&cas_buf, 0); |
| 3026 | grn_obj_get_value(ctx, cache_cas, rid, &cas_buf); |
| 3027 | if (header->cas != GRN_UINT64_VALUE(&cas_buf)) { |
| 3028 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_NOT_STORED, 0, 0, 0); }); |
| 3029 | } |
| 3030 | } |
| 3031 | { |
| 3032 | grn_obj text_buf, uint32_buf; |
| 3033 | GRN_TEXT_INIT(&text_buf, GRN_OBJ_DO_SHALLOW_COPY); |
| 3034 | GRN_TEXT_SET_REF(&text_buf, value, valuelen); |
| 3035 | grn_obj_set_value(ctx, cache_value, rid, &text_buf, GRN_OBJ_SET); |
| 3036 | GRN_UINT32_INIT(&uint32_buf, 0); |
| 3037 | GRN_UINT32_SET(ctx, &uint32_buf, flags); |
| 3038 | grn_obj_set_value(ctx, |
| 3039 | cache_flags, |
| 3040 | rid, |
| 3041 | &uint32_buf, |
| 3042 | GRN_OBJ_SET); |
| 3043 | if (expire && expire < RELATIVE_TIME_THRESH) { |
| 3044 | grn_timeval tv; |
| 3045 | grn_timeval_now(ctx, &tv); |
| 3046 | expire += tv.tv_sec; |
| 3047 | } |
| 3048 | GRN_UINT32_SET(ctx, &uint32_buf, expire); |
| 3049 | grn_obj_set_value(ctx, |
| 3050 | cache_expire, |
| 3051 | rid, |
| 3052 | &uint32_buf, |
| 3053 | GRN_OBJ_SET); |
| 3054 | { |
| 3055 | grn_obj cas_buf; |
| 3056 | uint64_t cas_id = get_mbreq_cas_id(); |
| 3057 | GRN_UINT64_INIT(&cas_buf, 0); |
| 3058 | GRN_UINT64_SET(ctx, &cas_buf, cas_id); |
| 3059 | grn_obj_set_value(ctx, cache_cas, rid, &cas_buf, GRN_OBJ_SET); |
| 3060 | GRN_MSG_MBRES({ |
| 3061 | ((grn_msg *)re)->header.cas = cas_id; |
| 3062 | MBRES(ctx, re, MBRES_SUCCESS, 0, 0, 0); |
| 3063 | }); |
| 3064 | } |
| 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | } |
| 3069 | } |
| 3070 | break; |
| 3071 | case MBCMD_DELETEQ: |
| 3072 | quiet = 1; |
| 3073 | /* fallthru */ |
| 3074 | case MBCMD_DELETE: |
| 3075 | { |
| 3076 | grn_id rid; |
| 3077 | uint16_t keylen = ntohs(header->keylen); |
| 3078 | char *key = GRN_BULK_HEAD((grn_obj *)msg); |
| 3079 | rid = grn_table_get(ctx, cache_table, key, keylen); |
| 3080 | if (!rid) { |
| 3081 | /* GRN_LOG(ctx, GRN_LOG_NOTICE, "GET k=%d not found", keylen); */ |
| 3082 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 3083 | } else { |
| 3084 | grn_table_delete_by_id(ctx, cache_table, rid); |
| 3085 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_SUCCESS, 0, 4, 0); }); |
| 3086 | } |
| 3087 | } |
| 3088 | break; |
| 3089 | case MBCMD_INCREMENTQ: |
| 3090 | case MBCMD_DECREMENTQ: |
| 3091 | quiet = 1; |
| 3092 | /* fallthru */ |
| 3093 | case MBCMD_INCREMENT: |
| 3094 | case MBCMD_DECREMENT: |
| 3095 | { |
| 3096 | grn_id rid; |
| 3097 | int added = 0; |
| 3098 | uint64_t delta, init; |
| 3099 | uint16_t keylen = ntohs(header->keylen); |
| 3100 | char *body = GRN_BULK_HEAD((grn_obj *)msg); |
| 3101 | char *key = body + 20; |
| 3102 | uint32_t expire = ntohl(*((uint32_t *)(body + 16))); |
| 3103 | grn_ntoh(&delta, body, 8); |
| 3104 | grn_ntoh(&init, body + 8, 8); |
| 3105 | GRN_ASSERT(header->level == 20); /* extralen */ |
| 3106 | if (expire == 0xffffffff) { |
| 3107 | rid = grn_table_get(ctx, cache_table, key, keylen); |
| 3108 | } else { |
| 3109 | rid = grn_table_add(ctx, cache_table, key, keylen, &added); |
| 3110 | } |
| 3111 | if (!rid) { |
| 3112 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 3113 | } else { |
| 3114 | grn_obj uint32_buf, text_buf; |
| 3115 | GRN_UINT32_INIT(&uint32_buf, 0); |
| 3116 | GRN_TEXT_INIT(&text_buf, GRN_OBJ_DO_SHALLOW_COPY); |
| 3117 | if (added) { |
| 3118 | GRN_TEXT_SET_REF(&text_buf, &init, 8); |
| 3119 | grn_obj_set_value(ctx, cache_value, rid, &text_buf, GRN_OBJ_SET); |
| 3120 | GRN_UINT32_SET(ctx, &uint32_buf, 0); |
| 3121 | grn_obj_set_value(ctx, cache_flags, rid, &uint32_buf, GRN_OBJ_SET); |
| 3122 | } else { |
| 3123 | grn_timeval tv; |
| 3124 | uint32_t oexpire; |
| 3125 | |
| 3126 | grn_obj_get_value(ctx, cache_expire, rid, &uint32_buf); |
| 3127 | oexpire = GRN_UINT32_VALUE(&uint32_buf); |
| 3128 | grn_timeval_now(ctx, &tv); |
| 3129 | |
| 3130 | if (oexpire && oexpire < tv.tv_sec) { |
| 3131 | if (expire == 0xffffffffU) { |
| 3132 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 3133 | break; |
| 3134 | } else { |
| 3135 | GRN_TEXT_SET_REF(&text_buf, &init, 8); |
| 3136 | grn_obj_set_value(ctx, cache_value, rid, &text_buf, GRN_OBJ_SET); |
| 3137 | GRN_UINT32_SET(ctx, &uint32_buf, 0); |
| 3138 | grn_obj_set_value(ctx, |
| 3139 | cache_flags, |
| 3140 | rid, |
| 3141 | &uint32_buf, |
| 3142 | GRN_OBJ_SET); |
| 3143 | } |
| 3144 | } else { |
| 3145 | grn_obj uint64_buf; |
| 3146 | GRN_UINT64_INIT(&uint64_buf, 0); |
| 3147 | GRN_UINT64_SET(ctx, &uint64_buf, delta); |
| 3148 | grn_obj_set_value(ctx, |
| 3149 | cache_value, |
| 3150 | rid, |
| 3151 | &uint64_buf, |
| 3152 | header->qtype == MBCMD_INCREMENT || |
| 3153 | header->qtype == MBCMD_INCREMENTQ |
| 3154 | ? GRN_OBJ_INCR |
| 3155 | : GRN_OBJ_DECR); |
| 3156 | } |
| 3157 | } |
| 3158 | if (expire && expire < RELATIVE_TIME_THRESH) { |
| 3159 | grn_timeval tv; |
| 3160 | grn_timeval_now(ctx, &tv); |
| 3161 | expire += tv.tv_sec; |
| 3162 | } |
| 3163 | GRN_UINT32_SET(ctx, &uint32_buf, expire); |
| 3164 | grn_obj_set_value(ctx, cache_expire, rid, &uint32_buf, GRN_OBJ_SET); |
| 3165 | GRN_MSG_MBRES({ |
| 3166 | /* TODO: get_mbreq_cas_id() */ |
| 3167 | grn_obj_get_value(ctx, cache_value, rid, re); |
| 3168 | grn_hton(&delta, (uint64_t *)GRN_BULK_HEAD(re), 8); |
| 3169 | GRN_TEXT_SET(ctx, re, &delta, sizeof(uint64_t)); |
| 3170 | MBRES(ctx, re, MBRES_SUCCESS, 0, sizeof(uint64_t), 0); |
| 3171 | }); |
| 3172 | } |
| 3173 | } |
| 3174 | break; |
| 3175 | case MBCMD_FLUSHQ: |
| 3176 | quiet = 1; |
| 3177 | /* fallthru */ |
| 3178 | case MBCMD_FLUSH: |
| 3179 | { |
| 3180 | uint32_t expire; |
| 3181 | uint8_t extralen = header->level; |
| 3182 | if (extralen) { |
| 3183 | char *body = GRN_BULK_HEAD((grn_obj *)msg); |
| 3184 | GRN_ASSERT(extralen == 4); |
| 3185 | expire = ntohl(*((uint32_t *)(body))); |
| 3186 | if (expire < RELATIVE_TIME_THRESH) { |
| 3187 | grn_timeval tv; |
| 3188 | grn_timeval_now(ctx, &tv); |
| 3189 | if (expire) { |
| 3190 | expire += tv.tv_sec; |
| 3191 | } else { |
| 3192 | expire = tv.tv_sec - 1; |
| 3193 | } |
| 3194 | } |
| 3195 | } else { |
| 3196 | grn_timeval tv; |
| 3197 | grn_timeval_now(ctx, &tv); |
| 3198 | expire = tv.tv_sec - 1; |
| 3199 | } |
| 3200 | { |
| 3201 | grn_obj exp_buf; |
| 3202 | GRN_UINT32_INIT(&exp_buf, 0); |
| 3203 | GRN_UINT32_SET(ctx, &exp_buf, expire); |
| 3204 | GRN_TABLE_EACH(ctx, cache_table, 0, 0, rid, NULL, NULL, NULL, { |
| 3205 | grn_obj_set_value(ctx, cache_expire, rid, &exp_buf, GRN_OBJ_SET); |
| 3206 | }); |
| 3207 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_SUCCESS, 0, 4, 0); }); |
| 3208 | grn_obj_close(ctx, &exp_buf); |
| 3209 | } |
| 3210 | } |
| 3211 | break; |
| 3212 | case MBCMD_NOOP: |
| 3213 | break; |
| 3214 | case MBCMD_VERSION: |
| 3215 | GRN_MSG_MBRES({ |
| 3216 | grn_bulk_write(ctx, re, PACKAGE_VERSION, strlen(PACKAGE_VERSION)); |
| 3217 | MBRES(ctx, re, MBRES_SUCCESS, 0, 0, 0); |
| 3218 | }); |
| 3219 | break; |
| 3220 | case MBCMD_GETKQ: |
| 3221 | flags = GRN_CTX_MORE; |
| 3222 | /* fallthru */ |
| 3223 | case MBCMD_GETK: |
| 3224 | { |
| 3225 | grn_id rid; |
| 3226 | uint16_t keylen = ntohs(header->keylen); |
| 3227 | char *key = GRN_BULK_HEAD((grn_obj *)msg); |
| 3228 | rid = grn_table_get(ctx, cache_table, key, keylen); |
| 3229 | if (!rid) { |
| 3230 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 3231 | } else { |
| 3232 | grn_obj uint32_buf; |
| 3233 | grn_timeval tv; |
| 3234 | uint32_t expire; |
| 3235 | GRN_UINT32_INIT(&uint32_buf, 0); |
| 3236 | grn_obj_get_value(ctx, cache_expire, rid, &uint32_buf); |
| 3237 | expire = GRN_UINT32_VALUE(&uint32_buf); |
| 3238 | grn_timeval_now(ctx, &tv); |
| 3239 | if (expire && expire < tv.tv_sec) { |
| 3240 | grn_table_delete_by_id(ctx, cache_table, rid); |
| 3241 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_KEY_ENOENT, 0, 0, 0); }); |
| 3242 | } else { |
| 3243 | grn_obj uint64_buf; |
| 3244 | GRN_UINT64_INIT(&uint64_buf, 0); |
| 3245 | grn_obj_get_value(ctx, cache_cas, rid, &uint64_buf); |
| 3246 | GRN_MSG_MBRES({ |
| 3247 | grn_obj_get_value(ctx, cache_flags, rid, re); |
| 3248 | grn_bulk_write(ctx, re, key, keylen); |
| 3249 | grn_obj_get_value(ctx, cache_value, rid, re); |
| 3250 | ((grn_msg *)re)->header.cas = GRN_UINT64_VALUE(&uint64_buf); |
| 3251 | MBRES(ctx, re, MBRES_SUCCESS, keylen, 4, flags); |
| 3252 | }); |
| 3253 | } |
| 3254 | } |
| 3255 | } |
| 3256 | break; |
| 3257 | case MBCMD_APPENDQ: |
| 3258 | case MBCMD_PREPENDQ: |
| 3259 | quiet = 1; |
| 3260 | /* fallthru */ |
| 3261 | case MBCMD_APPEND: |
| 3262 | case MBCMD_PREPEND: |
| 3263 | { |
| 3264 | grn_id rid; |
| 3265 | uint32_t size = ntohl(header->size); |
| 3266 | uint16_t keylen = ntohs(header->keylen); |
| 3267 | char *key = GRN_BULK_HEAD((grn_obj *)msg); |
| 3268 | char *value = key + keylen; |
| 3269 | uint32_t valuelen = size - keylen; |
| 3270 | rid = grn_table_add(ctx, cache_table, key, keylen, NULL); |
| 3271 | if (!rid) { |
| 3272 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_ENOMEM, 0, 0, 0); }); |
| 3273 | } else { |
| 3274 | /* FIXME: check expire */ |
| 3275 | grn_obj buf; |
| 3276 | int flags = |
| 3277 | header->qtype == MBCMD_APPEND ? GRN_OBJ_APPEND : GRN_OBJ_PREPEND; |
| 3278 | GRN_TEXT_INIT(&buf, GRN_OBJ_DO_SHALLOW_COPY); |
| 3279 | GRN_TEXT_SET_REF(&buf, value, valuelen); |
| 3280 | grn_obj_set_value(ctx, cache_value, rid, &buf, flags); |
| 3281 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_SUCCESS, 0, 0, 0); }); |
| 3282 | } |
| 3283 | } |
| 3284 | break; |
| 3285 | case MBCMD_STAT: |
| 3286 | { |
| 3287 | pid_t pid = grn_getpid(); |
| 3288 | GRN_MSG_MBRES({ |
| 3289 | grn_bulk_write(ctx, re, "pid", 3); |
| 3290 | grn_text_itoa(ctx, re, pid); |
| 3291 | MBRES(ctx, re, MBRES_SUCCESS, 3, 0, 0); |
| 3292 | }); |
| 3293 | } |
| 3294 | break; |
| 3295 | case MBCMD_QUITQ: |
| 3296 | quiet = 1; |
| 3297 | /* fallthru */ |
| 3298 | case MBCMD_QUIT: |
| 3299 | GRN_MSG_MBRES({ MBRES(ctx, re, MBRES_SUCCESS, 0, 0, 0); }); |
| 3300 | /* fallthru */ |
| 3301 | default: |
| 3302 | ctx->stat = GRN_CTX_QUIT; |
| 3303 | break; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | /* worker thread */ |
| 3308 | |
| 3309 | enum { |
| 3310 | EDGE_IDLE = 0x00, |
| 3311 | EDGE_WAIT = 0x01, |
| 3312 | EDGE_DOING = 0x02, |
| 3313 | EDGE_ABORT = 0x03, |
| 3314 | }; |
| 3315 | |
| 3316 | static void |
| 3317 | check_rlimit_nofile(grn_ctx *ctx) |
| 3318 | { |
| 3319 | #ifndef WIN32 |
| 3320 | struct rlimit limit; |
| 3321 | limit.rlim_cur = 0; |
| 3322 | limit.rlim_max = 0; |
| 3323 | getrlimit(RLIMIT_NOFILE, &limit); |
| 3324 | if (limit.rlim_cur < RLIMIT_NOFILE_MINIMUM) { |
| 3325 | limit.rlim_cur = RLIMIT_NOFILE_MINIMUM; |
| 3326 | limit.rlim_max = RLIMIT_NOFILE_MINIMUM; |
| 3327 | setrlimit(RLIMIT_NOFILE, &limit); |
| 3328 | limit.rlim_cur = 0; |
| 3329 | limit.rlim_max = 0; |
| 3330 | getrlimit(RLIMIT_NOFILE, &limit); |
| 3331 | } |
| 3332 | GRN_LOG(ctx, |
| 3333 | GRN_LOG_NOTICE, |
| 3334 | "RLIMIT_NOFILE(%" GRN_FMT_LLD ",%" GRN_FMT_LLD ")", |
| 3335 | (long long int)limit.rlim_cur, |
| 3336 | (long long int)limit.rlim_max); |
| 3337 | #endif /* WIN32 */ |
| 3338 | } |
| 3339 | |
| 3340 | static grn_thread_func_result CALLBACK |
| 3341 | h_worker(void *arg) |
| 3342 | { |
| 3343 | ht_context hc; |
| 3344 | grn_ctx ctx_, *ctx = &ctx_; |
| 3345 | grn_ctx_init(ctx, 0); |
| 3346 | grn_ctx_set_wal_role(ctx, worker_wal_role); |
| 3347 | grn_ctx_use(ctx, (grn_obj *)arg); |
| 3348 | grn_ctx_recv_handler_set(ctx, h_output, &hc); |
| 3349 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3350 | GRN_LOG(ctx, |
| 3351 | GRN_LOG_NOTICE, |
| 3352 | "thread start (%u/%u)", |
| 3353 | n_floating_threads, |
| 3354 | n_running_threads); |
| 3355 | while (n_running_threads <= max_n_floating_threads && |
| 3356 | grn_gctx.stat != GRN_CTX_QUIT) { |
| 3357 | grn_obj *msg; |
| 3358 | if (ctx->rc == GRN_CANCEL) { |
| 3359 | ctx->rc = GRN_SUCCESS; |
| 3360 | } |
| 3361 | n_floating_threads++; |
| 3362 | while (!(msg = (grn_obj *)grn_com_queue_deque(&grn_gctx, &ctx_new))) { |
| 3363 | COND_WAIT(q_cond, q_critical_section); |
| 3364 | if (grn_gctx.stat == GRN_CTX_QUIT) { |
| 3365 | n_floating_threads--; |
| 3366 | goto exit; |
| 3367 | } |
| 3368 | if (n_running_threads > max_n_floating_threads) { |
| 3369 | n_floating_threads--; |
| 3370 | goto exit; |
| 3371 | } |
| 3372 | } |
| 3373 | n_floating_threads--; |
| 3374 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3375 | hc.msg = (grn_msg *)msg; |
| 3376 | hc.in_body = false; |
| 3377 | hc.is_chunked = false; |
| 3378 | do_htreq(ctx, &hc); |
| 3379 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3380 | } |
| 3381 | exit: |
| 3382 | n_running_threads--; |
| 3383 | GRN_LOG(ctx, |
| 3384 | GRN_LOG_NOTICE, |
| 3385 | "thread end (%u/%u)", |
| 3386 | n_floating_threads, |
| 3387 | n_running_threads); |
| 3388 | if (grn_gctx.stat == GRN_CTX_QUIT && running_event_loop) { |
| 3389 | break_accept_event_loop(ctx); |
| 3390 | } |
| 3391 | grn_ctx_fin(ctx); |
| 3392 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3393 | return GRN_THREAD_FUNC_RETURN_VALUE; |
| 3394 | } |
| 3395 | |
| 3396 | static void |
| 3397 | h_handler(grn_ctx *ctx, grn_obj *msg) |
| 3398 | { |
| 3399 | grn_com *com = ((grn_msg *)msg)->u.peer; |
| 3400 | if (ctx->rc) { |
| 3401 | grn_com_close(ctx, com); |
| 3402 | grn_msg_close(ctx, msg); |
| 3403 | } else { |
| 3404 | grn_sock fd = com->fd; |
| 3405 | void *arg = com->ev->opaque; |
| 3406 | /* if not keep alive connection */ |
| 3407 | grn_com_event_del(ctx, com->ev, fd); |
| 3408 | ((grn_msg *)msg)->u.fd = fd; |
| 3409 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3410 | const char *immediate_path = NULL; |
| 3411 | int immediate_path_length = -1; |
| 3412 | if (n_floating_threads == 0 && |
| 3413 | n_running_threads == max_n_floating_threads) { |
| 3414 | h_header header; |
| 3415 | header.method_start = NULL; |
| 3416 | header.method_length = -1; |
| 3417 | header.path_start = NULL; |
| 3418 | header.path_length = -1; |
| 3419 | if (h_parse_header_request_line(ctx, |
| 3420 | GRN_BULK_HEAD((grn_obj *)msg), |
| 3421 | GRN_BULK_CURR((grn_obj *)msg), |
| 3422 | &header) && |
| 3423 | STRING_EQUAL(header.path_start, |
| 3424 | header.path_length, |
| 3425 | "/d/shutdown?mode=immediate")) { |
| 3426 | immediate_path = header.path_start; |
| 3427 | immediate_path_length = header.path_length; |
| 3428 | } |
| 3429 | } |
| 3430 | if (immediate_path) { |
| 3431 | ht_context hc; |
| 3432 | grn_ctx ctx_shutdown; |
| 3433 | grn_ctx_init(&ctx_shutdown, 0); |
| 3434 | grn_ctx_use(&ctx_shutdown, (grn_obj *)arg); |
| 3435 | grn_ctx_recv_handler_set(&ctx_shutdown, h_output, &hc); |
| 3436 | hc.msg = (grn_msg *)msg; |
| 3437 | hc.in_body = false; |
| 3438 | hc.is_chunked = false; |
| 3439 | grn_ctx_send(&ctx_shutdown, |
| 3440 | immediate_path, |
| 3441 | immediate_path_length, |
| 3442 | GRN_CTX_TAIL); |
| 3443 | grn_ctx_fin(&ctx_shutdown); |
| 3444 | } else { |
| 3445 | grn_com_queue_enque(ctx, &ctx_new, (grn_com_queue_entry *)msg); |
| 3446 | if (n_floating_threads == 0 && |
| 3447 | n_running_threads < max_n_floating_threads) { |
| 3448 | grn_thread thread; |
| 3449 | n_running_threads++; |
| 3450 | if (THREAD_CREATE(thread, h_worker, arg)) { |
| 3451 | n_running_threads--; |
| 3452 | SERR("pthread_create"); |
| 3453 | } |
| 3454 | } |
| 3455 | COND_SIGNAL(q_cond); |
| 3456 | } |
| 3457 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | static int |
| 3462 | h_server(char *path) |
| 3463 | { |
| 3464 | int exit_code = EXIT_FAILURE; |
| 3465 | grn_ctx ctx_, *ctx = &ctx_; |
| 3466 | grn_ctx_init(ctx, 0); |
| 3467 | grn_ctx_set_wal_role(ctx, wal_role); |
| 3468 | GRN_COM_QUEUE_INIT(&ctx_new); |
| 3469 | GRN_COM_QUEUE_INIT(&ctx_old); |
| 3470 | check_rlimit_nofile(ctx); |
| 3471 | GRN_TEXT_INIT(&http_response_server_line, 0); |
| 3472 | grn_text_printf(ctx, |
| 3473 | &http_response_server_line, |
| 3474 | "Server: %s/%s\r\n", |
| 3475 | grn_get_package_label(), |
| 3476 | grn_get_version()); |
| 3477 | exit_code = start_service(ctx, path, NULL, h_handler); |
| 3478 | GRN_OBJ_FIN(ctx, &http_response_server_line); |
| 3479 | grn_ctx_fin(ctx); |
| 3480 | return exit_code; |
| 3481 | } |
| 3482 | |
| 3483 | static grn_thread_func_result CALLBACK |
| 3484 | g_worker(void *arg) |
| 3485 | { |
| 3486 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3487 | GRN_LOG(&grn_gctx, |
| 3488 | GRN_LOG_NOTICE, |
| 3489 | "thread start (%u/%u)", |
| 3490 | n_floating_threads, |
| 3491 | n_running_threads); |
| 3492 | while (n_running_threads <= max_n_floating_threads && |
| 3493 | grn_gctx.stat != GRN_CTX_QUIT) { |
| 3494 | grn_ctx *ctx; |
| 3495 | grn_edge *edge; |
| 3496 | n_floating_threads++; |
| 3497 | while (!(edge = (grn_edge *)grn_com_queue_deque(&grn_gctx, &ctx_new))) { |
| 3498 | COND_WAIT(q_cond, q_critical_section); |
| 3499 | if (grn_gctx.stat == GRN_CTX_QUIT) { |
| 3500 | n_floating_threads--; |
| 3501 | goto exit; |
| 3502 | } |
| 3503 | if (n_running_threads > max_n_floating_threads) { |
| 3504 | n_floating_threads--; |
| 3505 | goto exit; |
| 3506 | } |
| 3507 | } |
| 3508 | ctx = &edge->ctx; |
| 3509 | n_floating_threads--; |
| 3510 | if (edge->stat == EDGE_DOING) { |
| 3511 | continue; |
| 3512 | } |
| 3513 | if (edge->stat == EDGE_WAIT) { |
| 3514 | edge->stat = EDGE_DOING; |
| 3515 | while (!GRN_COM_QUEUE_EMPTYP(&edge->recv_new)) { |
| 3516 | grn_obj *msg; |
| 3517 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3518 | /* if (edge->flags == GRN_EDGE_WORKER) */ |
| 3519 | while ( |
| 3520 | ctx->stat != GRN_CTX_QUIT && |
| 3521 | (edge->msg = (grn_msg *)grn_com_queue_deque(ctx, &edge->recv_new))) { |
| 3522 | grn_com_header *header = &edge->msg->header; |
| 3523 | msg = (grn_obj *)edge->msg; |
| 3524 | switch (header->proto) { |
| 3525 | case GRN_COM_PROTO_MBREQ: |
| 3526 | do_mbreq(ctx, edge); |
| 3527 | break; |
| 3528 | case GRN_COM_PROTO_GQTP: |
| 3529 | grn_ctx_send(ctx, |
| 3530 | GRN_BULK_HEAD(msg), |
| 3531 | GRN_BULK_VSIZE(msg), |
| 3532 | header->flags); |
| 3533 | ERRCLR(ctx); |
| 3534 | if (ctx->rc == GRN_CANCEL) { |
| 3535 | ctx->rc = GRN_SUCCESS; |
| 3536 | } |
| 3537 | break; |
| 3538 | default: |
| 3539 | ctx->stat = GRN_CTX_QUIT; |
| 3540 | break; |
| 3541 | } |
| 3542 | grn_msg_close(ctx, msg); |
| 3543 | } |
| 3544 | while ((msg = (grn_obj *)grn_com_queue_deque(ctx, &edge->send_old))) { |
| 3545 | grn_msg_close(ctx, msg); |
| 3546 | } |
| 3547 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3548 | if (ctx->stat == GRN_CTX_QUIT || edge->stat == EDGE_ABORT) { |
| 3549 | break; |
| 3550 | } |
| 3551 | } |
| 3552 | } |
| 3553 | if (ctx->stat == GRN_CTX_QUIT || edge->stat == EDGE_ABORT) { |
| 3554 | grn_com_queue_enque(&grn_gctx, &ctx_old, (grn_com_queue_entry *)edge); |
| 3555 | edge->stat = EDGE_ABORT; |
| 3556 | } else { |
| 3557 | edge->stat = EDGE_IDLE; |
| 3558 | } |
| 3559 | }; |
| 3560 | exit: |
| 3561 | n_running_threads--; |
| 3562 | GRN_LOG(&grn_gctx, |
| 3563 | GRN_LOG_NOTICE, |
| 3564 | "thread end (%u/%u)", |
| 3565 | n_floating_threads, |
| 3566 | n_running_threads); |
| 3567 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3568 | return GRN_THREAD_FUNC_RETURN_VALUE; |
| 3569 | } |
| 3570 | |
| 3571 | static void |
| 3572 | g_dispatcher(grn_ctx *ctx, grn_edge *edge) |
| 3573 | { |
| 3574 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3575 | if (edge->stat == EDGE_IDLE) { |
| 3576 | grn_com_queue_enque(ctx, &ctx_new, (grn_com_queue_entry *)edge); |
| 3577 | edge->stat = EDGE_WAIT; |
| 3578 | if (n_floating_threads == 0 && n_running_threads < max_n_floating_threads) { |
| 3579 | grn_thread thread; |
| 3580 | n_running_threads++; |
| 3581 | if (THREAD_CREATE(thread, g_worker, NULL)) { |
| 3582 | n_running_threads--; |
| 3583 | SERR("pthread_create"); |
| 3584 | } |
| 3585 | } |
| 3586 | COND_SIGNAL(q_cond); |
| 3587 | } |
| 3588 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3589 | } |
| 3590 | |
| 3591 | static void |
| 3592 | g_output(grn_ctx *ctx, int flags, void *arg) |
| 3593 | { |
| 3594 | grn_edge *edge = arg; |
| 3595 | grn_com *com = edge->com; |
| 3596 | grn_msg *req = edge->msg, *msg = (grn_msg *)ctx->impl->output.buf; |
| 3597 | msg->edge_id = req->edge_id; |
| 3598 | msg->header.proto = req->header.proto == GRN_COM_PROTO_MBREQ |
| 3599 | ? GRN_COM_PROTO_MBRES |
| 3600 | : req->header.proto; |
| 3601 | if (ctx->rc != GRN_SUCCESS && GRN_BULK_VSIZE(ctx->impl->output.buf) == 0) { |
| 3602 | GRN_TEXT_PUTS(ctx, ctx->impl->output.buf, ctx->errbuf); |
| 3603 | } |
| 3604 | if (grn_msg_send(ctx, |
| 3605 | (grn_obj *)msg, |
| 3606 | (flags & GRN_CTX_MORE) ? GRN_CTX_MORE : GRN_CTX_TAIL)) { |
| 3607 | edge->stat = EDGE_ABORT; |
| 3608 | } |
| 3609 | ctx->impl->output.buf = grn_msg_open(ctx, com, &edge->send_old); |
| 3610 | } |
| 3611 | |
| 3612 | static void |
| 3613 | g_handler(grn_ctx *ctx, grn_obj *msg) |
| 3614 | { |
| 3615 | grn_edge *edge; |
| 3616 | grn_com *com = ((grn_msg *)msg)->u.peer; |
| 3617 | if (ctx->rc) { |
| 3618 | if (com->has_sid) { |
| 3619 | if ((edge = com->opaque)) { |
| 3620 | CRITICAL_SECTION_ENTER(q_critical_section); |
| 3621 | if (edge->stat == EDGE_IDLE) { |
| 3622 | grn_com_queue_enque(ctx, &ctx_old, (grn_com_queue_entry *)edge); |
| 3623 | } |
| 3624 | edge->stat = EDGE_ABORT; |
| 3625 | CRITICAL_SECTION_LEAVE(q_critical_section); |
| 3626 | } else { |
| 3627 | grn_com_close(ctx, com); |
| 3628 | } |
| 3629 | } |
| 3630 | grn_msg_close(ctx, msg); |
| 3631 | } else { |
| 3632 | int added; |
| 3633 | edge = grn_edges_add(ctx, &((grn_msg *)msg)->edge_id, &added); |
| 3634 | if (added) { |
| 3635 | grn_ctx_init(&edge->ctx, 0); |
| 3636 | grn_ctx_set_wal_role(ctx, worker_wal_role); |
| 3637 | GRN_COM_QUEUE_INIT(&edge->recv_new); |
| 3638 | GRN_COM_QUEUE_INIT(&edge->send_old); |
| 3639 | grn_ctx_use(&edge->ctx, (grn_obj *)com->ev->opaque); |
| 3640 | grn_ctx_recv_handler_set(&edge->ctx, g_output, edge); |
| 3641 | com->opaque = edge; |
| 3642 | grn_obj_close(&edge->ctx, edge->ctx.impl->output.buf); |
| 3643 | edge->ctx.impl->output.buf = |
| 3644 | grn_msg_open(&edge->ctx, com, &edge->send_old); |
| 3645 | edge->com = com; |
| 3646 | edge->stat = EDGE_IDLE; |
| 3647 | edge->flags = GRN_EDGE_WORKER; |
| 3648 | } |
| 3649 | if (edge->ctx.stat == GRN_CTX_QUIT || edge->stat == EDGE_ABORT) { |
| 3650 | grn_msg_close(ctx, msg); |
| 3651 | } else { |
| 3652 | grn_com_queue_enque(ctx, &edge->recv_new, (grn_com_queue_entry *)msg); |
| 3653 | g_dispatcher(ctx, edge); |
| 3654 | } |
| 3655 | } |
| 3656 | } |
| 3657 | |
| 3658 | static int |
| 3659 | g_server(char *path) |
| 3660 | { |
| 3661 | int exit_code = EXIT_FAILURE; |
| 3662 | grn_ctx ctx_, *ctx = &ctx_; |
| 3663 | grn_ctx_init(ctx, 0); |
| 3664 | grn_ctx_set_wal_role(ctx, wal_role); |
| 3665 | GRN_COM_QUEUE_INIT(&ctx_new); |
| 3666 | GRN_COM_QUEUE_INIT(&ctx_old); |
| 3667 | check_rlimit_nofile(ctx); |
| 3668 | exit_code = start_service(ctx, path, g_dispatcher, g_handler); |
| 3669 | grn_ctx_fin(ctx); |
| 3670 | return exit_code; |
| 3671 | } |
| 3672 | |
| 3673 | enum { |
| 3674 | ACTION_USAGE = 1, |
| 3675 | ACTION_VERSION, |
| 3676 | ACTION_SHOW_CONFIG, |
| 3677 | ACTION_ERROR |
| 3678 | }; |
| 3679 | |
| 3680 | #define ACTION_MASK (0x0f) |
| 3681 | #define MODE_MASK (0xf0) |
| 3682 | #define FLAG_MODE_ALONE (1 << 4) |
| 3683 | #define FLAG_MODE_CLIENT (1 << 5) |
| 3684 | #define FLAG_MODE_DAEMON (1 << 6) |
| 3685 | #define FLAG_MODE_SERVER (1 << 7) |
| 3686 | #define FLAG_NEW_DB (1 << 8) |
| 3687 | #define FLAG_USE_WINDOWS_EVENT_LOG (1 << 9) |
| 3688 | |
| 3689 | static uint32_t |
| 3690 | get_core_number(void) |
| 3691 | { |
| 3692 | #ifdef WIN32 |
| 3693 | SYSTEM_INFO sinfo; |
| 3694 | GetSystemInfo(&sinfo); |
| 3695 | return sinfo.dwNumberOfProcessors; |
| 3696 | #else |
| 3697 | # ifdef _SC_NPROCESSORS_CONF |
| 3698 | return sysconf(_SC_NPROCESSORS_CONF); |
| 3699 | # else |
| 3700 | return 1; |
| 3701 | # endif |
| 3702 | #endif |
| 3703 | } |
| 3704 | |
| 3705 | /* |
| 3706 | * The length of each line, including an end-of-line, in config file should be |
| 3707 | * shorter than (CONFIG_FILE_BUF_SIZE - 1) bytes. Too long lines are ignored. |
| 3708 | * Note that both '\r' and '\n' are handled as end-of-lines. |
| 3709 | * |
| 3710 | * '#' and ';' are special symbols to start comments. A comment ends with an |
| 3711 | * end-of-line. |
| 3712 | * |
| 3713 | * Format: name[=value] |
| 3714 | * - Preceding/trailing white-spaces of each line are removed. |
| 3715 | * - White-spaces around '=' are removed. |
| 3716 | * - name does not allow white-spaces. |
| 3717 | */ |
| 3718 | #define CONFIG_FILE_BUF_SIZE 4096 |
| 3719 | #define CONFIG_FILE_MAX_NAME_LENGTH 128 |
| 3720 | #define CONFIG_FILE_MAX_VALUE_LENGTH 2048 |
| 3721 | |
| 3722 | typedef enum { |
| 3723 | CONFIG_FILE_SUCCESS, |
| 3724 | CONFIG_FILE_FORMAT_ERROR, |
| 3725 | CONFIG_FILE_FOPEN_ERROR, |
| 3726 | CONFIG_FILE_MALLOC_ERROR, |
| 3727 | CONFIG_FILE_ATEXIT_ERROR |
| 3728 | } config_file_status; |
| 3729 | |
| 3730 | /* |
| 3731 | * The node type of a linked list for storing values. Note that a value is |
| 3732 | * stored in the extra space of an object. |
| 3733 | */ |
| 3734 | typedef struct _config_file_entry { |
| 3735 | struct _config_file_entry *next; |
| 3736 | } config_file_entry; |
| 3737 | |
| 3738 | static config_file_entry *config_file_entry_head = NULL; |
| 3739 | |
| 3740 | static void |
| 3741 | config_file_clear(void) |
| 3742 | { |
| 3743 | while (config_file_entry_head) { |
| 3744 | config_file_entry *next = config_file_entry_head->next; |
| 3745 | free(config_file_entry_head); |
| 3746 | config_file_entry_head = next; |
| 3747 | } |
| 3748 | } |
| 3749 | |
| 3750 | static config_file_status |
| 3751 | config_file_register(const char *path, |
| 3752 | const grn_str_getopt_opt *opts, |
| 3753 | int *flags, |
| 3754 | const char *name, |
| 3755 | size_t name_length, |
| 3756 | const char *value, |
| 3757 | size_t value_length) |
| 3758 | { |
| 3759 | char name_buf[CONFIG_FILE_MAX_NAME_LENGTH + 3]; |
| 3760 | config_file_entry *entry = NULL; |
| 3761 | char *args[4]; |
| 3762 | |
| 3763 | name_buf[0] = name_buf[1] = '-'; |
| 3764 | grn_strcpy(name_buf + 2, CONFIG_FILE_MAX_NAME_LENGTH + 1, name); |
| 3765 | |
| 3766 | if (value) { |
| 3767 | const size_t entry_size = sizeof(config_file_entry) + value_length + 1; |
| 3768 | entry = (config_file_entry *)malloc(entry_size); |
| 3769 | if (!entry) { |
| 3770 | fprintf(stderr, |
| 3771 | "memory allocation failed: %u bytes\n", |
| 3772 | (unsigned int)entry_size); |
| 3773 | return CONFIG_FILE_MALLOC_ERROR; |
| 3774 | } |
| 3775 | grn_strcpy((char *)(entry + 1), value_length + 1, value); |
| 3776 | entry->next = config_file_entry_head; |
| 3777 | if (!config_file_entry_head) { |
| 3778 | if (atexit(config_file_clear)) { |
| 3779 | free(entry); |
| 3780 | return CONFIG_FILE_ATEXIT_ERROR; |
| 3781 | } |
| 3782 | } |
| 3783 | config_file_entry_head = entry; |
| 3784 | } |
| 3785 | |
| 3786 | args[0] = (char *)path; |
| 3787 | args[1] = name_buf; |
| 3788 | args[2] = entry ? (char *)(entry + 1) : NULL; |
| 3789 | args[3] = NULL; |
| 3790 | grn_str_getopt(entry ? 3 : 2, args, opts, flags); |
| 3791 | return CONFIG_FILE_SUCCESS; |
| 3792 | } |
| 3793 | |
| 3794 | static config_file_status |
| 3795 | config_file_parse(const char *path, |
| 3796 | const grn_str_getopt_opt *opts, |
| 3797 | int *flags, |
| 3798 | char *buf) |
| 3799 | { |
| 3800 | char *ptr, *name, *value; |
| 3801 | size_t name_length, value_length; |
| 3802 | |
| 3803 | while (isspace((unsigned char)*buf)) { |
| 3804 | buf++; |
| 3805 | } |
| 3806 | |
| 3807 | ptr = buf; |
| 3808 | while (*ptr && *ptr != '#' && *ptr != ';') { |
| 3809 | ptr++; |
| 3810 | } |
| 3811 | |
| 3812 | do { |
| 3813 | *ptr-- = '\0'; |
| 3814 | } while (ptr >= buf && isspace((unsigned char)*ptr)); |
| 3815 | |
| 3816 | if (!*buf) { |
| 3817 | return CONFIG_FILE_SUCCESS; |
| 3818 | } |
| 3819 | |
| 3820 | name = ptr = buf; |
| 3821 | while (*ptr && !isspace((unsigned char)*ptr) && *ptr != '=') { |
| 3822 | ptr++; |
| 3823 | } |
| 3824 | while (isspace((unsigned char)*ptr)) { |
| 3825 | *ptr++ = '\0'; |
| 3826 | } |
| 3827 | |
| 3828 | name_length = strlen(name); |
| 3829 | if (name_length == 0) { |
| 3830 | return CONFIG_FILE_SUCCESS; |
| 3831 | } else if (name_length > CONFIG_FILE_MAX_NAME_LENGTH) { |
| 3832 | fprintf(stderr, |
| 3833 | "too long name in config file: %u bytes\n", |
| 3834 | (unsigned int)name_length); |
| 3835 | return CONFIG_FILE_FORMAT_ERROR; |
| 3836 | } |
| 3837 | |
| 3838 | if (*ptr == '=') { |
| 3839 | *ptr++ = '\0'; |
| 3840 | while (isspace((unsigned char)*ptr)) { |
| 3841 | ptr++; |
| 3842 | } |
| 3843 | value = ptr; |
| 3844 | } else if (*ptr) { |
| 3845 | fprintf(stderr, "invalid name in config file\n"); |
| 3846 | return CONFIG_FILE_FORMAT_ERROR; |
| 3847 | } else { |
| 3848 | value = NULL; |
| 3849 | } |
| 3850 | |
| 3851 | value_length = value ? strlen(value) : 0; |
| 3852 | if (value_length > CONFIG_FILE_MAX_VALUE_LENGTH) { |
| 3853 | fprintf(stderr, |
| 3854 | "too long value in config file: %u bytes\n", |
| 3855 | (unsigned int)value_length); |
| 3856 | return CONFIG_FILE_FORMAT_ERROR; |
| 3857 | } |
| 3858 | |
| 3859 | return config_file_register(path, |
| 3860 | opts, |
| 3861 | flags, |
| 3862 | name, |
| 3863 | name_length, |
| 3864 | value, |
| 3865 | value_length); |
| 3866 | } |
| 3867 | |
| 3868 | static config_file_status |
| 3869 | config_file_load(const char *path, const grn_str_getopt_opt *opts, int *flags) |
| 3870 | { |
| 3871 | config_file_status status = CONFIG_FILE_SUCCESS; |
| 3872 | char buf[CONFIG_FILE_BUF_SIZE]; |
| 3873 | size_t length = 0; |
| 3874 | FILE *const file = fopen(path, "rb"); |
| 3875 | if (!file) { |
| 3876 | return CONFIG_FILE_FOPEN_ERROR; |
| 3877 | } |
| 3878 | |
| 3879 | for (;;) { |
| 3880 | int c = fgetc(file); |
| 3881 | if (c == '\r' || c == '\n' || c == EOF) { |
| 3882 | if (length < sizeof(buf) - 1) { |
| 3883 | buf[length] = '\0'; |
| 3884 | status = config_file_parse(path, opts, flags, buf); |
| 3885 | if (status != CONFIG_FILE_SUCCESS) { |
| 3886 | break; |
| 3887 | } |
| 3888 | } |
| 3889 | length = 0; |
| 3890 | } else if (c == '\0') { |
| 3891 | fprintf(stderr, "prohibited '\\0' in config file: %s\n", path); |
| 3892 | status = CONFIG_FILE_FORMAT_ERROR; |
| 3893 | break; |
| 3894 | } else { |
| 3895 | if (length < sizeof(buf) - 1) { |
| 3896 | buf[length] = (char)c; |
| 3897 | } |
| 3898 | length++; |
| 3899 | } |
| 3900 | |
| 3901 | if (c == EOF) { |
| 3902 | break; |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | fclose(file); |
| 3907 | return status; |
| 3908 | } |
| 3909 | |
| 3910 | static const int default_http_port = DEFAULT_HTTP_PORT; |
| 3911 | static const int default_gqtp_port = DEFAULT_GQTP_PORT; |
| 3912 | static grn_encoding default_encoding = GRN_ENC_DEFAULT; |
| 3913 | static uint32_t default_max_n_threads = DEFAULT_MAX_N_FLOATING_THREADS; |
| 3914 | static const grn_log_level default_log_level = GRN_LOG_DEFAULT_LEVEL; |
| 3915 | static const char *const default_protocol = "gqtp"; |
| 3916 | static const char *default_hostname = "localhost"; |
| 3917 | static const char *const default_dest = "localhost"; |
| 3918 | static const char *default_log_path = ""; |
| 3919 | static const char *default_query_log_path = ""; |
| 3920 | static const char *default_config_path = ""; |
| 3921 | static const char *default_document_root = ""; |
| 3922 | static grn_command_version default_default_command_version = |
| 3923 | GRN_COMMAND_VERSION_DEFAULT; |
| 3924 | static int64_t default_default_match_escalation_threshold = 0; |
| 3925 | static const char *const default_bind_address = "0.0.0.0"; |
| 3926 | static double default_default_request_timeout = 0.0; |
| 3927 | |
| 3928 | static void |
| 3929 | init_default_settings(void) |
| 3930 | { |
| 3931 | output = stdout; |
| 3932 | |
| 3933 | default_encoding = grn_encoding_parse(GRN_DEFAULT_ENCODING); |
| 3934 | |
| 3935 | { |
| 3936 | const uint32_t n_cores = get_core_number(); |
| 3937 | if (n_cores != 0) { |
| 3938 | default_max_n_threads = n_cores; |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | default_log_path = grn_default_logger_get_path(); |
| 3943 | default_query_log_path = grn_default_query_logger_get_path(); |
| 3944 | |
| 3945 | default_config_path = getenv("GRN_CONFIG_PATH"); |
| 3946 | if (!default_config_path) { |
| 3947 | default_config_path = GRN_CONFIG_PATH; |
| 3948 | if (!default_config_path) { |
| 3949 | default_config_path = ""; |
| 3950 | } |
| 3951 | } |
| 3952 | |
| 3953 | #ifdef WIN32 |
| 3954 | { |
| 3955 | static char windows_default_document_root[PATH_MAX]; |
| 3956 | size_t document_root_length = strlen(grn_windows_base_dir()) + 1 + |
| 3957 | strlen(GRN_DEFAULT_RELATIVE_DOCUMENT_ROOT) + |
| 3958 | 1; |
| 3959 | if (document_root_length >= PATH_MAX) { |
| 3960 | fprintf(stderr, "can't use default root: too long path\n"); |
| 3961 | } else { |
| 3962 | grn_strcpy(windows_default_document_root, |
| 3963 | PATH_MAX, |
| 3964 | grn_windows_base_dir()); |
| 3965 | grn_strcat(windows_default_document_root, PATH_MAX, "/"); |
| 3966 | grn_strcat(windows_default_document_root, |
| 3967 | PATH_MAX, |
| 3968 | GRN_DEFAULT_RELATIVE_DOCUMENT_ROOT); |
| 3969 | default_document_root = windows_default_document_root; |
| 3970 | } |
| 3971 | } |
| 3972 | #else |
| 3973 | default_document_root = GRN_DEFAULT_DOCUMENT_ROOT; |
| 3974 | #endif |
| 3975 | |
| 3976 | default_default_command_version = grn_get_default_command_version(); |
| 3977 | default_default_match_escalation_threshold = |
| 3978 | grn_get_default_match_escalation_threshold(); |
| 3979 | default_default_request_timeout = grn_get_default_request_timeout(); |
| 3980 | } |
| 3981 | |
| 3982 | static void |
| 3983 | show_config(FILE *out, const grn_str_getopt_opt *opts, int flags) |
| 3984 | { |
| 3985 | const grn_str_getopt_opt *o; |
| 3986 | |
| 3987 | for (o = opts; o->opt || o->longopt; o++) { |
| 3988 | switch (o->op) { |
| 3989 | case GETOPT_OP_NONE: |
| 3990 | if (o->arg && *o->arg) { |
| 3991 | if (o->longopt && strcmp(o->longopt, "config-path")) { |
| 3992 | fprintf(out, "%s=%s\n", o->longopt, *o->arg); |
| 3993 | } |
| 3994 | } |
| 3995 | break; |
| 3996 | case GETOPT_OP_ON: |
| 3997 | if (flags & o->flag) { |
| 3998 | goto no_arg; |
| 3999 | } |
| 4000 | break; |
| 4001 | case GETOPT_OP_OFF: |
| 4002 | if (!(flags & o->flag)) { |
| 4003 | goto no_arg; |
| 4004 | } |
| 4005 | break; |
| 4006 | case GETOPT_OP_UPDATE: |
| 4007 | if (flags == o->flag) { |
| 4008 | no_arg: |
| 4009 | if (o->longopt) { |
| 4010 | fprintf(out, "%s\n", o->longopt); |
| 4011 | } |
| 4012 | } |
| 4013 | break; |
| 4014 | } |
| 4015 | } |
| 4016 | } |
| 4017 | |
| 4018 | static void |
| 4019 | show_version(void) |
| 4020 | { |
| 4021 | printf("%s %s [", grn_get_package_label(), grn_get_version()); |
| 4022 | |
| 4023 | /* FIXME: Should we detect host information dynamically on Windows? */ |
| 4024 | #ifdef HOST_OS |
| 4025 | printf("%s,", HOST_OS); |
| 4026 | #endif |
| 4027 | #ifdef HOST_CPU |
| 4028 | printf("%s,", HOST_CPU); |
| 4029 | #endif |
| 4030 | printf("%s", GRN_DEFAULT_ENCODING); |
| 4031 | |
| 4032 | printf(",match-escalation-threshold=%" GRN_FMT_INT64D, |
| 4033 | grn_get_default_match_escalation_threshold()); |
| 4034 | |
| 4035 | #ifndef NO_NFKC |
| 4036 | printf(",nfkc"); |
| 4037 | #endif |
| 4038 | #ifdef GRN_WITH_MECAB |
| 4039 | printf(",mecab"); |
| 4040 | #endif |
| 4041 | #ifdef GRN_WITH_MESSAGE_PACK |
| 4042 | printf(",message-pack"); |
| 4043 | #endif |
| 4044 | #ifdef GRN_WITH_MRUBY |
| 4045 | printf(",mruby"); |
| 4046 | #endif |
| 4047 | #ifdef GRN_WITH_ONIGMO |
| 4048 | printf(",onigmo"); |
| 4049 | #endif |
| 4050 | #ifdef GRN_WITH_ZLIB |
| 4051 | printf(",zlib"); |
| 4052 | #endif |
| 4053 | #ifdef GRN_WITH_LZ4 |
| 4054 | printf(",lz4"); |
| 4055 | #endif |
| 4056 | #ifdef GRN_WITH_ZSTD |
| 4057 | printf(",zstandard"); |
| 4058 | #endif |
| 4059 | #ifdef USE_KQUEUE |
| 4060 | printf(",kqueue"); |
| 4061 | #endif |
| 4062 | #ifdef USE_EPOLL |
| 4063 | printf(",epoll"); |
| 4064 | #endif |
| 4065 | #ifdef USE_POLL |
| 4066 | printf(",poll"); |
| 4067 | #endif |
| 4068 | #ifdef GRN_WITH_RAPIDJSON |
| 4069 | printf(",rapidjson"); |
| 4070 | #endif |
| 4071 | #ifdef GRN_WITH_APACHE_ARROW |
| 4072 | printf(",apache-arrow"); |
| 4073 | #endif |
| 4074 | #ifdef GRN_WITH_XXHASH |
| 4075 | printf(",xxhash"); |
| 4076 | #endif |
| 4077 | #ifdef GRN_WITH_BLOSC |
| 4078 | printf(",blosc"); |
| 4079 | #endif |
| 4080 | #ifdef GRN_HAVE_BFLOAT16 |
| 4081 | printf(",bfloat16"); |
| 4082 | #endif |
| 4083 | #ifdef GRN_WITH_H3 |
| 4084 | printf(",h3"); |
| 4085 | #endif |
| 4086 | #ifdef GRN_WITH_SIMDJSON |
| 4087 | printf(",simdjson"); |
| 4088 | #endif |
| 4089 | #ifdef GRN_WITH_LLAMA_CPP |
| 4090 | printf(",llama.cpp"); |
| 4091 | #endif |
| 4092 | #ifdef GRN_WITH_FAISS |
| 4093 | printf(",faiss"); |
| 4094 | #endif |
| 4095 | #ifdef GRN_WITH_OPENZL |
| 4096 | printf(",openzl"); |
| 4097 | #endif |
| 4098 | #ifdef GRN_WITH_JSONCONS |
| 4099 | printf(",jsoncons"); |
| 4100 | #endif |
| 4101 | printf("]\n"); |
| 4102 | |
| 4103 | #ifdef CONFIGURE_OPTIONS |
| 4104 | printf("\n"); |
| 4105 | printf("configure options: <%s>\n", CONFIGURE_OPTIONS); |
| 4106 | #endif |
| 4107 | } |
| 4108 | |
| 4109 | static void |
| 4110 | show_usage(FILE *output) |
| 4111 | { |
| 4112 | uint32_t default_cache_limit = GRN_CACHE_DEFAULT_MAX_N_ENTRIES; |
| 4113 | |
| 4114 | fprintf( |
| 4115 | output, |
| 4116 | "Usage: groonga [options...] [dest]\n" |
| 4117 | "\n" |
| 4118 | "Mode options: (default: standalone)\n" |
| 4119 | " By default, groonga runs in standalone mode.\n" |
| 4120 | " -c: run in client mode\n" |
| 4121 | " -s: run in server mode\n" |
| 4122 | " -d: run in daemon mode\n" |
| 4123 | "\n" |
| 4124 | "Database creation options:\n" |
| 4125 | " -n: create new database (except client mode)\n" |
| 4126 | " -e, --encoding <encoding>:\n" |
| 4127 | " specify encoding for new database\n" |
| 4128 | " [none|euc|utf8|sjis|latin1|koi8r] (default: %s)\n" |
| 4129 | "\n" |
| 4130 | "Standalone/client options:\n" |
| 4131 | " --file <path>: read commands from specified file\n" |
| 4132 | " --input-fd <FD>: read commands from specified file " |
| 4133 | "descriptor\n" |
| 4134 | " --file has a priority over --input-fd\n" |
| 4135 | " --output-fd <FD>: output response to specified file " |
| 4136 | "descriptor\n" |
| 4137 | " -p, --port <port number>: specify server port number (client mode " |
| 4138 | "only)\n" |
| 4139 | " (default: %d)\n" |
| 4140 | "\n" |
| 4141 | "Server/daemon options:\n" |
| 4142 | " --bind-address <ip/hostname>:\n" |
| 4143 | " specify server address to bind\n" |
| 4144 | " (default: %s)\n" |
| 4145 | " -p, --port <port number>: specify server port number\n" |
| 4146 | " (HTTP default: %d, GQTP default: %d)\n" |
| 4147 | " -i, --server-id <ip/hostname>:\n" |
| 4148 | " specify server ID address (default: %s)\n" |
| 4149 | " --protocol <protocol>: specify server protocol to listen\n" |
| 4150 | " [gqtp|http|memcached] (default: %s)\n" |
| 4151 | " --document-root <path>: specify document root path (http only)\n" |
| 4152 | " (default: %s)\n" |
| 4153 | " --cache-limit <limit>: specify max number of cache data " |
| 4154 | "(default: %u)\n" |
| 4155 | " -t, --max-threads <max threads>:\n" |
| 4156 | " specify max number of threads (default: " |
| 4157 | "%u)\n" |
| 4158 | " --pid-path <path>: specify file to write process ID to\n" |
| 4159 | " --default-n-workers <n>:\n" |
| 4160 | " specify the default number of workers\n" |
| 4161 | " --default-request-timeout <timeout>:\n" |
| 4162 | " specify the default request timeout in " |
| 4163 | "seconds\n" |
| 4164 | " (default: %f)\n" |
| 4165 | " --cache-base-path <path>: specify the cache base path\n" |
| 4166 | " You can make cache persistent by this " |
| 4167 | "option\n" |
| 4168 | " You must specify path on memory file " |
| 4169 | "system\n" |
| 4170 | " (default: none; disabled)\n" |
| 4171 | " --listen-backlog <backlog>: specify the backlog for listen(2)\n" |
| 4172 | " (default: %d)\n" |
| 4173 | "\n" |
| 4174 | "Memcached options:\n" |
| 4175 | " --memcached-column <column>:\n" |
| 4176 | " specify column to access by memcached " |
| 4177 | "protocol\n" |
| 4178 | " The column must be text type column and\n" |
| 4179 | " its table must be not NO_KEY table\n" |
| 4180 | "\n" |
| 4181 | "Logging options:\n" |
| 4182 | " -l, --log-level <log level>:\n" |
| 4183 | " specify log level\n" |
| 4184 | " [none|emergency|alert|critical|\n" |
| 4185 | " error|warning|notice|info|debug|dump]\n" |
| 4186 | " (default: %s)\n" |
| 4187 | " --log-flags <log flags>:\n" |
| 4188 | " specify log flags\n" |
| 4189 | " '+' prefix means that 'add the flag'\n" |
| 4190 | " '-' prefix means that 'remove the flag'\n" |
| 4191 | " No prefix means that 'replace existing flags'\n" |
| 4192 | " Multiple log flags can be specified by\n" |
| 4193 | " separating flags with '|'\n" |
| 4194 | " Example: default|+pid|-time\n" |
| 4195 | " [none|time|title|message|location|\n" |
| 4196 | " pid|process_id|thread_id|context_id|\n" |
| 4197 | " all|default]\n" |
| 4198 | " (default: %s)\n" |
| 4199 | " --log-path <path>: specify log path\n" |
| 4200 | " (default: %s)\n" |
| 4201 | " --log-rotate-threshold-size <threshold>:\n" |
| 4202 | " specify threshold for log rotate\n" |
| 4203 | " Log file is rotated when\n" |
| 4204 | " log file size is larger than or\n" |
| 4205 | " equals to the threshold\n" |
| 4206 | " (default: 0; disabled)\n" |
| 4207 | #ifdef WIN32 |
| 4208 | " --use-windows-event-log:\n" |
| 4209 | " report logs as Windows events\n" |
| 4210 | #endif /* WIN32 */ |
| 4211 | " --query-log-path <path>:\n" |
| 4212 | " specify query log path\n" |
| 4213 | " (default: %s)\n" |
| 4214 | " --query-log-rotate-threshold-size <threshold>:\n" |
| 4215 | " specify threshold for query log rotate\n" |
| 4216 | " Query log file is rotated when\n" |
| 4217 | " query log file size is larger than or\n" |
| 4218 | " equals to the threshold\n" |
| 4219 | " (default: 0; disabled)\n" |
| 4220 | "\n" |
| 4221 | "Common options:\n" |
| 4222 | " --working-directory <path>:\n" |
| 4223 | " specify working directory path\n" |
| 4224 | " (none)\n" |
| 4225 | " --config-path <path>:\n" |
| 4226 | " specify config file path\n" |
| 4227 | " (default: %s)\n" |
| 4228 | " --default-command-version <version>:\n" |
| 4229 | " specify default command version (default: %d)\n" |
| 4230 | " --default-match-escalation-threshold <threshold>:\n" |
| 4231 | " specify default match escalation threshold" |
| 4232 | " (default: %" GRN_FMT_INT64D ")\n" |
| 4233 | "\n" |
| 4234 | " --show-config: show config\n" |
| 4235 | " -h, --help: show usage\n" |
| 4236 | " --version: show groonga version\n" |
| 4237 | "\n" |
| 4238 | "dest:\n" |
| 4239 | " <db pathname> [<commands>]: in standalone mode\n" |
| 4240 | " <db pathname>: in server/daemon mode\n" |
| 4241 | " <dest hostname> [<commands>]: in client mode (default: %s)\n", |
| 4242 | grn_encoding_to_string(default_encoding), |
| 4243 | default_gqtp_port, |
| 4244 | default_bind_address, |
| 4245 | default_http_port, |
| 4246 | default_gqtp_port, |
| 4247 | default_hostname, |
| 4248 | default_protocol, |
| 4249 | default_document_root, |
| 4250 | default_cache_limit, |
| 4251 | default_max_n_threads, |
| 4252 | default_default_request_timeout, |
| 4253 | listen_backlog, |
| 4254 | grn_log_level_to_string(default_log_level), |
| 4255 | "time|message", /* TODO: Generate from GRN_LOG_DEFAULT */ |
| 4256 | default_log_path, |
| 4257 | default_query_log_path, |
| 4258 | default_config_path, |
| 4259 | default_default_command_version, |
| 4260 | default_default_match_escalation_threshold, |
| 4261 | default_dest); |
| 4262 | } |
| 4263 | |
| 4264 | int |
| 4265 | main(int argc, char **argv) |
| 4266 | { |
| 4267 | const char *port_arg = NULL; |
| 4268 | const char *encoding_arg = NULL; |
| 4269 | const char *max_n_threads_arg = NULL; |
| 4270 | const char *log_level_arg = NULL; |
| 4271 | const char *bind_address_arg = NULL; |
| 4272 | const char *hostname_arg = NULL; |
| 4273 | const char *protocol_arg = NULL; |
| 4274 | const char *log_path_arg = GRN_LOG_PATH; |
| 4275 | const char *log_rotate_threshold_size_arg = NULL; |
| 4276 | const char *query_log_path_arg = NULL; |
| 4277 | const char *query_log_rotate_threshold_size_arg = NULL; |
| 4278 | const char *cache_limit_arg = NULL; |
| 4279 | const char *document_root_arg = NULL; |
| 4280 | const char *default_command_version_arg = NULL; |
| 4281 | const char *default_match_escalation_threshold_arg = NULL; |
| 4282 | const char *input_fd_arg = NULL; |
| 4283 | const char *output_fd_arg = NULL; |
| 4284 | const char *working_directory_arg = NULL; |
| 4285 | const char *config_path = NULL; |
| 4286 | const char *default_request_timeout_arg = NULL; |
| 4287 | const char *cache_base_path = NULL; |
| 4288 | const char *listen_backlog_arg = NULL; |
| 4289 | const char *log_flags_arg = NULL; |
| 4290 | const char *wal_role_arg = NULL; |
| 4291 | const char *default_n_workers_arg = NULL; |
| 4292 | int exit_code = EXIT_SUCCESS; |
| 4293 | int i; |
| 4294 | int flags = 0; |
| 4295 | uint32_t cache_limit = 0; |
| 4296 | grn_command_version default_command_version; |
| 4297 | int64_t default_match_escalation_threshold = 0; |
| 4298 | double default_request_timeout = 0.0; |
| 4299 | bool need_line_editor = false; |
| 4300 | static grn_str_getopt_opt opts[] = { |
| 4301 | {'p', "port", NULL, 0, GETOPT_OP_NONE}, |
| 4302 | {'e', "encoding", NULL, 0, GETOPT_OP_NONE}, |
| 4303 | {'t', "max-threads", NULL, 0, GETOPT_OP_NONE}, |
| 4304 | {'h', "help", NULL, ACTION_USAGE, GETOPT_OP_UPDATE}, |
| 4305 | {'c', NULL, NULL, FLAG_MODE_CLIENT, GETOPT_OP_ON}, |
| 4306 | {'d', NULL, NULL, FLAG_MODE_DAEMON, GETOPT_OP_ON}, |
| 4307 | {'s', NULL, NULL, FLAG_MODE_SERVER, GETOPT_OP_ON}, |
| 4308 | {'l', "log-level", NULL, 0, GETOPT_OP_NONE}, |
| 4309 | {'i', "server-id", NULL, 0, GETOPT_OP_NONE}, |
| 4310 | {'n', NULL, NULL, FLAG_NEW_DB, GETOPT_OP_ON}, |
| 4311 | {'\0', "protocol", NULL, 0, GETOPT_OP_NONE}, |
| 4312 | {'\0', "version", NULL, ACTION_VERSION, GETOPT_OP_UPDATE}, |
| 4313 | {'\0', "log-path", NULL, 0, GETOPT_OP_NONE}, |
| 4314 | {'\0', "log-rotate-threshold-size", NULL, 0, GETOPT_OP_NONE}, |
| 4315 | {'\0', "query-log-path", NULL, 0, GETOPT_OP_NONE}, |
| 4316 | {'\0', "query-log-rotate-threshold-size", NULL, 0, GETOPT_OP_NONE}, |
| 4317 | {'\0', "pid-path", NULL, 0, GETOPT_OP_NONE}, |
| 4318 | {'\0', "config-path", NULL, 0, GETOPT_OP_NONE}, |
| 4319 | {'\0', "show-config", NULL, ACTION_SHOW_CONFIG, GETOPT_OP_UPDATE}, |
| 4320 | {'\0', "cache-limit", NULL, 0, GETOPT_OP_NONE}, |
| 4321 | {'\0', "file", NULL, 0, GETOPT_OP_NONE}, |
| 4322 | {'\0', "document-root", NULL, 0, GETOPT_OP_NONE}, |
| 4323 | {'\0', "default-command-version", NULL, 0, GETOPT_OP_NONE}, |
| 4324 | {'\0', "default-match-escalation-threshold", NULL, 0, GETOPT_OP_NONE}, |
| 4325 | {'\0', "bind-address", NULL, 0, GETOPT_OP_NONE}, |
| 4326 | {'\0', "input-fd", NULL, 0, GETOPT_OP_NONE}, |
| 4327 | {'\0', "output-fd", NULL, 0, GETOPT_OP_NONE}, |
| 4328 | {'\0', "working-directory", NULL, 0, GETOPT_OP_NONE}, |
| 4329 | {'\0', |
| 4330 | "use-windows-event-log", |
| 4331 | NULL, |
| 4332 | FLAG_USE_WINDOWS_EVENT_LOG, |
| 4333 | GETOPT_OP_ON}, |
| 4334 | {'\0', "memcached-column", NULL, 0, GETOPT_OP_NONE}, |
| 4335 | {'\0', "default-request-timeout", NULL, 0, GETOPT_OP_NONE}, |
| 4336 | {'\0', "cache-base-path", NULL, 0, GETOPT_OP_NONE}, |
| 4337 | {'\0', "listen-backlog", NULL, 0, GETOPT_OP_NONE}, |
| 4338 | {'\0', "log-flags", NULL, 0, GETOPT_OP_NONE}, |
| 4339 | {'\0', "wal-role", NULL, 0, GETOPT_OP_NONE}, |
| 4340 | {'\0', "default-n-workers", NULL, 0, GETOPT_OP_NONE}, |
| 4341 | {'\0', NULL, NULL, 0, 0}}; |
| 4342 | opts[0].arg = &port_arg; |
| 4343 | opts[1].arg = &encoding_arg; |
| 4344 | opts[2].arg = &max_n_threads_arg; |
| 4345 | opts[7].arg = &log_level_arg; |
| 4346 | opts[8].arg = &hostname_arg; |
| 4347 | opts[10].arg = &protocol_arg; |
| 4348 | opts[12].arg = &log_path_arg; |
| 4349 | opts[13].arg = &log_rotate_threshold_size_arg; |
| 4350 | opts[14].arg = &query_log_path_arg; |
| 4351 | opts[15].arg = &query_log_rotate_threshold_size_arg; |
| 4352 | opts[16].arg = &pid_file_path; |
| 4353 | opts[17].arg = &config_path; |
| 4354 | opts[19].arg = &cache_limit_arg; |
| 4355 | opts[20].arg = &input_path; |
| 4356 | opts[21].arg = &document_root_arg; |
| 4357 | opts[22].arg = &default_command_version_arg; |
| 4358 | opts[23].arg = &default_match_escalation_threshold_arg; |
| 4359 | opts[24].arg = &bind_address_arg; |
| 4360 | opts[25].arg = &input_fd_arg; |
| 4361 | opts[26].arg = &output_fd_arg; |
| 4362 | opts[27].arg = &working_directory_arg; |
| 4363 | opts[29].arg = &memcached_column_name; |
| 4364 | opts[30].arg = &default_request_timeout_arg; |
| 4365 | opts[31].arg = &cache_base_path; |
| 4366 | opts[32].arg = &listen_backlog_arg; |
| 4367 | opts[33].arg = &log_flags_arg; |
| 4368 | opts[34].arg = &wal_role_arg; |
| 4369 | opts[35].arg = &default_n_workers_arg; |
| 4370 | |
| 4371 | reset_ready_notify_pipe(); |
| 4372 | |
| 4373 | init_default_settings(); |
| 4374 | |
| 4375 | /* only for parsing --config-path. */ |
| 4376 | i = grn_str_getopt(argc, argv, opts, &flags); |
| 4377 | if (i < 0) { |
| 4378 | show_usage(stderr); |
| 4379 | return EXIT_FAILURE; |
| 4380 | } |
| 4381 | |
| 4382 | if (config_path) { |
| 4383 | const config_file_status status = |
| 4384 | config_file_load(config_path, opts, &flags); |
| 4385 | if (status == CONFIG_FILE_FOPEN_ERROR) { |
| 4386 | fprintf(stderr, |
| 4387 | "%s: can't open config file: %s (%s)\n", |
| 4388 | argv[0], |
| 4389 | config_path, |
| 4390 | strerror(errno)); |
| 4391 | return EXIT_FAILURE; |
| 4392 | } else if (status != CONFIG_FILE_SUCCESS) { |
| 4393 | fprintf(stderr, |
| 4394 | "%s: failed to parse config file: %s (%s)\n", |
| 4395 | argv[0], |
| 4396 | config_path, |
| 4397 | (status == CONFIG_FILE_FORMAT_ERROR) ? "Invalid format" |
| 4398 | : strerror(errno)); |
| 4399 | return EXIT_FAILURE; |
| 4400 | } |
| 4401 | } else if (*default_config_path) { |
| 4402 | const config_file_status status = |
| 4403 | config_file_load(default_config_path, opts, &flags); |
| 4404 | if (status != CONFIG_FILE_SUCCESS && status != CONFIG_FILE_FOPEN_ERROR) { |
| 4405 | fprintf(stderr, |
| 4406 | "%s: failed to parse config file: %s (%s)\n", |
| 4407 | argv[0], |
| 4408 | default_config_path, |
| 4409 | (status == CONFIG_FILE_FORMAT_ERROR) ? "Invalid format" |
| 4410 | : strerror(errno)); |
| 4411 | return EXIT_FAILURE; |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | if (working_directory_arg) { |
| 4416 | if (chdir(working_directory_arg) == -1) { |
| 4417 | fprintf(stderr, |
| 4418 | "%s: failed to change directory: %s: %s\n", |
| 4419 | argv[0], |
| 4420 | working_directory_arg, |
| 4421 | strerror(errno)); |
| 4422 | return EXIT_FAILURE; |
| 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | if (cache_base_path) { |
| 4427 | grn_set_default_cache_base_path(cache_base_path); |
| 4428 | } |
| 4429 | |
| 4430 | /* ignore mode option in config file */ |
| 4431 | flags = (flags == ACTION_ERROR) ? 0 : (flags & ~ACTION_MASK); |
| 4432 | |
| 4433 | i = grn_str_getopt(argc, argv, opts, &flags); |
| 4434 | if (i < 0) { |
| 4435 | flags = ACTION_ERROR; |
| 4436 | } |
| 4437 | switch (flags & ACTION_MASK) { |
| 4438 | case ACTION_VERSION: |
| 4439 | show_version(); |
| 4440 | return EXIT_SUCCESS; |
| 4441 | case ACTION_USAGE: |
| 4442 | show_usage(output); |
| 4443 | return EXIT_SUCCESS; |
| 4444 | case ACTION_SHOW_CONFIG: |
| 4445 | show_config(output, opts, flags & ~ACTION_MASK); |
| 4446 | return EXIT_SUCCESS; |
| 4447 | case ACTION_ERROR: |
| 4448 | show_usage(stderr); |
| 4449 | return EXIT_FAILURE; |
| 4450 | } |
| 4451 | |
| 4452 | if ((flags & MODE_MASK) == 0) { |
| 4453 | flags |= FLAG_MODE_ALONE; |
| 4454 | } |
| 4455 | |
| 4456 | if (port_arg) { |
| 4457 | const char *const end = port_arg + strlen(port_arg); |
| 4458 | const char *rest = NULL; |
| 4459 | const int value = grn_atoi(port_arg, end, &rest); |
| 4460 | if (rest != end || value <= 0 || value > 65535) { |
| 4461 | fprintf(stderr, "invalid port number: <%s>\n", port_arg); |
| 4462 | return EXIT_FAILURE; |
| 4463 | } |
| 4464 | port = value; |
| 4465 | } else { |
| 4466 | if (protocol_arg) { |
| 4467 | if (*protocol_arg == 'h' || *protocol_arg == 'H') { |
| 4468 | port = default_http_port; |
| 4469 | } |
| 4470 | } |
| 4471 | } |
| 4472 | |
| 4473 | if (encoding_arg) { |
| 4474 | switch (*encoding_arg) { |
| 4475 | case 'n': |
| 4476 | case 'N': |
| 4477 | encoding = GRN_ENC_NONE; |
| 4478 | break; |
| 4479 | case 'e': |
| 4480 | case 'E': |
| 4481 | encoding = GRN_ENC_EUC_JP; |
| 4482 | break; |
| 4483 | case 'u': |
| 4484 | case 'U': |
| 4485 | encoding = GRN_ENC_UTF8; |
| 4486 | break; |
| 4487 | case 's': |
| 4488 | case 'S': |
| 4489 | encoding = GRN_ENC_SJIS; |
| 4490 | break; |
| 4491 | case 'l': |
| 4492 | case 'L': |
| 4493 | encoding = GRN_ENC_LATIN1; |
| 4494 | break; |
| 4495 | case 'k': |
| 4496 | case 'K': |
| 4497 | encoding = GRN_ENC_KOI8R; |
| 4498 | break; |
| 4499 | default: |
| 4500 | encoding = GRN_ENC_DEFAULT; |
| 4501 | break; |
| 4502 | } |
| 4503 | } else { |
| 4504 | encoding = GRN_ENC_DEFAULT; |
| 4505 | } |
| 4506 | |
| 4507 | if (!grn_document_root) { |
| 4508 | grn_document_root = default_document_root; |
| 4509 | } |
| 4510 | |
| 4511 | if (protocol_arg) { |
| 4512 | switch (*protocol_arg) { |
| 4513 | case 'g': |
| 4514 | case 'G': |
| 4515 | do_client = g_client; |
| 4516 | do_server = g_server; |
| 4517 | break; |
| 4518 | case 'h': |
| 4519 | case 'H': |
| 4520 | do_client = g_client; |
| 4521 | do_server = h_server; |
| 4522 | break; |
| 4523 | case 'm': |
| 4524 | case 'M': |
| 4525 | is_memcached_mode = true; |
| 4526 | do_client = g_client; |
| 4527 | do_server = g_server; |
| 4528 | break; |
| 4529 | default: |
| 4530 | do_client = g_client; |
| 4531 | do_server = g_server; |
| 4532 | break; |
| 4533 | } |
| 4534 | } else { |
| 4535 | do_client = g_client; |
| 4536 | do_server = g_server; |
| 4537 | } |
| 4538 | |
| 4539 | #ifdef WIN32 |
| 4540 | if (flags & FLAG_USE_WINDOWS_EVENT_LOG) { |
| 4541 | use_windows_event_log = true; |
| 4542 | } |
| 4543 | #endif /* WIN32 */ |
| 4544 | |
| 4545 | if (use_windows_event_log) { |
| 4546 | grn_windows_event_logger_set(NULL, windows_event_source_name); |
| 4547 | } |
| 4548 | |
| 4549 | if (log_path_arg) { |
| 4550 | grn_default_logger_set_path(log_path_arg); |
| 4551 | } |
| 4552 | |
| 4553 | if (log_rotate_threshold_size_arg) { |
| 4554 | const char *const end = |
| 4555 | log_rotate_threshold_size_arg + strlen(log_rotate_threshold_size_arg); |
| 4556 | const char *rest = NULL; |
| 4557 | const uint64_t value = |
| 4558 | grn_atoull(log_rotate_threshold_size_arg, end, &rest); |
| 4559 | if (end != rest) { |
| 4560 | fprintf(stderr, |
| 4561 | "invalid log rotate threshold size: <%s>\n", |
| 4562 | log_rotate_threshold_size_arg); |
| 4563 | return EXIT_FAILURE; |
| 4564 | } |
| 4565 | grn_default_logger_set_rotate_threshold_size(value); |
| 4566 | } |
| 4567 | |
| 4568 | if (log_flags_arg) { |
| 4569 | int log_flags = GRN_LOG_DEFAULT; |
| 4570 | if (!grn_log_flags_parse(log_flags_arg, -1, &log_flags)) { |
| 4571 | fprintf(stderr, "invalid log flags: <%s>\n", log_flags_arg); |
| 4572 | return EXIT_FAILURE; |
| 4573 | } |
| 4574 | grn_default_logger_set_flags(log_flags); |
| 4575 | } |
| 4576 | |
| 4577 | if (query_log_path_arg) { |
| 4578 | grn_default_query_logger_set_path(query_log_path_arg); |
| 4579 | } |
| 4580 | |
| 4581 | if (query_log_rotate_threshold_size_arg) { |
| 4582 | const char *const end = query_log_rotate_threshold_size_arg + |
| 4583 | strlen(query_log_rotate_threshold_size_arg); |
| 4584 | const char *rest = NULL; |
| 4585 | const uint64_t value = |
| 4586 | grn_atoull(query_log_rotate_threshold_size_arg, end, &rest); |
| 4587 | if (end != rest) { |
| 4588 | fprintf(stderr, |
| 4589 | "invalid query log rotate threshold size: <%s>\n", |
| 4590 | query_log_rotate_threshold_size_arg); |
| 4591 | return EXIT_FAILURE; |
| 4592 | } |
| 4593 | grn_default_query_logger_set_rotate_threshold_size(value); |
| 4594 | } |
| 4595 | |
| 4596 | { |
| 4597 | grn_log_level log_level; |
| 4598 | |
| 4599 | if (log_level_arg) { |
| 4600 | if (!grn_log_level_parse(log_level_arg, &log_level)) { |
| 4601 | const char *const end = log_level_arg + strlen(log_level_arg); |
| 4602 | const char *rest = NULL; |
| 4603 | const int value = grn_atoi(log_level_arg, end, &rest); |
| 4604 | if (end != rest || value < GRN_LOG_NONE || value > GRN_LOG_DUMP) { |
| 4605 | fprintf(stderr, "invalid log level: <%s>\n", log_level_arg); |
| 4606 | return EXIT_FAILURE; |
| 4607 | } |
| 4608 | log_level = value; |
| 4609 | } |
| 4610 | } else { |
| 4611 | log_level = default_log_level; |
| 4612 | } |
| 4613 | |
| 4614 | grn_default_logger_set_max_level(log_level); |
| 4615 | } |
| 4616 | |
| 4617 | if (max_n_threads_arg) { |
| 4618 | const char *const end = max_n_threads_arg + strlen(max_n_threads_arg); |
| 4619 | const char *rest = NULL; |
| 4620 | const uint32_t value = grn_atoui(max_n_threads_arg, end, &rest); |
| 4621 | if (end != rest || value < 1 || value > 100) { |
| 4622 | fprintf(stderr, |
| 4623 | "invalid max number of threads: <%s>\n", |
| 4624 | max_n_threads_arg); |
| 4625 | return EXIT_FAILURE; |
| 4626 | } |
| 4627 | max_n_floating_threads = value; |
| 4628 | } else { |
| 4629 | if (flags & FLAG_MODE_ALONE) { |
| 4630 | max_n_floating_threads = 1; |
| 4631 | } else { |
| 4632 | max_n_floating_threads = default_max_n_threads; |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | grn_thread_set_get_limit_func(groonga_get_thread_limit, NULL); |
| 4637 | grn_thread_set_set_limit_func(groonga_set_thread_limit, NULL); |
| 4638 | grn_thread_set_set_limit_with_ctx_func(groonga_set_thread_limit_with_ctx, |
| 4639 | NULL); |
| 4640 | |
| 4641 | if (output_fd_arg) { |
| 4642 | const char *const end = output_fd_arg + strlen(output_fd_arg); |
| 4643 | const char *rest = NULL; |
| 4644 | const int output_fd = grn_atoi(output_fd_arg, end, &rest); |
| 4645 | if (rest != end || output_fd == 0) { |
| 4646 | fprintf(stderr, "invalid output FD: <%s>\n", output_fd_arg); |
| 4647 | return EXIT_FAILURE; |
| 4648 | } |
| 4649 | output = fdopen(output_fd, "w"); |
| 4650 | if (!output) { |
| 4651 | fprintf(stderr, |
| 4652 | "can't open output FD: %d (%s)\n", |
| 4653 | output_fd, |
| 4654 | strerror(errno)); |
| 4655 | return EXIT_FAILURE; |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | if (bind_address_arg) { |
| 4660 | const size_t bind_address_length = strlen(bind_address_arg); |
| 4661 | if (bind_address_length > HOST_NAME_MAX) { |
| 4662 | fprintf(stderr, |
| 4663 | "too long bind address: %s (%u bytes):" |
| 4664 | " must not be longer than %u bytes\n", |
| 4665 | bind_address_arg, |
| 4666 | (unsigned int)bind_address_length, |
| 4667 | HOST_NAME_MAX); |
| 4668 | return EXIT_FAILURE; |
| 4669 | } |
| 4670 | grn_strcpy(bind_address, HOST_NAME_MAX + 1, bind_address_arg); |
| 4671 | } else { |
| 4672 | grn_strcpy(bind_address, HOST_NAME_MAX + 1, default_bind_address); |
| 4673 | } |
| 4674 | |
| 4675 | if (hostname_arg) { |
| 4676 | const size_t hostname_length = strlen(hostname_arg); |
| 4677 | if (hostname_length > HOST_NAME_MAX) { |
| 4678 | fprintf(stderr, |
| 4679 | "too long hostname: %s (%u bytes):" |
| 4680 | " must not be longer than %u bytes\n", |
| 4681 | hostname_arg, |
| 4682 | (unsigned int)hostname_length, |
| 4683 | HOST_NAME_MAX); |
| 4684 | return EXIT_FAILURE; |
| 4685 | } |
| 4686 | grn_strcpy(hostname, HOST_NAME_MAX + 1, hostname_arg); |
| 4687 | } else { |
| 4688 | grn_strcpy(hostname, HOST_NAME_MAX + 1, default_hostname); |
| 4689 | } |
| 4690 | |
| 4691 | if (document_root_arg) { |
| 4692 | grn_document_root = document_root_arg; |
| 4693 | } |
| 4694 | |
| 4695 | if (default_command_version_arg) { |
| 4696 | const char *const end = |
| 4697 | default_command_version_arg + strlen(default_command_version_arg); |
| 4698 | const char *rest = NULL; |
| 4699 | const int value = grn_atoi(default_command_version_arg, end, &rest); |
| 4700 | if (end != rest || value < GRN_COMMAND_VERSION_MIN || |
| 4701 | value > GRN_COMMAND_VERSION_MAX) { |
| 4702 | fprintf(stderr, |
| 4703 | "invalid command version: <%s>\n", |
| 4704 | default_command_version_arg); |
| 4705 | return EXIT_FAILURE; |
| 4706 | } |
| 4707 | default_command_version = value; |
| 4708 | } else { |
| 4709 | default_command_version = default_default_command_version; |
| 4710 | } |
| 4711 | |
| 4712 | if (default_match_escalation_threshold_arg) { |
| 4713 | const char *const end = default_match_escalation_threshold_arg + |
| 4714 | strlen(default_match_escalation_threshold_arg); |
| 4715 | const char *rest = NULL; |
| 4716 | const int64_t value = |
| 4717 | grn_atoll(default_match_escalation_threshold_arg, end, &rest); |
| 4718 | if (end != rest) { |
| 4719 | fprintf(stderr, |
| 4720 | "invalid match escalation threshold: <%s>\n", |
| 4721 | default_match_escalation_threshold_arg); |
| 4722 | return EXIT_FAILURE; |
| 4723 | } |
| 4724 | default_match_escalation_threshold = value; |
| 4725 | } else { |
| 4726 | default_match_escalation_threshold = |
| 4727 | default_default_match_escalation_threshold; |
| 4728 | } |
| 4729 | |
| 4730 | if (cache_limit_arg) { |
| 4731 | const char *const end = cache_limit_arg + strlen(cache_limit_arg); |
| 4732 | const char *rest = NULL; |
| 4733 | const uint32_t value = grn_atoui(cache_limit_arg, end, &rest); |
| 4734 | if (end != rest) { |
| 4735 | fprintf(stderr, "invalid --cache-limit value: <%s>\n", cache_limit_arg); |
| 4736 | return EXIT_FAILURE; |
| 4737 | } |
| 4738 | cache_limit = value; |
| 4739 | } |
| 4740 | |
| 4741 | if (default_request_timeout_arg) { |
| 4742 | const char *const end = |
| 4743 | default_request_timeout_arg + strlen(default_request_timeout_arg); |
| 4744 | char *rest = NULL; |
| 4745 | double value; |
| 4746 | value = strtod(default_request_timeout_arg, &rest); |
| 4747 | if (end != rest) { |
| 4748 | fprintf(stderr, |
| 4749 | "invalid default request timeout: <%s>\n", |
| 4750 | default_request_timeout_arg); |
| 4751 | return EXIT_FAILURE; |
| 4752 | } |
| 4753 | default_request_timeout = value; |
| 4754 | } else { |
| 4755 | default_request_timeout = default_default_request_timeout; |
| 4756 | } |
| 4757 | |
| 4758 | if (listen_backlog_arg) { |
| 4759 | const char *const end = listen_backlog_arg + strlen(listen_backlog_arg); |
| 4760 | const char *rest = NULL; |
| 4761 | int value; |
| 4762 | value = grn_atoi(listen_backlog_arg, end, &rest); |
| 4763 | if (rest != end || value <= 0) { |
| 4764 | fprintf(stderr, "invalid listen backlog: <%s>\n", listen_backlog_arg); |
| 4765 | return EXIT_FAILURE; |
| 4766 | } |
| 4767 | listen_backlog = value; |
| 4768 | } |
| 4769 | |
| 4770 | if (!wal_role_arg) { |
| 4771 | wal_role_arg = getenv("GRN_WAL_ROLE"); |
| 4772 | } |
| 4773 | if (wal_role_arg) { |
| 4774 | if (strcmp(wal_role_arg, "none") == 0) { |
| 4775 | } else if (strcmp(wal_role_arg, "primary") == 0) { |
| 4776 | wal_role = GRN_WAL_ROLE_PRIMARY; |
| 4777 | worker_wal_role = GRN_WAL_ROLE_SECONDARY; |
| 4778 | } else if (strcmp(wal_role_arg, "secondary") == 0) { |
| 4779 | wal_role = GRN_WAL_ROLE_SECONDARY; |
| 4780 | worker_wal_role = GRN_WAL_ROLE_SECONDARY; |
| 4781 | } else { |
| 4782 | fprintf(stderr, "invalid WAL role: <%s>\n", wal_role_arg); |
| 4783 | fprintf(stderr, "available WAL roles: none, primary, secondary\n"); |
| 4784 | return EXIT_FAILURE; |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | if (default_n_workers_arg) { |
| 4789 | const char *const end = |
| 4790 | default_n_workers_arg + strlen(default_n_workers_arg); |
| 4791 | const char *rest = NULL; |
| 4792 | const int value = grn_atoi(default_n_workers_arg, end, &rest); |
| 4793 | if (end != rest) { |
| 4794 | fprintf(stderr, |
| 4795 | "invalid --default-n-workers value: <%s>\n", |
| 4796 | default_n_workers_arg); |
| 4797 | return EXIT_FAILURE; |
| 4798 | } |
| 4799 | grn_set_default_n_workers(value); |
| 4800 | } |
| 4801 | |
| 4802 | grn_gctx.errbuf[0] = '\0'; |
| 4803 | if (grn_init()) { |
| 4804 | fprintf(stderr, "failed to initialize Groonga: %s\n", grn_gctx.errbuf); |
| 4805 | return EXIT_FAILURE; |
| 4806 | } |
| 4807 | |
| 4808 | grn_set_default_encoding(encoding); |
| 4809 | |
| 4810 | if (default_command_version_arg) { |
| 4811 | grn_set_default_command_version(default_command_version); |
| 4812 | } |
| 4813 | |
| 4814 | if (default_match_escalation_threshold_arg) { |
| 4815 | grn_set_default_match_escalation_threshold( |
| 4816 | default_match_escalation_threshold); |
| 4817 | } |
| 4818 | |
| 4819 | if (default_request_timeout_arg) { |
| 4820 | grn_set_default_request_timeout(default_request_timeout); |
| 4821 | } |
| 4822 | |
| 4823 | grn_set_segv_handler(); |
| 4824 | grn_set_abrt_handler(); |
| 4825 | grn_set_int_handler(); |
| 4826 | grn_set_term_handler(); |
| 4827 | #ifdef ENABLE_LOG_REOPEN_BY_SIGNAL |
| 4828 | set_usr1_handler(); |
| 4829 | #endif |
| 4830 | |
| 4831 | if (cache_limit_arg) { |
| 4832 | grn_cache *cache; |
| 4833 | cache = grn_cache_current_get(&grn_gctx); |
| 4834 | grn_cache_set_max_n_entries(&grn_gctx, cache, cache_limit); |
| 4835 | } |
| 4836 | |
| 4837 | CRITICAL_SECTION_INIT(q_critical_section); |
| 4838 | COND_INIT(q_cond); |
| 4839 | |
| 4840 | if (input_path) { |
| 4841 | input_reader = grn_file_reader_open(&grn_gctx, input_path); |
| 4842 | if (!input_reader) { |
| 4843 | fprintf(stderr, |
| 4844 | "can't open input file: <%s>: %s\n", |
| 4845 | input_path, |
| 4846 | grn_gctx.errbuf); |
| 4847 | return EXIT_FAILURE; |
| 4848 | } |
| 4849 | batchmode = true; |
| 4850 | } else { |
| 4851 | if (input_fd_arg) { |
| 4852 | const char *const end = input_fd_arg + strlen(input_fd_arg); |
| 4853 | const char *rest = NULL; |
| 4854 | const int input_fd = grn_atoi(input_fd_arg, end, &rest); |
| 4855 | if (rest != end || input_fd == 0) { |
| 4856 | fprintf(stderr, "invalid input FD: <%s>\n", input_fd_arg); |
| 4857 | return EXIT_FAILURE; |
| 4858 | } |
| 4859 | if (dup2(input_fd, STDIN_FILENO) == -1) { |
| 4860 | fprintf(stderr, |
| 4861 | "can't open input FD: %d (%s)\n", |
| 4862 | input_fd, |
| 4863 | strerror(errno)); |
| 4864 | return EXIT_FAILURE; |
| 4865 | } |
| 4866 | input_reader = grn_file_reader_open(&grn_gctx, "-"); |
| 4867 | if (!input_reader) { |
| 4868 | fprintf(stderr, "%s", grn_gctx.errbuf); |
| 4869 | return EXIT_FAILURE; |
| 4870 | } |
| 4871 | batchmode = true; |
| 4872 | } else { |
| 4873 | input_reader = grn_file_reader_open(&grn_gctx, "-"); |
| 4874 | if (!input_reader) { |
| 4875 | fprintf(stderr, "%s", grn_gctx.errbuf); |
| 4876 | return EXIT_FAILURE; |
| 4877 | } |
| 4878 | if (argc - i > 1) { |
| 4879 | batchmode = true; |
| 4880 | } else { |
| 4881 | batchmode = !grn_isatty(0); |
| 4882 | } |
| 4883 | } |
| 4884 | } |
| 4885 | |
| 4886 | if ((flags & (FLAG_MODE_ALONE | FLAG_MODE_CLIENT)) && !batchmode) { |
| 4887 | need_line_editor = true; |
| 4888 | } |
| 4889 | |
| 4890 | #ifdef GRN_WITH_LIBEDIT |
| 4891 | if (need_line_editor) { |
| 4892 | line_editor_init(argc, argv); |
| 4893 | } |
| 4894 | #endif |
| 4895 | |
| 4896 | newdb = (flags & FLAG_NEW_DB); |
| 4897 | is_daemon_mode = (flags & FLAG_MODE_DAEMON); |
| 4898 | if (flags & FLAG_MODE_CLIENT) { |
| 4899 | exit_code = do_client(argc - i, argv + i); |
| 4900 | } else if (is_daemon_mode || (flags & FLAG_MODE_SERVER)) { |
| 4901 | exit_code = do_server(argc > i ? argv[i] : NULL); |
| 4902 | } else { |
| 4903 | exit_code = do_alone(argc - i, argv + i); |
| 4904 | } |
| 4905 | |
| 4906 | COND_FIN(q_cond); |
| 4907 | CRITICAL_SECTION_FIN(q_critical_section); |
| 4908 | |
| 4909 | if (input_reader) { |
| 4910 | grn_file_reader_close(&grn_gctx, input_reader); |
| 4911 | } |
| 4912 | #ifdef GRN_WITH_LIBEDIT |
| 4913 | if (need_line_editor) { |
| 4914 | line_editor_fin(); |
| 4915 | } |
| 4916 | #endif |
| 4917 | if (output != stdout) { |
| 4918 | fclose(output); |
| 4919 | } |
| 4920 | grn_fin(); |
| 4921 | return exit_code; |
| 4922 | } |