First outline of a process management service server.
[barrelfish] / usr / proc_mgmt / service.c
1 /**
2  * \file
3  * \brief Process management service.
4  */
5
6 /*
7  * Copyright (c) 2017, ETH Zurich.
8  * All rights reserved.
9  *
10  * This file is distributed under the terms in the attached LICENSE file.
11  * If you do not find this file, copies can be found by writing to:
12  * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
13  */
14
15 #include <stdio.h>
16 #include <barrelfish/barrelfish.h>
17 #include <barrelfish/nameservice_client.h>
18 #include <barrelfish/proc_mgmt_client.h>
19 #include <if/proc_mgmt_defs.h>
20
21 #include "internal.h"
22
23 static struct proc_mgmt_rpc_rx_vtbl rpc_rx_vtbl;
24
25 static errval_t alloc_ep_handler(struct proc_mgmt_binding *b, errval_t *err,
26         struct capref *ep)
27 {
28     struct proc_mgmt_lmp_binding *lmpb = (struct proc_mgmt_lmp_binding*) malloc(
29             sizeof(struct proc_mgmt_lmp_binding));
30     assert(lmpb != NULL);
31
32     *err = proc_mgmt_client_lmp_accept(lmpb, get_default_waitset(),
33             DEFAULT_LMP_BUF_WORDS);
34     if (err_is_ok(*err)) {
35         *ep = lmpb->chan.local_cap;
36
37         // struct proc_mgmt_state *st = (struct proc_mgmt_state*) malloc(
38         //         sizeof(struct proc_mgmt_state));
39         // assert(st != NULL);
40         // st->queue.head = st->queue.tail = NULL;
41
42         lmpb->b.rpc_rx_vtbl = rpc_rx_vtbl;
43         // lmpb->b.st = st;
44     } else {
45         free(lmpb);
46     }
47
48     return SYS_ERR_OK;
49 }
50
51 static errval_t spawn_handler(struct proc_mgmt_binding *b,
52                           coreid_t core,
53                           const char *path,
54                           const char *argvbuf,
55                           size_t argvbytes,
56                           const char *envbuf,
57                           size_t envbytes,
58                           uint8_t flags,
59                           errval_t *err,
60                           struct capref *domainid_cap)
61 {
62     return LIB_ERR_NOT_IMPLEMENTED;
63 }
64
65 static errval_t span_handler(struct proc_mgmt_binding *b,
66                          struct capref domainid_cap,
67                          coreid_t core,
68                          struct capref vroot,
69                          struct capref disp_mem,
70                          errval_t *err)
71 {
72     return LIB_ERR_NOT_IMPLEMENTED;
73 }
74
75 static errval_t kill_handler(struct proc_mgmt_binding *b,
76                          struct capref domainid_cap,
77                          errval_t *err)
78 {
79     return LIB_ERR_NOT_IMPLEMENTED;
80 }
81
82 static struct proc_mgmt_rpc_rx_vtbl rpc_rx_vtbl = {
83     .alloc_ep_call = alloc_ep_handler,
84     .spawn_call = spawn_handler,
85     .span_call = span_handler,
86     .kill_call = kill_handler
87 };
88
89 static void export_cb(void *st, errval_t err, iref_t iref)
90 {
91     if (err_is_fail(err)) {
92         USER_PANIC_ERR(err, "export failed");
93     }
94
95     // construct name
96     char namebuf[32];
97     size_t len = snprintf(namebuf, sizeof(namebuf), "%s.%d", SERVICE_BASENAME,
98                           my_core_id);
99     assert(len < sizeof(namebuf));
100     namebuf[sizeof(namebuf) - 1] = '\0';
101
102     // register this iref with the name service
103     err = nameservice_register(namebuf, iref);
104     if (err_is_fail(err)) {
105         USER_PANIC_ERR(err, "nameservice_register failed");
106     }
107 }
108
109 static errval_t connect_cb(void *st, struct proc_mgmt_binding *b)
110 {
111     b->rpc_rx_vtbl = rpc_rx_vtbl;
112     return SYS_ERR_OK;
113 }
114
115 errval_t start_service(void)
116 {
117     return proc_mgmt_export(NULL, export_cb, connect_cb, get_default_waitset(),
118             IDC_EXPORT_FLAGS_DEFAULT);
119 }