[launchd-changes] [23481] trunk/launchd/src

source_changes at macosforge.org source_changes at macosforge.org
Fri Jan 11 09:13:01 PST 2008


Revision: 23481
          http://trac.macosforge.org/projects/launchd/changeset/23481
Author:   zarzycki at 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


More information about the launchd-changes mailing list