source: mainline/uspace/lib/sysman/src/ctl.c@ 102f641

Last change on this file since 102f641 was 102f641, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Correcting syntax according to ccheck

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[5559712]1/*
2 * Copyright (c) 2015 Michal Koutny
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#include <async.h>
30#include <errno.h>
[b55f62a]31#include <stdlib.h>
[5559712]32#include <str.h>
33#include <sysman/ctl.h>
34#include <sysman/sysman.h>
35
[241f1985]36errno_t sysman_unit_handle(const char *unit_name, unit_handle_t *handle_ptr)
[ed5367b]37{
38 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
39
40 ipc_call_t call;
41 aid_t req = async_send_0(exch, SYSMAN_CTL_UNIT_HANDLE, &call);
[241f1985]42 errno_t rc = async_data_write_start(exch, unit_name, str_size(unit_name));
[ed5367b]43 sysman_exchange_end(exch);
44
45 if (rc != EOK) {
46 async_forget(req);
47 return rc;
48 }
49
50 async_wait_for(req, &rc);
51 if (rc == EOK) {
[241f1985]52 *handle_ptr = ipc_get_arg1(&call);
[ed5367b]53 }
54 return rc;
55}
56
[4224ef7]57/*
58 * TODO
59 * Non-blocking favor of this API is effectively incomplete as it doesn't
60 * provide means how to obtain result of the start operation.
61 * Probably devise individual API for brokers that could exploit the fact that
62 * broker knows when appropriate exposee is created and the request succeeded.
63 * Still though, it's necessary to centralize timeout into sysman.
[ed5367b]64 * TODO convert to name->handle API
[4224ef7]65 */
[241f1985]66errno_t sysman_unit_start_by_name(const char *unit_name, int flags)
[5559712]67{
68 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
69
[504d103]70 aid_t req = async_send_1(exch, SYSMAN_CTL_UNIT_START_BY_NAME, flags, NULL);
[241f1985]71 errno_t rc = async_data_write_start(exch, unit_name, str_size(unit_name));
[5559712]72 sysman_exchange_end(exch);
73
74 if (rc != EOK) {
75 async_forget(req);
76 return rc;
77 }
78
79 async_wait_for(req, &rc);
80 return rc;
81}
[b55f62a]82
[241f1985]83errno_t sysman_unit_start(unit_handle_t handle, int flags)
[8fab3f6]84{
85 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
86
[241f1985]87 errno_t rc = async_req_2_0(exch, SYSMAN_CTL_UNIT_START, handle, flags);
[8fab3f6]88 sysman_exchange_end(exch);
[102f641]89
[8fab3f6]90 return rc;
91}
92
[241f1985]93errno_t sysman_unit_stop(unit_handle_t handle, int flags)
[ed5367b]94{
95 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
96
[241f1985]97 errno_t rc = async_req_2_0(exch, SYSMAN_CTL_UNIT_STOP, handle, flags);
[ed5367b]98 sysman_exchange_end(exch);
[102f641]99
[ed5367b]100 return rc;
101}
102
[241f1985]103static errno_t sysman_get_units_once(sysarg_t *buf, size_t buf_size,
[b55f62a]104 size_t *act_size)
105{
106 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
107
108 ipc_call_t answer;
109 aid_t req = async_send_0(exch, SYSMAN_CTL_GET_UNITS, &answer);
[241f1985]110 errno_t rc = async_data_read_start(exch, buf, buf_size);
[b55f62a]111
112 sysman_exchange_end(exch);
113
114 if (rc != EOK) {
115 async_forget(req);
116 return rc;
117 }
118
[241f1985]119 errno_t retval;
[b55f62a]120 async_wait_for(req, &retval);
121
122 if (retval != EOK) {
123 return retval;
124 }
125
[241f1985]126 *act_size = ipc_get_arg1(&answer);
[b55f62a]127 return EOK;
128}
129
[241f1985]130errno_t sysman_get_units(unit_handle_t **units_ptr, size_t *cnt_ptr)
[b55f62a]131{
132 *units_ptr = NULL;
133 *cnt_ptr = 0;
134
135 unit_handle_t *units = NULL;
136 size_t alloc_size = 0;
137 size_t act_size = 0;
138
139 while (true) {
[241f1985]140 errno_t rc = sysman_get_units_once(units, alloc_size, &act_size);
[b55f62a]141 if (rc != EOK) {
142 return rc;
143 }
144
145 if (act_size <= alloc_size) {
146 break;
147 }
148
149 alloc_size = act_size;
150 units = realloc(units, alloc_size);
151 if (units == NULL) {
152 return ENOMEM;
153 }
154 }
155
156 *units_ptr = units;
157 *cnt_ptr = act_size / sizeof(unit_handle_t);
158 return EOK;
159}
160
[241f1985]161errno_t sysman_unit_get_name(unit_handle_t handle, char *buf, size_t buf_size)
[b55f62a]162{
163 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
164
165 ipc_call_t answer;
166 aid_t req = async_send_1(exch, SYSMAN_CTL_UNIT_GET_NAME, handle, &answer);
[241f1985]167 errno_t rc = async_data_read_start(exch, buf, buf_size);
[b55f62a]168
169 sysman_exchange_end(exch);
170
171 if (rc != EOK) {
172 async_forget(req);
173 return rc;
174 }
175
[241f1985]176 errno_t retval;
[b55f62a]177 async_wait_for(req, &retval);
178
179 if (retval != EOK) {
180 return retval;
181 }
182
183 return EOK;
184}
185
[241f1985]186errno_t sysman_unit_get_state(unit_handle_t handle, unit_state_t *state)
[b55f62a]187{
188 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
[241f1985]189 errno_t rc = async_req_1_1(exch, SYSMAN_CTL_UNIT_GET_STATE, handle, (sysarg_t *)state);
[b55f62a]190 sysman_exchange_end(exch);
191
192 return rc;
193}
[8d74fdd]194
[241f1985]195errno_t sysman_shutdown(void)
[8d74fdd]196{
197 async_exch_t *exch = sysman_exchange_begin(SYSMAN_PORT_CTL);
[241f1985]198 errno_t rc = async_req_0_0(exch, SYSMAN_CTL_SHUTDOWN);
[8d74fdd]199 sysman_exchange_end(exch);
200
201 return rc;
202}
Note: See TracBrowser for help on using the repository browser.