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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25d2de69 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
RevLine 
[21580dd]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
[25d2de69]29/** @addtogroup libnet
30 * @{
[21580dd]31 */
32
33/** @file
[25d2de69]34 * Character string to module map implementation.
[21580dd]35 */
36
37#include <malloc.h>
38#include <task.h>
39#include <unistd.h>
[c5b59ce]40#include <err.h>
[21580dd]41
42#include <ipc/services.h>
43
[c7a8442]44#include <net/modules.h>
[21580dd]45
[849ed54]46#include <adt/generic_char_map.h>
47#include <adt/module_map.h>
[21580dd]48
[aadf01e]49GENERIC_CHAR_MAP_IMPLEMENT(modules, module_t)
[21580dd]50
[25d2de69]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{
[21580dd]69 ERROR_DECLARE;
70
[aadf01e]71 module_ref tmp_module;
[21580dd]72
[aadf01e]73 tmp_module = (module_ref) malloc(sizeof(module_t));
[25d2de69]74 if (!tmp_module)
[aadf01e]75 return ENOMEM;
[25d2de69]76
[21580dd]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;
[25d2de69]84
85 if (ERROR_OCCURRED(modules_add(modules, tmp_module->name, 0,
86 tmp_module))) {
[aadf01e]87 free(tmp_module);
[21580dd]88 return ERROR_CODE;
89 }
[25d2de69]90 if (module)
[aadf01e]91 *module = tmp_module;
[25d2de69]92
[21580dd]93 return EOK;
94}
95
[25d2de69]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{
[aadf01e]109 module_ref module;
[21580dd]110
[aadf01e]111 module = modules_find(modules, name, 0);
[25d2de69]112 if (!module)
[aadf01e]113 return NULL;
[25d2de69]114
115 if (!module->task_id) {
[aadf01e]116 module->task_id = spawn(module->filename);
[25d2de69]117 if (!module->task_id)
[aadf01e]118 return NULL;
[21580dd]119 }
[25d2de69]120 if (!module->phone)
[aadf01e]121 module->phone = module->connect_module(module->service);
[25d2de69]122
[21580dd]123 return module;
124}
125
[25d2de69]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 */
[d9fae235]132task_id_t spawn(const char *fname)
133{
134 const char *argv[2];
[aadf01e]135 task_id_t res;
[d9fae235]136
[aadf01e]137 argv[0] = fname;
138 argv[1] = NULL;
[d9fae235]139 res = task_spawn(fname, argv, NULL);
140
[21580dd]141 return res;
142}
143
144/** @}
145 */
Note: See TracBrowser for help on using the repository browser.