--- launchd/src/Makefile.in.orig	2005-09-29 19:22:38.000000000 +0100
+++ launchd/src/Makefile.in	2005-09-29 19:22:40.000000000 +0100
@@ -106,7 +106,7 @@
 SystemStarter_SOURCES = StartupItems.c IPC.c SystemStarter.c
 
 launchd_CFLAGS = -DPID1_REAP_ADOPTED_CHILDREN -mdynamic-no-pic $(AM_CFLAGS) -Wno-unused-parameter
-launchd_LDFLAGS = -lbsm
+launchd_LDFLAGS = -lbsm -framework IOKit -framework CoreFoundation
 launchd_SOURCES = launchd.c init.c bootstrap.c lists.c rpc_services.c bootstrapServer.c
 
 launchproxy_LDFLAGS = -weak_framework Security
--- launchd/src/launch.h.orig	2005-09-29 18:45:46.000000000 +0100
+++ launchd/src/launch.h	2005-09-29 18:50:17.000000000 +0100
@@ -71,6 +71,9 @@
 #define LAUNCH_JOBKEY_STARTCALENDARINTERVAL	"StartCalendarInterval"
 #define LAUNCH_JOBKEY_BONJOURFDS		"BonjourFDs"
 
+// added by leeg for Power Management support
+#define LAUNCH_JOBKEY_NOTONBATTERY		"NotOnBattery"
+
 #define LAUNCH_JOBINETDCOMPATIBILITY_WAIT	"Wait"
 
 #define LAUNCH_JOBKEY_CAL_MINUTE		"Minute"
--- launchd/src/launchd.c.orig	2005-09-29 17:46:58.000000000 +0100
+++ launchd/src/launchd.c	2005-09-30 14:02:42.000000000 +0100
@@ -61,6 +61,19 @@
 #include <dlfcn.h>
 #include <dirent.h>
 
+//includes for Power Management support
+#include <mach/mach_init.h>
+#include <IOKit/IOKitLib.h>
+#include <IOKit/pwr_mgt/IOPMLib.h>
+#include <IOKit/pwr_mgt/IOPM.h>
+#include <IOKit/IOReturn.h>
+#include <CoreFoundation/CFArray.h>
+#include <CoreFoundation/CFBase.h>
+#include <CoreFoundation/CFNumber.h>
+#include <CoreFoundation/CFData.h>
+#include <CoreFoundation/CoreFoundation.h>
+
+
 #include "launch.h"
 #include "launch_priv.h"
 #include "launchd.h"
@@ -144,6 +157,9 @@
 static void job_log(struct jobcb *j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4)));
 static void job_log_error(struct jobcb *j, int pri, const char *msg, ...) __attribute__((format(printf, 3, 4)));
 
+//added for power management support
+static bool launch_am_on_battery(void);
+
 static void ipc_open(int fd, struct jobcb *j);
 static void ipc_close(struct conncb *c);
 static void ipc_callback(void *, struct kevent *);
@@ -1553,6 +1569,7 @@
 	int spair[2];
 	int execspair[2];
 	bool sipc;
+	bool notonbattery = job_get_bool(j->ldj, LAUNCH_JOBKEY_NOTONBATTERY);
 	char nbuf[64];
 	pid_t c;
 
@@ -1562,6 +1579,11 @@
 		job_log(j, LOG_DEBUG, "already running");
 		return;
 	}
+	
+	if(notonbattery && launch_am_on_battery()) {
+		job_log(j, LOG_DEBUG, "not running as we're on battery power");
+		return;
+	}
 
 	j->checkedin = false;
 
@@ -2400,3 +2422,40 @@
 		syslog(LOG_DEBUG, "unexpected: kevent() returned something != 0, -1 or 1");
 	}
 }
+
+/* this is where the Power Management facility is tested
+ * shamelessly (and rather directly) based on Apple's version of Paul Vixie's Cron (see cron/do_command.c)
+ * --leeg
+ */
+static bool launch_am_on_battery(void)
+{
+	CFArrayRef pmcfarray;
+	static mach_port_t master=0;
+	static io_connect_t pmcon=0;
+
+	syslog(LOG_DEBUG,"launch_am_on_battery()");
+
+	IOMasterPort(bootstrap_port, &master);
+	pmcon = IOPMFindPowerManagement(master);
+
+	if( IOPMCopyBatteryInfo(master, &pmcfarray) == kIOReturnSuccess)
+	{
+		CFDictionaryRef dict;
+		CFNumberRef cfnum;
+		int flags;
+		
+		dict = CFArrayGetValueAtIndex(pmcfarray, 0);
+		cfnum = CFDictionaryGetValue(dict, CFSTR(kIOBatteryFlagsKey));
+		CFNumberGetValue(cfnum, kCFNumberLongType, &flags);
+		
+		if( !(flags & kIOBatteryChargerConnect) )
+		{
+			return true;
+		}
+		else
+		{
+			return false;
+		}
+	}
+	return false;
+}

Raw Source