__aeabi_ldivmod: fix sign logic
[minix.git] / lib / libsys / ds.c
blob371400b188293fcfb0cb7b9f22b3864012957cf8
2 #include <minix/ds.h>
3 #include <string.h>
5 #include "syslib.h"
7 static message m;
9 static int do_invoke_ds(int type, const char *ds_name)
11 cp_grant_id_t g_key;
12 size_t len_key;
13 int access, r;
15 if(type == DS_CHECK || type == DS_RETRIEVE_LABEL) {
16 len_key = DS_MAX_KEYLEN;
17 access = CPF_WRITE;
18 } else {
19 len_key = strlen(ds_name)+1;
20 access = CPF_READ;
23 /* Grant for key. */
24 g_key = cpf_grant_direct(DS_PROC_NR, (vir_bytes) ds_name,
25 len_key, access);
26 if(!GRANT_VALID(g_key))
27 return errno;
29 m.DS_KEY_GRANT = g_key;
30 m.DS_KEY_LEN = len_key;
32 r = _taskcall(DS_PROC_NR, type, &m);
34 cpf_revoke(g_key);
35 return r;
38 int ds_publish_label(const char *ds_name, endpoint_t endpoint, int flags)
40 m.DS_VAL = (u32_t) endpoint;
41 m.DS_FLAGS = DSF_TYPE_LABEL | flags;
42 return do_invoke_ds(DS_PUBLISH, ds_name);
45 int ds_publish_u32(const char *ds_name, u32_t value, int flags)
47 m.DS_VAL = value;
48 m.DS_FLAGS = DSF_TYPE_U32 | flags;
49 return do_invoke_ds(DS_PUBLISH, ds_name);
52 static int ds_publish_raw(const char *ds_name, void *vaddr, size_t length,
53 int flags)
55 cp_grant_id_t gid;
56 int r;
58 /* Grant for memory range. */
59 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, length, CPF_READ);
60 if(!GRANT_VALID(gid))
61 return errno;
63 m.DS_VAL = gid;
64 m.DS_VAL_LEN = length;
65 m.DS_FLAGS = flags;
67 r = do_invoke_ds(DS_PUBLISH, ds_name);
68 cpf_revoke(gid);
70 return r;
73 int ds_publish_str(const char *ds_name, char *value, int flags)
75 size_t length;
76 length = strlen(value) + 1;
77 value[length - 1] = '\0';
78 return ds_publish_raw(ds_name, value, length, flags | DSF_TYPE_STR);
81 int ds_publish_mem(const char *ds_name, void *vaddr, size_t length, int flags)
83 return ds_publish_raw(ds_name, vaddr, length, flags | DSF_TYPE_MEM);
86 int ds_snapshot_map(const char *ds_name, int *nr_snapshot)
88 int r;
89 r = do_invoke_ds(DS_SNAPSHOT, ds_name);
90 *nr_snapshot = m.DS_NR_SNAPSHOT;
91 return r;
94 int ds_retrieve_label_name(char *ds_name, endpoint_t endpoint)
96 int r;
97 m.DS_VAL = (u32_t) endpoint;
98 r = do_invoke_ds(DS_RETRIEVE_LABEL, ds_name);
99 return r;
102 int ds_retrieve_label_endpt(const char *ds_name, endpoint_t *endpoint)
104 int r;
105 m.DS_FLAGS = DSF_TYPE_LABEL;
106 r = do_invoke_ds(DS_RETRIEVE, ds_name);
107 *endpoint = (endpoint_t) m.DS_VAL;
108 return r;
111 int ds_retrieve_u32(const char *ds_name, u32_t *value)
113 int r;
114 m.DS_FLAGS = DSF_TYPE_U32;
115 r = do_invoke_ds(DS_RETRIEVE, ds_name);
116 *value = m.DS_VAL;
117 return r;
120 static int ds_retrieve_raw(const char *ds_name, char *vaddr, size_t *length,
121 int flags)
123 cp_grant_id_t gid;
124 int r;
126 /* Grant for memory range. */
127 gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, *length, CPF_WRITE);
128 if(!GRANT_VALID(gid))
129 return errno;
131 m.DS_VAL = gid;
132 m.DS_VAL_LEN = *length;
133 m.DS_FLAGS = flags;
134 r = do_invoke_ds(DS_RETRIEVE, ds_name);
135 *length = m.DS_VAL_LEN;
136 cpf_revoke(gid);
138 return r;
141 int ds_retrieve_str(const char *ds_name, char *value, size_t len_str)
143 int r;
144 size_t length = len_str + 1;
145 r = ds_retrieve_raw(ds_name, value, &length, DSF_TYPE_STR);
146 value[length - 1] = '\0';
147 return r;
150 int ds_retrieve_mem(const char *ds_name, char *vaddr, size_t *length)
152 return ds_retrieve_raw(ds_name, vaddr, length, DSF_TYPE_MEM);
155 int ds_delete_u32(const char *ds_name)
157 m.DS_FLAGS = DSF_TYPE_U32;
158 return do_invoke_ds(DS_DELETE, ds_name);
161 int ds_delete_str(const char *ds_name)
163 m.DS_FLAGS = DSF_TYPE_STR;
164 return do_invoke_ds(DS_DELETE, ds_name);
167 int ds_delete_mem(const char *ds_name)
169 m.DS_FLAGS = DSF_TYPE_MEM;
170 return do_invoke_ds(DS_DELETE, ds_name);
173 int ds_delete_label(const char *ds_name)
175 m.DS_FLAGS = DSF_TYPE_LABEL;
176 return do_invoke_ds(DS_DELETE, ds_name);
179 int ds_subscribe(const char *regexp, int flags)
181 m.DS_FLAGS = flags;
182 return do_invoke_ds(DS_SUBSCRIBE, regexp);
185 int ds_check(char *ds_key, int *type, endpoint_t *owner_e)
187 int r;
188 r = do_invoke_ds(DS_CHECK, ds_key);
189 if(type) *type = m.DS_FLAGS;
190 if(owner_e) *owner_e = m.DS_OWNER;
191 return r;