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

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

remcons: minor refactoring

  • Property mode set to 100644
File size: 4.1 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
62telnet_user_t *telnet_user_create(int socket)
63{
64 static int telnet_user_id_counter = 0;
65
66 telnet_user_t *user = malloc(sizeof(telnet_user_t));
67 if (user == NULL) {
68 return NULL;
69 }
70
71 user->id = ++telnet_user_id_counter;
72
73 int rc = asprintf(&user->service_name, "%s/telnet%d", NAMESPACE, user->id);
74 if (rc < 0) {
75 free(user);
76 return NULL;
77 }
78
79 user->socket = socket;
80 user->service_id = (service_id_t) -1;
81 prodcons_initialize(&user->in_events);
82 link_initialize(&user->link);
83 user->socket_buffer_len = 0;
84 user->socket_buffer_pos = 0;
85
86 fibril_condvar_initialize(&user->refcount_cv);
87 fibril_mutex_initialize(&user->refcount_mutex);
88 user->task_finished = false;
89 user->socket_closed = false;
90 user->locsrv_connection_count = 0;
91
92
93 fibril_mutex_lock(&users_guard);
94 list_append(&user->link, &users);
95 fibril_mutex_unlock(&users_guard);
96
97 return user;
98}
99
100void telnet_user_destroy(telnet_user_t *user)
101{
102 assert(user);
103
104 fibril_mutex_lock(&users_guard);
105 list_remove(&user->link);
106 fibril_mutex_unlock(&users_guard);
107
108 free(user);
109}
110
111telnet_user_t *telnet_user_get_for_client_connection(service_id_t id)
112{
113 telnet_user_t *user = NULL;
114
115 fibril_mutex_lock(&users_guard);
116 list_foreach(users, link) {
117 telnet_user_t *tmp = list_get_instance(link, telnet_user_t, link);
118 if (tmp->service_id == id) {
119 user = tmp;
120 break;
121 }
122 }
123 if (user == NULL) {
124 fibril_mutex_unlock(&users_guard);
125 return NULL;
126 }
127
128 telnet_user_t *tmp = user;
129 fibril_mutex_lock(&tmp->refcount_mutex);
130 user->locsrv_connection_count++;
131
132 /*
133 * Refuse to return user whose task already finished or when
134 * the socket is already closed().
135 */
136 if (user->task_finished || user->socket_closed) {
137 user = NULL;
138 user->locsrv_connection_count--;
139 }
140
141 fibril_mutex_unlock(&tmp->refcount_mutex);
142
143
144 fibril_mutex_unlock(&users_guard);
145
146 return user;
147}
148
149void telnet_user_notify_client_disconnected(telnet_user_t *user)
150{
151 fibril_mutex_lock(&user->refcount_mutex);
152 assert(user->locsrv_connection_count > 0);
153 user->locsrv_connection_count--;
154 fibril_condvar_signal(&user->refcount_cv);
155 fibril_mutex_unlock(&user->refcount_mutex);
156}
157
158
159/**
160 * @}
161 */
Note: See TracBrowser for help on using the repository browser.