[1f2dd20] | 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>
|
---|
[b52dd1de] | 41 | #include <io/logctl.h>
|
---|
[1f2dd20] | 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 |
|
---|
[494f417] | 50 | static int handle_namespace_level_change(sysarg_t new_level)
|
---|
| 51 | {
|
---|
| 52 | void *namespace_name;
|
---|
| 53 | int rc = async_data_write_accept(&namespace_name, true, 0, 0, 0, NULL);
|
---|
| 54 | if (rc != EOK) {
|
---|
| 55 | return rc;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | logging_namespace_t *namespace = namespace_writer_attach((const char *) namespace_name);
|
---|
| 59 | free(namespace_name);
|
---|
| 60 | if (namespace == NULL)
|
---|
| 61 | return ENOENT;
|
---|
| 62 |
|
---|
| 63 | rc = namespace_change_level(namespace, (log_level_t) new_level);
|
---|
| 64 | namespace_writer_detach(namespace);
|
---|
| 65 |
|
---|
| 66 | return rc;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[669f5cae] | 69 | static void connection_handler_control(void)
|
---|
| 70 | {
|
---|
| 71 | printf(NAME "/control: new client.\n");
|
---|
| 72 |
|
---|
| 73 | while (true) {
|
---|
| 74 | ipc_call_t call;
|
---|
| 75 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 76 |
|
---|
| 77 | if (!IPC_GET_IMETHOD(call))
|
---|
| 78 | break;
|
---|
| 79 |
|
---|
| 80 | int rc;
|
---|
| 81 |
|
---|
| 82 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 83 | case LOGGER_CTL_GET_DEFAULT_LEVEL:
|
---|
| 84 | async_answer_1(callid, EOK, get_default_logging_level());
|
---|
| 85 | break;
|
---|
| 86 | case LOGGER_CTL_SET_DEFAULT_LEVEL:
|
---|
| 87 | rc = set_default_logging_level(IPC_GET_ARG1(call));
|
---|
| 88 | async_answer_0(callid, rc);
|
---|
| 89 | break;
|
---|
[494f417] | 90 | case LOGGER_CTL_SET_NAMESPACE_LEVEL:
|
---|
| 91 | rc = handle_namespace_level_change(IPC_GET_ARG1(call));
|
---|
| 92 | async_answer_0(callid, rc);
|
---|
| 93 | break;
|
---|
[669f5cae] | 94 | default:
|
---|
| 95 | async_answer_0(callid, EINVAL);
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | printf(NAME "/control: client terminated.\n");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[ef5be68] | 103 | static logging_namespace_t *find_namespace_and_attach_writer(void)
|
---|
[1f2dd20] | 104 | {
|
---|
| 105 | ipc_call_t call;
|
---|
| 106 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 107 |
|
---|
| 108 | if (IPC_GET_IMETHOD(call) != LOGGER_REGISTER) {
|
---|
| 109 | async_answer_0(callid, EINVAL);
|
---|
| 110 | return NULL;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void *name;
|
---|
| 114 | int rc = async_data_write_accept(&name, true, 1, MAX_NAMESPACE_LENGTH, 0, NULL);
|
---|
| 115 | async_answer_0(callid, rc);
|
---|
| 116 |
|
---|
| 117 | if (rc != EOK) {
|
---|
| 118 | return NULL;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[ef5be68] | 121 | logging_namespace_t *result = namespace_writer_attach((const char *) name);
|
---|
[1f2dd20] | 122 |
|
---|
| 123 | free(name);
|
---|
| 124 |
|
---|
| 125 | return result;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | static int handle_receive_message(logging_namespace_t *namespace, int level)
|
---|
| 129 | {
|
---|
[6e9e12b] | 130 | bool skip_message = !namespace_has_reader(namespace, level);
|
---|
[f6bc83a] | 131 | if (skip_message) {
|
---|
| 132 | /* Abort the actual message buffer transfer. */
|
---|
| 133 | ipc_callid_t callid;
|
---|
| 134 | size_t size;
|
---|
| 135 | int rc = ENAK;
|
---|
| 136 | if (!async_data_write_receive(&callid, &size))
|
---|
| 137 | rc = EINVAL;
|
---|
| 138 |
|
---|
| 139 | async_answer_0(callid, rc);
|
---|
| 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[1f2dd20] | 143 | void *message;
|
---|
| 144 | int rc = async_data_write_accept(&message, true, 0, 0, 0, NULL);
|
---|
| 145 | if (rc != EOK) {
|
---|
| 146 | return rc;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | namespace_add_message(namespace, message, level);
|
---|
| 150 |
|
---|
| 151 | free(message);
|
---|
| 152 |
|
---|
| 153 | return EOK;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | static void connection_handler_sink(logging_namespace_t *namespace)
|
---|
| 157 | {
|
---|
| 158 | printf(NAME "/sink: new client %s.\n", namespace_get_name(namespace));
|
---|
| 159 |
|
---|
| 160 | while (true) {
|
---|
| 161 | ipc_call_t call;
|
---|
| 162 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 163 |
|
---|
| 164 | if (!IPC_GET_IMETHOD(call))
|
---|
| 165 | break;
|
---|
| 166 |
|
---|
| 167 | int rc;
|
---|
| 168 |
|
---|
| 169 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 170 | case LOGGER_MESSAGE:
|
---|
| 171 | rc = handle_receive_message(namespace, IPC_GET_ARG1(call));
|
---|
| 172 | async_answer_0(callid, rc);
|
---|
| 173 | break;
|
---|
| 174 | default:
|
---|
| 175 | async_answer_0(callid, EINVAL);
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | printf(NAME "/sink: client %s terminated.\n", namespace_get_name(namespace));
|
---|
| 181 | namespace_writer_detach(namespace);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | static void connection_handler(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 185 | {
|
---|
| 186 | logger_interface_t iface = IPC_GET_ARG1(*icall);
|
---|
| 187 |
|
---|
| 188 | logging_namespace_t *namespace = NULL;
|
---|
| 189 |
|
---|
| 190 | switch (iface) {
|
---|
[669f5cae] | 191 | case LOGGER_INTERFACE_CONTROL:
|
---|
| 192 | async_answer_0(iid, EOK);
|
---|
| 193 | connection_handler_control();
|
---|
| 194 | break;
|
---|
[1f2dd20] | 195 | case LOGGER_INTERFACE_SINK:
|
---|
| 196 | /* First call has to be the registration. */
|
---|
| 197 | async_answer_0(iid, EOK);
|
---|
[ef5be68] | 198 | namespace = find_namespace_and_attach_writer();
|
---|
[1f2dd20] | 199 | if (namespace == NULL) {
|
---|
| 200 | fprintf(stderr, NAME ": failed to register namespace.\n");
|
---|
| 201 | break;
|
---|
| 202 | }
|
---|
| 203 | connection_handler_sink(namespace);
|
---|
| 204 | break;
|
---|
| 205 | default:
|
---|
| 206 | async_answer_0(iid, EINVAL);
|
---|
| 207 | break;
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | int main(int argc, char *argv[])
|
---|
| 212 | {
|
---|
| 213 | printf(NAME ": HelenOS Logging Service\n");
|
---|
| 214 |
|
---|
[b52dd1de] | 215 | /* Get default logging level from sysinfo (if available). */
|
---|
| 216 | log_level_t boot_logging_level = LVL_NOTE;
|
---|
| 217 | int rc = logctl_get_boot_level(&boot_logging_level);
|
---|
| 218 | if (rc == EOK)
|
---|
| 219 | set_default_logging_level(boot_logging_level);
|
---|
| 220 | else
|
---|
| 221 | printf(NAME ": Warn: failed to get logging level from sysinfo: %s.\n",
|
---|
| 222 | str_error(rc));
|
---|
| 223 |
|
---|
[1f2dd20] | 224 | async_set_client_connection(connection_handler);
|
---|
| 225 |
|
---|
[b52dd1de] | 226 | rc = service_register(SERVICE_LOGGER);
|
---|
[1f2dd20] | 227 | if (rc != EOK) {
|
---|
| 228 | printf(NAME ": failed to register: %s.\n", str_error(rc));
|
---|
| 229 | return -1;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | printf(NAME ": Accepting connections\n");
|
---|
| 233 | async_manager();
|
---|
| 234 |
|
---|
| 235 | /* Never reached */
|
---|
| 236 | return 0;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
| 240 | * @}
|
---|
| 241 | */
|
---|