From source_changes at macosforge.org Wed Jan 9 09:30:05 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Wed Jan 9 09:30:07 2008 Subject: [launchd-changes] [23477] trunk/launchd/src Message-ID: <20080109173005.0A0C5D8FEB08@lists.macosforge.org> Revision: 23477 http://trac.macosforge.org/projects/launchd/changeset/23477 Author: zarzycki@apple.com Date: 2008-01-09 09:30:02 -0800 (Wed, 09 Jan 2008) Log Message: ----------- Misc branch hints. Modified Paths: -------------- trunk/launchd/src/launchctl.c trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_runtime.c Modified: trunk/launchd/src/launchctl.c =================================================================== --- trunk/launchd/src/launchctl.c 2007-12-21 21:53:37 UTC (rev 23476) +++ trunk/launchd/src/launchctl.c 2008-01-09 17:30:02 UTC (rev 23477) @@ -1475,6 +1475,7 @@ assumes(load_and_unload_cmd(4, load_launchd_items) == 0); +#ifdef __ppc__ /* * 5066316 * @@ -1483,7 +1484,7 @@ * I want a plist defined knob for jobs to give advisory hints that * will "hopefully" serialize bootstrap. Reasons for doing so include * pragmatic performance optimizations and attempts to workaround bugs - * in jobs. My current thought is something like what follows. + * in jobs. Something like what follows might work: * * The BootCache would switch to launchd and add this to the plist: * @@ -1519,6 +1520,7 @@ */ mach_timespec_t w = { 5, 0 }; IOKitWaitQuiet(kIOMasterPortDefault, &w); +#endif do_BootCache_magic(BOOTCACHE_TAG); Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2007-12-21 21:53:37 UTC (rev 23476) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-09 17:30:02 UTC (rev 23477) @@ -275,7 +275,7 @@ }; #define jobmgr_assumes(jm, e) \ - (__builtin_expect(!(e), 0) ? jobmgr_log_bug(jm, __rcs_file_version__, __FILE__, __LINE__, #e), false : true) + (likely(e) ? true : jobmgr_log_bug(jm, __LINE__), false) static jobmgr_t jobmgr_new(jobmgr_t jm, mach_port_t requestorport, mach_port_t transfer_port, bool sflag, const char *name); static job_t jobmgr_import2(jobmgr_t jm, launch_data_t pload); @@ -296,7 +296,7 @@ static void jobmgr_logv(jobmgr_t jm, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); static void jobmgr_log(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); /* static void jobmgr_log_error(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); */ -static void jobmgr_log_bug(jobmgr_t jm, const char *rcs_rev, const char *path, unsigned int line, const char *test); +static void jobmgr_log_bug(jobmgr_t jm, unsigned int line); #define DO_RUSAGE_SUMMATION 0 @@ -373,7 +373,7 @@ #define job_assumes(j, e) \ - (__builtin_expect(!(e), 0) ? job_log_bug(j, __rcs_file_version__, __FILE__, __LINE__, #e), false : true) + (likely(e) ? true : job_log_bug(j, __LINE__), false) static void job_import_keys(launch_data_t obj, const char *key, void *context); static void job_import_bool(job_t j, const char *key, bool value); @@ -415,7 +415,7 @@ static void job_log_stdouterr(job_t j); static void job_logv(job_t j, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); static void job_log_error(job_t j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); -static void job_log_bug(job_t j, const char *rcs_rev, const char *path, unsigned int line, const char *test); +static void job_log_bug(job_t j, unsigned int line); static void job_log_stdouterr2(job_t j, const char *msg, ...); static void job_set_exeception_port(job_t j, mach_port_t port); static kern_return_t job_handle_mpm_wait(job_t j, mach_port_t srp, int *waitstatus); @@ -1266,7 +1266,7 @@ { job_t j = jobmgr_import2(root_jobmgr, pload); - if (j == NULL) { + if (unlikely(j == NULL)) { return NULL; } @@ -1283,17 +1283,16 @@ ja = alloca(c * sizeof(job_t )); for (i = 0; i < c; i++) { - if ((ja[i] = jobmgr_import2(root_jobmgr, launch_data_array_get_index(pload, i)))) { + if (likely(ja[i] = jobmgr_import2(root_jobmgr, launch_data_array_get_index(pload, i)))) { errno = 0; } launch_data_array_set_index(resp, launch_data_new_errno(errno), i); } for (i = 0; i < c; i++) { - if (ja[i] == NULL) { - continue; + if (likely(ja[i])) { + job_dispatch(ja[i], false); } - job_dispatch(ja[i], false); } return resp; @@ -1804,22 +1803,22 @@ return NULL; } - if (launch_data_get_type(pload) != LAUNCH_DATA_DICTIONARY) { + if (unlikely(launch_data_get_type(pload) != LAUNCH_DATA_DICTIONARY)) { errno = EINVAL; return NULL; } - if (!(tmp = launch_data_dict_lookup(pload, LAUNCH_JOBKEY_LABEL))) { + if (unlikely(!(tmp = launch_data_dict_lookup(pload, LAUNCH_JOBKEY_LABEL)))) { errno = EINVAL; return NULL; } - if (launch_data_get_type(tmp) != LAUNCH_DATA_STRING) { + if (unlikely(launch_data_get_type(tmp) != LAUNCH_DATA_STRING)) { errno = EINVAL; return NULL; } - if (!(label = launch_data_get_string(tmp))) { + if (unlikely(!(label = launch_data_get_string(tmp)))) { errno = EINVAL; return NULL; } @@ -1858,15 +1857,15 @@ if (unlikely((j = job_find(label)) != NULL)) { errno = EEXIST; return NULL; - } else if (label[0] == '\0' || (strncasecmp(label, "", strlen("com.apple.launchd")) == 0) || - (strtol(label, NULL, 10) != 0)) { + } else if (unlikely(label[0] == '\0' || (strncasecmp(label, "", strlen("com.apple.launchd")) == 0) || + (strtol(label, NULL, 10) != 0))) { jobmgr_log(jm, LOG_ERR, "Somebody attempted to use a reserved prefix for a label: %s", label); /* the empty string, com.apple.launchd and number prefixes for labels are reserved */ errno = EINVAL; return NULL; } - if ((j = job_new(jm, label, prog, argv))) { + if (likely(j = job_new(jm, label, prog, argv))) { launch_data_dict_iterate(pload, job_import_keys, j); } @@ -1959,7 +1958,8 @@ mib[3] = ldc.pid; - if (jobmgr_assumes(root_jobmgr, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) && jobmgr_assumes(root_jobmgr, len == sizeof(kp))) { + if (jobmgr_assumes(root_jobmgr, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) + && jobmgr_assumes(root_jobmgr, len == sizeof(kp))) { 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); } } @@ -2074,7 +2074,7 @@ job_log(j, LOG_DEBUG, "Reaping"); - if (j->weird_bootstrap) { + if (unlikely(j->weird_bootstrap)) { mach_msg_size_t mxmsgsz = sizeof(union __RequestUnion__job_mig_protocol_vproc_subsystem); if (job_mig_protocol_vproc_subsystem.maxsize > mxmsgsz) { @@ -2295,7 +2295,7 @@ rsz = read(j->log_redirect_fd, buf, BIG_PIPE_SIZE); - if (rsz == 0) { + if (unlikely(rsz == 0)) { job_log(j, LOG_DEBUG, "Standard out/error pipe closed"); close_log_redir = true; } else if (!job_assumes(j, rsz != -1)) { @@ -2312,7 +2312,7 @@ free(buf); - if (close_log_redir) { + if (unlikely(close_log_redir)) { job_assumes(j, runtime_close(j->log_redirect_fd) != -1); j->log_redirect_fd = 0; job_dispatch(j, false); @@ -2383,7 +2383,8 @@ struct kinfo_proc kp; size_t len = sizeof(kp); - if (job_assumes(j, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) && job_assumes(j, len == sizeof(kp))) { + if (job_assumes(j, sysctl(mib, 4, &kp, &len, NULL, 0) != -1) + && job_assumes(j, len == sizeof(kp))) { char newlabel[1000]; snprintf(newlabel, sizeof(newlabel), "%p.%s", j, kp.kp_proc.p_comm); @@ -3209,39 +3210,55 @@ } void -jobmgr_log_bug(jobmgr_t jm, const char *rcs_rev, const char *path, unsigned int line, const char *test) +jobmgr_log_bug(jobmgr_t jm, unsigned int line) { + static const char *file; int saved_errno = errno; - const char *file = strrchr(path, '/'); char buf[100]; - extract_rcsid_substr(rcs_rev, buf, sizeof(buf)); + extract_rcsid_substr(__rcs_file_version__, buf, sizeof(buf)); if (!file) { - file = path; + file = strrchr(__FILE__, '/'); + if (!file) { + file = __FILE__; + } else { + file += 1; + } + } + + /* the only time 'jm' should not be set is if setting up the first bootstrap fails for some reason */ + if (likely(jm)) { + jobmgr_log(jm, LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } else { - file += 1; + runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } - - jobmgr_log(jm, LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); } void -job_log_bug(job_t j, const char *rcs_rev, const char *path, unsigned int line, const char *test) +job_log_bug(job_t j, unsigned int line) { + static const char *file; int saved_errno = errno; - const char *file = strrchr(path, '/'); char buf[100]; - extract_rcsid_substr(rcs_rev, buf, sizeof(buf)); + extract_rcsid_substr(__rcs_file_version__, buf, sizeof(buf)); if (!file) { - file = path; + file = strrchr(__FILE__, '/'); + if (!file) { + file = __FILE__; + } else { + file += 1; + } + } + + /* I cannot think of any reason why 'j' should ever be NULL, nor have I ever seen the case in the wild */ + if (likely(j)) { + job_log(j, LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } else { - file += 1; + runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } - - job_log(j, LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); } void @@ -3417,7 +3434,7 @@ job_assumes(j, runtime_close(si->fd) == 0); si->fd = -1; } - } while ((si->fd == -1) && (saved_errno == ENOENT)); + } while (unlikely((si->fd == -1) && (saved_errno == ENOENT))); if (saved_errno == ENOTSUP) { /* @@ -3501,7 +3518,7 @@ struct tm *tmptm = context; int64_t val; - if (LAUNCH_DATA_INTEGER != launch_data_get_type(obj)) { + if (unlikely(LAUNCH_DATA_INTEGER != launch_data_get_type(obj))) { /* hack to let caller know something went wrong */ tmptm->tm_sec = -1; return; @@ -3540,13 +3557,13 @@ return false; } - if (LAUNCH_DATA_DICTIONARY != launch_data_get_type(obj)) { + if (unlikely(LAUNCH_DATA_DICTIONARY != launch_data_get_type(obj))) { return false; } launch_data_dict_iterate(obj, calendarinterval_new_from_obj_dict_walk, &tmptm); - if (tmptm.tm_sec == -1) { + if (unlikely(tmptm.tm_sec == -1)) { return false; } @@ -3591,7 +3608,7 @@ struct calendarinterval *ci = LIST_FIRST(&sorted_calendar_events); time_t now = time(NULL); - if (ci && (ci->when_next < now)) { + if (unlikely(ci && (ci->when_next < now))) { jobmgr_assumes(root_jobmgr, raise(SIGUSR1) != -1); } } @@ -3681,7 +3698,7 @@ char buf[10000]; unsigned int i, buf_off = 0; - if (sg->junkfds) { + if (unlikely(sg->junkfds)) { return; } @@ -3892,6 +3909,7 @@ struct stat sb; bool good_exit = (WIFEXITED(j->last_exit_status) && WEXITSTATUS(j->last_exit_status) == 0); +#ifdef __ppc__ /* * 5066316 * @@ -3901,6 +3919,11 @@ if (j->mgr->global_on_demand_cnt > 0 && strcmp(j->label, "com.apple.kextd") != 0) { return false; } +#else + if (j->mgr->global_on_demand_cnt > 0) { + return false; + } +#endif if (j->start_pending) { job_log(j, LOG_DEBUG, "KeepAlive check: Pent-up non-IPC launch criteria."); @@ -4071,9 +4094,9 @@ struct machservice * machservice_new(job_t j, const char *name, mach_port_t *serviceport, bool pid_local) { - struct machservice *ms; + struct machservice *ms = calloc(1, sizeof(struct machservice) + strlen(name) + 1); - if ((ms = calloc(1, sizeof(struct machservice) + strlen(name) + 1)) == NULL) { + if (!job_assumes(j, ms != NULL)) { return NULL; } @@ -4411,8 +4434,8 @@ } jmr = calloc(1, sizeof(struct jobmgr_s) + (name ? (strlen(name) + 1) : 128)); - - if (jmr == NULL) { + + if (!jobmgr_assumes(jm, jmr != NULL)) { return NULL; } @@ -6677,7 +6700,9 @@ } } - if ((msp = calloc(1, sizeof(struct mspolicy) + strlen(name) + 1)) == NULL) { + msp = calloc(1, sizeof(struct mspolicy) + strlen(name) + 1); + + if (!job_assumes(j, msp != NULL)) { return false; } Modified: trunk/launchd/src/launchd_runtime.c =================================================================== --- trunk/launchd/src/launchd_runtime.c 2007-12-21 21:53:37 UTC (rev 23476) +++ trunk/launchd/src/launchd_runtime.c 2008-01-09 17:30:02 UTC (rev 23477) @@ -186,7 +186,7 @@ for (;;) { kr = mach_msg(&dummy.header, MACH_RCV_MSG|MACH_RCV_LARGE, 0, 0, demand_port_set, 0, MACH_PORT_NULL); - if (kr == MACH_RCV_PORT_CHANGED) { + if (unlikely(kr == MACH_RCV_PORT_CHANGED)) { break; } else if (!launchd_assumes(kr == MACH_RCV_TOO_LARGE)) { continue; @@ -357,7 +357,7 @@ unsigned short flags = kev->flags; unsigned int fflags = kev->fflags; - if (!(LOG_MASK(level) & internal_mask_pri)) { + if (likely(!(LOG_MASK(level) & internal_mask_pri))) { return; } @@ -592,9 +592,7 @@ bulk_kev = kev; - launchd_assumes((bulk_kev_cnt = kevent(fd, NULL, 0, kev, BULK_KEV_MAX, &ts)) != -1); - - if (bulk_kev_cnt > 0) { + if (launchd_assumes((bulk_kev_cnt = kevent(fd, NULL, 0, kev, BULK_KEV_MAX, &ts)) != -1)) { #if 0 Dl_info dli; @@ -631,11 +629,11 @@ int flags = VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|TRUE; for (;;) { - if (req) { + if (likely(req)) { launchd_assumes(vm_deallocate(mach_task_self(), (vm_address_t)req, mz) == KERN_SUCCESS); req = NULL; } - if (resp) { + if (likely(resp)) { launchd_assumes(vm_deallocate(mach_task_self(), (vm_address_t)resp, mz) == KERN_SUCCESS); resp = NULL; } @@ -676,7 +674,7 @@ if (which == MACH_NOTIFY_NO_SENDERS) { /* Always make sure the send count is zero, in case a receive right is reused */ errno = mach_port_set_mscount(mach_task_self(), name, 0); - if (errno != KERN_SUCCESS) { + if (unlikely(errno != KERN_SUCCESS)) { return errno; } } @@ -684,7 +682,7 @@ errno = mach_port_request_notification(mach_task_self(), name, which, msgc, where, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous); - if (errno == 0 && previous != MACH_PORT_NULL) { + if (likely(errno == 0) && previous != MACH_PORT_NULL) { launchd_assumes(launchd_mport_deallocate(previous) == KERN_SUCCESS); } @@ -749,7 +747,7 @@ msg_size = round_page(msg_size + MAX_TRAILER_SIZE); - if (needed_table_sz > mig_cb_table_sz) { + if (unlikely(needed_table_sz > mig_cb_table_sz)) { needed_table_sz *= 2; /* Let's try and avoid realloc'ing for a while */ mig_callback *new_table = malloc(needed_table_sz); @@ -757,7 +755,7 @@ return KERN_RESOURCE_SHORTAGE; } - if (mig_cb_table) { + if (likely(mig_cb_table)) { memcpy(new_table, mig_cb_table, mig_cb_table_sz); free(mig_cb_table); } @@ -960,7 +958,7 @@ trailer_size = tp->msgh_trailer_size - (mach_msg_size_t)(sizeof(mach_msg_trailer_type_t) - sizeof(mach_msg_trailer_size_t)); - if (trailer_size < (mach_msg_size_t)sizeof(audit_token_t)) { + if (unlikely(trailer_size < (mach_msg_size_t)sizeof(audit_token_t))) { au_tok = NULL; return; } @@ -971,7 +969,7 @@ bool runtime_get_caller_creds(struct ldcred *ldc) { - if (!au_tok) { + if (unlikely(!au_tok)) { return false; } @@ -999,7 +997,7 @@ for (;;) { to = MACH_MSG_TIMEOUT_NONE; - if (msg_size != max_msg_size) { + if (unlikely(msg_size != max_msg_size)) { /* The buffer isn't big enougth to receive messages anymore... */ tmp_options &= ~MACH_RCV_MSG; options &= ~MACH_RCV_MSG; @@ -1023,13 +1021,13 @@ tmp_options = options; - if (mr == MACH_SEND_INVALID_DEST || mr == MACH_SEND_TIMED_OUT) { + if (unlikely(mr == MACH_SEND_INVALID_DEST || mr == MACH_SEND_TIMED_OUT)) { /* We need to clean up and start over. */ if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) { mach_msg_destroy(&bufReply->Head); } continue; - } else if (mr == MACH_RCV_TIMED_OUT) { + } else if (unlikely(mr == MACH_RCV_TIMED_OUT)) { if (to != MACH_MSG_TIMEOUT_NONE) { if (runtime_busy_cnt == 0) { launchd_shutdown(); @@ -1046,7 +1044,7 @@ bufRequest = bufReply; bufReply = bufTemp; - if (!(tmp_options & MACH_RCV_MSG)) { + if (unlikely(!(tmp_options & MACH_RCV_MSG))) { continue; } @@ -1069,7 +1067,7 @@ * struct to declare our intent. */ static int no_hang_fd = -1; - if (no_hang_fd == -1) { + if (unlikely(no_hang_fd == -1)) { no_hang_fd = _fd(open("/dev/autofs_nowait", 0)); } @@ -1207,7 +1205,7 @@ /* we do this to make the unpacking for the log_drain cause unalignment faults */ lm_sz = ROUND_TO_64BIT_WORD_SIZE(lm_sz); - if (!(lm = calloc(1, lm_sz))) { + if (unlikely((lm = calloc(1, lm_sz)) == NULL)) { return false; } @@ -1255,7 +1253,7 @@ mig_allocate(outval, *outvalCnt); - if (*outval == 0) { + if (unlikely(*outval == 0)) { return 1; } @@ -1299,7 +1297,7 @@ tmp_port = drain_reply_port; drain_reply_port = MACH_PORT_NULL; - if ((errno = job_mig_log_drain_reply(tmp_port, 0, outval, outvalCnt))) { + if (unlikely(errno = job_mig_log_drain_reply(tmp_port, 0, outval, outvalCnt))) { launchd_assumes(errno == MACH_SEND_INVALID_DEST); launchd_assumes(launchd_mport_deallocate(tmp_port) == KERN_SUCCESS); } @@ -1307,7 +1305,16 @@ mig_deallocate(outval, outvalCnt); } +#if 0 void +runtime_kernel_trace(void *code, void *a, void *b, void *c, void *d) +{ + /* Request codes from Joe S. */ + syscall(180 , code, a, b, c, d); +} +#endif + +void runtime_log_push(void) { static pthread_mutex_t ourlock = PTHREAD_MUTEX_INITIALIZER; @@ -1332,7 +1339,7 @@ return; } - if (shutdown_start == 0) { + if (unlikely(shutdown_start == 0)) { shutdown_start = runtime_get_wall_time(); launchd_log_vm_stats(); } @@ -1340,14 +1347,14 @@ pthread_mutex_lock(&ourlock); - if (ourlogfile == NULL) { + if (unlikely(ourlogfile == NULL)) { rename("/var/log/launchd-shutdown.log", "/var/log/launchd-shutdown.log.1"); ourlogfile = fopen("/var/log/launchd-shutdown.log", "a"); } pthread_mutex_unlock(&ourlock); - if (!ourlogfile) { + if (unlikely(!ourlogfile)) { return; } @@ -1541,7 +1548,7 @@ static int apple_internal_logging = 1; struct stat sb; - if (apple_internal_logging == 1) { + if (unlikely(apple_internal_logging == 1)) { apple_internal_logging = stat("/AppleInternal", &sb); } @@ -1599,4 +1606,3 @@ tbi_float_val = tbi.numer; tbi_float_val /= tbi.denom; } - -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080109/79e086d8/attachment-0001.html From source_changes at macosforge.org Wed Jan 9 09:35:13 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Wed Jan 9 09:35:14 2008 Subject: [launchd-changes] [23478] trunk/launchd/src Message-ID: <20080109173513.8802BD9037E5@lists.macosforge.org> Revision: 23478 http://trac.macosforge.org/projects/launchd/changeset/23478 Author: zarzycki@apple.com Date: 2008-01-09 09:35:12 -0800 (Wed, 09 Jan 2008) Log Message: ----------- More misc. branch hints, plus basic (and untested) "StandardInPath" support. Modified Paths: -------------- trunk/launchd/src/launchd.plist.5 trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/liblaunch_public.h Modified: trunk/launchd/src/launchd.plist.5 =================================================================== --- trunk/launchd/src/launchd.plist.5 2008-01-09 17:30:02 UTC (rev 23477) +++ trunk/launchd/src/launchd.plist.5 2008-01-09 17:35:12 UTC (rev 23478) @@ -226,6 +226,9 @@ .It Sy Month The month on which this job will be run. .El +.It Sy StandardInPath +This optional key specifies what file should be used for data being supplied to stdin when using +.Xr stdio 3 . .It Sy StandardOutPath This optional key specifies what file should be used for data being sent to stdout when using .Xr stdio 3 . Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2008-01-09 17:30:02 UTC (rev 23477) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-09 17:35:12 UTC (rev 23478) @@ -331,6 +331,7 @@ char *workingdir; char *username; char *groupname; + char *stdinpath; char *stdoutpath; char *stderrpath; char *alt_exc_handler; @@ -343,7 +344,8 @@ pid_t p; int argc; int last_exit_status; - int forkfd; + int stdin_fd; + int fork_fd; int log_redirect_fd; int nice; unsigned int timeout; @@ -458,7 +460,7 @@ static size_t total_anon_children; static mach_port_t the_exception_server; static bool did_first_per_user_launchd_BootCache_hack; -#define JOB_BOOTCACHE_HACK_CHECK(j) (j->per_user && !did_first_per_user_launchd_BootCache_hack && (j->mach_uid >= 500) && (j->mach_uid != (uid_t)-2)) +#define JOB_BOOTCACHE_HACK_CHECK(j) (unlikely(j->per_user && !did_first_per_user_launchd_BootCache_hack && (j->mach_uid >= 500) && (j->mach_uid != (uid_t)-2))) static jobmgr_t background_jobmgr; static job_t workaround_5477111; @@ -576,6 +578,9 @@ if (j->prog && (tmp = launch_data_new_string(j->prog))) { launch_data_dict_insert(r, tmp, LAUNCH_JOBKEY_PROGRAM); } + if (j->stdinpath && (tmp = launch_data_new_string(j->stdinpath))) { + launch_data_dict_insert(r, tmp, LAUNCH_JOBKEY_STANDARDINPATH); + } if (j->stdoutpath && (tmp = launch_data_new_string(j->stdoutpath))) { launch_data_dict_insert(r, tmp, LAUNCH_JOBKEY_STANDARDOUTPATH); } @@ -810,10 +815,14 @@ job_set_global_on_demand(j, false); } - if (!job_assumes(j, j->forkfd == 0)) { - job_assumes(j, runtime_close(j->forkfd) != -1); + if (!job_assumes(j, j->fork_fd == 0)) { + job_assumes(j, runtime_close(j->fork_fd) != -1); } + if (j->stdin_fd) { + job_assumes(j, runtime_close(j->stdin_fd) != -1); + } + if (!job_assumes(j, j->log_redirect_fd == 0)) { job_assumes(j, runtime_close(j->log_redirect_fd) != -1); } @@ -872,6 +881,9 @@ if (j->groupname) { free(j->groupname); } + if (j->stdinpath) { + free(j->stdinpath); + } if (j->stdoutpath) { free(j->stdoutpath); } @@ -1499,6 +1511,17 @@ where2put = &j->stdoutpath; } else if (strcasecmp(key, LAUNCH_JOBKEY_STANDARDERRORPATH) == 0) { where2put = &j->stderrpath; + } else if (strcasecmp(key, LAUNCH_JOBKEY_STANDARDINPATH) == 0) { + where2put = &j->stdinpath; + j->stdin_fd = _fd(open(value, O_RDONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, DEFFILEMODE)); + if (job_assumes(j, j->stdin_fd != -1)) { + /* open() should not block, but regular IO by the job should */ + job_assumes(j, fcntl(j->stdin_fd, F_SETFL, 0) != -1); + /* XXX -- EV_CLEAR should make named pipes happy? */ + job_assumes(j, kevent_mod(j->stdin_fd, EVFILT_READ, EV_ADD|EV_CLEAR, 0, 0, j) != -1); + } else { + j->stdin_fd = 0; + } } else if (strcasecmp(key, LAUNCH_JOBKEY_SANDBOXPROFILE) == 0) { where2put = &j->seatbelt_profile; } @@ -2094,9 +2117,9 @@ } } - if (j->forkfd) { - job_assumes(j, runtime_close(j->forkfd) != -1); - j->forkfd = 0; + if (j->fork_fd) { + job_assumes(j, runtime_close(j->fork_fd) != -1); + j->fork_fd = 0; } if (j->anonymous) { @@ -2460,6 +2483,8 @@ { if (ident == j->log_redirect_fd) { job_log_stdouterr(j); + } else if (ident == j->stdin_fd) { + job_dispatch(j, true); } else { socketgroup_callback(j); } @@ -2580,7 +2605,7 @@ return; } - if (job_active(j)) { + if (unlikely(job_active(j))) { job_log(j, LOG_DEBUG, "Already started"); return; } @@ -2611,7 +2636,7 @@ j->sent_sigterm_time = 0; - if (!j->legacy_mach_job) { + if (likely(!j->legacy_mach_job)) { sipc = (!SLIST_EMPTY(&j->sockets) || !SLIST_EMPTY(&j->machservices)); } @@ -2623,7 +2648,7 @@ job_assumes(j, socketpair(AF_UNIX, SOCK_STREAM, 0, execspair) != -1); - if (!j->legacy_mach_job && job_assumes(j, pipe(oepair) != -1)) { + if (likely(!j->legacy_mach_job) && job_assumes(j, pipe(oepair) != -1)) { j->log_redirect_fd = _fd(oepair[0]); job_assumes(j, fcntl(j->log_redirect_fd, F_SETFL, O_NONBLOCK) != -1); job_assumes(j, kevent_mod(j->log_redirect_fd, EVFILT_READ, EV_ADD, 0, 0, j) != -1); @@ -2640,14 +2665,14 @@ job_assumes(j, runtime_close(spair[0]) == 0); job_assumes(j, runtime_close(spair[1]) == 0); } - if (!j->legacy_mach_job) { + if (likely(!j->legacy_mach_job)) { job_assumes(j, runtime_close(oepair[0]) != -1); job_assumes(j, runtime_close(oepair[1]) != -1); j->log_redirect_fd = 0; } break; case 0: - if (_vproc_post_fork_ping()) { + if (unlikely(_vproc_post_fork_ping())) { _exit(EXIT_FAILURE); } if (!j->legacy_mach_job) { @@ -2679,16 +2704,16 @@ did_first_per_user_launchd_BootCache_hack = true; } - if (!j->legacy_mach_job) { + if (likely(!j->legacy_mach_job)) { job_assumes(j, runtime_close(oepair[1]) != -1); } j->p = c; - if (j->hopefully_exits_first) { + if (unlikely(j->hopefully_exits_first)) { j->mgr->hopefully_first_cnt++; - } else if (!j->hopefully_exits_last) { + } else if (likely(!j->hopefully_exits_last)) { j->mgr->normal_active_cnt++; } - j->forkfd = _fd(execspair[0]); + j->fork_fd = _fd(execspair[0]); job_assumes(j, runtime_close(execspair[1]) == 0); if (sipc) { job_assumes(j, runtime_close(spair[1]) == 0); @@ -2700,7 +2725,7 @@ job_reap(j); } - if (!j->stall_before_exec) { + if (likely(!j->stall_before_exec)) { job_uncork_fork(j); } break; @@ -2745,7 +2770,7 @@ job_setup_attributes(j); - if (j->argv && j->globargv) { + if (unlikely(j->argv && j->globargv)) { g.gl_offs = 1; for (i = 0; i < j->argc; i++) { if (i > 0) { @@ -2772,18 +2797,18 @@ argv[2] = NULL; } - if (!j->inetcompat) { + if (likely(!j->inetcompat)) { argv++; } - if (j->wait4debugger) { + if (unlikely(j->wait4debugger)) { job_log(j, LOG_WARNING, "Spawned and waiting for the debugger to attach before continuing..."); spflags |= POSIX_SPAWN_START_SUSPENDED; } job_assumes(j, posix_spawnattr_setflags(&spattr, spflags) == 0); - if (j->j_binpref_cnt) { + if (unlikely(j->j_binpref_cnt)) { job_assumes(j, posix_spawnattr_setbinpref_np(&spattr, j->j_binpref_cnt, j->j_binpref, &binpref_out_cnt) == 0); job_assumes(j, binpref_out_cnt == j->j_binpref_cnt); } @@ -2811,7 +2836,7 @@ psf = j->prog ? posix_spawn : posix_spawnp; - if (!j->inetcompat) { + if (likely(!j->inetcompat)) { file2exec = j->prog ? j->prog : argv[0]; } @@ -2964,22 +2989,22 @@ strlcpy(loginname, pwe->pw_name, sizeof(loginname)); strlcpy(homedir, pwe->pw_dir, sizeof(homedir)); - if (pwe->pw_expire && time(NULL) >= pwe->pw_expire) { + if (unlikely(pwe->pw_expire && time(NULL) >= pwe->pw_expire)) { job_log(j, LOG_ERR, "Expired account"); _exit(EXIT_FAILURE); } - if (j->username && strcmp(j->username, loginname) != 0) { + if (unlikely(j->username && strcmp(j->username, loginname) != 0)) { job_log(j, LOG_WARNING, "Suspicious setup: User \"%s\" maps to user: %s", j->username, loginname); - } else if (j->mach_uid && (j->mach_uid != desired_uid)) { + } else if (unlikely(j->mach_uid && (j->mach_uid != desired_uid))) { job_log(j, LOG_WARNING, "Suspicious setup: UID %u maps to UID %u", j->mach_uid, desired_uid); } if (j->groupname) { struct group *gre; - if ((gre = getgrnam(j->groupname)) == NULL) { + if (unlikely((gre = getgrnam(j->groupname)) == NULL)) { job_log(j, LOG_ERR, "getgrnam(\"%s\") failed", j->groupname); _exit(EXIT_FAILURE); } @@ -3000,7 +3025,7 @@ * called after setgid(). See 4616864 for more information. */ - if (!j->no_init_groups) { + if (likely(!j->no_init_groups)) { if (!job_assumes(j, initgroups(loginname, desired_gid) != -1)) { _exit(EXIT_FAILURE); } @@ -3012,7 +3037,7 @@ r = confstr(_CS_DARWIN_USER_TEMP_DIR, tmpdirpath, sizeof(tmpdirpath)); - if (r > 0 && r < sizeof(tmpdirpath)) { + if (likely(r > 0 && r < sizeof(tmpdirpath))) { setenv("TMPDIR", tmpdirpath, 0); } @@ -3028,7 +3053,7 @@ struct limititem *li; struct envitem *ei; - if (j->setnice) { + if (unlikely(j->setnice)) { job_assumes(j, setpriority(PRIO_PROCESS, 0, j->nice) != -1); } @@ -3051,30 +3076,35 @@ } } - if (!j->inetcompat && j->session_create) { + if (unlikely(!j->inetcompat && j->session_create)) { launchd_SessionCreate(); } - if (j->low_pri_io) { + if (unlikely(j->low_pri_io)) { job_assumes(j, setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, IOPOL_THROTTLE) != -1); } - if (j->rootdir) { + if (unlikely(j->rootdir)) { job_assumes(j, chroot(j->rootdir) != -1); job_assumes(j, chdir(".") != -1); } job_postfork_become_user(j); - if (j->workingdir) { + if (unlikely(j->workingdir)) { job_assumes(j, chdir(j->workingdir) != -1); } - if (j->setmask) { + if (unlikely(j->setmask)) { umask(j->mask); } - job_setup_fd(j, STDOUT_FILENO, j->stdoutpath, O_WRONLY|O_APPEND|O_CREAT); - job_setup_fd(j, STDERR_FILENO, j->stderrpath, O_WRONLY|O_APPEND|O_CREAT); + if (j->stdin_fd) { + job_assumes(j, dup2(j->stdin_fd, STDIN_FILENO) != -1); + } else { + job_setup_fd(j, STDIN_FILENO, j->stdinpath, O_RDONLY|O_CREAT); + } + job_setup_fd(j, STDOUT_FILENO, j->stdoutpath, O_WRONLY|O_CREAT|O_APPEND); + job_setup_fd(j, STDERR_FILENO, j->stderrpath, O_WRONLY|O_CREAT|O_APPEND); jobmgr_setup_env_from_other_jobs(j->mgr); @@ -3119,7 +3149,7 @@ struct dirent *de; bool r = 0; - if (!dd) { + if (unlikely(!dd)) { return -1; } @@ -3185,7 +3215,7 @@ ctime_r(&later, time_string); time_string_len = strlen(time_string); - if (time_string_len && time_string[time_string_len - 1] == '\n') { + if (likely(time_string_len && time_string[time_string_len - 1] == '\n')) { time_string[time_string_len - 1] = '\0'; } @@ -3287,13 +3317,13 @@ snprintf(newmsg, newmsgsz, "%s", msg); } - if (j->debug) { + if (unlikely(j->debug)) { oldmask = setlogmask(LOG_UPTO(LOG_DEBUG)); } runtime_vsyslog(&attr, newmsg, ap); - if (j->debug) { + if (unlikely(j->debug)) { setlogmask(oldmask); } } @@ -4414,9 +4444,9 @@ job_log(j, LOG_DEBUG, "Uncorking the fork()."); /* this unblocks the child and avoids a race * between the above fork() and the kevent_mod() */ - job_assumes(j, write(j->forkfd, &c, sizeof(c)) == sizeof(c)); - job_assumes(j, runtime_close(j->forkfd) != -1); - j->forkfd = 0; + job_assumes(j, write(j->fork_fd, &c, sizeof(c)) == sizeof(c)); + job_assumes(j, runtime_close(j->fork_fd) != -1); + j->fork_fd = 0; } jobmgr_t Modified: trunk/launchd/src/liblaunch_public.h =================================================================== --- trunk/launchd/src/liblaunch_public.h 2008-01-09 17:30:02 UTC (rev 23477) +++ trunk/launchd/src/liblaunch_public.h 2008-01-09 17:35:12 UTC (rev 23478) @@ -85,6 +85,7 @@ #define LAUNCH_JOBKEY_STARTONMOUNT "StartOnMount" #define LAUNCH_JOBKEY_SOFTRESOURCELIMITS "SoftResourceLimits" #define LAUNCH_JOBKEY_HARDRESOURCELIMITS "HardResourceLimits" +#define LAUNCH_JOBKEY_STANDARDINPATH "StandardInPath" #define LAUNCH_JOBKEY_STANDARDOUTPATH "StandardOutPath" #define LAUNCH_JOBKEY_STANDARDERRORPATH "StandardErrorPath" #define LAUNCH_JOBKEY_DEBUG "Debug" -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080109/1b82a5f4/attachment-0001.html From source_changes at macosforge.org Wed Jan 9 14:18:12 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Wed Jan 9 14:18:15 2008 Subject: [launchd-changes] [23479] trunk/launchd/src Message-ID: <20080109221812.14526D93AEA1@lists.macosforge.org> Revision: 23479 http://trac.macosforge.org/projects/launchd/changeset/23479 Author: zarzycki@apple.com Date: 2008-01-09 14:18:09 -0800 (Wed, 09 Jan 2008) Log Message: ----------- We should use a better internal ABI when we can. Modified Paths: -------------- trunk/launchd/src/Makefile.am trunk/launchd/src/Makefile.in trunk/launchd/src/launchd.c trunk/launchd/src/launchd.h trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_core_logic.h trunk/launchd/src/launchd_runtime.c trunk/launchd/src/launchd_runtime.h trunk/launchd/src/launchd_unix_ipc.c Modified: trunk/launchd/src/Makefile.am =================================================================== --- trunk/launchd/src/Makefile.am 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/Makefile.am 2008-01-09 22:18:09 UTC (rev 23479) @@ -1,4 +1,4 @@ -AM_CFLAGS = -F/System/Library/PrivateFrameworks -Wall -Wextra -Waggregate-return -Wfloat-equal -Wshadow -Wpacked -Wpadded -Wstrict-overflow=4 -Wmissing-prototypes -Wmissing-declarations -Werror -D__MigTypeCheck=1 -fvisibility=hidden -freorder-blocks -Dmig_external=__private_extern__ +AM_CFLAGS = -F/System/Library/PrivateFrameworks -Wall -Wextra -Waggregate-return -Wfloat-equal -Wshadow -Wpacked -Wpadded -Wstrict-overflow=4 -Wmissing-prototypes -Wmissing-declarations -Werror -D__MigTypeCheck=1 -fvisibility=hidden -Dmig_external=__private_extern__ CLEANFILES = protocol_vproc.h protocol_vprocServer.c protocol_vprocUser.c protocol_vprocServer.h \ launchd_internal.h launchd_internalServer.h launchd_internalServer.c launchd_internalUser.c \ @@ -46,7 +46,7 @@ SystemStarter_LDFLAGS = -framework CoreFoundation -framework IOKit SystemStarter_SOURCES = StartupItems.c IPC.c SystemStarter.c -launchd_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) # -Wconversion # -Wstrict-aliasing=2 -fstrict-aliasing +launchd_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) -freorder-blocks # -fomit-frame-pointer # -Wconversion # -Wstrict-aliasing=2 -fstrict-aliasing launchd_LDFLAGS = -lbsm launchd_SOURCES = launchd.c launchd_core_logic.c launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c launchd_internalUser.c launchd_internalServer.c job_replyUser.c launchd_runtime.c launchd_runtime_kill.c job_forwardUser.c mach_excServer.c Modified: trunk/launchd/src/Makefile.in =================================================================== --- trunk/launchd/src/Makefile.in 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/Makefile.in 2008-01-09 22:18:09 UTC (rev 23479) @@ -238,7 +238,7 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -AM_CFLAGS = -F/System/Library/PrivateFrameworks -Wall -Wextra -Waggregate-return -Wfloat-equal -Wshadow -Wpacked -Wpadded -Wstrict-overflow=4 -Wmissing-prototypes -Wmissing-declarations -Werror -D__MigTypeCheck=1 -fvisibility=hidden -freorder-blocks -Dmig_external=__private_extern__ +AM_CFLAGS = -F/System/Library/PrivateFrameworks -Wall -Wextra -Waggregate-return -Wfloat-equal -Wshadow -Wpacked -Wpadded -Wstrict-overflow=4 -Wmissing-prototypes -Wmissing-declarations -Werror -D__MigTypeCheck=1 -fvisibility=hidden -Dmig_external=__private_extern__ CLEANFILES = protocol_vproc.h protocol_vprocServer.c protocol_vprocUser.c protocol_vprocServer.h \ launchd_internal.h launchd_internalServer.h launchd_internalServer.c launchd_internalUser.c \ notifyServer.c notifyServer.h job_replyUser.c job_reply.h __version.c mach_excServer.c mach_excServer.h @@ -254,7 +254,7 @@ @LIBS_ONLY_FALSE@SystemStarter_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) @LIBS_ONLY_FALSE@SystemStarter_LDFLAGS = -framework CoreFoundation -framework IOKit @LIBS_ONLY_FALSE@SystemStarter_SOURCES = StartupItems.c IPC.c SystemStarter.c -@LIBS_ONLY_FALSE@launchd_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) # -Wconversion # -Wstrict-aliasing=2 -fstrict-aliasing +@LIBS_ONLY_FALSE@launchd_CFLAGS = -mdynamic-no-pic $(AM_CFLAGS) -freorder-blocks # -fomit-frame-pointer # -Wconversion # -Wstrict-aliasing=2 -fstrict-aliasing @LIBS_ONLY_FALSE@launchd_LDFLAGS = -lbsm @LIBS_ONLY_FALSE@launchd_SOURCES = launchd.c launchd_core_logic.c launchd_unix_ipc.c protocol_vprocServer.c notifyServer.c launchd_internalUser.c launchd_internalServer.c job_replyUser.c launchd_runtime.c launchd_runtime_kill.c job_forwardUser.c mach_excServer.c @LIBS_ONLY_FALSE@launchproxy_LDFLAGS = -weak_framework Security Modified: trunk/launchd/src/launchd.c =================================================================== --- trunk/launchd/src/launchd.c 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd.c 2008-01-09 22:18:09 UTC (rev 23479) @@ -227,7 +227,7 @@ } -int +INTERNAL_ABI int _fd(int fd) { if (fd >= 0) { @@ -242,7 +242,7 @@ launchd_assumes(mkdir(SHUTDOWN_LOG_DIR, S_IRWXU) != -1 || errno == EEXIST); } -void +INTERNAL_ABI void launchd_shutdown(void) { int64_t now; @@ -271,7 +271,7 @@ launchd_assert(jobmgr_shutdown(root_jobmgr) != NULL); } -void +INTERNAL_ABI void launchd_single_user(void) { runtime_syslog(LOG_NOTICE, "Going to single-user mode"); @@ -285,7 +285,7 @@ runtime_kill(-1, SIGKILL); } -void +INTERNAL_ABI void launchd_SessionCreate(void) { OSStatus (*sescr)(SessionCreationFlags flags, SessionAttributeBits attributes); @@ -392,7 +392,7 @@ } } -void +INTERNAL_ABI bool _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test) { int saved_errno = errno; @@ -417,4 +417,6 @@ } runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); + + return false; } Modified: trunk/launchd/src/launchd.h =================================================================== --- trunk/launchd/src/launchd.h 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd.h 2008-01-09 22:18:09 UTC (rev 23479) @@ -35,16 +35,16 @@ extern bool fake_shutdown_in_progress; extern bool network_up; -bool init_check_pid(pid_t); +INTERNAL_ABI bool init_check_pid(pid_t); -launch_data_t launchd_setstdio(int d, launch_data_t o); -void launchd_SessionCreate(void); -void launchd_shutdown(void); -void launchd_single_user(void); +INTERNAL_ABI launch_data_t launchd_setstdio(int d, launch_data_t o); +INTERNAL_ABI void launchd_SessionCreate(void); +INTERNAL_ABI void launchd_shutdown(void); +INTERNAL_ABI void launchd_single_user(void); boolean_t launchd_mach_ipc_demux(mach_msg_header_t *Request, mach_msg_header_t *Reply); -void mach_start_shutdown(void); +INTERNAL_ABI void mach_start_shutdown(void); -int _fd(int fd); +INTERNAL_ABI int _fd(int fd); #endif Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-09 22:18:09 UTC (rev 23479) @@ -275,7 +275,7 @@ }; #define jobmgr_assumes(jm, e) \ - (likely(e) ? true : jobmgr_log_bug(jm, __LINE__), false) + (likely(e) ? true : jobmgr_log_bug(jm, __LINE__)) static jobmgr_t jobmgr_new(jobmgr_t jm, mach_port_t requestorport, mach_port_t transfer_port, bool sflag, const char *name); static job_t jobmgr_import2(jobmgr_t jm, launch_data_t pload); @@ -296,7 +296,7 @@ static void jobmgr_logv(jobmgr_t jm, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); static void jobmgr_log(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); /* static void jobmgr_log_error(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); */ -static void jobmgr_log_bug(jobmgr_t jm, unsigned int line); +static bool jobmgr_log_bug(jobmgr_t jm, unsigned int line); #define DO_RUSAGE_SUMMATION 0 @@ -375,7 +375,7 @@ #define job_assumes(j, e) \ - (likely(e) ? true : job_log_bug(j, __LINE__), false) + (likely(e) ? true : job_log_bug(j, __LINE__)) static void job_import_keys(launch_data_t obj, const char *key, void *context); static void job_import_bool(job_t j, const char *key, bool value); @@ -417,7 +417,7 @@ static void job_log_stdouterr(job_t j); static void job_logv(job_t j, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); static void job_log_error(job_t j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); -static void job_log_bug(job_t j, unsigned int line); +static bool job_log_bug(job_t j, unsigned int line); static void job_log_stdouterr2(job_t j, const char *msg, ...); static void job_set_exeception_port(job_t j, mach_port_t port); static kern_return_t job_handle_mpm_wait(job_t j, mach_port_t srp, int *waitstatus); @@ -530,7 +530,7 @@ } } -void +INTERNAL_ABI void job_stop(job_t j) { if (!j->p || j->anonymous) { @@ -548,7 +548,7 @@ job_log(j, LOG_DEBUG, "Sent SIGTERM signal"); } -launch_data_t +INTERNAL_ABI launch_data_t job_export(job_t j) { launch_data_t tmp, tmp2, tmp3, r = launch_data_alloc(LAUNCH_DATA_DICTIONARY); @@ -690,7 +690,7 @@ runtime_closelog(); /* hack to flush logs */ } -jobmgr_t +INTERNAL_ABI jobmgr_t jobmgr_shutdown(jobmgr_t jm) { jobmgr_t jmi, jmn; @@ -773,7 +773,7 @@ free(jm); } -void +INTERNAL_ABI void job_remove(job_t j) { struct waiting_for_removal *w4r; @@ -1273,7 +1273,7 @@ return NULL; } -job_t +INTERNAL_ABI job_t job_import(launch_data_t pload) { job_t j = jobmgr_import2(root_jobmgr, pload); @@ -1285,7 +1285,7 @@ return job_dispatch(j, false); } -launch_data_t +INTERNAL_ABI launch_data_t job_import_bulk(launch_data_t pload) { launch_data_t resp = launch_data_alloc(LAUNCH_DATA_ARRAY); @@ -1895,7 +1895,7 @@ return j; } -job_t +INTERNAL_ABI job_t job_find(const char *label) { job_t ji; @@ -1964,7 +1964,7 @@ return NULL; } -job_t +INTERNAL_ABI job_t job_mig_intran(mach_port_t p) { struct ldcred ldc; @@ -1990,7 +1990,7 @@ return jr; } -job_t +INTERNAL_ABI job_t job_find_by_service_port(mach_port_t p) { struct machservice *ms; @@ -2004,7 +2004,7 @@ return NULL; } -void +INTERNAL_ABI void job_mig_destructor(job_t j) { /* @@ -2042,7 +2042,7 @@ } } -launch_data_t +INTERNAL_ABI launch_data_t job_export_all(void) { launch_data_t resp = launch_data_alloc(LAUNCH_DATA_DICTIONARY); @@ -2255,7 +2255,7 @@ } } -job_t +INTERNAL_ABI job_t job_dispatch(job_t j, bool kickstart) { /* @@ -3239,7 +3239,7 @@ } } -void +bool jobmgr_log_bug(jobmgr_t jm, unsigned int line) { static const char *file; @@ -3263,9 +3263,11 @@ } else { runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } + + return false; } -void +bool job_log_bug(job_t j, unsigned int line) { static const char *file; @@ -3289,6 +3291,8 @@ } else { runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } + + return false; } void @@ -3338,7 +3342,7 @@ va_end(ap); } -void +INTERNAL_ABI void job_log(job_t j, int pri, const char *msg, ...) { va_list ap; @@ -4587,7 +4591,7 @@ return bootstrapper; } -jobmgr_t +INTERNAL_ABI jobmgr_t jobmgr_delete_anything_with_port(jobmgr_t jm, mach_port_t port) { struct machservice *ms, *next_ms; @@ -4787,13 +4791,13 @@ return argv_ret; } -void +INTERNAL_ABI void job_checkin(job_t j) { j->checkedin = true; } -bool +INTERNAL_ABI bool job_ack_port_destruction(mach_port_t p) { struct machservice *ms; @@ -4822,7 +4826,7 @@ return true; } -void +INTERNAL_ABI void job_ack_no_senders(job_t j) { j->priv_port_has_senders = false; @@ -4845,7 +4849,7 @@ return NULL; } -bool +INTERNAL_ABI bool job_is_anonymous(job_t j) { return j->anonymous; @@ -5064,7 +5068,7 @@ } } -void +INTERNAL_ABI void jobmgr_dispatch_all_semaphores(jobmgr_t jm) { jobmgr_t jmi, jmn; @@ -6666,7 +6670,7 @@ return BOOTSTRAP_SUCCESS; } -void +INTERNAL_ABI void jobmgr_init(bool sflag) { const char *root_session_type = getpid() == 1 ? VPROCMGR_SESSION_SYSTEM : VPROCMGR_SESSION_BACKGROUND; Modified: trunk/launchd/src/launchd_core_logic.h =================================================================== --- trunk/launchd/src/launchd_core_logic.h 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd_core_logic.h 2008-01-09 22:18:09 UTC (rev 23479) @@ -20,6 +20,7 @@ * @APPLE_APACHE_LICENSE_HEADER_END@ */ +#include #include "libbootstrap_public.h" #include "liblaunch_public.h" @@ -29,27 +30,27 @@ extern jobmgr_t root_jobmgr; extern mach_port_t inherited_bootstrap_port; -void jobmgr_init(bool); -jobmgr_t jobmgr_shutdown(jobmgr_t jm); -void jobmgr_dispatch_all_semaphores(jobmgr_t jm); -jobmgr_t jobmgr_delete_anything_with_port(jobmgr_t jm, mach_port_t port); +INTERNAL_ABI void jobmgr_init(bool); +INTERNAL_ABI jobmgr_t jobmgr_shutdown(jobmgr_t jm); +INTERNAL_ABI void jobmgr_dispatch_all_semaphores(jobmgr_t jm); +INTERNAL_ABI jobmgr_t jobmgr_delete_anything_with_port(jobmgr_t jm, mach_port_t port); -launch_data_t job_export_all(void); +INTERNAL_ABI launch_data_t job_export_all(void); -job_t job_dispatch(job_t j, bool kickstart); /* returns j on success, NULL on job removal */ -job_t job_find(const char *label); -job_t job_find_by_service_port(mach_port_t p); -bool job_ack_port_destruction(mach_port_t p); -bool job_is_anonymous(job_t j); -launch_data_t job_export(job_t j); -void job_stop(job_t j); -void job_checkin(job_t j); -void job_remove(job_t j); -job_t job_import(launch_data_t pload); -launch_data_t job_import_bulk(launch_data_t pload); -job_t job_mig_intran(mach_port_t mp); -void job_mig_destructor(job_t j); -void job_ack_no_senders(job_t j); -void job_log(job_t j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); +INTERNAL_ABI job_t job_dispatch(job_t j, bool kickstart); /* returns j on success, NULL on job removal */ +INTERNAL_ABI job_t job_find(const char *label); +INTERNAL_ABI job_t job_find_by_service_port(mach_port_t p); +INTERNAL_ABI bool job_ack_port_destruction(mach_port_t p); +INTERNAL_ABI bool job_is_anonymous(job_t j); +INTERNAL_ABI launch_data_t job_export(job_t j); +INTERNAL_ABI void job_stop(job_t j); +INTERNAL_ABI void job_checkin(job_t j); +INTERNAL_ABI void job_remove(job_t j); +INTERNAL_ABI job_t job_import(launch_data_t pload); +INTERNAL_ABI launch_data_t job_import_bulk(launch_data_t pload); +INTERNAL_ABI job_t job_mig_intran(mach_port_t mp); +INTERNAL_ABI void job_mig_destructor(job_t j); +INTERNAL_ABI void job_ack_no_senders(job_t j); +INTERNAL_ABI void job_log(job_t j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); #endif Modified: trunk/launchd/src/launchd_runtime.c =================================================================== --- trunk/launchd/src/launchd_runtime.c 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd_runtime.c 2008-01-09 22:18:09 UTC (rev 23479) @@ -122,7 +122,7 @@ static FILE *ourlogfile; -mach_port_t +INTERNAL_ABI mach_port_t runtime_get_kernel_port(void) { return launchd_internal_port; @@ -132,7 +132,7 @@ //static int internal_mask_pri = LOG_UPTO(LOG_DEBUG); -void +INTERNAL_ABI void launchd_runtime_init(void) { mach_msg_size_t mxmsgsz; @@ -167,7 +167,7 @@ pthread_attr_destroy(&attr); } -void +INTERNAL_ABI void launchd_runtime_init2(void) { size_t i; @@ -197,7 +197,7 @@ return NULL; } -const char * +INTERNAL_ABI const char * proc_flags_to_C_names(unsigned int flags) { #define MAX_PFLAG_STR "P_ADVLOCK|P_CONTROLT|P_LP64|P_NOCLDSTOP|P_PPWAIT|P_PROFIL|P_SELECT|P_CONTINUED|P_SUGID|P_SYSTEM|P_TIMEOUT|P_TRACED|P_RESV3|P_WEXIT|P_EXEC|P_OWEUPC|P_AFFINITY|P_TRANSLATED|P_RESV5|P_CHECKOPENEVT|P_DEPENDENCY_CAPABLE|P_REBOOT|P_TBE|P_RESV7|P_THCWD|P_RESV9|P_RESV10|P_RESV11|P_NOSHLIB|P_FORCEQUOTA|P_NOCLDWAIT|P_NOREMOTEHANG|0xdeadbeeffeedface" @@ -261,7 +261,7 @@ return flags_buf; } -const char * +INTERNAL_ABI const char * reboot_flags_to_C_names(unsigned int flags) { #define MAX_RB_STR "RB_ASKNAME|RB_SINGLE|RB_NOSYNC|RB_HALT|RB_INITNAME|RB_DFLTROOT|RB_ALTBOOT|RB_UNIPROC|RB_SAFEBOOT|RB_UPSDELAY|0xdeadbeeffeedface" @@ -300,7 +300,7 @@ return flags_buf; } -const char * +INTERNAL_ABI const char * signal_to_C_name(unsigned int sig) { static char unknown[25]; @@ -621,7 +621,7 @@ -void +INTERNAL_ABI void launchd_runtime(void) { mig_reply_error_t *req = NULL, *resp = NULL; @@ -653,19 +653,19 @@ } } -kern_return_t +INTERNAL_ABI kern_return_t launchd_set_bport(mach_port_t name) { return errno = task_set_bootstrap_port(mach_task_self(), name); } -kern_return_t +INTERNAL_ABI kern_return_t launchd_get_bport(mach_port_t *name) { return errno = task_get_bootstrap_port(mach_task_self(), name); } -kern_return_t +INTERNAL_ABI kern_return_t launchd_mport_notify_req(mach_port_t name, mach_msg_id_t which) { mach_port_mscount_t msgc = (which == MACH_NOTIFY_PORT_DESTROYED) ? 0 : 1; @@ -689,7 +689,7 @@ return errno; } -pid_t +INTERNAL_ABI pid_t runtime_fork(mach_port_t bsport) { sigset_t emptyset, oset; @@ -727,7 +727,7 @@ } -void +INTERNAL_ABI void runtime_set_timeout(timeout_callback to_cb, unsigned int sec) { if (sec == 0 || to_cb == NULL) { @@ -739,7 +739,7 @@ runtime_idle_timeout = sec * 1000; } -kern_return_t +INTERNAL_ABI kern_return_t runtime_add_mport(mach_port_t name, mig_callback demux, mach_msg_size_t msg_size) { size_t needed_table_sz = (MACH_PORT_INDEX(name) + 1) * sizeof(mig_callback); @@ -773,7 +773,7 @@ return errno = mach_port_move_member(mach_task_self(), name, target_set); } -kern_return_t +INTERNAL_ABI kern_return_t runtime_remove_mport(mach_port_t name) { mig_cb_table[MACH_PORT_INDEX(name)] = NULL; @@ -781,31 +781,31 @@ return errno = mach_port_move_member(mach_task_self(), name, MACH_PORT_NULL); } -kern_return_t +INTERNAL_ABI kern_return_t launchd_mport_make_send(mach_port_t name) { return errno = mach_port_insert_right(mach_task_self(), name, name, MACH_MSG_TYPE_MAKE_SEND); } -kern_return_t +INTERNAL_ABI kern_return_t launchd_mport_close_recv(mach_port_t name) { return errno = mach_port_mod_refs(mach_task_self(), name, MACH_PORT_RIGHT_RECEIVE, -1); } -kern_return_t +INTERNAL_ABI kern_return_t launchd_mport_create_recv(mach_port_t *name) { return errno = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, name); } -kern_return_t +INTERNAL_ABI kern_return_t launchd_mport_deallocate(mach_port_t name) { return errno = mach_port_deallocate(mach_task_self(), name); } -int +INTERNAL_ABI int kevent_bulk_mod(struct kevent *kev, size_t kev_cnt) { size_t i; @@ -817,7 +817,7 @@ return kevent(mainkq, kev, kev_cnt, kev, kev_cnt, NULL); } -int +INTERNAL_ABI int kevent_mod(uintptr_t ident, short filter, u_short flags, u_int fflags, intptr_t data, void *udata) { struct kevent kev; @@ -966,7 +966,7 @@ au_tok = &tp->msgh_audit; } -bool +INTERNAL_ABI bool runtime_get_caller_creds(struct ldcred *ldc) { if (unlikely(!au_tok)) { @@ -1098,7 +1098,7 @@ } } -int +INTERNAL_ABI int runtime_close(int fd) { int i; @@ -1108,7 +1108,7 @@ case EVFILT_VNODE: case EVFILT_WRITE: case EVFILT_READ: - if ((int)bulk_kev[i].ident == fd) { + if (unlikely((int)bulk_kev[i].ident == fd)) { runtime_syslog(LOG_DEBUG, "Skipping kevent index: %d", i); bulk_kev[i].filter = 0; } @@ -1120,7 +1120,7 @@ return close(fd); } -void +INTERNAL_ABI void runtime_closelog(void) { runtime_log_push(); @@ -1131,7 +1131,7 @@ } } -int +INTERNAL_ABI int runtime_fsync(int fd) { #if 0 @@ -1145,7 +1145,7 @@ #endif } -int +INTERNAL_ABI int runtime_setlogmask(int maskpri) { internal_mask_pri = maskpri; @@ -1153,7 +1153,7 @@ return internal_mask_pri; } -void +INTERNAL_ABI void runtime_syslog(int pri, const char *message, ...) { struct runtime_syslog_attr attr = { @@ -1170,7 +1170,7 @@ va_end(ap); } -void +INTERNAL_ABI void runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) { int saved_errno = errno; @@ -1314,7 +1314,7 @@ } #endif -void +INTERNAL_ABI void runtime_log_push(void) { static pthread_mutex_t ourlock = PTHREAD_MUTEX_INITIALIZER; @@ -1368,7 +1368,7 @@ } } -kern_return_t +INTERNAL_ABI kern_return_t runtime_log_forward(uid_t forward_uid, gid_t forward_gid, vm_offset_t inval, mach_msg_type_number_t invalCnt) { struct logmsg_s *lm, *lm_walk; @@ -1404,7 +1404,7 @@ return 0; } -kern_return_t +INTERNAL_ABI kern_return_t runtime_log_drain(mach_port_t srp, vm_offset_t *outval, mach_msg_type_number_t *outvalCnt) { launchd_assumes(drain_reply_port == 0); @@ -1428,13 +1428,13 @@ * In the long run, reference counting should completely automate when a * process can and should exit. */ -void +INTERNAL_ABI void runtime_add_ref(void) { runtime_busy_cnt++; } -void +INTERNAL_ABI void runtime_del_ref(void) { runtime_busy_cnt--; @@ -1494,7 +1494,7 @@ return 0; } -void +INTERNAL_ABI void launchd_log_vm_stats(void) { static struct vm_statistics orig_stats; @@ -1542,7 +1542,7 @@ launchd_mport_deallocate(mhs); } -bool +INTERNAL_ABI bool do_apple_internal_logging(void) { static int apple_internal_logging = 1; @@ -1555,7 +1555,7 @@ return (apple_internal_logging == 0); } -int64_t +INTERNAL_ABI int64_t runtime_get_wall_time(void) { struct timeval tv; @@ -1570,13 +1570,13 @@ return r; } -uint64_t +INTERNAL_ABI uint64_t runtime_get_opaque_time(void) { return mach_absolute_time(); } -uint64_t +INTERNAL_ABI uint64_t runtime_opaque_time_to_nano(uint64_t o) { #if defined(__i386__) Modified: trunk/launchd/src/launchd_runtime.h =================================================================== --- trunk/launchd/src/launchd_runtime.h 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd_runtime.h 2008-01-09 22:18:09 UTC (rev 23479) @@ -70,6 +70,12 @@ #endif +#ifdef __i386__ +#define INTERNAL_ABI __attribute__((regparm(3))) +#else +#define INTERNAL_ABI +#endif + #define likely(x) __builtin_expect((bool)(x), true) #define unlikely(x) __builtin_expect((bool)(x), false) @@ -87,53 +93,50 @@ * Use launchd_assert() for core initialization routines. */ #define launchd_assumes(e) \ - (__builtin_expect(!(e), 0) ? _log_launchd_bug(__rcs_file_version__, __FILE__, __LINE__, #e), false : true) + (likely(e) ? true : _log_launchd_bug(__rcs_file_version__, __FILE__, __LINE__, #e)) -#define launchd_blame(e, b) \ - (__builtin_expect(!(e), 0) ? syslog(LOG_DEBUG, "Encountered bug: %d", b), false : true) - #define launchd_assert(e) if (__builtin_constant_p(e)) { char __compile_time_assert__[e ? 1 : -1] __attribute__((unused)); } else if (!launchd_assumes(e)) { abort(); } -void _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test); +INTERNAL_ABI bool _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test); typedef void (*kq_callback)(void *, struct kevent *); typedef boolean_t (*mig_callback)(mach_msg_header_t *, mach_msg_header_t *); typedef void (*timeout_callback)(void); -mach_port_t runtime_get_kernel_port(void); +INTERNAL_ABI mach_port_t runtime_get_kernel_port(void); -void runtime_add_ref(void); -void runtime_del_ref(void); +INTERNAL_ABI void runtime_add_ref(void); +INTERNAL_ABI void runtime_del_ref(void); -void launchd_runtime_init(void); -void launchd_runtime_init2(void); -void launchd_runtime(void) __attribute__((noreturn)); +INTERNAL_ABI void launchd_runtime_init(void); +INTERNAL_ABI void launchd_runtime_init2(void); +INTERNAL_ABI void launchd_runtime(void) __attribute__((noreturn)); -void launchd_log_vm_stats(void); +INTERNAL_ABI void launchd_log_vm_stats(void); -int runtime_close(int fd); -int runtime_fsync(int fd); +INTERNAL_ABI int runtime_close(int fd); +INTERNAL_ABI int runtime_fsync(int fd); #define RUNTIME_ADVISABLE_IDLE_TIMEOUT 30 -void runtime_set_timeout(timeout_callback to_cb, unsigned int sec); -kern_return_t runtime_add_mport(mach_port_t name, mig_callback demux, mach_msg_size_t msg_size); -kern_return_t runtime_remove_mport(mach_port_t name); -bool runtime_get_caller_creds(struct ldcred *ldc); +INTERNAL_ABI void runtime_set_timeout(timeout_callback to_cb, unsigned int sec); +INTERNAL_ABI kern_return_t runtime_add_mport(mach_port_t name, mig_callback demux, mach_msg_size_t msg_size); +INTERNAL_ABI kern_return_t runtime_remove_mport(mach_port_t name); +INTERNAL_ABI bool runtime_get_caller_creds(struct ldcred *ldc); -const char *signal_to_C_name(unsigned int sig); -const char *reboot_flags_to_C_names(unsigned int flags); -const char *proc_flags_to_C_names(unsigned int flags); +INTERNAL_ABI const char *signal_to_C_name(unsigned int sig); +INTERNAL_ABI const char *reboot_flags_to_C_names(unsigned int flags); +INTERNAL_ABI const char *proc_flags_to_C_names(unsigned int flags); -bool do_apple_internal_logging(void); +INTERNAL_ABI bool do_apple_internal_logging(void); -int kevent_bulk_mod(struct kevent *kev, size_t kev_cnt); -int kevent_mod(uintptr_t ident, short filter, u_short flags, u_int fflags, intptr_t data, void *udata); +INTERNAL_ABI int kevent_bulk_mod(struct kevent *kev, size_t kev_cnt); +INTERNAL_ABI int kevent_mod(uintptr_t ident, short filter, u_short flags, u_int fflags, intptr_t data, void *udata); -pid_t runtime_fork(mach_port_t bsport); +INTERNAL_ABI pid_t runtime_fork(mach_port_t bsport); -kern_return_t runtime_log_forward(uid_t forward_uid, gid_t forward_gid, vm_offset_t inval, mach_msg_type_number_t invalCnt); -kern_return_t runtime_log_drain(mach_port_t srp, vm_offset_t *outval, mach_msg_type_number_t *outvalCnt); +INTERNAL_ABI kern_return_t runtime_log_forward(uid_t forward_uid, gid_t forward_gid, vm_offset_t inval, mach_msg_type_number_t invalCnt); +INTERNAL_ABI kern_return_t runtime_log_drain(mach_port_t srp, vm_offset_t *outval, mach_msg_type_number_t *outvalCnt); #define LOG_APPLEONLY 0x4141504c /* AAPL in hex */ @@ -147,24 +150,23 @@ pid_t about_pid; }; -int runtime_setlogmask(int maskpri); -void runtime_closelog(void); -void runtime_syslog(int pri, const char *message, ...) __attribute__((format(printf, 2, 3))); -void runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) __attribute__((format(printf, 2, 0))); -void runtime_log_push(void); +INTERNAL_ABI int runtime_setlogmask(int maskpri); +INTERNAL_ABI void runtime_closelog(void); +INTERNAL_ABI void runtime_syslog(int pri, const char *message, ...) __attribute__((format(printf, 2, 3))); +INTERNAL_ABI void runtime_vsyslog(struct runtime_syslog_attr *attr, const char *message, va_list args) __attribute__((format(printf, 2, 0))); +INTERNAL_ABI void runtime_log_push(void); -int64_t runtime_get_wall_time(void); -uint64_t runtime_get_opaque_time(void); -uint64_t runtime_opaque_time_to_nano(uint64_t o); +INTERNAL_ABI int64_t runtime_get_wall_time(void); +INTERNAL_ABI uint64_t runtime_get_opaque_time(void); +INTERNAL_ABI uint64_t runtime_opaque_time_to_nano(uint64_t o); +INTERNAL_ABI kern_return_t launchd_set_bport(mach_port_t name); +INTERNAL_ABI kern_return_t launchd_get_bport(mach_port_t *name); +INTERNAL_ABI kern_return_t launchd_mport_notify_req(mach_port_t name, mach_msg_id_t which); +INTERNAL_ABI kern_return_t launchd_mport_notify_cancel(mach_port_t name, mach_msg_id_t which); +INTERNAL_ABI kern_return_t launchd_mport_create_recv(mach_port_t *name); +INTERNAL_ABI kern_return_t launchd_mport_deallocate(mach_port_t name); +INTERNAL_ABI kern_return_t launchd_mport_make_send(mach_port_t name); +INTERNAL_ABI kern_return_t launchd_mport_close_recv(mach_port_t name); -kern_return_t launchd_set_bport(mach_port_t name); -kern_return_t launchd_get_bport(mach_port_t *name); -kern_return_t launchd_mport_notify_req(mach_port_t name, mach_msg_id_t which); -kern_return_t launchd_mport_notify_cancel(mach_port_t name, mach_msg_id_t which); -kern_return_t launchd_mport_create_recv(mach_port_t *name); -kern_return_t launchd_mport_deallocate(mach_port_t name); -kern_return_t launchd_mport_make_send(mach_port_t name); -kern_return_t launchd_mport_close_recv(mach_port_t name); - #endif Modified: trunk/launchd/src/launchd_unix_ipc.c =================================================================== --- trunk/launchd/src/launchd_unix_ipc.c 2008-01-09 17:35:12 UTC (rev 23478) +++ trunk/launchd/src/launchd_unix_ipc.c 2008-01-09 22:18:09 UTC (rev 23479) @@ -418,11 +418,17 @@ rmc->resp = resp; } +static int +close_abi_fixup(int fd) +{ + return runtime_close(fd); +} + void ipc_close(struct conncb *c) { LIST_REMOVE(c, sle); - launchd_close(c->conn, runtime_close); + launchd_close(c->conn, close_abi_fixup); free(c); } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080109/6de5b36c/attachment-0001.html From source_changes at macosforge.org Fri Jan 11 07:17:19 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Fri Jan 11 07:18:48 2008 Subject: [launchd-changes] [23480] trunk/launchd/src Message-ID: <20080111151719.67805891AE0@beta.macosforge.org> Revision: 23480 http://trac.macosforge.org/projects/launchd/changeset/23480 Author: zarzycki@apple.com Date: 2008-01-11 07:17:14 -0800 (Fri, 11 Jan 2008) Log Message: ----------- Misc updates. Modified Paths: -------------- trunk/launchd/src/launchd.c trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_runtime.h trunk/launchd/src/launchd_unix_ipc.c trunk/launchd/src/launchd_unix_ipc.h Modified: trunk/launchd/src/launchd.c =================================================================== --- trunk/launchd/src/launchd.c 2008-01-09 22:18:09 UTC (rev 23479) +++ trunk/launchd/src/launchd.c 2008-01-11 15:17:14 UTC (rev 23480) @@ -82,7 +82,7 @@ extern char **environ; -static void pfsystem_callback(void *, struct kevent *); +INTERNAL_ABI static void pfsystem_callback(void *, struct kevent *); static kq_callback kqpfsystem_callback = pfsystem_callback; @@ -376,7 +376,7 @@ launchd_assumes(kevent_mod(pfs, EVFILT_READ, EV_ADD, 0, 0, &kqpfsystem_callback) != -1); } -void +INTERNAL_ABI void pfsystem_callback(void *obj __attribute__((unused)), struct kevent *kev) { bool new_networking_state; @@ -392,7 +392,7 @@ } } -INTERNAL_ABI bool +INTERNAL_ABI void _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test) { int saved_errno = errno; @@ -417,6 +417,4 @@ } runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u: %s", file, line, buf, saved_errno, test); - - return false; } Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2008-01-09 22:18:09 UTC (rev 23479) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-11 15:17:14 UTC (rev 23480) @@ -263,6 +263,7 @@ LIST_HEAD(, job_s) jobs; LIST_HEAD(, job_s) active_jobs[ACTIVE_JOB_HASH_SIZE]; LIST_HEAD(, machservice) ms_hash[MACHSERVICE_HASH_SIZE]; + LIST_HEAD(, job_s) global_env_jobs; mach_port_t jm_port; mach_port_t req_port; jobmgr_t parentmgr; @@ -275,7 +276,7 @@ }; #define jobmgr_assumes(jm, e) \ - (likely(e) ? true : jobmgr_log_bug(jm, __LINE__)) + (unlikely(!(e)) ? jobmgr_log_bug(jm, __LINE__), false : true) static jobmgr_t jobmgr_new(jobmgr_t jm, mach_port_t requestorport, mach_port_t transfer_port, bool sflag, const char *name); static job_t jobmgr_import2(jobmgr_t jm, launch_data_t pload); @@ -289,14 +290,14 @@ static job_t jobmgr_find_by_pid(jobmgr_t jm, pid_t p, bool create_anon); static job_t job_mig_intran2(jobmgr_t jm, mach_port_t mport, pid_t upid); static void job_export_all2(jobmgr_t jm, launch_data_t where); -static void jobmgr_callback(void *obj, struct kevent *kev); +INTERNAL_ABI static void jobmgr_callback(void *obj, struct kevent *kev); static void jobmgr_setup_env_from_other_jobs(jobmgr_t jm); static void jobmgr_export_env_from_other_jobs(jobmgr_t jm, launch_data_t dict); static struct machservice *jobmgr_lookup_service(jobmgr_t jm, const char *name, bool check_parent, pid_t target_pid); static void jobmgr_logv(jobmgr_t jm, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); static void jobmgr_log(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); /* static void jobmgr_log_error(jobmgr_t jm, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); */ -static bool jobmgr_log_bug(jobmgr_t jm, unsigned int line); +static void jobmgr_log_bug(jobmgr_t jm, unsigned int line); #define DO_RUSAGE_SUMMATION 0 @@ -307,6 +308,7 @@ LIST_ENTRY(job_s) sle; LIST_ENTRY(job_s) pid_hash_sle; LIST_ENTRY(job_s) label_hash_sle; + LIST_ENTRY(job_s) global_env_sle; SLIST_HEAD(, socketgroup) sockets; SLIST_HEAD(, calendarinterval) cal_intervals; SLIST_HEAD(, envitem) global_env; @@ -375,7 +377,7 @@ #define job_assumes(j, e) \ - (likely(e) ? true : job_log_bug(j, __LINE__)) + (unlikely(!(e)) ? job_log_bug(j, __LINE__), false : true) static void job_import_keys(launch_data_t obj, const char *key, void *context); static void job_import_bool(job_t j, const char *key, bool value); @@ -401,7 +403,7 @@ static void job_force_sampletool(job_t j); static void job_setup_exception_port(job_t j, task_t target_task); static void job_reparent_hack(job_t j, const char *where); -static void job_callback(void *obj, struct kevent *kev); +INTERNAL_ABI static void job_callback(void *obj, struct kevent *kev); static void job_callback_proc(job_t j, int fflags); static void job_callback_timer(job_t j, void *ident); static void job_callback_read(job_t j, int ident); @@ -417,7 +419,7 @@ static void job_log_stdouterr(job_t j); static void job_logv(job_t j, int pri, int err, const char *msg, va_list ap) __attribute__((format(printf, 4, 0))); static void job_log_error(job_t j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4))); -static bool job_log_bug(job_t j, unsigned int line); +static void job_log_bug(job_t j, unsigned int line); static void job_log_stdouterr2(job_t j, const char *msg, ...); static void job_set_exeception_port(job_t j, mach_port_t port); static kern_return_t job_handle_mpm_wait(job_t j, mach_port_t srp, int *waitstatus); @@ -680,7 +682,7 @@ } -static void +INTERNAL_ABI static void still_alive_with_check(void) { jobmgr_log(root_jobmgr, LOG_NOTICE, "Still alive with %lu/%lu children", total_children, total_anon_children); @@ -785,17 +787,18 @@ struct mspolicy *msp; struct envitem *ei; - if (j->p && j->anonymous) { - job_reap(j); - } else if (j->p) { - job_log(j, LOG_DEBUG, "Removal pended until the job exits"); + if (unlikely(j->p)) { + if (j->anonymous) { + job_reap(j); + } else { + job_log(j, LOG_DEBUG, "Removal pended until the job exits"); - if (!j->removal_pending) { - j->removal_pending = true; - job_stop(j); + if (!j->removal_pending) { + j->removal_pending = true; + job_stop(j); + } + return; } - - return; } ipc_close_all_with_job(j); @@ -866,7 +869,7 @@ if (j->prog) { free(j->prog); } - if (likely(j->argv)) { + if (j->argv) { free(j->argv); } if (j->rootdir) { @@ -1017,7 +1020,7 @@ free(argv); /* jobs can easily be denied creation during shutdown */ - if (!jr) { + if (unlikely(jr == NULL)) { goto out_bad; } @@ -1081,7 +1084,7 @@ return NULL; } - if (len != sizeof(kp)) { + if (unlikely(len != sizeof(kp))) { jobmgr_log(jm, LOG_DEBUG, "Tried to create an anonymous job for nonexistent PID: %u", anonpid); errno = ESRCH; return NULL; @@ -1096,7 +1099,7 @@ jobmgr_log(jm, LOG_DEBUG, "Tried to create an anonymous job for zombie PID %u: %s", anonpid, kp.kp_proc.p_comm); } - if (kp.kp_proc.p_flag & P_SUGID) { + if (unlikely(kp.kp_proc.p_flag & P_SUGID)) { jobmgr_log(jm, LOG_APPLEONLY, "Inconsistency: P_SUGID is set on PID %u: %s", anonpid, kp.kp_proc.p_comm); } @@ -1107,7 +1110,7 @@ kp_gid = kp.kp_eproc.e_pcred.p_rgid; kp_svgid = kp.kp_eproc.e_pcred.p_svgid; - if (kp_euid != kp_uid || kp_euid != kp_svuid || kp_uid != kp_svuid || kp_egid != kp_gid || kp_egid != kp_svgid || kp_gid != kp_svgid) { + if (unlikely(kp_euid != kp_uid || kp_euid != kp_svuid || kp_uid != kp_svuid || kp_egid != kp_gid || kp_egid != kp_svgid || kp_gid != kp_svgid)) { jobmgr_log(jm, LOG_APPLEONLY, "Inconsistency: Mixed credentials (e/r/s UID %u/%u/%u GID %u/%u/%u) detected on PID %u: %s", kp_euid, kp_uid, kp_svuid, kp_egid, kp_gid, kp_svgid, anonpid, kp.kp_proc.p_comm); } @@ -1128,13 +1131,13 @@ break; } - if (jp && !jp->anonymous && !(kp.kp_proc.p_flag & P_EXEC)) { + if (jp && !jp->anonymous && unlikely(!(kp.kp_proc.p_flag & P_EXEC))) { job_log(jp, LOG_APPLEONLY, "Performance and sanity: fork() without exec*(). Please switch to posix_spawn()"); } /* A total hack: Normally, job_new() returns an error during shutdown, but anonymous jobs are special. */ - if ((shutdown_state = jm->shutting_down)) { + if (unlikely(shutdown_state = jm->shutting_down)) { jm->shutting_down = false; } @@ -1158,14 +1161,14 @@ job_assumes(jr, mspolicy_copy(jr, jp)); } - if (shutdown_state && jm->hopefully_first_cnt == 0) { + if (unlikely(shutdown_state && jm->hopefully_first_cnt == 0)) { job_log(jr, LOG_APPLEONLY, "This process showed up to the party while all the guests were leaving. Odds are that it will have a miserable time."); } job_log(jr, LOG_DEBUG, "Created PID %u anonymously by PPID %u%s%s", anonpid, kp.kp_eproc.e_ppid, jp ? ": " : "", jp ? jp->label : ""); } - if (shutdown_state) { + if (unlikely(shutdown_state)) { jm->shutting_down = true; } @@ -1195,7 +1198,7 @@ return NULL; } - if (label == AUTO_PICK_LEGACY_LABEL) { + if (unlikely(label == AUTO_PICK_LEGACY_LABEL)) { bn = prog ? prog : basename((char *)argv[0]); /* prog for auto labels is kp.kp_kproc.p_comm */ snprintf(auto_label, sizeof(auto_label), "%s.%s", sizeof(void *) == 8 ? "0xdeadbeeffeedface" : "0xbabecafe", bn); label = auto_label; @@ -1211,7 +1214,7 @@ return NULL; } - if (label == auto_label) { + if (unlikely(label == auto_label)) { snprintf((char *)j->label, strlen(label) + 1, "%p.%s", j, bn); } else { strcpy((char *)j->label, label); @@ -1429,7 +1432,7 @@ break; } - if (!found_key) { + if (unlikely(!found_key)) { job_log(j, LOG_WARNING, "Unknown key for boolean: %s", key); } } @@ -1531,7 +1534,7 @@ break; } - if (where2put) { + if (likely(where2put)) { job_assumes(j, (*where2put = strdup(value)) != NULL); } else { job_log(j, LOG_WARNING, "Unknown key: %s", key); @@ -1821,7 +1824,7 @@ const char **argv = NULL; job_t j; - if (!launchd_assumes(pload != NULL)) { + if (!jobmgr_assumes(jm, pload != NULL)) { errno = EINVAL; return NULL; } @@ -1901,10 +1904,8 @@ job_t ji; LIST_FOREACH(ji, &label_hash[hash_label(label)], label_hash_sle) { - if (ji->removal_pending) { - continue; /* 5351245 */ - } else if (ji->mgr->shutting_down) { - continue; /* 5488633 */ + if (unlikely(ji->removal_pending || ji->mgr->shutting_down)) { + continue; /* 5351245 and 5488633 respectively */ } if (strcmp(ji->label, label) == 0) { @@ -1919,21 +1920,15 @@ job_t jobmgr_find_by_pid(jobmgr_t jm, pid_t p, bool create_anon) { - job_t ji = NULL; + job_t ji; LIST_FOREACH(ji, &jm->active_jobs[ACTIVE_JOB_HASH(p)], pid_hash_sle) { if (ji->p == p) { - break; + return ji; } } - if (ji) { - return ji; - } else if (create_anon) { - return job_new_anonymous(jm, p); - } else { - return NULL; - } + return create_anon ? job_new_anonymous(jm, p) : NULL; } job_t @@ -2013,7 +2008,7 @@ * 'j' can be invalid at this point. We should fix this up after Leopard ships. */ - if (j && j != workaround_5477111 && j->unload_at_mig_return) { + if (unlikely(j && (j != workaround_5477111) && j->unload_at_mig_return)) { job_log(j, LOG_NOTICE, "Unloading PID %u at MIG return.", j->p); job_remove(j); } @@ -2466,7 +2461,7 @@ job_log(j, LOG_ERR, "Did not die after sending SIGKILL %llu seconds ago...", td); } else { job_force_sampletool(j); - if (j->debug_before_kill) { + if (unlikely(j->debug_before_kill)) { job_log(j, LOG_NOTICE, "Exit timeout elapsed. Entering the kernel debugger."); job_assumes(j, host_reboot(mach_host_self(), HOST_REBOOT_DEBUGGER) == KERN_SUCCESS); } @@ -2506,7 +2501,7 @@ } } -void +INTERNAL_ABI void jobmgr_callback(void *obj, struct kevent *kev) { jobmgr_t jm = obj; @@ -2515,9 +2510,7 @@ switch (kev->filter) { case EVFILT_PROC: jobmgr_reap_bulk(jm, kev); - if (launchd_assumes(root_jobmgr != NULL)) { - root_jobmgr = jobmgr_do_garbage_collection(root_jobmgr); - } + root_jobmgr = jobmgr_do_garbage_collection(root_jobmgr); break; case EVFILT_SIGNAL: switch (kev->ident) { @@ -2566,7 +2559,7 @@ } } -void +INTERNAL_ABI void job_callback(void *obj, struct kevent *kev) { job_t j = obj; @@ -2887,7 +2880,7 @@ jobmgr_setup_env_from_other_jobs(jm->parentmgr); } - LIST_FOREACH(ji, &jm->jobs, sle) { + LIST_FOREACH(ji, &jm->global_env_jobs, global_env_sle) { SLIST_FOREACH(ei, &ji->global_env, sle) { setenv(ei->key, ei->value, 1); } @@ -3239,7 +3232,7 @@ } } -bool +void jobmgr_log_bug(jobmgr_t jm, unsigned int line) { static const char *file; @@ -3263,11 +3256,9 @@ } else { runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } - - return false; } -bool +void job_log_bug(job_t j, unsigned int line) { static const char *file; @@ -3291,8 +3282,6 @@ } else { runtime_syslog(LOG_NOTICE, "Bug: %s:%u (%s):%u", file, line, buf, saved_errno); } - - return false; } void @@ -3784,6 +3773,9 @@ strcpy(ei->value, v); if (global) { + if (SLIST_EMPTY(&j->global_env)) { + LIST_INSERT_HEAD(&j->mgr->global_env_jobs, j, global_env_sle); + } SLIST_INSERT_HEAD(&j->global_env, ei, sle); } else { SLIST_INSERT_HEAD(&j->env, ei, sle); @@ -3799,6 +3791,9 @@ { if (global) { SLIST_REMOVE(&j->global_env, ei, envitem, sle); + if (SLIST_EMPTY(&j->global_env)) { + LIST_REMOVE(j, global_env_sle); + } } else { SLIST_REMOVE(&j->env, ei, envitem, sle); } @@ -3950,11 +3945,11 @@ * We definitely need to revisit this after Leopard ships. Please see * launchctl.c for the other half of this hack. */ - if (j->mgr->global_on_demand_cnt > 0 && strcmp(j->label, "com.apple.kextd") != 0) { + if (unlikely(j->mgr->global_on_demand_cnt > 0 && strcmp(j->label, "com.apple.kextd") != 0)) { return false; } #else - if (j->mgr->global_on_demand_cnt > 0) { + if (unlikely(j->mgr->global_on_demand_cnt > 0)) { return false; } #endif @@ -3976,7 +3971,7 @@ continue; } if (status.mps_msgcount) { - job_log(j, LOG_DEBUG, "KeepAlive check: job restarted due to %d queued Mach messages on service: %s", + job_log(j, LOG_DEBUG, "KeepAlive check: %d queued Mach messages on service: %s", status.mps_msgcount, ms->name); return true; } @@ -4139,7 +4134,7 @@ ms->gen_num = 1; ms->per_pid = pid_local; - if (*serviceport == MACH_PORT_NULL) { + if (likely(*serviceport == MACH_PORT_NULL)) { if (!job_assumes(j, launchd_mport_create_recv(&ms->port) == KERN_SUCCESS)) { goto out_bad; } @@ -4187,16 +4182,16 @@ thread_state_flavor_t f = 0; mach_port_t exc_port = the_exception_server; - if (j->alt_exc_handler) { + if (unlikely(j->alt_exc_handler)) { ms = jobmgr_lookup_service(j->mgr, j->alt_exc_handler, true, 0); - if (ms) { + if (likely(ms)) { exc_port = machservice_port(ms); } else { job_log(j, LOG_WARNING, "Falling back to default Mach exception handler. Could not find: %s", j->alt_exc_handler); } - } else if (j->internal_exc_handler) { + } else if (unlikely(j->internal_exc_handler)) { exc_port = runtime_get_kernel_port(); - } else if (!exc_port) { + } else if (unlikely(!exc_port)) { return; } @@ -4206,7 +4201,7 @@ f = x86_THREAD_STATE; #endif - if (target_task) { + if (likely(target_task)) { job_assumes(j, task_set_exception_ports(target_task, EXC_MASK_CRASH, exc_port, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, f) == KERN_SUCCESS); } else if (getpid() == 1 && the_exception_server) { @@ -4221,7 +4216,7 @@ void job_set_exeception_port(job_t j, mach_port_t port) { - if (!the_exception_server) { + if (unlikely(!the_exception_server)) { the_exception_server = port; job_setup_exception_port(j, 0); } else { @@ -4298,13 +4293,12 @@ struct machservice *ms; mach_port_t p = MACH_PORT_NULL; - if ((ms = jobmgr_lookup_service(j->mgr, key, false, 0))) { + if (unlikely(ms = jobmgr_lookup_service(j->mgr, key, false, 0))) { job_log(j, LOG_WARNING, "Conflict with job: %s over Mach service: %s", ms->job->label, key); return; } - if ((ms = machservice_new(j, key, &p, false)) == NULL) { - job_log_error(j, LOG_WARNING, "Cannot add service: %s", key); + if (!job_assumes(j, (ms = machservice_new(j, key, &p, false)) != NULL)) { return; } @@ -4325,7 +4319,7 @@ jobmgr_do_garbage_collection(jmi); } - if (!jm->shutting_down) { + if (likely(!jm->shutting_down)) { return jm; } @@ -4393,7 +4387,7 @@ size_t i, kp_cnt = 0, kp_skipped = 0, len = 10*1024*1024; struct kinfo_proc *kp; - if (jm->parentmgr || getpid() != 1) { + if (likely(jm->parentmgr || getpid() != 1)) { return; } @@ -4413,7 +4407,7 @@ const char *z = (kp[i].kp_proc.p_stat == SZOMB) ? "zombie " : ""; const char *n = kp[i].kp_proc.p_comm; - if (p_i == 0 || p_i == 1) { + if (unlikely(p_i == 0 || p_i == 1)) { kp_skipped++; continue; } @@ -4462,7 +4456,7 @@ launchd_assert(offsetof(struct jobmgr_s, kqjobmgr_callback) == 0); - if (jm && requestorport == MACH_PORT_NULL) { + if (unlikely(jm && requestorport == MACH_PORT_NULL)) { jobmgr_log(jm, LOG_ERR, "Mach sub-bootstrap create request requires a requester port"); return NULL; } @@ -4609,7 +4603,7 @@ if (jm == root_jobmgr) { if (port == inherited_bootstrap_port) { - launchd_assumes(launchd_mport_deallocate(port) == KERN_SUCCESS); + jobmgr_assumes(jm, launchd_mport_deallocate(port) == KERN_SUCCESS); inherited_bootstrap_port = MACH_PORT_NULL; return jobmgr_shutdown(jm); @@ -4638,24 +4632,31 @@ jobmgr_lookup_service(jobmgr_t jm, const char *name, bool check_parent, pid_t target_pid) { struct machservice *ms; + job_t target_j; if (target_pid) { - jobmgr_assumes(jm, !check_parent); - } + //jobmgr_assumes(jm, !check_parent); - LIST_FOREACH(ms, &jm->ms_hash[hash_ms(name)], name_hash_sle) { - if ((target_pid && ms->per_pid && ms->job->p == target_pid) || (!target_pid && !ms->per_pid)) { - if (strcmp(name, ms->name) == 0) { + if (unlikely((target_j = jobmgr_find_by_pid(jm, target_pid, false)) == NULL)) { + return NULL; + } + + SLIST_FOREACH(ms, &target_j->machservices, sle) { + if (ms->per_pid && strcmp(name, ms->name) == 0) { return ms; } } - } - if (jm->parentmgr == NULL) { return NULL; } - if (!check_parent) { + LIST_FOREACH(ms, &jm->ms_hash[hash_ms(name)], name_hash_sle) { + if (!ms->per_pid && strcmp(name, ms->name) == 0) { + return ms; + } + } + + if (jm->parentmgr == NULL || !check_parent) { return NULL; } @@ -4695,7 +4696,7 @@ void machservice_delete(job_t j, struct machservice *ms, bool port_died) { - if (ms->debug_on_close) { + if (unlikely(ms->debug_on_close)) { job_log(j, LOG_NOTICE, "About to enter kernel debugger because of Mach port: 0x%x", ms->port); job_assumes(j, host_reboot(mach_host_self(), HOST_REBOOT_DEBUGGER) == KERN_SUCCESS); } @@ -4706,7 +4707,7 @@ job_assumes(j, launchd_mport_deallocate(ms->port) == KERN_SUCCESS); - if (ms->port == the_exception_server) { + if (unlikely(ms->port == the_exception_server)) { the_exception_server = 0; } @@ -5276,7 +5277,7 @@ /* XXX - This code should go away once the per session launchd is integrated with the rest of the system */ #ifdef LET_MERE_MORTALS_ADD_SERVERS_TO_PID1 if (getpid() == 1) { - if (ldc.euid && server_uid && (ldc.euid != server_uid)) { + if (unlikely(ldc.euid && server_uid && (ldc.euid != server_uid))) { job_log(j, LOG_WARNING, "Server create: \"%s\": Will run as UID %d, not UID %d as they told us to", server_cmd, ldc.euid, server_uid); server_uid = ldc.euid; @@ -5284,7 +5285,7 @@ } else #endif if (getuid()) { - if (server_uid != getuid()) { + if (unlikely(server_uid != getuid())) { job_log(j, LOG_WARNING, "Server create: \"%s\": As UID %d, we will not be able to switch to UID %d", server_cmd, getuid(), server_uid); } @@ -5293,7 +5294,7 @@ js = job_new_via_mach_init(j, server_cmd, server_uid, on_demand); - if (js == NULL) { + if (unlikely(js == NULL)) { return BOOTSTRAP_NO_MEMORY; } @@ -5313,11 +5314,11 @@ runtime_get_caller_creds(&ldc); - if (ldc.euid != 0 && ldc.euid != getuid()) { + if (unlikely(ldc.euid != 0 && ldc.euid != getuid())) { return BOOTSTRAP_NOT_PRIVILEGED; } - if (!(otherj = job_find(targetlabel))) { + if (unlikely(!(otherj = job_find(targetlabel)))) { return BOOTSTRAP_UNKNOWN_SERVICE; } @@ -5374,7 +5375,7 @@ runtime_get_caller_creds(&ldc); - if (ldc.euid) { + if (unlikely(ldc.euid)) { return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5398,11 +5399,11 @@ return BOOTSTRAP_NO_MEMORY; } - if (inkey && ldc.euid && ldc.euid != getuid()) { + if (unlikely(inkey && ldc.euid && ldc.euid != getuid())) { return BOOTSTRAP_NOT_PRIVILEGED; } - if (inkey && outkey && !job_assumes(j, inkey == outkey)) { + if (unlikely(inkey && outkey && !job_assumes(j, inkey == outkey))) { return 1; } @@ -5422,7 +5423,7 @@ return 1; } - if (invalCnt && !job_assumes(j, (input_obj = launch_data_unpack((void *)inval, invalCnt, NULL, 0, &data_offset, NULL)) != NULL)) { + if (unlikely(invalCnt && !job_assumes(j, (input_obj = launch_data_unpack((void *)inval, invalCnt, NULL, 0, &data_offset, NULL)) != NULL))) { goto out_bad; } @@ -5492,11 +5493,11 @@ return BOOTSTRAP_NO_MEMORY; } - if (inkey && ldc.euid && ldc.euid != getuid()) { + if (unlikely(inkey && ldc.euid && ldc.euid != getuid())) { return BOOTSTRAP_NOT_PRIVILEGED; } - if (inkey && outkey && !job_assumes(j, inkey == outkey)) { + if (unlikely(inkey && outkey && !job_assumes(j, inkey == outkey))) { return 1; } @@ -5627,7 +5628,7 @@ errno = task_set_special_port(child_task, ms->special_port_num, ms->port); - if (errno) { + if (unlikely(errno)) { int desired_log_level = LOG_ERR; if (j->anonymous) { @@ -5661,13 +5662,13 @@ return BOOTSTRAP_NO_MEMORY; } - if (getpid() != 1) { + if (unlikely(getpid() != 1)) { return BOOTSTRAP_NOT_PRIVILEGED; } runtime_get_caller_creds(&ldc); - if (ldc.euid) { + if (unlikely(ldc.euid)) { return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5702,7 +5703,7 @@ ipc_server_init(); - if (!sockpath) { + if (unlikely(!sockpath)) { return BOOTSTRAP_NO_MEMORY; } @@ -5730,7 +5731,7 @@ void ensure_root_bkgd_setup(void) { - if (background_jobmgr || getpid() != 1) { + if (likely(background_jobmgr) || getpid() != 1) { return; } @@ -5756,7 +5757,7 @@ runtime_get_caller_creds(&ldc); - if (getpid() != 1) { + if (unlikely(getpid() != 1)) { job_log(j, LOG_ERR, "Only PID 1 supports per user launchd lookups."); return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5791,7 +5792,7 @@ break; } - if (ji == NULL) { + if (unlikely(ji == NULL)) { struct machservice *ms; char lbuf[1024]; @@ -5843,12 +5844,12 @@ ms = jobmgr_lookup_service(j->mgr, servicename, true, 0); - if (ms == NULL) { + if (unlikely(ms == NULL)) { job_log(j, LOG_DEBUG, "Check-in of Mach service failed. Unknown: %s", servicename); return BOOTSTRAP_UNKNOWN_SERVICE; } - if ((jo = machservice_job(ms)) != j) { + if (unlikely((jo = machservice_job(ms)) != j)) { static pid_t last_warned_pid; if (last_warned_pid != ldc.pid) { @@ -5858,7 +5859,7 @@ return BOOTSTRAP_NOT_PRIVILEGED; } - if (machservice_active(ms)) { + if (unlikely(machservice_active(ms))) { job_log(j, LOG_WARNING, "Check-in of Mach service failed. Already active: %s", servicename); return BOOTSTRAP_SERVICE_ACTIVE; } @@ -5894,7 +5895,7 @@ * 92) is a rogue application (not our UID, not root and not a child of * us). We'll have to reconcile this design friction at a later date. */ - if (j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.uid != getuid() && ldc.uid != 92) { + if (unlikely(j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.uid != getuid() && ldc.uid != 92)) { if (getpid() == 1) { return VPROC_ERR_TRY_PER_USER; } else { @@ -5904,7 +5905,7 @@ ms = jobmgr_lookup_service(j->mgr, servicename, false, flags & BOOTSTRAP_PER_PID_SERVICE ? ldc.pid : 0); - if (ms) { + if (unlikely(ms)) { if (machservice_job(ms) != j) { return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5916,8 +5917,8 @@ machservice_delete(j, ms, false); } - if (serviceport != MACH_PORT_NULL) { - if ((ms = machservice_new(j, servicename, &serviceport, flags & BOOTSTRAP_PER_PID_SERVICE ? true : false))) { + if (likely(serviceport != MACH_PORT_NULL)) { + if (likely(ms = machservice_new(j, servicename, &serviceport, flags & BOOTSTRAP_PER_PID_SERVICE ? true : false))) { machservice_request_notifications(ms); } else { return BOOTSTRAP_NO_MEMORY; @@ -5940,11 +5941,11 @@ runtime_get_caller_creds(&ldc); - if (getpid() == 1 && j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.euid != 0) { + if (unlikely(getpid() == 1 && j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.euid != 0)) { return VPROC_ERR_TRY_PER_USER; } - if (!mspolicy_check(j, servicename, flags & BOOTSTRAP_PER_PID_SERVICE)) { + if (unlikely(!mspolicy_check(j, servicename, flags & BOOTSTRAP_PER_PID_SERVICE))) { job_log(j, LOG_NOTICE, "Policy denied Mach service lookup: %s", servicename); return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5957,15 +5958,15 @@ if (ms && machservice_hidden(ms) && !job_active(machservice_job(ms))) { ms = NULL; - } else if (ms && ms->per_user_hack) { + } else if (ms && unlikely(ms->per_user_hack)) { ms = NULL; } if (ms) { - launchd_assumes(machservice_port(ms) != MACH_PORT_NULL); + job_assumes(j, machservice_port(ms) != MACH_PORT_NULL); job_log(j, LOG_DEBUG, "%sMach service lookup: %s", flags & BOOTSTRAP_PER_PID_SERVICE ? "Per PID " : "", servicename); - if (j->lastlookup == ms && j->lastlookup_gennum == ms->gen_num && !j->per_user) { + if (unlikely(j->lastlookup == ms && j->lastlookup_gennum == ms->gen_num && !j->per_user)) { job_log(ms->job, LOG_APPLEONLY, "Performance: Please fix the framework to cache the Mach port for service: %s", servicename); } @@ -6048,12 +6049,12 @@ } mig_allocate((vm_address_t *)&service_names, cnt * sizeof(service_names[0])); - if (!launchd_assumes(service_names != NULL)) { + if (!job_assumes(j, service_names != NULL)) { goto out_bad; } mig_allocate((vm_address_t *)&service_actives, cnt * sizeof(service_actives[0])); - if (!launchd_assumes(service_actives != NULL)) { + if (!job_assumes(j, service_actives != NULL)) { goto out_bad; } @@ -6067,7 +6068,7 @@ } } - launchd_assumes(cnt == cnt2); + job_assumes(j, cnt == cnt2); out: *servicenamesp = service_names; @@ -6108,7 +6109,7 @@ } SLIST_FOREACH(jmi, &root_jobmgr->submgrs, sle) { - if (jmi->shutting_down) { + if (unlikely(jmi->shutting_down)) { continue; } else if (strcasecmp(jmi->name, where) == 0) { goto jm_found; @@ -6223,7 +6224,7 @@ */ SLIST_FOREACH(jmi, &root_jobmgr->submgrs, sle) { - if (jmi->shutting_down) { + if (unlikely(jmi->shutting_down)) { continue; } else if (strcasecmp(jmi->name, session_type) == 0) { jobmgr_shutdown(jmi); @@ -6281,13 +6282,13 @@ j_for_service = jobmgr_find_by_pid(jmr, target_pid, true); - if (!j_for_service) { + if (unlikely(!j_for_service)) { /* The PID probably exited */ job_assumes(j, launchd_mport_deallocate(l2l_ports[l2l_i]) == KERN_SUCCESS); continue; } - if ((ms = machservice_new(j_for_service, serv_name, &l2l_ports[l2l_i], serv_perpid))) { + if (likely(ms = machservice_new(j_for_service, serv_name, &l2l_ports[l2l_i], serv_perpid))) { machservice_request_notifications(ms); } } @@ -6333,16 +6334,19 @@ jm = j->mgr; - if (getpid() != 1) { + if (unlikely(getpid() != 1)) { job_log(j, LOG_ERR, "Only the system launchd will transfer Mach sub-bootstraps."); return BOOTSTRAP_NOT_PRIVILEGED; - } else if (jobmgr_parent(jm) == NULL) { + } + if (unlikely(jobmgr_parent(jm) == NULL)) { job_log(j, LOG_ERR, "Root Mach bootstrap cannot be transferred."); return BOOTSTRAP_NOT_PRIVILEGED; - } else if (strcasecmp(jm->name, VPROCMGR_SESSION_AQUA) == 0) { + } + if (unlikely(strcasecmp(jm->name, VPROCMGR_SESSION_AQUA) == 0)) { job_log(j, LOG_ERR, "Cannot transfer a setup GUI session."); return BOOTSTRAP_NOT_PRIVILEGED; - } else if (!j->anonymous) { + } + if (unlikely(!j->anonymous)) { job_log(j, LOG_ERR, "Only the anonymous job can transfer Mach sub-bootstraps."); return BOOTSTRAP_NOT_PRIVILEGED; } @@ -6370,7 +6374,7 @@ } mig_allocate((vm_address_t *)&ports, cnt * sizeof(ports[0])); - if (!launchd_assumes(ports != NULL)) { + if (!job_assumes(j, ports != NULL)) { goto out_bad; } @@ -6412,7 +6416,7 @@ } } - launchd_assumes(cnt == cnt2); + job_assumes(j, cnt == cnt2); packed_size = launch_data_pack(outdata_obj_array, (void *)*outdata, *outdataCnt, NULL, NULL); if (!job_assumes(j, packed_size != 0)) { @@ -6467,13 +6471,13 @@ } /* Since we use recursion, we need an artificial depth for subsets */ - if (bsdepth > 100) { + if (unlikely(bsdepth > 100)) { job_log(j, LOG_ERR, "Mach sub-bootstrap create request failed. Depth greater than: %d", bsdepth); return BOOTSTRAP_NO_MEMORY; } - if ((jmr = jobmgr_new(j->mgr, requestorport, MACH_PORT_NULL, false, NULL)) == NULL) { - if (requestorport == MACH_PORT_NULL) { + if (!job_assumes(j, (jmr = jobmgr_new(j->mgr, requestorport, MACH_PORT_NULL, false, NULL)) != NULL)) { + if (unlikely(requestorport == MACH_PORT_NULL)) { return BOOTSTRAP_NOT_PRIVILEGED; } return BOOTSTRAP_NO_MEMORY; @@ -6492,18 +6496,18 @@ return BOOTSTRAP_NO_MEMORY; } - if (job_prog(j)[0] == '\0') { + if (unlikely(job_prog(j)[0] == '\0')) { job_log(j, LOG_ERR, "Mach service creation requires a target server: %s", servicename); return BOOTSTRAP_NOT_PRIVILEGED; } - if (!j->legacy_mach_job) { + if (unlikely(!j->legacy_mach_job)) { job_log(j, LOG_ERR, "bootstrap_create_service() is only allowed against legacy Mach jobs: %s", servicename); return BOOTSTRAP_NOT_PRIVILEGED; } ms = jobmgr_lookup_service(j->mgr, servicename, false, 0); - if (ms) { + if (unlikely(ms)) { job_log(j, LOG_DEBUG, "Mach service creation attempt for failed. Already exists: %s", servicename); return BOOTSTRAP_NAME_IN_USE; } @@ -6513,15 +6517,11 @@ *serviceportp = MACH_PORT_NULL; ms = machservice_new(j, servicename, serviceportp, false); - if (!launchd_assumes(ms != NULL)) { - goto out_bad; + if (!job_assumes(j, ms != NULL)) { + return BOOTSTRAP_NO_MEMORY; } return BOOTSTRAP_SUCCESS; - -out_bad: - launchd_assumes(launchd_mport_close_recv(*serviceportp) == KERN_SUCCESS); - return BOOTSTRAP_NO_MEMORY; } kern_return_t @@ -6544,7 +6544,7 @@ return BOOTSTRAP_NO_MEMORY; } - if (!j->stall_before_exec) { + if (unlikely(!j->stall_before_exec)) { job_log(j, LOG_WARNING, "Attempt to uncork a job that isn't in the middle of a fork()."); return 1; } @@ -6563,6 +6563,12 @@ return BOOTSTRAP_NO_MEMORY; } + if (unlikely(!SLIST_EMPTY(&j->mspolicies))) { + job_log(j, LOG_WARNING, "Jobs that have policies assigned to them may not set policies."); + return BOOTSTRAP_NOT_PRIVILEGED; + } + + target_j = jobmgr_find_by_pid(j->mgr, target_pid, true); if (unlikely(target_j == NULL)) { @@ -6572,24 +6578,15 @@ return BOOTSTRAP_NO_MEMORY; } - if (!job_assumes(j, (target_j = jobmgr_find_by_pid(j->mgr, target_pid, true)) != NULL)) { - return BOOTSTRAP_NO_MEMORY; - } + job_log(j, LOG_DEBUG, "Setting policy on job \"%s\" for Mach service: %s", target_j->label, target_service); + if (target_service[0]) { + bool r = mspolicy_new(target_j, target_service, flags & BOOTSTRAP_ALLOW_LOOKUP, flags & BOOTSTRAP_PER_PID_SERVICE, false); - if (SLIST_EMPTY(&j->mspolicies)) { - job_log(j, LOG_DEBUG, "Setting policy on job \"%s\" for Mach service: %s", target_j->label, target_service); - if (target_service[0]) { - bool r = mspolicy_new(target_j, target_service, flags & BOOTSTRAP_ALLOW_LOOKUP, flags & BOOTSTRAP_PER_PID_SERVICE, false); - - if (unlikely(!r) && job_assumes(j, errno == EEXIST)) { - job_log(j, LOG_ERR, "Tried to update a known policy on PID %u: %s", target_pid, target_service); - } - } else { - target_j->deny_unknown_mslookups = !(flags & BOOTSTRAP_ALLOW_LOOKUP); + if (unlikely(!r) && job_assumes(j, errno == EEXIST)) { + job_log(j, LOG_ERR, "Tried to update a known policy on PID %u: %s", target_pid, target_service); } } else { - job_log(j, LOG_WARNING, "Jobs that have policies assigned to them may not set policies."); - return BOOTSTRAP_NOT_PRIVILEGED; + target_j->deny_unknown_mslookups = !(flags & BOOTSTRAP_ALLOW_LOOKUP); } return 0; @@ -6609,7 +6606,7 @@ return BOOTSTRAP_NO_MEMORY; } - if (getpid() == 1 && ldc.euid && ldc.uid) { + if (unlikely(getpid() == 1 && ldc.euid && ldc.uid)) { job_log(j, LOG_DEBUG, "Punting spawn to per-user-context"); return VPROC_ERR_TRY_PER_USER; } Modified: trunk/launchd/src/launchd_runtime.h =================================================================== --- trunk/launchd/src/launchd_runtime.h 2008-01-09 22:18:09 UTC (rev 23479) +++ trunk/launchd/src/launchd_runtime.h 2008-01-11 15:17:14 UTC (rev 23480) @@ -93,15 +93,15 @@ * Use launchd_assert() for core initialization routines. */ #define launchd_assumes(e) \ - (likely(e) ? true : _log_launchd_bug(__rcs_file_version__, __FILE__, __LINE__, #e)) + (unlikely(!(e)) ? _log_launchd_bug(__rcs_file_version__, __FILE__, __LINE__, #e), false : true) #define launchd_assert(e) if (__builtin_constant_p(e)) { char __compile_time_assert__[e ? 1 : -1] __attribute__((unused)); } else if (!launchd_assumes(e)) { abort(); } -INTERNAL_ABI bool _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test); +INTERNAL_ABI void _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test); -typedef void (*kq_callback)(void *, struct kevent *); +typedef INTERNAL_ABI void (*kq_callback)(void *, struct kevent *); typedef boolean_t (*mig_callback)(mach_msg_header_t *, mach_msg_header_t *); -typedef void (*timeout_callback)(void); +typedef INTERNAL_ABI void (*timeout_callback)(void); INTERNAL_ABI mach_port_t runtime_get_kernel_port(void); Modified: trunk/launchd/src/launchd_unix_ipc.c =================================================================== --- trunk/launchd/src/launchd_unix_ipc.c 2008-01-09 22:18:09 UTC (rev 23479) +++ trunk/launchd/src/launchd_unix_ipc.c 2008-01-11 15:17:14 UTC (rev 23480) @@ -60,8 +60,9 @@ static launch_data_t adjust_rlimits(launch_data_t in); static void ipc_readmsg2(launch_data_t data, const char *cmd, void *context); +static void ipc_readmsg(launch_data_t msg, void *context); -static void ipc_listen_callback(void *obj __attribute__((unused)), struct kevent *kev); +INTERNAL_ABI static void ipc_listen_callback(void *obj __attribute__((unused)), struct kevent *kev); static kq_callback kqipc_listen_callback = ipc_listen_callback; @@ -72,7 +73,7 @@ static bool ipc_inited = false; -void +static void ipc_clean_up(void) { if (ipc_self != getpid()) { @@ -86,7 +87,7 @@ } } -void +INTERNAL_ABI void ipc_server_init(void) { struct sockaddr_un sun; @@ -176,7 +177,7 @@ } } -void +INTERNAL_ABI void ipc_open(int fd, job_t j) { struct conncb *c = calloc(1, sizeof(struct conncb)); @@ -190,7 +191,7 @@ kevent_mod(fd, EVFILT_READ, EV_ADD, 0, 0, &c->kqconn_callback); } -void +INTERNAL_ABI void ipc_listen_callback(void *obj __attribute__((unused)), struct kevent *kev) { struct sockaddr_un sun; @@ -204,7 +205,7 @@ ipc_open(cfd, NULL); } -void +INTERNAL_ABI void ipc_callback(void *obj, struct kevent *kev) { struct conncb *c = obj; @@ -238,7 +239,7 @@ setenv(key, launch_data_get_string(obj), 1); } -void +INTERNAL_ABI void ipc_close_all_with_job(job_t j) { struct conncb *ci, *cin; @@ -250,7 +251,7 @@ } } -void +INTERNAL_ABI void ipc_close_fds(launch_data_t o) { size_t i; @@ -273,7 +274,7 @@ } } -void +INTERNAL_ABI void ipc_revoke_fds(launch_data_t o) { size_t i; @@ -424,7 +425,7 @@ return runtime_close(fd); } -void +INTERNAL_ABI void ipc_close(struct conncb *c) { LIST_REMOVE(c, sle); Modified: trunk/launchd/src/launchd_unix_ipc.h =================================================================== --- trunk/launchd/src/launchd_unix_ipc.h 2008-01-09 22:18:09 UTC (rev 23479) +++ trunk/launchd/src/launchd_unix_ipc.h 2008-01-11 15:17:14 UTC (rev 23480) @@ -35,14 +35,12 @@ extern char *sockpath; -void ipc_open(int fd, job_t j); -void ipc_close_all_with_job(job_t j); -void ipc_close(struct conncb *c); -void ipc_callback(void *, struct kevent *); -void ipc_readmsg(launch_data_t msg, void *context); -void ipc_revoke_fds(launch_data_t o); -void ipc_close_fds(launch_data_t o); -void ipc_clean_up(void); -void ipc_server_init(void); +INTERNAL_ABI void ipc_open(int fd, job_t j); +INTERNAL_ABI void ipc_close_all_with_job(job_t j); +INTERNAL_ABI void ipc_close(struct conncb *c); +INTERNAL_ABI void ipc_callback(void *, struct kevent *); +INTERNAL_ABI void ipc_revoke_fds(launch_data_t o); +INTERNAL_ABI void ipc_close_fds(launch_data_t o); +INTERNAL_ABI void ipc_server_init(void); #endif -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080111/d44ba0f8/attachment-0001.html From source_changes at macosforge.org Fri Jan 11 09:13:01 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Fri Jan 11 09:14:27 2008 Subject: [launchd-changes] [23481] trunk/launchd/src Message-ID: <20080111171301.DF1868971DD@beta.macosforge.org> Revision: 23481 http://trac.macosforge.org/projects/launchd/changeset/23481 Author: zarzycki@apple.com Date: 2008-01-11 09:12:59 -0800 (Fri, 11 Jan 2008) Log Message: ----------- Most of the time, when launchd calls getpid(), it simply wants to know whether it should do "PID 1 magic." This simplifies the code a bit. Modified Paths: -------------- trunk/launchd/src/launchd.c trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_runtime.c trunk/launchd/src/launchd_runtime.h trunk/launchd/src/launchd_unix_ipc.c Modified: trunk/launchd/src/launchd.c =================================================================== --- trunk/launchd/src/launchd.c 2008-01-11 15:17:14 UTC (rev 23480) +++ trunk/launchd/src/launchd.c 2008-01-11 17:12:59 UTC (rev 23481) @@ -134,7 +134,7 @@ setenv("PATH", _PATH_STDPATH, 1); } - if (getpid() == 1) { + if (pid1_magic) { pid1_magic_init(); } else { ipc_server_init(); @@ -142,7 +142,7 @@ monitor_networking_state(); - if (getpid() == 1) { + if (pid1_magic) { handle_pid1_crashes_separately(); } @@ -253,7 +253,7 @@ shutdown_in_progress = true; - if (getpid() == 1) { + if (pid1_magic) { /* * When this changes to a more sustainable API, update this: * http://howto.apple.com/db.cgi?Debugging_Apps_Non-Responsive_At_Shutdown Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2008-01-11 15:17:14 UTC (rev 23480) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-11 17:12:59 UTC (rev 23481) @@ -714,7 +714,7 @@ } } - if (do_apple_internal_logging() && jm->parentmgr == NULL && getpid() == 1) { + if (do_apple_internal_logging() && jm->parentmgr == NULL && pid1_magic) { runtime_set_timeout(still_alive_with_check, 5); } @@ -756,7 +756,7 @@ if (jm->parentmgr) { runtime_del_ref(); SLIST_REMOVE(&jm->parentmgr->submgrs, jm, jobmgr_s, sle); - } else if (getpid() == 1) { + } else if (pid1_magic) { jobmgr_log(jm, LOG_DEBUG, "About to call: sync()"); sync(); /* We're are going to rely on log timestamps to benchmark this call */ jobmgr_log(jm, LOG_DEBUG, "Unmounting all filesystems except / and /dev"); @@ -1120,7 +1120,7 @@ /* the kernel */ break; case 1: - if (getpid() != 1) { + if (!pid1_magic) { /* we cannot possibly find a parent job_t that is useful in this function */ break; } @@ -2524,7 +2524,7 @@ runtime_closelog(); /* HACK -- force 'start' time to be set */ - if (getpid() == 1) { + if (pid1_magic) { int64_t now = runtime_get_wall_time(); jobmgr_log(jm, LOG_NOTICE, "Anticipatory shutdown began at: %lld.%06llu", now / USEC_PER_SEC, now % USEC_PER_SEC); @@ -4204,7 +4204,7 @@ if (likely(target_task)) { job_assumes(j, task_set_exception_ports(target_task, EXC_MASK_CRASH, exc_port, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, f) == KERN_SUCCESS); - } else if (getpid() == 1 && the_exception_server) { + } else if (pid1_magic && the_exception_server) { mach_port_t mhp = mach_host_self(); job_assumes(j, host_set_exception_ports(mhp, EXC_MASK_CRASH, the_exception_server, EXCEPTION_STATE_IDENTITY | MACH_EXCEPTION_CODES, f) == KERN_SUCCESS); @@ -4254,7 +4254,7 @@ SLIST_INSERT_HEAD(&special_ports, ms, special_port_sle); break; } - } else if (strcasecmp(key, LAUNCH_JOBKEY_MACH_HOSTSPECIALPORT) == 0 && getpid() == 1) { + } else if (strcasecmp(key, LAUNCH_JOBKEY_MACH_HOSTSPECIALPORT) == 0 && pid1_magic) { if (which_port > HOST_MAX_SPECIAL_KERNEL_PORT) { job_assumes(ms->job, (errno = host_set_special_port(mhp, which_port, ms->port)) == KERN_SUCCESS); } else { @@ -4387,7 +4387,7 @@ size_t i, kp_cnt = 0, kp_skipped = 0, len = 10*1024*1024; struct kinfo_proc *kp; - if (likely(jm->parentmgr || getpid() != 1)) { + if (likely(jm->parentmgr || !pid1_magic)) { return; } @@ -4483,7 +4483,7 @@ if (transfer_port != MACH_PORT_NULL) { jobmgr_assumes(jmr, jm != NULL); jmr->jm_port = transfer_port; - } else if (!jm && getpid() != 1) { + } else if (!jm && !pid1_magic) { char *trusted_fd = getenv(LAUNCHD_TRUSTED_FD_ENV); name_t service_buf; @@ -5276,7 +5276,7 @@ #define LET_MERE_MORTALS_ADD_SERVERS_TO_PID1 /* XXX - This code should go away once the per session launchd is integrated with the rest of the system */ #ifdef LET_MERE_MORTALS_ADD_SERVERS_TO_PID1 - if (getpid() == 1) { + if (pid1_magic) { if (unlikely(ldc.euid && server_uid && (ldc.euid != server_uid))) { job_log(j, LOG_WARNING, "Server create: \"%s\": Will run as UID %d, not UID %d as they told us to", server_cmd, ldc.euid, server_uid); @@ -5662,7 +5662,7 @@ return BOOTSTRAP_NO_MEMORY; } - if (unlikely(getpid() != 1)) { + if (unlikely(!pid1_magic)) { return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5731,7 +5731,7 @@ void ensure_root_bkgd_setup(void) { - if (likely(background_jobmgr) || getpid() != 1) { + if (likely(background_jobmgr) || !pid1_magic) { return; } @@ -5757,7 +5757,7 @@ runtime_get_caller_creds(&ldc); - if (unlikely(getpid() != 1)) { + if (unlikely(!pid1_magic)) { job_log(j, LOG_ERR, "Only PID 1 supports per user launchd lookups."); return BOOTSTRAP_NOT_PRIVILEGED; } @@ -5896,7 +5896,7 @@ * us). We'll have to reconcile this design friction at a later date. */ if (unlikely(j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.uid != getuid() && ldc.uid != 92)) { - if (getpid() == 1) { + if (pid1_magic) { return VPROC_ERR_TRY_PER_USER; } else { return BOOTSTRAP_NOT_PRIVILEGED; @@ -5941,7 +5941,7 @@ runtime_get_caller_creds(&ldc); - if (unlikely(getpid() == 1 && j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.euid != 0)) { + if (unlikely(pid1_magic && j->anonymous && job_get_bs(j)->parentmgr == NULL && ldc.uid != 0 && ldc.euid != 0)) { return VPROC_ERR_TRY_PER_USER; } @@ -5981,7 +5981,7 @@ job_assumes(j, vproc_mig_look_up2_forward(inherited_bootstrap_port, srp, servicename, 0, 0) == 0); /* The previous routine moved the reply port, we're forced to return MIG_NO_REPLY now */ return MIG_NO_REPLY; - } else if (getpid() == 1 && j->anonymous && ldc.euid >= 500 && strcasecmp(job_get_bs(j)->name, VPROCMGR_SESSION_LOGINWINDOW) == 0) { + } else if (pid1_magic && j->anonymous && ldc.euid >= 500 && strcasecmp(job_get_bs(j)->name, VPROCMGR_SESSION_LOGINWINDOW) == 0) { /* * 5240036 Should start background session when a lookup of CCacheServer occurs * @@ -6113,7 +6113,7 @@ continue; } else if (strcasecmp(jmi->name, where) == 0) { goto jm_found; - } else if (strcasecmp(jmi->name, VPROCMGR_SESSION_BACKGROUND) == 0 && getpid() == 1) { + } else if (strcasecmp(jmi->name, VPROCMGR_SESSION_BACKGROUND) == 0 && pid1_magic) { SLIST_FOREACH(jmi2, &jmi->submgrs, sle) { if (strcasecmp(jmi2->name, where) == 0) { jmi = jmi2; @@ -6162,7 +6162,7 @@ job_t j2; if (j->mgr->session_initialized) { - if (ldc.uid == 0 && getpid() == 1) { + if (ldc.uid == 0 && pid1_magic) { if (strcmp(j->mgr->name, VPROCMGR_SESSION_LOGINWINDOW) == 0) { job_t ji, jn; @@ -6200,7 +6200,7 @@ kr = BOOTSTRAP_NOT_PRIVILEGED; goto out; } - } else if (ldc.uid == 0 && getpid() == 1 && strcmp(session_type, VPROCMGR_SESSION_STANDARDIO) == 0) { + } else if (ldc.uid == 0 && pid1_magic && strcmp(session_type, VPROCMGR_SESSION_STANDARDIO) == 0) { ensure_root_bkgd_setup(); SLIST_REMOVE(&j->mgr->parentmgr->submgrs, j->mgr, jobmgr_s, sle); @@ -6334,7 +6334,7 @@ jm = j->mgr; - if (unlikely(getpid() != 1)) { + if (unlikely(!pid1_magic)) { job_log(j, LOG_ERR, "Only the system launchd will transfer Mach sub-bootstraps."); return BOOTSTRAP_NOT_PRIVILEGED; } @@ -6606,7 +6606,7 @@ return BOOTSTRAP_NO_MEMORY; } - if (unlikely(getpid() == 1 && ldc.euid && ldc.uid)) { + if (unlikely(pid1_magic && ldc.euid && ldc.uid)) { job_log(j, LOG_DEBUG, "Punting spawn to per-user-context"); return VPROC_ERR_TRY_PER_USER; } @@ -6632,7 +6632,7 @@ job_reparent_hack(jr, NULL); - if (getpid() == 1) { + if (pid1_magic) { jr->mach_uid = ldc.uid; } @@ -6670,7 +6670,7 @@ INTERNAL_ABI void jobmgr_init(bool sflag) { - const char *root_session_type = getpid() == 1 ? VPROCMGR_SESSION_SYSTEM : VPROCMGR_SESSION_BACKGROUND; + const char *root_session_type = pid1_magic ? VPROCMGR_SESSION_SYSTEM : VPROCMGR_SESSION_BACKGROUND; launchd_assert((root_jobmgr = jobmgr_new(NULL, MACH_PORT_NULL, MACH_PORT_NULL, sflag, root_session_type)) != NULL); } Modified: trunk/launchd/src/launchd_runtime.c =================================================================== --- trunk/launchd/src/launchd_runtime.c 2008-01-11 15:17:14 UTC (rev 23480) +++ trunk/launchd/src/launchd_runtime.c 2008-01-11 17:12:59 UTC (rev 23481) @@ -120,8 +120,8 @@ }; static sigset_t sigign_set; static FILE *ourlogfile; +bool pid1_magic; - INTERNAL_ABI mach_port_t runtime_get_kernel_port(void) { @@ -1158,7 +1158,7 @@ { struct runtime_syslog_attr attr = { "com.apple.launchd", "com.apple.launchd", - getpid() == 1 ? "System" : "Background", + pid1_magic ? "System" : "Background", pri, getuid(), getpid(), getpid() }; va_list ap; @@ -1326,7 +1326,7 @@ if (logmsg_queue_cnt == 0) { launchd_assumes(STAILQ_EMPTY(&logmsg_queue)); return; - } else if (getpid() != 1) { + } else if (!pid1_magic) { if (runtime_log_pack(&outval, &outvalCnt) == 0) { launchd_assumes(_vprocmgr_log_forward(inherited_bootstrap_port, (void *)outval, outvalCnt) == NULL); mig_deallocate(outval, outvalCnt); @@ -1605,4 +1605,8 @@ launchd_assert(mach_timebase_info(&tbi) == 0); tbi_float_val = tbi.numer; tbi_float_val /= tbi.denom; + + if (getpid() == 1) { + pid1_magic = true; + } } Modified: trunk/launchd/src/launchd_runtime.h =================================================================== --- trunk/launchd/src/launchd_runtime.h 2008-01-11 15:17:14 UTC (rev 23480) +++ trunk/launchd/src/launchd_runtime.h 2008-01-11 17:12:59 UTC (rev 23481) @@ -103,6 +103,8 @@ typedef boolean_t (*mig_callback)(mach_msg_header_t *, mach_msg_header_t *); typedef INTERNAL_ABI void (*timeout_callback)(void); +extern bool pid1_magic; + INTERNAL_ABI mach_port_t runtime_get_kernel_port(void); INTERNAL_ABI void runtime_add_ref(void); Modified: trunk/launchd/src/launchd_unix_ipc.c =================================================================== --- trunk/launchd/src/launchd_unix_ipc.c 2008-01-11 15:17:14 UTC (rev 23480) +++ trunk/launchd/src/launchd_unix_ipc.c 2008-01-11 17:12:59 UTC (rev 23481) @@ -102,7 +102,7 @@ memset(&sun, 0, sizeof(sun)); sun.sun_family = AF_UNIX; - if (getpid() == 1) { + if (pid1_magic) { strcpy(ourdir, LAUNCHD_SOCK_PREFIX); strncpy(sun.sun_path, LAUNCHD_SOCK_PREFIX "/sock", sizeof(sun.sun_path)); @@ -458,7 +458,7 @@ continue; } - if (/* XXX readcfg_pid && */ getpid() == 1 && (i == RLIMIT_NOFILE || i == RLIMIT_NPROC)) { + if (/* XXX readcfg_pid && */ pid1_magic && (i == RLIMIT_NOFILE || i == RLIMIT_NPROC)) { int gmib[] = { CTL_KERN, KERN_MAXPROC }; int pmib[] = { CTL_KERN, KERN_MAXPROCPERUID }; const char *gstr = "kern.maxproc"; -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080111/483f6910/attachment-0001.html From source_changes at macosforge.org Mon Jan 14 09:44:47 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Mon Jan 14 09:47:23 2008 Subject: [launchd-changes] [23482] trunk/launchd/src Message-ID: <20080114174447.25E3F9C94D7@beta.macosforge.org> Revision: 23482 http://trac.macosforge.org/projects/launchd/changeset/23482 Author: zarzycki@apple.com Date: 2008-01-14 09:44:45 -0800 (Mon, 14 Jan 2008) Log Message: ----------- Misc. changes Modified Paths: -------------- trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_runtime.c Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2008-01-11 17:12:59 UTC (rev 23481) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-14 17:44:45 UTC (rev 23482) @@ -535,7 +535,7 @@ INTERNAL_ABI void job_stop(job_t j) { - if (!j->p || j->anonymous) { + if (unlikely(!j->p || j->anonymous)) { return; } @@ -1938,8 +1938,7 @@ job_t ji; if (jm->jm_port == mport) { - jobmgr_assumes(jm, (ji = jobmgr_find_by_pid(jm, upid, true)) != NULL); - return ji; + return jobmgr_find_by_pid(jm, upid, true); } SLIST_FOREACH(jmi, &jm->submgrs, sle) { @@ -2340,7 +2339,7 @@ void job_kill(job_t j) { - if (!j->p || j->anonymous) { + if (unlikely(!j->p || j->anonymous)) { return; } @@ -4850,12 +4849,6 @@ return NULL; } -INTERNAL_ABI bool -job_is_anonymous(job_t j) -{ - return j->anonymous; -} - void job_force_sampletool(job_t j) { @@ -5956,13 +5949,15 @@ ms = jobmgr_lookup_service(j->mgr, servicename, true, 0); } - if (ms && machservice_hidden(ms) && !job_active(machservice_job(ms))) { - ms = NULL; - } else if (ms && unlikely(ms->per_user_hack)) { - ms = NULL; + if (likely(ms)) { + if (machservice_hidden(ms) && !job_active(machservice_job(ms))) { + ms = NULL; + } else if (unlikely(ms->per_user_hack)) { + ms = NULL; + } } - if (ms) { + if (likely(ms)) { job_assumes(j, machservice_port(ms) != MACH_PORT_NULL); job_log(j, LOG_DEBUG, "%sMach service lookup: %s", flags & BOOTSTRAP_PER_PID_SERVICE ? "Per PID " : "", servicename); Modified: trunk/launchd/src/launchd_runtime.c =================================================================== --- trunk/launchd/src/launchd_runtime.c 2008-01-11 17:12:59 UTC (rev 23481) +++ trunk/launchd/src/launchd_runtime.c 2008-01-14 17:44:45 UTC (rev 23482) @@ -1049,16 +1049,19 @@ } /* we have another request message */ - +#if 0 if (!launchd_assumes(mig_cb_table != NULL)) { break; } +#endif the_demux = mig_cb_table[MACH_PORT_INDEX(bufRequest->Head.msgh_local_port)]; +#if 0 if (!launchd_assumes(the_demux != NULL)) { break; } +#endif record_caller_creds(&bufRequest->Head); @@ -1334,7 +1337,7 @@ return; } - if (!shutdown_in_progress && !fake_shutdown_in_progress) { + if (likely(!shutdown_in_progress && !fake_shutdown_in_progress)) { runtime_log_uncork_pending_drain(); return; } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080114/a3cdf91e/attachment.html From source_changes at macosforge.org Mon Jan 14 16:51:22 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Mon Jan 14 16:52:16 2008 Subject: [launchd-changes] [23483] trunk/launchd/src/launchd_runtime.c Message-ID: <20080115005122.793FB9E65C6@beta.macosforge.org> Revision: 23483 http://trac.macosforge.org/projects/launchd/changeset/23483 Author: zarzycki@apple.com Date: 2008-01-14 16:51:21 -0800 (Mon, 14 Jan 2008) Log Message: ----------- More misc changes. Modified Paths: -------------- trunk/launchd/src/launchd_runtime.c Modified: trunk/launchd/src/launchd_runtime.c =================================================================== --- trunk/launchd/src/launchd_runtime.c 2008-01-14 17:44:45 UTC (rev 23482) +++ trunk/launchd/src/launchd_runtime.c 2008-01-15 00:51:21 UTC (rev 23483) @@ -1021,13 +1021,16 @@ tmp_options = options; - if (unlikely(mr == MACH_SEND_INVALID_DEST || mr == MACH_SEND_TIMED_OUT)) { + /* It looks like the compiler doesn't optimize switch(unlikely(...)) */ + if (unlikely(mr)) switch (mr) { + case MACH_SEND_INVALID_DEST: + case MACH_SEND_TIMED_OUT: /* We need to clean up and start over. */ if (bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) { mach_msg_destroy(&bufReply->Head); } continue; - } else if (unlikely(mr == MACH_RCV_TIMED_OUT)) { + case MACH_RCV_TIMED_OUT: if (to != MACH_MSG_TIMEOUT_NONE) { if (runtime_busy_cnt == 0) { launchd_shutdown(); @@ -1036,7 +1039,8 @@ } } continue; - } else if (!launchd_assumes(mr == MACH_MSG_SUCCESS)) { + default: + launchd_assumes(mr == MACH_MSG_SUCCESS); continue; } @@ -1076,25 +1080,28 @@ if (the_demux(&bufRequest->Head, &bufReply->Head) == FALSE) { /* XXX - also gross */ - if (bufRequest->Head.msgh_id == MACH_NOTIFY_NO_SENDERS) { + if (likely(bufRequest->Head.msgh_id == MACH_NOTIFY_NO_SENDERS)) { notify_server(&bufRequest->Head, &bufReply->Head); } } + /* bufReply is a union. If MACH_MSGH_BITS_COMPLEX is set, then bufReply->RetCode is assumed to be zero. */ if (!(bufReply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) { - if (bufReply->RetCode == MIG_NO_REPLY) { - bufReply->Head.msgh_remote_port = MACH_PORT_NULL; - } else if ((bufReply->RetCode != KERN_SUCCESS) && (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)) { - /* destroy the request - but not the reply port */ - bufRequest->Head.msgh_remote_port = MACH_PORT_NULL; - mach_msg_destroy(&bufRequest->Head); + if (unlikely(bufReply->RetCode != KERN_SUCCESS)) { + if (likely(bufReply->RetCode == MIG_NO_REPLY)) { + bufReply->Head.msgh_remote_port = MACH_PORT_NULL; + } else if (bufRequest->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX) { + /* destroy the request - but not the reply port */ + bufRequest->Head.msgh_remote_port = MACH_PORT_NULL; + mach_msg_destroy(&bufRequest->Head); + } } } - if (bufReply->Head.msgh_remote_port != MACH_PORT_NULL) { + if (likely(bufReply->Head.msgh_remote_port != MACH_PORT_NULL)) { tmp_options |= MACH_SEND_MSG; - if (MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) != MACH_MSG_TYPE_MOVE_SEND_ONCE) { + if (unlikely(MACH_MSGH_BITS_REMOTE(bufReply->Head.msgh_bits) != MACH_MSG_TYPE_MOVE_SEND_ONCE)) { tmp_options |= MACH_SEND_TIMEOUT; } } -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080114/37a799fd/attachment.html From source_changes at macosforge.org Wed Jan 16 12:56:13 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Wed Jan 16 12:57:39 2008 Subject: [launchd-changes] [23484] trunk/launchd/src Message-ID: <20080116205613.E6EB3A4BA92@beta.macosforge.org> Revision: 23484 http://trac.macosforge.org/projects/launchd/changeset/23484 Author: zarzycki@apple.com Date: 2008-01-16 12:56:12 -0800 (Wed, 16 Jan 2008) Log Message: ----------- A simple change. Modified Paths: -------------- trunk/launchd/src/launchd.c trunk/launchd/src/launchd.h trunk/launchd/src/launchd_core_logic.c trunk/launchd/src/launchd_runtime.c trunk/launchd/src/launchd_runtime.h Modified: trunk/launchd/src/launchd.c =================================================================== --- trunk/launchd/src/launchd.c 2008-01-15 00:51:21 UTC (rev 23483) +++ trunk/launchd/src/launchd.c 2008-01-16 20:56:12 UTC (rev 23484) @@ -77,7 +77,6 @@ #define LAUNCHD_CONF ".launchd.conf" #define SECURITY_LIB "/System/Library/Frameworks/Security.framework/Versions/A/Security" -#define SHUTDOWN_LOG_DIR "/var/log/shutdown" extern char **environ; @@ -93,7 +92,6 @@ static void monitor_networking_state(void); static void fatal_signal_handler(int sig, siginfo_t *si, void *uap); static void handle_pid1_crashes_separately(void); -static void prep_shutdown_log_dir(void); static bool re_exec_in_single_user_mode; static void *crash_addr; @@ -236,12 +234,6 @@ return fd; } -void -prep_shutdown_log_dir(void) -{ - launchd_assumes(mkdir(SHUTDOWN_LOG_DIR, S_IRWXU) != -1 || errno == EEXIST); -} - INTERNAL_ABI void launchd_shutdown(void) { @@ -259,7 +251,6 @@ * http://howto.apple.com/db.cgi?Debugging_Apps_Non-Responsive_At_Shutdown */ runtime_setlogmask(LOG_UPTO(LOG_DEBUG)); - prep_shutdown_log_dir(); } runtime_log_push(); Modified: trunk/launchd/src/launchd.h =================================================================== --- trunk/launchd/src/launchd.h 2008-01-15 00:51:21 UTC (rev 23483) +++ trunk/launchd/src/launchd.h 2008-01-16 20:56:12 UTC (rev 23484) @@ -26,8 +26,6 @@ #include "libbootstrap_public.h" #include "launchd_runtime.h" -#define SHUTDOWN_LOG_DIR "/var/log/shutdown" - struct kevent; struct conncb; Modified: trunk/launchd/src/launchd_core_logic.c =================================================================== --- trunk/launchd/src/launchd_core_logic.c 2008-01-15 00:51:21 UTC (rev 23483) +++ trunk/launchd/src/launchd_core_logic.c 2008-01-16 20:56:12 UTC (rev 23484) @@ -96,6 +96,7 @@ #define LAUNCHD_DEFAULT_EXIT_TIMEOUT 20 #define LAUNCHD_SIGKILL_TIMER 5 +#define SHUTDOWN_LOG_DIR "/var/log/shutdown" #define TAKE_SUBSET_NAME "TakeSubsetName" #define TAKE_SUBSET_PID "TakeSubsetPID" @@ -714,7 +715,7 @@ } } - if (do_apple_internal_logging() && jm->parentmgr == NULL && pid1_magic) { + if (do_apple_internal_logging && jm->parentmgr == NULL && pid1_magic) { runtime_set_timeout(still_alive_with_check, 5); } @@ -4862,10 +4863,14 @@ int wstatus; pid_t sp; - if (!do_apple_internal_logging()) { + if (!do_apple_internal_logging) { return; } + if (!job_assumes(j, mkdir(SHUTDOWN_LOG_DIR, S_IRWXU) != -1 || errno == EEXIST)) { + return; + } + snprintf(pidstr, sizeof(pidstr), "%u", j->p); snprintf(logfile, sizeof(logfile), SHUTDOWN_LOG_DIR "/%s-%u.sample.txt", j->label, j->p); Modified: trunk/launchd/src/launchd_runtime.c =================================================================== --- trunk/launchd/src/launchd_runtime.c 2008-01-15 00:51:21 UTC (rev 23483) +++ trunk/launchd/src/launchd_runtime.c 2008-01-16 20:56:12 UTC (rev 23484) @@ -121,7 +121,9 @@ static sigset_t sigign_set; static FILE *ourlogfile; bool pid1_magic; +bool do_apple_internal_logging; + INTERNAL_ABI mach_port_t runtime_get_kernel_port(void) { @@ -1145,7 +1147,7 @@ runtime_fsync(int fd) { #if 0 - if (do_apple_internal_logging()) { + if (do_apple_internal_logging) { return fcntl(fd, F_FULLFSYNC, NULL); } else { return fsync(fd); @@ -1187,7 +1189,7 @@ char newmsg[10000]; if (attr->priority == LOG_APPLEONLY) { - if (do_apple_internal_logging()) { + if (do_apple_internal_logging) { attr->priority = LOG_NOTICE; } else { return; @@ -1552,19 +1554,6 @@ launchd_mport_deallocate(mhs); } -INTERNAL_ABI bool -do_apple_internal_logging(void) -{ - static int apple_internal_logging = 1; - struct stat sb; - - if (unlikely(apple_internal_logging == 1)) { - apple_internal_logging = stat("/AppleInternal", &sb); - } - - return (apple_internal_logging == 0); -} - INTERNAL_ABI int64_t runtime_get_wall_time(void) { @@ -1612,6 +1601,8 @@ void do_file_init(void) { + struct stat sb; + launchd_assert(mach_timebase_info(&tbi) == 0); tbi_float_val = tbi.numer; tbi_float_val /= tbi.denom; @@ -1619,4 +1610,8 @@ if (getpid() == 1) { pid1_magic = true; } + + if (stat("/AppleInternal", &sb) != -1) { + do_apple_internal_logging = true; + } } Modified: trunk/launchd/src/launchd_runtime.h =================================================================== --- trunk/launchd/src/launchd_runtime.h 2008-01-15 00:51:21 UTC (rev 23483) +++ trunk/launchd/src/launchd_runtime.h 2008-01-16 20:56:12 UTC (rev 23484) @@ -104,6 +104,7 @@ typedef INTERNAL_ABI void (*timeout_callback)(void); extern bool pid1_magic; +extern bool do_apple_internal_logging; INTERNAL_ABI mach_port_t runtime_get_kernel_port(void); @@ -130,8 +131,6 @@ INTERNAL_ABI const char *reboot_flags_to_C_names(unsigned int flags); INTERNAL_ABI const char *proc_flags_to_C_names(unsigned int flags); -INTERNAL_ABI bool do_apple_internal_logging(void); - INTERNAL_ABI int kevent_bulk_mod(struct kevent *kev, size_t kev_cnt); INTERNAL_ABI int kevent_mod(uintptr_t ident, short filter, u_short flags, u_int fflags, intptr_t data, void *udata); -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20080116/f295995e/attachment-0001.html From source_changes at macosforge.org Fri Jan 18 09:49:36 2008 From: source_changes at macosforge.org (source_changes@macosforge.org) Date: Fri Jan 18 09:51:00 2008 Subject: [launchd-changes] [23485] branches/SULeopard/launchd/src Message-ID: <20080118174936.7D345AEA56B@beta.macosforge.org> Revision: 23485 http://trac.macosforge.org/projects/launchd/changeset/23485 Author: zarzycki@apple.com Date: 2008-01-18 09:49:34 -0800 (Fri, 18 Jan 2008) Log Message: ----------- SpringBoard needs custom launchd job management API Modified Paths: -------------- branches/SULeopard/launchd/src/launchd_core_logic.c branches/SULeopard/launchd/src/launchd_runtime.h branches/SULeopard/launchd/src/liblaunch_public.h branches/SULeopard/launchd/src/libvproc.c branches/SULeopard/launchd/src/libvproc_private.h branches/SULeopard/launchd/src/protocol_job.defs Modified: branches/SULeopard/launchd/src/launchd_core_logic.c =================================================================== --- branches/SULeopard/launchd/src/launchd_core_logic.c 2008-01-16 20:56:12 UTC (rev 23484) +++ branches/SULeopard/launchd/src/launchd_core_logic.c 2008-01-18 17:49:34 UTC (rev 23485) @@ -322,7 +322,7 @@ cpu_type_t *j_binpref; size_t j_binpref_cnt; mach_port_t j_port; - mach_port_t wait_reply_port; + mach_port_t wait_reply_port; /* we probably should switch to a list of waiters */ uid_t mach_uid; jobmgr_t mgr; char **argv; @@ -361,7 +361,7 @@ currently_ignored:1, forced_peers_to_demand_mode:1, setnice:1, hopefully_exits_last:1, removal_pending:1, wait4pipe_eof:1, sent_sigkill:1, debug_before_kill:1, weird_bootstrap:1, start_on_mount:1, per_user:1, hopefully_exits_first:1, deny_unknown_mslookups:1, unload_at_mig_return:1, abandon_pg:1, - poll_for_vfs_changes:1, internal_exc_handler:1; + poll_for_vfs_changes:1, internal_exc_handler:1, can_kickstart:1; const char label[0]; }; @@ -1568,12 +1568,41 @@ } } +static void +policy_setup(launch_data_t obj, const char *key, void *context) +{ + job_t j = context; + bool found_key = false; + + switch (key[0]) { + case 'c': + case 'C': + if (strcasecmp(key, LAUNCH_JOBPOLICY_CANKICKSTARTOTHERJOBS) == 0) { + j->can_kickstart = launch_data_get_bool(obj); + found_key = true; + } + break; + default: + break; + } + + if (unlikely(!found_key)) { + job_log(j, LOG_WARNING, "Unknown policy: %s", key); + } +} + void job_import_dictionary(job_t j, const char *key, launch_data_t value) { launch_data_t tmp; switch (key[0]) { + case 'p': + case 'P': + if (strcasecmp(key, LAUNCH_JOBKEY_POLICIES) == 0) { + launch_data_dict_iterate(value, policy_setup, j); + } + break; case 'k': case 'K': if (strcasecmp(key, LAUNCH_JOBKEY_KEEPALIVE) == 0) { @@ -6367,6 +6396,61 @@ } kern_return_t +job_mig_embedded_wait(job_t j, name_t targetlabel, integer_t *waitstatus) +{ + job_t otherj; + + if (!launchd_assumes(j != NULL)) { + return BOOTSTRAP_NO_MEMORY; + } + + if (unlikely(!(otherj = job_find(targetlabel)))) { + return BOOTSTRAP_UNKNOWN_SERVICE; + } + + *waitstatus = j->last_exit_status; + + return 0; +} + +kern_return_t +job_mig_embedded_kickstart(job_t j, name_t targetlabel, pid_t *out_pid, mach_port_t *out_name_port) +{ + struct ldcred ldc; + kern_return_t kr; + job_t otherj; + + if (!launchd_assumes(j != NULL)) { + return BOOTSTRAP_NO_MEMORY; + } + + runtime_get_caller_creds(&ldc); + + if (!j->can_kickstart || (ldc.euid != 0 && ldc.euid != geteuid())) { + return BOOTSTRAP_NOT_PRIVILEGED; + } + + if (unlikely(!(otherj = job_find(targetlabel)))) { + return BOOTSTRAP_UNKNOWN_SERVICE; + } + + otherj = job_dispatch(otherj, true); + + if (!job_assumes(j, otherj && otherj->p)) { + return BOOTSTRAP_NO_MEMORY; + } + + kr = task_name_for_pid(mach_task_self(), otherj->p, out_name_port); + if (!job_assumes(j, kr == 0)) { + return kr; + } + + *out_pid = otherj->p; + + return 0; +} + +kern_return_t job_mig_wait(job_t j, mach_port_t srp, integer_t *waitstatus) { if (!launchd_assumes(j != NULL)) { Modified: branches/SULeopard/launchd/src/launchd_runtime.h =================================================================== --- branches/SULeopard/launchd/src/launchd_runtime.h 2008-01-16 20:56:12 UTC (rev 23484) +++ branches/SULeopard/launchd/src/launchd_runtime.h 2008-01-18 17:49:34 UTC (rev 23485) @@ -50,6 +50,9 @@ #define launchd_assert(e) if (__builtin_constant_p(e)) { char __compile_time_assert__[e ? 1 : -1] __attribute__((unused)); } else if (!launchd_assumes(e)) { abort(); } +#define likely(x) __builtin_expect((bool)(x), true) +#define unlikely(x) __builtin_expect((bool)(x), false) + void _log_launchd_bug(const char *rcs_rev, const char *path, unsigned int line, const char *test); typedef void (*kq_callback)(void *, struct kevent *); Modified: branches/SULeopard/launchd/src/liblaunch_public.h =================================================================== --- branches/SULeopard/launchd/src/liblaunch_public.h 2008-01-16 20:56:12 UTC (rev 23484) +++ branches/SULeopard/launchd/src/liblaunch_public.h 2008-01-18 17:49:34 UTC (rev 23485) @@ -99,7 +99,10 @@ #define LAUNCH_JOBKEY_THROTTLEINTERVAL "ThrottleInterval" #define LAUNCH_JOBKEY_LAUNCHONLYONCE "LaunchOnlyOnce" #define LAUNCH_JOBKEY_ABANDONPROCESSGROUP "AbandonProcessGroup" +#define LAUNCH_JOBKEY_POLICIES "Policies" +#define LAUNCH_JOBPOLICY_CANKICKSTARTOTHERJOBS "CanKickStartOtherJobs" + #define LAUNCH_JOBINETDCOMPATIBILITY_WAIT "Wait" #define LAUNCH_JOBKEY_MACH_RESETATCLOSE "ResetAtClose" Modified: branches/SULeopard/launchd/src/libvproc.c =================================================================== --- branches/SULeopard/launchd/src/libvproc.c 2008-01-16 20:56:12 UTC (rev 23484) +++ branches/SULeopard/launchd/src/libvproc.c 2008-01-18 17:49:34 UTC (rev 23485) @@ -553,6 +553,26 @@ } vproc_err_t +_vproc_kickstart_by_label(const char *label, pid_t *out_pid, mach_port_t *out_port_name) +{ + if (vproc_mig_embedded_kickstart(bootstrap_port, (char *)label, out_pid, out_port_name) == 0) { + return NULL; + } + + return (vproc_err_t)_vproc_kickstart_by_label; +} + +vproc_err_t +_vproc_wait_by_label(const char *label, int *out_wstatus) +{ + if (vproc_mig_embedded_wait(bootstrap_port, (char *)label, out_wstatus) == 0) { + return NULL; + } + + return (vproc_err_t)_vproc_wait_by_label; +} + +vproc_err_t _vproc_set_global_on_demand(bool state) { int64_t val = state ? ~0 : 0; Modified: branches/SULeopard/launchd/src/libvproc_private.h =================================================================== --- branches/SULeopard/launchd/src/libvproc_private.h 2008-01-16 20:56:12 UTC (rev 23484) +++ branches/SULeopard/launchd/src/libvproc_private.h 2008-01-18 17:49:34 UTC (rev 23485) @@ -61,6 +61,8 @@ vproc_err_t _vprocmgr_log_drain(vproc_t vp, pthread_mutex_t *optional_mutex_around_callback, _vprocmgr_log_drain_callback_t func); vproc_err_t _vproc_send_signal_by_label(const char *label, int sig); +vproc_err_t _vproc_kickstart_by_label(const char *label, pid_t *out_pid, mach_port_t *out_port_name); +vproc_err_t _vproc_wait_by_label(const char *label, int *out_wstatus); void _vproc_log(int pri, const char *msg, ...) __attribute__((format(printf, 2, 3))); void _vproc_log_error(int pri, const char *msg, ...) __attribute__((format(printf, 2, 3))); Modified: branches/SULeopard/launchd/src/protocol_job.defs =================================================================== --- branches/SULeopard/launchd/src/protocol_job.defs 2008-01-16 20:56:12 UTC (rev 23484) +++ branches/SULeopard/launchd/src/protocol_job.d