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 rp->r_alive_tm = getticks(); /* currently alive */ 333 rp->r_stop_tm = 0; /* not exiting yet */ 334 rp->r_asr_count = 0; /* no ASR updates yet */ 335 rp->r_restarts = 0; /* no restarts so far */ 336 rp->r_period = 0; /* no period yet */ 337 rp->r_exec = NULL; /* no in-memory copy yet */ 338 rp->r_exec_len = 0; 339 340 /* Mark as in use and active. */ 341 rp->r_flags = RS_IN_USE | RS_ACTIVE; 342 rproc_ptr[_ENDPOINT_P(rpub->endpoint)]= rp; 343 rpub->in_use = TRUE; 344 } 345 346 /* - Step 2: allow every system service in the boot image to run. */ 347 nr_uncaught_init_srvs = 0; 348 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 349 boot_image_priv = &boot_image_priv_table[i]; 350 351 /* System services only. */ 352 if(iskerneln(_ENDPOINT_P(boot_image_priv->endpoint))) { 353 continue; 354 } 355 356 /* Lookup the corresponding slot in the system process table. */ 357 rp = &rproc[boot_image_priv - boot_image_priv_table]; 358 rpub = rp->r_pub; 359 360 /* RS/VM are already running as we speak. */ 361 if(boot_image_priv->endpoint == RS_PROC_NR || 362 boot_image_priv->endpoint == VM_PROC_NR) { 363 if ((s = init_service(rp, SEF_INIT_FRESH, rp->r_priv.s_init_flags)) != OK) { 364 panic("unable to initialize %d: %d", boot_image_priv->endpoint, s); 365 } 366 /* VM will still send an RS_INIT message, though. */ 367 if (boot_image_priv->endpoint != RS_PROC_NR) { 368 nr_uncaught_init_srvs++; 369 } 370 continue; 371 } 372 373 /* Allow the service to run. */ 374 if ((s = sched_init_proc(rp)) != OK) { 375 panic("unable to initialize scheduling: %d", s); 376 } 377 if ((s = sys_privctl(rpub->endpoint, SYS_PRIV_ALLOW, NULL)) != OK) { 378 panic("unable to initialize privileges: %d", s); 379 } 380 381 /* Initialize service. We assume every service will always get 382 * back to us here at boot time. 383 */ 384 if(boot_image_priv->flags & SYS_PROC) { 385 if ((s = init_service(rp, SEF_INIT_FRESH, rp->r_priv.s_init_flags)) != OK) { 386 panic("unable to initialize service: %d", s); 387 } 388 if(rpub->sys_flags & SF_SYNCH_BOOT) { 389 /* Catch init ready message now to synchronize. */ 390 catch_boot_init_ready(rpub->endpoint); 391 } 392 else { 393 /* Catch init ready message later. */ 394 nr_uncaught_init_srvs++; 395 } 396 } 397 } 398 399 /* - Step 3: let every system service complete initialization by 400 * catching all the init ready messages left. 401 */ 402 while(nr_uncaught_init_srvs) { 403 catch_boot_init_ready(ANY); 404 nr_uncaught_init_srvs--; 405 } 406 407 /* - Step 4: all the system services in the boot image are now running. 408 * Complete the initialization of the system process table in collaboration 409 * with other system services. 410 */ 411 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 412 boot_image_priv = &boot_image_priv_table[i]; 413 414 /* System services only. */ 415 if(iskerneln(_ENDPOINT_P(boot_image_priv->endpoint))) { 416 continue; 417 } 418 419 /* Lookup the corresponding slot in the system process table. */ 420 rp = &rproc[boot_image_priv - boot_image_priv_table]; 421 rpub = rp->r_pub; 422 423 /* Get pid from PM. */ 424 rp->r_pid = getnpid(rpub->endpoint); 425 if(rp->r_pid < 0) { 426 panic("unable to get pid: %d", rp->r_pid); 427 } 428 } 429 430 /* Set alarm to periodically check service status. */ 431 if (OK != (s=sys_setalarm(RS_DELTA_T, 0))) 432 panic("couldn't set alarm: %d", s); 433 434 #if USE_LIVEUPDATE 435 /* Now create a new RS instance and let the current 436 * instance live update into the replica. Clone RS' own slot first. 437 */ 438 rp = rproc_ptr[_ENDPOINT_P(RS_PROC_NR)]; 439 if((s = clone_slot(rp, &replica_rp)) != OK) { 440 panic("unable to clone current RS instance: %d", s); 441 } 442 443 /* Fork a new RS instance with root:operator. */ 444 pid = srv_fork(0, 0); 445 if(pid < 0) { 446 panic("unable to fork a new RS instance: %d", pid); 447 } 448 replica_pid = pid ? pid : getpid(); 449 if ((s = getprocnr(replica_pid, &replica_endpoint)) != 0) 450 panic("unable to get replica endpoint: %d", s); 451 replica_rp->r_pid = replica_pid; 452 replica_rp->r_pub->endpoint = replica_endpoint; 453 454 if(pid == 0) { 455 /* New RS instance running. */ 456 457 /* Live update the old instance into the new one. */ 458 s = update_service(&rp, &replica_rp, RS_SWAP, 0); 459 if(s != OK) { 460 panic("unable to live update RS: %d", s); 461 } 462 cpf_reload(); 463 464 /* Clean up the old RS instance, the new instance will take over. */ 465 cleanup_service(rp); 466 467 /* Ask VM to pin memory for the new RS instance. */ 468 if((s = vm_memctl(RS_PROC_NR, VM_RS_MEM_PIN, 0, 0)) != OK) { 469 panic("unable to pin memory for the new RS instance: %d", s); 470 } 471 } 472 else { 473 /* Old RS instance running. */ 474 475 /* Set up privileges for the new instance and let it run. */ 476 s = sys_privctl(replica_endpoint, SYS_PRIV_SET_SYS, &(replica_rp->r_priv)); 477 if(s != OK) { 478 panic("unable to set privileges for the new RS instance: %d", s); 479 } 480 if ((s = sched_init_proc(replica_rp)) != OK) { 481 panic("unable to initialize RS replica scheduling: %d", s); 482 } 483 s = sys_privctl(replica_endpoint, SYS_PRIV_YIELD, NULL); 484 if(s != OK) { 485 panic("unable to yield control to the new RS instance: %d", s); 486 } 487 NOT_REACHABLE; 488 } 489 #endif /* USE_LIVEUPDATE */ 490 491 return(OK); 492 } 493 494 /*===========================================================================* 495 * sef_cb_init_restart * 496 *===========================================================================*/ 497 static int sef_cb_init_restart(int type, sef_init_info_t *info) 498 { 499 /* Restart the reincarnation server. */ 500 int r; 501 struct rproc *old_rs_rp, *new_rs_rp; 502 503 assert(info->endpoint == RS_PROC_NR); 504 505 /* Perform default state transfer first. */ 506 r = SEF_CB_INIT_RESTART_STATEFUL(type, info); 507 if(r != OK) { 508 printf("SEF_CB_INIT_RESTART_STATEFUL failed: %d\n", r); 509 return r; 510 } 511 512 /* New RS takes over. */ 513 old_rs_rp = rproc_ptr[_ENDPOINT_P(RS_PROC_NR)]; 514 new_rs_rp = rproc_ptr[_ENDPOINT_P(info->old_endpoint)]; 515 if(rs_verbose) 516 printf("RS: %s is the new RS after restart\n", srv_to_string(new_rs_rp)); 517 518 /* If an update was in progress, end it. */ 519 if(SRV_IS_UPDATING(old_rs_rp)) { 520 end_update(ERESTART, RS_REPLY); 521 } 522 523 /* Update the service into the replica. */ 524 r = update_service(&old_rs_rp, &new_rs_rp, RS_DONTSWAP, 0); 525 if(r != OK) { 526 printf("update_service failed: %d\n", r); 527 return r; 528 } 529 530 /* Initialize the new RS instance. */ 531 r = init_service(new_rs_rp, SEF_INIT_RESTART, 0); 532 if(r != OK) { 533 printf("init_service failed: %d\n", r); 534 return r; 535 } 536 537 /* Reschedule a synchronous alarm for the next period. */ 538 if (OK != (r=sys_setalarm(RS_DELTA_T, 0))) 539 panic("couldn't set alarm: %d", r); 540 541 return OK; 542 } 543 544 /*===========================================================================* 545 * sef_cb_init_lu * 546 *===========================================================================*/ 547 static int sef_cb_init_lu(int type, sef_init_info_t *info) 548 { 549 /* Start a new version of the reincarnation server. */ 550 int r; 551 struct rproc *old_rs_rp, *new_rs_rp; 552 553 assert(info->endpoint == RS_PROC_NR); 554 555 /* Perform default state transfer first. */ 556 sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL); 557 r = SEF_CB_INIT_LU_DEFAULT(type, info); 558 if(r != OK) { 559 printf("SEF_CB_INIT_LU_DEFAULT failed: %d\n", r); 560 return r; 561 } 562 563 /* New RS takes over. */ 564 old_rs_rp = rproc_ptr[_ENDPOINT_P(RS_PROC_NR)]; 565 new_rs_rp = rproc_ptr[_ENDPOINT_P(info->old_endpoint)]; 566 if(rs_verbose) 567 printf("RS: %s is the new RS after live update\n", 568 srv_to_string(new_rs_rp)); 569 570 /* Update the service into the replica. */ 571 r = update_service(&old_rs_rp, &new_rs_rp, RS_DONTSWAP, 0); 572 if(r != OK) { 573 printf("update_service failed: %d\n", r); 574 return r; 575 } 576 577 /* Check if everything is as expected. */ 578 assert(RUPDATE_IS_UPDATING()); 579 assert(RUPDATE_IS_INITIALIZING()); 580 assert(rupdate.num_rpupds > 0); 581 assert(rupdate.num_init_ready_pending > 0); 582 583 return OK; 584 } 585 586 /*===========================================================================* 587 * sef_cb_init_response * 588 *===========================================================================*/ 589 int sef_cb_init_response(message *m_ptr) 590 { 591 int r; 592 593 /* Return now if RS initialization failed. */ 594 r = m_ptr->m_rs_init.result; 595 if(r != OK) { 596 return r; 597 } 598 599 /* Simulate an RS-to-RS init message. */ 600 r = do_init_ready(m_ptr); 601 602 /* Assume everything is OK if EDONTREPLY was returned. */ 603 if(r == EDONTREPLY) { 604 r = OK; 605 } 606 return r; 607 } 608 609 /*===========================================================================* 610 * sef_cb_lu_response * 611 *===========================================================================*/ 612 int sef_cb_lu_response(message *m_ptr) 613 { 614 int r; 615 616 /* Simulate an RS-to-RS update ready message. */ 617 r = do_upd_ready(m_ptr); 618 619 /* If we get this far, we didn't get updated for some reason. Report error. */ 620 if(r == EDONTREPLY) { 621 r = EGENERIC; 622 } 623 return r; 624 } 625 626 /*===========================================================================* 627 * sef_cb_signal_handler * 628 *===========================================================================*/ 629 static void sef_cb_signal_handler(int signo) 630 { 631 /* Check for known signals, ignore anything else. */ 632 switch(signo) { 633 case SIGCHLD: 634 do_sigchld(); 635 break; 636 case SIGTERM: 637 do_shutdown(NULL); 638 break; 639 } 640 } 641 642 /*===========================================================================* 643 * sef_cb_signal_manager * 644 *===========================================================================*/ 645 static int sef_cb_signal_manager(endpoint_t target, int signo) 646 { 647 /* Process system signal on behalf of the kernel. */ 648 int target_p; 649 struct rproc *rp; 650 message m; 651 652 /* Lookup slot. */ 653 if(rs_isokendpt(target, &target_p) != OK || rproc_ptr[target_p] == NULL) { 654 if(rs_verbose) 655 printf("RS: ignoring spurious signal %d for process %d\n", 656 signo, target); 657 return OK; /* clear the signal */ 658 } 659 rp = rproc_ptr[target_p]; 660 661 /* Don't bother if a termination signal has already been processed. */ 662 if((rp->r_flags & RS_TERMINATED) && !(rp->r_flags & RS_EXITING)) { 663 return EDEADEPT; /* process is gone */ 664 } 665 666 /* Ignore external signals for inactive service instances. */ 667 if( !(rp->r_flags & RS_ACTIVE) && !(rp->r_flags & RS_EXITING)) { 668 if(rs_verbose) 669 printf("RS: ignoring signal %d for inactive %s\n", 670 signo, srv_to_string(rp)); 671 return OK; /* clear the signal */ 672 } 673 674 if(rs_verbose) 675 printf("RS: %s got %s signal %d\n", srv_to_string(rp), 676 SIGS_IS_TERMINATION(signo) ? "termination" : "non-termination",signo); 677 678 /* Print stacktrace if necessary. */ 679 if(SIGS_IS_STACKTRACE(signo)) { 680 sys_diagctl_stacktrace(target); 681 } 682 683 /* In case of termination signal handle the event. */ 684 if(SIGS_IS_TERMINATION(signo)) { 685 rp->r_flags |= RS_TERMINATED; 686 terminate_service(rp); 687 rs_idle_period(); 688 689 return EDEADEPT; /* process is now gone */ 690 } 691 /* Never deliver signals to VM. */ 692 if (rp->r_pub->endpoint == VM_PROC_NR) { 693 return OK; 694 } 695 696 /* Translate every non-termination signal into a message. */ 697 m.m_type = SIGS_SIGNAL_RECEIVED; 698 m.m_pm_lsys_sigs_signal.num = signo; 699 rs_asynsend(rp, &m, 1); 700 701 return OK; /* signal has been delivered */ 702 } 703 704 /*===========================================================================* 705 * boot_image_info_lookup * 706 *===========================================================================*/ 707 static void boot_image_info_lookup(endpoint, image, ip, pp, sp, dp) 708 endpoint_t endpoint; 709 struct boot_image *image; 710 struct boot_image **ip; 711 struct boot_image_priv **pp; 712 struct boot_image_sys **sp; 713 struct boot_image_dev **dp; 714 { 715 /* Lookup entries in boot image tables. */ 716 int i; 717 718 /* When requested, locate the corresponding entry in the boot image table 719 * or panic if not found. 720 */ 721 if(ip) { 722 for (i=0; i < NR_BOOT_PROCS; i++) { 723 if(image[i].endpoint == endpoint) { 724 *ip = &image[i]; 725 break; 726 } 727 } 728 if(i == NR_BOOT_PROCS) { 729 panic("boot image table lookup failed"); 730 } 731 } 732 733 /* When requested, locate the corresponding entry in the boot image priv table 734 * or panic if not found. 735 */ 736 if(pp) { 737 for (i=0; boot_image_priv_table[i].endpoint != NULL_BOOT_NR; i++) { 738 if(boot_image_priv_table[i].endpoint == endpoint) { 739 *pp = &boot_image_priv_table[i]; 740 break; 741 } 742 } 743 if(i == NULL_BOOT_NR) { 744 panic("boot image priv table lookup failed"); 745 } 746 } 747 748 /* When requested, locate the corresponding entry in the boot image sys table 749 * or resort to the default entry if not found. 750 */ 751 if(sp) { 752 for (i=0; boot_image_sys_table[i].endpoint != DEFAULT_BOOT_NR; i++) { 753 if(boot_image_sys_table[i].endpoint == endpoint) { 754 *sp = &boot_image_sys_table[i]; 755 break; 756 } 757 } 758 if(boot_image_sys_table[i].endpoint == DEFAULT_BOOT_NR) { 759 *sp = &boot_image_sys_table[i]; /* accept the default entry */ 760 } 761 } 762 763 /* When requested, locate the corresponding entry in the boot image dev table 764 * or resort to the default entry if not found. 765 */ 766 if(dp) { 767 for (i=0; boot_image_dev_table[i].endpoint != DEFAULT_BOOT_NR; i++) { 768 if(boot_image_dev_table[i].endpoint == endpoint) { 769 *dp = &boot_image_dev_table[i]; 770 break; 771 } 772 } 773 if(boot_image_dev_table[i].endpoint == DEFAULT_BOOT_NR) { 774 *dp = &boot_image_dev_table[i]; /* accept the default entry */ 775 } 776 } 777 } 778 779 /*===========================================================================* 780 * catch_boot_init_ready * 781 *===========================================================================*/ 782 static void catch_boot_init_ready(endpoint) 783 endpoint_t endpoint; 784 { 785 /* Block and catch an init ready message from the given source. */ 786 int r; 787 int ipc_status; 788 message m; 789 struct rproc *rp; 790 int result; 791 792 /* Receive init ready message. */ 793 if ((r = sef_receive_status(endpoint, &m, &ipc_status)) != OK) { 794 panic("unable to receive init reply: %d", r); 795 } 796 if(m.m_type != RS_INIT) { 797 panic("unexpected reply from service: %d", m.m_source); 798 } 799 result = m.m_rs_init.result; 800 rp = rproc_ptr[_ENDPOINT_P(m.m_source)]; 801 802 /* Check result. */ 803 if(result != OK) { 804 panic("unable to complete init for service: %d", m.m_source); 805 } 806 807 /* Send a reply to unblock the service, except to VM, which sent the reply 808 * asynchronously. Synchronous replies could lead to deadlocks there. 809 */ 810 if (m.m_source != VM_PROC_NR) { 811 m.m_type = OK; 812 reply(m.m_source, rp, &m); 813 } 814 815 /* Mark the slot as no longer initializing. */ 816 rp->r_flags &= ~RS_INITIALIZING; 817 rp->r_check_tm = 0; 818 rp->r_alive_tm = getticks(); 819 } 820 821 /*===========================================================================* 822 * get_work * 823 *===========================================================================*/ 824 static void get_work(m_ptr, status_ptr) 825 message *m_ptr; /* pointer to message */ 826 int *status_ptr; /* pointer to status */ 827 { 828 int r; 829 if (OK != (r=sef_receive_status(ANY, m_ptr, status_ptr))) 830 panic("sef_receive_status failed: %d", r); 831 } 832 833