source: mainline/uspace/lib/net/adt/module_map.c@ d3ce33fa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d3ce33fa was 61bfc370, checked in by Martin Decky <martin@…>, 14 years ago
  • make measured_string and other network-related data types binary-safe
  • fix several network-related routines binary-safe (replace clearly suspicious use of str_lcmp() with bcmp())
  • rename spawn() to net_spawn()
  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
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 libnet
30 * @{
31 */
32
33/** @file
34 * Character string to module map implementation.
35 */
36
37#include <malloc.h>
38#include <task.h>
39#include <unistd.h>
40#include <errno.h>
41
42#include <ipc/services.h>
43
44#include <net/modules.h>
45
46#include <adt/generic_char_map.h>
47#include <adt/module_map.h>
48
49GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t)
50
51/** Adds module to the module map.
52 *
53 * @param[out] module The module structure added.
54 * @param[in] modules The module map.
55 * @param[in] name The module name.
56 * @param[in] filename The full path filename.
57 * @param[in] service The module service.
58 * @param[in] task_id The module current task identifier. Zero means not
59 * running.
60 * @param[in] connect_module The module connecting function.
61 * @return EOK on success.
62 * @return ENOMEM if there is not enough memory left.
63 */
64int
65add_module(module_t **module, modules_t *modules, const uint8_t *name,
66 const uint8_t *filename, services_t service, task_id_t task_id,
67 connect_module_t connect_module)
68{
69 module_t *tmp_module;
70 int rc;
71
72 tmp_module = (module_t *) malloc(sizeof(module_t));
73 if (!tmp_module)
74 return ENOMEM;
75
76 tmp_module->task_id = task_id;
77 tmp_module->phone = 0;
78 tmp_module->usage = 0;
79 tmp_module->name = name;
80 tmp_module->filename = filename;
81 tmp_module->service = service;
82 tmp_module->connect_module = connect_module;
83
84 rc = modules_add(modules, tmp_module->name, 0, tmp_module);
85 if (rc != EOK) {
86 free(tmp_module);
87 return rc;
88 }
89 if (module)
90 *module = tmp_module;
91
92 return EOK;
93}
94
95/** Searches and returns the specified module.
96 *
97 * If the module is not running, the module filaname is spawned.
98 * If the module is not connected, the connect_function is called.
99 *
100 * @param[in] modules The module map.
101 * @param[in] name The module name.
102 * @return The running module found. It does not have to be
103 * connected.
104 * @return NULL if there is no such module.
105 */
106module_t *get_running_module(modules_t *modules, uint8_t *name)
107{
108 module_t *module;
109
110 module = modules_find(modules, name, 0);
111 if (!module)
112 return NULL;
113
114 if (!module->task_id) {
115 module->task_id = net_spawn(module->filename);
116 if (!module->task_id)
117 return NULL;
118 }
119 if (!module->phone)
120 module->phone = module->connect_module(module->service);
121
122 return module;
123}
124
125/** Start the given module.
126 *
127 * @param[in] fname The module full or relative path filename.
128 *
129 * @return The new module task identifier on success.
130 * @return Zero if there is no such module.
131 *
132 */
133task_id_t net_spawn(const uint8_t *fname)
134{
135 task_id_t id;
136 int rc;
137
138 rc = task_spawnl(&id, (const char *) fname, (const char *) fname, NULL);
139 if (rc != EOK)
140 return 0;
141
142 return id;
143}
144
145/** @}
146 */
Note: See TracBrowser for help on using the repository browser.