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_REMOTE
)
91 cupsdSaveRemoteCache();
93 if (DirtyFiles
& CUPSD_DIRTY_PRINTCAP
)
96 if (DirtyFiles
& CUPSD_DIRTY_JOBS
)
98 cupsd_job_t
*job
; /* Current job */
102 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
104 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
109 if (DirtyFiles
& CUPSD_DIRTY_SUBSCRIPTIONS
)
110 cupsdSaveAllSubscriptions();
112 DirtyFiles
= CUPSD_DIRTY_NONE
;
120 * 'cupsdMarkDirty()' - Mark config or state files as needing a write.
124 cupsdMarkDirty(int what
) /* I - What file(s) are dirty? */
126 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdMarkDirty(%c%c%c%c%c%c)",
127 (what
& CUPSD_DIRTY_PRINTERS
) ? 'P' : '-',
128 (what
& CUPSD_DIRTY_CLASSES
) ? 'C' : '-',
129 (what
& CUPSD_DIRTY_REMOTE
) ? 'R' : '-',
130 (what
& CUPSD_DIRTY_PRINTCAP
) ? 'p' : '-',
131 (what
& CUPSD_DIRTY_JOBS
) ? 'J' : '-',
132 (what
& CUPSD_DIRTY_SUBSCRIPTIONS
) ? 'S' : '-');
134 if (what
== CUPSD_DIRTY_PRINTCAP
&& !Printcap
)
140 DirtyCleanTime
= time(NULL
) + DirtyCleanInterval
;
147 * 'cupsdSetBusyState()' - Let the system know when we are busy doing something.
151 cupsdSetBusyState(void)
153 int i
; /* Looping var */
154 cupsd_job_t
*job
; /* Current job */
155 cupsd_printer_t
*p
; /* Current printer */
156 int newbusy
; /* New busy state */
157 static int busy
= 0; /* Current busy state */
158 static const char * const busy_text
[] =
159 { /* Text for busy states */
163 "Printing jobs and dirty files",
165 "Active clients and dirty files",
166 "Active clients and printing jobs",
167 "Active clients, printing jobs, and dirty files"
169 #ifdef HAVE_VPROC_TRANSACTION_BEGIN
170 static vproc_transaction_t vtran
= 0; /* Current busy transaction */
171 #endif /* HAVE_VPROC_TRANSACTION_BEGIN */
175 * Figure out how busy we are...
178 newbusy
= (DirtyCleanTime
? 1 : 0) |
179 (cupsArrayCount(ActiveClients
) ? 4 : 0);
181 for (job
= (cupsd_job_t
*)cupsArrayFirst(PrintingJobs
);
183 job
= (cupsd_job_t
*)cupsArrayNext(PrintingJobs
))
185 if ((p
= job
->printer
) != NULL
)
187 for (i
= 0; i
< p
->num_reasons
; i
++)
188 if (!strcmp(p
->reasons
[i
], "connecting-to-device"))
191 if (!p
->num_reasons
|| i
>= p
->num_reasons
)
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 */
217 #ifdef kIOPMAssertionTypeDenySystemSleep
218 if ((busy
& 2) && !dark_wake
)
220 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Asserting dark wake.");
221 IOPMAssertionCreateWithName(kIOPMAssertionTypeDenySystemSleep
,
222 kIOPMAssertionLevelOn
,
223 CFSTR("org.cups.cupsd"), &dark_wake
);
225 else if (!(busy
& 2) && dark_wake
)
227 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Releasing dark wake assertion.");
228 IOPMAssertionRelease(dark_wake
);
231 #endif /* kIOPMAssertionTypeDenySystemSleep */
233 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdSetBusyState: %s", busy_text
[busy
]);
240 * This is the Apple-specific system event code. It works by creating
241 * a worker thread that waits for events from the OS and relays them
242 * to the main thread via a traditional pipe.
246 * Include MacOS-specific headers...
249 # include <IOKit/IOKitLib.h>
250 # include <IOKit/IOMessage.h>
251 # include <IOKit/pwr_mgt/IOPMLib.h>
252 # include <SystemConfiguration/SystemConfiguration.h>
253 # include <pthread.h>
260 # define SYSEVENT_CANSLEEP 0x1 /* Decide whether to allow sleep or not */
261 # define SYSEVENT_WILLSLEEP 0x2 /* Computer will go to sleep */
262 # define SYSEVENT_WOKE 0x4 /* Computer woke from sleep */
263 # define SYSEVENT_NETCHANGED 0x8 /* Network changed */
264 # define SYSEVENT_NAMECHANGED 0x10 /* Computer name changed */
271 typedef struct cupsd_sysevent_s
/*** System event data ****/
273 unsigned char event
; /* Event bit field */
274 io_connect_t powerKernelPort
; /* Power context data */
275 long powerNotificationID
; /* Power event data */
279 typedef struct cupsd_thread_data_s
/*** Thread context data ****/
281 cupsd_sysevent_t sysevent
; /* System event */
282 CFRunLoopTimerRef timerRef
; /* Timer to delay some change *
284 } cupsd_thread_data_t
;
291 static pthread_t SysEventThread
= NULL
;
292 /* Thread to host a runloop */
293 static pthread_mutex_t SysEventThreadMutex
= { 0 };
294 /* Coordinates access to shared gloabals */
295 static pthread_cond_t SysEventThreadCond
= { 0 };
296 /* Thread initialization complete condition */
297 static CFRunLoopRef SysEventRunloop
= NULL
;
298 /* The runloop. Access must be protected! */
299 static CFStringRef ComputerNameKey
= NULL
,
300 /* Computer name key */
301 BTMMKey
= NULL
, /* Back to My Mac key */
302 NetworkGlobalKeyIPv4
= NULL
,
303 /* Network global IPv4 key */
304 NetworkGlobalKeyIPv6
= NULL
,
305 /* Network global IPv6 key */
306 NetworkGlobalKeyDNS
= NULL
,
307 /* Network global DNS key */
310 NetworkInterfaceKeyIPv4
= NULL
,
311 /* Netowrk interface key */
312 NetworkInterfaceKeyIPv6
= NULL
;
313 /* Netowrk interface key */
314 static cupsd_sysevent_t LastSysEvent
; /* Last system event (for delayed sleep) */
321 static void *sysEventThreadEntry(void);
322 static void sysEventPowerNotifier(void *context
, io_service_t service
,
323 natural_t messageType
,
324 void *messageArgument
);
325 static void sysEventConfigurationNotifier(SCDynamicStoreRef store
,
326 CFArrayRef changedKeys
,
328 static void sysEventTimerNotifier(CFRunLoopTimerRef timer
, void *context
);
329 static void sysUpdate(void);
333 * 'cupsdAllowSleep()' - Tell the OS it is now OK to sleep.
337 cupsdAllowSleep(void)
341 IOAllowPowerChange(LastSysEvent
.powerKernelPort
,
342 LastSysEvent
.powerNotificationID
);
347 * 'cupsdStartSystemMonitor()' - Start monitoring for system change.
351 cupsdStartSystemMonitor(void)
353 int flags
; /* fcntl flags on pipe */
356 if (cupsdOpenPipe(SysEventPipes
))
358 cupsdLogMessage(CUPSD_LOG_ERROR
, "System event monitor pipe() failed - %s!",
363 cupsdAddSelect(SysEventPipes
[0], (cupsd_selfunc_t
)sysUpdate
, NULL
, NULL
);
366 * Set non-blocking mode on the descriptor we will be receiving notification
370 flags
= fcntl(SysEventPipes
[0], F_GETFL
, 0);
371 fcntl(SysEventPipes
[0], F_SETFL
, flags
| O_NONBLOCK
);
374 * Start the thread that runs the runloop...
377 pthread_mutex_init(&SysEventThreadMutex
, NULL
);
378 pthread_cond_init(&SysEventThreadCond
, NULL
);
379 pthread_create(&SysEventThread
, NULL
, (void *(*)())sysEventThreadEntry
, NULL
);
384 * 'cupsdStopSystemMonitor()' - Stop monitoring for system change.
388 cupsdStopSystemMonitor(void)
390 CFRunLoopRef rl
; /* The event handler runloop */
396 * Make sure the thread has completed it's initialization and
397 * stored it's runloop reference in the shared global.
400 pthread_mutex_lock(&SysEventThreadMutex
);
402 if (!SysEventRunloop
)
403 pthread_cond_wait(&SysEventThreadCond
, &SysEventThreadMutex
);
405 rl
= SysEventRunloop
;
406 SysEventRunloop
= NULL
;
408 pthread_mutex_unlock(&SysEventThreadMutex
);
413 pthread_join(SysEventThread
, NULL
);
414 pthread_mutex_destroy(&SysEventThreadMutex
);
415 pthread_cond_destroy(&SysEventThreadCond
);
418 if (SysEventPipes
[0] >= 0)
420 cupsdRemoveSelect(SysEventPipes
[0]);
421 cupsdClosePipe(SysEventPipes
);
427 * 'sysEventThreadEntry()' - A thread to receive power and computer name
428 * change notifications.
431 static void * /* O - Return status/value */
432 sysEventThreadEntry(void)
434 io_object_t powerNotifierObj
;
435 /* Power notifier object */
436 IONotificationPortRef powerNotifierPort
;
437 /* Power notifier port */
438 SCDynamicStoreRef store
= NULL
;/* System Config dynamic store */
439 CFRunLoopSourceRef powerRLS
= NULL
,/* Power runloop source */
440 storeRLS
= NULL
;/* System Config runloop source */
441 CFStringRef key
[6], /* System Config keys */
442 pattern
[2]; /* System Config patterns */
443 CFArrayRef keys
= NULL
, /* System Config key array*/
444 patterns
= NULL
;/* System Config pattern array */
445 SCDynamicStoreContext storeContext
; /* Dynamic store context */
446 CFRunLoopTimerContext timerContext
; /* Timer context */
447 cupsd_thread_data_t threadData
; /* Thread context data for the *
448 * runloop notifiers */
452 * Register for power state change notifications
455 bzero(&threadData
, sizeof(threadData
));
457 threadData
.sysevent
.powerKernelPort
=
458 IORegisterForSystemPower(&threadData
, &powerNotifierPort
,
459 sysEventPowerNotifier
, &powerNotifierObj
);
461 if (threadData
.sysevent
.powerKernelPort
)
463 powerRLS
= IONotificationPortGetRunLoopSource(powerNotifierPort
);
464 CFRunLoopAddSource(CFRunLoopGetCurrent(), powerRLS
, kCFRunLoopDefaultMode
);
467 DEBUG_puts("sysEventThreadEntry: error registering for system power "
471 * Register for system configuration change notifications
474 bzero(&storeContext
, sizeof(storeContext
));
475 storeContext
.info
= &threadData
;
477 store
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"),
478 sysEventConfigurationNotifier
, &storeContext
);
480 if (!ComputerNameKey
)
481 ComputerNameKey
= SCDynamicStoreKeyCreateComputerName(kCFAllocatorDefault
);
484 BTMMKey
= SCDynamicStoreKeyCreate(kCFAllocatorDefault
,
485 CFSTR("Setup:/Network/BackToMyMac"));
487 if (!NetworkGlobalKeyIPv4
)
488 NetworkGlobalKeyIPv4
=
489 SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault
,
490 kSCDynamicStoreDomainState
,
493 if (!NetworkGlobalKeyIPv6
)
494 NetworkGlobalKeyIPv6
=
495 SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault
,
496 kSCDynamicStoreDomainState
,
499 if (!NetworkGlobalKeyDNS
)
500 NetworkGlobalKeyDNS
=
501 SCDynamicStoreKeyCreateNetworkGlobalEntity(kCFAllocatorDefault
,
502 kSCDynamicStoreDomainState
,
506 HostNamesKey
= SCDynamicStoreKeyCreateHostNames(kCFAllocatorDefault
);
508 if (!NetworkInterfaceKeyIPv4
)
509 NetworkInterfaceKeyIPv4
=
510 SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault
,
511 kSCDynamicStoreDomainState
,
515 if (!NetworkInterfaceKeyIPv6
)
516 NetworkInterfaceKeyIPv6
=
517 SCDynamicStoreKeyCreateNetworkInterfaceEntity(kCFAllocatorDefault
,
518 kSCDynamicStoreDomainState
,
522 if (store
&& ComputerNameKey
&& HostNamesKey
&&
523 NetworkGlobalKeyIPv4
&& NetworkGlobalKeyIPv6
&& NetworkGlobalKeyDNS
&&
524 NetworkInterfaceKeyIPv4
&& NetworkInterfaceKeyIPv6
)
526 key
[0] = ComputerNameKey
;
528 key
[2] = NetworkGlobalKeyIPv4
;
529 key
[3] = NetworkGlobalKeyIPv6
;
530 key
[4] = NetworkGlobalKeyDNS
;
531 key
[5] = HostNamesKey
;
533 pattern
[0] = NetworkInterfaceKeyIPv4
;
534 pattern
[1] = NetworkInterfaceKeyIPv6
;
536 keys
= CFArrayCreate(kCFAllocatorDefault
, (const void **)key
,
537 sizeof(key
) / sizeof(key
[0]),
538 &kCFTypeArrayCallBacks
);
540 patterns
= CFArrayCreate(kCFAllocatorDefault
, (const void **)pattern
,
541 sizeof(pattern
) / sizeof(pattern
[0]),
542 &kCFTypeArrayCallBacks
);
544 if (keys
&& patterns
&&
545 SCDynamicStoreSetNotificationKeys(store
, keys
, patterns
))
547 if ((storeRLS
= SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault
,
550 CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLS
,
551 kCFRunLoopDefaultMode
);
554 DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreateRunLoopSource "
555 "failed: %s\n", SCErrorString(SCError())));
558 DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreSetNotificationKeys "
559 "failed: %s\n", SCErrorString(SCError())));
562 DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreate failed: %s\n",
563 SCErrorString(SCError())));
572 * Set up a timer to delay the wake change notifications.
574 * The initial time is set a decade or so into the future, we'll adjust
578 bzero(&timerContext
, sizeof(timerContext
));
579 timerContext
.info
= &threadData
;
581 threadData
.timerRef
=
582 CFRunLoopTimerCreate(kCFAllocatorDefault
,
583 CFAbsoluteTimeGetCurrent() + (86400L * 365L * 10L),
584 86400L * 365L * 10L, 0, 0, sysEventTimerNotifier
,
586 CFRunLoopAddTimer(CFRunLoopGetCurrent(), threadData
.timerRef
,
587 kCFRunLoopDefaultMode
);
590 * Store our runloop in a global so the main thread can use it to stop us.
593 pthread_mutex_lock(&SysEventThreadMutex
);
595 SysEventRunloop
= CFRunLoopGetCurrent();
597 pthread_cond_signal(&SysEventThreadCond
);
598 pthread_mutex_unlock(&SysEventThreadMutex
);
601 * Disappear into the runloop until it's stopped by the main thread.
607 * Clean up before exiting.
610 if (threadData
.timerRef
)
612 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), threadData
.timerRef
,
613 kCFRunLoopDefaultMode
);
614 CFRelease(threadData
.timerRef
);
617 if (threadData
.sysevent
.powerKernelPort
)
619 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), powerRLS
,
620 kCFRunLoopDefaultMode
);
621 IODeregisterForSystemPower(&powerNotifierObj
);
622 IOServiceClose(threadData
.sysevent
.powerKernelPort
);
623 IONotificationPortDestroy(powerNotifierPort
);
628 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), storeRLS
,
629 kCFRunLoopDefaultMode
);
630 CFRunLoopSourceInvalidate(storeRLS
);
642 * 'sysEventPowerNotifier()' - Handle power notification events.
646 sysEventPowerNotifier(
647 void *context
, /* I - Thread context data */
648 io_service_t service
, /* I - Unused service info */
649 natural_t messageType
, /* I - Type of message */
650 void *messageArgument
) /* I - Message data */
652 int sendit
= 1; /* Send event to main thread? *
653 * (0 = no, 1 = yes, 2 = delayed */
654 cupsd_thread_data_t
*threadData
; /* Thread context data */
657 threadData
= (cupsd_thread_data_t
*)context
;
659 (void)service
; /* anti-compiler-warning-code */
663 case kIOMessageCanSystemPowerOff
:
664 case kIOMessageCanSystemSleep
:
665 threadData
->sysevent
.event
|= SYSEVENT_CANSLEEP
;
668 case kIOMessageSystemWillRestart
:
669 case kIOMessageSystemWillPowerOff
:
670 case kIOMessageSystemWillSleep
:
671 threadData
->sysevent
.event
|= SYSEVENT_WILLSLEEP
;
674 case kIOMessageSystemHasPoweredOn
:
676 * Because powered on is followed by a net-changed event, delay
681 threadData
->sysevent
.event
|= SYSEVENT_WOKE
;
684 case kIOMessageSystemWillNotPowerOff
:
685 case kIOMessageSystemWillNotSleep
:
686 # ifdef kIOMessageSystemWillPowerOn
687 case kIOMessageSystemWillPowerOn
:
688 # endif /* kIOMessageSystemWillPowerOn */
695 IOAllowPowerChange(threadData
->sysevent
.powerKernelPort
,
696 (long)messageArgument
);
699 threadData
->sysevent
.powerNotificationID
= (long)messageArgument
;
704 * Send the event to the main thread now.
707 write(SysEventPipes
[1], &threadData
->sysevent
,
708 sizeof(threadData
->sysevent
));
709 threadData
->sysevent
.event
= 0;
714 * Send the event to the main thread after 1 to 2 seconds.
717 CFRunLoopTimerSetNextFireDate(threadData
->timerRef
,
718 CFAbsoluteTimeGetCurrent() + 2);
725 * 'sysEventConfigurationNotifier()' - Network configuration change notification
730 sysEventConfigurationNotifier(
731 SCDynamicStoreRef store
, /* I - System data (unused) */
732 CFArrayRef changedKeys
, /* I - Changed data */
733 void *context
) /* I - Thread context data */
735 cupsd_thread_data_t
*threadData
; /* Thread context data */
738 threadData
= (cupsd_thread_data_t
*)context
;
740 (void)store
; /* anti-compiler-warning-code */
742 CFRange range
= CFRangeMake(0, CFArrayGetCount(changedKeys
));
744 if (CFArrayContainsValue(changedKeys
, range
, ComputerNameKey
) ||
745 CFArrayContainsValue(changedKeys
, range
, BTMMKey
))
746 threadData
->sysevent
.event
|= SYSEVENT_NAMECHANGED
;
749 threadData
->sysevent
.event
|= SYSEVENT_NETCHANGED
;
752 * Indicate the network interface list needs updating...
759 * Because we registered for several different kinds of change notifications
760 * this callback usually gets called several times in a row. We use a timer to
761 * de-bounce these so we only end up generating one event for the main thread.
764 CFRunLoopTimerSetNextFireDate(threadData
->timerRef
,
765 CFAbsoluteTimeGetCurrent() + 5);
770 * 'sysEventTimerNotifier()' - Handle delayed event notifications.
774 sysEventTimerNotifier(
775 CFRunLoopTimerRef timer
, /* I - Timer information */
776 void *context
) /* I - Thread context data */
778 cupsd_thread_data_t
*threadData
; /* Thread context data */
783 threadData
= (cupsd_thread_data_t
*)context
;
786 * If an event is still pending send it to the main thread.
789 if (threadData
->sysevent
.event
)
791 write(SysEventPipes
[1], &threadData
->sysevent
,
792 sizeof(threadData
->sysevent
));
793 threadData
->sysevent
.event
= 0;
799 * 'sysUpdate()' - Update the current system state.
805 int i
; /* Looping var */
806 cupsd_sysevent_t sysevent
; /* The system event */
807 cupsd_printer_t
*p
; /* Printer information */
811 * Drain the event pipe...
814 while (read((int)SysEventPipes
[0], &sysevent
, sizeof(sysevent
))
817 if (sysevent
.event
& SYSEVENT_CANSLEEP
)
820 * If there are active printers that don't have the connecting-to-device
821 * printer-state-reason then cancel the sleep request (i.e. this reason
822 * indicates a job that is not yet connected to the printer)...
825 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
827 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
831 for (i
= 0; i
< p
->num_reasons
; i
++)
832 if (!strcmp(p
->reasons
[i
], "connecting-to-device"))
835 if (!p
->num_reasons
|| i
>= p
->num_reasons
)
842 cupsdLogMessage(CUPSD_LOG_INFO
,
843 "System sleep canceled because printer %s is active",
845 IOCancelPowerChange(sysevent
.powerKernelPort
,
846 sysevent
.powerNotificationID
);
850 cupsdLogMessage(CUPSD_LOG_DEBUG
, "System wants to sleep");
851 IOAllowPowerChange(sysevent
.powerKernelPort
,
852 sysevent
.powerNotificationID
);
856 if (sysevent
.event
& SYSEVENT_WILLSLEEP
)
858 cupsdLogMessage(CUPSD_LOG_DEBUG
, "System going to sleep");
862 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
864 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
866 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
868 cupsdLogMessage(CUPSD_LOG_DEBUG
,
869 "Deleting remote destination \"%s\"", p
->name
);
870 cupsArraySave(Printers
);
871 cupsdDeletePrinter(p
, 0);
872 cupsArrayRestore(Printers
);
876 cupsdLogMessage(CUPSD_LOG_DEBUG
,
877 "Deregistering local printer \"%s\"", p
->name
);
878 cupsdDeregisterPrinter(p
, 0);
884 #ifdef kIOPMAssertionTypeDenySystemSleep
886 * Tell the OS it is OK to sleep when we remove our assertion...
889 IOAllowPowerChange(sysevent
.powerKernelPort
,
890 sysevent
.powerNotificationID
);
894 * If we have no printing jobs, allow the power change immediately.
895 * Otherwise set the SleepJobs time to 15 seconds in the future when
896 * we'll take more drastic measures...
899 if (cupsArrayCount(PrintingJobs
) == 0)
900 IOAllowPowerChange(sysevent
.powerKernelPort
,
901 sysevent
.powerNotificationID
);
905 * If there are active printers that don't have the connecting-to-device
906 * printer-state-reason then delay the sleep request (i.e. this reason
907 * indicates a job that is not yet connected to the printer)...
910 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
912 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
916 for (i
= 0; i
< p
->num_reasons
; i
++)
917 if (!strcmp(p
->reasons
[i
], "connecting-to-device"))
920 if (!p
->num_reasons
|| i
>= p
->num_reasons
)
927 LastSysEvent
= sysevent
;
928 SleepJobs
= time(NULL
) + 10;
932 IOAllowPowerChange(sysevent
.powerKernelPort
,
933 sysevent
.powerNotificationID
);
936 #endif /* kIOPMAssertionTypeDenySystemSleep */
939 if (sysevent
.event
& SYSEVENT_WOKE
)
941 cupsdLogMessage(CUPSD_LOG_DEBUG
, "System woke from sleep");
942 IOAllowPowerChange(sysevent
.powerKernelPort
,
943 sysevent
.powerNotificationID
);
948 if (sysevent
.event
& SYSEVENT_NETCHANGED
)
952 cupsdLogMessage(CUPSD_LOG_DEBUG
,
953 "System network configuration changed");
956 * Resetting browse_time before calling cupsdSendBrowseList causes
957 * browse packets to be sent for local shared printers.
960 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
962 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
965 cupsdSendBrowseList();
966 cupsdRestartPolling();
969 cupsdLogMessage(CUPSD_LOG_DEBUG
,
970 "System network configuration changed; "
971 "ignored while sleeping");
974 if (sysevent
.event
& SYSEVENT_NAMECHANGED
)
978 cupsdLogMessage(CUPSD_LOG_DEBUG
,
979 "Computer name or BTMM domains changed");
982 * De-register the individual printers...
985 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
987 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
988 cupsdDeregisterPrinter(p
, 1);
991 * Update the computer name and BTMM domain list...
994 cupsdUpdateDNSSDName();
997 * Now re-register them...
1000 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1002 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1005 cupsdRegisterPrinter(p
);
1009 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1010 "Computer name or BTMM domains changed; ignored while "
1015 #endif /* __APPLE__ */
1019 * End of "$Id: sysman.c 7928 2008-09-10 22:14:22Z mike $".