Projects
Browse Source     Search     Timeline     Wiki

Changeset 23477

Show
Ignore:
Timestamp:
01/09/08 09:30:02 (11 months ago)
Author:
zarzycki@…
Message:

Misc branch hints.

Location:
trunk/launchd/src
Files:
3 modified

Legend:

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

    r23458 r23477  
    14761476        assumes(load_and_unload_cmd(4, load_launchd_items) == 0); 
    14771477 
     1478#ifdef __ppc__ 
    14781479        /* 
    14791480         * 5066316 
     
    14841485         * will "hopefully" serialize bootstrap. Reasons for doing so include 
    14851486         * 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: 
    14871488         * 
    14881489         * The BootCache would switch to launchd and add this to the plist: 
     
    15201521        mach_timespec_t w = { 5, 0 }; 
    15211522        IOKitWaitQuiet(kIOMasterPortDefault, &w); 
     1523#endif 
    15221524 
    15231525        do_BootCache_magic(BOOTCACHE_TAG); 
  • trunk/launchd/src/launchd_core_logic.c

    r23476 r23477  
    276276 
    277277#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) 
    279279 
    280280static jobmgr_t jobmgr_new(jobmgr_t jm, mach_port_t requestorport, mach_port_t transfer_port, bool sflag, const char *name); 
     
    297297static void jobmgr_log(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); 
    298298/* 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); 
     299static void jobmgr_log_bug(jobmgr_t jm, unsigned int line); 
    300300 
    301301#define DO_RUSAGE_SUMMATION 0 
     
    374374 
    375375#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) 
    377377 
    378378static void job_import_keys(launch_data_t obj, const char *key, void *context); 
     
    416416static void job_logv(job_t j, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); 
    417417static 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); 
     418static void job_log_bug(job_t j, unsigned int line); 
    419419static void job_log_stdouterr2(job_t j, const char *msg, ...); 
    420420static void job_set_exeception_port(job_t j, mach_port_t port); 
     
    12671267        job_t j = jobmgr_import2(root_jobmgr, pload); 
    12681268 
    1269         if (j == NULL) { 
     1269        if (unlikely(j == NULL)) { 
    12701270                return NULL; 
    12711271        } 
     
    12841284 
    12851285        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)))) { 
    12871287                        errno = 0; 
    12881288                } 
     
    12911291 
    12921292        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                } 
    12971296        } 
    12981297 
     
    18051804        } 
    18061805 
    1807         if (launch_data_get_type(pload) != LAUNCH_DATA_DICTIONARY) { 
     1806        if (unlikely(launch_data_get_type(pload) != LAUNCH_DATA_DICTIONARY)) { 
    18081807                errno = EINVAL; 
    18091808                return NULL; 
    18101809        } 
    18111810 
    1812         if (!(tmp = launch_data_dict_lookup(pload, LAUNCH_JOBKEY_LABEL))) { 
     1811        if (unlikely(!(tmp = launch_data_dict_lookup(pload, LAUNCH_JOBKEY_LABEL)))) { 
    18131812                errno = EINVAL; 
    18141813                return NULL; 
    18151814        } 
    18161815 
    1817         if (launch_data_get_type(tmp) != LAUNCH_DATA_STRING) { 
     1816        if (unlikely(launch_data_get_type(tmp) != LAUNCH_DATA_STRING)) { 
    18181817                errno = EINVAL; 
    18191818                return NULL; 
    18201819        } 
    18211820 
    1822         if (!(label = launch_data_get_string(tmp))) { 
     1821        if (unlikely(!(label = launch_data_get_string(tmp)))) { 
    18231822                errno = EINVAL; 
    18241823                return NULL; 
     
    18591858                errno = EEXIST; 
    18601859                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))) { 
    18631862                jobmgr_log(jm, LOG_ERR, "Somebody attempted to use a reserved prefix for a label: %s", label); 
    18641863                /* the empty string, com.apple.launchd and number prefixes for labels are reserved */ 
     
    18671866        } 
    18681867 
    1869         if ((j = job_new(jm, label, prog, argv))) { 
     1868        if (likely(j = job_new(jm, label, prog, argv))) { 
    18701869                launch_data_dict_iterate(pload, job_import_keys, j); 
    18711870        } 
     
    19601959                mib[3] = ldc.pid; 
    19611960 
    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))) { 
    19631963                        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); 
    19641964                } 
     
    20752075        job_log(j, LOG_DEBUG, "Reaping"); 
    20762076 
    2077         if (j->weird_bootstrap) { 
     2077        if (unlikely(j->weird_bootstrap)) { 
    20782078                mach_msg_size_t mxmsgsz = sizeof(union __RequestUnion__job_mig_protocol_vproc_subsystem); 
    20792079 
     
    22962296        rsz = read(j->log_redirect_fd, buf, BIG_PIPE_SIZE); 
    22972297 
    2298         if (rsz == 0) { 
     2298        if (unlikely(rsz == 0)) { 
    22992299                job_log(j, LOG_DEBUG, "Standard out/error pipe closed"); 
    23002300                close_log_redir = true; 
     
    23132313        free(buf); 
    23142314 
    2315         if (close_log_redir) { 
     2315        if (unlikely(close_log_redir)) { 
    23162316                job_assumes(j, runtime_close(j->log_redirect_fd) != -1); 
    23172317                j->log_redirect_fd = 0; 
     
    23842384                        size_t len = sizeof(kp); 
    23852385 
    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))) { 
    23872388                                char newlabel[1000]; 
    23882389 
     
    32103211 
    32113212void 
    3212 jobmgr_log_bug(jobmgr_t jm, const char *rcs_rev, const char *path, unsigned int line, const char *test) 
    3213 { 
     3213jobmgr_log_bug(jobmgr_t jm, unsigned int line) 
     3214{ 
     3215        static const char *file; 
    32143216        int saved_errno = errno; 
    3215         const char *file = strrchr(path, '/'); 
    32163217        char buf[100]; 
    32173218 
    3218         extract_rcsid_substr(rcs_rev, buf, sizeof(buf)); 
     3219        extract_rcsid_substr(__rcs_file_version__, buf, sizeof(buf)); 
    32193220 
    32203221        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); 
    32223233        } 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 
     3238void 
     3239job_log_bug(job_t j, unsigned int line) 
     3240{ 
     3241        static const char *file; 
    32323242        int saved_errno = errno; 
    3233         const char *file = strrchr(path, '/'); 
    32343243        char buf[100]; 
    32353244 
    3236         extract_rcsid_substr(rcs_rev, buf, sizeof(buf)); 
     3245        extract_rcsid_substr(__rcs_file_version__, buf, sizeof(buf)); 
    32373246 
    32383247        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); 
    32403259        } 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        } 
    32453262} 
    32463263 
     
    34183435                        si->fd = -1; 
    34193436                } 
    3420         } while ((si->fd == -1) && (saved_errno == ENOENT)); 
     3437        } while (unlikely((si->fd == -1) && (saved_errno == ENOENT))); 
    34213438 
    34223439        if (saved_errno == ENOTSUP) { 
     
    35023519        int64_t val; 
    35033520 
    3504         if (LAUNCH_DATA_INTEGER != launch_data_get_type(obj)) { 
     3521        if (unlikely(LAUNCH_DATA_INTEGER != launch_data_get_type(obj))) { 
    35053522                /* hack to let caller know something went wrong */ 
    35063523                tmptm->tm_sec = -1; 
     
    35413558        } 
    35423559 
    3543         if (LAUNCH_DATA_DICTIONARY != launch_data_get_type(obj)) { 
     3560        if (unlikely(LAUNCH_DATA_DICTIONARY != launch_data_get_type(obj))) { 
    35443561                return false; 
    35453562        } 
     
    35473564        launch_data_dict_iterate(obj, calendarinterval_new_from_obj_dict_walk, &tmptm); 
    35483565 
    3549         if (tmptm.tm_sec == -1) { 
     3566        if (unlikely(tmptm.tm_sec == -1)) { 
    35503567                return false; 
    35513568        } 
     
    35923609        time_t now = time(NULL); 
    35933610 
    3594         if (ci && (ci->when_next < now)) { 
     3611        if (unlikely(ci && (ci->when_next < now))) { 
    35953612                jobmgr_assumes(root_jobmgr, raise(SIGUSR1) != -1); 
    35963613        } 
     
    36823699        unsigned int i, buf_off = 0; 
    36833700 
    3684         if (sg->junkfds) { 
     3701        if (unlikely(sg->junkfds)) { 
    36853702                return; 
    36863703        } 
     
    38933910        bool good_exit = (WIFEXITED(j->last_exit_status) && WEXITSTATUS(j->last_exit_status) == 0); 
    38943911 
     3912#ifdef __ppc__ 
    38953913        /* 
    38963914         * 5066316 
     
    39023920                return false; 
    39033921        } 
     3922#else 
     3923        if (j->mgr->global_on_demand_cnt > 0) { 
     3924                return false; 
     3925        } 
     3926#endif 
    39043927 
    39053928        if (j->start_pending) { 
     
    40724095machservice_new(job_t j, const char *name, mach_port_t *serviceport, bool pid_local) 
    40734096{ 
    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)) { 
    40774100                return NULL; 
    40784101        } 
     
    44124435 
    44134436        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)) { 
    44164439                return NULL; 
    44174440        } 
     
    66786701        } 
    66796702 
    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)) { 
    66816706                return false; 
    66826707        } 
  • trunk/launchd/src/launchd_runtime.c

    r23472 r23477  
    187187        for (;;) { 
    188188                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)) { 
    190190                        break; 
    191191                } else if (!launchd_assumes(kr == MACH_RCV_TOO_LARGE)) { 
     
    358358        unsigned int fflags = kev->fflags; 
    359359 
    360         if (!(LOG_MASK(level) & internal_mask_pri)) { 
     360        if (likely(!(LOG_MASK(level) & internal_mask_pri))) { 
    361361                return; 
    362362        } 
     
    593593        bulk_kev = kev; 
    594594 
    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)) { 
    598596#if 0 
    599597                Dl_info dli; 
     
    632630 
    633631        for (;;) { 
    634                 if (req) { 
     632                if (likely(req)) { 
    635633                        launchd_assumes(vm_deallocate(mach_task_self(), (vm_address_t)req, mz) == KERN_SUCCESS); 
    636634                        req = NULL; 
    637635                } 
    638                 if (resp) { 
     636                if (likely(resp)) { 
    639637                        launchd_assumes(vm_deallocate(mach_task_self(), (vm_address_t)resp, mz) == KERN_SUCCESS); 
    640638                        resp = NULL; 
     
    677675                /* Always make sure the send count is zero, in case a receive right is reused */ 
    678676                errno = mach_port_set_mscount(mach_task_self(), name, 0); 
    679                 if (errno != KERN_SUCCESS) { 
     677                if (unlikely(errno != KERN_SUCCESS)) { 
    680678                        return errno; 
    681679                } 
     
    685683                        MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous); 
    686684 
    687         if (errno == 0 && previous != MACH_PORT_NULL) { 
     685        if (likely(errno == 0) && previous != MACH_PORT_NULL) { 
    688686                launchd_assumes(launchd_mport_deallocate(previous) == KERN_SUCCESS); 
    689687        } 
     
    750748        msg_size = round_page(msg_size + MAX_TRAILER_SIZE); 
    751749 
    752         if (needed_table_sz > mig_cb_table_sz) { 
     750        if (unlikely(needed_table_sz > mig_cb_table_sz)) { 
    753751                needed_table_sz *= 2; /* Let's try and avoid realloc'ing for a while */ 
    754752                mig_callback *new_table = malloc(needed_table_sz); 
     
    758756                } 
    759757 
    760                 if (mig_cb_table) { 
     758                if (likely(mig_cb_table)) { 
    761759                        memcpy(new_table, mig_cb_table, mig_cb_table_sz); 
    762760                        free(mig_cb_table); 
     
    961959        trailer_size = tp->msgh_trailer_size - (mach_msg_size_t)(sizeof(mach_msg_trailer_type_t) - sizeof(mach_msg_trailer_size_t)); 
    962960 
    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))) { 
    964962                au_tok = NULL; 
    965963                return; 
     
    972970runtime_get_caller_creds(struct ldcred *ldc) 
    973971{ 
    974         if (!au_tok) { 
     972        if (unlikely(!au_tok)) { 
    975973                return false; 
    976974        } 
     
    1000998                to = MACH_MSG_TIMEOUT_NONE; 
    1001999 
    1002                 if (msg_size != max_msg_size) { 
     1000                if (unlikely(msg_size != max_msg_size)) { 
    10031001                        /* The buffer isn't big enougth to receive messages anymore... */ 
    10041002                        tmp_options &= ~MACH_RCV_MSG; 
     
    10241022                tmp_options = options; 
    10251023 
    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)) { 
    10271025                        /* We need to clean up and start over. */ 
    10281026                        if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) { 
     
    10301028                        } 
    10311029                        continue; 
    1032                 } else if (mr == MACH_RCV_TIMED_OUT) { 
     1030                } else if (unlikely(mr == MACH_RCV_TIMED_OUT)) { 
    10331031                        if (to != MACH_MSG_TIMEOUT_NONE) { 
    10341032                                if (runtime_busy_cnt == 0) { 
     
    10471045                bufReply = bufTemp; 
    10481046 
    1049                 if (!(tmp_options & MACH_RCV_MSG)) { 
     1047                if (unlikely(!(tmp_options & MACH_RCV_MSG))) { 
    10501048                        continue; 
    10511049                } 
     
    10701068                 */ 
    10711069                static int no_hang_fd = -1; 
    1072                 if (no_hang_fd == -1) { 
     1070                if (unlikely(no_hang_fd == -1)) { 
    10731071                        no_hang_fd = _fd(open("/dev/autofs_nowait", 0)); 
    10741072                } 
     
    12081206        lm_sz = ROUND_TO_64BIT_WORD_SIZE(lm_sz); 
    12091207 
    1210         if (!(lm = calloc(1, lm_sz))) { 
     1208        if (unlikely((lm = calloc(1, lm_sz)) == NULL)) { 
    12111209                return false; 
    12121210        } 
     
    12561254        mig_allocate(outval, *outvalCnt); 
    12571255 
    1258         if (*outval == 0) { 
     1256        if (unlikely(*outval == 0)) { 
    12591257                return 1; 
    12601258        } 
     
    13001298        drain_reply_port = MACH_PORT_NULL; 
    13011299 
    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))) { 
    13031301                launchd_assumes(errno == MACH_SEND_INVALID_DEST); 
    13041302                launchd_assumes(launchd_mport_deallocate(tmp_port) == KERN_SUCCESS); 
     
    13071305        mig_deallocate(outval, outvalCnt); 
    13081306} 
     1307 
     1308#if 0 
     1309void 
     1310runtime_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 
    13091316 
    13101317void 
     
    13331340        } 
    13341341 
    1335         if (shutdown_start == 0) { 
     1342        if (unlikely(shutdown_start == 0)) { 
    13361343                shutdown_start = runtime_get_wall_time(); 
    13371344                launchd_log_vm_stats(); 
     
    13411348        pthread_mutex_lock(&ourlock); 
    13421349 
    1343         if (ourlogfile == NULL) { 
     1350        if (unlikely(ourlogfile == NULL)) { 
    13441351                rename("/var/log/launchd-shutdown.log", "/var/log/launchd-shutdown.log.1"); 
    13451352                ourlogfile = fopen("/var/log/launchd-shutdown.log", "a"); 
     
    13481355        pthread_mutex_unlock(&ourlock); 
    13491356 
    1350         if (!ourlogfile) { 
     1357        if (unlikely(!ourlogfile)) { 
    13511358                return; 
    13521359        } 
     
    15421549        struct stat sb; 
    15431550 
    1544         if (apple_internal_logging == 1) { 
     1551        if (unlikely(apple_internal_logging == 1)) { 
    15451552                apple_internal_logging = stat("/AppleInternal", &sb); 
    15461553        } 
     
    16001607        tbi_float_val /= tbi.denom; 
    16011608} 
    1602