minix/lib/libsys/ds.c
David van Moolenbroek 80bd109cd3 libsys: various updates
- move system calls for use by services from libminlib into libsys;
- move srv_fork(2) and srv_kill(2) from RS and into libsys;
- replace getprocnr(2) with sef_self(3);
- rename previous getnprocnr(2) to getprocnr(2);
- clean up getepinfo(2);
- change all libsys calls that used _syscall to use _taskcall, so as
  to avoid going through errno to pass errors; this is already how
  most calls work anyway, and many of the calls previously using
  _syscall were already assumed to return the actual error;
- initialize request messages to zero, for future compatibility
  (note that this does not include PCI calls, which are in need of a
  much bigger overhaul, nor kernel calls);
- clean up more of dead DS code as a side effect.

Change-Id: I8788f54c68598fcf58e23486e270c2d749780ebb
2014-03-01 09:05:00 +01:00

219 lines
4.3 KiB
C

#include <minix/ds.h>
#include <string.h>
#include "syslib.h"
static int do_invoke_ds(message *m, int type, const char *ds_name)
{
cp_grant_id_t g_key;
size_t len_key;
int access, r;
if(type == DS_CHECK || type == DS_RETRIEVE_LABEL) {
len_key = DS_MAX_KEYLEN;
access = CPF_WRITE;
} else {
len_key = strlen(ds_name)+1;
access = CPF_READ;
}
/* Grant for key. */
g_key = cpf_grant_direct(DS_PROC_NR, (vir_bytes) ds_name,
len_key, access);
if(!GRANT_VALID(g_key))
return ENOMEM;
m->DS_KEY_GRANT = g_key;
m->DS_KEY_LEN = len_key;
r = _taskcall(DS_PROC_NR, type, m);
cpf_revoke(g_key);
return r;
}
int ds_publish_label(const char *ds_name, endpoint_t endpoint, int flags)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_VAL = (u32_t) endpoint;
m.DS_FLAGS = DSF_TYPE_LABEL | flags;
return do_invoke_ds(&m, DS_PUBLISH, ds_name);
}
int ds_publish_u32(const char *ds_name, u32_t value, int flags)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_VAL = value;
m.DS_FLAGS = DSF_TYPE_U32 | flags;
return do_invoke_ds(&m, DS_PUBLISH, ds_name);
}
static int ds_publish_raw(const char *ds_name, void *vaddr, size_t length,
int flags)
{
cp_grant_id_t gid;
message m;
int r;
/* Grant for memory range. */
gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, length, CPF_READ);
if(!GRANT_VALID(gid))
return ENOMEM;
memset(&m, 0, sizeof(m));
m.DS_VAL = gid;
m.DS_VAL_LEN = length;
m.DS_FLAGS = flags;
r = do_invoke_ds(&m, DS_PUBLISH, ds_name);
cpf_revoke(gid);
return r;
}
int ds_publish_str(const char *ds_name, char *value, int flags)
{
size_t length;
length = strlen(value) + 1;
value[length - 1] = '\0';
return ds_publish_raw(ds_name, value, length, flags | DSF_TYPE_STR);
}
int ds_publish_mem(const char *ds_name, void *vaddr, size_t length, int flags)
{
return ds_publish_raw(ds_name, vaddr, length, flags | DSF_TYPE_MEM);
}
int ds_retrieve_label_name(char *ds_name, endpoint_t endpoint)
{
message m;
int r;
memset(&m, 0, sizeof(m));
m.DS_VAL = (u32_t) endpoint;
r = do_invoke_ds(&m, DS_RETRIEVE_LABEL, ds_name);
return r;
}
int ds_retrieve_label_endpt(const char *ds_name, endpoint_t *endpoint)
{
message m;
int r;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = DSF_TYPE_LABEL;
r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
*endpoint = (endpoint_t) m.DS_VAL;
return r;
}
int ds_retrieve_u32(const char *ds_name, u32_t *value)
{
message m;
int r;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = DSF_TYPE_U32;
r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
*value = m.DS_VAL;
return r;
}
static int ds_retrieve_raw(const char *ds_name, char *vaddr, size_t *length,
int flags)
{
message m;
cp_grant_id_t gid;
int r;
/* Grant for memory range. */
gid = cpf_grant_direct(DS_PROC_NR, (vir_bytes)vaddr, *length, CPF_WRITE);
if(!GRANT_VALID(gid))
return ENOMEM;
memset(&m, 0, sizeof(m));
m.DS_VAL = gid;
m.DS_VAL_LEN = *length;
m.DS_FLAGS = flags;
r = do_invoke_ds(&m, DS_RETRIEVE, ds_name);
*length = m.DS_VAL_LEN;
cpf_revoke(gid);
return r;
}
int ds_retrieve_str(const char *ds_name, char *value, size_t len_str)
{
int r;
size_t length = len_str + 1;
r = ds_retrieve_raw(ds_name, value, &length, DSF_TYPE_STR);
value[length - 1] = '\0';
return r;
}
int ds_retrieve_mem(const char *ds_name, char *vaddr, size_t *length)
{
return ds_retrieve_raw(ds_name, vaddr, length, DSF_TYPE_MEM);
}
int ds_delete_u32(const char *ds_name)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = DSF_TYPE_U32;
return do_invoke_ds(&m, DS_DELETE, ds_name);
}
int ds_delete_str(const char *ds_name)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = DSF_TYPE_STR;
return do_invoke_ds(&m, DS_DELETE, ds_name);
}
int ds_delete_mem(const char *ds_name)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = DSF_TYPE_MEM;
return do_invoke_ds(&m, DS_DELETE, ds_name);
}
int ds_delete_label(const char *ds_name)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = DSF_TYPE_LABEL;
return do_invoke_ds(&m, DS_DELETE, ds_name);
}
int ds_subscribe(const char *regexp, int flags)
{
message m;
memset(&m, 0, sizeof(m));
m.DS_FLAGS = flags;
return do_invoke_ds(&m, DS_SUBSCRIBE, regexp);
}
int ds_check(char *ds_key, int *type, endpoint_t *owner_e)
{
message m;
int r;
memset(&m, 0, sizeof(m));
r = do_invoke_ds(&m, DS_CHECK, ds_key);
if(type) *type = m.DS_FLAGS;
if(owner_e) *owner_e = m.DS_OWNER;
return r;
}