1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
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 vbd
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <async.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <io/log.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <ipc/vbd.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <task.h>
|
---|
45 |
|
---|
46 | #include "types/vbd.h"
|
---|
47 |
|
---|
48 | #define NAME "vbd"
|
---|
49 |
|
---|
50 | static void vbd_client_conn(ipc_callid_t, ipc_call_t *, void *);
|
---|
51 |
|
---|
52 | static int vbd_init(void)
|
---|
53 | {
|
---|
54 | int rc;
|
---|
55 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_init()");
|
---|
56 |
|
---|
57 | async_set_client_connection(vbd_client_conn);
|
---|
58 |
|
---|
59 | rc = loc_server_register(NAME);
|
---|
60 | if (rc != EOK) {
|
---|
61 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server (%d).", rc);
|
---|
62 | return EEXIST;
|
---|
63 | }
|
---|
64 |
|
---|
65 | service_id_t sid;
|
---|
66 | rc = loc_service_register(SERVICE_NAME_VBD, &sid);
|
---|
67 | if (rc != EOK) {
|
---|
68 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service (%d).", rc);
|
---|
69 | return EEXIST;
|
---|
70 | }
|
---|
71 |
|
---|
72 | return EOK;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static int vbd_disk_add(service_id_t sid)
|
---|
76 | {
|
---|
77 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_disk_add(%zu)", sid);
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | static int vbd_disk_remove(service_id_t sid)
|
---|
82 | {
|
---|
83 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_disk_remove(%zu)", sid);
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static int vbd_disk_info(service_id_t sid, vbd_disk_info_t *info)
|
---|
88 | {
|
---|
89 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_disk_info(%zu)", sid);
|
---|
90 | info->ltype = lt_mbr;
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static int vbd_label_create(service_id_t sid, label_type_t ltype)
|
---|
95 | {
|
---|
96 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_label_create(%zu, %d)", sid,
|
---|
97 | ltype);
|
---|
98 | return EOK;
|
---|
99 | }
|
---|
100 |
|
---|
101 | static int vbd_label_delete(service_id_t sid, label_type_t ltype)
|
---|
102 | {
|
---|
103 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_label_delete(%zu, %d)", sid,
|
---|
104 | ltype);
|
---|
105 | return EOK;
|
---|
106 | }
|
---|
107 |
|
---|
108 | static void vbd_disk_add_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
109 | {
|
---|
110 | service_id_t disk_sid;
|
---|
111 | int rc;
|
---|
112 |
|
---|
113 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_disk_add_srv()");
|
---|
114 |
|
---|
115 | disk_sid = IPC_GET_ARG1(*icall);
|
---|
116 | rc = vbd_disk_add(disk_sid);
|
---|
117 | async_answer_0(iid, (sysarg_t) rc);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static void vbd_disk_remove_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
121 | {
|
---|
122 | service_id_t disk_sid;
|
---|
123 | int rc;
|
---|
124 |
|
---|
125 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_disk_remove_srv()");
|
---|
126 |
|
---|
127 | disk_sid = IPC_GET_ARG1(*icall);
|
---|
128 | rc = vbd_disk_remove(disk_sid);
|
---|
129 | async_answer_0(iid, (sysarg_t) rc);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static void vbd_disk_info_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
133 | {
|
---|
134 | service_id_t disk_sid;
|
---|
135 | vbd_disk_info_t dinfo;
|
---|
136 | int rc;
|
---|
137 |
|
---|
138 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_disk_info_srv()");
|
---|
139 |
|
---|
140 | disk_sid = IPC_GET_ARG1(*icall);
|
---|
141 | rc = vbd_disk_info(disk_sid, &dinfo);
|
---|
142 | async_answer_1(iid, (sysarg_t)rc, (sysarg_t)dinfo.ltype);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static void vbd_label_create_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
146 | {
|
---|
147 | service_id_t disk_sid;
|
---|
148 | label_type_t ltype;
|
---|
149 | int rc;
|
---|
150 |
|
---|
151 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_label_create_srv()");
|
---|
152 |
|
---|
153 | disk_sid = IPC_GET_ARG1(*icall);
|
---|
154 | ltype = IPC_GET_ARG2(*icall);
|
---|
155 | rc = vbd_label_create(disk_sid, ltype);
|
---|
156 | async_answer_0(iid, (sysarg_t) rc);
|
---|
157 | }
|
---|
158 |
|
---|
159 | static void vbd_label_delete_srv(ipc_callid_t iid, ipc_call_t *icall)
|
---|
160 | {
|
---|
161 | service_id_t disk_sid;
|
---|
162 | label_type_t ltype;
|
---|
163 | int rc;
|
---|
164 |
|
---|
165 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_label_delete_srv()");
|
---|
166 |
|
---|
167 | disk_sid = IPC_GET_ARG1(*icall);
|
---|
168 | ltype = IPC_GET_ARG2(*icall);
|
---|
169 | rc = vbd_label_delete(disk_sid, ltype);
|
---|
170 | async_answer_0(iid, (sysarg_t) rc);
|
---|
171 | }
|
---|
172 |
|
---|
173 | static void vbd_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
174 | {
|
---|
175 | log_msg(LOG_DEFAULT, LVL_NOTE, "vbd_client_conn()");
|
---|
176 |
|
---|
177 | /* Accept the connection */
|
---|
178 | async_answer_0(iid, EOK);
|
---|
179 |
|
---|
180 | while (true) {
|
---|
181 | ipc_call_t call;
|
---|
182 | ipc_callid_t callid = async_get_call(&call);
|
---|
183 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
184 |
|
---|
185 | if (!method) {
|
---|
186 | /* The other side has hung up */
|
---|
187 | async_answer_0(callid, EOK);
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | switch (method) {
|
---|
192 | case VBD_DISK_ADD:
|
---|
193 | vbd_disk_add_srv(callid, &call);
|
---|
194 | break;
|
---|
195 | case VBD_DISK_REMOVE:
|
---|
196 | vbd_disk_remove_srv(callid, &call);
|
---|
197 | break;
|
---|
198 | case VBD_DISK_INFO:
|
---|
199 | vbd_disk_info_srv(callid, &call);
|
---|
200 | break;
|
---|
201 | case VBD_LABEL_CREATE:
|
---|
202 | vbd_label_create_srv(callid, &call);
|
---|
203 | break;
|
---|
204 | case VBD_LABEL_DELETE:
|
---|
205 | vbd_label_delete_srv(callid, &call);
|
---|
206 | break;
|
---|
207 | default:
|
---|
208 | async_answer_0(callid, EINVAL);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | int main(int argc, char *argv[])
|
---|
214 | {
|
---|
215 | int rc;
|
---|
216 |
|
---|
217 | printf("%s: Virtual Block Device service\n", NAME);
|
---|
218 |
|
---|
219 | if (log_init(NAME) != EOK) {
|
---|
220 | printf(NAME ": Failed to initialize logging.\n");
|
---|
221 | return 1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | rc = vbd_init();
|
---|
225 | if (rc != EOK)
|
---|
226 | return 1;
|
---|
227 |
|
---|
228 | printf(NAME ": Accepting connections.\n");
|
---|
229 | task_retval(0);
|
---|
230 | async_manager();
|
---|
231 |
|
---|
232 | return 0;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** @}
|
---|
236 | */
|
---|