1 /* Reincarnation Server. This servers starts new system services and detects 2 * they are exiting. In case of errors, system services can be restarted. 3 * The RS server periodically checks the status of all registered services 4 * services to see whether they are still alive. The system services are 5 * expected to periodically send a heartbeat message. 6 * 7 * Changes: 8 * Nov 22, 2009: rewrite of boot process (Cristiano Giuffrida) 9 * Jul 22, 2005: Created (Jorrit N. Herder) 10 */ 11 #include "inc.h" 12 #include <fcntl.h> 13 #include "kernel/const.h" 14 #include "kernel/type.h" 15 #include "kernel/proc.h" 16 17 /* Declare some local functions. */ 18 static void boot_image_info_lookup( endpoint_t endpoint, struct 19 boot_image *image, struct boot_image **ip, struct boot_image_priv **pp, 20 struct boot_image_sys **sp, struct boot_image_dev **dp); 21 static void catch_boot_init_ready(endpoint_t endpoint); 22 static void get_work(message *m_ptr, int *status_ptr); 23 24 /* SEF functions and variables. */ 25 static void sef_local_startup(void); 26 static int sef_cb_init_fresh(int type, sef_init_info_t *info); 27 static int sef_cb_init_restart(int type, sef_init_info_t *info); 28 static int sef_cb_init_lu(int type, sef_init_info_t *info); 29 static int sef_cb_init_response(message *m_ptr); 30 static int sef_cb_lu_response(message *m_ptr); 31 static void sef_cb_signal_handler(int signo); 32 static int sef_cb_signal_manager(endpoint_t target, int signo); 33 34 35 /*===========================================================================* 36 * main * 37 *===========================================================================*/ 38 int main(void) 39 { 40 /* This is the main routine of this service. The main loop consists of 41 * three major activities: getting new work, processing the work, and 42 * sending the reply. The loop never terminates, unless a panic occurs. 43 */ 44 message m; /* request message */ 45 int ipc_status; /* status code */ 46 int call_nr, who_e,who_p; /* call number and caller */ 47 int result; /* result to return */ 48 int s; 49 50 /* SEF local startup. */ 51 sef_local_startup(); 52 53 if (OK != (s=sys_getmachine(&machine))) 54 panic("couldn't get machine info: %d", s); 55 56 /* Main loop - get work and do it, forever. */ 57 while (TRUE) { 58 /* Perform sensitive background operations when RS is idle. */ 59 rs_idle_period(); 60 61 /* Wait for request message. */ 62 get_work(&m, &ipc_status); 63 who_e = m.m_source; 64 if(rs_isokendpt(who_e, &who_p) != OK) { 65 panic("message from bogus source: %d", who_e); 66 } 67 68 call_nr = m.m_type; 69 70 /* Now determine what to do. Four types of requests are expected: 71 * - Heartbeat messages (notifications from registered system services) 72 * - System notifications (synchronous alarm) 73 * - User requests (control messages to manage system services) 74 * - Ready messages (reply messages from registered services) 75 */ 76 77 /* Notification messages are control messages and do not need a reply. 78 * These include heartbeat messages and system notifications. 79 */ 80 if (is_ipc_notify(ipc_status)) { 81 switch (who_p) { 82 case CLOCK: 83 do_period(&m); /* check services status */ 84 continue; 85 default: /* heartbeat notification */ 86 if (rproc_ptr[who_p] != NULL) { /* mark heartbeat time */ 87 rproc_ptr[who_p]->r_alive_tm = m.m_notify.timestamp; 88 } else { 89 printf("RS: warning: got unexpected notify message from %d\n", 90 m.m_source); 91 } 92 } 93 } 94 95 /* If we get this far, this is a normal request. 96 * Handle the request and send a reply to the caller. 97 */ 98 else { 99 /* Handler functions are responsible for permission checking. */ 100 switch(call_nr) { 101 /* User requests. */ 102 case RS_UP: result = do_up(&m); break; 103 case RS_DOWN: result = do_down(&m); break; 104 case RS_REFRESH: result = do_refresh(&m); break; 105 case RS_RESTART: result = do_restart(&m); break; 106 case RS_SHUTDOWN: result = do_shutdown(&m); break; 107 case RS_UPDATE: result = do_update(&m); break; 108 case RS_CLONE: result = do_clone(&m); break; 109 case RS_UNCLONE: result = do_unclone(&m); break; 110 case RS_EDIT: result = do_edit(&m); break; 111 case RS_SYSCTL: result = do_sysctl(&m); break; 112 case RS_FI: result = do_fi(&m); break; 113 case RS_GETSYSINFO: result = do_getsysinfo(&m); break; 114 case RS_LOOKUP: result = do_lookup(&m); break; 115 /* Ready messages. */ 116 case RS_INIT: result = do_init_ready(&m); break; 117 case RS_LU_PREPARE: result = do_upd_ready(&m); break; 118 default: 119 printf("RS: warning: got unexpected request %d from %d\n", 120 m.m_type, m.m_source); 121 result = ENOSYS; 122 } 123 124 /* Finally send reply message, unless disabled. */ 125 if (result != EDONTREPLY) { 126 m.m_type = result; 127 reply(who_e, NULL, &m); 128 } 129 } 130 } 131 } 132 133 /*===========================================================================* 134 * sef_local_startup * 135 *===========================================================================*/ 136 static void sef_local_startup() 137 { 138 /* Register init callbacks. */ 139 sef_setcb_init_fresh(sef_cb_init_fresh); 140 sef_setcb_init_restart(sef_cb_init_restart); 141 sef_setcb_init_lu(sef_cb_init_lu); 142 143 /* Register response callbacks. */ 144 sef_setcb_init_response(sef_cb_init_response); 145 sef_setcb_lu_response(sef_cb_lu_response); 146 147 /* Register signal callbacks. */ 148 sef_setcb_signal_handler(sef_cb_signal_handler); 149 sef_setcb_signal_manager(sef_cb_signal_manager); 150 151 /* Let SEF perform startup. */ 152 sef_startup(); 153 } 154 155 /*===========================================================================* 156 * sef_cb_init_fresh * 157 *===========================================================================*/ 158 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info)) 159 { 160 /* Initialize the reincarnation server. */ 161 struct boot_image *ip; 162 int s,i; 163 int nr_image_srvs, nr_image_priv_srvs, nr_uncaught_init_srvs; 164 struct rproc *rp; 165 struct rproc *replica_rp; 166 struct rprocpub *rpub; 167 struct boot_image image[NR_BOOT_PROCS]; 168 struct boot_image_priv *boot_image_priv; 169 struct boot_image_sys *boot_image_sys; 170 struct boot_image_dev *boot_image_dev; 171 int pid, replica_pid; 172 endpoint_t replica_endpoint; 173 int ipc_to; 174 int *calls; 175 int all_c[] = { ALL_C, NULL_C }; 176 int no_c[] = { NULL_C }; 177 178 /* See if we run in verbose mode. */ 179 env_parse("rs_verbose", "d", 0, &rs_verbose, 0, 1); 180 181 if ((s = sys_getinfo(GET_HZ, &system_hz, sizeof(system_hz), 0, 0)) != OK) 182 panic("Cannot get system timer frequency\n"); 183 184 /* Initialize the global init descriptor. */ 185 rinit.rproctab_gid = cpf_grant_direct(ANY, (vir_bytes) rprocpub, 186 sizeof(rprocpub), CPF_READ); 187 if(!GRANT_VALID(rinit.rproctab_gid)) { 188 panic("unable to create rprocpub table grant: %d", rinit.rproctab_gid); 189 } 190 191 /* Initialize some global variables. */ 192 RUPDATE_INIT(); 193 shutting_down = FALSE; 194 195 /* Get a copy of the boot image table. */ 196 if ((s = sys_getimage(image)) != OK) { 197 panic("unable to get copy of boot image table: %d", s); 198 } 199 200 /* Determine the number of system services in the boot image table. */ 201 nr_image_srvs = 0; 202 for(i=0;i<NR_BOOT_PROCS;i++) { 203 ip = &image[i]; 204 205 /* System services only. */ 206 if(iskerneln(_ENDPOINT_P(ip->endpoint))) { 207 continue; 208 } 209 nr_image_srvs++; 210 } 211 212 /* Determine the number of entries in the boot image priv table and make sure 213 * it matches the number of system services in the boot image table. 214 */ 215 nr_image_priv_srvs = 0; 216 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 217 boot_image_priv = &boot_image_priv_table[i]; 218 219 /* System services only. */ 220 if(iskerneln(_ENDPOINT_P(boot_image_priv->endpoint))) { 221 continue; 222 } 223 nr_image_priv_srvs++; 224 } 225 if(nr_image_srvs != nr_image_priv_srvs) { 226 panic("boot image table and boot image priv table mismatch"); 227 } 228 229 /* Reset the system process table. */ 230 for (rp=BEG_RPROC_ADDR; rp<END_RPROC_ADDR; rp++) { 231 rp->r_flags = 0; 232 rp->r_init_err = ERESTART; 233 rp->r_pub = &rprocpub[rp - rproc]; 234 rp->r_pub->in_use = FALSE; 235 rp->r_pub->old_endpoint = NONE; 236 rp->r_pub->new_endpoint = NONE; 237 } 238 239 /* Initialize the system process table in 4 steps, each of them following 240 * the appearance of system services in the boot image priv table. 241 * - Step 1: set priviliges, sys properties, and dev properties (if any) 242 * for every system service. 243 */ 244 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 245 boot_image_priv = &boot_image_priv_table[i]; 246 247 /* System services only. */ 248 if(iskerneln(_ENDPOINT_P(boot_image_priv->endpoint))) { 249 continue; 250 } 251 252 /* Lookup the corresponding entries in other tables. */ 253 boot_image_info_lookup(boot_image_priv->endpoint, image, 254 &ip, NULL, &boot_image_sys, &boot_image_dev); 255 rp = &rproc[boot_image_priv - boot_image_priv_table]; 256 rpub = rp->r_pub; 257 258 /* 259 * Set privileges. 260 */ 261 /* Get label. */ 262 strcpy(rpub->label, boot_image_priv->label); 263 264 /* Force a static priv id for system services in the boot image. */ 265 rp->r_priv.s_id = static_priv_id( 266 _ENDPOINT_P(boot_image_priv->endpoint)); 267 268 /* Initialize privilege bitmaps and signal manager. */ 269 rp->r_priv.s_flags = boot_image_priv->flags; /* priv flags */ 270 rp->r_priv.s_init_flags = SRV_OR_USR(rp, SRV_I, USR_I); /* init flags */ 271 rp->r_priv.s_trap_mask= SRV_OR_USR(rp, SRV_T, USR_T); /* traps */ 272 ipc_to = SRV_OR_USR(rp, SRV_M, USR_M); /* targets */ 273 fill_send_mask(&rp->r_priv.s_ipc_to, ipc_to == ALL_M); 274 rp->r_priv.s_sig_mgr= SRV_OR_USR(rp, SRV_SM, USR_SM); /* sig mgr */ 275 rp->r_priv.s_bak_sig_mgr = NONE; /* backup sig mgr */ 276 277 /* Initialize kernel call mask bitmap. */ 278 calls = SRV_OR_USR(rp, SRV_KC, USR_KC) == ALL_C ? all_c : no_c; 279 fill_call_mask(calls, NR_SYS_CALLS, 280 rp->r_priv.s_k_call_mask, KERNEL_CALL, TRUE); 281 282 /* Set the privilege structure. RS and VM are exceptions and are already 283 * running. 284 */ 285 if(boot_image_priv->endpoint != RS_PROC_NR && 286 boot_image_priv->endpoint != VM_PROC_NR) { 287 if ((s = sys_privctl(ip->endpoint, SYS_PRIV_SET_SYS, &(rp->r_priv))) 288 != OK) { 289 panic("unable to set privilege structure: %d", s); 290 } 291 } 292 293 /* Synch the privilege structure with the kernel. */ 294 if ((s = sys_getpriv(&(rp->r_priv), ip->endpoint)) != OK) { 295 panic("unable to synch privilege structure: %d", s); 296 } 297 298 /* 299 * Set sys properties. 300 */ 301 rpub->sys_flags = boot_image_sys->flags; /* sys flags */ 302 303 /* 304 * Set dev properties. 305 */ 306 rpub->dev_nr = boot_image_dev->dev_nr; /* major device number */ 307 308 /* Build command settings. This will also set the process name. */ 309 strlcpy(rp->r_cmd, ip->proc_name, sizeof(rp->r_cmd)); 310 rp->r_script[0]= '\0'; 311 build_cmd_dep(rp); 312 313 /* Initialize vm call mask bitmap. */ 314 calls = SRV_OR_USR(rp, SRV_VC, USR_VC) == ALL_C ? all_c : no_c; 315 fill_call_mask(calls, NR_VM_CALLS, rpub->vm_call_mask, VM_RQ_BASE, TRUE); 316 317 /* Scheduling parameters. */ 318 rp->r_scheduler = SRV_OR_USR(rp, SRV_SCH, USR_SCH); 319 rp->r_priority = SRV_OR_USR(rp, SRV_Q, USR_Q); 320 rp->r_quantum = SRV_OR_USR(rp, SRV_QT, USR_QT); 321 322 /* Get some settings from the boot image table. */ 323 rpub->endpoint = ip->endpoint; 324 325 /* Set some defaults. */ 326 rp->r_old_rp = NULL; /* no old version yet */ 327 rp->r_new_rp = NULL; /* no new version yet */ 328 rp->r_prev_rp = NULL; /* no prev replica yet */ 329 rp->r_next_rp = NULL; /* no next replica yet */ 330 rp->r_uid = 0; /* root */ 331 rp->r_check_tm = 0; /* not checked yet */ 332 getticks(&rp->r_alive_tm); /* currently alive */ 333 rp->r_stop_tm = 0; /* not exiting yet */ 334 rp->r_restarts = 0; /* no restarts so far */ 335 rp->r_period = 0; /* no period yet */ 336 rp->r_exec = NULL; /* no in-memory copy yet */ 337 rp->r_exec_len = 0; 338 339 /* Mark as in use and active. */ 340 rp->r_flags = RS_IN_USE | RS_ACTIVE; 341 rproc_ptr[_ENDPOINT_P(rpub->endpoint)]= rp; 342 rpub->in_use = TRUE; 343 } 344 345 /* - Step 2: allow every system service in the boot image to run. */ 346 nr_uncaught_init_srvs = 0; 347 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 348 boot_image_priv = &boot_image_priv_table[i]; 349 350 /* System services only. */ 351 if(iskerneln(_ENDPOINT_P(boot_image_priv->endpoint))) { 352 continue; 353 } 354 355 /* Lookup the corresponding slot in the system process table. */ 356 rp = &rproc[boot_image_priv - boot_image_priv_table]; 357 rpub = rp->r_pub; 358 359 /* RS/VM are already running as we speak. */ 360 if(boot_image_priv->endpoint == RS_PROC_NR || 361 boot_image_priv->endpoint == VM_PROC_NR) { 362 if ((s = init_service(rp, SEF_INIT_FRESH, rp->r_priv.s_init_flags)) != OK) { 363 panic("unable to initialize %d: %d", boot_image_priv->endpoint, s); 364 } 365 /* VM will still send an RS_INIT message, though. */ 366 if (boot_image_priv->endpoint != RS_PROC_NR) { 367 nr_uncaught_init_srvs++; 368 } 369 continue; 370 } 371 372 /* Allow the service to run. */ 373 if ((s = sched_init_proc(rp)) != OK) { 374 panic("unable to initialize scheduling: %d", s); 375 } 376 if ((s = sys_privctl(rpub->endpoint, SYS_PRIV_ALLOW, NULL)) != OK) { 377 panic("unable to initialize privileges: %d", s); 378 } 379 380 /* Initialize service. We assume every service will always get 381 * back to us here at boot time. 382 */ 383 if(boot_image_priv->flags & SYS_PROC) { 384 if ((s = init_service(rp, SEF_INIT_FRESH, rp->r_priv.s_init_flags)) != OK) { 385 panic("unable to initialize service: %d", s); 386 } 387 if(rpub->sys_flags & SF_SYNCH_BOOT) { 388 /* Catch init ready message now to synchronize. */ 389 catch_boot_init_ready(rpub->endpoint); 390 } 391 else { 392 /* Catch init ready message later. */ 393 nr_uncaught_init_srvs++; 394 } 395 } 396 } 397 398 /* - Step 3: let every system service complete initialization by 399 * catching all the init ready messages left. 400 */ 401 while(nr_uncaught_init_srvs) { 402 catch_boot_init_ready(ANY); 403 nr_uncaught_init_srvs--; 404 } 405 406 /* - Step 4: all the system services in the boot image are now running. 407 * Complete the initialization of the system process table in collaboration 408 * with other system services. 409 */ 410 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 411 boot_image_priv = &boot_image_priv_table[i]; 412 413 /* System services only. */ 414 if(iskerneln(_ENDPOINT_P(boot_image_priv->endpoint))) { 415 continue; 416 } 417 418 /* Lookup the corresponding slot in the system process table. */ 419 rp = &rproc[boot_image_priv - boot_image_priv_table]; 420 rpub = rp->r_pub; 421 422 /* Get pid from PM. */ 423 rp->r_pid = getnpid(rpub->endpoint); 424 if(rp->r_pid < 0) { 425 panic("unable to get pid: %d", rp->r_pid); 426 } 427 } 428 429 /* Set alarm to periodically check service status. */ 430 if (OK != (s=sys_setalarm(RS_DELTA_T, 0))) 431 panic("couldn't set alarm: %d", s); 432 433 #if USE_LIVEUPDATE 434 /* Now create a new RS instance and let the current 435 * instance live update into the replica. Clone RS' own slot first. 436 */ 437 rp = rproc_ptr[_ENDPOINT_P(RS_PROC_NR)]; 438 if((s = clone_slot(rp, &replica_rp)) != OK) { 439 panic("unable to clone current RS instance: %d", s); 440 } 441 442 /* Fork a new RS instance with root:operator. */ 443 pid = srv_fork(0, 0); 444 if(pid < 0) { 445 panic("unable to fork a new RS instance: %d", pid); 446 } 447 replica_pid = pid ? pid : getpid(); 448 if ((s = getprocnr(replica_pid, &replica_endpoint)) != 0) 449 panic("unable to get replica endpoint: %d", s); 450 replica_rp->r_pid = replica_pid; 451 replica_rp->r_pub->endpoint = replica_endpoint; 452 453 if(pid == 0) { 454 /* New RS instance running. */ 455 456 /* Live update the old instance into the new one. */ 457 s = update_service(&rp, &replica_rp, RS_SWAP, 0); 458 if(s != OK) { 459 panic("unable to live update RS: %d", s); 460 } 461 cpf_reload(); 462 463 /* Clean up the old RS instance, the new instance will take over. */ 464 cleanup_service(rp); 465 466 /* Ask VM to pin memory for the new RS instance. */ 467 if((s = vm_memctl(RS_PROC_NR, VM_RS_MEM_PIN, 0, 0)) != OK) { 468 panic("unable to pin memory for the new RS instance: %d", s); 469 } 470 } 471 else { 472 /* Old RS instance running. */ 473 474 /* Set up privileges for the new instance and let it run. */ 475 s = sys_privctl(replica_endpoint, SYS_PRIV_SET_SYS, &(replica_rp->r_priv)); 476 if(s != OK) { 477 panic("unable to set privileges for the new RS instance: %d", s); 478 } 479 if ((s = sched_init_proc(replica_rp)) != OK) { 480 panic("unable to initialize RS replica scheduling: %d", s); 481 } 482 s = sys_privctl(replica_endpoint, SYS_PRIV_YIELD, NULL); 483 if(s != OK) { 484 panic("unable to yield control to the new RS instance: %d", s); 485 } 486 NOT_REACHABLE; 487 } 488 #endif /* USE_LIVEUPDATE */ 489 490 return(OK); 491 } 492 493 /*===========================================================================* 494 * sef_cb_init_restart * 495 *===========================================================================*/ 496 static int sef_cb_init_restart(int type, sef_init_info_t *info) 497 { 498 /* Restart the reincarnation server. */ 499 int r; 500 struct rproc *old_rs_rp, *new_rs_rp; 501 502 assert(info->endpoint == RS_PROC_NR); 503 504 /* Perform default state transfer first. */ 505 r = SEF_CB_INIT_RESTART_STATEFUL(type, info); 506 if(r != OK) { 507 printf("SEF_CB_INIT_RESTART_STATEFUL failed: %d\n", r); 508 return r; 509 } 510 511 /* New RS takes over. */ 512 old_rs_rp = rproc_ptr[_ENDPOINT_P(RS_PROC_NR)]; 513 new_rs_rp = rproc_ptr[_ENDPOINT_P(info->old_endpoint)]; 514 if(rs_verbose) 515 printf("RS: %s is the new RS after restart\n", srv_to_string(new_rs_rp)); 516 517 /* If an update was in progress, end it. */ 518 if(SRV_IS_UPDATING(old_rs_rp)) { 519 end_update(ERESTART, RS_REPLY); 520 } 521 522 /* Update the service into the replica. */ 523 r = update_service(&old_rs_rp, &new_rs_rp, RS_DONTSWAP, 0); 524 if(r != OK) { 525 printf("update_service failed: %d\n", r); 526 return r; 527 } 528 529 /* Initialize the new RS instance. */ 530 r = init_service(new_rs_rp, SEF_INIT_RESTART, 0); 531 if(r != OK) { 532 printf("init_service failed: %d\n", r); 533 return r; 534 } 535 536 /* Reschedule a synchronous alarm for the next period. */ 537 if (OK != (r=sys_setalarm(RS_DELTA_T, 0))) 538 panic("couldn't set alarm: %d", r); 539 540 return OK; 541 } 542 543 /*===========================================================================* 544 * sef_cb_init_lu * 545 *===========================================================================*/ 546 static int sef_cb_init_lu(int type, sef_init_info_t *info) 547 { 548 /* Start a new version of the reincarnation server. */ 549 int r; 550 struct rproc *old_rs_rp, *new_rs_rp; 551 552 assert(info->endpoint == RS_PROC_NR); 553 554 /* Perform default state transfer first. */ 555 sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL); 556 r = SEF_CB_INIT_LU_DEFAULT(type, info); 557 if(r != OK) { 558 printf("SEF_CB_INIT_LU_DEFAULT failed: %d\n", r); 559 return r; 560 } 561 562 /* New RS takes over. */ 563 old_rs_rp = rproc_ptr[_ENDPOINT_P(RS_PROC_NR)]; 564 new_rs_rp = rproc_ptr[_ENDPOINT_P(info->old_endpoint)]; 565 if(rs_verbose) 566 printf("RS: %s is the new RS after live update\n", 567 srv_to_string(new_rs_rp)); 568 569 /* Update the service into the replica. */ 570 r = update_service(&old_rs_rp, &new_rs_rp, RS_DONTSWAP, 0); 571 if(r != OK) { 572 printf("update_service failed: %d\n", r); 573 return r; 574 } 575 576 /* Check if everything is as expected. */ 577 assert(RUPDATE_IS_UPDATING()); 578 assert(RUPDATE_IS_INITIALIZING()); 579 assert(rupdate.num_rpupds > 0); 580 assert(rupdate.num_init_ready_pending > 0); 581 582 return OK; 583 } 584 585 /*===========================================================================* 586 * sef_cb_init_response * 587 *===========================================================================*/ 588 int sef_cb_init_response(message *m_ptr) 589 { 590 int r; 591 592 /* Return now if RS initialization failed. */ 593 r = m_ptr->m_rs_init.result; 594 if(r != OK) { 595 return r; 596 } 597 598 /* Simulate an RS-to-RS init message. */ 599 r = do_init_ready(m_ptr); 600 601 /* Assume everything is OK if EDONTREPLY was returned. */ 602 if(r == EDONTREPLY) { 603 r = OK; 604 } 605 return r; 606 } 607 608 /*===========================================================================* 609 * sef_cb_lu_response * 610 *===========================================================================*/ 611 int sef_cb_lu_response(message *m_ptr) 612 { 613 int r; 614 615 /* Simulate an RS-to-RS update ready message. */ 616 r = do_upd_ready(m_ptr); 617 618 /* If we get this far, we didn't get updated for some reason. Report error. */ 619 if(r == EDONTREPLY) { 620 r = EGENERIC; 621 } 622 return r; 623 } 624 625 /*===========================================================================* 626 * sef_cb_signal_handler * 627 *===========================================================================*/ 628 static void sef_cb_signal_handler(int signo) 629 { 630 /* Check for known signals, ignore anything else. */ 631 switch(signo) { 632 case SIGCHLD: 633 do_sigchld(); 634 break; 635 case SIGTERM: 636 do_shutdown(NULL); 637 break; 638 } 639 } 640 641 /*===========================================================================* 642 * sef_cb_signal_manager * 643 *===========================================================================*/ 644 static int sef_cb_signal_manager(endpoint_t target, int signo) 645 { 646 /* Process system signal on behalf of the kernel. */ 647 int target_p; 648 struct rproc *rp; 649 message m; 650 651 /* Lookup slot. */ 652 if(rs_isokendpt(target, &target_p) != OK || rproc_ptr[target_p] == NULL) { 653 if(rs_verbose) 654 printf("RS: ignoring spurious signal %d for process %d\n", 655 signo, target); 656 return OK; /* clear the signal */ 657 } 658 rp = rproc_ptr[target_p]; 659 660 /* Don't bother if a termination signal has already been processed. */ 661 if((rp->r_flags & RS_TERMINATED) && !(rp->r_flags & RS_EXITING)) { 662 return EDEADEPT; /* process is gone */ 663 } 664 665 /* Ignore external signals for inactive service instances. */ 666 if( !(rp->r_flags & RS_ACTIVE) && !(rp->r_flags & RS_EXITING)) { 667 if(rs_verbose) 668 printf("RS: ignoring signal %d for inactive %s\n", 669 signo, srv_to_string(rp)); 670 return OK; /* clear the signal */ 671 } 672 673 if(rs_verbose) 674 printf("RS: %s got %s signal %d\n", srv_to_string(rp), 675 SIGS_IS_TERMINATION(signo) ? "termination" : "non-termination",signo); 676 677 /* Print stacktrace if necessary. */ 678 if(SIGS_IS_STACKTRACE(signo)) { 679 sys_diagctl_stacktrace(target); 680 } 681 682 /* In case of termination signal handle the event. */ 683 if(SIGS_IS_TERMINATION(signo)) { 684 rp->r_flags |= RS_TERMINATED; 685 terminate_service(rp); 686 rs_idle_period(); 687 688 return EDEADEPT; /* process is now gone */ 689 } 690 /* Never deliver signals to VM. */ 691 if (rp->r_pub->endpoint == VM_PROC_NR) { 692 return OK; 693 } 694 695 /* Translate every non-termination signal into a message. */ 696 m.m_type = SIGS_SIGNAL_RECEIVED; 697 m.m_pm_lsys_sigs_signal.num = signo; 698 rs_asynsend(rp, &m, 1); 699 700 return OK; /* signal has been delivered */ 701 } 702 703 /*===========================================================================* 704 * boot_image_info_lookup * 705 *===========================================================================*/ 706 static void boot_image_info_lookup(endpoint, image, ip, pp, sp, dp) 707 endpoint_t endpoint; 708 struct boot_image *image; 709 struct boot_image **ip; 710 struct boot_image_priv **pp; 711 struct boot_image_sys **sp; 712 struct boot_image_dev **dp; 713 { 714 /* Lookup entries in boot image tables. */ 715 int i; 716 717 /* When requested, locate the corresponding entry in the boot image table 718 * or panic if not found. 719 */ 720 if(ip) { 721 for (i=0; i < NR_BOOT_PROCS; i++) { 722 if(image[i].endpoint == endpoint) { 723 *ip = &image[i]; 724 break; 725 } 726 } 727 if(i == NR_BOOT_PROCS) { 728 panic("boot image table lookup failed"); 729 } 730 } 731 732 /* When requested, locate the corresponding entry in the boot image priv table 733 * or panic if not found. 734 */ 735 if(pp) { 736 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 737 if(boot_image_priv_table[i].endpoint == endpoint) { 738 *pp = &boot_image_priv_table[i]; 739 break; 740 } 741 } 742 if(i == NULL_BOOT_NR) { 743 panic("boot image priv table lookup failed"); 744 } 745 } 746 747 /* When requested, locate the corresponding entry in the boot image sys table 748 * or resort to the default entry if not found. 749 */ 750 if(sp) { 751 for (i=0; boot_image_sys_table[i].endpoint != DEFAULT_BOOT_NR; i++) { 752 if(boot_image_sys_table[i].endpoint == endpoint) { 753 *sp = &boot_image_sys_table[i]; 754 break; 755 } 756 } 757 if(boot_image_sys_table[i].endpoint == DEFAULT_BOOT_NR) { 758 *sp = &boot_image_sys_table[i]; /* accept the default entry */ 759 } 760 } 761 762 /* When requested, locate the corresponding entry in the boot image dev table 763 * or resort to the default entry if not found. 764 */ 765 if(dp) { 766 for (i=0; boot_image_dev_table[i].endpoint != DEFAULT_BOOT_NR; i++) { 767 if(boot_image_dev_table[i].endpoint == endpoint) { 768 *dp = &boot_image_dev_table[i]; 769 break; 770 } 771 } 772 if(boot_image_dev_table[i].endpoint == DEFAULT_BOOT_NR) { 773 *dp = &boot_image_dev_table[i]; /* accept the default entry */ 774 } 775 } 776 } 777 778 /*===========================================================================* 779 * catch_boot_init_ready * 780 *===========================================================================*/ 781 static void catch_boot_init_ready(endpoint) 782 endpoint_t endpoint; 783 { 784 /* Block and catch an init ready message from the given source. */ 785 int r; 786 int ipc_status; 787 message m; 788 struct rproc *rp; 789 int result; 790 791 /* Receive init ready message. */ 792 if ((r = sef_receive_status(endpoint, &m, &ipc_status)) != OK) { 793 panic("unable to receive init reply: %d", r); 794 } 795 if(m.m_type != RS_INIT) { 796 panic("unexpected reply from service: %d", m.m_source); 797 } 798 result = m.m_rs_init.result; 799 rp = rproc_ptr[_ENDPOINT_P(m.m_source)]; 800 801 /* Check result. */ 802 if(result != OK) { 803 panic("unable to complete init for service: %d", m.m_source); 804 } 805 806 /* Send a reply to unblock the service, except to VM, which sent the reply 807 * asynchronously. Synchronous replies could lead to deadlocks there. 808 */ 809 if (m.m_source != VM_PROC_NR) { 810 m.m_type = OK; 811 reply(m.m_source, rp, &m); 812 } 813 814 /* Mark the slot as no longer initializing. */ 815 rp->r_flags &= ~RS_INITIALIZING; 816 rp->r_check_tm = 0; 817 getticks(&rp->r_alive_tm); 818 } 819 820 /*===========================================================================* 821 * get_work * 822 *===========================================================================*/ 823 static void get_work(m_ptr, status_ptr) 824 message *m_ptr; /* pointer to message */ 825 int *status_ptr; /* pointer to status */ 826 { 827 int r; 828 if (OK != (r=sef_receive_status(ANY, m_ptr, status_ptr))) 829 panic("sef_receive_status failed: %d", r); 830 } 831 832