source: mainline/uspace/lib/c/generic/ns.c@ f0cc1c64

Last change on this file since f0cc1c64 was ca48672, checked in by Jiri Svoboda <jiri@…>, 9 days ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2011 Martin Decky
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 libc
31 * @{
32 */
33/** @file
34 */
35
36#include <ns.h>
37#include <ipc/ns.h>
38#include <async.h>
39#include <macros.h>
40#include <errno.h>
41#include "private/ns.h"
42
43/*
44 * XXX ns does not know about session_ns, so we create an extra session for
45 * actual communicaton
46 */
47static async_sess_t *sess_ns = NULL;
48
49errno_t service_register(service_t service, iface_t iface,
50 async_port_handler_t handler, void *data)
51{
52 errno_t rc;
53 async_sess_t *sess = ns_session_get(&rc);
54 if (sess == NULL)
55 return rc;
56
57 port_id_t port;
58 rc = async_create_port(iface, handler, data, &port);
59 if (rc != EOK)
60 return rc;
61
62 async_exch_t *exch = async_exchange_begin(sess);
63
64 ipc_call_t answer;
65 aid_t req = async_send_2(exch, NS_REGISTER, service, iface, &answer);
66 rc = async_connect_to_me(exch, iface, service, 0);
67
68 async_exchange_end(exch);
69
70 if (rc != EOK) {
71 async_port_destroy(port);
72 async_forget(req);
73 return rc;
74 }
75
76 errno_t retval;
77 async_wait_for(req, &retval);
78
79 if (rc != EOK)
80 async_port_destroy(port);
81 return rc;
82}
83
84errno_t service_register_broker(service_t service, async_port_handler_t handler,
85 void *data)
86{
87 async_set_fallback_port_handler(handler, data);
88
89 errno_t rc;
90 async_sess_t *sess = ns_session_get(&rc);
91 if (sess == NULL)
92 return rc;
93
94 async_exch_t *exch = async_exchange_begin(sess);
95
96 ipc_call_t answer;
97 aid_t req = async_send_1(exch, NS_REGISTER_BROKER, service, &answer);
98 rc = async_connect_to_me(exch, INTERFACE_ANY, service, 0);
99
100 async_exchange_end(exch);
101
102 if (rc != EOK) {
103 async_forget(req);
104 return rc;
105 }
106
107 errno_t retval;
108 async_wait_for(req, &retval);
109 return rc;
110}
111
112/** Connect to a singleton service.
113 *
114 * @param service Singleton service ID.
115 * @param iface Interface to connect to.
116 * @param arg3 Custom connection argument.
117 * @param rc Placeholder for return code. Unused if NULL.
118 *
119 * @return New session on success or NULL on error.
120 *
121 */
122async_sess_t *service_connect(service_t service, iface_t iface, sysarg_t arg3,
123 errno_t *rc)
124{
125 async_sess_t *sess = ns_session_get(rc);
126 if (sess == NULL)
127 return NULL;
128
129 async_exch_t *exch = async_exchange_begin(sess);
130 if (exch == NULL)
131 return NULL;
132
133 async_sess_t *csess =
134 async_connect_me_to(exch, iface, service, arg3, rc);
135 async_exchange_end(exch);
136
137 if (csess == NULL)
138 return NULL;
139
140 /*
141 * FIXME Ugly hack to work around limitation of implementing
142 * parallel exchanges using multiple connections. Shift out
143 * first argument for non-initial connections.
144 */
145 async_sess_args_set(csess, iface, arg3, 0);
146
147 return csess;
148}
149
150/** Wait and connect to a singleton service.
151 *
152 * @param service Singleton service ID.
153 * @param iface Interface to connect to.
154 * @param arg3 Custom connection argument.
155 * @param rc Placeholder for return code. Unused if NULL.
156 *
157 * @return New session on success or NULL on error.
158 *
159 */
160async_sess_t *service_connect_blocking(service_t service, iface_t iface,
161 sysarg_t arg3, errno_t *rc)
162{
163 async_sess_t *sess = ns_session_get(rc);
164 if (sess == NULL)
165 return NULL;
166
167 async_exch_t *exch = async_exchange_begin(sess);
168 async_sess_t *csess =
169 async_connect_me_to_blocking(exch, iface, service, arg3, rc);
170 async_exchange_end(exch);
171
172 if (csess == NULL)
173 return NULL;
174
175 /*
176 * FIXME Ugly hack to work around limitation of implementing
177 * parallel exchanges using multiple connections. Shift out
178 * first argument for non-initial connections.
179 */
180 async_sess_args_set(csess, iface, arg3, 0);
181
182 return csess;
183}
184
185errno_t ns_ping(void)
186{
187 errno_t rc;
188 async_sess_t *sess = ns_session_get(&rc);
189 if (sess == NULL)
190 return rc;
191
192 async_exch_t *exch = async_exchange_begin(sess);
193 rc = async_req_0_0(exch, NS_PING);
194 async_exchange_end(exch);
195
196 return rc;
197}
198
199errno_t ns_intro(task_id_t id)
200{
201 errno_t rc;
202 async_sess_t *sess = ns_session_get(&rc);
203 if (sess == NULL)
204 return EIO;
205
206 async_exch_t *exch = async_exchange_begin(sess);
207 rc = async_req_2_0(exch, NS_ID_INTRO, LOWER32(id), UPPER32(id));
208 async_exchange_end(exch);
209
210 return rc;
211}
212
213async_sess_t *ns_session_get(errno_t *rc)
214{
215 async_exch_t *exch;
216
217 if (sess_ns == NULL) {
218 exch = async_exchange_begin(&session_ns);
219 sess_ns = async_connect_me_to(exch, 0, 0, 0, rc);
220 async_exchange_end(exch);
221 if (sess_ns == NULL)
222 return NULL;
223 }
224
225 return sess_ns;
226}
227
228/** @}
229 */
Note: See TracBrowser for help on using the repository browser.