Projects
Browse Source     Search     Timeline     Wiki

Changeset 23470

Show
Ignore:
Timestamp:
12/19/07 10:13:19 (12 months ago)
Author:
zarzycki@…
Message:

Better shutdown logging.

Location:
trunk/launchd/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/launchd/src/launchd.c

    r23466 r23470  
    9696static void prep_shutdown_log_dir(void); 
    9797 
    98 static bool re_exec_in_single_user_mode = false; 
     98static bool re_exec_in_single_user_mode; 
    9999static void *crash_addr; 
    100100static pid_t crash_pid; 
    101101 
    102 bool shutdown_in_progress = false; 
    103 bool network_up = false; 
     102bool shutdown_in_progress; 
     103bool fake_shutdown_in_progress; 
     104bool network_up; 
    104105 
    105106int 
     
    262263        } 
    263264 
     265        runtime_log_push(); 
     266 
    264267        if (launchd_assumes(gettimeofday(&tvnow, NULL) != -1)) { 
    265268                runtime_syslog(LOG_NOTICE, "Shutdown began at: %lu.%06u", tvnow.tv_sec, tvnow.tv_usec); 
    266269        } 
    267  
    268         launchd_log_vm_stats(); 
    269270 
    270271        launchd_assert(jobmgr_shutdown(root_jobmgr) != NULL); 
  • trunk/launchd/src/launchd.h

    r23466 r23470  
    3333 
    3434extern bool shutdown_in_progress; 
     35extern bool fake_shutdown_in_progress; 
    3536extern bool network_up; 
    3637 
  • trunk/launchd/src/launchd_core_logic.c

    r23469 r23470  
    24822482jobmgr_callback(void *obj, struct kevent *kev) 
    24832483{ 
     2484        struct timeval tvnow; 
    24842485        jobmgr_t jm = obj; 
     2486        job_t ji; 
    24852487 
    24862488        switch (kev->filter) { 
     
    24972499                case SIGUSR1: 
    24982500                        return calendarinterval_callback(); 
     2501                case SIGUSR2: 
     2502                        fake_shutdown_in_progress = true; 
     2503                        runtime_setlogmask(LOG_UPTO(LOG_DEBUG)); 
     2504 
     2505                        runtime_closelog(); /* HACK -- force 'start' time to be set */ 
     2506 
     2507                        if (getpid() == 1 && jobmgr_assumes(jm, gettimeofday(&tvnow, NULL) != -1)) { 
     2508                                jobmgr_log(jm, LOG_NOTICE, "Anticipatory shutdown began at: %lu.%06u", tvnow.tv_sec, tvnow.tv_usec); 
     2509                                LIST_FOREACH(ji, &root_jobmgr->jobs, sle) { 
     2510                                        if (ji->per_user && ji->p) { 
     2511                                                job_assumes(ji, runtime_kill(ji->p, SIGUSR2) != -1); 
     2512                                        } 
     2513                                } 
     2514                        } else { 
     2515                                jobmgr_log(jm, LOG_NOTICE, "Anticipatory per-user launchd shutdown"); 
     2516                        } 
     2517 
     2518                        return; 
    24992519                default: 
    25002520                        return (void)jobmgr_assumes(jm, false); 
     
    44544474                jobmgr_assumes(jmr, kevent_mod(SIGTERM, EVFILT_SIGNAL, EV_ADD, 0, 0, jmr) != -1); 
    44554475                jobmgr_assumes(jmr, kevent_mod(SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, jmr) != -1); 
     4476                jobmgr_assumes(jmr, kevent_mod(SIGUSR2, EVFILT_SIGNAL, EV_ADD, 0, 0, jmr) != -1); 
    44564477                jobmgr_assumes(jmr, kevent_mod(0, EVFILT_FS, EV_ADD, VQ_MOUNT|VQ_UNMOUNT|VQ_UPDATE, 0, jmr) != -1); 
    44574478        } 
     
    57505771job_mig_check_in(job_t j, name_t servicename, mach_port_t *serviceportp) 
    57515772{ 
    5752         static pid_t last_warned_pid = 0; 
    57535773        struct machservice *ms; 
    57545774        struct ldcred ldc; 
     
    57695789 
    57705790        if ((jo = machservice_job(ms)) != j) { 
     5791                static pid_t last_warned_pid; 
     5792 
    57715793                if (last_warned_pid != ldc.pid) { 
    57725794                        job_log(j, LOG_NOTICE, "Check-in of Mach service failed. The service \"%s\" is owned by: %s", servicename, jo->label); 
    57735795                        last_warned_pid = ldc.pid; 
    57745796                } 
    5775                  return BOOTSTRAP_NOT_PRIVILEGED; 
     5797 
     5798                return BOOTSTRAP_NOT_PRIVILEGED; 
    57765799        } 
    57775800        if (machservice_active(ms)) { 
  • trunk/launchd/src/launchd_runtime.c

    r23469 r23470  
    106106static void runtime_log_uncork_pending_drain(void); 
    107107static kern_return_t runtime_log_pack(vm_offset_t *outval, mach_msg_type_number_t *outvalCnt); 
    108 static void runtime_log_push(void); 
    109108 
    110109static bool logmsg_add(struct runtime_syslog_attr *attr, int err_num, const char *msg); 
     
    117116}; 
    118117static sigset_t sigign_set; 
     118static FILE *ourlogfile; 
     119 
    119120 
    120121mach_port_t 
     
    11181119} 
    11191120 
    1120 static FILE *ourlogfile; 
    1121  
    11221121void 
    11231122runtime_closelog(void) 
    11241123{ 
     1124        runtime_log_push(); 
     1125 
    11251126        if (ourlogfile) { 
    11261127                launchd_assumes(fflush(ourlogfile) == 0); 
     
    11321133runtime_fsync(int fd) 
    11331134{ 
     1135#if 0 
    11341136        if (do_apple_internal_logging()) { 
    11351137                return fcntl(fd, F_FULLFSYNC, NULL); 
     
    11371139                return fsync(fd); 
    11381140        } 
     1141#else 
     1142        return fsync(fd); 
     1143#endif 
    11391144} 
    11401145 
     
    11671172runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) 
    11681173{ 
    1169         static pthread_mutex_t ourlock = PTHREAD_MUTEX_INITIALIZER; 
    1170         static struct timeval shutdown_start; 
    1171         static struct timeval prev_msg; 
    1172         struct timeval tvnow, tvd_total, tvd_msg_delta = { 0, 0 }; 
    11731174        int saved_errno = errno; 
    11741175        char newmsg[10000]; 
    1175         size_t i, j; 
    11761176 
    11771177        if (attr->priority == LOG_APPLEONLY) { 
     
    11871187        } 
    11881188 
    1189         if (getpid() != 1 || !shutdown_in_progress) { 
    1190                 vsnprintf(newmsg, sizeof(newmsg), message, args); 
    1191                 logmsg_add(attr, saved_errno, newmsg); 
    1192                 return; 
    1193         } 
    1194  
    1195         if (shutdown_start.tv_sec == 0) { 
    1196                 gettimeofday(&shutdown_start, NULL); 
    1197         } 
    1198  
    1199         if (gettimeofday(&tvnow, NULL) == -1) { 
    1200                 tvnow.tv_sec = 0; 
    1201                 tvnow.tv_usec = 0; 
    1202         } 
    1203  
    1204         pthread_mutex_lock(&ourlock); 
    1205  
    1206         if (ourlogfile == NULL) { 
    1207                 rename("/var/log/launchd-shutdown.log", "/var/log/launchd-shutdown.log.1"); 
    1208                 ourlogfile = fopen("/var/log/launchd-shutdown.log", "a"); 
    1209         } 
    1210  
    1211         pthread_mutex_unlock(&ourlock); 
    1212  
    1213         if (ourlogfile == NULL) { 
    1214                 return; 
    1215         } 
    1216  
    1217         if (message == NULL) { 
    1218                 return; 
    1219         } 
    1220  
    1221         timersub(&tvnow, &shutdown_start, &tvd_total); 
    1222  
    1223         if (prev_msg.tv_sec != 0) { 
    1224                 timersub(&tvnow, &prev_msg, &tvd_msg_delta); 
    1225         } 
    1226  
    1227         prev_msg = tvnow; 
    1228  
    1229         snprintf(newmsg, sizeof(newmsg), "%3ld.%06d%4ld.%06d%6u %-40s%6u %-40s ", 
    1230                         tvd_total.tv_sec, tvd_total.tv_usec, 
    1231                         tvd_msg_delta.tv_sec, tvd_msg_delta.tv_usec, 
    1232                         attr->from_pid, attr->from_name, 
    1233                         attr->about_pid, attr->about_name); 
    1234  
    1235         for (i = 0, j = strlen(newmsg); message[i];) { 
    1236                 if (message[i] == '%' && message[i + 1] == 'm') { 
    1237                         char *errs = strerror(saved_errno); 
    1238                         strcpy(newmsg + j, errs ? errs : "unknown error"); 
    1239                         j += strlen(newmsg + j); 
    1240                         i += 2; 
    1241                 } else { 
    1242                         newmsg[j] = message[i]; 
    1243                         j++; 
    1244                         i++; 
    1245                 } 
    1246         } 
    1247  
    1248         strcpy(newmsg + j, "\n"); 
    1249  
    1250         vfprintf(ourlogfile, newmsg, args); 
    1251 } 
     1189        vsnprintf(newmsg, sizeof(newmsg), message, args); 
     1190        logmsg_add(attr, saved_errno, newmsg); 
     1191} 
     1192 
    12521193 
    12531194bool 
     
    13661307runtime_log_push(void) 
    13671308{ 
     1309        static pthread_mutex_t ourlock = PTHREAD_MUTEX_INITIALIZER; 
     1310        static struct timeval shutdown_start; 
     1311        struct timeval tvd; 
    13681312        mach_msg_type_number_t outvalCnt; 
     1313        struct logmsg_s *lm; 
    13691314        vm_offset_t outval; 
    13701315 
     
    13721317                launchd_assumes(STAILQ_EMPTY(&logmsg_queue)); 
    13731318                return; 
    1374         } else if (getpid() == 1) { 
     1319        } else if (getpid() != 1) { 
     1320                if (runtime_log_pack(&outval, &outvalCnt) == 0) { 
     1321                        launchd_assumes(_vprocmgr_log_forward(inherited_bootstrap_port, (void *)outval, outvalCnt) == NULL); 
     1322                        mig_deallocate(outval, outvalCnt); 
     1323                } 
     1324                return; 
     1325        } 
     1326 
     1327        if (!shutdown_in_progress && !fake_shutdown_in_progress) { 
    13751328                runtime_log_uncork_pending_drain(); 
    13761329                return; 
    13771330        } 
    13781331 
    1379         if (runtime_log_pack(&outval, &outvalCnt) != 0) { 
     1332        if (shutdown_start.tv_sec == 0) { 
     1333                gettimeofday(&shutdown_start, NULL); 
     1334                launchd_log_vm_stats(); 
     1335        } 
     1336 
     1337 
     1338        pthread_mutex_lock(&ourlock); 
     1339 
     1340        if (ourlogfile == NULL) { 
     1341                rename("/var/log/launchd-shutdown.log", "/var/log/launchd-shutdown.log.1"); 
     1342                ourlogfile = fopen("/var/log/launchd-shutdown.log", "a"); 
     1343        } 
     1344 
     1345        pthread_mutex_unlock(&ourlock); 
     1346 
     1347        if (!ourlogfile) { 
    13801348                return; 
    13811349        } 
    13821350 
    1383         launchd_assumes(_vprocmgr_log_forward(inherited_bootstrap_port, (void *)outval, outvalCnt) == NULL); 
    1384  
    1385         mig_deallocate(outval, outvalCnt); 
     1351        while ((lm = STAILQ_FIRST(&logmsg_queue))) { 
     1352                timersub(&lm->when, &shutdown_start, &tvd); 
     1353 
     1354                /* don't ask */ 
     1355                if (tvd.tv_sec < 0) { 
     1356                        tvd.tv_sec = 0; 
     1357                        tvd.tv_usec = 0; 
     1358                } 
     1359 
     1360                fprintf(ourlogfile, "%3ld.%06d%6u %-40s%6u %-40s %s\n", tvd.tv_sec, tvd.tv_usec, 
     1361                                lm->from_pid, lm->from_name, lm->about_pid, lm->about_name, lm->msg); 
     1362 
     1363                logmsg_remove(lm); 
     1364        } 
    13861365} 
    13871366 
     
    14251404runtime_log_drain(mach_port_t srp, vm_offset_t *outval, mach_msg_type_number_t *outvalCnt) 
    14261405{ 
    1427         if (logmsg_queue_cnt == 0) { 
    1428                 launchd_assumes(STAILQ_EMPTY(&logmsg_queue)); 
    1429                 launchd_assumes(drain_reply_port == 0); 
    1430  
     1406        launchd_assumes(drain_reply_port == 0); 
     1407 
     1408        if ((logmsg_queue_cnt == 0) || shutdown_in_progress || fake_shutdown_in_progress) { 
    14311409                drain_reply_port = srp; 
    14321410                launchd_assumes(launchd_mport_notify_req(drain_reply_port, MACH_NOTIFY_DEAD_NAME) == KERN_SUCCESS); 
  • trunk/launchd/src/launchd_runtime.h

    r23466 r23470  
    150150void runtime_syslog(int pri, const char *message, ...) __attribute__((format(printf, 2, 3))); 
    151151void runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) __attribute__((format(printf, 2, 0))); 
     152void runtime_log_push(void); 
    152153 
    153154