source: mainline/uspace/srv/hid/remcons/user.c@ 5923cf82

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5923cf82 was 5923cf82, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

remcons: add comments

  • Property mode set to 100644
File size: 4.6 KB
Line 
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#include <async.h>
35#include <stdio.h>
36#include <adt/prodcons.h>
37#include <ipc/input.h>
38#include <ipc/console.h>
39#include <ipc/vfs.h>
40#include <errno.h>
41#include <str_error.h>
42#include <loc.h>
43#include <event.h>
44#include <io/keycode.h>
45#include <align.h>
46#include <malloc.h>
47#include <as.h>
48#include <fibril_synch.h>
49#include <task.h>
50#include <net/in.h>
51#include <net/inet.h>
52#include <net/socket.h>
53#include <io/console.h>
54#include <inttypes.h>
55#include <assert.h>
56#include "user.h"
57
58static FIBRIL_MUTEX_INITIALIZE(users_guard);
59static LIST_INITIALIZE(users);
60
61/** Create new telnet user.
62 *
63 * @param socket Socket the user communicates through.
64 * @return New telnet user or NULL when out of memory.
65 */
66telnet_user_t *telnet_user_create(int socket)
67{
68 static int telnet_user_id_counter = 0;
69
70 telnet_user_t *user = malloc(sizeof(telnet_user_t));
71 if (user == NULL) {
72 return NULL;
73 }
74
75 user->id = ++telnet_user_id_counter;
76
77 int rc = asprintf(&user->service_name, "%s/telnet%d", NAMESPACE, user->id);
78 if (rc < 0) {
79 free(user);
80 return NULL;
81 }
82
83 user->socket = socket;
84 user->service_id = (service_id_t) -1;
85 prodcons_initialize(&user->in_events);
86 link_initialize(&user->link);
87 user->socket_buffer_len = 0;
88 user->socket_buffer_pos = 0;
89
90 fibril_condvar_initialize(&user->refcount_cv);
91 fibril_mutex_initialize(&user->refcount_mutex);
92 user->task_finished = false;
93 user->socket_closed = false;
94 user->locsrv_connection_count = 0;
95
96
97 fibril_mutex_lock(&users_guard);
98 list_append(&user->link, &users);
99 fibril_mutex_unlock(&users_guard);
100
101 return user;
102}
103
104/** Destroy telnet user structure.
105 *
106 * @param user User to be destroyed.
107 */
108void telnet_user_destroy(telnet_user_t *user)
109{
110 assert(user);
111
112 fibril_mutex_lock(&users_guard);
113 list_remove(&user->link);
114 fibril_mutex_unlock(&users_guard);
115
116 free(user);
117}
118
119/** Find user by service id and increments reference counter.
120 *
121 * @param id Location service id of the telnet user's terminal.
122 */
123telnet_user_t *telnet_user_get_for_client_connection(service_id_t id)
124{
125 telnet_user_t *user = NULL;
126
127 fibril_mutex_lock(&users_guard);
128 list_foreach(users, link) {
129 telnet_user_t *tmp = list_get_instance(link, telnet_user_t, link);
130 if (tmp->service_id == id) {
131 user = tmp;
132 break;
133 }
134 }
135 if (user == NULL) {
136 fibril_mutex_unlock(&users_guard);
137 return NULL;
138 }
139
140 telnet_user_t *tmp = user;
141 fibril_mutex_lock(&tmp->refcount_mutex);
142 user->locsrv_connection_count++;
143
144 /*
145 * Refuse to return user whose task already finished or when
146 * the socket is already closed().
147 */
148 if (user->task_finished || user->socket_closed) {
149 user = NULL;
150 user->locsrv_connection_count--;
151 }
152
153 fibril_mutex_unlock(&tmp->refcount_mutex);
154
155
156 fibril_mutex_unlock(&users_guard);
157
158 return user;
159}
160
161/** Notify that client disconnected from the remote terminal.
162 *
163 * @param user To which user the client was connected.
164 */
165void telnet_user_notify_client_disconnected(telnet_user_t *user)
166{
167 fibril_mutex_lock(&user->refcount_mutex);
168 assert(user->locsrv_connection_count > 0);
169 user->locsrv_connection_count--;
170 fibril_condvar_signal(&user->refcount_cv);
171 fibril_mutex_unlock(&user->refcount_mutex);
172}
173
174
175/**
176 * @}
177 */
Note: See TracBrowser for help on using the repository browser.