1433d6423SLionel Sambuc /* Data Store Server.
2433d6423SLionel Sambuc * This service implements a little publish/subscribe data store that is
3433d6423SLionel Sambuc * crucial for the system's fault tolerance. Components that require state
4433d6423SLionel Sambuc * can store it here, for later retrieval, e.g., after a crash and subsequent
5433d6423SLionel Sambuc * restart by the reincarnation server.
6433d6423SLionel Sambuc *
7433d6423SLionel Sambuc * Created:
8433d6423SLionel Sambuc * Oct 19, 2005 by Jorrit N. Herder
9433d6423SLionel Sambuc */
10433d6423SLionel Sambuc
11433d6423SLionel Sambuc #include "inc.h" /* include master header file */
12433d6423SLionel Sambuc #include <minix/endpoint.h>
13433d6423SLionel Sambuc
14433d6423SLionel Sambuc /* Allocate space for the global variables. */
15433d6423SLionel Sambuc static endpoint_t who_e; /* caller's proc number */
16433d6423SLionel Sambuc static int callnr; /* system call number */
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc /* Declare some local functions. */
19433d6423SLionel Sambuc static void get_work(message *m_ptr);
20433d6423SLionel Sambuc static void reply(endpoint_t whom, message *m_ptr);
21433d6423SLionel Sambuc
22433d6423SLionel Sambuc /* SEF functions and variables. */
23433d6423SLionel Sambuc static void sef_local_startup(void);
24433d6423SLionel Sambuc
25433d6423SLionel Sambuc /*===========================================================================*
26433d6423SLionel Sambuc * main *
27433d6423SLionel Sambuc *===========================================================================*/
main(int argc,char ** argv)28433d6423SLionel Sambuc int main(int argc, char **argv)
29433d6423SLionel Sambuc {
30433d6423SLionel Sambuc /* This is the main routine of this service. The main loop consists of
31433d6423SLionel Sambuc * three major activities: getting new work, processing the work, and
32433d6423SLionel Sambuc * sending the reply. The loop never terminates, unless a panic occurs.
33433d6423SLionel Sambuc */
34433d6423SLionel Sambuc message m;
35433d6423SLionel Sambuc int result;
36433d6423SLionel Sambuc
37433d6423SLionel Sambuc /* SEF local startup. */
38433d6423SLionel Sambuc env_setargs(argc, argv);
39433d6423SLionel Sambuc sef_local_startup();
40433d6423SLionel Sambuc
41433d6423SLionel Sambuc /* Main loop - get work and do it, forever. */
42433d6423SLionel Sambuc while (TRUE) {
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc /* Wait for incoming message, sets 'callnr' and 'who'. */
45433d6423SLionel Sambuc get_work(&m);
46433d6423SLionel Sambuc
47433d6423SLionel Sambuc if (is_notify(callnr)) {
48433d6423SLionel Sambuc printf("DS: warning, got illegal notify from: %d\n", m.m_source);
49433d6423SLionel Sambuc result = EINVAL;
50433d6423SLionel Sambuc goto send_reply;
51433d6423SLionel Sambuc }
52433d6423SLionel Sambuc
53433d6423SLionel Sambuc switch (callnr) {
54433d6423SLionel Sambuc case DS_PUBLISH:
55433d6423SLionel Sambuc result = do_publish(&m);
56433d6423SLionel Sambuc break;
57433d6423SLionel Sambuc case DS_RETRIEVE:
58433d6423SLionel Sambuc result = do_retrieve(&m);
59433d6423SLionel Sambuc break;
60433d6423SLionel Sambuc case DS_RETRIEVE_LABEL:
61433d6423SLionel Sambuc result = do_retrieve_label(&m);
62433d6423SLionel Sambuc break;
63433d6423SLionel Sambuc case DS_DELETE:
64433d6423SLionel Sambuc result = do_delete(&m);
65433d6423SLionel Sambuc break;
66433d6423SLionel Sambuc case DS_SUBSCRIBE:
67433d6423SLionel Sambuc result = do_subscribe(&m);
68433d6423SLionel Sambuc break;
69433d6423SLionel Sambuc case DS_CHECK:
70433d6423SLionel Sambuc result = do_check(&m);
71433d6423SLionel Sambuc break;
72433d6423SLionel Sambuc case DS_GETSYSINFO:
73433d6423SLionel Sambuc result = do_getsysinfo(&m);
74433d6423SLionel Sambuc break;
75433d6423SLionel Sambuc default:
76433d6423SLionel Sambuc printf("DS: warning, got illegal request from %d\n", m.m_source);
77433d6423SLionel Sambuc result = EINVAL;
78433d6423SLionel Sambuc }
79433d6423SLionel Sambuc
80433d6423SLionel Sambuc send_reply:
81433d6423SLionel Sambuc /* Finally send reply message, unless disabled. */
82433d6423SLionel Sambuc if (result != EDONTREPLY) {
83433d6423SLionel Sambuc m.m_type = result; /* build reply message */
84433d6423SLionel Sambuc reply(who_e, &m); /* send it away */
85433d6423SLionel Sambuc }
86433d6423SLionel Sambuc }
87433d6423SLionel Sambuc return(OK); /* shouldn't come here */
88433d6423SLionel Sambuc }
89433d6423SLionel Sambuc
90433d6423SLionel Sambuc /*===========================================================================*
91433d6423SLionel Sambuc * sef_local_startup *
92433d6423SLionel Sambuc *===========================================================================*/
sef_local_startup()93433d6423SLionel Sambuc static void sef_local_startup()
94433d6423SLionel Sambuc {
95433d6423SLionel Sambuc /* Register init callbacks. */
96433d6423SLionel Sambuc sef_setcb_init_fresh(sef_cb_init_fresh);
97*3f82ac6aSCristiano Giuffrida sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL);
98433d6423SLionel Sambuc
992b641b28SCristiano Giuffrida /* Register state transfer callbacks. */
1002b641b28SCristiano Giuffrida sef_llvm_ds_st_init();
1012b641b28SCristiano Giuffrida
102433d6423SLionel Sambuc /* Let SEF perform startup. */
103433d6423SLionel Sambuc sef_startup();
104433d6423SLionel Sambuc }
105433d6423SLionel Sambuc
106433d6423SLionel Sambuc /*===========================================================================*
107433d6423SLionel Sambuc * get_work *
108433d6423SLionel Sambuc *===========================================================================*/
get_work(message * m_ptr)109433d6423SLionel Sambuc static void get_work(
110433d6423SLionel Sambuc message *m_ptr /* message buffer */
111433d6423SLionel Sambuc )
112433d6423SLionel Sambuc {
113433d6423SLionel Sambuc int status = sef_receive(ANY, m_ptr); /* blocks until message arrives */
114433d6423SLionel Sambuc if (OK != status)
115433d6423SLionel Sambuc panic("failed to receive message!: %d", status);
116433d6423SLionel Sambuc who_e = m_ptr->m_source; /* message arrived! set sender */
117433d6423SLionel Sambuc callnr = m_ptr->m_type; /* set function call number */
118433d6423SLionel Sambuc }
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc /*===========================================================================*
121433d6423SLionel Sambuc * reply *
122433d6423SLionel Sambuc *===========================================================================*/
reply(endpoint_t who_e,message * m_ptr)123433d6423SLionel Sambuc static void reply(
124433d6423SLionel Sambuc endpoint_t who_e, /* destination */
125433d6423SLionel Sambuc message *m_ptr /* message buffer */
126433d6423SLionel Sambuc )
127433d6423SLionel Sambuc {
128433d6423SLionel Sambuc int s = ipc_send(who_e, m_ptr); /* send the message */
129433d6423SLionel Sambuc if (OK != s)
130433d6423SLionel Sambuc printf("DS: unable to send reply to %d: %d\n", who_e, s);
131433d6423SLionel Sambuc }
132433d6423SLionel Sambuc
133