source: mainline/uspace/srv/hid/remcons/remcons.c@ acd7ac2

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since acd7ac2 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 9.7 KB
RevLine 
[21a9869]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/** @addtogroup remcons
30 * @{
31 */
32/** @file
33 */
34
35#include <async.h>
36#include <errno.h>
[5d94b16c]37#include <io/con_srv.h>
38#include <stdio.h>
39#include <stdlib.h>
[21a9869]40#include <str_error.h>
41#include <loc.h>
42#include <io/keycode.h>
43#include <align.h>
44#include <fibril_synch.h>
45#include <task.h>
[fab2746]46#include <inet/addr.h>
47#include <inet/endpoint.h>
48#include <inet/tcp.h>
[21a9869]49#include <io/console.h>
50#include <inttypes.h>
[1d6dd2a]51#include <str.h>
[3806317]52#include "telnet.h"
[30d4706]53#include "user.h"
[21a9869]54
55#define APP_GETTERM "/app/getterm"
[03e0a244]56#define APP_SHELL "/app/bdsh"
[5576358]57
[3806317]58/** Telnet commands to force character mode
59 * (redundant to be on the safe side).
60 * See
[787b65b]61 * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
[3806317]62 * for discussion.
63 */
64static const telnet_cmd_t telnet_force_character_mode_command[] = {
65 TELNET_IAC, TELNET_WILL, TELNET_ECHO,
66 TELNET_IAC, TELNET_WILL, TELNET_SUPPRESS_GO_AHEAD,
67 TELNET_IAC, TELNET_WONT, TELNET_LINEMODE
68};
[3123d2a]69
[3806317]70static const size_t telnet_force_character_mode_command_count =
71 sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
72
[b7fd2a0]73static errno_t remcons_open(con_srvs_t *, con_srv_t *);
74static errno_t remcons_close(con_srv_t *);
75static errno_t remcons_write(con_srv_t *, void *, size_t, size_t *);
[5d94b16c]76static void remcons_sync(con_srv_t *);
77static void remcons_clear(con_srv_t *);
78static void remcons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
[b7fd2a0]79static errno_t remcons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
80static errno_t remcons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
81static errno_t remcons_get_color_cap(con_srv_t *, console_caps_t *);
82static errno_t remcons_get_event(con_srv_t *, cons_event_t *);
[5d94b16c]83
84static con_ops_t con_ops = {
85 .open = remcons_open,
86 .close = remcons_close,
87 .read = NULL,
88 .write = remcons_write,
89 .sync = remcons_sync,
90 .clear = remcons_clear,
91 .set_pos = remcons_set_pos,
92 .get_pos = remcons_get_pos,
93 .get_size = remcons_get_size,
94 .get_color_cap = remcons_get_color_cap,
95 .set_style = NULL,
96 .set_color = NULL,
97 .set_rgb_color = NULL,
98 .set_cursor_visibility = NULL,
99 .get_event = remcons_get_event
100};
[21a9869]101
[fab2746]102static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn);
103
104static tcp_listen_cb_t listen_cb = {
105 .new_conn = remcons_new_conn
106};
107
108static tcp_cb_t conn_cb = {
109 .connected = NULL
110};
111
[5d94b16c]112static telnet_user_t *srv_to_user(con_srv_t *srv)
[21a9869]113{
[5d94b16c]114 return srv->srvs->sarg;
115}
[21a9869]116
[b7fd2a0]117static errno_t remcons_open(con_srvs_t *srvs, con_srv_t *srv)
[5d94b16c]118{
119 telnet_user_t *user = srv_to_user(srv);
[99c2e9f3]120
[5d94b16c]121 telnet_user_log(user, "New client connected (%p).", srv);
[21a9869]122
[5d94b16c]123 /* Force character mode. */
[fab2746]124 (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
125 telnet_force_character_mode_command_count);
[21a9869]126
[5d94b16c]127 return EOK;
128}
129
[b7fd2a0]130static errno_t remcons_close(con_srv_t *srv)
[5d94b16c]131{
132 telnet_user_t *user = srv_to_user(srv);
133
134 telnet_user_notify_client_disconnected(user);
135 telnet_user_log(user, "Client disconnected (%p).", srv);
136
137 return EOK;
138}
139
[b7fd2a0]140static errno_t remcons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
[5d94b16c]141{
142 telnet_user_t *user = srv_to_user(srv);
[b7fd2a0]143 errno_t rc;
[5d94b16c]144
145 rc = telnet_user_send_data(user, data, size);
146 if (rc != EOK)
147 return rc;
148
[c8211849]149 *nwritten = size;
150 return EOK;
[5d94b16c]151}
152
153static void remcons_sync(con_srv_t *srv)
154{
155 (void) srv;
156}
157
158static void remcons_clear(con_srv_t *srv)
159{
160 (void) srv;
161}
162
163static void remcons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
164{
165 telnet_user_t *user = srv_to_user(srv);
166
167 telnet_user_update_cursor_x(user, col);
168}
169
[b7fd2a0]170static errno_t remcons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
[5d94b16c]171{
172 telnet_user_t *user = srv_to_user(srv);
173
174 *col = user->cursor_x;
175 *row = 0;
176
177 return EOK;
178}
179
[b7fd2a0]180static errno_t remcons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
[5d94b16c]181{
182 (void) srv;
183
184 *cols = 100;
185 *rows = 1;
186
187 return EOK;
188}
189
[b7fd2a0]190static errno_t remcons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
[5d94b16c]191{
192 (void) srv;
193 *ccaps = CONSOLE_CAP_NONE;
194
195 return EOK;
196}
197
[b7fd2a0]198static errno_t remcons_get_event(con_srv_t *srv, cons_event_t *event)
[5d94b16c]199{
200 telnet_user_t *user = srv_to_user(srv);
[902f0906]201 kbd_event_t kevent;
[b7fd2a0]202 errno_t rc;
[5d94b16c]203
[902f0906]204 rc = telnet_user_get_next_keyboard_event(user, &kevent);
[5d94b16c]205 if (rc != EOK) {
206 /* XXX What? */
207 memset(event, 0, sizeof(*event));
208 return EOK;
[21a9869]209 }
[5d94b16c]210
[902f0906]211 event->type = CEV_KEY;
212 event->ev.key = kevent;
213
[5d94b16c]214 return EOK;
[21a9869]215}
216
[5923cf82]217/** Callback when client connects to a telnet terminal. */
[984a9ba]218static void client_connection(ipc_call_t *icall, void *arg)
[6f7cd5d]219{
[787b65b]220 /* Find the user. */
[fafb8e5]221 telnet_user_t *user = telnet_user_get_for_client_connection(ipc_get_arg2(icall));
[787b65b]222 if (user == NULL) {
[984a9ba]223 async_answer_0(icall, ENOENT);
[6f7cd5d]224 return;
225 }
226
[99c2e9f3]227 /* Handle messages. */
[984a9ba]228 con_conn(icall, &user->srvs);
[6f7cd5d]229}
230
[5923cf82]231/** Fibril for spawning the task running after user connects.
232 *
233 * @param arg Corresponding @c telnet_user_t structure.
234 */
[b7fd2a0]235static errno_t spawn_task_fibril(void *arg)
[21a9869]236{
[787b65b]237 telnet_user_t *user = arg;
[a35b458]238
[21a9869]239 task_id_t task;
[1c635d6]240 task_wait_t wait;
[b7fd2a0]241 errno_t rc = task_spawnl(&task, &wait, APP_GETTERM, APP_GETTERM, user->service_name,
[593e023]242 "/loc", "--msg", "--", APP_SHELL, NULL);
[21a9869]243 if (rc != EOK) {
[593e023]244 telnet_user_error(user, "Spawning `%s %s /loc --msg -- %s' "
245 "failed: %s.", APP_GETTERM, user->service_name, APP_SHELL,
246 str_error(rc));
[261bbdc]247 fibril_mutex_lock(&user->guard);
[787b65b]248 user->task_finished = true;
[5d94b16c]249 user->srvs.aborted = true;
[787b65b]250 fibril_condvar_signal(&user->refcount_cv);
[261bbdc]251 fibril_mutex_unlock(&user->guard);
[21a9869]252 return EOK;
253 }
254
[261bbdc]255 fibril_mutex_lock(&user->guard);
[787b65b]256 user->task_id = task;
[261bbdc]257 fibril_mutex_unlock(&user->guard);
[1870d81]258
[21a9869]259 task_exit_t task_exit;
260 int task_retval;
[1c635d6]261 task_wait(&wait, &task_exit, &task_retval);
[d545d03]262 telnet_user_log(user, "%s terminated %s, exit code %d.", APP_GETTERM,
263 task_exit == TASK_EXIT_NORMAL ? "normally" : "unexpectedly",
264 task_retval);
[21a9869]265
[7c2bb2c]266 /* Announce destruction. */
[261bbdc]267 fibril_mutex_lock(&user->guard);
[787b65b]268 user->task_finished = true;
[5d94b16c]269 user->srvs.aborted = true;
[787b65b]270 fibril_condvar_signal(&user->refcount_cv);
[261bbdc]271 fibril_mutex_unlock(&user->guard);
[7c2bb2c]272
273 return EOK;
274}
275
[5923cf82]276/** Tell whether given user can be destroyed (has no active clients).
277 *
278 * @param user The telnet user in question.
279 */
[787b65b]280static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
[1870d81]281{
[787b65b]282 return user->task_finished && user->socket_closed &&
283 (user->locsrv_connection_count == 0);
[1870d81]284}
[7c2bb2c]285
[d6ff08a0]286/** Handle network connection.
[5923cf82]287 *
[d6ff08a0]288 * @param lst Listener
289 * @param conn Connection
[5923cf82]290 */
[d6ff08a0]291static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
[7c2bb2c]292{
[d6ff08a0]293 telnet_user_t *user = telnet_user_create(conn);
294 assert(user);
295
296 con_srvs_init(&user->srvs);
297 user->srvs.ops = &con_ops;
298 user->srvs.sarg = user;
299 user->srvs.abort_timeout = 1000;
300
301 telnet_user_add(user);
[7c2bb2c]302
[b7fd2a0]303 errno_t rc = loc_service_register(user->service_name, &user->service_id);
[6f7cd5d]304 if (rc != EOK) {
[d545d03]305 telnet_user_error(user, "Unable to register %s with loc: %s.",
306 user->service_name, str_error(rc));
[d6ff08a0]307 return;
[6f7cd5d]308 }
[d545d03]309
310 telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
311 user->service_name, user->service_id);
[d6ff08a0]312
[787b65b]313 fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
[7c2bb2c]314 assert(spawn_fibril);
315 fibril_add_ready(spawn_fibril);
[d6ff08a0]316
[6f7cd5d]317 /* Wait for all clients to exit. */
[261bbdc]318 fibril_mutex_lock(&user->guard);
[787b65b]319 while (!user_can_be_destroyed_no_lock(user)) {
320 if (user->task_finished) {
[fab2746]321 user->conn = NULL;
[787b65b]322 user->socket_closed = true;
[5d94b16c]323 user->srvs.aborted = true;
[1870d81]324 continue;
[787b65b]325 } else if (user->socket_closed) {
326 if (user->task_id != 0) {
327 task_kill(user->task_id);
[1870d81]328 }
329 }
[261bbdc]330 fibril_condvar_wait_timeout(&user->refcount_cv, &user->guard, 1000);
[6f7cd5d]331 }
[261bbdc]332 fibril_mutex_unlock(&user->guard);
[d6ff08a0]333
[787b65b]334 rc = loc_service_unregister(user->service_id);
[7c2bb2c]335 if (rc != EOK) {
[d545d03]336 telnet_user_error(user,
337 "Unable to unregister %s from loc: %s (ignored).",
338 user->service_name, str_error(rc));
[7c2bb2c]339 }
340
[d545d03]341 telnet_user_log(user, "Destroying...");
[787b65b]342 telnet_user_destroy(user);
[fab2746]343}
344
[21a9869]345int main(int argc, char *argv[])
346{
[b7fd2a0]347 errno_t rc;
[fab2746]348 tcp_listener_t *lst;
349 tcp_t *tcp;
350 inet_ep_t ep;
351
[b688fd8]352 async_set_fallback_port_handler(client_connection, NULL);
[fab2746]353 rc = loc_server_register(NAME);
[3123d2a]354 if (rc != EOK) {
355 fprintf(stderr, "%s: Unable to register server\n", NAME);
356 return rc;
[21a9869]357 }
358
[fab2746]359 rc = tcp_create(&tcp);
[d6ff08a0]360 if (rc != EOK) {
361 fprintf(stderr, "%s: Error initializing TCP.\n", NAME);
[fab2746]362 return rc;
[21a9869]363 }
364
[fab2746]365 inet_ep_init(&ep);
[bf7587b0]366 ep.port = 2223;
[21a9869]367
[fab2746]368 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
369 &lst);
[21a9869]370 if (rc != EOK) {
[fab2746]371 fprintf(stderr, "%s: Error creating listener.\n", NAME);
372 return rc;
[21a9869]373 }
374
375 printf("%s: HelenOS Remote console service\n", NAME);
[6d5e378]376 task_retval(0);
[fab2746]377 async_manager();
[21a9869]378
[fab2746]379 /* Not reached */
[21a9869]380 return 0;
381}
382
383/** @}
384 */
Note: See TracBrowser for help on using the repository browser.