source: mainline/uspace/srv/loc/loc.c@ 40cdbec

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 40cdbec was a9abe5fc, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Suppress various messages.

  • Property mode set to 100644
File size: 33.0 KB
Line 
1/*
2 * Copyright (c) 2007 Josef Cejka
3 * Copyright (c) 2011 Jiri Svoboda
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/**
31 * @defgroup loc Location Service.
32 * @brief HelenOS location service.
33 * @{
34 */
35
36/** @file
37 */
38
39#include <ipc/services.h>
40#include <ns.h>
41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
44#include <bool.h>
45#include <fibril_synch.h>
46#include <macros.h>
47#include <stdlib.h>
48#include <str.h>
49#include <ipc/loc.h>
50#include <assert.h>
51
52#include "category.h"
53#include "loc.h"
54
55#define NAME "loc"
56#define NULL_SERVICES 256
57
58LIST_INITIALIZE(services_list);
59LIST_INITIALIZE(namespaces_list);
60LIST_INITIALIZE(servers_list);
61
62/* Locking order:
63 * servers_list_mutex
64 * services_list_mutex
65 * (loc_server_t *)->services_mutex
66 * create_id_mutex
67 **/
68
69FIBRIL_MUTEX_INITIALIZE(services_list_mutex);
70static FIBRIL_CONDVAR_INITIALIZE(services_list_cv);
71static FIBRIL_MUTEX_INITIALIZE(servers_list_mutex);
72static FIBRIL_MUTEX_INITIALIZE(create_id_mutex);
73static FIBRIL_MUTEX_INITIALIZE(null_services_mutex);
74
75static service_id_t last_id = 0;
76static loc_service_t *null_services[NULL_SERVICES];
77
78/*
79 * Dummy list for null services. This is necessary so that null services can
80 * be used just as any other services, e.g. in loc_service_unregister_core().
81 */
82static LIST_INITIALIZE(dummy_null_services);
83
84/** Service directory ogranized by categories (yellow pages) */
85static categ_dir_t cdir;
86
87static FIBRIL_MUTEX_INITIALIZE(callback_sess_mutex);
88static async_sess_t *callback_sess = NULL;
89
90service_id_t loc_create_id(void)
91{
92 /* TODO: allow reusing old ids after their unregistration
93 * and implement some version of LRU algorithm, avoid overflow
94 */
95
96 fibril_mutex_lock(&create_id_mutex);
97 last_id++;
98 fibril_mutex_unlock(&create_id_mutex);
99
100 return last_id;
101}
102
103/** Convert fully qualified service name to namespace and service name.
104 *
105 * A fully qualified service name can be either a plain service name
106 * (then the namespace is considered to be an empty string) or consist
107 * of two components separated by a slash. No more than one slash
108 * is allowed.
109 *
110 */
111static bool loc_fqsn_split(const char *fqsn, char **ns_name, char **name)
112{
113 size_t cnt = 0;
114 size_t slash_offset = 0;
115 size_t slash_after = 0;
116
117 size_t offset = 0;
118 size_t offset_prev = 0;
119 wchar_t c;
120
121 while ((c = str_decode(fqsn, &offset, STR_NO_LIMIT)) != 0) {
122 if (c == '/') {
123 cnt++;
124 slash_offset = offset_prev;
125 slash_after = offset;
126 }
127 offset_prev = offset;
128 }
129
130 /* More than one slash */
131 if (cnt > 1)
132 return false;
133
134 /* No slash -> namespace is empty */
135 if (cnt == 0) {
136 *ns_name = str_dup("");
137 if (*ns_name == NULL)
138 return false;
139
140 *name = str_dup(fqsn);
141 if (*name == NULL) {
142 free(*ns_name);
143 return false;
144 }
145
146 if (str_cmp(*name, "") == 0) {
147 free(*name);
148 free(*ns_name);
149 return false;
150 }
151
152 return true;
153 }
154
155 /* Exactly one slash */
156 *ns_name = str_ndup(fqsn, slash_offset);
157 if (*ns_name == NULL)
158 return false;
159
160 *name = str_dup(fqsn + slash_after);
161 if (*name == NULL) {
162 free(*ns_name);
163 return false;
164 }
165
166 if (str_cmp(*name, "") == 0) {
167 free(*name);
168 free(*ns_name);
169 return false;
170 }
171
172 return true;
173}
174
175/** Find namespace with given name. */
176static loc_namespace_t *loc_namespace_find_name(const char *name)
177{
178 assert(fibril_mutex_is_locked(&services_list_mutex));
179
180 list_foreach(namespaces_list, item) {
181 loc_namespace_t *namespace =
182 list_get_instance(item, loc_namespace_t, namespaces);
183 if (str_cmp(namespace->name, name) == 0)
184 return namespace;
185 }
186
187 return NULL;
188}
189
190/** Find namespace with given ID.
191 *
192 * @todo: use hash table
193 *
194 */
195static loc_namespace_t *loc_namespace_find_id(service_id_t id)
196{
197 assert(fibril_mutex_is_locked(&services_list_mutex));
198
199 list_foreach(namespaces_list, item) {
200 loc_namespace_t *namespace =
201 list_get_instance(item, loc_namespace_t, namespaces);
202 if (namespace->id == id)
203 return namespace;
204 }
205
206 return NULL;
207}
208
209/** Find service with given name. */
210static loc_service_t *loc_service_find_name(const char *ns_name,
211 const char *name)
212{
213 assert(fibril_mutex_is_locked(&services_list_mutex));
214
215 list_foreach(services_list, item) {
216 loc_service_t *service =
217 list_get_instance(item, loc_service_t, services);
218 if ((str_cmp(service->namespace->name, ns_name) == 0)
219 && (str_cmp(service->name, name) == 0))
220 return service;
221 }
222
223 return NULL;
224}
225
226/** Find service with given ID.
227 *
228 * @todo: use hash table
229 *
230 */
231static loc_service_t *loc_service_find_id(service_id_t id)
232{
233 assert(fibril_mutex_is_locked(&services_list_mutex));
234
235 list_foreach(services_list, item) {
236 loc_service_t *service =
237 list_get_instance(item, loc_service_t, services);
238 if (service->id == id)
239 return service;
240 }
241
242 return NULL;
243}
244
245/** Create a namespace (if not already present). */
246static loc_namespace_t *loc_namespace_create(const char *ns_name)
247{
248 loc_namespace_t *namespace;
249
250 assert(fibril_mutex_is_locked(&services_list_mutex));
251
252 namespace = loc_namespace_find_name(ns_name);
253 if (namespace != NULL)
254 return namespace;
255
256 namespace = (loc_namespace_t *) malloc(sizeof(loc_namespace_t));
257 if (namespace == NULL)
258 return NULL;
259
260 namespace->name = str_dup(ns_name);
261 if (namespace->name == NULL) {
262 free(namespace);
263 return NULL;
264 }
265
266 namespace->id = loc_create_id();
267 namespace->refcnt = 0;
268
269 /*
270 * Insert new namespace into list of registered namespaces
271 */
272 list_append(&(namespace->namespaces), &namespaces_list);
273
274 return namespace;
275}
276
277/** Destroy a namespace (if it is no longer needed). */
278static void loc_namespace_destroy(loc_namespace_t *namespace)
279{
280 assert(fibril_mutex_is_locked(&services_list_mutex));
281
282 if (namespace->refcnt == 0) {
283 list_remove(&(namespace->namespaces));
284
285 free(namespace->name);
286 free(namespace);
287 }
288}
289
290/** Increase namespace reference count by including service. */
291static void loc_namespace_addref(loc_namespace_t *namespace,
292 loc_service_t *service)
293{
294 assert(fibril_mutex_is_locked(&services_list_mutex));
295
296 service->namespace = namespace;
297 namespace->refcnt++;
298}
299
300/** Decrease namespace reference count. */
301static void loc_namespace_delref(loc_namespace_t *namespace)
302{
303 assert(fibril_mutex_is_locked(&services_list_mutex));
304
305 namespace->refcnt--;
306 loc_namespace_destroy(namespace);
307}
308
309/** Unregister service and free it. */
310static void loc_service_unregister_core(loc_service_t *service)
311{
312 assert(fibril_mutex_is_locked(&services_list_mutex));
313 assert(fibril_mutex_is_locked(&cdir.mutex));
314
315 loc_namespace_delref(service->namespace);
316 list_remove(&(service->services));
317 list_remove(&(service->server_services));
318
319 /* Remove service from all categories. */
320 while (!list_empty(&service->cat_memb)) {
321 link_t *link = list_first(&service->cat_memb);
322 svc_categ_t *memb = list_get_instance(link, svc_categ_t,
323 svc_link);
324 fibril_mutex_lock(&memb->cat->mutex);
325 category_remove_service(memb);
326 fibril_mutex_unlock(&memb->cat->mutex);
327 }
328
329 free(service->name);
330 free(service);
331}
332
333/**
334 * Read info about new server and add it into linked list of registered
335 * servers.
336 */
337static loc_server_t *loc_server_register(void)
338{
339 ipc_call_t icall;
340 ipc_callid_t iid = async_get_call(&icall);
341
342 if (IPC_GET_IMETHOD(icall) != LOC_SERVER_REGISTER) {
343 async_answer_0(iid, EREFUSED);
344 return NULL;
345 }
346
347 loc_server_t *server =
348 (loc_server_t *) malloc(sizeof(loc_server_t));
349 if (server == NULL) {
350 async_answer_0(iid, ENOMEM);
351 return NULL;
352 }
353
354 /*
355 * Get server name
356 */
357 int rc = async_data_write_accept((void **) &server->name, true, 0,
358 LOC_NAME_MAXLEN, 0, NULL);
359 if (rc != EOK) {
360 free(server);
361 async_answer_0(iid, rc);
362 return NULL;
363 }
364
365 /*
366 * Create connection to the server
367 */
368 server->sess = async_callback_receive(EXCHANGE_SERIALIZE);
369 if (!server->sess) {
370 free(server->name);
371 free(server);
372 async_answer_0(iid, ENOTSUP);
373 return NULL;
374 }
375
376 /*
377 * Initialize mutex for list of services
378 * supplied by this server
379 */
380 fibril_mutex_initialize(&server->services_mutex);
381
382 /*
383 * Initialize list of supplied services
384 */
385 list_initialize(&server->services);
386
387 link_initialize(&server->servers);
388
389 fibril_mutex_lock(&servers_list_mutex);
390
391 /* TODO:
392 * Check that no server with name equal to
393 * server->name is registered
394 */
395
396 /*
397 * Insert new server into list of registered servers
398 */
399 list_append(&(server->servers), &servers_list);
400 fibril_mutex_unlock(&servers_list_mutex);
401
402 async_answer_0(iid, EOK);
403
404 return server;
405}
406
407/**
408 * Unregister server, unregister all its services and free server
409 * structure.
410 *
411 */
412static int loc_server_unregister(loc_server_t *server)
413{
414 if (server == NULL)
415 return EEXISTS;
416
417 fibril_mutex_lock(&servers_list_mutex);
418
419 if (server->sess)
420 async_hangup(server->sess);
421
422 /* Remove it from list of servers */
423 list_remove(&(server->servers));
424
425 /* Unregister all its services */
426 fibril_mutex_lock(&services_list_mutex);
427 fibril_mutex_lock(&server->services_mutex);
428 fibril_mutex_lock(&cdir.mutex);
429
430 while (!list_empty(&server->services)) {
431 loc_service_t *service = list_get_instance(
432 list_first(&server->services), loc_service_t,
433 server_services);
434 loc_service_unregister_core(service);
435 }
436
437 fibril_mutex_unlock(&cdir.mutex);
438 fibril_mutex_unlock(&server->services_mutex);
439 fibril_mutex_unlock(&services_list_mutex);
440 fibril_mutex_unlock(&servers_list_mutex);
441
442 /* Free name and server */
443 if (server->name != NULL)
444 free(server->name);
445
446 free(server);
447
448 return EOK;
449}
450
451/** Register service
452 *
453 */
454static void loc_service_register(ipc_callid_t iid, ipc_call_t *icall,
455 loc_server_t *server)
456{
457 if (server == NULL) {
458 async_answer_0(iid, EREFUSED);
459 return;
460 }
461
462 /* Create new service entry */
463 loc_service_t *service =
464 (loc_service_t *) malloc(sizeof(loc_service_t));
465 if (service == NULL) {
466 async_answer_0(iid, ENOMEM);
467 return;
468 }
469
470 /* Set the interface, if any. */
471 service->forward_interface = IPC_GET_ARG1(*icall);
472
473 /* Get fqsn */
474 char *fqsn;
475 int rc = async_data_write_accept((void **) &fqsn, true, 0,
476 LOC_NAME_MAXLEN, 0, NULL);
477 if (rc != EOK) {
478 free(service);
479 async_answer_0(iid, rc);
480 return;
481 }
482
483 char *ns_name;
484 if (!loc_fqsn_split(fqsn, &ns_name, &service->name)) {
485 free(fqsn);
486 free(service);
487 async_answer_0(iid, EINVAL);
488 return;
489 }
490
491 free(fqsn);
492
493 fibril_mutex_lock(&services_list_mutex);
494
495 loc_namespace_t *namespace = loc_namespace_create(ns_name);
496 free(ns_name);
497 if (namespace == NULL) {
498 fibril_mutex_unlock(&services_list_mutex);
499 free(service->name);
500 free(service);
501 async_answer_0(iid, ENOMEM);
502 return;
503 }
504
505 link_initialize(&service->services);
506 link_initialize(&service->server_services);
507 list_initialize(&service->cat_memb);
508
509 /* Check that service is not already registered */
510 if (loc_service_find_name(namespace->name, service->name) != NULL) {
511 printf("%s: Service '%s/%s' already registered\n", NAME,
512 namespace->name, service->name);
513 loc_namespace_destroy(namespace);
514 fibril_mutex_unlock(&services_list_mutex);
515 free(service->name);
516 free(service);
517 async_answer_0(iid, EEXISTS);
518 return;
519 }
520
521 /* Get unique service ID */
522 service->id = loc_create_id();
523
524 loc_namespace_addref(namespace, service);
525 service->server = server;
526
527 /* Insert service into list of all services */
528 list_append(&service->services, &services_list);
529
530 /* Insert service into list of services supplied by one server */
531 fibril_mutex_lock(&service->server->services_mutex);
532
533 list_append(&service->server_services, &service->server->services);
534
535 fibril_mutex_unlock(&service->server->services_mutex);
536 fibril_condvar_broadcast(&services_list_cv);
537 fibril_mutex_unlock(&services_list_mutex);
538
539 async_answer_1(iid, EOK, service->id);
540}
541
542/**
543 *
544 */
545static void loc_service_unregister(ipc_callid_t iid, ipc_call_t *icall,
546 loc_server_t *server)
547{
548 loc_service_t *svc;
549
550 fibril_mutex_lock(&services_list_mutex);
551 svc = loc_service_find_id(IPC_GET_ARG1(*icall));
552 if (svc == NULL) {
553 fibril_mutex_unlock(&services_list_mutex);
554 async_answer_0(iid, ENOENT);
555 return;
556 }
557
558 fibril_mutex_lock(&cdir.mutex);
559 loc_service_unregister_core(svc);
560 fibril_mutex_unlock(&cdir.mutex);
561 fibril_mutex_unlock(&services_list_mutex);
562 async_answer_0(iid, EOK);
563}
564
565static void loc_category_get_name(ipc_callid_t iid, ipc_call_t *icall)
566{
567 ipc_callid_t callid;
568 size_t size;
569 size_t act_size;
570 category_t *cat;
571
572 if (!async_data_read_receive(&callid, &size)) {
573 async_answer_0(callid, EREFUSED);
574 async_answer_0(iid, EREFUSED);
575 return;
576 }
577
578 fibril_mutex_lock(&cdir.mutex);
579
580 cat = category_get(&cdir, IPC_GET_ARG1(*icall));
581 if (cat == NULL) {
582 fibril_mutex_unlock(&cdir.mutex);
583 async_answer_0(callid, ENOENT);
584 async_answer_0(iid, ENOENT);
585 return;
586 }
587
588 act_size = str_size(cat->name);
589 if (act_size > size) {
590 fibril_mutex_unlock(&cdir.mutex);
591 async_answer_0(callid, EOVERFLOW);
592 async_answer_0(iid, EOVERFLOW);
593 return;
594 }
595
596 sysarg_t retval = async_data_read_finalize(callid, cat->name,
597 min(size, act_size));
598
599 fibril_mutex_unlock(&cdir.mutex);
600
601 async_answer_0(iid, retval);
602}
603
604static void loc_service_get_name(ipc_callid_t iid, ipc_call_t *icall)
605{
606 ipc_callid_t callid;
607 size_t size;
608 size_t act_size;
609 loc_service_t *svc;
610
611 if (!async_data_read_receive(&callid, &size)) {
612 async_answer_0(callid, EREFUSED);
613 async_answer_0(iid, EREFUSED);
614 return;
615 }
616
617 fibril_mutex_lock(&services_list_mutex);
618
619 svc = loc_service_find_id(IPC_GET_ARG1(*icall));
620 if (svc == NULL) {
621 fibril_mutex_unlock(&services_list_mutex);
622 async_answer_0(callid, ENOENT);
623 async_answer_0(iid, ENOENT);
624 return;
625 }
626
627 act_size = str_size(svc->name);
628 if (act_size > size) {
629 fibril_mutex_unlock(&services_list_mutex);
630 async_answer_0(callid, EOVERFLOW);
631 async_answer_0(iid, EOVERFLOW);
632 return;
633 }
634
635 sysarg_t retval = async_data_read_finalize(callid, svc->name,
636 min(size, act_size));
637
638 fibril_mutex_unlock(&services_list_mutex);
639
640 async_answer_0(iid, retval);
641}
642
643/** Connect client to the service.
644 *
645 * Find server supplying requested service and forward
646 * the message to it.
647 *
648 */
649static void loc_forward(ipc_callid_t callid, ipc_call_t *call)
650{
651 fibril_mutex_lock(&services_list_mutex);
652
653 /*
654 * Get ID from request
655 */
656 service_id_t id = IPC_GET_ARG2(*call);
657 loc_service_t *svc = loc_service_find_id(id);
658
659 if ((svc == NULL) || (svc->server == NULL) || (!svc->server->sess)) {
660 fibril_mutex_unlock(&services_list_mutex);
661 async_answer_0(callid, ENOENT);
662 return;
663 }
664
665 async_exch_t *exch = async_exchange_begin(svc->server->sess);
666
667 if (svc->forward_interface == 0)
668 async_forward_fast(callid, exch, svc->id, 0, 0, IPC_FF_NONE);
669 else
670 async_forward_fast(callid, exch, svc->forward_interface,
671 svc->id, 0, IPC_FF_NONE);
672
673 async_exchange_end(exch);
674
675 fibril_mutex_unlock(&services_list_mutex);
676}
677
678/** Find ID for service identified by name.
679 *
680 * In answer will be send EOK and service ID in arg1 or a error
681 * code from errno.h.
682 *
683 */
684static void loc_service_get_id(ipc_callid_t iid, ipc_call_t *icall)
685{
686 char *fqsn;
687
688 /* Get fqsn */
689 int rc = async_data_write_accept((void **) &fqsn, true, 0,
690 LOC_NAME_MAXLEN, 0, NULL);
691 if (rc != EOK) {
692 async_answer_0(iid, rc);
693 return;
694 }
695
696 char *ns_name;
697 char *name;
698 if (!loc_fqsn_split(fqsn, &ns_name, &name)) {
699 free(fqsn);
700 async_answer_0(iid, EINVAL);
701 return;
702 }
703
704 free(fqsn);
705
706 fibril_mutex_lock(&services_list_mutex);
707 const loc_service_t *svc;
708
709recheck:
710
711 /*
712 * Find service name in the list of known services.
713 */
714 svc = loc_service_find_name(ns_name, name);
715
716 /*
717 * Device was not found.
718 */
719 if (svc == NULL) {
720 if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
721 /* Blocking lookup */
722 fibril_condvar_wait(&services_list_cv,
723 &services_list_mutex);
724 goto recheck;
725 }
726
727 async_answer_0(iid, ENOENT);
728 free(ns_name);
729 free(name);
730 fibril_mutex_unlock(&services_list_mutex);
731 return;
732 }
733
734 async_answer_1(iid, EOK, svc->id);
735
736 fibril_mutex_unlock(&services_list_mutex);
737 free(ns_name);
738 free(name);
739}
740
741/** Find ID for namespace identified by name.
742 *
743 * In answer will be send EOK and service ID in arg1 or a error
744 * code from errno.h.
745 *
746 */
747static void loc_namespace_get_id(ipc_callid_t iid, ipc_call_t *icall)
748{
749 char *name;
750
751 /* Get service name */
752 int rc = async_data_write_accept((void **) &name, true, 0,
753 LOC_NAME_MAXLEN, 0, NULL);
754 if (rc != EOK) {
755 async_answer_0(iid, rc);
756 return;
757 }
758
759 fibril_mutex_lock(&services_list_mutex);
760 const loc_namespace_t *namespace;
761
762recheck:
763
764 /*
765 * Find namespace name in the list of known namespaces.
766 */
767 namespace = loc_namespace_find_name(name);
768
769 /*
770 * Namespace was not found.
771 */
772 if (namespace == NULL) {
773 if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
774 /* Blocking lookup */
775 fibril_condvar_wait(&services_list_cv,
776 &services_list_mutex);
777 goto recheck;
778 }
779
780 async_answer_0(iid, ENOENT);
781 free(name);
782 fibril_mutex_unlock(&services_list_mutex);
783 return;
784 }
785
786 async_answer_1(iid, EOK, namespace->id);
787
788 fibril_mutex_unlock(&services_list_mutex);
789 free(name);
790}
791
792/** Find ID for category specified by name.
793 *
794 * On success, answer will contain EOK int retval and service ID in arg1.
795 * On failure, error code will be sent in retval.
796 *
797 */
798static void loc_callback_create(ipc_callid_t iid, ipc_call_t *icall)
799{
800 async_sess_t *cb_sess = async_callback_receive(EXCHANGE_SERIALIZE);
801 if (cb_sess == NULL) {
802 async_answer_0(iid, ENOMEM);
803 return;
804 }
805
806 fibril_mutex_lock(&callback_sess_mutex);
807 if (callback_sess != NULL) {
808 fibril_mutex_unlock(&callback_sess_mutex);
809 async_answer_0(iid, EEXIST);
810 return;
811 }
812
813 callback_sess = cb_sess;
814 fibril_mutex_unlock(&callback_sess_mutex);
815
816 async_answer_0(iid, EOK);
817}
818
819void loc_category_change_event(void)
820{
821 fibril_mutex_lock(&callback_sess_mutex);
822
823 if (callback_sess != NULL) {
824 async_exch_t *exch = async_exchange_begin(callback_sess);
825 async_msg_0(exch, LOC_EVENT_CAT_CHANGE);
826 async_exchange_end(exch);
827 }
828
829 fibril_mutex_unlock(&callback_sess_mutex);
830}
831
832/** Find ID for category specified by name.
833 *
834 * On success, answer will contain EOK int retval and service ID in arg1.
835 * On failure, error code will be sent in retval.
836 *
837 */
838static void loc_category_get_id(ipc_callid_t iid, ipc_call_t *icall)
839{
840 char *name;
841 category_t *cat;
842
843 /* Get service name */
844 int rc = async_data_write_accept((void **) &name, true, 0,
845 LOC_NAME_MAXLEN, 0, NULL);
846 if (rc != EOK) {
847 async_answer_0(iid, rc);
848 return;
849 }
850
851 fibril_mutex_lock(&cdir.mutex);
852
853 cat = category_find_by_name(&cdir, name);
854 if (cat == NULL) {
855 /* Category not found */
856 async_answer_0(iid, ENOENT);
857 goto cleanup;
858 }
859
860 async_answer_1(iid, EOK, cat->id);
861cleanup:
862 fibril_mutex_unlock(&cdir.mutex);
863 free(name);
864}
865
866static void loc_id_probe(ipc_callid_t iid, ipc_call_t *icall)
867{
868 fibril_mutex_lock(&services_list_mutex);
869
870 loc_namespace_t *namespace =
871 loc_namespace_find_id(IPC_GET_ARG1(*icall));
872 if (namespace == NULL) {
873 loc_service_t *svc =
874 loc_service_find_id(IPC_GET_ARG1(*icall));
875 if (svc == NULL)
876 async_answer_1(iid, EOK, LOC_OBJECT_NONE);
877 else
878 async_answer_1(iid, EOK, LOC_OBJECT_SERVICE);
879 } else
880 async_answer_1(iid, EOK, LOC_OBJECT_NAMESPACE);
881
882 fibril_mutex_unlock(&services_list_mutex);
883}
884
885static void loc_get_namespace_count(ipc_callid_t iid, ipc_call_t *icall)
886{
887 fibril_mutex_lock(&services_list_mutex);
888 async_answer_1(iid, EOK, list_count(&namespaces_list));
889 fibril_mutex_unlock(&services_list_mutex);
890}
891
892static void loc_get_service_count(ipc_callid_t iid, ipc_call_t *icall)
893{
894 fibril_mutex_lock(&services_list_mutex);
895
896 loc_namespace_t *namespace =
897 loc_namespace_find_id(IPC_GET_ARG1(*icall));
898 if (namespace == NULL)
899 async_answer_0(iid, EEXISTS);
900 else
901 async_answer_1(iid, EOK, namespace->refcnt);
902
903 fibril_mutex_unlock(&services_list_mutex);
904}
905
906static void loc_get_categories(ipc_callid_t iid, ipc_call_t *icall)
907{
908 ipc_callid_t callid;
909 size_t size;
910 size_t act_size;
911 int rc;
912
913 if (!async_data_read_receive(&callid, &size)) {
914 async_answer_0(callid, EREFUSED);
915 async_answer_0(iid, EREFUSED);
916 return;
917 }
918
919 category_id_t *id_buf = (category_id_t *) malloc(size);
920 if (id_buf == NULL) {
921 fibril_mutex_unlock(&cdir.mutex);
922 async_answer_0(callid, ENOMEM);
923 async_answer_0(iid, ENOMEM);
924 return;
925 }
926
927 fibril_mutex_lock(&cdir.mutex);
928
929 rc = categ_dir_get_categories(&cdir, id_buf, size, &act_size);
930 if (rc != EOK) {
931 fibril_mutex_unlock(&cdir.mutex);
932 async_answer_0(callid, rc);
933 async_answer_0(iid, rc);
934 return;
935 }
936
937 fibril_mutex_unlock(&cdir.mutex);
938
939 sysarg_t retval = async_data_read_finalize(callid, id_buf, size);
940 free(id_buf);
941
942 async_answer_1(iid, retval, act_size);
943}
944
945static void loc_get_namespaces(ipc_callid_t iid, ipc_call_t *icall)
946{
947 ipc_callid_t callid;
948 size_t size;
949 if (!async_data_read_receive(&callid, &size)) {
950 async_answer_0(callid, EREFUSED);
951 async_answer_0(iid, EREFUSED);
952 return;
953 }
954
955 if ((size % sizeof(loc_sdesc_t)) != 0) {
956 async_answer_0(callid, EINVAL);
957 async_answer_0(iid, EINVAL);
958 return;
959 }
960
961 fibril_mutex_lock(&services_list_mutex);
962
963 size_t count = size / sizeof(loc_sdesc_t);
964 if (count != list_count(&namespaces_list)) {
965 fibril_mutex_unlock(&services_list_mutex);
966 async_answer_0(callid, EOVERFLOW);
967 async_answer_0(iid, EOVERFLOW);
968 return;
969 }
970
971 loc_sdesc_t *desc = (loc_sdesc_t *) malloc(size);
972 if (desc == NULL) {
973 fibril_mutex_unlock(&services_list_mutex);
974 async_answer_0(callid, ENOMEM);
975 async_answer_0(iid, ENOMEM);
976 return;
977 }
978
979 size_t pos = 0;
980 list_foreach(namespaces_list, item) {
981 loc_namespace_t *namespace =
982 list_get_instance(item, loc_namespace_t, namespaces);
983
984 desc[pos].id = namespace->id;
985 str_cpy(desc[pos].name, LOC_NAME_MAXLEN, namespace->name);
986 pos++;
987 }
988
989 sysarg_t retval = async_data_read_finalize(callid, desc, size);
990
991 free(desc);
992 fibril_mutex_unlock(&services_list_mutex);
993
994 async_answer_0(iid, retval);
995}
996
997static void loc_get_services(ipc_callid_t iid, ipc_call_t *icall)
998{
999 /* FIXME: Use faster algorithm which can make better use
1000 of namespaces */
1001
1002 ipc_callid_t callid;
1003 size_t size;
1004 if (!async_data_read_receive(&callid, &size)) {
1005 async_answer_0(callid, EREFUSED);
1006 async_answer_0(iid, EREFUSED);
1007 return;
1008 }
1009
1010 if ((size % sizeof(loc_sdesc_t)) != 0) {
1011 async_answer_0(callid, EINVAL);
1012 async_answer_0(iid, EINVAL);
1013 return;
1014 }
1015
1016 fibril_mutex_lock(&services_list_mutex);
1017
1018 loc_namespace_t *namespace =
1019 loc_namespace_find_id(IPC_GET_ARG1(*icall));
1020 if (namespace == NULL) {
1021 fibril_mutex_unlock(&services_list_mutex);
1022 async_answer_0(callid, ENOENT);
1023 async_answer_0(iid, ENOENT);
1024 return;
1025 }
1026
1027 size_t count = size / sizeof(loc_sdesc_t);
1028 if (count != namespace->refcnt) {
1029 fibril_mutex_unlock(&services_list_mutex);
1030 async_answer_0(callid, EOVERFLOW);
1031 async_answer_0(iid, EOVERFLOW);
1032 return;
1033 }
1034
1035 loc_sdesc_t *desc = (loc_sdesc_t *) malloc(size);
1036 if (desc == NULL) {
1037 fibril_mutex_unlock(&services_list_mutex);
1038 async_answer_0(callid, ENOMEM);
1039 async_answer_0(iid, EREFUSED);
1040 return;
1041 }
1042
1043 size_t pos = 0;
1044 list_foreach(services_list, item) {
1045 loc_service_t *service =
1046 list_get_instance(item, loc_service_t, services);
1047
1048 if (service->namespace == namespace) {
1049 desc[pos].id = service->id;
1050 str_cpy(desc[pos].name, LOC_NAME_MAXLEN, service->name);
1051 pos++;
1052 }
1053 }
1054
1055 sysarg_t retval = async_data_read_finalize(callid, desc, size);
1056
1057 free(desc);
1058 fibril_mutex_unlock(&services_list_mutex);
1059
1060 async_answer_0(iid, retval);
1061}
1062
1063static void loc_category_get_svcs(ipc_callid_t iid, ipc_call_t *icall)
1064{
1065 ipc_callid_t callid;
1066 size_t size;
1067 size_t act_size;
1068 int rc;
1069
1070 if (!async_data_read_receive(&callid, &size)) {
1071 async_answer_0(callid, EREFUSED);
1072 async_answer_0(iid, EREFUSED);
1073 return;
1074 }
1075
1076 fibril_mutex_lock(&cdir.mutex);
1077
1078 category_t *cat = category_get(&cdir, IPC_GET_ARG1(*icall));
1079 if (cat == NULL) {
1080 fibril_mutex_unlock(&cdir.mutex);
1081 async_answer_0(callid, ENOENT);
1082 async_answer_0(iid, ENOENT);
1083 return;
1084 }
1085
1086 category_id_t *id_buf = (category_id_t *) malloc(size);
1087 if (id_buf == NULL) {
1088 fibril_mutex_unlock(&cdir.mutex);
1089 async_answer_0(callid, ENOMEM);
1090 async_answer_0(iid, ENOMEM);
1091 return;
1092 }
1093
1094 fibril_mutex_lock(&cat->mutex);
1095
1096 rc = category_get_services(cat, id_buf, size, &act_size);
1097 if (rc != EOK) {
1098 fibril_mutex_unlock(&cat->mutex);
1099 fibril_mutex_unlock(&cdir.mutex);
1100 async_answer_0(callid, rc);
1101 async_answer_0(iid, rc);
1102 return;
1103 }
1104
1105 fibril_mutex_unlock(&cat->mutex);
1106 fibril_mutex_unlock(&cdir.mutex);
1107
1108 sysarg_t retval = async_data_read_finalize(callid, id_buf, size);
1109 free(id_buf);
1110
1111 async_answer_1(iid, retval, act_size);
1112}
1113
1114
1115static void loc_null_create(ipc_callid_t iid, ipc_call_t *icall)
1116{
1117 fibril_mutex_lock(&null_services_mutex);
1118
1119 unsigned int i;
1120 bool fnd = false;
1121
1122 for (i = 0; i < NULL_SERVICES; i++) {
1123 if (null_services[i] == NULL) {
1124 fnd = true;
1125 break;
1126 }
1127 }
1128
1129 if (!fnd) {
1130 fibril_mutex_unlock(&null_services_mutex);
1131 async_answer_0(iid, ENOMEM);
1132 return;
1133 }
1134
1135 char null[LOC_NAME_MAXLEN];
1136 snprintf(null, LOC_NAME_MAXLEN, "%u", i);
1137
1138 char *dev_name = str_dup(null);
1139 if (dev_name == NULL) {
1140 fibril_mutex_unlock(&null_services_mutex);
1141 async_answer_0(iid, ENOMEM);
1142 return;
1143 }
1144
1145 loc_service_t *service =
1146 (loc_service_t *) malloc(sizeof(loc_service_t));
1147 if (service == NULL) {
1148 fibril_mutex_unlock(&null_services_mutex);
1149 async_answer_0(iid, ENOMEM);
1150 return;
1151 }
1152
1153 fibril_mutex_lock(&services_list_mutex);
1154
1155 loc_namespace_t *namespace = loc_namespace_create("null");
1156 if (namespace == NULL) {
1157 fibril_mutex_lock(&services_list_mutex);
1158 fibril_mutex_unlock(&null_services_mutex);
1159 async_answer_0(iid, ENOMEM);
1160 return;
1161 }
1162
1163 link_initialize(&service->services);
1164 link_initialize(&service->server_services);
1165
1166 /* Get unique service ID */
1167 service->id = loc_create_id();
1168 service->server = NULL;
1169
1170 loc_namespace_addref(namespace, service);
1171 service->name = dev_name;
1172
1173 /*
1174 * Insert service into list of all services and into null services array.
1175 * Insert service into a dummy list of null server's services so that it
1176 * can be safely removed later.
1177 */
1178 list_append(&service->services, &services_list);
1179 list_append(&service->server_services, &dummy_null_services);
1180 null_services[i] = service;
1181
1182 fibril_mutex_unlock(&services_list_mutex);
1183 fibril_mutex_unlock(&null_services_mutex);
1184
1185 async_answer_1(iid, EOK, (sysarg_t) i);
1186}
1187
1188static void loc_null_destroy(ipc_callid_t iid, ipc_call_t *icall)
1189{
1190 sysarg_t i = IPC_GET_ARG1(*icall);
1191 if (i >= NULL_SERVICES) {
1192 async_answer_0(iid, ELIMIT);
1193 return;
1194 }
1195
1196 fibril_mutex_lock(&null_services_mutex);
1197
1198 if (null_services[i] == NULL) {
1199 fibril_mutex_unlock(&null_services_mutex);
1200 async_answer_0(iid, ENOENT);
1201 return;
1202 }
1203
1204 fibril_mutex_lock(&services_list_mutex);
1205 fibril_mutex_lock(&cdir.mutex);
1206 loc_service_unregister_core(null_services[i]);
1207 fibril_mutex_unlock(&cdir.mutex);
1208 fibril_mutex_unlock(&services_list_mutex);
1209
1210 null_services[i] = NULL;
1211
1212 fibril_mutex_unlock(&null_services_mutex);
1213 async_answer_0(iid, EOK);
1214}
1215
1216static void loc_service_add_to_cat(ipc_callid_t iid, ipc_call_t *icall)
1217{
1218 category_t *cat;
1219 loc_service_t *svc;
1220 catid_t cat_id;
1221 service_id_t svc_id;
1222 sysarg_t retval;
1223
1224 svc_id = IPC_GET_ARG1(*icall);
1225 cat_id = IPC_GET_ARG2(*icall);
1226
1227 fibril_mutex_lock(&services_list_mutex);
1228 fibril_mutex_lock(&cdir.mutex);
1229
1230 cat = category_get(&cdir, cat_id);
1231 svc = loc_service_find_id(svc_id);
1232
1233 if (cat == NULL || svc == NULL) {
1234 fibril_mutex_unlock(&cdir.mutex);
1235 fibril_mutex_unlock(&services_list_mutex);
1236 async_answer_0(iid, ENOENT);
1237 return;
1238 }
1239
1240 fibril_mutex_lock(&cat->mutex);
1241 retval = category_add_service(cat, svc);
1242
1243 fibril_mutex_unlock(&cat->mutex);
1244 fibril_mutex_unlock(&cdir.mutex);
1245 fibril_mutex_unlock(&services_list_mutex);
1246
1247 async_answer_0(iid, retval);
1248
1249 loc_category_change_event();
1250}
1251
1252
1253/** Initialize location service.
1254 *
1255 *
1256 */
1257static bool loc_init(void)
1258{
1259 unsigned int i;
1260 category_t *cat;
1261
1262 for (i = 0; i < NULL_SERVICES; i++)
1263 null_services[i] = NULL;
1264
1265 categ_dir_init(&cdir);
1266
1267 cat = category_new("bd");
1268 categ_dir_add_cat(&cdir, cat);
1269
1270 cat = category_new("keyboard");
1271 categ_dir_add_cat(&cdir, cat);
1272
1273 cat = category_new("mouse");
1274 categ_dir_add_cat(&cdir, cat);
1275
1276 cat = category_new("serial");
1277 categ_dir_add_cat(&cdir, cat);
1278
1279 cat = category_new("test3");
1280 categ_dir_add_cat(&cdir, cat);
1281
1282 cat = category_new("usbhc");
1283 categ_dir_add_cat(&cdir, cat);
1284
1285 cat = category_new("virt-null");
1286 categ_dir_add_cat(&cdir, cat);
1287
1288 cat = category_new("virtual");
1289 categ_dir_add_cat(&cdir, cat);
1290
1291
1292 return true;
1293}
1294
1295/** Handle connection on supplier port.
1296 *
1297 */
1298static void loc_connection_supplier(ipc_callid_t iid, ipc_call_t *icall)
1299{
1300 /* Accept connection */
1301 async_answer_0(iid, EOK);
1302
1303 loc_server_t *server = loc_server_register();
1304 if (server == NULL)
1305 return;
1306
1307 while (true) {
1308 ipc_call_t call;
1309 ipc_callid_t callid = async_get_call(&call);
1310
1311 if (!IPC_GET_IMETHOD(call))
1312 break;
1313
1314 switch (IPC_GET_IMETHOD(call)) {
1315 case LOC_SERVER_UNREGISTER:
1316 if (server == NULL)
1317 async_answer_0(callid, ENOENT);
1318 else
1319 async_answer_0(callid, EOK);
1320 break;
1321 case LOC_SERVICE_ADD_TO_CAT:
1322 /* Add service to category */
1323 loc_service_add_to_cat(callid, &call);
1324 break;
1325 case LOC_SERVICE_REGISTER:
1326 /* Register one service */
1327 loc_service_register(callid, &call, server);
1328 break;
1329 case LOC_SERVICE_UNREGISTER:
1330 /* Remove one service */
1331 loc_service_unregister(callid, &call, server);
1332 break;
1333 case LOC_SERVICE_GET_ID:
1334 loc_service_get_id(callid, &call);
1335 break;
1336 case LOC_NAMESPACE_GET_ID:
1337 loc_namespace_get_id(callid, &call);
1338 break;
1339 default:
1340 async_answer_0(callid, ENOENT);
1341 }
1342 }
1343
1344 if (server != NULL) {
1345 /*
1346 * Unregister the server and all its services.
1347 */
1348 loc_server_unregister(server);
1349 server = NULL;
1350 }
1351}
1352
1353/** Handle connection on consumer port.
1354 *
1355 */
1356static void loc_connection_consumer(ipc_callid_t iid, ipc_call_t *icall)
1357{
1358 /* Accept connection */
1359 async_answer_0(iid, EOK);
1360
1361 while (true) {
1362 ipc_call_t call;
1363 ipc_callid_t callid = async_get_call(&call);
1364
1365 if (!IPC_GET_IMETHOD(call))
1366 break;
1367
1368 switch (IPC_GET_IMETHOD(call)) {
1369 case LOC_SERVICE_GET_ID:
1370 loc_service_get_id(callid, &call);
1371 break;
1372 case LOC_SERVICE_GET_NAME:
1373 loc_service_get_name(callid, &call);
1374 break;
1375 case LOC_NAMESPACE_GET_ID:
1376 loc_namespace_get_id(callid, &call);
1377 break;
1378 case LOC_CALLBACK_CREATE:
1379 loc_callback_create(callid, &call);
1380 break;
1381 case LOC_CATEGORY_GET_ID:
1382 loc_category_get_id(callid, &call);
1383 break;
1384 case LOC_CATEGORY_GET_NAME:
1385 loc_category_get_name(callid, &call);
1386 break;
1387 case LOC_CATEGORY_GET_SVCS:
1388 loc_category_get_svcs(callid, &call);
1389 break;
1390 case LOC_ID_PROBE:
1391 loc_id_probe(callid, &call);
1392 break;
1393 case LOC_NULL_CREATE:
1394 loc_null_create(callid, &call);
1395 break;
1396 case LOC_NULL_DESTROY:
1397 loc_null_destroy(callid, &call);
1398 break;
1399 case LOC_GET_NAMESPACE_COUNT:
1400 loc_get_namespace_count(callid, &call);
1401 break;
1402 case LOC_GET_SERVICE_COUNT:
1403 loc_get_service_count(callid, &call);
1404 break;
1405 case LOC_GET_CATEGORIES:
1406 loc_get_categories(callid, &call);
1407 break;
1408 case LOC_GET_NAMESPACES:
1409 loc_get_namespaces(callid, &call);
1410 break;
1411 case LOC_GET_SERVICES:
1412 loc_get_services(callid, &call);
1413 break;
1414 default:
1415 async_answer_0(callid, ENOENT);
1416 }
1417 }
1418}
1419
1420/** Function for handling connections to location service
1421 *
1422 */
1423static void loc_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
1424{
1425 /* Select interface */
1426 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
1427 case LOC_PORT_SUPPLIER:
1428 loc_connection_supplier(iid, icall);
1429 break;
1430 case LOC_PORT_CONSUMER:
1431 loc_connection_consumer(iid, icall);
1432 break;
1433 case LOC_CONNECT_TO_SERVICE:
1434 /* Connect client to selected service */
1435 loc_forward(iid, icall);
1436 break;
1437 default:
1438 /* No such interface */
1439 async_answer_0(iid, ENOENT);
1440 }
1441}
1442
1443/**
1444 *
1445 */
1446int main(int argc, char *argv[])
1447{
1448 printf("%s: HelenOS Location Service\n", NAME);
1449
1450 if (!loc_init()) {
1451 printf("%s: Error while initializing service\n", NAME);
1452 return -1;
1453 }
1454
1455 /* Set a handler of incomming connections */
1456 async_set_client_connection(loc_connection);
1457
1458 /* Register location service at naming service */
1459 if (service_register(SERVICE_LOC) != EOK)
1460 return -1;
1461
1462 printf("%s: Accepting connections\n", NAME);
1463 async_manager();
1464
1465 /* Never reached */
1466 return 0;
1467}
1468
1469/**
1470 * @}
1471 */
Note: See TracBrowser for help on using the repository browser.