Changeset 23477
- Timestamp:
- 01/09/08 09:30:02 (11 months ago)
- Location:
- trunk/launchd/src
- Files:
-
- 3 modified
-
launchctl.c (modified) (3 diffs)
-
launchd_core_logic.c (modified) (27 diffs)
-
launchd_runtime.c (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/launchd/src/launchctl.c
r23458 r23477 1476 1476 assumes(load_and_unload_cmd(4, load_launchd_items) == 0); 1477 1477 1478 #ifdef __ppc__ 1478 1479 /* 1479 1480 * 5066316 … … 1484 1485 * will "hopefully" serialize bootstrap. Reasons for doing so include 1485 1486 * pragmatic performance optimizations and attempts to workaround bugs 1486 * in jobs. My current thought is something like what follows.1487 * in jobs. Something like what follows might work: 1487 1488 * 1488 1489 * The BootCache would switch to launchd and add this to the plist: … … 1520 1521 mach_timespec_t w = { 5, 0 }; 1521 1522 IOKitWaitQuiet(kIOMasterPortDefault, &w); 1523 #endif 1522 1524 1523 1525 do_BootCache_magic(BOOTCACHE_TAG); -
trunk/launchd/src/launchd_core_logic.c
r23476 r23477 276 276 277 277 #define jobmgr_assumes(jm, e) \ 278 ( __builtin_expect(!(e), 0) ? jobmgr_log_bug(jm, __rcs_file_version__, __FILE__, __LINE__, #e), false : true)278 (likely(e) ? true : jobmgr_log_bug(jm, __LINE__), false) 279 279 280 280 static jobmgr_t jobmgr_new(jobmgr_t jm, mach_port_t requestorport, mach_port_t transfer_port, bool sflag, const char *name); … … 297 297 static void jobmgr_log(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); 298 298 /* static void jobmgr_log_error(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); */ 299 static void jobmgr_log_bug(jobmgr_t jm, const char *rcs_rev, const char *path, unsigned int line, const char *test);299 static void jobmgr_log_bug(jobmgr_t jm, unsigned int line); 300 300 301 301 #define DO_RUSAGE_SUMMATION 0 … … 374 374 375 375 #define job_assumes(j, e) \ 376 ( __builtin_expect(!(e), 0) ? job_log_bug(j, __rcs_file_version__, __FILE__, __LINE__, #e), false : true)376 (likely(e) ? true : job_log_bug(j, __LINE__), false) 377 377 378 378 static void job_import_keys(launch_data_t obj, const char *key, void *context); … … 416 416 static void job_logv(job_t j, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); 417 417 static void job_log_error(job_t j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); 418 static void job_log_bug(job_t j, const char *rcs_rev, const char *path, unsigned int line, const char *test);418 static void job_log_bug(job_t j, unsigned int line); 419 419 static void job_log_stdouterr2(job_t j, const char *msg, ...); 420 420 static void job_set_exeception_port(job_t j, mach_port_t port); … … 1267 1267 job_t j = jobmgr_import2(root_jobmgr, pload); 1268 1268 1269 if ( j == NULL) {1269 if (unlikely(j == NULL)) { 1270 1270 return NULL; 1271 1271 } … … 1284 1284 1285 1285 for (i = 0; i < c; i++) { 1286 if ( (ja[i] = jobmgr_import2(root_jobmgr, launch_data_array_get_index(pload, i)))) {1286 if (likely(ja[i] = jobmgr_import2(root_jobmgr, launch_data_array_get_index(pload, i)))) { 1287 1287 errno = 0; 1288 1288 } … … 1291 1291 1292 1292 for (i = 0; i < c; i++) { 1293 if (ja[i] == NULL) { 1294 continue; 1295 } 1296 job_dispatch(ja[i], false); 1293 if (likely(ja[i])) { 1294 job_dispatch(ja[i], false); 1295 } 1297 1296 } 1298 1297 … … 1805 1804 } 1806 1805 1807 if ( launch_data_get_type(pload) != LAUNCH_DATA_DICTIONARY) {1806 if (unlikely(launch_data_get_type(pload) != LAUNCH_DATA_DICTIONARY)) { 1808 1807 errno = EINVAL; 1809 1808 return NULL; 1810 1809 } 1811 1810 1812 if ( !(tmp = launch_data_dict_lookup(pload, LAUNCH_JOBKEY_LABEL))) {1811 if (unlikely(!(tmp = launch_data_dict_lookup(pload, LAUNCH_JOBKEY_LABEL)))) { 1813 1812 errno = EINVAL; 1814 1813 return NULL; 1815 1814 } 1816 1815 1817 if ( launch_data_get_type(tmp) != LAUNCH_DATA_STRING) {1816 if (unlikely(launch_data_get_type(tmp) != LAUNCH_DATA_STRING)) { 1818 1817 errno = EINVAL; 1819 1818 return NULL; 1820 1819 } 1821 1820 1822 if ( !(label = launch_data_get_string(tmp))) {1821 if (unlikely(!(label = launch_data_get_string(tmp)))) { 1823 1822 errno = EINVAL; 1824 1823 return NULL; … … 1859 1858 errno = EEXIST; 1860 1859 return NULL; 1861 } else if ( label[0] == '\0' || (strncasecmp(label, "", strlen("com.apple.launchd")) == 0) ||1862 (strtol(label, NULL, 10) != 0)) {1860 } else if (unlikely(label[0] == '\0' || (strncasecmp(label, "", strlen("com.apple.launchd")) == 0) || 1861 (strtol(label, NULL, 10) != 0))) { 1863 1862 jobmgr_log(jm, LOG_ERR, "Somebody attempted to use a reserved prefix for a label: %s", label); 1864 1863 /* the empty string, com.apple.launchd and number prefixes for labels are reserved */ … … 1867 1866 } 1868 1867 1869 if ( (j = job_new(jm, label, prog, argv))) {1868 if (likely(j = job_new(jm, label, prog, argv))) { 1870 1869 launch_data_dict_iterate(pload, job_import_keys, j); 1871 1870 } … … 1960 1959 mib[3] = ldc.pid; 1961 1960 1962 if (jobmgr_assumes(root_jobmgr, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) && jobmgr_assumes(root_jobmgr, len == sizeof(kp))) { 1961 if (jobmgr_assumes(root_jobmgr, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) 1962 && jobmgr_assumes(root_jobmgr, len == sizeof(kp))) { 1963 1963 jobmgr_log(root_jobmgr, LOG_ERR, "%s() was confused by PID %u UID %u EUID %u Mach Port 0x%x: %s", __func__, ldc.pid, ldc.uid, ldc.euid, p, kp.kp_proc.p_comm); 1964 1964 } … … 2075 2075 job_log(j, LOG_DEBUG, "Reaping"); 2076 2076 2077 if ( j->weird_bootstrap) {2077 if (unlikely(j->weird_bootstrap)) { 2078 2078 mach_msg_size_t mxmsgsz = sizeof(union __RequestUnion__job_mig_protocol_vproc_subsystem); 2079 2079 … … 2296 2296 rsz = read(j->log_redirect_fd, buf, BIG_PIPE_SIZE); 2297 2297 2298 if ( rsz == 0) {2298 if (unlikely(rsz == 0)) { 2299 2299 job_log(j, LOG_DEBUG, "Standard out/error pipe closed"); 2300 2300 close_log_redir = true; … … 2313 2313 free(buf); 2314 2314 2315 if ( close_log_redir) {2315 if (unlikely(close_log_redir)) { 2316 2316 job_assumes(j, runtime_close(j->log_redirect_fd) != -1); 2317 2317 j->log_redirect_fd = 0; … … 2384 2384 size_t len = sizeof(kp); 2385 2385 2386 if (job_assumes(j, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) && job_assumes(j, len == sizeof(kp))) { 2386 if (job_assumes(j, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) 2387 && job_assumes(j, len == sizeof(kp))) { 2387 2388 char newlabel[1000]; 2388 2389 … … 3210 3211 3211 3212 void 3212 jobmgr_log_bug(jobmgr_t jm, const char *rcs_rev, const char *path, unsigned int line, const char *test) 3213 { 3213 jobmgr_log_bug(jobmgr_t jm, unsigned int line) 3214 { 3215 static const char *file; 3214 3216 int saved_errno = errno; 3215 const char *file = strrchr(path, '/');3216 3217 char buf[100]; 3217 3218 3218 extract_rcsid_substr( rcs_rev, buf, sizeof(buf));3219 extract_rcsid_substr(__rcs_file_version__, buf, sizeof(buf)); 3219 3220 3220 3221 if (!file) { 3221 file = path; 3222 file = strrchr(__FILE__, '/'); 3223 if (!file) { 3224 file = __FILE__; 3225 } else { 3226 file += 1; 3227 } 3228 } 3229 3230 /* the only time 'jm' should not be set is if setting up the first bootstrap fails for some reason */ 3231 if (likely(jm)) { 3232 jobmgr_log(jm, LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); 3222 3233 } else { 3223 file += 1; 3224 } 3225 3226 jobmgr_log(jm, LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); 3227 } 3228 3229 void 3230 job_log_bug(job_t j, const char *rcs_rev, const char *path, unsigned int line, const char *test) 3231 { 3234 runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); 3235 } 3236 } 3237 3238 void 3239 job_log_bug(job_t j, unsigned int line) 3240 { 3241 static const char *file; 3232 3242 int saved_errno = errno; 3233 const char *file = strrchr(path, '/');3234 3243 char buf[100]; 3235 3244 3236 extract_rcsid_substr( rcs_rev, buf, sizeof(buf));3245 extract_rcsid_substr(__rcs_file_version__, buf, sizeof(buf)); 3237 3246 3238 3247 if (!file) { 3239 file = path; 3248 file = strrchr(__FILE__, '/'); 3249 if (!file) { 3250 file = __FILE__; 3251 } else { 3252 file += 1; 3253 } 3254 } 3255 3256 /* I cannot think of any reason why 'j' should ever be NULL, nor have I ever seen the case in the wild */ 3257 if (likely(j)) { 3258 job_log(j, LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); 3240 3259 } else { 3241 file += 1; 3242 } 3243 3244 job_log(j, LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); 3260 runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); 3261 } 3245 3262 } 3246 3263 … … 3418 3435 si->fd = -1; 3419 3436 } 3420 } while ( (si->fd == -1) && (saved_errno == ENOENT));3437 } while (unlikely((si->fd == -1) && (saved_errno == ENOENT))); 3421 3438 3422 3439 if (saved_errno == ENOTSUP) { … … 3502 3519 int64_t val; 3503 3520 3504 if ( LAUNCH_DATA_INTEGER != launch_data_get_type(obj)) {3521 if (unlikely(LAUNCH_DATA_INTEGER != launch_data_get_type(obj))) { 3505 3522 /* hack to let caller know something went wrong */ 3506 3523 tmptm->tm_sec = -1; … … 3541 3558 } 3542 3559 3543 if ( LAUNCH_DATA_DICTIONARY != launch_data_get_type(obj)) {3560 if (unlikely(LAUNCH_DATA_DICTIONARY != launch_data_get_type(obj))) { 3544 3561 return false; 3545 3562 } … … 3547 3564 launch_data_dict_iterate(obj, calendarinterval_new_from_obj_dict_walk, &tmptm); 3548 3565 3549 if ( tmptm.tm_sec == -1) {3566 if (unlikely(tmptm.tm_sec == -1)) { 3550 3567 return false; 3551 3568 } … … 3592 3609 time_t now = time(NULL); 3593 3610 3594 if ( ci && (ci->when_next < now)) {3611 if (unlikely(ci && (ci->when_next < now))) { 3595 3612 jobmgr_assumes(root_jobmgr, raise(SIGUSR1) != -1); 3596 3613 } … … 3682 3699 unsigned int i, buf_off = 0; 3683 3700 3684 if ( sg->junkfds) {3701 if (unlikely(sg->junkfds)) { 3685 3702 return; 3686 3703 } … … 3893 3910 bool good_exit = (WIFEXITED(j->last_exit_status) && WEXITSTATUS(j->last_exit_status) == 0); 3894 3911 3912 #ifdef __ppc__ 3895 3913 /* 3896 3914 * 5066316 … … 3902 3920 return false; 3903 3921 } 3922 #else 3923 if (j->mgr->global_on_demand_cnt > 0) { 3924 return false; 3925 } 3926 #endif 3904 3927 3905 3928 if (j->start_pending) { … … 4072 4095 machservice_new(job_t j, const char *name, mach_port_t *serviceport, bool pid_local) 4073 4096 { 4074 struct machservice *ms ;4075 4076 if ( (ms = calloc(1, sizeof(struct machservice) + strlen(name) + 1)) == NULL) {4097 struct machservice *ms = calloc(1, sizeof(struct machservice) + strlen(name) + 1); 4098 4099 if (!job_assumes(j, ms != NULL)) { 4077 4100 return NULL; 4078 4101 } … … 4412 4435 4413 4436 jmr = calloc(1, sizeof(struct jobmgr_s) + (name ? (strlen(name) + 1) : 128)); 4414 4415 if ( jmr == NULL) {4437 4438 if (!jobmgr_assumes(jm, jmr != NULL)) { 4416 4439 return NULL; 4417 4440 } … … 6678 6701 } 6679 6702 6680 if ((msp = calloc(1, sizeof(struct mspolicy) + strlen(name) + 1)) == NULL) { 6703 msp = calloc(1, sizeof(struct mspolicy) + strlen(name) + 1); 6704 6705 if (!job_assumes(j, msp != NULL)) { 6681 6706 return false; 6682 6707 } -
trunk/launchd/src/launchd_runtime.c
r23472 r23477 187 187 for (;;) { 188 188 kr = mach_msg(&dummy.header, MACH_RCV_MSG|MACH_RCV_LARGE, 0, 0, demand_port_set, 0, MACH_PORT_NULL); 189 if ( kr == MACH_RCV_PORT_CHANGED) {189 if (unlikely(kr == MACH_RCV_PORT_CHANGED)) { 190 190 break; 191 191 } else if (!launchd_assumes(kr == MACH_RCV_TOO_LARGE)) { … … 358 358 unsigned int fflags = kev->fflags; 359 359 360 if ( !(LOG_MASK(level) & internal_mask_pri)) {360 if (likely(!(LOG_MASK(level) & internal_mask_pri))) { 361 361 return; 362 362 } … … 593 593 bulk_kev = kev; 594 594 595 launchd_assumes((bulk_kev_cnt = kevent(fd, NULL, 0, kev, BULK_KEV_MAX, &ts)) != -1); 596 597 if (bulk_kev_cnt > 0) { 595 if (launchd_assumes((bulk_kev_cnt = kevent(fd, NULL, 0, kev, BULK_KEV_MAX, &ts)) != -1)) { 598 596 #if 0 599 597 Dl_info dli; … … 632 630 633 631 for (;;) { 634 if ( req) {632 if (likely(req)) { 635 633 launchd_assumes(vm_deallocate(mach_task_self(), (vm_address_t)req, mz) == KERN_SUCCESS); 636 634 req = NULL; 637 635 } 638 if ( resp) {636 if (likely(resp)) { 639 637 launchd_assumes(vm_deallocate(mach_task_self(), (vm_address_t)resp, mz) == KERN_SUCCESS); 640 638 resp = NULL; … … 677 675 /* Always make sure the send count is zero, in case a receive right is reused */ 678 676 errno = mach_port_set_mscount(mach_task_self(), name, 0); 679 if ( errno != KERN_SUCCESS) {677 if (unlikely(errno != KERN_SUCCESS)) { 680 678 return errno; 681 679 } … … 685 683 MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous); 686 684 687 if ( errno == 0&& previous != MACH_PORT_NULL) {685 if (likely(errno == 0) && previous != MACH_PORT_NULL) { 688 686 launchd_assumes(launchd_mport_deallocate(previous) == KERN_SUCCESS); 689 687 } … … 750 748 msg_size = round_page(msg_size + MAX_TRAILER_SIZE); 751 749 752 if ( needed_table_sz > mig_cb_table_sz) {750 if (unlikely(needed_table_sz > mig_cb_table_sz)) { 753 751 needed_table_sz *= 2; /* Let's try and avoid realloc'ing for a while */ 754 752 mig_callback *new_table = malloc(needed_table_sz); … … 758 756 } 759 757 760 if ( mig_cb_table) {758 if (likely(mig_cb_table)) { 761 759 memcpy(new_table, mig_cb_table, mig_cb_table_sz); 762 760 free(mig_cb_table); … … 961 959 trailer_size = tp->msgh_trailer_size - (mach_msg_size_t)(sizeof(mach_msg_trailer_type_t) - sizeof(mach_msg_trailer_size_t)); 962 960 963 if ( trailer_size < (mach_msg_size_t)sizeof(audit_token_t)) {961 if (unlikely(trailer_size < (mach_msg_size_t)sizeof(audit_token_t))) { 964 962 au_tok = NULL; 965 963 return; … … 972 970 runtime_get_caller_creds(struct ldcred *ldc) 973 971 { 974 if ( !au_tok) {972 if (unlikely(!au_tok)) { 975 973 return false; 976 974 } … … 1000 998 to = MACH_MSG_TIMEOUT_NONE; 1001 999 1002 if ( msg_size != max_msg_size) {1000 if (unlikely(msg_size != max_msg_size)) { 1003 1001 /* The buffer isn't big enougth to receive messages anymore... */ 1004 1002 tmp_options &= ~MACH_RCV_MSG; … … 1024 1022 tmp_options = options; 1025 1023 1026 if ( mr == MACH_SEND_INVALID_DEST || mr == MACH_SEND_TIMED_OUT) {1024 if (unlikely(mr == MACH_SEND_INVALID_DEST || mr == MACH_SEND_TIMED_OUT)) { 1027 1025 /* We need to clean up and start over. */ 1028 1026 if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) { … … 1030 1028 } 1031 1029 continue; 1032 } else if ( mr == MACH_RCV_TIMED_OUT) {1030 } else if (unlikely(mr == MACH_RCV_TIMED_OUT)) { 1033 1031 if (to != MACH_MSG_TIMEOUT_NONE) { 1034 1032 if (runtime_busy_cnt == 0) { … … 1047 1045 bufReply = bufTemp; 1048 1046 1049 if ( !(tmp_options & MACH_RCV_MSG)) {1047 if (unlikely(!(tmp_options & MACH_RCV_MSG))) { 1050 1048 continue; 1051 1049 } … … 1070 1068 */ 1071 1069 static int no_hang_fd = -1; 1072 if ( no_hang_fd == -1) {1070 if (unlikely(no_hang_fd == -1)) { 1073 1071 no_hang_fd = _fd(open("/dev/autofs_nowait", 0)); 1074 1072 } … … 1208 1206 lm_sz = ROUND_TO_64BIT_WORD_SIZE(lm_sz); 1209 1207 1210 if ( !(lm = calloc(1, lm_sz))) {1208 if (unlikely((lm = calloc(1, lm_sz)) == NULL)) { 1211 1209 return false; 1212 1210 } … … 1256 1254 mig_allocate(outval, *outvalCnt); 1257 1255 1258 if ( *outval == 0) {1256 if (unlikely(*outval == 0)) { 1259 1257 return 1; 1260 1258 } … … 1300 1298 drain_reply_port = MACH_PORT_NULL; 1301 1299 1302 if ( (errno = job_mig_log_drain_reply(tmp_port, 0, outval, outvalCnt))) {1300 if (unlikely(errno = job_mig_log_drain_reply(tmp_port, 0, outval, outvalCnt))) { 1303 1301 launchd_assumes(errno == MACH_SEND_INVALID_DEST); 1304 1302 launchd_assumes(launchd_mport_deallocate(tmp_port) == KERN_SUCCESS); … … 1307 1305 mig_deallocate(outval, outvalCnt); 1308 1306 } 1307 1308 #if 0 1309 void 1310 runtime_kernel_trace(void *code, void *a, void *b, void *c, void *d) 1311 { 1312 /* Request codes from Joe S. */ 1313 syscall(180 , code, a, b, c, d); 1314 } 1315 #endif 1309 1316 1310 1317 void … … 1333 1340 } 1334 1341 1335 if ( shutdown_start == 0) {1342 if (unlikely(shutdown_start == 0)) { 1336 1343 shutdown_start = runtime_get_wall_time(); 1337 1344 launchd_log_vm_stats(); … … 1341 1348 pthread_mutex_lock(&ourlock); 1342 1349 1343 if ( ourlogfile == NULL) {1350 if (unlikely(ourlogfile == NULL)) { 1344 1351 rename("/var/log/launchd-shutdown.log", "/var/log/launchd-shutdown.log.1"); 1345 1352 ourlogfile = fopen("/var/log/launchd-shutdown.log", "a"); … … 1348 1355 pthread_mutex_unlock(&ourlock); 1349 1356 1350 if ( !ourlogfile) {1357 if (unlikely(!ourlogfile)) { 1351 1358 return; 1352 1359 } … … 1542 1549 struct stat sb; 1543 1550 1544 if ( apple_internal_logging == 1) {1551 if (unlikely(apple_internal_logging == 1)) { 1545 1552 apple_internal_logging = stat("/AppleInternal", &sb); 1546 1553 } … … 1600 1607 tbi_float_val /= tbi.denom; 1601 1608 } 1602

