| 1 |
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <pthread.h> |
| 5 |
|
| 6 |
#include "protocol_extension.h" |
| 7 |
|
| 8 |
union c99hack { |
| 9 |
void *pointer; |
| 10 |
void (*exit_function)(void); |
| 11 |
}; |
| 12 |
|
| 13 |
static void* check_stdin_thread(void* arg) |
| 14 |
{ |
| 15 |
pthread_detach(pthread_self()); |
| 16 |
|
| 17 |
while (!feof(stdin)) { |
| 18 |
getc(stdin); |
| 19 |
} |
| 20 |
|
| 21 |
fprintf(stderr, "EOF on stdin. Exiting\n"); |
| 22 |
union c99hack ch = { .pointer = arg }; |
| 23 |
ch.exit_function(); |
| 24 |
/* NOTREACHED */ |
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
static const char *get_name(void) { |
| 29 |
return "stdin_check"; |
| 30 |
} |
| 31 |
|
| 32 |
static EXTENSION_DAEMON_DESCRIPTOR descriptor = { |
| 33 |
.get_name = get_name |
| 34 |
}; |
| 35 |
|
| 36 |
MEMCACHED_PUBLIC_API |
| 37 |
EXTENSION_ERROR_CODE memcached_extensions_initialize(const char *config, |
| 38 |
GET_SERVER_API get_server_api) { |
| 39 |
|
| 40 |
SERVER_HANDLE_V1 *server = get_server_api(); |
| 41 |
if (server == NULL) { |
| 42 |
return EXTENSION_FATAL; |
| 43 |
} |
| 44 |
|
| 45 |
if (!server->extension->register_extension(EXTENSION_DAEMON, &descriptor)) { |
| 46 |
return EXTENSION_FATAL; |
| 47 |
} |
| 48 |
|
| 49 |
union c99hack ch = { .exit_function = server->core->shutdown }; |
| 50 |
|
| 51 |
pthread_t t; |
| 52 |
if (pthread_create(&t, NULL, check_stdin_thread, ch.pointer) != 0) { |
| 53 |
perror("couldn't create stdin checking thread."); |
| 54 |
server->extension->unregister_extension(EXTENSION_DAEMON, &descriptor); |
| 55 |
return EXTENSION_FATAL; |
| 56 |
} |
| 57 |
|
| 58 |
return EXTENSION_SUCCESS; |
| 59 |
} |