Debian Codegraph @

common/hv_store.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2007 Christophe Fillot ([email protected])
4 *
5 * Hypervisor store.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdarg.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <pthread.h>
24
25 #include "utils.h"
26 #include "base64.h"
27 #include "registry.h"
28 #include "hypervisor.h"
29
30 struct store_object {
31 char *name; /* Object name */
32 void *data; /* Data pointer */
33 size_t len; /* Data length */
34 };
35
36 /* Create an object of the given name */
37 static struct store_object *so_create(char *name,void *data,size_t len)
38 {
39 struct store_object *so;
40
41 if (!(so = malloc(sizeof(*so))))
42 return NULL;
43
44 if (!(so->name = strdup(name)))
45 goto err_name;
46
47 so->data = data;
48 so->len = len;
49
50 if (registry_add(so->name,OBJ_TYPE_STORE,so) == -1)
51 goto err_registry;
52
53 return so;
54
55 err_registry:
56 free(so->name);
57 err_name:
58 free(so);
59 return NULL;
60 }
61
62 /* Delete a store object */
63 static void so_delete(struct store_object *so)
64 {
65 if (so != NULL) {
66 free(so->name);
67 free(so->data);
68 free(so);
69 }
70 }
71
72 /* Free resources used by a store object (registry callback) */
73 static int so_reg_delete(void *data,void *arg)
74 {
75 so_delete((struct store_object *)data);
76 return(TRUE);
77 }
78
79 /* Delete a store object from the registry */
80 static int so_delete_from_registry(char *name)
81 {
82 return(registry_delete_if_unused(name,OBJ_TYPE_STORE,so_reg_delete,NULL));
83 }
84
85 /* Write an object (data provided in base64 encoding) */
86 static int cmd_write(hypervisor_conn_t *conn,int argc,char *argv[])
87 {
88 struct store_object *so;
89 u_char *buffer;
90 ssize_t len;
91
92 /* Convert base64 input to standard text */
93 if (!(buffer = malloc(3 * strlen(argv[1]))))
94 goto err_alloc_base64;
95
96 if ((len = base64_decode(buffer,(u_char *)argv[1],0)) < 0)
97 goto err_decode_base64;
98
99 if (!(so = so_create(argv[0],buffer,len))) {
100 free(buffer);
101 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to store object");
102 return(-1);
103 }
104
105 registry_unref(so->name,OBJ_TYPE_STORE);
106 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
107 return(0);
108
109 err_decode_base64:
110 free(buffer);
111 err_alloc_base64:
112 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,"unable to decode base64");
113 return(-1);
114 }
115
116 /* Read an object and return data in base64 encoding */
117 static int cmd_read(hypervisor_conn_t *conn,int argc,char *argv[])
118 {
119 struct store_object *so;
120 u_char *buffer;
121
122 if (!(so = hypervisor_find_object(conn,argv[0],OBJ_TYPE_STORE)))
123 return(-1);
124
125 /*
126 * Convert data to base64. base64 is about 1/3 larger than input,
127 * let's be on the safe side with twice longer.
128 */
129 if (!(buffer = malloc(so->len * 2)))
130 goto err_alloc_base64;
131
132 base64_encode(buffer,so->data,so->len);
133
134 registry_unref(so->name,OBJ_TYPE_STORE);
135 hypervisor_send_reply(conn,HSC_INFO_OK,1,"%s",buffer);
136 free(buffer);
137 return(0);
138
139 err_alloc_base64:
140 registry_unref(so->name,OBJ_TYPE_STORE);
141
142 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
143 "unable to encode data in base64",
144 argv[0]);
145 return(-1);
146 }
147
148 /* Rename an object */
149 static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[])
150 {
151 struct store_object *so;
152 char *newname;
153
154 if (!(so = hypervisor_find_object(conn,argv[0],OBJ_TYPE_STORE)))
155 return(-1);
156
157 if (registry_exists(argv[1],OBJ_TYPE_STORE)) {
158 registry_unref(so->name,OBJ_TYPE_STORE);
159 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
160 "unable to rename object '%s', '%s' already exists",
161 argv[0],argv[1]);
162 return(-1);
163 }
164
165 if(!(newname = strdup(argv[1]))) {
166 registry_unref(so->name,OBJ_TYPE_STORE);
167 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
168 "unable to rename object '%s', out of memory",
169 argv[0]);
170 return(-1);
171 }
172
173 if (registry_rename(argv[0],newname,OBJ_TYPE_STORE)) {
174 free(newname);
175 registry_unref(so->name,OBJ_TYPE_STORE);
176 hypervisor_send_reply(conn,HSC_ERR_RENAME,1,
177 "unable to rename object '%s'",
178 argv[0]);
179 return(-1);
180 }
181
182 free(so->name);
183 so->name = newname;
184
185 registry_unref(so->name,OBJ_TYPE_STORE);
186 hypervisor_send_reply(conn,HSC_INFO_OK,1,"object '%s' renamed to '%s'",argv[0],argv[1]);
187 return(0);
188 }
189
190 /* Delete an object from the store */
191 static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[])
192 {
193 if (so_delete_from_registry(argv[0]) < 0) {
194 hypervisor_send_reply(conn,HSC_ERR_DELETE,1,"unable to delete object %s",
195 argv[0]);
196 return(-1);
197 } else {
198 hypervisor_send_reply(conn,HSC_INFO_OK,1,"object %s deleted",argv[0]);
199 return(0);
200 }
201 }
202
203 /* Delete all objects from the store */
204 static int cmd_delete_all(hypervisor_conn_t *conn,int argc,char *argv[])
205 {
206 registry_delete_type(OBJ_TYPE_STORE,so_reg_delete,NULL);
207 hypervisor_send_reply(conn,HSC_INFO_OK,1,"object store deleted");
208 return(0);
209 }
210
211 /* Show info about a store object */
212 static void cmd_show_obj_list(registry_entry_t *entry,void *opt,int *err)
213 {
214 hypervisor_conn_t *conn = opt;
215 struct store_object *so = entry->data;
216
217 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s (len=%ld)",
218 entry->name,(long)so->len);
219 }
220
221 /* Object list */
222 static int cmd_obj_list(hypervisor_conn_t *conn,int argc,char *argv[])
223 {
224 int err = 0;
225 registry_foreach_type(OBJ_TYPE_STORE,cmd_show_obj_list,conn,&err);
226 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
227 return(0);
228 }
229
230 /* "store" commands */
231 static hypervisor_cmd_t store_cmd_array[] = {
232 { "write", 2, 2, cmd_write, NULL },
233 { "read", 1, 1, cmd_read, NULL },
234 { "rename", 2, 2, cmd_rename, NULL },
235 { "delete", 1, 1, cmd_delete, NULL },
236 { "delete_all", 0, 0, cmd_delete_all, NULL },
237 { "list", 0, 0, cmd_obj_list, NULL },
238 { NULL, -1, -1, NULL, NULL },
239 };
240
241 /* Hypervisor store initialization */
242 int hypervisor_store_init(void)
243 {
244 hypervisor_module_t *module;
245
246 module = hypervisor_register_module("object_store",NULL);
247 assert(module != NULL);
248
249 hypervisor_register_cmd_array(module,store_cmd_array);
250 return(0);
251 }