1 | /*
|
---|
2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
3 | * Copyright (c) 2025 Jiří Zárevúcky
|
---|
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 |
|
---|
30 | /** @addtogroup kio
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <_bits/decls.h>
|
---|
38 | #include <libarch/config.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <async.h>
|
---|
41 | #include <as.h>
|
---|
42 | #include <ddi.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <io/kio.h>
|
---|
46 | #include <sysinfo.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <fibril_synch.h>
|
---|
49 | #include <adt/list.h>
|
---|
50 | #include <adt/prodcons.h>
|
---|
51 | #include <tinput.h>
|
---|
52 | #include <uchar.h>
|
---|
53 | #include <vfs/vfs.h>
|
---|
54 |
|
---|
55 | #define NAME "kio"
|
---|
56 | #define LOG_FNAME "/log/kio"
|
---|
57 |
|
---|
58 | /* Producer/consumer buffers */
|
---|
59 | typedef struct {
|
---|
60 | link_t link;
|
---|
61 | size_t bytes;
|
---|
62 | char *data;
|
---|
63 | } item_t;
|
---|
64 |
|
---|
65 | static prodcons_t pc;
|
---|
66 |
|
---|
67 | /* Notification mutex */
|
---|
68 | static FIBRIL_MUTEX_INITIALIZE(mtx);
|
---|
69 |
|
---|
70 | #define READ_BUFFER_SIZE PAGE_SIZE
|
---|
71 |
|
---|
72 | static size_t current_at;
|
---|
73 | static char read_buffer[READ_BUFFER_SIZE];
|
---|
74 |
|
---|
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.
|
---|
81 | * @param data Pointer to the kernel kio buffer.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | static void producer(size_t bytes, char *data)
|
---|
85 | {
|
---|
86 | item_t *item = malloc(sizeof(item_t));
|
---|
87 | if (item == NULL)
|
---|
88 | return;
|
---|
89 |
|
---|
90 | item->bytes = bytes;
|
---|
91 | item->data = malloc(bytes);
|
---|
92 | if (!item->data) {
|
---|
93 | free(item);
|
---|
94 | return;
|
---|
95 | }
|
---|
96 |
|
---|
97 | memcpy(item->data, data, bytes);
|
---|
98 |
|
---|
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 | */
|
---|
114 | static errno_t consumer(void *data)
|
---|
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));
|
---|
120 |
|
---|
121 | while (true) {
|
---|
122 | link_t *link = prodcons_consume(&pc);
|
---|
123 | item_t *item = list_get_instance(link, item_t, link);
|
---|
124 |
|
---|
125 | fwrite(item->data, 1, item->bytes, stdout);
|
---|
126 |
|
---|
127 | if (log) {
|
---|
128 | fwrite(item->data, 1, item->bytes, log);
|
---|
129 | fflush(log);
|
---|
130 | vfs_sync(fileno(log));
|
---|
131 | }
|
---|
132 |
|
---|
133 | free(item->data);
|
---|
134 | free(item);
|
---|
135 | }
|
---|
136 |
|
---|
137 | fclose(log);
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Kernel notification handler
|
---|
142 | *
|
---|
143 | * Receives kernel kio notifications.
|
---|
144 | *
|
---|
145 | * @param call IPC call structure
|
---|
146 | * @param arg Local argument
|
---|
147 | *
|
---|
148 | */
|
---|
149 | static void kio_notification_handler(ipc_call_t *call, void *arg)
|
---|
150 | {
|
---|
151 | size_t kio_written = (size_t) ipc_get_arg1(call);
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Make sure we process only a single notification
|
---|
155 | * at any time to limit the chance of the consumer
|
---|
156 | * starving.
|
---|
157 | */
|
---|
158 |
|
---|
159 | fibril_mutex_lock(&mtx);
|
---|
160 |
|
---|
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;
|
---|
165 |
|
---|
166 | current_at += read;
|
---|
167 |
|
---|
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 | }
|
---|
173 |
|
---|
174 | producer(read, read_buffer);
|
---|
175 | }
|
---|
176 |
|
---|
177 | async_event_unmask(EVENT_KIO);
|
---|
178 | fibril_mutex_unlock(&mtx);
|
---|
179 | }
|
---|
180 |
|
---|
181 | int main(int argc, char *argv[])
|
---|
182 | {
|
---|
183 | prodcons_initialize(&pc);
|
---|
184 | errno_t rc = async_event_subscribe(EVENT_KIO, kio_notification_handler, NULL);
|
---|
185 | if (rc != EOK) {
|
---|
186 | fprintf(stderr, "%s: Unable to register kio notifications\n",
|
---|
187 | NAME);
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
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 | }
|
---|
197 |
|
---|
198 | tinput_t *input = tinput_new();
|
---|
199 | if (!input) {
|
---|
200 | fprintf(stderr, "%s: Could not create input\n", NAME);
|
---|
201 | return ENOMEM;
|
---|
202 | }
|
---|
203 |
|
---|
204 | fibril_add_ready(fid);
|
---|
205 | async_event_unmask(EVENT_KIO);
|
---|
206 | kio_update();
|
---|
207 |
|
---|
208 | tinput_set_prompt(input, "kio> ");
|
---|
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 |
|
---|
217 | kio_command(str, str_size(str));
|
---|
218 | free(str);
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (rc == ENOENT)
|
---|
222 | rc = EOK;
|
---|
223 |
|
---|
224 | return EOK;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /** @}
|
---|
228 | */
|
---|