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

source_changes at macosforge.org source_changes at macosforge.org
Wed Sep 12 17:48:46 PDT 2007


Revision: 23376
          http://trac.macosforge.org/projects/launchd/changeset/23376
Author:   zarzycki at apple.com
Date:     2007-09-12 17:48:46 -0700 (Wed, 12 Sep 2007)

Log Message:
-----------
<rdar://problem/5475980> The per-user launchd needs to idle exit

Modified Paths:
--------------
    trunk/launchd/src/launchd_core_logic.c
    trunk/launchd/src/launchd_runtime.c
    trunk/launchd/src/launchd_runtime.h

Modified: trunk/launchd/src/launchd_core_logic.c
===================================================================
--- trunk/launchd/src/launchd_core_logic.c	2007-09-13 00:35:57 UTC (rev 23375)
+++ trunk/launchd/src/launchd_core_logic.c	2007-09-13 00:48:46 UTC (rev 23376)
@@ -93,7 +93,6 @@
 #include "job_reply.h"
 
 #define LAUNCHD_MIN_JOB_RUN_TIME 10
-#define LAUNCHD_ADVISABLE_IDLE_TIMEOUT 30
 #define LAUNCHD_DEFAULT_EXIT_TIMEOUT 20
 #define LAUNCHD_SIGKILL_TIMER 5
 
@@ -450,7 +449,7 @@
 static void do_file_init(void) __attribute__((constructor));
 
 /* file local globals */
-static unsigned int total_children;
+static size_t total_children;
 static mach_port_t the_exception_server;
 static bool did_first_per_user_launchd_BootCache_hack;
 static jobmgr_t background_jobmgr;
@@ -646,7 +645,7 @@
 static void
 still_alive_with_check(void)
 {
-	jobmgr_log(root_jobmgr, LOG_NOTICE, "Still alive with %u children.", total_children);
+	jobmgr_log(root_jobmgr, LOG_NOTICE, "Still alive with %lu children.", total_children);
 
 	runtime_closelog(); /* hack to flush logs */
 }
@@ -740,6 +739,10 @@
 	struct mspolicy *msp;
 	struct envitem *ei;
 
+	if (j == workaround_5477111) {
+		job_log(j, LOG_NOTICE, "@@@@@ Tried to remove ahead of schedule!");
+	}
+
 	if (j->p && j->anonymous) {
 		job_reap(j);
 	} else if (j->p) {
@@ -837,6 +840,7 @@
 		free(j->j_binpref);
 	}
 	if (j->start_interval) {
+		runtime_del_ref();
 		job_assumes(j, kevent_mod((uintptr_t)&j->start_interval, EVFILT_TIMER, EV_DELETE, 0, 0, NULL) != -1);
 	}
 
@@ -1122,7 +1126,7 @@
 	j->kqjob_callback = job_callback;
 	j->mgr = jm;
 	j->min_run_time = LAUNCHD_MIN_JOB_RUN_TIME;
-	j->timeout = LAUNCHD_ADVISABLE_IDLE_TIMEOUT;
+	j->timeout = RUNTIME_ADVISABLE_IDLE_TIMEOUT;
 	j->exit_timeout = LAUNCHD_DEFAULT_EXIT_TIMEOUT;
 	j->currently_ignored = true;
 	j->ondemand = true;
@@ -1460,6 +1464,7 @@
 			if (value <= 0) {
 				job_log(j, LOG_WARNING, "StartInterval is not greater than zero, ignoring");
 			} else {
+				runtime_add_ref();
 				j->start_interval = value;
 			}
 			if (-1 == kevent_mod((uintptr_t)&j->start_interval, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, value, j)) {
@@ -1950,6 +1955,9 @@
 		kevent_mod((uintptr_t)&j->exit_timeout, EVFILT_TIMER, EV_DELETE, 0, 0, NULL);
 	}
 
+	if (!j->anonymous) {
+		runtime_del_ref();
+	}
 	total_children--;
 	LIST_REMOVE(j, pid_hash_sle);
 
@@ -2388,6 +2396,7 @@
 
 		j->start_pending = false;
 
+		runtime_add_ref();
 		total_children++;
 		LIST_INSERT_HEAD(&j->mgr->active_jobs[ACTIVE_JOB_HASH(c)], j, pid_hash_sle);
 
@@ -3262,6 +3271,8 @@
 	
 	calendarinterval_setalarm(j, ci);
 
+	runtime_add_ref();
+
 	return true;
 }
 
@@ -3272,6 +3283,8 @@
 	LIST_REMOVE(ci, global_sle);
 
 	free(ci);
+
+	runtime_del_ref();
 }
 
 void
@@ -3329,6 +3342,8 @@
 
 	SLIST_INSERT_HEAD(&j->sockets, sg, sle);
 
+	runtime_add_ref();
+
 	return true;
 }
 
@@ -3345,6 +3360,8 @@
 
 	free(sg->fds);
 	free(sg);
+
+	runtime_del_ref();
 }
 
 void
@@ -3542,7 +3559,7 @@
 		job_log(j, LOG_DEBUG, "Exited while removal was pending.");
 		return true;
 	} else if (j->mgr->shutting_down) {
-		job_log(j, LOG_DEBUG, "Exited while shutdown in progress. Processes remaining: %u", total_children);
+		job_log(j, LOG_DEBUG, "Exited while shutdown in progress. Processes remaining: %lu", total_children);
 		return true;
 	} else if (j->legacy_mach_job) {
 		if (SLIST_EMPTY(&j->machservices)) {
@@ -4559,6 +4576,8 @@
 
 	SLIST_INSERT_HEAD(&j->semaphores, si, sle);
 
+	runtime_add_ref();
+
 	return true;
 }
 
@@ -4572,6 +4591,8 @@
 	}
 
 	free(si);
+
+	runtime_del_ref();
 }
 
 void
@@ -5122,10 +5143,16 @@
 		break;
 	case VPROC_GSK_START_INTERVAL:
 		if ((unsigned int)inval > 0) {
+			if (j->start_interval == 0) {
+				runtime_add_ref();
+			}
 			j->start_interval = inval;
 			job_assumes(j, kevent_mod((uintptr_t)&j->start_interval, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, j->start_interval, j) != -1);
 		} else if (j->start_interval) {
 			job_assumes(j, kevent_mod((uintptr_t)&j->start_interval, EVFILT_TIMER, EV_DELETE, 0, 0, NULL) != -1);
+			if (j->start_interval != 0) {
+				runtime_del_ref();
+			}
 			j->start_interval = 0;
 		}
 		break;

Modified: trunk/launchd/src/launchd_runtime.c
===================================================================
--- trunk/launchd/src/launchd_runtime.c	2007-09-13 00:35:57 UTC (rev 23375)
+++ trunk/launchd/src/launchd_runtime.c	2007-09-13 00:48:46 UTC (rev 23376)
@@ -90,9 +90,10 @@
 static mach_msg_size_t max_msg_size;
 static mig_callback *mig_cb_table;
 static size_t mig_cb_table_sz;
-static timeout_callback runtime_idle_callback;
-static mach_msg_timeout_t runtime_idle_timeout;
+static timeout_callback runtime_idle_callback = launchd_shutdown;
+static mach_msg_timeout_t runtime_idle_timeout = RUNTIME_ADVISABLE_IDLE_TIMEOUT * 1000;
 static audit_token_t *au_tok;
+static size_t runtime_busy_cnt;
 
 
 static STAILQ_HEAD(, logmsg_s) logmsg_queue = STAILQ_HEAD_INITIALIZER(logmsg_queue);
@@ -635,15 +636,15 @@
 
 
 void
-runtime_set_timeout(timeout_callback to_cb, mach_msg_timeout_t to)
+runtime_set_timeout(timeout_callback to_cb, unsigned int sec)
 {
-	if (to == 0 || to_cb == NULL) {
+	if (sec == 0 || to_cb == NULL) {
 		runtime_idle_callback = NULL;
 		runtime_idle_timeout = 0;
 	}
 
 	runtime_idle_callback = to_cb;
-	runtime_idle_timeout = to;
+	runtime_idle_timeout = sec * 1000;
 }
 
 kern_return_t
@@ -911,7 +912,7 @@
 			}
 		}
 
-		if ((tmp_options & MACH_RCV_MSG) && runtime_idle_callback) {
+		if ((tmp_options & MACH_RCV_MSG) && runtime_idle_callback && (runtime_busy_cnt == 0)) {
 			tmp_options |= MACH_RCV_TIMEOUT;
 
 			if (!(tmp_options & MACH_SEND_TIMEOUT)) {
@@ -1349,3 +1350,15 @@
 
 	return runtime_log_pack(outval, outvalCnt);
 }
+
+void
+runtime_add_ref(void)
+{
+	runtime_busy_cnt++;
+}
+
+void
+runtime_del_ref(void)
+{
+	runtime_busy_cnt--;
+}

Modified: trunk/launchd/src/launchd_runtime.h
===================================================================
--- trunk/launchd/src/launchd_runtime.h	2007-09-13 00:35:57 UTC (rev 23375)
+++ trunk/launchd/src/launchd_runtime.h	2007-09-13 00:48:46 UTC (rev 23376)
@@ -56,6 +56,9 @@
 
 boolean_t launchd_internal_demux(mach_msg_header_t *Request, mach_msg_header_t *Reply);
 
+void runtime_add_ref(void);
+void runtime_del_ref(void);
+
 void launchd_runtime_init(void);
 void launchd_runtime_init2(void);
 void launchd_runtime(void) __attribute__((noreturn));
@@ -63,7 +66,9 @@
 int runtime_close(int fd);
 int runtime_fsync(int fd);
 
-void runtime_set_timeout(timeout_callback to_cb, mach_msg_timeout_t to);
+#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);

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/launchd-changes/attachments/20070912/e84a22bf/attachment.html


More information about the launchd-changes mailing list