2 * "$Id: sysman.c 7928 2008-09-10 22:14:22Z mike $"
4 * System management functions for the CUPS scheduler.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdCleanDirty() - Write dirty config and state files.
18 * cupsdMarkDirty() - Mark config or state files as needing a
20 * cupsdSetBusyState() - Let the system know when we are busy
22 * cupsdAllowSleep() - Tell the OS it is now OK to sleep.
23 * cupsdStartSystemMonitor() - Start monitoring for system change.
24 * cupsdStopSystemMonitor() - Stop monitoring for system change.
25 * sysEventThreadEntry() - A thread to receive power and computer
26 * name change notifications.
27 * sysEventPowerNotifier() - Handle power notification events.
28 * sysEventConfigurationNotifier() - Computer name changed notification
30 * sysEventTimerNotifier() - Handle delayed event notifications.
31 * sysUpdate() - Update the current system state.
36 * Include necessary headers...
40 #ifdef HAVE_VPROC_TRANSACTION_BEGIN
42 #endif /* HAVE_VPROC_TRANSACTION_BEGIN */
44 # include <IOKit/pwr_mgt/IOPMLib.h>
45 # ifdef HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H
46 # include <IOKit/pwr_mgt/IOPMLibPrivate.h>
47 # endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
48 #endif /* __APPLE__ */
52 * The system management functions cover disk and power management which
53 * are primarily used on portable computers.
55 * Disk management involves delaying the write of certain configuration
56 * and state files to minimize the number of times the disk has to spin
59 * Power management support is currently only implemented on MacOS X, but
60 * essentially we use four functions to let the OS know when it is OK to
61 * put the system to sleep, typically when we are not in the middle of
64 * Once put to sleep, we invalidate all remote printers since it is common
65 * to wake up in a new location/on a new wireless network.
72 #ifdef kIOPMAssertionTypeDenySystemSleep
73 static IOPMAssertionID dark_wake
= 0; /* "Dark wake" assertion for sharing */
74 #endif /* kIOPMAssertionTypeDenySystemSleep */
78 * 'cupsdCleanDirty()' - Write dirty config and state files.
84 if (DirtyFiles
& CUPSD_DIRTY_PRINTERS
)
85 cupsdSaveAllPrinters();
87 if (DirtyFiles
& CUPSD_DIRTY_CLASSES
)
88 cupsdSaveAllClasses();
90 if (DirtyFiles
& CUPSD_DIRTY_PRINTCAP
)
93 if (DirtyFiles
& CUPSD_DIRTY_JOBS
)
95 cupsd_job_t
*job
; /* Current job */
99 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
101 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
106 if (DirtyFiles
& CUPSD_DIRTY_SUBSCRIPTIONS
)
107 cupsdSaveAllSubscriptions();
109 DirtyFiles
= CUPSD_DIRTY_NONE
;
117 * 'cupsdMarkDirty()' - Mark config or state files as needing a write.
121 cupsdMarkDirty(int what
) /* I - What file(s) are dirty? */
123 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdMarkDirty(%c%c%c%c%c)",
124 (what
& CUPSD_DIRTY_PRINTERS
) ? 'P' : '-',
125 (what
& CUPSD_DIRTY_CLASSES
) ? 'C' : '-',
126 (what
& CUPSD_DIRTY_PRINTCAP
) ? 'p' : '-',
127 (what
& CUPSD_DIRTY_JOBS
) ? 'J' : '-',
128 (what
& CUPSD_DIRTY_SUBSCRIPTIONS
) ? 'S' : '-');
130 if (what
== CUPSD_DIRTY_PRINTCAP
&& !Printcap
)
136 DirtyCleanTime
= time(NULL
) + DirtyCleanInterval
;
143 * 'cupsdSetBusyState()' - Let the system know when we are busy doing something.
147 cupsdSetBusyState(void)
149 int i
; /* Looping var */
150 cupsd_job_t
*job
; /* Current job */
151 cupsd_printer_t
*p
; /* Current printer */
152 int newbusy
; /* New busy state */
153 static int busy
= 0; /* Current busy state */
154 static const char * const busy_text
[] =
155 { /* Text for busy states */
159 "Printing jobs and dirty files",
161 "Active clients and dirty files",
162 "Active clients and printing jobs",
163 "Active clients, printing jobs, and dirty files"
165 #ifdef HAVE_VPROC_TRANSACTION_BEGIN
166 static vproc_transaction_t vtran
= 0; /* Current busy transaction */
167 #endif /* HAVE_VPROC_TRANSACTION_BEGIN */
171 * Figure out how busy we are...
174 newbusy
= (DirtyCleanTime
? 1 : 0) |
175 (cupsArrayCount(ActiveClients
) ? 4 : 0);
177 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
179 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
181 if ((p
= job
->printer
) != NULL
)
183 for (i
= 0; i
< p
->num_reasons
; i
++)
184 if (!strcmp(p
->reasons
[i
], "connecting-to-device"))
187 if (!p
->num_reasons
|| i
>= p
->num_reasons
)
195 cupsdLogMessage(CUPSD_LOG_DEBUG
,
196 "cupsdSetBusyState: newbusy=\"%s\", busy=\"%s\"",
197 busy_text
[newbusy
], busy_text
[busy
]);
200 * Manage state changes...
207 #ifdef HAVE_VPROC_TRANSACTION_BEGIN
209 vtran
= vproc_transaction_begin(NULL
);
210 else if (!busy
&& vtran
)
212 vproc_transaction_end(NULL
, vtran
);
215 #endif /* HAVE_VPROC_TRANSACTION_BEGIN */
218 #ifdef kIOPMAssertionTypeDenySystemSleep
219 if (cupsArrayCount(PrintingJobs
) > 0 && !dark_wake
)
221 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Asserting dark wake.");
222 IOPMAssertionCreateWithName(kIOPMAssertionTypeDenySystemSleep
,
223 kIOPMAssertionLevelOn
,
224 CFSTR("org.cups.cupsd"), &dark_wake
);
226 else if (cupsArrayCount(PrintingJobs
) == 0 && dark_wake
)
228 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Releasing dark wake assertion.");
229 IOPMAssertionRelease(dark_wake
);
232 #endif /* kIOPMAssertionTypeDenySystemSleep */
238 * This is the Apple-specific system event code. It works by creating
239 * a worker thread that waits for events from the OS and relays them
240 * to the main thread via a traditional pipe.
244 * Include MacOS-specific headers...
247 # include <IOKit/IOKitLib.h>
248 # include <IOKit/IOMessage.h>
249 # include <IOKit/pwr_mgt/IOPMLib.h>
250 # include <SystemConfiguration/SystemConfiguration.h>
251 # include <pthread.h>
258 # define SYSEVENT_CANSLEEP 0x1 /* Decide whether to allow sleep or not */
259 # define SYSEVENT_WILLSLEEP 0x2 /* Computer will go to sleep */
260 # define SYSEVENT_WOKE 0x4 /* Computer woke from sleep */
261 # define SYSEVENT_NETCHANGED 0x8 /* Network changed */
262 # define SYSEVENT_NAMECHANGED 0x10 /* Computer name changed */
269 typedef struct cupsd_sysevent_s
/*** System event data ****/
271 unsigned char event
; /* Event bit field */
272 io_connect_t powerKernelPort
; /* Power context data */
273 long powerNotificationID
; /* Power event data */
277 typedef struct cupsd_thread_data_s
/*** Thread context data ****/
279 cupsd_sysevent_t sysevent
; /* System event */
280 CFRunLoopTimerRef timerRef
; /* Timer to delay some change *
282 } cupsd_thread_data_t
;
289 static pthread_t SysEventThread
= NULL
;
290 /* Thread to host a runloop */
291 static pthread_mutex_t SysEventThreadMutex
= { 0 };
292 /* Coordinates access to shared gloabals */
293 static pthread_cond_t SysEventThreadCond
= { 0 };
294 /* Thread initialization complete condition */
295 static CFRunLoopRef SysEventRunloop
= NULL
;
296 /* The runloop. Access must be protected! */
297 static CFStringRef ComputerNameKey
= NULL
,
298 /* Computer name key */
299 BTMMKey
= NULL
, /* Back to My Mac key */
300 NetworkGlobalKeyIPv4
= NULL
,
301 /* Network global IPv4 key */
302 NetworkGlobalKeyIPv6
= NULL
,
303 /* Network global IPv6 key */
304 NetworkGlobalKeyDNS
= NULL
,
305 /* Network global DNS key */
308 NetworkInterfaceKeyIPv4
= NULL
,
309 /* Netowrk interface key */
310 NetworkInterfaceKeyIPv6
= NULL
;
311 /* Netowrk interface key */
312 static cupsd_sysevent_t LastSysEvent
; /* Last system event (for delayed sleep) */
319 static void *sysEventThreadEntry(void);
320 static void sysEventPowerNotifier(void *context
, io_service_t service
,
321 natural_t messageType
,
322 void *messageArgument
);
323 static void sysEventConfigurationNotifier(SCDynamicStoreRef store
,
324 CFArrayRef changedKeys
,
326 static void sysEventTimerNotifier(CFRunLoopTimerRef timer
, void *context
);
327 static void sysUpdate(void);
331 * 'cupsdAllowSleep()' - Tell the OS it is now OK to sleep.
335 cupsdAllowSleep(void)
339 IOAllowPowerChange(LastSysEvent
.powerKernelPort
,
340 LastSysEvent
.powerNotificationID
);
345 * 'cupsdStartSystemMonitor()' - Start monitoring for system change.
349 cupsdStartSystemMonitor(void)
351 int flags
; /* fcntl flags on pipe */
354 if (cupsdOpenPipe(SysEventPipes
))
356 cupsdLogMessage(CUPSD_LOG_ERROR
, "System event monitor pipe() failed - %s!",
361 cupsdAddSelect(SysEventPipes
[0], (cupsd_selfunc_t
)sysUpdate
, NULL
, NULL
);
364 * Set non-blocking mode on the descriptor we will be receiving notification
368 flags
= fcntl(SysEventPipes
[0], F_GETFL
, 0);
369 fcntl(SysEventPipes
[0], F_SETFL
, flags
| O_NONBLOCK
);
372 * Start the thread that runs the runloop...
375 pthread_mutex_init(&SysEventThreadMutex
, NULL
);
376 pthread_cond_init(&SysEventThreadCond
, NULL
);
377 pthread_create(&SysEventThread
, NULL
, (void *(*)())sysEventThreadEntry
, NULL
);
382 * 'cupsdStopSystemMonitor()' - Stop monitoring for system change.
386 cupsdStopSystemMonitor(void)
388 CFRunLoopRef rl
; /* The event handler runloop */
394 * Make sure the thread has completed it's initialization and
395 * stored it's runloop reference in the shared global.
398 pthread_mutex_lock(&SysEventThreadMutex
);
400 if (!SysEventRunloop
)
401 pthread_cond_wait(&SysEventThreadCond
, &SysEventThreadMutex
);
403 rl
= SysEventRunloop
;
404 SysEventRunloop
= NULL
;
406 pthread_mutex_unlock(&SysEventThreadMutex
);
411 pthread_join(SysEventThread
, NULL
);
412 pthread_mutex_destroy(&SysEventThreadMutex
);
413 pthread_cond_destroy(&SysEventThreadCond
);
416 if (SysEventPipes
[0] >= 0)
418 cupsdRemoveSelect(SysEventPipes
[0]);
419 cupsdClosePipe(SysEventPipes
);
425 * 'sysEventThreadEntry()' - A thread to receive power and computer name
426 * change notifications.
429 static void * /* O - Return status/value */
430 sysEventThreadEntry(void)
432 io_object_t powerNotifierObj
;
433 /* Power notifier object */
434 IONotificationPortRef powerNotifierPort
;
435 /* Power notifier port */
436 SCDynamicStoreRef store
= NULL
;/* System Config dynamic store */
437 CFRunLoopSourceRef powerRLS
= NULL
,/* Power runloop source */
438 storeRLS
= NULL
;/* System Config runloop source */
439 CFStringRef key
[6], /* System Config keys */
440 pattern
[2]; /* System Config patterns */
441 CFArrayRef keys
= NULL
, /* System Config key array*/
442 patterns
= NULL
;/* System Config pattern array */
443 SCDynamicStoreContext storeContext
; /* Dynamic store context */
444 CFRunLoopTimerContext timerContext
; /* Timer context */
445 cupsd_thread_data_t threadData
; /* Thread context data for the *
446 * runloop notifiers */
450 * Register for power state change notifications
453 bzero(&threadData
, sizeof(threadData
));
455 threadData
.sysevent
.powerKernelPort
=
456 IORegisterForSystemPower(&threadData
, &powerNotifierPort
,
457 sysEventPowerNotifier
, &powerNotifierObj
);
459 if (threadData
.sysevent
.powerKernelPort
)
461 powerRLS
= IONotificationPortGetRunLoopSource(powerNotifierPort
);
462 CFRunLoopAddSource(CFRunLoopGetCurrent(), powerRLS
, kCFRunLoopDefaultMode
);
465 DEBUG_puts("sysEventThreadEntry: error registering for system power "
469 * Register for system configuration change notifications
472 bzero(&storeContext
, sizeof(storeContext
));
473 storeContext
.info
= &threadData
;
475 store
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"),
476 sysEventConfigurationNotifier
, &storeContext
);
478 if (!ComputerNameKey
)
479 ComputerNameKey
= SCDynamicStoreKeyCreateComputerName(kCFAllocatorDefault
);
482 BTMMKey
= SCDynamicStoreKeyCreate(kCFAllocatorDefault
,
483 CFSTR("Setup:/Network/BackToMyMac"));
485 if (!NetworkGlobalKeyIPv4
)
486 NetworkGlobalKeyIPv4
=
487 SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault
,
488 kSCDynamicStoreDomainState
,
491 if (!NetworkGlobalKeyIPv6
)
492 NetworkGlobalKeyIPv6
=
493 SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault
,
494 kSCDynamicStoreDomainState
,
497 if (!NetworkGlobalKeyDNS
)
498 NetworkGlobalKeyDNS
=
499 SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault
,
500 kSCDynamicStoreDomainState
,
504 HostNamesKey
= SCDynamicStoreKeyCreateHostNames(kCFAllocatorDefault
);
506 if (!NetworkInterfaceKeyIPv4
)
507 NetworkInterfaceKeyIPv4
=
508 SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault
,
509 kSCDynamicStoreDomainState
,
513 if (!NetworkInterfaceKeyIPv6
)
514 NetworkInterfaceKeyIPv6
=
515 SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault
,
516 kSCDynamicStoreDomainState
,
520 if (store
&& ComputerNameKey
&& HostNamesKey
&&
521 NetworkGlobalKeyIPv4
&& NetworkGlobalKeyIPv6
&& NetworkGlobalKeyDNS
&&
522 NetworkInterfaceKeyIPv4
&& NetworkInterfaceKeyIPv6
)
524 key
[0] = ComputerNameKey
;
526 key
[2] = NetworkGlobalKeyIPv4
;
527 key
[3] = NetworkGlobalKeyIPv6
;
528 key
[4] = NetworkGlobalKeyDNS
;
529 key
[5] = HostNamesKey
;
531 pattern
[0] = NetworkInterfaceKeyIPv4
;
532 pattern
[1] = NetworkInterfaceKeyIPv6
;
534 keys
= CFArrayCreate(kCFAllocatorDefault
, (const void **)key
,
535 sizeof(key
) / sizeof(key
[0]),
536 &kCFTypeArrayCallBacks
);
538 patterns
= CFArrayCreate(kCFAllocatorDefault
, (const void **)pattern
,
539 sizeof(pattern
) / sizeof(pattern
[0]),
540 &kCFTypeArrayCallBacks
);
542 if (keys
&& patterns
&&
543 SCDynamicStoreSetNotificationKeys(store
, keys
, patterns
))
545 if ((storeRLS
= SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault
,
548 CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLS
,
549 kCFRunLoopDefaultMode
);
552 DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreateRunLoopSource "
553 "failed: %s\n", SCErrorString(SCError())));
556 DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreSetNotificationKeys "
557 "failed: %s\n", SCErrorString(SCError())));
560 DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreate failed: %s\n",
561 SCErrorString(SCError())));
570 * Set up a timer to delay the wake change notifications.
572 * The initial time is set a decade or so into the future, we'll adjust
576 bzero(&timerContext
, sizeof(timerContext
));
577 timerContext
.info
= &threadData
;
579 threadData
.timerRef
=
580 CFRunLoopTimerCreate(kCFAllocatorDefault
,
581 CFAbsoluteTimeGetCurrent() + (86400L * 365L * 10L),
582 86400L * 365L * 10L, 0, 0, sysEventTimerNotifier
,
584 CFRunLoopAddTimer(CFRunLoopGetCurrent(), threadData
.timerRef
,
585 kCFRunLoopDefaultMode
);
588 * Store our runloop in a global so the main thread can use it to stop us.
591 pthread_mutex_lock(&SysEventThreadMutex
);
593 SysEventRunloop
= CFRunLoopGetCurrent();
595 pthread_cond_signal(&SysEventThreadCond
);
596 pthread_mutex_unlock(&SysEventThreadMutex
);
599 * Disappear into the runloop until it's stopped by the main thread.
605 * Clean up before exiting.
608 if (threadData
.timerRef
)
610 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), threadData
.timerRef
,
611 kCFRunLoopDefaultMode
);
612 CFRelease(threadData
.timerRef
);
615 if (threadData
.sysevent
.powerKernelPort
)
617 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), powerRLS
,
618 kCFRunLoopDefaultMode
);
619 IODeregisterForSystemPower(&powerNotifierObj
);
620 IOServiceClose(threadData
.sysevent
.powerKernelPort
);
621 IONotificationPortDestroy(powerNotifierPort
);
626 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), storeRLS
,
627 kCFRunLoopDefaultMode
);
628 CFRunLoopSourceInvalidate(storeRLS
);
640 * 'sysEventPowerNotifier()' - Handle power notification events.
644 sysEventPowerNotifier(
645 void *context
, /* I - Thread context data */
646 io_service_t service
, /* I - Unused service info */
647 natural_t messageType
, /* I - Type of message */
648 void *messageArgument
) /* I - Message data */
650 int sendit
= 1; /* Send event to main thread? *
651 * (0 = no, 1 = yes, 2 = delayed */
652 cupsd_thread_data_t
*threadData
; /* Thread context data */
655 threadData
= (cupsd_thread_data_t
*)context
;
657 (void)service
; /* anti-compiler-warning-code */
661 case kIOMessageCanSystemPowerOff
:
662 case kIOMessageCanSystemSleep
:
663 threadData
->sysevent
.event
|= SYSEVENT_CANSLEEP
;
666 case kIOMessageSystemWillRestart
:
667 case kIOMessageSystemWillPowerOff
:
668 case kIOMessageSystemWillSleep
:
669 threadData
->sysevent
.event
|= SYSEVENT_WILLSLEEP
;
672 case kIOMessageSystemHasPoweredOn
:
674 * Because powered on is followed by a net-changed event, delay
679 threadData
->sysevent
.event
|= SYSEVENT_WOKE
;
682 case kIOMessageSystemWillNotPowerOff
:
683 case kIOMessageSystemWillNotSleep
:
684 # ifdef kIOMessageSystemWillPowerOn
685 case kIOMessageSystemWillPowerOn
:
686 # endif /* kIOMessageSystemWillPowerOn */
693 IOAllowPowerChange(threadData
->sysevent
.powerKernelPort
,
694 (long)messageArgument
);
697 threadData
->sysevent
.powerNotificationID
= (long)messageArgument
;
702 * Send the event to the main thread now.
705 write(SysEventPipes
[1], &threadData
->sysevent
,
706 sizeof(threadData
->sysevent
));
707 threadData
->sysevent
.event
= 0;
712 * Send the event to the main thread after 1 to 2 seconds.
715 CFRunLoopTimerSetNextFireDate(threadData
->timerRef
,
716 CFAbsoluteTimeGetCurrent() + 2);
723 * 'sysEventConfigurationNotifier()' - Network configuration change notification
728 sysEventConfigurationNotifier(
729 SCDynamicStoreRef store
, /* I - System data (unused) */
730 CFArrayRef changedKeys
, /* I - Changed data */
731 void *context
) /* I - Thread context data */
733 cupsd_thread_data_t
*threadData
; /* Thread context data */
736 threadData
= (cupsd_thread_data_t
*)context
;
738 (void)store
; /* anti-compiler-warning-code */
740 CFRange range
= CFRangeMake(0, CFArrayGetCount(changedKeys
));
742 if (CFArrayContainsValue(changedKeys
, range
, ComputerNameKey
) ||
743 CFArrayContainsValue(changedKeys
, range
, BTMMKey
))
744 threadData
->sysevent
.event
|= SYSEVENT_NAMECHANGED
;
747 threadData
->sysevent
.event
|= SYSEVENT_NETCHANGED
;
750 * Indicate the network interface list needs updating...
757 * Because we registered for several different kinds of change notifications
758 * this callback usually gets called several times in a row. We use a timer to
759 * de-bounce these so we only end up generating one event for the main thread.
762 CFRunLoopTimerSetNextFireDate(threadData
->timerRef
,
763 CFAbsoluteTimeGetCurrent() + 5);
768 * 'sysEventTimerNotifier()' - Handle delayed event notifications.
772 sysEventTimerNotifier(
773 CFRunLoopTimerRef timer
, /* I - Timer information */
774 void *context
) /* I - Thread context data */
776 cupsd_thread_data_t
*threadData
; /* Thread context data */
781 threadData
= (cupsd_thread_data_t
*)context
;
784 * If an event is still pending send it to the main thread.
787 if (threadData
->sysevent
.event
)
789 write(SysEventPipes
[1], &threadData
->sysevent
,
790 sizeof(threadData
->sysevent
));
791 threadData
->sysevent
.event
= 0;
797 * 'sysUpdate()' - Update the current system state.
803 int i
; /* Looping var */
804 cupsd_sysevent_t sysevent
; /* The system event */
805 cupsd_printer_t
*p
; /* Printer information */
809 * Drain the event pipe...
812 while (read((int)SysEventPipes
[0], &sysevent
, sizeof(sysevent
))
815 if (sysevent
.event
& SYSEVENT_CANSLEEP
)
818 * If there are active printers that don't have the connecting-to-device
819 * printer-state-reason then cancel the sleep request (i.e. this reason
820 * indicates a job that is not yet connected to the printer)...
823 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
825 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
829 for (i
= 0; i
< p
->num_reasons
; i
++)
830 if (!strcmp(p
->reasons
[i
], "connecting-to-device"))
833 if (!p
->num_reasons
|| i
>= p
->num_reasons
)
840 cupsdLogMessage(CUPSD_LOG_INFO
,
841 "System sleep canceled because printer %s is active",
843 IOCancelPowerChange(sysevent
.powerKernelPort
,
844 sysevent
.powerNotificationID
);
848 cupsdLogMessage(CUPSD_LOG_DEBUG
, "System wants to sleep");
849 IOAllowPowerChange(sysevent
.powerKernelPort
,
850 sysevent
.powerNotificationID
);
854 if (sysevent
.event
& SYSEVENT_WILLSLEEP
)
856 cupsdLogMessage(CUPSD_LOG_DEBUG
, "System going to sleep");
860 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
862 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
864 cupsdLogMessage(CUPSD_LOG_DEBUG
,
865 "Deregistering local printer \"%s\"", p
->name
);
866 cupsdDeregisterPrinter(p
, 0);
871 #ifdef kIOPMAssertionTypeDenySystemSleep
873 * Remove our assertion as needed since the user wants the system to
874 * sleep (different than idle sleep)...
879 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Releasing dark wake assertion.");
880 IOPMAssertionRelease(dark_wake
);
883 #endif /* kIOPMAssertionTypeDenySystemSleep */
886 * If we have no printing jobs, allow the power change immediately.
887 * Otherwise set the SleepJobs time to 15 seconds in the future when
888 * we'll take more drastic measures...
891 if (cupsArrayCount(PrintingJobs
) == 0)
892 IOAllowPowerChange(sysevent
.powerKernelPort
,
893 sysevent
.powerNotificationID
);
897 * If there are active printers that don't have the connecting-to-device
898 * printer-state-reason then delay the sleep request (i.e. this reason
899 * indicates a job that is not yet connected to the printer)...
902 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
904 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
908 for (i
= 0; i
< p
->num_reasons
; i
++)
909 if (!strcmp(p
->reasons
[i
], "connecting-to-device"))
912 if (!p
->num_reasons
|| i
>= p
->num_reasons
)
919 LastSysEvent
= sysevent
;
920 SleepJobs
= time(NULL
) + 10;
924 IOAllowPowerChange(sysevent
.powerKernelPort
,
925 sysevent
.powerNotificationID
);
930 if (sysevent
.event
& SYSEVENT_WOKE
)
932 cupsdLogMessage(CUPSD_LOG_DEBUG
, "System woke from sleep");
933 IOAllowPowerChange(sysevent
.powerKernelPort
,
934 sysevent
.powerNotificationID
);
937 #ifdef kIOPMAssertionTypeDenySystemSleep
938 if (cupsArrayCount(PrintingJobs
) > 0 && !dark_wake
)
940 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Asserting dark wake.");
941 IOPMAssertionCreateWithName(kIOPMAssertionTypeDenySystemSleep
,
942 kIOPMAssertionLevelOn
,
943 CFSTR("org.cups.cupsd"), &dark_wake
);
945 #endif /* kIOPMAssertionTypeDenySystemSleep */
950 if (sysevent
.event
& SYSEVENT_NETCHANGED
)
953 cupsdLogMessage(CUPSD_LOG_DEBUG
,
954 "System network configuration changed");
956 cupsdLogMessage(CUPSD_LOG_DEBUG
,
957 "System network configuration changed; "
958 "ignored while sleeping");
961 if (sysevent
.event
& SYSEVENT_NAMECHANGED
)
965 cupsdLogMessage(CUPSD_LOG_DEBUG
,
966 "Computer name or BTMM domains changed");
969 * De-register the individual printers...
972 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
974 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
975 cupsdDeregisterPrinter(p
, 1);
977 # if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
979 * Update the computer name and BTMM domain list...
982 cupsdUpdateDNSSDName();
983 # endif /* HAVE_DNSSD || HAVE_AVAHI */
986 * Now re-register them...
989 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
991 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
992 cupsdRegisterPrinter(p
);
995 cupsdLogMessage(CUPSD_LOG_DEBUG
,
996 "Computer name or BTMM domains changed; ignored while "
1001 #endif /* __APPLE__ */
1005 * End of "$Id: sysman.c 7928 2008-09-10 22:14:22Z mike $".