re-establish kernel assert()s.
[minix.git] / lib / libsys / sef_liveupdate.c
blob8da36e59024d83bef092ffcd9bde2f9686c817f0
1 #include "syslib.h"
2 #include <assert.h>
3 #include <minix/sysutil.h>
5 /* SEF Live update variables. */
6 PRIVATE int sef_lu_state = SEF_LU_STATE_NULL;
8 /* SEF Live update callbacks. */
9 PRIVATE struct sef_cbs {
10 sef_cb_lu_prepare_t sef_cb_lu_prepare;
11 sef_cb_lu_state_isvalid_t sef_cb_lu_state_isvalid;
12 sef_cb_lu_state_changed_t sef_cb_lu_state_changed;
13 sef_cb_lu_state_dump_t sef_cb_lu_state_dump;
14 sef_cb_lu_ready_pre_t sef_cb_lu_ready_pre;
15 } sef_cbs = {
16 SEF_CB_LU_PREPARE_DEFAULT,
17 SEF_CB_LU_STATE_ISVALID_DEFAULT,
18 SEF_CB_LU_STATE_CHANGED_DEFAULT,
19 SEF_CB_LU_STATE_DUMP_DEFAULT,
20 SEF_CB_LU_READY_PRE_DEFAULT,
23 /* SEF Live update prototypes for sef_receive(). */
24 PUBLIC _PROTOTYPE( void do_sef_lu_before_receive, (void) );
25 PUBLIC _PROTOTYPE( int do_sef_lu_request, (message *m_ptr) );
27 /* Debug. */
28 EXTERN _PROTOTYPE( char* sef_debug_header, (void) );
29 PRIVATE int sef_lu_debug_cycle = 0;
31 /*===========================================================================*
32 * do_sef_lu_before_receive *
33 *===========================================================================*/
34 PUBLIC void do_sef_lu_before_receive()
36 /* Handle SEF Live update before receive events. */
38 /* Nothing to do if we are not preparing for a live update. */
39 if(sef_lu_state == SEF_LU_STATE_NULL) {
40 return;
43 /* Debug. */
44 #if SEF_LU_DEBUG
45 sef_lu_debug_cycle++;
46 sef_lu_debug_begin();
47 sef_lu_dprint("%s, cycle=%d. Dumping state variables:\n",
48 sef_debug_header(), sef_lu_debug_cycle);
49 sef_cbs.sef_cb_lu_state_dump(sef_lu_state);
50 sef_lu_debug_end();
51 #endif
53 /* Let the callback code handle the event.
54 * For SEF_LU_STATE_WORK_FREE, we're always ready, tell immediately.
56 if(sef_lu_state == SEF_LU_STATE_WORK_FREE) {
57 sef_lu_ready(OK);
59 else {
60 sef_cbs.sef_cb_lu_prepare(sef_lu_state);
64 /*===========================================================================*
65 * do_sef_lu_request *
66 *===========================================================================*/
67 PUBLIC int do_sef_lu_request(message *m_ptr)
69 /* Handle a SEF Live update request. */
70 int old_state, is_valid_state;
72 sef_lu_debug_cycle = 0;
73 old_state = sef_lu_state;
75 /* Only accept live update requests with a valid state. */
76 is_valid_state = sef_cbs.sef_cb_lu_state_isvalid(m_ptr->RS_LU_STATE);
77 if(!is_valid_state) {
78 sef_lu_ready(EINVAL);
80 else {
81 /* Set the new live update state. */
82 sef_lu_state = m_ptr->RS_LU_STATE;
84 /* If the live update state changed, let the callback code
85 * handle the rest.
87 if(old_state != sef_lu_state) {
88 sef_cbs.sef_cb_lu_state_changed(old_state, sef_lu_state);
92 /* Return OK not to let anybody else intercept the request. */
93 return(OK);
96 /*===========================================================================*
97 * sef_lu_ready *
98 *===========================================================================*/
99 PUBLIC void sef_lu_ready(int result)
101 message m;
102 int old_state, r;
104 #if SEF_LU_DEBUG
105 sef_lu_debug_begin();
106 sef_lu_dprint("%s, cycle=%d. Ready to update with result: %d%s\n",
107 sef_debug_header(), sef_lu_debug_cycle,
108 result, (result == OK ? "(OK)" : ""));
109 sef_lu_debug_end();
110 #endif
112 /* Let the callback code perform any pre-ready operations. */
113 r = sef_cbs.sef_cb_lu_ready_pre(result);
114 if(r != OK) {
115 /* Abort update if callback returned error. */
116 result = r;
118 else {
119 /* Inform RS that we're ready with the given result. */
120 m.m_type = RS_LU_PREPARE;
121 m.RS_LU_STATE = sef_lu_state;
122 m.RS_LU_RESULT = result;
123 r = sendrec(RS_PROC_NR, &m);
124 if ( r != OK) {
125 panic("sendrec failed: %d", r);
129 #if SEF_LU_DEBUG
130 sef_lu_debug_begin();
131 sef_lu_dprint("%s, cycle=%d. The %s aborted the update!\n",
132 sef_debug_header(), sef_lu_debug_cycle,
133 (result == OK ? "server" : "client"));
134 sef_lu_debug_end();
135 #endif
137 /* Something went wrong. Update was aborted and we didn't get updated.
138 * Restore things back to normal and continue executing.
140 old_state = sef_lu_state;
141 sef_lu_state = SEF_LU_STATE_NULL;
142 if(old_state != sef_lu_state) {
143 sef_cbs.sef_cb_lu_state_changed(old_state, sef_lu_state);
147 /*===========================================================================*
148 * sef_setcb_lu_prepare *
149 *===========================================================================*/
150 PUBLIC void sef_setcb_lu_prepare(sef_cb_lu_prepare_t cb)
152 assert(cb != NULL);
153 sef_cbs.sef_cb_lu_prepare = cb;
156 /*===========================================================================*
157 * sef_setcb_lu_state_isvalid *
158 *===========================================================================*/
159 PUBLIC void sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_t cb)
161 assert(cb != NULL);
162 sef_cbs.sef_cb_lu_state_isvalid = cb;
165 /*===========================================================================*
166 * sef_setcb_lu_state_changed *
167 *===========================================================================*/
168 PUBLIC void sef_setcb_lu_state_changed(sef_cb_lu_state_changed_t cb)
170 assert(cb != NULL);
171 sef_cbs.sef_cb_lu_state_changed = cb;
174 /*===========================================================================*
175 * sef_setcb_lu_state_dump *
176 *===========================================================================*/
177 PUBLIC void sef_setcb_lu_state_dump(sef_cb_lu_state_dump_t cb)
179 assert(cb != NULL);
180 sef_cbs.sef_cb_lu_state_dump = cb;
183 /*===========================================================================*
184 * sef_setcb_lu_ready_pre *
185 *===========================================================================*/
186 PUBLIC void sef_setcb_lu_ready_pre(sef_cb_lu_ready_pre_t cb)
188 assert(cb != NULL);
189 sef_cbs.sef_cb_lu_ready_pre = cb;
192 /*===========================================================================*
193 * sef_cb_lu_prepare_null *
194 *===========================================================================*/
195 PUBLIC void sef_cb_lu_prepare_null(int state)
199 /*===========================================================================*
200 * sef_cb_lu_state_isvalid_null *
201 *===========================================================================*/
202 PUBLIC int sef_cb_lu_state_isvalid_null(int state)
204 return FALSE;
207 /*===========================================================================*
208 * sef_cb_lu_state_changed_null *
209 *===========================================================================*/
210 PUBLIC void sef_cb_lu_state_changed_null(int old_state, int state)
214 /*===========================================================================*
215 * sef_cb_lu_state_dump_null *
216 *===========================================================================*/
217 PUBLIC void sef_cb_lu_state_dump_null(int state)
219 sef_lu_dprint("NULL\n");
222 /*===========================================================================*
223 * sef_cb_lu_ready_pre_null *
224 *===========================================================================*/
225 PUBLIC int sef_cb_lu_ready_pre_null(int result)
227 return(OK);
230 /*===========================================================================*
231 * sef_cb_lu_prepare_always_ready *
232 *===========================================================================*/
233 PUBLIC void sef_cb_lu_prepare_always_ready(int state)
235 sef_lu_ready(OK);
238 /*===========================================================================*
239 * sef_cb_lu_state_isvalid_standard *
240 *===========================================================================*/
241 PUBLIC int sef_cb_lu_state_isvalid_standard(int state)
243 return SEF_LU_STATE_IS_STANDARD(state);