xref: /minix/minix/lib/libsys/sef_init.c (revision 7f5f010b)
1 #include "syslib.h"
2 #include <assert.h>
3 #include <unistd.h>
4 #include <minix/sysutil.h>
5 #include <string.h>
6 
7 /* SEF Init callbacks. */
8 static struct sef_cbs {
9     sef_cb_init_t                       sef_cb_init_fresh;
10     sef_cb_init_t                       sef_cb_init_lu;
11     sef_cb_init_t                       sef_cb_init_restart;
12     sef_cb_init_response_t              sef_cb_init_response;
13 } sef_cbs = {
14     SEF_CB_INIT_FRESH_DEFAULT,
15     SEF_CB_INIT_LU_DEFAULT,
16     SEF_CB_INIT_RESTART_DEFAULT,
17     SEF_CB_INIT_RESPONSE_DEFAULT
18 };
19 
20 /* SEF Init prototypes for sef_startup(). */
21 int do_sef_rs_init(endpoint_t old_endpoint);
22 int do_sef_init_request(message *m_ptr);
23 
24 /* Debug. */
25 EXTERN char* sef_debug_header(void);
26 
27 /* Information about SELF. */
28 EXTERN endpoint_t sef_self_endpoint;
29 EXTERN endpoint_t sef_self_priv_flags;
30 
31 /*===========================================================================*
32  *                              process_init             		     *
33  *===========================================================================*/
34 static int process_init(int type, sef_init_info_t *info)
35 {
36 /* Process initialization. */
37   int r, result;
38   message m;
39 
40   /* Debug. */
41 #if SEF_INIT_DEBUG
42   sef_init_debug_begin();
43   sef_init_dprint("%s. Got a SEF Init request of type: %d. About to init.\n",
44       sef_debug_header(), type);
45   sef_init_debug_end();
46 #endif
47 
48   /* Let the callback code handle the specific initialization type. */
49   switch(type) {
50       case SEF_INIT_FRESH:
51           result = sef_cbs.sef_cb_init_fresh(type, info);
52       break;
53       case SEF_INIT_LU:
54           result = sef_cbs.sef_cb_init_lu(type, info);
55       break;
56       case SEF_INIT_RESTART:
57           result = sef_cbs.sef_cb_init_restart(type, info);
58       break;
59 
60       default:
61           /* Not a valid SEF init type. */
62           result = EINVAL;
63       break;
64   }
65 
66   memset(&m, 0, sizeof(m));
67   m.m_source = sef_self_endpoint;
68   m.m_type = RS_INIT;
69   m.m_rs_init.result = result;
70   r = sef_cbs.sef_cb_init_response(&m);
71 
72   return r;
73 }
74 
75 /*===========================================================================*
76  *                              do_sef_rs_init             		     *
77  *===========================================================================*/
78 int do_sef_rs_init(endpoint_t old_endpoint)
79 {
80 /* Special SEF Init for RS. */
81   int r;
82   int type;
83   sef_init_info_t info;
84 
85   /* Get init parameters from SEF. */
86   type = SEF_INIT_FRESH;
87   if(sef_self_priv_flags & LU_SYS_PROC) {
88       type = SEF_INIT_LU;
89   }
90   else if(sef_self_priv_flags & RST_SYS_PROC) {
91       type = SEF_INIT_RESTART;
92   }
93   info.rproctab_gid = -1;
94   info.endpoint = sef_self_endpoint;
95   info.old_endpoint = old_endpoint;
96 
97   /* Peform initialization. */
98   r = process_init(type, &info);
99 
100   return r;
101 }
102 
103 /*===========================================================================*
104  *                            do_sef_init_request             		     *
105  *===========================================================================*/
106 int do_sef_init_request(message *m_ptr)
107 {
108 /* Handle a SEF Init request. */
109   int r;
110   int type;
111   sef_init_info_t info;
112 
113   /* Get init parameters from message. */
114   type = m_ptr->m_rs_init.type;
115   info.rproctab_gid = m_ptr->m_rs_init.rproctab_gid;
116   info.endpoint = sef_self_endpoint;
117   info.old_endpoint = m_ptr->m_rs_init.old_endpoint;
118 
119   /* Peform initialization. */
120   r = process_init(type, &info);
121 
122   return r;
123 }
124 
125 /*===========================================================================*
126  *                         sef_setcb_init_fresh                              *
127  *===========================================================================*/
128 void sef_setcb_init_fresh(sef_cb_init_t cb)
129 {
130   assert(cb != NULL);
131   sef_cbs.sef_cb_init_fresh = cb;
132 }
133 
134 /*===========================================================================*
135  *                            sef_setcb_init_lu                              *
136  *===========================================================================*/
137 void sef_setcb_init_lu(sef_cb_init_t cb)
138 {
139   assert(cb != NULL);
140   sef_cbs.sef_cb_init_lu = cb;
141 }
142 
143 /*===========================================================================*
144  *                         sef_setcb_init_restart                            *
145  *===========================================================================*/
146 void sef_setcb_init_restart(sef_cb_init_t cb)
147 {
148   assert(cb != NULL);
149   sef_cbs.sef_cb_init_restart = cb;
150 }
151 
152 /*===========================================================================*
153  *                         sef_setcb_init_response                           *
154  *===========================================================================*/
155 void sef_setcb_init_response(sef_cb_init_response_t cb)
156 {
157   assert(cb != NULL);
158   sef_cbs.sef_cb_init_response = cb;
159 }
160 
161 /*===========================================================================*
162  *      	              sef_cb_init_null                               *
163  *===========================================================================*/
164 int sef_cb_init_null(int UNUSED(type),
165    sef_init_info_t *UNUSED(info))
166 {
167   return OK;
168 }
169 
170 /*===========================================================================*
171  *                        sef_cb_init_response_null        		     *
172  *===========================================================================*/
173 int sef_cb_init_response_null(message * UNUSED(m_ptr))
174 {
175   return ENOSYS;
176 }
177 
178 /*===========================================================================*
179  *      	              sef_cb_init_fail                               *
180  *===========================================================================*/
181 int sef_cb_init_fail(int UNUSED(type), sef_init_info_t *UNUSED(info))
182 {
183   return ENOSYS;
184 }
185 
186 /*===========================================================================*
187  *      	              sef_cb_init_reset                              *
188  *===========================================================================*/
189 int sef_cb_init_reset(int UNUSED(type), sef_init_info_t *UNUSED(info))
190 {
191   /* Tell RS to reincarnate us, with no old resources, and a new endpoint. */
192   return ERESTART;
193 }
194 
195 /*===========================================================================*
196  *      	              sef_cb_init_crash                              *
197  *===========================================================================*/
198 int sef_cb_init_crash(int UNUSED(type), sef_init_info_t *UNUSED(info))
199 {
200   panic("Simulating a crash at initialization time...");
201 
202   return OK;
203 }
204 
205 /*===========================================================================*
206  *                       sef_cb_init_response_rs_reply        		     *
207  *===========================================================================*/
208 int sef_cb_init_response_rs_reply(message *m_ptr)
209 {
210   int r;
211 
212   /* Inform RS that we completed initialization with the given result. */
213   r = ipc_sendrec(RS_PROC_NR, m_ptr);
214 
215   return r;
216 }
217 
218