xref: /minix3/minix/lib/libsys/ds.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc 
2*433d6423SLionel Sambuc #include <minix/ds.h>
3*433d6423SLionel Sambuc #include <string.h>
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc #include "syslib.h"
6*433d6423SLionel Sambuc 
do_invoke_ds(message * m,int type,const char * ds_name)7*433d6423SLionel Sambuc static int do_invoke_ds(message *m, int type, const char *ds_name)
8*433d6423SLionel Sambuc {
9*433d6423SLionel Sambuc 	cp_grant_id_t g_key;
10*433d6423SLionel Sambuc 	size_t len_key;
11*433d6423SLionel Sambuc 	int access, r;
12*433d6423SLionel Sambuc 
13*433d6423SLionel Sambuc 	if(type == DS_CHECK || type == DS_RETRIEVE_LABEL) {
14*433d6423SLionel Sambuc 		len_key = DS_MAX_KEYLEN;
15*433d6423SLionel Sambuc 		access = CPF_WRITE;
16*433d6423SLionel Sambuc 	} else {
17*433d6423SLionel Sambuc 		len_key = strlen(ds_name)+1;
18*433d6423SLionel Sambuc 		access = CPF_READ;
19*433d6423SLionel Sambuc 	}
20*433d6423SLionel Sambuc 
21*433d6423SLionel Sambuc 	/* Grant for key. */
22*433d6423SLionel Sambuc 	g_key = cpf_grant_direct(DS_PROC_NR, (vir_bytes) ds_name,
23*433d6423SLionel Sambuc 		len_key, access);
24*433d6423SLionel Sambuc 	if(!GRANT_VALID(g_key))
25*433d6423SLionel Sambuc 		return ENOMEM;
26*433d6423SLionel Sambuc 
27*433d6423SLionel Sambuc 	m->m_ds_req.key_grant = g_key;
28*433d6423SLionel Sambuc 	m->m_ds_req.key_len = len_key;
29*433d6423SLionel Sambuc 
30*433d6423SLionel Sambuc 	r = _taskcall(DS_PROC_NR, type, m);
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc 	cpf_revoke(g_key);
33*433d6423SLionel Sambuc 	return r;
34*433d6423SLionel Sambuc }
35*433d6423SLionel Sambuc 
ds_publish_label(const char * ds_name,endpoint_t endpoint,int flags)36*433d6423SLionel Sambuc int ds_publish_label(const char *ds_name, endpoint_t endpoint, int flags)
37*433d6423SLionel Sambuc {
38*433d6423SLionel Sambuc 	message m;
39*433d6423SLionel Sambuc 
40*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
41*433d6423SLionel Sambuc 	m.m_ds_req.val_in.ep = endpoint;
42*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_LABEL | flags;
43*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_PUBLISH, ds_name);
44*433d6423SLionel Sambuc }
45*433d6423SLionel Sambuc 
ds_publish_u32(const char * ds_name,u32_t value,int flags)46*433d6423SLionel Sambuc int ds_publish_u32(const char *ds_name, u32_t value, int flags)
47*433d6423SLionel Sambuc {
48*433d6423SLionel Sambuc 	message m;
49*433d6423SLionel Sambuc 
50*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
51*433d6423SLionel Sambuc 	m.m_ds_req.val_in.u32 = value;
52*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_U32 | flags;
53*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_PUBLISH, ds_name);
54*433d6423SLionel Sambuc }
55*433d6423SLionel Sambuc 
ds_publish_raw(const char * ds_name,void * vaddr,size_t length,int flags)56*433d6423SLionel Sambuc static int ds_publish_raw(const char *ds_name, void *vaddr, size_t length,
57*433d6423SLionel Sambuc 	int flags)
58*433d6423SLionel Sambuc {
59*433d6423SLionel Sambuc 	cp_grant_id_t gid;
60*433d6423SLionel Sambuc 	message m;
61*433d6423SLionel Sambuc 	int r;
62*433d6423SLionel Sambuc 
63*433d6423SLionel Sambuc 	/* Grant for memory range. */
64*433d6423SLionel Sambuc 	gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, length, CPF_READ);
65*433d6423SLionel Sambuc 	if(!GRANT_VALID(gid))
66*433d6423SLionel Sambuc 		return ENOMEM;
67*433d6423SLionel Sambuc 
68*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
69*433d6423SLionel Sambuc 	m.m_ds_req.val_in.grant = gid;
70*433d6423SLionel Sambuc 	m.m_ds_req.val_len = length;
71*433d6423SLionel Sambuc 	m.m_ds_req.flags = flags;
72*433d6423SLionel Sambuc 
73*433d6423SLionel Sambuc 	r = do_invoke_ds(&m, DS_PUBLISH, ds_name);
74*433d6423SLionel Sambuc 	cpf_revoke(gid);
75*433d6423SLionel Sambuc 
76*433d6423SLionel Sambuc 	return r;
77*433d6423SLionel Sambuc }
78*433d6423SLionel Sambuc 
ds_publish_str(const char * ds_name,char * value,int flags)79*433d6423SLionel Sambuc int ds_publish_str(const char *ds_name, char *value, int flags)
80*433d6423SLionel Sambuc {
81*433d6423SLionel Sambuc 	size_t length;
82*433d6423SLionel Sambuc 	length = strlen(value) + 1;
83*433d6423SLionel Sambuc 	value[length - 1] = '\0';
84*433d6423SLionel Sambuc 	return ds_publish_raw(ds_name, value, length, flags | DSF_TYPE_STR);
85*433d6423SLionel Sambuc }
86*433d6423SLionel Sambuc 
ds_publish_mem(const char * ds_name,void * vaddr,size_t length,int flags)87*433d6423SLionel Sambuc int ds_publish_mem(const char *ds_name, void *vaddr, size_t length, int flags)
88*433d6423SLionel Sambuc {
89*433d6423SLionel Sambuc 	return ds_publish_raw(ds_name, vaddr, length, flags | DSF_TYPE_MEM);
90*433d6423SLionel Sambuc }
91*433d6423SLionel Sambuc 
ds_retrieve_label_name(char * ds_name,endpoint_t endpoint)92*433d6423SLionel Sambuc int ds_retrieve_label_name(char *ds_name, endpoint_t endpoint)
93*433d6423SLionel Sambuc {
94*433d6423SLionel Sambuc 	message m;
95*433d6423SLionel Sambuc 	int r;
96*433d6423SLionel Sambuc 
97*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
98*433d6423SLionel Sambuc 	m.m_ds_req.val_in.ep = endpoint;
99*433d6423SLionel Sambuc 	r = do_invoke_ds(&m, DS_RETRIEVE_LABEL, ds_name);
100*433d6423SLionel Sambuc 	return r;
101*433d6423SLionel Sambuc }
102*433d6423SLionel Sambuc 
ds_retrieve_label_endpt(const char * ds_name,endpoint_t * endpoint)103*433d6423SLionel Sambuc int ds_retrieve_label_endpt(const char *ds_name, endpoint_t *endpoint)
104*433d6423SLionel Sambuc {
105*433d6423SLionel Sambuc 	message m;
106*433d6423SLionel Sambuc 	int r;
107*433d6423SLionel Sambuc 
108*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
109*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_LABEL;
110*433d6423SLionel Sambuc 	r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
111*433d6423SLionel Sambuc 	*endpoint = m.m_ds_reply.val_out.ep;
112*433d6423SLionel Sambuc 	return r;
113*433d6423SLionel Sambuc }
114*433d6423SLionel Sambuc 
ds_retrieve_u32(const char * ds_name,u32_t * value)115*433d6423SLionel Sambuc int ds_retrieve_u32(const char *ds_name, u32_t *value)
116*433d6423SLionel Sambuc {
117*433d6423SLionel Sambuc 	message m;
118*433d6423SLionel Sambuc 	int r;
119*433d6423SLionel Sambuc 
120*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
121*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_U32;
122*433d6423SLionel Sambuc 	r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
123*433d6423SLionel Sambuc 	*value = m.m_ds_reply.val_out.u32;
124*433d6423SLionel Sambuc 	return r;
125*433d6423SLionel Sambuc }
126*433d6423SLionel Sambuc 
ds_retrieve_raw(const char * ds_name,char * vaddr,size_t * length,int flags)127*433d6423SLionel Sambuc static int ds_retrieve_raw(const char *ds_name, char *vaddr, size_t *length,
128*433d6423SLionel Sambuc 	int flags)
129*433d6423SLionel Sambuc {
130*433d6423SLionel Sambuc 	message m;
131*433d6423SLionel Sambuc 	cp_grant_id_t gid;
132*433d6423SLionel Sambuc 	int r;
133*433d6423SLionel Sambuc 
134*433d6423SLionel Sambuc 	/* Grant for memory range. */
135*433d6423SLionel Sambuc 	gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, *length, CPF_WRITE);
136*433d6423SLionel Sambuc 	if(!GRANT_VALID(gid))
137*433d6423SLionel Sambuc 		return ENOMEM;
138*433d6423SLionel Sambuc 
139*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
140*433d6423SLionel Sambuc 	m.m_ds_req.val_in.grant = gid;
141*433d6423SLionel Sambuc 	m.m_ds_req.val_len = *length;
142*433d6423SLionel Sambuc 	m.m_ds_req.flags = flags;
143*433d6423SLionel Sambuc 	r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
144*433d6423SLionel Sambuc 	*length = m.m_ds_reply.val_len;
145*433d6423SLionel Sambuc 	cpf_revoke(gid);
146*433d6423SLionel Sambuc 
147*433d6423SLionel Sambuc 	return r;
148*433d6423SLionel Sambuc }
149*433d6423SLionel Sambuc 
ds_retrieve_str(const char * ds_name,char * value,size_t len_str)150*433d6423SLionel Sambuc int ds_retrieve_str(const char *ds_name, char *value, size_t len_str)
151*433d6423SLionel Sambuc {
152*433d6423SLionel Sambuc 	int r;
153*433d6423SLionel Sambuc 	size_t length = len_str + 1;
154*433d6423SLionel Sambuc 	r = ds_retrieve_raw(ds_name, value, &length, DSF_TYPE_STR);
155*433d6423SLionel Sambuc 	value[length - 1] = '\0';
156*433d6423SLionel Sambuc 	return r;
157*433d6423SLionel Sambuc }
158*433d6423SLionel Sambuc 
ds_retrieve_mem(const char * ds_name,char * vaddr,size_t * length)159*433d6423SLionel Sambuc int ds_retrieve_mem(const char *ds_name, char *vaddr, size_t *length)
160*433d6423SLionel Sambuc {
161*433d6423SLionel Sambuc 	return ds_retrieve_raw(ds_name, vaddr, length, DSF_TYPE_MEM);
162*433d6423SLionel Sambuc }
163*433d6423SLionel Sambuc 
ds_delete_u32(const char * ds_name)164*433d6423SLionel Sambuc int ds_delete_u32(const char *ds_name)
165*433d6423SLionel Sambuc {
166*433d6423SLionel Sambuc 	message m;
167*433d6423SLionel Sambuc 
168*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
169*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_U32;
170*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_DELETE, ds_name);
171*433d6423SLionel Sambuc }
172*433d6423SLionel Sambuc 
ds_delete_str(const char * ds_name)173*433d6423SLionel Sambuc int ds_delete_str(const char *ds_name)
174*433d6423SLionel Sambuc {
175*433d6423SLionel Sambuc 	message m;
176*433d6423SLionel Sambuc 
177*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
178*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_STR;
179*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_DELETE, ds_name);
180*433d6423SLionel Sambuc }
181*433d6423SLionel Sambuc 
ds_delete_mem(const char * ds_name)182*433d6423SLionel Sambuc int ds_delete_mem(const char *ds_name)
183*433d6423SLionel Sambuc {
184*433d6423SLionel Sambuc 	message m;
185*433d6423SLionel Sambuc 
186*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
187*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_MEM;
188*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_DELETE, ds_name);
189*433d6423SLionel Sambuc }
190*433d6423SLionel Sambuc 
ds_delete_label(const char * ds_name)191*433d6423SLionel Sambuc int ds_delete_label(const char *ds_name)
192*433d6423SLionel Sambuc {
193*433d6423SLionel Sambuc 	message m;
194*433d6423SLionel Sambuc 
195*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
196*433d6423SLionel Sambuc 	m.m_ds_req.flags = DSF_TYPE_LABEL;
197*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_DELETE, ds_name);
198*433d6423SLionel Sambuc }
199*433d6423SLionel Sambuc 
ds_subscribe(const char * regexp,int flags)200*433d6423SLionel Sambuc int ds_subscribe(const char *regexp, int flags)
201*433d6423SLionel Sambuc {
202*433d6423SLionel Sambuc 	message m;
203*433d6423SLionel Sambuc 
204*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
205*433d6423SLionel Sambuc 	m.m_ds_req.flags = flags;
206*433d6423SLionel Sambuc 	return do_invoke_ds(&m, DS_SUBSCRIBE, regexp);
207*433d6423SLionel Sambuc }
208*433d6423SLionel Sambuc 
ds_check(char * ds_key,int * type,endpoint_t * owner_e)209*433d6423SLionel Sambuc int ds_check(char *ds_key, int *type, endpoint_t *owner_e)
210*433d6423SLionel Sambuc {
211*433d6423SLionel Sambuc 	message m;
212*433d6423SLionel Sambuc 	int r;
213*433d6423SLionel Sambuc 
214*433d6423SLionel Sambuc 	memset(&m, 0, sizeof(m));
215*433d6423SLionel Sambuc 	r = do_invoke_ds(&m, DS_CHECK, ds_key);
216*433d6423SLionel Sambuc 	if(type) *type = m.m_ds_req.flags;
217*433d6423SLionel Sambuc 	if(owner_e) *owner_e = m.m_ds_req.owner;
218*433d6423SLionel Sambuc 	return r;
219*433d6423SLionel Sambuc }
220