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