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