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 <ns.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <str_error.h>
|
---|
46 | #include <malloc.h>
|
---|
47 | #include "logger.h"
|
---|
48 |
|
---|
49 | static void connection_handler_control(void)
|
---|
50 | {
|
---|
51 | printf(NAME "/control: new client.\n");
|
---|
52 |
|
---|
53 | while (true) {
|
---|
54 | ipc_call_t call;
|
---|
55 | ipc_callid_t callid = async_get_call(&call);
|
---|
56 |
|
---|
57 | if (!IPC_GET_IMETHOD(call))
|
---|
58 | break;
|
---|
59 |
|
---|
60 | int rc;
|
---|
61 |
|
---|
62 | switch (IPC_GET_IMETHOD(call)) {
|
---|
63 | case LOGGER_CTL_GET_DEFAULT_LEVEL:
|
---|
64 | async_answer_1(callid, EOK, get_default_logging_level());
|
---|
65 | break;
|
---|
66 | case LOGGER_CTL_SET_DEFAULT_LEVEL:
|
---|
67 | rc = set_default_logging_level(IPC_GET_ARG1(call));
|
---|
68 | async_answer_0(callid, rc);
|
---|
69 | break;
|
---|
70 | default:
|
---|
71 | async_answer_0(callid, EINVAL);
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | printf(NAME "/control: client terminated.\n");
|
---|
77 | }
|
---|
78 |
|
---|
79 | static logging_namespace_t *find_namespace_and_attach_writer(void)
|
---|
80 | {
|
---|
81 | ipc_call_t call;
|
---|
82 | ipc_callid_t callid = async_get_call(&call);
|
---|
83 |
|
---|
84 | if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
|
---|
85 | async_answer_0(callid, EINVAL);
|
---|
86 | return NULL;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void *name;
|
---|
90 | int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
|
---|
91 | async_answer_0(callid, rc);
|
---|
92 |
|
---|
93 | if (rc != EOK) {
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | logging_namespace_t *result = namespace_writer_attach((const char *) name);
|
---|
98 |
|
---|
99 | free(name);
|
---|
100 |
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 |
|
---|
104 | static int handle_receive_message(logging_namespace_t *namespace, int level)
|
---|
105 | {
|
---|
106 | bool skip_message = (level > (int)get_default_logging_level()) && !namespace_has_reader(namespace, level);
|
---|
107 | if (skip_message) {
|
---|
108 | /* Abort the actual message buffer transfer. */
|
---|
109 | ipc_callid_t callid;
|
---|
110 | size_t size;
|
---|
111 | int rc = ENAK;
|
---|
112 | if (!async_data_write_receive(&callid, &size))
|
---|
113 | rc = EINVAL;
|
---|
114 |
|
---|
115 | async_answer_0(callid, rc);
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void *message;
|
---|
120 | int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
|
---|
121 | if (rc != EOK) {
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 |
|
---|
125 | namespace_add_message(namespace, message, level);
|
---|
126 |
|
---|
127 | free(message);
|
---|
128 |
|
---|
129 | return EOK;
|
---|
130 | }
|
---|
131 |
|
---|
132 | static void connection_handler_sink(logging_namespace_t *namespace)
|
---|
133 | {
|
---|
134 | printf(NAME "/sink: new client %s.\n", namespace_get_name(namespace));
|
---|
135 |
|
---|
136 | while (true) {
|
---|
137 | ipc_call_t call;
|
---|
138 | ipc_callid_t callid = async_get_call(&call);
|
---|
139 |
|
---|
140 | if (!IPC_GET_IMETHOD(call))
|
---|
141 | break;
|
---|
142 |
|
---|
143 | int rc;
|
---|
144 |
|
---|
145 | switch (IPC_GET_IMETHOD(call)) {
|
---|
146 | case LOGGER_MESSAGE:
|
---|
147 | rc = handle_receive_message(namespace, IPC_GET_ARG1(call));
|
---|
148 | async_answer_0(callid, rc);
|
---|
149 | break;
|
---|
150 | default:
|
---|
151 | async_answer_0(callid, EINVAL);
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
|
---|
157 | namespace_writer_detach(namespace);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | static logging_namespace_t *find_namespace_and_attach_reader(void)
|
---|
162 | {
|
---|
163 | ipc_call_t call;
|
---|
164 | ipc_callid_t callid = async_get_call(&call);
|
---|
165 |
|
---|
166 | if (IPC_GET_IMETHOD(call) != LOGGER_CONNECT) {
|
---|
167 | async_answer_0(callid, EINVAL);
|
---|
168 | return NULL;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void *name;
|
---|
172 | int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
|
---|
173 |
|
---|
174 | if (rc != EOK) {
|
---|
175 | async_answer_0(callid, rc);
|
---|
176 | return NULL;
|
---|
177 | }
|
---|
178 |
|
---|
179 | logging_namespace_t *result = namespace_reader_attach((const char *) name);
|
---|
180 | if (result == NULL) {
|
---|
181 | rc = ENOENT;
|
---|
182 | }
|
---|
183 |
|
---|
184 | async_answer_0(callid, rc);
|
---|
185 |
|
---|
186 | free(name);
|
---|
187 |
|
---|
188 | return result;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | static int handle_send_message(logging_namespace_t *namespace, int *level)
|
---|
193 | {
|
---|
194 | ipc_callid_t callid;
|
---|
195 | size_t size;
|
---|
196 | bool expects_data_read = async_data_read_receive(&callid, &size);
|
---|
197 | if (!expects_data_read) {
|
---|
198 | return EINVAL;
|
---|
199 | }
|
---|
200 |
|
---|
201 | log_message_t *message = namespace_get_next_message(namespace);
|
---|
202 | size_t message_len = str_size(message->message) + 1;
|
---|
203 |
|
---|
204 | if (size > message_len) {
|
---|
205 | size = message_len;
|
---|
206 | }
|
---|
207 |
|
---|
208 | async_data_read_finalize(callid, message->message, size);
|
---|
209 |
|
---|
210 | *level = (int) message->level;
|
---|
211 | message_destroy(message);
|
---|
212 |
|
---|
213 | return EOK;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | static void connection_handler_source(logging_namespace_t *namespace)
|
---|
218 | {
|
---|
219 | printf(NAME "/source: new client for %s.\n", namespace_get_name(namespace));
|
---|
220 |
|
---|
221 | while (true) {
|
---|
222 | ipc_call_t call;
|
---|
223 | ipc_callid_t callid = async_get_call(&call);
|
---|
224 |
|
---|
225 | if (!IPC_GET_IMETHOD(call))
|
---|
226 | break;
|
---|
227 |
|
---|
228 | int rc;
|
---|
229 | int message_level = 0;
|
---|
230 |
|
---|
231 | switch (IPC_GET_IMETHOD(call)) {
|
---|
232 | case LOGGER_GET_MESSAGE:
|
---|
233 | rc = handle_send_message(namespace, &message_level);
|
---|
234 | async_answer_1(callid, rc, message_level);
|
---|
235 | break;
|
---|
236 | default:
|
---|
237 | async_answer_0(callid, EINVAL);
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | printf(NAME "/source: client %s terminated.\n", namespace_get_name(namespace));
|
---|
243 | namespace_reader_detach(namespace);
|
---|
244 | }
|
---|
245 |
|
---|
246 | static void connection_handler(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
247 | {
|
---|
248 | logger_interface_t iface = IPC_GET_ARG1(*icall);
|
---|
249 |
|
---|
250 | logging_namespace_t *namespace = NULL;
|
---|
251 |
|
---|
252 | switch (iface) {
|
---|
253 | case LOGGER_INTERFACE_CONTROL:
|
---|
254 | async_answer_0(iid, EOK);
|
---|
255 | connection_handler_control();
|
---|
256 | break;
|
---|
257 | case LOGGER_INTERFACE_SINK:
|
---|
258 | /* First call has to be the registration. */
|
---|
259 | async_answer_0(iid, EOK);
|
---|
260 | namespace = find_namespace_and_attach_writer();
|
---|
261 | if (namespace == NULL) {
|
---|
262 | fprintf(stderr, NAME ": failed to register namespace.\n");
|
---|
263 | break;
|
---|
264 | }
|
---|
265 | connection_handler_sink(namespace);
|
---|
266 | break;
|
---|
267 | case LOGGER_INTERFACE_SOURCE:
|
---|
268 | async_answer_0(iid, EOK);
|
---|
269 | /* First call has to find existing namespace. */
|
---|
270 | namespace = find_namespace_and_attach_reader();
|
---|
271 | if (namespace == NULL) {
|
---|
272 | fprintf(stderr, NAME ": failed to attach client.\n");
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | connection_handler_source(namespace);
|
---|
276 | break;
|
---|
277 | default:
|
---|
278 | async_answer_0(iid, EINVAL);
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | int main(int argc, char *argv[])
|
---|
284 | {
|
---|
285 | printf(NAME ": HelenOS Logging Service\n");
|
---|
286 |
|
---|
287 | async_set_client_connection(connection_handler);
|
---|
288 |
|
---|
289 | int rc = service_register(SERVICE_LOGGER);
|
---|
290 | if (rc != EOK) {
|
---|
291 | printf(NAME ": failed to register: %s.\n", str_error(rc));
|
---|
292 | return -1;
|
---|
293 | }
|
---|
294 |
|
---|
295 | printf(NAME ": Accepting connections\n");
|
---|
296 | async_manager();
|
---|
297 |
|
---|
298 | /* Never reached */
|
---|
299 | return 0;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * @}
|
---|
304 | */
|
---|