/*
- * "$Id: sysman.c 7676 2008-06-18 23:42:37Z mike $"
+ * "$Id$"
*
- * System management definitions for the Common UNIX Printing System (CUPS).
+ * System management functions for the CUPS scheduler.
*
- * Copyright 2007-2008 by Apple Inc.
+ * Copyright 2007-2013 by Apple Inc.
* Copyright 2006 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* write.
* cupsdSetBusyState() - Let the system know when we are busy
* doing something.
+ * cupsdAllowSleep() - Tell the OS it is now OK to sleep.
* cupsdStartSystemMonitor() - Start monitoring for system change.
* cupsdStopSystemMonitor() - Stop monitoring for system change.
* sysEventThreadEntry() - A thread to receive power and computer
*/
#include "cupsd.h"
+#ifdef HAVE_VPROC_TRANSACTION_BEGIN
+# include <vproc.h>
+#endif /* HAVE_VPROC_TRANSACTION_BEGIN */
+#ifdef __APPLE__
+# include <IOKit/pwr_mgt/IOPMLib.h>
+# ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
+# include <IOKit/pwr_mgt/IOPMLibPrivate.h>
+# else
+# define kIOPMAssertionTypeDenySystemSleep CFSTR("DenySystemSleep")
+# endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
+#endif /* __APPLE__ */
/*
*
* Power management support is currently only implemented on MacOS X, but
* essentially we use four functions to let the OS know when it is OK to
- * put the system to idle sleep, typically when we are not in the middle of
+ * put the system to sleep, typically when we are not in the middle of
* printing a job.
*
* Once put to sleep, we invalidate all remote printers since it is common
* to wake up in a new location/on a new wireless network.
*/
+/*
+ * Local globals...
+ */
+
+#if defined(kIOPMAssertionTypeDenySystemSleep) || defined(kIOPMAssertNetworkClientActive)
+static IOPMAssertionID keep_awake = 0; /* Keep the system awake while printing */
+#endif /* kIOPMAssertionTypeDenySystemSleep || kIOPMAssertNetworkClientActive */
+
/*
* 'cupsdCleanDirty()' - Write dirty config and state files.
if (DirtyFiles & CUPSD_DIRTY_CLASSES)
cupsdSaveAllClasses();
- if (DirtyFiles & CUPSD_DIRTY_REMOTE)
- cupsdSaveRemoteCache();
-
if (DirtyFiles & CUPSD_DIRTY_PRINTCAP)
cupsdWritePrintcap();
void
cupsdMarkDirty(int what) /* I - What file(s) are dirty? */
{
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdMarkDirty(%c%c%c%c%c)",
+ (what & CUPSD_DIRTY_PRINTERS) ? 'P' : '-',
+ (what & CUPSD_DIRTY_CLASSES) ? 'C' : '-',
+ (what & CUPSD_DIRTY_PRINTCAP) ? 'p' : '-',
+ (what & CUPSD_DIRTY_JOBS) ? 'J' : '-',
+ (what & CUPSD_DIRTY_SUBSCRIPTIONS) ? 'S' : '-');
+
if (what == CUPSD_DIRTY_PRINTCAP && !Printcap)
return;
void
cupsdSetBusyState(void)
{
- int newbusy; /* New busy state */
- static int busy = 0; /* Current busy state */
+ int i; /* Looping var */
+ cupsd_job_t *job; /* Current job */
+ cupsd_printer_t *p; /* Current printer */
+ int newbusy; /* New busy state */
+ static int busy = 0; /* Current busy state */
static const char * const busy_text[] =
{ /* Text for busy states */
"Not busy",
"Active clients and printing jobs",
"Active clients, printing jobs, and dirty files"
};
+#ifdef HAVE_VPROC_TRANSACTION_BEGIN
+ static vproc_transaction_t vtran = 0; /* Current busy transaction */
+#endif /* HAVE_VPROC_TRANSACTION_BEGIN */
+
+ /*
+ * Figure out how busy we are...
+ */
newbusy = (DirtyCleanTime ? 1 : 0) |
- (cupsArrayCount(PrintingJobs) ? 2 : 0) |
(cupsArrayCount(ActiveClients) ? 4 : 0);
+ for (job = (cupsd_job_t *)cupsArrayFirst(PrintingJobs);
+ job;
+ job = (cupsd_job_t *)cupsArrayNext(PrintingJobs))
+ {
+ if ((p = job->printer) != NULL)
+ {
+ for (i = 0; i < p->num_reasons; i ++)
+ if (!strcmp(p->reasons[i], "connecting-to-device"))
+ break;
+
+ if (!p->num_reasons || i >= p->num_reasons)
+ break;
+ }
+ }
+
+ if (job)
+ newbusy |= 2;
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdSetBusyState: newbusy=\"%s\", busy=\"%s\"",
+ busy_text[newbusy], busy_text[busy]);
+
+ /*
+ * Manage state changes...
+ */
+
if (newbusy != busy)
{
busy = newbusy;
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetBusyState: %s", busy_text[busy]);
+#ifdef HAVE_VPROC_TRANSACTION_BEGIN
+ if (busy && !vtran)
+ vtran = vproc_transaction_begin(NULL);
+ else if (!busy && vtran)
+ {
+ vproc_transaction_end(NULL, vtran);
+ vtran = 0;
+ }
+#endif /* HAVE_VPROC_TRANSACTION_BEGIN */
}
+
+#if defined(kIOPMAssertionTypeDenySystemSleep) || defined(kIOPMAssertNetworkClientActive)
+ if (cupsArrayCount(PrintingJobs) > 0 && !keep_awake)
+ {
+# ifdef kIOPMAssertNetworkClientActive
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Asserting NetworkClientActive.");
+
+ IOPMAssertionCreateWithName(kIOPMAssertNetworkClientActive,
+ kIOPMAssertionLevelOn,
+ CFSTR("org.cups.cupsd"), &keep_awake);
+
+# else
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Asserting DenySystemSleep.");
+
+ IOPMAssertionCreateWithName(kIOPMAssertionTypeDenySystemSleep,
+ kIOPMAssertionLevelOn,
+ CFSTR("org.cups.cupsd"), &keep_awake);
+
+# endif /* kIOPMAssertNetworkClientActive */
+ }
+ else if (cupsArrayCount(PrintingJobs) == 0 && keep_awake)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Releasing power assertion.");
+ IOPMAssertionRelease(keep_awake);
+ keep_awake = 0;
+ }
+#endif /* kIOPMAssertionTypeDenySystemSleep || kIOPMAssertNetworkClientActive */
}
# define SYSEVENT_NAMECHANGED 0x10 /* Computer name changed */
-/*
- * Structures...
+/*
+ * Structures...
*/
typedef struct cupsd_sysevent_s /*** System event data ****/
} cupsd_thread_data_t;
-/*
- * Local globals...
+/*
+ * Local globals...
*/
static pthread_t SysEventThread = NULL;
/* Thread to host a runloop */
static pthread_mutex_t SysEventThreadMutex = { 0 };
- /* Coordinates access to shared gloabals */
+ /* Coordinates access to shared gloabals */
static pthread_cond_t SysEventThreadCond = { 0 };
/* Thread initialization complete condition */
static CFRunLoopRef SysEventRunloop = NULL;
/* The runloop. Access must be protected! */
static CFStringRef ComputerNameKey = NULL,
/* Computer name key */
+ BTMMKey = NULL, /* Back to My Mac key */
NetworkGlobalKeyIPv4 = NULL,
/* Network global IPv4 key */
NetworkGlobalKeyIPv6 = NULL,
/* Netowrk interface key */
NetworkInterfaceKeyIPv6 = NULL;
/* Netowrk interface key */
+static cupsd_sysevent_t LastSysEvent; /* Last system event (for delayed sleep) */
-/*
- * Local functions...
+/*
+ * Local functions...
*/
static void *sysEventThreadEntry(void);
static void sysUpdate(void);
+/*
+ * 'cupsdAllowSleep()' - Tell the OS it is now OK to sleep.
+ */
+
+void
+cupsdAllowSleep(void)
+{
+ cupsdCleanDirty();
+
+ IOAllowPowerChange(LastSysEvent.powerKernelPort,
+ LastSysEvent.powerNotificationID);
+}
+
+
/*
* 'cupsdStartSystemMonitor()' - Start monitoring for system change.
*/
SCDynamicStoreRef store = NULL;/* System Config dynamic store */
CFRunLoopSourceRef powerRLS = NULL,/* Power runloop source */
storeRLS = NULL;/* System Config runloop source */
- CFStringRef key[5], /* System Config keys */
+ CFStringRef key[6], /* System Config keys */
pattern[2]; /* System Config patterns */
CFArrayRef keys = NULL, /* System Config key array*/
patterns = NULL;/* System Config pattern array */
bzero(&storeContext, sizeof(storeContext));
storeContext.info = &threadData;
- store = SCDynamicStoreCreate(NULL, CFSTR("cupsd"),
+ store = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("cupsd"),
sysEventConfigurationNotifier, &storeContext);
if (!ComputerNameKey)
- ComputerNameKey = SCDynamicStoreKeyCreateComputerName(NULL);
+ ComputerNameKey = SCDynamicStoreKeyCreateComputerName(kCFAllocatorDefault);
+
+ if (!BTMMKey)
+ BTMMKey = SCDynamicStoreKeyCreate(kCFAllocatorDefault,
+ CFSTR("Setup:/Network/BackToMyMac"));
if (!NetworkGlobalKeyIPv4)
NetworkGlobalKeyIPv4 =
- SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL,
+ SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault,
kSCDynamicStoreDomainState,
kSCEntNetIPv4);
if (!NetworkGlobalKeyIPv6)
NetworkGlobalKeyIPv6 =
- SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL,
+ SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault,
kSCDynamicStoreDomainState,
kSCEntNetIPv6);
if (!NetworkGlobalKeyDNS)
- NetworkGlobalKeyDNS =
- SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL,
+ NetworkGlobalKeyDNS =
+ SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault,
kSCDynamicStoreDomainState,
kSCEntNetDNS);
if (!HostNamesKey)
- HostNamesKey = SCDynamicStoreKeyCreateHostNames(NULL);
+ HostNamesKey = SCDynamicStoreKeyCreateHostNames(kCFAllocatorDefault);
if (!NetworkInterfaceKeyIPv4)
NetworkInterfaceKeyIPv4 =
- SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
+ SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault,
kSCDynamicStoreDomainState,
kSCCompAnyRegex,
kSCEntNetIPv4);
if (!NetworkInterfaceKeyIPv6)
NetworkInterfaceKeyIPv6 =
- SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL,
+ SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault,
kSCDynamicStoreDomainState,
kSCCompAnyRegex,
kSCEntNetIPv6);
NetworkInterfaceKeyIPv4 && NetworkInterfaceKeyIPv6)
{
key[0] = ComputerNameKey;
- key[1] = NetworkGlobalKeyIPv4;
- key[2] = NetworkGlobalKeyIPv6;
- key[3] = NetworkGlobalKeyDNS;
- key[4] = HostNamesKey;
+ key[1] = BTMMKey;
+ key[2] = NetworkGlobalKeyIPv4;
+ key[3] = NetworkGlobalKeyIPv6;
+ key[4] = NetworkGlobalKeyDNS;
+ key[5] = HostNamesKey;
pattern[0] = NetworkInterfaceKeyIPv4;
pattern[1] = NetworkInterfaceKeyIPv6;
- keys = CFArrayCreate(NULL, (const void **)key,
- sizeof(key) / sizeof(key[0]),
- &kCFTypeArrayCallBacks);
+ keys = CFArrayCreate(kCFAllocatorDefault, (const void **)key,
+ sizeof(key) / sizeof(key[0]),
+ &kCFTypeArrayCallBacks);
- patterns = CFArrayCreate(NULL, (const void **)pattern,
+ patterns = CFArrayCreate(kCFAllocatorDefault, (const void **)pattern,
sizeof(pattern) / sizeof(pattern[0]),
&kCFTypeArrayCallBacks);
if (keys && patterns &&
SCDynamicStoreSetNotificationKeys(store, keys, patterns))
{
- if ((storeRLS = SCDynamicStoreCreateRunLoopSource(NULL, store, 0))
- != NULL)
+ if ((storeRLS = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault,
+ store, 0)) != NULL)
{
CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLS,
kCFRunLoopDefaultMode);
timerContext.info = &threadData;
threadData.timerRef =
- CFRunLoopTimerCreate(NULL,
- CFAbsoluteTimeGetCurrent() + (86400L * 365L * 10L),
+ CFRunLoopTimerCreate(kCFAllocatorDefault,
+ CFAbsoluteTimeGetCurrent() + (86400L * 365L * 10L),
86400L * 365L * 10L, 0, 0, sysEventTimerNotifier,
&timerContext);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), threadData.timerRef,
break;
case kIOMessageSystemHasPoweredOn:
- /*
+ /*
* Because powered on is followed by a net-changed event, delay
* before sending it.
*/
case kIOMessageSystemWillNotPowerOff:
case kIOMessageSystemWillNotSleep:
-#ifdef kIOMessageSystemWillPowerOn
+# ifdef kIOMessageSystemWillPowerOn
case kIOMessageSystemWillPowerOn:
-#endif /* kIOMessageSystemWillPowerOn */
+# endif /* kIOMessageSystemWillPowerOn */
default:
sendit = 0;
break;
if (sendit == 1)
{
- /*
+ /*
* Send the event to the main thread now.
*/
}
else
{
- /*
+ /*
* Send the event to the main thread after 1 to 2 seconds.
*/
/*
- * 'sysEventConfigurationNotifier()' - Computer name changed notification
+ * 'sysEventConfigurationNotifier()' - Network configuration change notification
* callback.
*/
threadData = (cupsd_thread_data_t *)context;
-
+
(void)store; /* anti-compiler-warning-code */
CFRange range = CFRangeMake(0, CFArrayGetCount(changedKeys));
- if (CFArrayContainsValue(changedKeys, range, ComputerNameKey))
+ if (CFArrayContainsValue(changedKeys, range, ComputerNameKey) ||
+ CFArrayContainsValue(changedKeys, range, BTMMKey))
threadData->sysevent.event |= SYSEVENT_NAMECHANGED;
else
{
}
/*
- * Because we registered for several different kinds of change notifications
- * this callback usually gets called several times in a row. We use a timer to
+ * Because we registered for several different kinds of change notifications
+ * this callback usually gets called several times in a row. We use a timer to
* de-bounce these so we only end up generating one event for the main thread.
*/
- CFRunLoopTimerSetNextFireDate(threadData->timerRef,
+ CFRunLoopTimerSetNextFireDate(threadData->timerRef,
CFAbsoluteTimeGetCurrent() + 5);
}
cupsd_thread_data_t *threadData; /* Thread context data */
+ (void)timer;
+
threadData = (cupsd_thread_data_t *)context;
/*
Sleeping = 1;
- cupsdStopAllJobs(0);
-
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
- if (p->type & CUPS_PRINTER_DISCOVERED)
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Deregistering local printer \"%s\"", p->name);
+ cupsdDeregisterPrinter(p, 0);
+ }
+
+ cupsdCleanDirty();
+
+#ifdef kIOPMAssertionTypeDenySystemSleep
+ /*
+ * Remove our assertion as needed since the user wants the system to
+ * sleep (different than idle sleep)...
+ */
+
+ if (keep_awake)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Releasing dark wake assertion.");
+ IOPMAssertionRelease(keep_awake);
+ keep_awake = 0;
+ }
+#endif /* kIOPMAssertionTypeDenySystemSleep */
+
+ /*
+ * If we have no printing jobs, allow the power change immediately.
+ * Otherwise set the SleepJobs time to 15 seconds in the future when
+ * we'll take more drastic measures...
+ */
+
+ if (cupsArrayCount(PrintingJobs) == 0)
+ IOAllowPowerChange(sysevent.powerKernelPort,
+ sysevent.powerNotificationID);
+ else
+ {
+ /*
+ * If there are active printers that don't have the connecting-to-device
+ * printer-state-reason then delay the sleep request (i.e. this reason
+ * indicates a job that is not yet connected to the printer)...
+ */
+
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Deleting remote destination \"%s\"", p->name);
- cupsArraySave(Printers);
- cupsdDeletePrinter(p, 0);
- cupsArrayRestore(Printers);
+ if (p->job)
+ {
+ for (i = 0; i < p->num_reasons; i ++)
+ if (!strcmp(p->reasons[i], "connecting-to-device"))
+ break;
+
+ if (!p->num_reasons || i >= p->num_reasons)
+ break;
+ }
+ }
+
+ if (p)
+ {
+ LastSysEvent = sysevent;
+ SleepJobs = time(NULL) + 10;
}
else
{
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Deregistering local printer \"%s\"", p->name);
- cupsdDeregisterPrinter(p, 0);
+ IOAllowPowerChange(sysevent.powerKernelPort,
+ sysevent.powerNotificationID);
}
}
-
- cupsdCleanDirty();
-
- IOAllowPowerChange(sysevent.powerKernelPort,
- sysevent.powerNotificationID);
}
if (sysevent.event & SYSEVENT_WOKE)
IOAllowPowerChange(sysevent.powerKernelPort,
sysevent.powerNotificationID);
Sleeping = 0;
+
+#ifdef kIOPMAssertionTypeDenySystemSleep
+ if (cupsArrayCount(PrintingJobs) > 0 && !keep_awake)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Asserting dark wake.");
+ IOPMAssertionCreateWithName(kIOPMAssertionTypeDenySystemSleep,
+ kIOPMAssertionLevelOn,
+ CFSTR("org.cups.cupsd"), &keep_awake);
+ }
+#endif /* kIOPMAssertionTypeDenySystemSleep */
+
cupsdCheckJobs();
}
if (sysevent.event & SYSEVENT_NETCHANGED)
{
if (!Sleeping)
- {
cupsdLogMessage(CUPSD_LOG_DEBUG,
"System network configuration changed");
-
- /*
- * Resetting browse_time before calling cupsdSendBrowseList causes
- * browse packets to be sent for local shared printers.
- */
-
- for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
- p;
- p = (cupsd_printer_t *)cupsArrayNext(Printers))
- p->browse_time = 0;
-
- cupsdSendBrowseList();
- cupsdRestartPolling();
- }
else
cupsdLogMessage(CUPSD_LOG_DEBUG,
"System network configuration changed; "
{
if (!Sleeping)
{
- cupsdLogMessage(CUPSD_LOG_DEBUG, "Computer name changed");
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Computer name or BTMM domains changed");
/*
* De-register the individual printers...
p = (cupsd_printer_t *)cupsArrayNext(Printers))
cupsdDeregisterPrinter(p, 1);
+# if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
/*
- * Update the computer name...
+ * Update the computer name and BTMM domain list...
*/
cupsdUpdateDNSSDName();
+# endif /* HAVE_DNSSD || HAVE_AVAHI */
/*
* Now re-register them...
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
- {
- p->browse_time = 0;
cupsdRegisterPrinter(p);
- }
}
else
cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Computer name changed; ignored while sleeping");
+ "Computer name or BTMM domains changed; ignored while "
+ "sleeping");
}
}
}
/*
- * End of "$Id: sysman.c 7676 2008-06-18 23:42:37Z mike $".
+ * End of "$Id$".
*/