[51dbadf3] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[d5b37b6] | 3 | * Copyright (c) 2025 Jiří Zárevúcky
|
---|
[51dbadf3] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[a63966d] | 30 | /** @addtogroup kio
|
---|
[b2951e2] | 31 | * @{
|
---|
[d8fcfc0] | 32 | */
|
---|
[b2951e2] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[d5b37b6] | 37 | #include <_bits/decls.h>
|
---|
| 38 | #include <libarch/config.h>
|
---|
[51dbadf3] | 39 | #include <stdio.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <as.h>
|
---|
[6119f24] | 42 | #include <ddi.h>
|
---|
[1c03c17] | 43 | #include <errno.h>
|
---|
[5a9f4d7] | 44 | #include <str_error.h>
|
---|
[6fa9a99d] | 45 | #include <io/kio.h>
|
---|
[6119f24] | 46 | #include <sysinfo.h>
|
---|
[38d150e] | 47 | #include <stdlib.h>
|
---|
[4e1a2f5] | 48 | #include <fibril_synch.h>
|
---|
| 49 | #include <adt/list.h>
|
---|
| 50 | #include <adt/prodcons.h>
|
---|
[297cb73] | 51 | #include <tinput.h>
|
---|
[d5b37b6] | 52 | #include <uchar.h>
|
---|
[a56cef9] | 53 | #include <vfs/vfs.h>
|
---|
[1c03c17] | 54 |
|
---|
[6fa9a99d] | 55 | #define NAME "kio"
|
---|
| 56 | #define LOG_FNAME "/log/kio"
|
---|
[1c03c17] | 57 |
|
---|
[4e1a2f5] | 58 | /* Producer/consumer buffers */
|
---|
| 59 | typedef struct {
|
---|
| 60 | link_t link;
|
---|
[690ad20] | 61 | size_t bytes;
|
---|
| 62 | char *data;
|
---|
[4e1a2f5] | 63 | } item_t;
|
---|
| 64 |
|
---|
| 65 | static prodcons_t pc;
|
---|
| 66 |
|
---|
| 67 | /* Notification mutex */
|
---|
| 68 | static FIBRIL_MUTEX_INITIALIZE(mtx);
|
---|
[5a9f4d7] | 69 |
|
---|
[690ad20] | 70 | #define READ_BUFFER_SIZE PAGE_SIZE
|
---|
[d5b37b6] | 71 |
|
---|
| 72 | static size_t current_at;
|
---|
[690ad20] | 73 | static char read_buffer[READ_BUFFER_SIZE];
|
---|
[af77459] | 74 |
|
---|
[4e1a2f5] | 75 | /** Klog producer
|
---|
| 76 | *
|
---|
| 77 | * Copies the contents of a character buffer to local
|
---|
| 78 | * producer/consumer queue.
|
---|
| 79 | *
|
---|
| 80 | * @param length Number of characters to copy.
|
---|
[6fa9a99d] | 81 | * @param data Pointer to the kernel kio buffer.
|
---|
[4e1a2f5] | 82 | *
|
---|
| 83 | */
|
---|
[690ad20] | 84 | static void producer(size_t bytes, char *data)
|
---|
[51dbadf3] | 85 | {
|
---|
[690ad20] | 86 | item_t *item = malloc(sizeof(item_t));
|
---|
[4e1a2f5] | 87 | if (item == NULL)
|
---|
| 88 | return;
|
---|
[a35b458] | 89 |
|
---|
[690ad20] | 90 | item->bytes = bytes;
|
---|
| 91 | item->data = malloc(bytes);
|
---|
| 92 | if (!item->data) {
|
---|
[4e1a2f5] | 93 | free(item);
|
---|
| 94 | return;
|
---|
| 95 | }
|
---|
[a35b458] | 96 |
|
---|
[690ad20] | 97 | memcpy(item->data, data, bytes);
|
---|
[a35b458] | 98 |
|
---|
[4e1a2f5] | 99 | link_initialize(&item->link);
|
---|
| 100 | prodcons_produce(&pc, &item->link);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /** Klog consumer
|
---|
| 104 | *
|
---|
| 105 | * Waits in an infinite loop for the character data created by
|
---|
| 106 | * the producer and outputs them to stdout and optionally into
|
---|
| 107 | * a file.
|
---|
| 108 | *
|
---|
| 109 | * @param data Unused.
|
---|
| 110 | *
|
---|
| 111 | * @return Always EOK (unreachable).
|
---|
| 112 | *
|
---|
| 113 | */
|
---|
[b7fd2a0] | 114 | static errno_t consumer(void *data)
|
---|
[4e1a2f5] | 115 | {
|
---|
| 116 | FILE *log = fopen(LOG_FNAME, "a");
|
---|
| 117 | if (log == NULL)
|
---|
| 118 | printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
|
---|
| 119 | str_error(errno));
|
---|
[a35b458] | 120 |
|
---|
[4e1a2f5] | 121 | while (true) {
|
---|
| 122 | link_t *link = prodcons_consume(&pc);
|
---|
| 123 | item_t *item = list_get_instance(link, item_t, link);
|
---|
[a35b458] | 124 |
|
---|
[690ad20] | 125 | fwrite(item->data, 1, item->bytes, stdout);
|
---|
[a35b458] | 126 |
|
---|
[690ad20] | 127 | if (log) {
|
---|
| 128 | fwrite(item->data, 1, item->bytes, log);
|
---|
[4e1a2f5] | 129 | fflush(log);
|
---|
[a56cef9] | 130 | vfs_sync(fileno(log));
|
---|
[4e1a2f5] | 131 | }
|
---|
[a35b458] | 132 |
|
---|
[4e1a2f5] | 133 | free(item->data);
|
---|
| 134 | free(item);
|
---|
[5a9f4d7] | 135 | }
|
---|
[a35b458] | 136 |
|
---|
[4e1a2f5] | 137 | fclose(log);
|
---|
| 138 | return EOK;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Kernel notification handler
|
---|
| 142 | *
|
---|
[6fa9a99d] | 143 | * Receives kernel kio notifications.
|
---|
[4e1a2f5] | 144 | *
|
---|
[3815efb] | 145 | * @param call IPC call structure
|
---|
| 146 | * @param arg Local argument
|
---|
[4e1a2f5] | 147 | *
|
---|
| 148 | */
|
---|
[01c3bb4] | 149 | static void kio_notification_handler(ipc_call_t *call, void *arg)
|
---|
[4e1a2f5] | 150 | {
|
---|
[d5b37b6] | 151 | size_t kio_written = (size_t) ipc_get_arg1(call);
|
---|
| 152 |
|
---|
[4e1a2f5] | 153 | /*
|
---|
| 154 | * Make sure we process only a single notification
|
---|
| 155 | * at any time to limit the chance of the consumer
|
---|
| 156 | * starving.
|
---|
| 157 | */
|
---|
[a35b458] | 158 |
|
---|
[4e1a2f5] | 159 | fibril_mutex_lock(&mtx);
|
---|
[a35b458] | 160 |
|
---|
[d5b37b6] | 161 | while (current_at != kio_written) {
|
---|
| 162 | size_t read = kio_read(read_buffer, READ_BUFFER_SIZE, current_at);
|
---|
| 163 | if (read == 0)
|
---|
| 164 | break;
|
---|
[a35b458] | 165 |
|
---|
[d5b37b6] | 166 | current_at += read;
|
---|
[a35b458] | 167 |
|
---|
[d5b37b6] | 168 | if (read > READ_BUFFER_SIZE) {
|
---|
| 169 | /* We missed some data. */
|
---|
| 170 | // TODO: Send a message with the number of lost characters to the consumer.
|
---|
| 171 | read = READ_BUFFER_SIZE;
|
---|
| 172 | }
|
---|
[a35b458] | 173 |
|
---|
[d5b37b6] | 174 | producer(read, read_buffer);
|
---|
[af77459] | 175 | }
|
---|
[a35b458] | 176 |
|
---|
[8820544] | 177 | async_event_unmask(EVENT_KIO);
|
---|
[4e1a2f5] | 178 | fibril_mutex_unlock(&mtx);
|
---|
[51dbadf3] | 179 | }
|
---|
| 180 |
|
---|
| 181 | int main(int argc, char *argv[])
|
---|
| 182 | {
|
---|
[4e1a2f5] | 183 | prodcons_initialize(&pc);
|
---|
[d5b37b6] | 184 | errno_t rc = async_event_subscribe(EVENT_KIO, kio_notification_handler, NULL);
|
---|
[6119f24] | 185 | if (rc != EOK) {
|
---|
[6fa9a99d] | 186 | fprintf(stderr, "%s: Unable to register kio notifications\n",
|
---|
[6119f24] | 187 | NAME);
|
---|
| 188 | return rc;
|
---|
[05641a9e] | 189 | }
|
---|
[a35b458] | 190 |
|
---|
[4e1a2f5] | 191 | fid_t fid = fibril_create(consumer, NULL);
|
---|
| 192 | if (!fid) {
|
---|
| 193 | fprintf(stderr, "%s: Unable to create consumer fibril\n",
|
---|
| 194 | NAME);
|
---|
| 195 | return ENOMEM;
|
---|
| 196 | }
|
---|
[a35b458] | 197 |
|
---|
[297cb73] | 198 | tinput_t *input = tinput_new();
|
---|
| 199 | if (!input) {
|
---|
| 200 | fprintf(stderr, "%s: Could not create input\n", NAME);
|
---|
| 201 | return ENOMEM;
|
---|
[8820544] | 202 | }
|
---|
[297cb73] | 203 |
|
---|
[4e1a2f5] | 204 | fibril_add_ready(fid);
|
---|
[8820544] | 205 | async_event_unmask(EVENT_KIO);
|
---|
[6fa9a99d] | 206 | kio_update();
|
---|
[a35b458] | 207 |
|
---|
[6fa9a99d] | 208 | tinput_set_prompt(input, "kio> ");
|
---|
[297cb73] | 209 |
|
---|
| 210 | char *str;
|
---|
| 211 | while ((rc = tinput_read(input, &str)) == EOK) {
|
---|
| 212 | if (str_cmp(str, "") == 0) {
|
---|
| 213 | free(str);
|
---|
| 214 | continue;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[6fa9a99d] | 217 | kio_command(str, str_size(str));
|
---|
[297cb73] | 218 | free(str);
|
---|
| 219 | }
|
---|
[8820544] | 220 |
|
---|
[297cb73] | 221 | if (rc == ENOENT)
|
---|
[8820544] | 222 | rc = EOK;
|
---|
[297cb73] | 223 |
|
---|
| 224 | return EOK;
|
---|
[51dbadf3] | 225 | }
|
---|
[b2951e2] | 226 |
|
---|
| 227 | /** @}
|
---|
| 228 | */
|
---|