|
| 1 | +/* vim: set expandtab ts=4 sw=4: */ |
| 2 | +/* |
| 3 | + * You may redistribute this program and/or modify it under the terms of |
| 4 | + * the GNU General Public License as published by the Free Software Foundation, |
| 5 | + * either version 3 of the License, or (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | +#include <unistd.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <stdio.h> |
| 18 | +#include <errno.h> |
| 19 | +#include <sys/types.h> |
| 20 | +#include <sysexits.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +#include "util/Hex.h" |
| 24 | +#include "memory/Allocator.h" |
| 25 | +#include "memory/MallocAllocator.h" |
| 26 | +#include "crypto/random/Random.h" |
| 27 | +#include "io/FileReader.h" |
| 28 | +#include "io/FileWriter.h" |
| 29 | +#include "util/CString.h" |
| 30 | +#include "util/Assert.h" |
| 31 | +#include "util/log/Log.h" |
| 32 | +#include "util/log/FileWriterLog.h" |
| 33 | +#include "util/events/Time.h" |
| 34 | +#include "util/events/EventBase.h" |
| 35 | +#include "util/events/Pipe.h" |
| 36 | +#include "util/events/Process.h" |
| 37 | +#include "admin/angel/InterfaceWaiter.h" |
| 38 | + |
| 39 | +static void onCoreExit(int64_t exit_status, int term_signal) |
| 40 | +{ |
| 41 | + Assert_failure("Core exited with status [%d], signal [%d]\n", (int)exit_status, term_signal); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Usage: |
| 46 | + * pass an absolute path to cjdroute executable as an argument |
| 47 | + * and this program will act as inetd boostrap, |
| 48 | + * capturing data comming from STDIN and passing it as inital core config |
| 49 | + * of cjdroute core and sending response on STDOUT. |
| 50 | + **/ |
| 51 | + |
| 52 | +int main(int argc, char** argv) |
| 53 | +{ |
| 54 | + if (argc != 2) { |
| 55 | + exit(EX_USAGE); |
| 56 | + } |
| 57 | + char* corePath = argv[1]; |
| 58 | + |
| 59 | + struct Except* eh = NULL; |
| 60 | + |
| 61 | + // Allow it to allocate 1MB |
| 62 | + struct Allocator* allocator = MallocAllocator_new(1<<20); |
| 63 | + struct Log* logger = NULL; // We don't want messages from the trap. |
| 64 | + struct Random* rand = Random_new(allocator, logger, eh); |
| 65 | + struct EventBase* eventBase = EventBase_new(allocator); |
| 66 | + |
| 67 | + struct Writer* stdoutWriter = FileWriter_new(stdout, allocator); |
| 68 | + |
| 69 | + struct Allocator* corePipeAlloc = Allocator_child(allocator); |
| 70 | + char corePipeName[64] = "client-core-"; |
| 71 | + Random_base32(rand, (uint8_t*)corePipeName+CString_strlen(corePipeName), 31); |
| 72 | + Assert_ifParanoid(EventBase_eventCount(eventBase) == 0); |
| 73 | + struct Pipe* corePipe = Pipe_named(corePipeName, eventBase, eh, corePipeAlloc); |
| 74 | + corePipe->logger = logger; |
| 75 | + Assert_ifParanoid(EventBase_eventCount(eventBase) == 2); |
| 76 | + |
| 77 | + struct Message* toCoreMsg = Message_new(0, 1024, allocator); |
| 78 | + unsigned char buff[1024] = { 0 }; |
| 79 | + int len; |
| 80 | + do { |
| 81 | + len = read(STDIN_FILENO, buff, 1024); |
| 82 | + if (len <= 0 && errno != EAGAIN) { |
| 83 | + fprintf(stderr, "Read returned: %d with errno %s.\n", len, strerror(errno)); |
| 84 | + exit(EX_NOINPUT); |
| 85 | + } |
| 86 | + // read will return -1 and set errno if there is nonblocking pipe. |
| 87 | + } while (len == -1 && errno == EAGAIN); |
| 88 | + |
| 89 | + Message_push(toCoreMsg, buff, len, eh); |
| 90 | + |
| 91 | + char* args[] = { "core", corePipeName, NULL }; |
| 92 | + |
| 93 | + Process_spawn(corePath, args, eventBase, allocator, onCoreExit); |
| 94 | + |
| 95 | + |
| 96 | + Iface_CALL(corePipe->iface.send, toCoreMsg, &corePipe->iface); |
| 97 | + |
| 98 | + struct Message* fromCoreMsg = |
| 99 | + InterfaceWaiter_waitForData(&corePipe->iface, eventBase, allocator, eh); |
| 100 | + Writer_write(stdoutWriter, fromCoreMsg->bytes, fromCoreMsg->length); |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
0 commit comments