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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 68f0c3a was 25d2de69, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cleanup networking module_map ADT.

  • 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 <err.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 * @returns EOK on success.
62 * @returns ENOMEM if there is not enough memory left.
63 */
64int
65add_module(module_ref *module, modules_ref modules, const char *name,
66 const char *filename, services_t service, task_id_t task_id,
67 connect_module_t connect_module)
68{
69 ERROR_DECLARE;
70
71 module_ref tmp_module;
72
73 tmp_module = (module_ref) malloc(sizeof(module_t));
74 if (!tmp_module)
75 return ENOMEM;
76
77 tmp_module->task_id = task_id;
78 tmp_module->phone = 0;
79 tmp_module->usage = 0;
80 tmp_module->name = name;
81 tmp_module->filename = filename;
82 tmp_module->service = service;
83 tmp_module->connect_module = connect_module;
84
85 if (ERROR_OCCURRED(modules_add(modules, tmp_module->name, 0,
86 tmp_module))) {
87 free(tmp_module);
88 return ERROR_CODE;
89 }
90 if (module)
91 *module = tmp_module;
92
93 return EOK;
94}
95
96/** Searches and returns the specified module.
97 *
98 * If the module is not running, the module filaname is spawned.
99 * If the module is not connected, the connect_function is called.
100 *
101 * @param[in] modules The module map.
102 * @param[in] name The module name.
103 * @returns The running module found. It does not have to be
104 * connected.
105 * @returns NULL if there is no such module.
106 */
107module_ref get_running_module(modules_ref modules, char *name)
108{
109 module_ref module;
110
111 module = modules_find(modules, name, 0);
112 if (!module)
113 return NULL;
114
115 if (!module->task_id) {
116 module->task_id = spawn(module->filename);
117 if (!module->task_id)
118 return NULL;
119 }
120 if (!module->phone)
121 module->phone = module->connect_module(module->service);
122
123 return module;
124}
125
126/** Starts the given module.
127 *
128 * @param[in] fname The module full or relative path filename.
129 * @returns The new module task identifier on success.
130 * @returns Zero if there is no such module.
131 */
132task_id_t spawn(const char *fname)
133{
134 const char *argv[2];
135 task_id_t res;
136
137 argv[0] = fname;
138 argv[1] = NULL;
139 res = task_spawn(fname, argv, NULL);
140
141 return res;
142}
143
144/** @}
145 */
Note: See TracBrowser for help on using the repository browser.