source: mainline/uspace/lib/c/generic/io/chardev_srv.c@ f5837524

Last change on this file since f5837524 was f5837524, checked in by Jakub Jermar <jakub@…>, 7 years ago

Use user-defined labels instead of phone hashes

This commit changes the way how the async framework maps incomming calls
to connections. Instead of abusing the kernel addresses of attached
phones as identifiers, the IPC_M_CONNECT_TO_ME and IPC_M_CONNECT_ME_TO
messages allow the server to specify an arbitrary label which is
remembered in the connected phone and consequently imprinted on each
call which is routed through this phone.

The async framework uses the address of the connection structure as the
label. This removes the need for a connection hash table because each
incoming call already remembers the connection in its label.

To disambiguate this new label and the other user-defined label used for
answers, the call structure now has the request_label member for the
former and answer_label member for the latter.

This commit also moves the kernel definition of ipc_data_t to abi/ and
removes the uspace redefinition thereof. Finally, when forwarding the
IPC_M_CONNECT_TO_ME call, the phone capability and the kernel object
allocated in request_process are now correctly disposed of.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2014 Jiri Svoboda
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 libc
30 * @{
31 */
32/**
33 * @file
34 * @brief Character device server stub
35 */
36#include <errno.h>
37#include <io/chardev_srv.h>
38#include <ipc/chardev.h>
39#include <macros.h>
40#include <stdlib.h>
41#include <stddef.h>
42
43static chardev_srv_t *chardev_srv_create(chardev_srvs_t *);
44
45static void chardev_read_srv(chardev_srv_t *srv, ipc_call_t *icall)
46{
47 void *buf;
48 size_t size;
49 size_t nread;
50 errno_t rc;
51
52 ipc_call_t call;
53 if (!async_data_read_receive(&call, &size)) {
54 async_answer_0(icall, EINVAL);
55 return;
56 }
57
58 buf = malloc(size);
59 if (buf == NULL) {
60 async_answer_0(&call, ENOMEM);
61 async_answer_0(icall, ENOMEM);
62 return;
63 }
64
65 if (srv->srvs->ops->read == NULL) {
66 async_answer_0(&call, ENOTSUP);
67 async_answer_0(icall, ENOTSUP);
68 free(buf);
69 return;
70 }
71
72 rc = srv->srvs->ops->read(srv, buf, size, &nread);
73 if (rc != EOK && nread == 0) {
74 async_answer_0(&call, rc);
75 async_answer_0(icall, rc);
76 free(buf);
77 return;
78 }
79
80 async_data_read_finalize(&call, buf, nread);
81
82 free(buf);
83 async_answer_2(icall, EOK, (sysarg_t) rc, nread);
84}
85
86static void chardev_write_srv(chardev_srv_t *srv, ipc_call_t *icall)
87{
88 void *data;
89 size_t size;
90 size_t nwr;
91 errno_t rc;
92
93 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
94 if (rc != EOK) {
95 async_answer_0(icall, rc);
96 return;
97 }
98
99 if (srv->srvs->ops->write == NULL) {
100 async_answer_0(icall, ENOTSUP);
101 return;
102 }
103
104 rc = srv->srvs->ops->write(srv, data, size, &nwr);
105 free(data);
106 if (rc != EOK && nwr == 0) {
107 async_answer_0(icall, rc);
108 return;
109 }
110
111 async_answer_2(icall, EOK, (sysarg_t) rc, nwr);
112}
113
114static chardev_srv_t *chardev_srv_create(chardev_srvs_t *srvs)
115{
116 chardev_srv_t *srv;
117
118 srv = calloc(1, sizeof(chardev_srv_t));
119 if (srv == NULL)
120 return NULL;
121
122 srv->srvs = srvs;
123 return srv;
124}
125
126void chardev_srvs_init(chardev_srvs_t *srvs)
127{
128 srvs->ops = NULL;
129 srvs->sarg = NULL;
130}
131
132errno_t chardev_conn(ipc_call_t *icall, chardev_srvs_t *srvs)
133{
134 chardev_srv_t *srv;
135 errno_t rc;
136
137 /* Accept the connection */
138 async_answer_5(icall, EOK, 0, 0, 0, 0, async_get_label());
139
140 srv = chardev_srv_create(srvs);
141 if (srv == NULL)
142 return ENOMEM;
143
144 if (srvs->ops->open != NULL) {
145 rc = srvs->ops->open(srvs, srv);
146 if (rc != EOK)
147 return rc;
148 }
149
150 while (true) {
151 ipc_call_t call;
152 async_get_call(&call);
153 sysarg_t method = IPC_GET_IMETHOD(call);
154
155 if (!method) {
156 /* The other side has hung up */
157 async_answer_0(&call, EOK);
158 break;
159 }
160
161 switch (method) {
162 case CHARDEV_READ:
163 chardev_read_srv(srv, &call);
164 break;
165 case CHARDEV_WRITE:
166 chardev_write_srv(srv, &call);
167 break;
168 default:
169 if (srv->srvs->ops->def_handler != NULL)
170 srv->srvs->ops->def_handler(srv, &call);
171 else
172 async_answer_0(&call, ENOTSUP);
173 }
174 }
175
176 if (srvs->ops->close != NULL)
177 rc = srvs->ops->close(srv);
178 else
179 rc = EOK;
180
181 free(srv);
182
183 return rc;
184}
185
186/** @}
187 */
Note: See TracBrowser for help on using the repository browser.