| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "libsm.h" |
| 4 | #include "mos-syslog.h" |
| 5 | |
| 6 | #include <librpc/internal.h> |
| 7 | #include <librpc/macro_magic.h> |
| 8 | #include <librpc/rpc.h> |
| 9 | #include <librpc/rpc_server.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | RPC_DECLARE_SERVER(syslogd, SYSLOGD_RPC_X) |
| 16 | |
| 17 | static void syslogd_on_connect(rpc_context_t *context) |
| 18 | { |
| 19 | const char *name = "<unknown>" ; |
| 20 | rpc_context_set_data(context, data: strdup(string: name)); |
| 21 | } |
| 22 | |
| 23 | static void syslogd_on_disconnect(rpc_context_t *context) |
| 24 | { |
| 25 | void *data = rpc_context_get_data(context); |
| 26 | if (data != NULL) |
| 27 | free(pointer: data); // client name |
| 28 | MOS_UNUSED(context); |
| 29 | } |
| 30 | |
| 31 | static rpc_result_code_t syslogd_set_name(rpc_context_t *context, const char *name) |
| 32 | { |
| 33 | MOS_UNUSED(context); |
| 34 | |
| 35 | if (name == NULL) |
| 36 | return RPC_RESULT_INVALID_ARGUMENT; |
| 37 | |
| 38 | printf(format: "syslogd: setting name to '%s'\n" , name); |
| 39 | void *old = rpc_context_set_data(context, data: strdup(string: name)); |
| 40 | if (old != NULL) |
| 41 | free(pointer: old); |
| 42 | return RPC_RESULT_OK; |
| 43 | } |
| 44 | |
| 45 | static rpc_result_code_t syslogd_log(rpc_context_t *context, const char *message) |
| 46 | { |
| 47 | MOS_UNUSED(context); |
| 48 | void *data = rpc_context_get_data(context); |
| 49 | printf(format: "[%s] %s\n" , (char *) data, message); |
| 50 | return RPC_RESULT_OK; |
| 51 | } |
| 52 | |
| 53 | static rpc_result_code_t syslogd_logc(rpc_context_t *context, const char *category, const char *message) |
| 54 | { |
| 55 | MOS_UNUSED(context); |
| 56 | void *data = rpc_context_get_data(context); |
| 57 | printf(format: "[%s] [%s] %s\n" , (char *) data, category, message); |
| 58 | return RPC_RESULT_OK; |
| 59 | } |
| 60 | |
| 61 | int main(int argc, char **argv) |
| 62 | { |
| 63 | MOS_UNUSED(argc); |
| 64 | MOS_UNUSED(argv); |
| 65 | |
| 66 | puts(string: "syslogd: starting" ); |
| 67 | rpc_server_t *const server = rpc_server_create(SYSLOGD_SERVICE_NAME, NULL); |
| 68 | rpc_server_set_on_connect(server, on_connect: syslogd_on_connect); |
| 69 | rpc_server_set_on_disconnect(server, on_disconnect: syslogd_on_disconnect); |
| 70 | rpc_server_register_functions(server, functions: syslogd_functions, count: MOS_ARRAY_SIZE(syslogd_functions)); |
| 71 | |
| 72 | ReportServiceState(status: UnitStatus::Started, message: "syslogd started" ); |
| 73 | rpc_server_exec(server); |
| 74 | fputs(string: "syslogd: server exited\n" , stream: stderr); |
| 75 | return 0; |
| 76 | } |
| 77 | |