make vfs & filesystems use failable copying
[minix3.git] / lib / libsys / ds.c
blob38d914e88f6bdd40ff5e51ea2e585e96170bc5cd
2 #include <minix/ds.h>
3 #include <string.h>
5 #include "syslib.h"
7 static int do_invoke_ds(message *m, int type, const char *ds_name)
9 cp_grant_id_t g_key;
10 size_t len_key;
11 int access, r;
13 if(type == DS_CHECK || type == DS_RETRIEVE_LABEL) {
14 len_key = DS_MAX_KEYLEN;
15 access = CPF_WRITE;
16 } else {
17 len_key = strlen(ds_name)+1;
18 access = CPF_READ;
21 /* Grant for key. */
22 g_key = cpf_grant_direct(DS_PROC_NR, (vir_bytes) ds_name,
23 len_key, access);
24 if(!GRANT_VALID(g_key))
25 return ENOMEM;
27 m->DS_KEY_GRANT = g_key;
28 m->DS_KEY_LEN = len_key;
30 r = _taskcall(DS_PROC_NR, type, m);
32 cpf_revoke(g_key);
33 return r;
36 int ds_publish_label(const char *ds_name, endpoint_t endpoint, int flags)
38 message m;
40 memset(&m, 0, sizeof(m));
41 m.DS_VAL = (u32_t) endpoint;
42 m.DS_FLAGS = DSF_TYPE_LABEL | flags;
43 return do_invoke_ds(&m, DS_PUBLISH, ds_name);
46 int ds_publish_u32(const char *ds_name, u32_t value, int flags)
48 message m;
50 memset(&m, 0, sizeof(m));
51 m.DS_VAL = value;
52 m.DS_FLAGS = DSF_TYPE_U32 | flags;
53 return do_invoke_ds(&m, DS_PUBLISH, ds_name);
56 static int ds_publish_raw(const char *ds_name, void *vaddr, size_t length,
57 int flags)
59 cp_grant_id_t gid;
60 message m;
61 int r;
63 /* Grant for memory range. */
64 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, length, CPF_READ);
65 if(!GRANT_VALID(gid))
66 return ENOMEM;
68 memset(&m, 0, sizeof(m));
69 m.DS_VAL = gid;
70 m.DS_VAL_LEN = length;
71 m.DS_FLAGS = flags;
73 r = do_invoke_ds(&m, DS_PUBLISH, ds_name);
74 cpf_revoke(gid);
76 return r;
79 int ds_publish_str(const char *ds_name, char *value, int flags)
81 size_t length;
82 length = strlen(value) + 1;
83 value[length - 1] = '\0';
84 return ds_publish_raw(ds_name, value, length, flags | DSF_TYPE_STR);
87 int ds_publish_mem(const char *ds_name, void *vaddr, size_t length, int flags)
89 return ds_publish_raw(ds_name, vaddr, length, flags | DSF_TYPE_MEM);
92 int ds_retrieve_label_name(char *ds_name, endpoint_t endpoint)
94 message m;
95 int r;
97 memset(&m, 0, sizeof(m));
98 m.DS_VAL = (u32_t) endpoint;
99 r = do_invoke_ds(&m, DS_RETRIEVE_LABEL, ds_name);
100 return r;
103 int ds_retrieve_label_endpt(const char *ds_name, endpoint_t *endpoint)
105 message m;
106 int r;
108 memset(&m, 0, sizeof(m));
109 m.DS_FLAGS = DSF_TYPE_LABEL;
110 r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
111 *endpoint = (endpoint_t) m.DS_VAL;
112 return r;
115 int ds_retrieve_u32(const char *ds_name, u32_t *value)
117 message m;
118 int r;
120 memset(&m, 0, sizeof(m));
121 m.DS_FLAGS = DSF_TYPE_U32;
122 r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
123 *value = m.DS_VAL;
124 return r;
127 static int ds_retrieve_raw(const char *ds_name, char *vaddr, size_t *length,
128 int flags)
130 message m;
131 cp_grant_id_t gid;
132 int r;
134 /* Grant for memory range. */
135 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, *length, CPF_WRITE);
136 if(!GRANT_VALID(gid))
137 return ENOMEM;
139 memset(&m, 0, sizeof(m));
140 m.DS_VAL = gid;
141 m.DS_VAL_LEN = *length;
142 m.DS_FLAGS = flags;
143 r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
144 *length = m.DS_VAL_LEN;
145 cpf_revoke(gid);
147 return r;
150 int ds_retrieve_str(const char *ds_name, char *value, size_t len_str)
152 int r;
153 size_t length = len_str + 1;
154 r = ds_retrieve_raw(ds_name, value, &length, DSF_TYPE_STR);
155 value[length - 1] = '\0';
156 return r;
159 int ds_retrieve_mem(const char *ds_name, char *vaddr, size_t *length)
161 return ds_retrieve_raw(ds_name, vaddr, length, DSF_TYPE_MEM);
164 int ds_delete_u32(const char *ds_name)
166 message m;
168 memset(&m, 0, sizeof(m));
169 m.DS_FLAGS = DSF_TYPE_U32;
170 return do_invoke_ds(&m, DS_DELETE, ds_name);
173 int ds_delete_str(const char *ds_name)
175 message m;
177 memset(&m, 0, sizeof(m));
178 m.DS_FLAGS = DSF_TYPE_STR;
179 return do_invoke_ds(&m, DS_DELETE, ds_name);
182 int ds_delete_mem(const char *ds_name)
184 message m;
186 memset(&m, 0, sizeof(m));
187 m.DS_FLAGS = DSF_TYPE_MEM;
188 return do_invoke_ds(&m, DS_DELETE, ds_name);
191 int ds_delete_label(const char *ds_name)
193 message m;
195 memset(&m, 0, sizeof(m));
196 m.DS_FLAGS = DSF_TYPE_LABEL;
197 return do_invoke_ds(&m, DS_DELETE, ds_name);
200 int ds_subscribe(const char *regexp, int flags)
202 message m;
204 memset(&m, 0, sizeof(m));
205 m.DS_FLAGS = flags;
206 return do_invoke_ds(&m, DS_SUBSCRIBE, regexp);
209 int ds_check(char *ds_key, int *type, endpoint_t *owner_e)
211 message m;
212 int r;
214 memset(&m, 0, sizeof(m));
215 r = do_invoke_ds(&m, DS_CHECK, ds_key);
216 if(type) *type = m.DS_FLAGS;
217 if(owner_e) *owner_e = m.DS_OWNER;
218 return r;