1 | /*
|
---|
2 | * Copyright (c) 2012 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @defgroup logger Logging service.
|
---|
31 | * @brief HelenOS logging service.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 |
|
---|
35 | /** @file
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <ipc/services.h>
|
---|
39 | #include <ipc/logger.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <io/logctl.h>
|
---|
42 | #include <ns.h>
|
---|
43 | #include <async.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <errno.h>
|
---|
46 | #include <str_error.h>
|
---|
47 | #include <malloc.h>
|
---|
48 | #include "logger.h"
|
---|
49 |
|
---|
50 | static void connection_handler_control(void)
|
---|
51 | {
|
---|
52 | printf(NAME "/control: new client.\n");
|
---|
53 |
|
---|
54 | while (true) {
|
---|
55 | ipc_call_t call;
|
---|
56 | ipc_callid_t callid = async_get_call(&call);
|
---|
57 |
|
---|
58 | if (!IPC_GET_IMETHOD(call))
|
---|
59 | break;
|
---|
60 |
|
---|
61 | int rc;
|
---|
62 |
|
---|
63 | switch (IPC_GET_IMETHOD(call)) {
|
---|
64 | case LOGGER_CTL_GET_DEFAULT_LEVEL:
|
---|
65 | async_answer_1(callid, EOK, get_default_logging_level());
|
---|
66 | break;
|
---|
67 | case LOGGER_CTL_SET_DEFAULT_LEVEL:
|
---|
68 | rc = set_default_logging_level(IPC_GET_ARG1(call));
|
---|
69 | async_answer_0(callid, rc);
|
---|
70 | break;
|
---|
71 | default:
|
---|
72 | async_answer_0(callid, EINVAL);
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | printf(NAME "/control: client terminated.\n");
|
---|
78 | }
|
---|
79 |
|
---|
80 | static logging_namespace_t *find_namespace_and_attach_writer(void)
|
---|
81 | {
|
---|
82 | ipc_call_t call;
|
---|
83 | ipc_callid_t callid = async_get_call(&call);
|
---|
84 |
|
---|
85 | if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
|
---|
86 | async_answer_0(callid, EINVAL);
|
---|
87 | return NULL;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void *name;
|
---|
91 | int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
|
---|
92 | async_answer_0(callid, rc);
|
---|
93 |
|
---|
94 | if (rc != EOK) {
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | logging_namespace_t *result = namespace_writer_attach((const char *) name);
|
---|
99 |
|
---|
100 | free(name);
|
---|
101 |
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int handle_receive_message(logging_namespace_t *namespace, int level)
|
---|
106 | {
|
---|
107 | bool skip_message = !namespace_has_reader(namespace, level);
|
---|
108 | if (skip_message) {
|
---|
109 | /* Abort the actual message buffer transfer. */
|
---|
110 | ipc_callid_t callid;
|
---|
111 | size_t size;
|
---|
112 | int rc = ENAK;
|
---|
113 | if (!async_data_write_receive(&callid, &size))
|
---|
114 | rc = EINVAL;
|
---|
115 |
|
---|
116 | async_answer_0(callid, rc);
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void *message;
|
---|
121 | int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
|
---|
122 | if (rc != EOK) {
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | namespace_add_message(namespace, message, level);
|
---|
127 |
|
---|
128 | free(message);
|
---|
129 |
|
---|
130 | return EOK;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void connection_handler_sink(logging_namespace_t *namespace)
|
---|
134 | {
|
---|
135 | printf(NAME "/sink: new client %s.\n", namespace_get_name(namespace));
|
---|
136 |
|
---|
137 | while (true) {
|
---|
138 | ipc_call_t call;
|
---|
139 | ipc_callid_t callid = async_get_call(&call);
|
---|
140 |
|
---|
141 | if (!IPC_GET_IMETHOD(call))
|
---|
142 | break;
|
---|
143 |
|
---|
144 | int rc;
|
---|
145 |
|
---|
146 | switch (IPC_GET_IMETHOD(call)) {
|
---|
147 | case LOGGER_MESSAGE:
|
---|
148 | rc = handle_receive_message(namespace, IPC_GET_ARG1(call));
|
---|
149 | async_answer_0(callid, rc);
|
---|
150 | break;
|
---|
151 | default:
|
---|
152 | async_answer_0(callid, EINVAL);
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
|
---|
158 | namespace_writer_detach(namespace);
|
---|
159 | }
|
---|
160 |
|
---|
161 | static void connection_handler(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
162 | {
|
---|
163 | logger_interface_t iface = IPC_GET_ARG1(*icall);
|
---|
164 |
|
---|
165 | logging_namespace_t *namespace = NULL;
|
---|
166 |
|
---|
167 | switch (iface) {
|
---|
168 | case LOGGER_INTERFACE_CONTROL:
|
---|
169 | async_answer_0(iid, EOK);
|
---|
170 | connection_handler_control();
|
---|
171 | break;
|
---|
172 | case LOGGER_INTERFACE_SINK:
|
---|
173 | /* First call has to be the registration. */
|
---|
174 | async_answer_0(iid, EOK);
|
---|
175 | namespace = find_namespace_and_attach_writer();
|
---|
176 | if (namespace == NULL) {
|
---|
177 | fprintf(stderr, NAME ": failed to register namespace.\n");
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | connection_handler_sink(namespace);
|
---|
181 | break;
|
---|
182 | default:
|
---|
183 | async_answer_0(iid, EINVAL);
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | int main(int argc, char *argv[])
|
---|
189 | {
|
---|
190 | printf(NAME ": HelenOS Logging Service\n");
|
---|
191 |
|
---|
192 | /* Get default logging level from sysinfo (if available). */
|
---|
193 | log_level_t boot_logging_level = LVL_NOTE;
|
---|
194 | int rc = logctl_get_boot_level(&boot_logging_level);
|
---|
195 | if (rc == EOK)
|
---|
196 | set_default_logging_level(boot_logging_level);
|
---|
197 | else
|
---|
198 | printf(NAME ": Warn: failed to get logging level from sysinfo: %s.\n",
|
---|
199 | str_error(rc));
|
---|
200 |
|
---|
201 | async_set_client_connection(connection_handler);
|
---|
202 |
|
---|
203 | rc = service_register(SERVICE_LOGGER);
|
---|
204 | if (rc != EOK) {
|
---|
205 | printf(NAME ": failed to register: %s.\n", str_error(rc));
|
---|
206 | return -1;
|
---|
207 | }
|
---|
208 |
|
---|
209 | printf(NAME ": Accepting connections\n");
|
---|
210 | async_manager();
|
---|
211 |
|
---|
212 | /* Never reached */
|
---|
213 | return 0;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * @}
|
---|
218 | */
|
---|