xref: /minix3/minix/lib/libsys/sef.c (revision 6c7e614940fdd473417d84d8690a79a672edd083)
1433d6423SLionel Sambuc #include "syslib.h"
2433d6423SLionel Sambuc #include <assert.h>
3433d6423SLionel Sambuc #include <minix/sysutil.h>
4d196e2c3SCristiano Giuffrida #include <minix/rs.h>
5d196e2c3SCristiano Giuffrida #include <minix/timers.h>
6d196e2c3SCristiano Giuffrida #include <minix/endpoint.h>
7433d6423SLionel Sambuc #include <stdio.h>
8433d6423SLionel Sambuc #include <stdlib.h>
9433d6423SLionel Sambuc #include <unistd.h>
10433d6423SLionel Sambuc #include <string.h>
11433d6423SLionel Sambuc 
12433d6423SLionel Sambuc /* Self variables. */
13433d6423SLionel Sambuc #define SEF_SELF_NAME_MAXLEN 20
14433d6423SLionel Sambuc char sef_self_name[SEF_SELF_NAME_MAXLEN];
15433d6423SLionel Sambuc endpoint_t sef_self_endpoint = NONE;
16d196e2c3SCristiano Giuffrida endpoint_t sef_self_proc_nr;
17433d6423SLionel Sambuc int sef_self_priv_flags;
18d196e2c3SCristiano Giuffrida int sef_self_init_flags;
19433d6423SLionel Sambuc int sef_self_receiving;
20*6c7e6149SDavid van Moolenbroek int sef_controlled_crash;
21433d6423SLionel Sambuc 
22d196e2c3SCristiano Giuffrida /* Extern variables. */
23d196e2c3SCristiano Giuffrida EXTERN int sef_lu_state;
24d196e2c3SCristiano Giuffrida EXTERN int __sef_st_before_receive_enabled;
250e78c016SCristiano Giuffrida EXTERN __attribute__((weak)) int __vm_init_fresh;
26d196e2c3SCristiano Giuffrida 
27433d6423SLionel Sambuc /* Debug. */
28433d6423SLionel Sambuc #if SEF_INIT_DEBUG || SEF_LU_DEBUG || SEF_PING_DEBUG || SEF_SIGNAL_DEBUG
29d196e2c3SCristiano Giuffrida #define SEF_DEBUG_HEADER_MAXLEN 50
30685aa793SDavid van Moolenbroek static int sef_debug_init = 0;
31433d6423SLionel Sambuc static time_t sef_debug_boottime = 0;
32433d6423SLionel Sambuc static u32_t sef_debug_system_hz = 0;
33433d6423SLionel Sambuc static time_t sef_debug_time_sec = 0;
34433d6423SLionel Sambuc static time_t sef_debug_time_us = 0;
35433d6423SLionel Sambuc static char sef_debug_header_buff[SEF_DEBUG_HEADER_MAXLEN];
36433d6423SLionel Sambuc static void sef_debug_refresh_params(void);
37433d6423SLionel Sambuc char* sef_debug_header(void);
38433d6423SLionel Sambuc #endif
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc /* SEF Init prototypes. */
41433d6423SLionel Sambuc EXTERN int do_sef_rs_init(endpoint_t old_endpoint);
42433d6423SLionel Sambuc EXTERN int do_sef_init_request(message *m_ptr);
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc /* SEF Ping prototypes. */
45433d6423SLionel Sambuc EXTERN int do_sef_ping_request(message *m_ptr);
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc /* SEF Live update prototypes. */
48433d6423SLionel Sambuc EXTERN void do_sef_lu_before_receive(void);
49433d6423SLionel Sambuc EXTERN int do_sef_lu_request(message *m_ptr);
50433d6423SLionel Sambuc 
51433d6423SLionel Sambuc /* SEF Signal prototypes. */
52433d6423SLionel Sambuc EXTERN int do_sef_signal_request(message *m_ptr);
53433d6423SLionel Sambuc 
54d196e2c3SCristiano Giuffrida /* State transfer prototypes. */
55d196e2c3SCristiano Giuffrida EXTERN void do_sef_st_before_receive(void);
56d196e2c3SCristiano Giuffrida 
57433d6423SLionel Sambuc /* SEF GCOV prototypes. */
58433d6423SLionel Sambuc #ifdef USE_COVERAGE
59433d6423SLionel Sambuc EXTERN int do_sef_gcov_request(message *m_ptr);
60433d6423SLionel Sambuc #endif
61433d6423SLionel Sambuc 
62433d6423SLionel Sambuc /* SEF Fault Injection prototypes. */
63433d6423SLionel Sambuc EXTERN int do_sef_fi_request(message *m_ptr);
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc /*===========================================================================*
66433d6423SLionel Sambuc  *				sef_startup				     *
67433d6423SLionel Sambuc  *===========================================================================*/
sef_startup()68433d6423SLionel Sambuc void sef_startup()
69433d6423SLionel Sambuc {
70433d6423SLionel Sambuc /* SEF startup interface for system services. */
71433d6423SLionel Sambuc   int r, status;
72433d6423SLionel Sambuc   endpoint_t old_endpoint;
73433d6423SLionel Sambuc   int priv_flags;
7476bf77a2SCristiano Giuffrida   int init_flags;
75a1760b57SCristiano Giuffrida   int sys_upd_flags = 0;
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc   /* Get information about self. */
78433d6423SLionel Sambuc   r = sys_whoami(&sef_self_endpoint, sef_self_name, SEF_SELF_NAME_MAXLEN,
7976bf77a2SCristiano Giuffrida       &priv_flags, &init_flags);
80433d6423SLionel Sambuc   if ( r != OK) {
8176bf77a2SCristiano Giuffrida       panic("sef_startup: sys_whoami failed: %d\n", r);
82433d6423SLionel Sambuc   }
83d196e2c3SCristiano Giuffrida 
84d196e2c3SCristiano Giuffrida   sef_self_proc_nr = _ENDPOINT_P(sef_self_endpoint);
85433d6423SLionel Sambuc   sef_self_priv_flags = priv_flags;
86d196e2c3SCristiano Giuffrida   sef_self_init_flags = init_flags;
87d196e2c3SCristiano Giuffrida   sef_lu_state = SEF_LU_STATE_NULL;
88*6c7e6149SDavid van Moolenbroek   sef_controlled_crash = FALSE;
89433d6423SLionel Sambuc   old_endpoint = NONE;
90d196e2c3SCristiano Giuffrida   if(init_flags & SEF_LU_NOMMAP) {
91d196e2c3SCristiano Giuffrida       sys_upd_flags |= SF_VM_NOMMAP;
92d196e2c3SCristiano Giuffrida   }
93433d6423SLionel Sambuc 
94433d6423SLionel Sambuc #if USE_LIVEUPDATE
95433d6423SLionel Sambuc   /* RS may wake up with the wrong endpoint, perfom the update in that case. */
96433d6423SLionel Sambuc   if((sef_self_priv_flags & ROOT_SYS_PROC) && sef_self_endpoint != RS_PROC_NR) {
97a1760b57SCristiano Giuffrida       r = vm_update(RS_PROC_NR, sef_self_endpoint, sys_upd_flags);
98433d6423SLionel Sambuc       if(r != OK) {
99433d6423SLionel Sambuc           panic("unable to update RS from instance %d to %d: %d",
100433d6423SLionel Sambuc               RS_PROC_NR, sef_self_endpoint, r);
101433d6423SLionel Sambuc       }
102433d6423SLionel Sambuc       old_endpoint = sef_self_endpoint;
103433d6423SLionel Sambuc       sef_self_endpoint = RS_PROC_NR;
104433d6423SLionel Sambuc   }
105433d6423SLionel Sambuc #endif /* USE_LIVEUPDATE */
106433d6423SLionel Sambuc 
107433d6423SLionel Sambuc #if INTERCEPT_SEF_INIT_REQUESTS
108433d6423SLionel Sambuc   /* Intercept SEF Init requests. */
109433d6423SLionel Sambuc   if(sef_self_priv_flags & ROOT_SYS_PROC) {
110433d6423SLionel Sambuc       /* RS initialization is special. */
111433d6423SLionel Sambuc       if((r = do_sef_rs_init(old_endpoint)) != OK) {
112433d6423SLionel Sambuc           panic("RS unable to complete init: %d", r);
113433d6423SLionel Sambuc       }
114433d6423SLionel Sambuc   }
1150e78c016SCristiano Giuffrida   else if(sef_self_endpoint == VM_PROC_NR && __vm_init_fresh) {
1160e78c016SCristiano Giuffrida       /* VM handles fresh initialization by RS later */
117433d6423SLionel Sambuc   } else {
118433d6423SLionel Sambuc       message m;
119433d6423SLionel Sambuc 
120433d6423SLionel Sambuc       /* Wait for an initialization message from RS. We need this to learn the
121433d6423SLionel Sambuc        * initialization type and parameters. When restarting after a crash, we
122433d6423SLionel Sambuc        * may get some spurious IPC messages from RS (e.g. update request) that
123433d6423SLionel Sambuc        * were originally meant to be delivered to the old instance. We discard
124433d6423SLionel Sambuc        * these messages and block till a proper initialization request arrives.
125433d6423SLionel Sambuc        */
126433d6423SLionel Sambuc       do {
127433d6423SLionel Sambuc           r = ipc_receive(RS_PROC_NR, &m, &status);
128433d6423SLionel Sambuc           if(r != OK) {
129433d6423SLionel Sambuc               panic("unable to ipc_receive from RS: %d", r);
130433d6423SLionel Sambuc           }
131006d6e94SCristiano Giuffrida       } while(!IS_SEF_INIT_REQUEST(&m, status));
132433d6423SLionel Sambuc 
133433d6423SLionel Sambuc       /* Process initialization request for this system service. */
134433d6423SLionel Sambuc       if((r = do_sef_init_request(&m)) != OK) {
135433d6423SLionel Sambuc           panic("unable to process init request: %d", r);
136433d6423SLionel Sambuc       }
137433d6423SLionel Sambuc   }
138433d6423SLionel Sambuc #endif
139433d6423SLionel Sambuc 
140433d6423SLionel Sambuc   /* (Re)initialize SEF variables. */
141433d6423SLionel Sambuc   sef_self_priv_flags = priv_flags;
142d196e2c3SCristiano Giuffrida   sef_self_init_flags = init_flags;
143d196e2c3SCristiano Giuffrida   sef_lu_state = SEF_LU_STATE_NULL;
144*6c7e6149SDavid van Moolenbroek   sef_controlled_crash = FALSE;
145433d6423SLionel Sambuc }
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc /*===========================================================================*
148433d6423SLionel Sambuc  *				sef_receive_status			     *
149433d6423SLionel Sambuc  *===========================================================================*/
sef_receive_status(endpoint_t src,message * m_ptr,int * status_ptr)150433d6423SLionel Sambuc int sef_receive_status(endpoint_t src, message *m_ptr, int *status_ptr)
151433d6423SLionel Sambuc {
152433d6423SLionel Sambuc /* SEF receive() interface for system services. */
153006d6e94SCristiano Giuffrida   int r, status, m_type;
154433d6423SLionel Sambuc 
155433d6423SLionel Sambuc   sef_self_receiving = TRUE;
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc   while(TRUE) {
158433d6423SLionel Sambuc       /* If the caller indicated that it no longer wants to receive a message,
159433d6423SLionel Sambuc        * return now.
160433d6423SLionel Sambuc        */
161433d6423SLionel Sambuc       if (!sef_self_receiving)
162433d6423SLionel Sambuc           return EINTR;
163433d6423SLionel Sambuc 
164433d6423SLionel Sambuc #if INTERCEPT_SEF_LU_REQUESTS
165433d6423SLionel Sambuc       /* Handle SEF Live update before receive events. */
166d196e2c3SCristiano Giuffrida       if(sef_lu_state != SEF_LU_STATE_NULL) {
167433d6423SLionel Sambuc           do_sef_lu_before_receive();
168d196e2c3SCristiano Giuffrida       }
169d196e2c3SCristiano Giuffrida 
170d196e2c3SCristiano Giuffrida       /* Handle State transfer before receive events. */
171d196e2c3SCristiano Giuffrida       if(__sef_st_before_receive_enabled) {
172d196e2c3SCristiano Giuffrida           do_sef_st_before_receive();
173d196e2c3SCristiano Giuffrida       }
174433d6423SLionel Sambuc #endif
175433d6423SLionel Sambuc 
176433d6423SLionel Sambuc       /* Receive and return in case of error. */
177433d6423SLionel Sambuc       r = ipc_receive(src, m_ptr, &status);
178433d6423SLionel Sambuc       if(status_ptr) *status_ptr = status;
179433d6423SLionel Sambuc       if(r != OK) {
180433d6423SLionel Sambuc           return r;
181433d6423SLionel Sambuc       }
182433d6423SLionel Sambuc 
183006d6e94SCristiano Giuffrida       m_type = m_ptr->m_type;
184006d6e94SCristiano Giuffrida       if (is_ipc_notify(status)) {
185006d6e94SCristiano Giuffrida           switch (m_ptr->m_source) {
186006d6e94SCristiano Giuffrida               case SYSTEM:
187006d6e94SCristiano Giuffrida                   m_type = SEF_SIGNAL_REQUEST_TYPE;
188006d6e94SCristiano Giuffrida               break;
189006d6e94SCristiano Giuffrida               case RS_PROC_NR:
190006d6e94SCristiano Giuffrida                   m_type = SEF_PING_REQUEST_TYPE;
191006d6e94SCristiano Giuffrida               break;
192006d6e94SCristiano Giuffrida           }
193006d6e94SCristiano Giuffrida       }
194006d6e94SCristiano Giuffrida       switch(m_type) {
195006d6e94SCristiano Giuffrida 
196006d6e94SCristiano Giuffrida #if INTERCEPT_SEF_INIT_REQUESTS
197006d6e94SCristiano Giuffrida       case SEF_INIT_REQUEST_TYPE:
198006d6e94SCristiano Giuffrida           /* Intercept SEF Init requests. */
199006d6e94SCristiano Giuffrida           if(IS_SEF_INIT_REQUEST(m_ptr, status)) {
200006d6e94SCristiano Giuffrida               /* Ignore spurious init requests. */
201006d6e94SCristiano Giuffrida               if (m_ptr->m_rs_init.type != SEF_INIT_FRESH
202006d6e94SCristiano Giuffrida                   || sef_self_endpoint != VM_PROC_NR)
203006d6e94SCristiano Giuffrida               continue;
204006d6e94SCristiano Giuffrida           }
205006d6e94SCristiano Giuffrida       break;
206006d6e94SCristiano Giuffrida #endif
207006d6e94SCristiano Giuffrida 
208433d6423SLionel Sambuc #if INTERCEPT_SEF_PING_REQUESTS
209006d6e94SCristiano Giuffrida       case SEF_PING_REQUEST_TYPE:
210433d6423SLionel Sambuc           /* Intercept SEF Ping requests. */
211433d6423SLionel Sambuc           if(IS_SEF_PING_REQUEST(m_ptr, status)) {
212433d6423SLionel Sambuc               if(do_sef_ping_request(m_ptr) == OK) {
213433d6423SLionel Sambuc                   continue;
214433d6423SLionel Sambuc               }
215433d6423SLionel Sambuc           }
216006d6e94SCristiano Giuffrida       break;
217433d6423SLionel Sambuc #endif
218433d6423SLionel Sambuc 
219433d6423SLionel Sambuc #if INTERCEPT_SEF_LU_REQUESTS
220006d6e94SCristiano Giuffrida       case SEF_LU_REQUEST_TYPE:
221433d6423SLionel Sambuc           /* Intercept SEF Live update requests. */
222433d6423SLionel Sambuc           if(IS_SEF_LU_REQUEST(m_ptr, status)) {
223433d6423SLionel Sambuc               if(do_sef_lu_request(m_ptr) == OK) {
224433d6423SLionel Sambuc                   continue;
225433d6423SLionel Sambuc               }
226433d6423SLionel Sambuc           }
227006d6e94SCristiano Giuffrida       break;
228433d6423SLionel Sambuc #endif
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc #if INTERCEPT_SEF_SIGNAL_REQUESTS
231006d6e94SCristiano Giuffrida       case SEF_SIGNAL_REQUEST_TYPE:
232433d6423SLionel Sambuc           /* Intercept SEF Signal requests. */
233433d6423SLionel Sambuc           if(IS_SEF_SIGNAL_REQUEST(m_ptr, status)) {
234433d6423SLionel Sambuc               if(do_sef_signal_request(m_ptr) == OK) {
235433d6423SLionel Sambuc                   continue;
236433d6423SLionel Sambuc               }
237433d6423SLionel Sambuc           }
238006d6e94SCristiano Giuffrida       break;
239433d6423SLionel Sambuc #endif
240433d6423SLionel Sambuc 
241dd096140SCristiano Giuffrida #if INTERCEPT_SEF_GCOV_REQUESTS && USE_COVERAGE
242006d6e94SCristiano Giuffrida       case SEF_GCOV_REQUEST_TYPE:
243433d6423SLionel Sambuc           /* Intercept GCOV data requests (sent by VFS in vfs/gcov.c). */
244dd096140SCristiano Giuffrida           if(IS_SEF_GCOV_REQUEST(m_ptr, status)) {
245433d6423SLionel Sambuc               if(do_sef_gcov_request(m_ptr) == OK) {
246433d6423SLionel Sambuc                   continue;
247433d6423SLionel Sambuc               }
248433d6423SLionel Sambuc           }
249006d6e94SCristiano Giuffrida       break;
250433d6423SLionel Sambuc #endif
251433d6423SLionel Sambuc 
252006d6e94SCristiano Giuffrida #if INTERCEPT_SEF_FI_REQUESTS
253006d6e94SCristiano Giuffrida       case SEF_FI_REQUEST_TYPE:
254006d6e94SCristiano Giuffrida           /* Intercept SEF Fault Injection requests. */
255433d6423SLionel Sambuc           if(IS_SEF_FI_REQUEST(m_ptr, status)) {
256433d6423SLionel Sambuc               if(do_sef_fi_request(m_ptr) == OK) {
257433d6423SLionel Sambuc                   continue;
258433d6423SLionel Sambuc               }
259433d6423SLionel Sambuc           }
260006d6e94SCristiano Giuffrida       break;
261433d6423SLionel Sambuc #endif
262433d6423SLionel Sambuc 
263006d6e94SCristiano Giuffrida       default:
264006d6e94SCristiano Giuffrida       break;
265006d6e94SCristiano Giuffrida       }
266006d6e94SCristiano Giuffrida 
267433d6423SLionel Sambuc       /* If we get this far, this is not a valid SEF request, return and
268433d6423SLionel Sambuc        * let the caller deal with that.
269433d6423SLionel Sambuc        */
270433d6423SLionel Sambuc       break;
271433d6423SLionel Sambuc   }
272433d6423SLionel Sambuc 
273433d6423SLionel Sambuc   return r;
274433d6423SLionel Sambuc }
275433d6423SLionel Sambuc 
276433d6423SLionel Sambuc /*===========================================================================*
277433d6423SLionel Sambuc  *				sef_self				     *
278433d6423SLionel Sambuc  *===========================================================================*/
sef_self(void)279433d6423SLionel Sambuc endpoint_t sef_self(void)
280433d6423SLionel Sambuc {
281433d6423SLionel Sambuc /* Return the process's own endpoint number. */
282433d6423SLionel Sambuc 
283433d6423SLionel Sambuc   if (sef_self_endpoint == NONE)
284433d6423SLionel Sambuc 	panic("sef_self called before initialization");
285433d6423SLionel Sambuc 
286433d6423SLionel Sambuc   return sef_self_endpoint;
287433d6423SLionel Sambuc }
288433d6423SLionel Sambuc 
289433d6423SLionel Sambuc /*===========================================================================*
290433d6423SLionel Sambuc  *				sef_cancel				     *
291433d6423SLionel Sambuc  *===========================================================================*/
sef_cancel(void)292433d6423SLionel Sambuc void sef_cancel(void)
293433d6423SLionel Sambuc {
294433d6423SLionel Sambuc /* Cancel receiving a message. This function be called from a callback invoked
295433d6423SLionel Sambuc  * from within sef_receive_status(), which will then return an EINTR error
296433d6423SLionel Sambuc  * code. In particular, this function can be used to exit from the main receive
297433d6423SLionel Sambuc  * loop when a signal handler causes the process to want to shut down.
298433d6423SLionel Sambuc  */
299433d6423SLionel Sambuc 
300433d6423SLionel Sambuc   sef_self_receiving = FALSE;
301433d6423SLionel Sambuc }
302433d6423SLionel Sambuc 
303433d6423SLionel Sambuc /*===========================================================================*
304d196e2c3SCristiano Giuffrida  *                              sef_getrndseed              		     *
305d196e2c3SCristiano Giuffrida  *===========================================================================*/
sef_getrndseed(void)306d196e2c3SCristiano Giuffrida int sef_getrndseed(void)
307d196e2c3SCristiano Giuffrida {
308d91f738bSDavid van Moolenbroek     return (int)getticks();
309d196e2c3SCristiano Giuffrida }
310d196e2c3SCristiano Giuffrida 
311d196e2c3SCristiano Giuffrida /*===========================================================================*
312433d6423SLionel Sambuc  *      	                  sef_exit                                   *
313433d6423SLionel Sambuc  *===========================================================================*/
sef_exit(int status)314433d6423SLionel Sambuc void sef_exit(int status)
315433d6423SLionel Sambuc {
316433d6423SLionel Sambuc /* System services use a special version of exit() that generates a
317433d6423SLionel Sambuc  * self-termination signal.
318433d6423SLionel Sambuc  */
319433d6423SLionel Sambuc 
320433d6423SLionel Sambuc   /* Ask the kernel to exit. */
321433d6423SLionel Sambuc   sys_exit();
322433d6423SLionel Sambuc 
323433d6423SLionel Sambuc   /* If everything else fails, hang. */
324433d6423SLionel Sambuc   printf("Warning: system service %d couldn't exit\n", sef_self_endpoint);
325433d6423SLionel Sambuc   for(;;) { }
326433d6423SLionel Sambuc }
327433d6423SLionel Sambuc 
328433d6423SLionel Sambuc #ifdef __weak_alias
329433d6423SLionel Sambuc __weak_alias(_exit, sef_exit);
330433d6423SLionel Sambuc __weak_alias(__exit, sef_exit);
331433d6423SLionel Sambuc #endif
332433d6423SLionel Sambuc 
333d196e2c3SCristiano Giuffrida /*===========================================================================*
334d196e2c3SCristiano Giuffrida  *                                sef_munmap                                 *
335d196e2c3SCristiano Giuffrida  *===========================================================================*/
sef_munmap(void * addrstart,vir_bytes len,int type)336d196e2c3SCristiano Giuffrida int sef_munmap(void *addrstart, vir_bytes len, int type)
337d196e2c3SCristiano Giuffrida {
338d196e2c3SCristiano Giuffrida /* System services use a special version of munmap() to control implicit
339d196e2c3SCristiano Giuffrida  * munmaps as startup and allow for asynchronous mnmap for VM.
340d196e2c3SCristiano Giuffrida  */
341d196e2c3SCristiano Giuffrida   message m;
342d196e2c3SCristiano Giuffrida   m.m_type = type;
343d196e2c3SCristiano Giuffrida   m.VMUM_ADDR = addrstart;
344d196e2c3SCristiano Giuffrida   m.VMUM_LEN = len;
345d196e2c3SCristiano Giuffrida   if(sef_self_endpoint == VM_PROC_NR) {
346d196e2c3SCristiano Giuffrida       return asynsend3(SELF, &m, AMF_NOREPLY);
347d196e2c3SCristiano Giuffrida   }
348d196e2c3SCristiano Giuffrida   return _syscall(VM_PROC_NR, type, &m);
349d196e2c3SCristiano Giuffrida }
350d196e2c3SCristiano Giuffrida 
351433d6423SLionel Sambuc #if SEF_INIT_DEBUG || SEF_LU_DEBUG || SEF_PING_DEBUG || SEF_SIGNAL_DEBUG
352433d6423SLionel Sambuc /*===========================================================================*
353433d6423SLionel Sambuc  *                         sef_debug_refresh_params              	     *
354433d6423SLionel Sambuc  *===========================================================================*/
sef_debug_refresh_params(void)355433d6423SLionel Sambuc static void sef_debug_refresh_params(void)
356433d6423SLionel Sambuc {
357433d6423SLionel Sambuc /* Refresh SEF debug params. */
358433d6423SLionel Sambuc   clock_t uptime;
359433d6423SLionel Sambuc 
360685aa793SDavid van Moolenbroek   /* Get boottime and system hz the first time. */
361685aa793SDavid van Moolenbroek   if(!sef_debug_init) {
362685aa793SDavid van Moolenbroek       if (sys_times(NONE, NULL, NULL, NULL, &sef_debug_boottime) != OK)
363685aa793SDavid van Moolenbroek 	  sef_debug_init = -1;
364685aa793SDavid van Moolenbroek       else if (sys_getinfo(GET_HZ, &sef_debug_system_hz,
365685aa793SDavid van Moolenbroek         sizeof(sef_debug_system_hz), 0, 0) != OK)
366685aa793SDavid van Moolenbroek 	  sef_debug_init = -1;
367685aa793SDavid van Moolenbroek       else
368685aa793SDavid van Moolenbroek 	  sef_debug_init = 1;
369433d6423SLionel Sambuc   }
370433d6423SLionel Sambuc 
371433d6423SLionel Sambuc   /* Get uptime. */
372433d6423SLionel Sambuc   uptime = -1;
373685aa793SDavid van Moolenbroek   if (sef_debug_init < 1 || sys_times(NONE, NULL, NULL, &uptime, NULL) != OK) {
374433d6423SLionel Sambuc       sef_debug_time_sec = 0;
375433d6423SLionel Sambuc       sef_debug_time_us = 0;
376433d6423SLionel Sambuc   }
377433d6423SLionel Sambuc   else {
378685aa793SDavid van Moolenbroek       /* Compute current time. */
379433d6423SLionel Sambuc       sef_debug_time_sec = (time_t) (sef_debug_boottime
380433d6423SLionel Sambuc           + (uptime/sef_debug_system_hz));
381433d6423SLionel Sambuc       sef_debug_time_us = (uptime%sef_debug_system_hz)
382433d6423SLionel Sambuc           * 1000000/sef_debug_system_hz;
383433d6423SLionel Sambuc   }
384433d6423SLionel Sambuc }
385433d6423SLionel Sambuc 
386433d6423SLionel Sambuc /*===========================================================================*
387433d6423SLionel Sambuc  *                              sef_debug_header              		     *
388433d6423SLionel Sambuc  *===========================================================================*/
sef_debug_header(void)389433d6423SLionel Sambuc char* sef_debug_header(void)
390433d6423SLionel Sambuc {
391433d6423SLionel Sambuc /* Build and return a SEF debug header. */
392433d6423SLionel Sambuc   sef_debug_refresh_params();
393433d6423SLionel Sambuc   snprintf(sef_debug_header_buff, sizeof(sef_debug_header_buff),
394433d6423SLionel Sambuc       "%s: time = %ds %06dus", sef_self_name, (int) sef_debug_time_sec,
395433d6423SLionel Sambuc       (int) sef_debug_time_us);
396433d6423SLionel Sambuc 
397433d6423SLionel Sambuc   return sef_debug_header_buff;
398433d6423SLionel Sambuc }
399433d6423SLionel Sambuc #endif /*SEF_INIT_DEBUG || SEF_LU_DEBUG || SEF_PING_DEBUG || SEF_SIGNAL_DEBUG*/
400433d6423SLionel Sambuc 
401