Changeset 23470
- Timestamp:
- 12/19/07 10:13:19 (12 months ago)
- Location:
- trunk/launchd/src
- Files:
-
- 5 modified
-
launchd.c (modified) (2 diffs)
-
launchd.h (modified) (1 diff)
-
launchd_core_logic.c (modified) (5 diffs)
-
launchd_runtime.c (modified) (10 diffs)
-
launchd_runtime.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/launchd/src/launchd.c
r23466 r23470 96 96 static void prep_shutdown_log_dir(void); 97 97 98 static bool re_exec_in_single_user_mode = false;98 static bool re_exec_in_single_user_mode; 99 99 static void *crash_addr; 100 100 static pid_t crash_pid; 101 101 102 bool shutdown_in_progress = false; 103 bool network_up = false; 102 bool shutdown_in_progress; 103 bool fake_shutdown_in_progress; 104 bool network_up; 104 105 105 106 int … … 262 263 } 263 264 265 runtime_log_push(); 266 264 267 if (launchd_assumes(gettimeofday(&tvnow, NULL) != -1)) { 265 268 runtime_syslog(LOG_NOTICE, "Shutdown began at: %lu.%06u", tvnow.tv_sec, tvnow.tv_usec); 266 269 } 267 268 launchd_log_vm_stats();269 270 270 271 launchd_assert(jobmgr_shutdown(root_jobmgr) != NULL); -
trunk/launchd/src/launchd.h
r23466 r23470 33 33 34 34 extern bool shutdown_in_progress; 35 extern bool fake_shutdown_in_progress; 35 36 extern bool network_up; 36 37 -
trunk/launchd/src/launchd_core_logic.c
r23469 r23470 2482 2482 jobmgr_callback(void *obj, struct kevent *kev) 2483 2483 { 2484 struct timeval tvnow; 2484 2485 jobmgr_t jm = obj; 2486 job_t ji; 2485 2487 2486 2488 switch (kev->filter) { … … 2497 2499 case SIGUSR1: 2498 2500 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; 2499 2519 default: 2500 2520 return (void)jobmgr_assumes(jm, false); … … 4454 4474 jobmgr_assumes(jmr, kevent_mod(SIGTERM, EVFILT_SIGNAL, EV_ADD, 0, 0, jmr) != -1); 4455 4475 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); 4456 4477 jobmgr_assumes(jmr, kevent_mod(0, EVFILT_FS, EV_ADD, VQ_MOUNT|VQ_UNMOUNT|VQ_UPDATE, 0, jmr) != -1); 4457 4478 } … … 5750 5771 job_mig_check_in(job_t j, name_t servicename, mach_port_t *serviceportp) 5751 5772 { 5752 static pid_t last_warned_pid = 0;5753 5773 struct machservice *ms; 5754 5774 struct ldcred ldc; … … 5769 5789 5770 5790 if ((jo = machservice_job(ms)) != j) { 5791 static pid_t last_warned_pid; 5792 5771 5793 if (last_warned_pid != ldc.pid) { 5772 5794 job_log(j, LOG_NOTICE, "Check-in of Mach service failed. The service \"%s\" is owned by: %s", servicename, jo->label); 5773 5795 last_warned_pid = ldc.pid; 5774 5796 } 5775 return BOOTSTRAP_NOT_PRIVILEGED; 5797 5798 return BOOTSTRAP_NOT_PRIVILEGED; 5776 5799 } 5777 5800 if (machservice_active(ms)) { -
trunk/launchd/src/launchd_runtime.c
r23469 r23470 106 106 static void runtime_log_uncork_pending_drain(void); 107 107 static kern_return_t runtime_log_pack(vm_offset_t *outval, mach_msg_type_number_t *outvalCnt); 108 static void runtime_log_push(void);109 108 110 109 static bool logmsg_add(struct runtime_syslog_attr *attr, int err_num, const char *msg); … … 117 116 }; 118 117 static sigset_t sigign_set; 118 static FILE *ourlogfile; 119 119 120 120 121 mach_port_t … … 1118 1119 } 1119 1120 1120 static FILE *ourlogfile;1121 1122 1121 void 1123 1122 runtime_closelog(void) 1124 1123 { 1124 runtime_log_push(); 1125 1125 1126 if (ourlogfile) { 1126 1127 launchd_assumes(fflush(ourlogfile) == 0); … … 1132 1133 runtime_fsync(int fd) 1133 1134 { 1135 #if 0 1134 1136 if (do_apple_internal_logging()) { 1135 1137 return fcntl(fd, F_FULLFSYNC, NULL); … … 1137 1139 return fsync(fd); 1138 1140 } 1141 #else 1142 return fsync(fd); 1143 #endif 1139 1144 } 1140 1145 … … 1167 1172 runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) 1168 1173 { 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 };1173 1174 int saved_errno = errno; 1174 1175 char newmsg[10000]; 1175 size_t i, j;1176 1176 1177 1177 if (attr->priority == LOG_APPLEONLY) { … … 1187 1187 } 1188 1188 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 1252 1193 1253 1194 bool … … 1366 1307 runtime_log_push(void) 1367 1308 { 1309 static pthread_mutex_t ourlock = PTHREAD_MUTEX_INITIALIZER; 1310 static struct timeval shutdown_start; 1311 struct timeval tvd; 1368 1312 mach_msg_type_number_t outvalCnt; 1313 struct logmsg_s *lm; 1369 1314 vm_offset_t outval; 1370 1315 … … 1372 1317 launchd_assumes(STAILQ_EMPTY(&logmsg_queue)); 1373 1318 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) { 1375 1328 runtime_log_uncork_pending_drain(); 1376 1329 return; 1377 1330 } 1378 1331 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) { 1380 1348 return; 1381 1349 } 1382 1350 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 } 1386 1365 } 1387 1366 … … 1425 1404 runtime_log_drain(mach_port_t srp, vm_offset_t *outval, mach_msg_type_number_t *outvalCnt) 1426 1405 { 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) { 1431 1409 drain_reply_port = srp; 1432 1410 launchd_assumes(launchd_mport_notify_req(drain_reply_port, MACH_NOTIFY_DEAD_NAME) == KERN_SUCCESS); -
trunk/launchd/src/launchd_runtime.h
r23466 r23470 150 150 void runtime_syslog(int pri, const char *message, ...) __attribute__((format(printf, 2, 3))); 151 151 void runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) __attribute__((format(printf, 2, 0))); 152 void runtime_log_push(void); 152 153 153 154

