]>
Commit | Line | Data |
---|---|---|
09ec0018 | 1 | /* |
bc44d920 | 2 | * "$Id: sysman.c 6649 2007-07-11 21:46:42Z mike $" |
09ec0018 | 3 | * |
4 | * System management definitions for the Common UNIX Printing System (CUPS). | |
5 | * | |
3dfe78b3 | 6 | * Copyright 2007-2008 by Apple Inc. |
09ec0018 | 7 | * Copyright 2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
09ec0018 | 14 | * |
15 | * Contents: | |
16 | * | |
3dfe78b3 MS |
17 | * cupsdCleanDirty() - Write dirty config and state files. |
18 | * cupsdMarkDirty() - Mark config or state files as needing a | |
19 | * write. | |
20 | * cupsdSetBusyState() - Let the system know when we are busy | |
21 | * doing something. | |
09ec0018 | 22 | * cupsdStartSystemMonitor() - Start monitoring for system change. |
23 | * cupsdStopSystemMonitor() - Stop monitoring for system change. | |
24 | * cupsdUpdateSystemMonitor() - Update the current system state. | |
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 | |
29 | * callback. | |
30 | * sysEventTimerNotifier() - Handle delayed event notifications. | |
31 | */ | |
32 | ||
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include "cupsd.h" | |
39 | ||
40 | ||
41 | /* | |
3dfe78b3 MS |
42 | * The system management functions cover disk and power management which |
43 | * are primarily used on portable computers. | |
09ec0018 | 44 | * |
3dfe78b3 MS |
45 | * Disk management involves delaying the write of certain configuration |
46 | * and state files to minimize the number of times the disk has to spin | |
47 | * up. | |
48 | * | |
49 | * Power management support is currently only implemented on MacOS X, but | |
50 | * essentially we use four functions to let the OS know when it is OK to | |
51 | * put the system to idle sleep, typically when we are not in the middle of | |
52 | * printing a job. | |
53 | * | |
54 | * Once put to sleep, we invalidate all remote printers since it is common | |
55 | * to wake up in a new location/on a new wireless network. | |
56 | */ | |
57 | ||
58 | ||
59 | /* | |
60 | * 'cupsdCleanDirty()' - Write dirty config and state files. | |
09ec0018 | 61 | */ |
62 | ||
3dfe78b3 MS |
63 | void |
64 | cupsdCleanDirty(void) | |
65 | { | |
66 | if (DirtyFiles & CUPSD_DIRTY_PRINTERS) | |
67 | cupsdSaveAllPrinters(); | |
68 | ||
69 | if (DirtyFiles & CUPSD_DIRTY_CLASSES) | |
70 | cupsdSaveAllClasses(); | |
71 | ||
72 | if (DirtyFiles & CUPSD_DIRTY_REMOTE) | |
73 | cupsdSaveRemoteCache(); | |
74 | ||
75 | if (DirtyFiles & CUPSD_DIRTY_PRINTCAP) | |
76 | cupsdWritePrintcap(); | |
77 | ||
78 | if (DirtyFiles & CUPSD_DIRTY_JOBS) | |
79 | { | |
80 | cupsd_job_t *job; /* Current job */ | |
81 | ||
82 | cupsdSaveAllJobs(); | |
83 | ||
84 | for (job = (cupsd_job_t *)cupsArrayFirst(Jobs); | |
85 | job; | |
86 | job = (cupsd_job_t *)cupsArrayNext(Jobs)) | |
87 | if (job->dirty) | |
88 | cupsdSaveJob(job); | |
89 | } | |
90 | ||
91 | if (DirtyFiles & CUPSD_DIRTY_SUBSCRIPTIONS) | |
92 | cupsdSaveAllSubscriptions(); | |
93 | ||
94 | DirtyFiles = CUPSD_DIRTY_NONE; | |
95 | DirtyCleanTime = 0; | |
96 | } | |
97 | ||
98 | ||
99 | /* | |
100 | * 'cupsdMarkDirty()' - Mark config or state files as needing a write. | |
101 | */ | |
102 | ||
103 | void | |
104 | cupsdMarkDirty(int what) /* I - What file(s) are dirty? */ | |
105 | { | |
106 | DirtyFiles |= what; | |
107 | ||
108 | if (!DirtyCleanTime) | |
109 | DirtyCleanTime = time(NULL) + DirtyCleanInterval; | |
110 | ||
111 | cupsdSetBusyState(); | |
112 | } | |
113 | ||
114 | ||
115 | /* | |
116 | * 'cupsdSetBusyState()' - Let the system know when we are busy doing something. | |
117 | */ | |
118 | ||
119 | void | |
120 | cupsdSetBusyState(void) | |
121 | { | |
122 | int newbusy; /* New busy state */ | |
123 | static int busy = 0; /* Current busy state */ | |
124 | ||
125 | ||
126 | newbusy = DirtyCleanTime || | |
127 | cupsArrayCount(PrintingJobs) || | |
128 | cupsArrayCount(ActiveClients); | |
129 | ||
130 | if (newbusy != busy) | |
131 | { | |
132 | busy = newbusy; | |
133 | ||
134 | if (busy) | |
135 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
136 | "cupsdSetBusyState: Server no longer busy..."); | |
137 | else | |
138 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
139 | "cupsdSetBusyState: Server is now busy..."); | |
140 | } | |
141 | } | |
142 | ||
143 | ||
09ec0018 | 144 | #ifdef __APPLE__ |
145 | /* | |
146 | * This is the Apple-specific system event code. It works by creating | |
147 | * a worker thread that waits for events from the OS and relays them | |
148 | * to the main thread via a traditional pipe. | |
149 | */ | |
150 | ||
151 | /* | |
152 | * Include MacOS-specific headers... | |
153 | */ | |
154 | ||
155 | # include <IOKit/IOKitLib.h> | |
156 | # include <IOKit/IOMessage.h> | |
157 | # include <IOKit/pwr_mgt/IOPMLib.h> | |
158 | # include <SystemConfiguration/SystemConfiguration.h> | |
159 | # include <pthread.h> | |
160 | ||
161 | ||
162 | /* | |
163 | * Constants... | |
164 | */ | |
165 | ||
a4d04587 | 166 | # define SYSEVENT_CANSLEEP 0x1 /* Decide whether to allow sleep or not */ |
167 | # define SYSEVENT_WILLSLEEP 0x2 /* Computer will go to sleep */ | |
168 | # define SYSEVENT_WOKE 0x4 /* Computer woke from sleep */ | |
169 | # define SYSEVENT_NETCHANGED 0x8 /* Network changed */ | |
170 | # define SYSEVENT_NAMECHANGED 0x10 /* Computer name changed */ | |
09ec0018 | 171 | |
172 | ||
173 | /* | |
174 | * Structures... | |
175 | */ | |
176 | ||
177 | typedef struct cupsd_sysevent_s /*** System event data ****/ | |
178 | { | |
179 | unsigned char event; /* Event bit field */ | |
180 | io_connect_t powerKernelPort; /* Power context data */ | |
181 | long powerNotificationID; /* Power event data */ | |
182 | } cupsd_sysevent_t; | |
183 | ||
184 | ||
185 | typedef struct cupsd_thread_data_s /*** Thread context data ****/ | |
186 | { | |
187 | cupsd_sysevent_t sysevent; /* System event */ | |
188 | CFRunLoopTimerRef timerRef; /* Timer to delay some change * | |
189 | * notifications */ | |
190 | } cupsd_thread_data_t; | |
191 | ||
192 | ||
193 | /* | |
194 | * Local globals... | |
195 | */ | |
196 | ||
197 | static pthread_t SysEventThread = NULL; | |
198 | /* Thread to host a runloop */ | |
199 | static pthread_mutex_t SysEventThreadMutex = { 0 }; | |
200 | /* Coordinates access to shared gloabals */ | |
201 | static pthread_cond_t SysEventThreadCond = { 0 }; | |
202 | /* Thread initialization complete condition */ | |
203 | static CFRunLoopRef SysEventRunloop = NULL; | |
204 | /* The runloop. Access must be protected! */ | |
205 | static CFStringRef ComputerNameKey = NULL, | |
206 | /* Computer name key */ | |
411affcf | 207 | NetworkGlobalKeyIPv4 = NULL, |
208 | /* Network global IPv4 key */ | |
209 | NetworkGlobalKeyIPv6 = NULL, | |
210 | /* Network global IPv6 key */ | |
211 | NetworkGlobalKeyDNS = NULL, | |
212 | /* Network global DNS key */ | |
09ec0018 | 213 | HostNamesKey = NULL, |
214 | /* Host name key */ | |
411affcf | 215 | NetworkInterfaceKeyIPv4 = NULL, |
216 | /* Netowrk interface key */ | |
217 | NetworkInterfaceKeyIPv6 = NULL; | |
09ec0018 | 218 | /* Netowrk interface key */ |
219 | ||
220 | ||
221 | /* | |
222 | * Local functions... | |
223 | */ | |
224 | ||
225 | static void *sysEventThreadEntry(void); | |
226 | static void sysEventPowerNotifier(void *context, io_service_t service, | |
227 | natural_t messageType, | |
228 | void *messageArgument); | |
229 | static void sysEventConfigurationNotifier(SCDynamicStoreRef store, | |
230 | CFArrayRef changedKeys, | |
231 | void *context); | |
232 | static void sysEventTimerNotifier(CFRunLoopTimerRef timer, void *context); | |
09ec0018 | 233 | |
234 | ||
235 | /* | |
236 | * 'cupsdStartSystemMonitor()' - Start monitoring for system change. | |
237 | */ | |
238 | ||
239 | void | |
240 | cupsdStartSystemMonitor(void) | |
241 | { | |
242 | int flags; /* fcntl flags on pipe */ | |
243 | ||
244 | ||
245 | if (cupsdOpenPipe(SysEventPipes)) | |
246 | { | |
247 | cupsdLogMessage(CUPSD_LOG_ERROR, "System event monitor pipe() failed - %s!", | |
248 | strerror(errno)); | |
249 | return; | |
250 | } | |
251 | ||
f7deaa1a | 252 | cupsdAddSelect(SysEventPipes[0], (cupsd_selfunc_t)cupsdUpdateSystemMonitor, |
253 | NULL, NULL); | |
09ec0018 | 254 | |
255 | /* | |
256 | * Set non-blocking mode on the descriptor we will be receiving notification | |
257 | * events on. | |
258 | */ | |
259 | ||
260 | flags = fcntl(SysEventPipes[0], F_GETFL, 0); | |
261 | fcntl(SysEventPipes[0], F_SETFL, flags | O_NONBLOCK); | |
262 | ||
263 | /* | |
264 | * Start the thread that runs the runloop... | |
265 | */ | |
266 | ||
267 | pthread_mutex_init(&SysEventThreadMutex, NULL); | |
268 | pthread_cond_init(&SysEventThreadCond, NULL); | |
269 | pthread_create(&SysEventThread, NULL, (void *(*)())sysEventThreadEntry, NULL); | |
270 | } | |
271 | ||
272 | ||
273 | /* | |
274 | * 'cupsdStopSystemMonitor()' - Stop monitoring for system change. | |
275 | */ | |
276 | ||
277 | void | |
278 | cupsdStopSystemMonitor(void) | |
279 | { | |
280 | CFRunLoopRef rl; /* The event handler runloop */ | |
281 | ||
282 | ||
283 | if (SysEventThread) | |
284 | { | |
285 | /* | |
286 | * Make sure the thread has completed it's initialization and | |
287 | * stored it's runloop reference in the shared global. | |
288 | */ | |
289 | ||
290 | pthread_mutex_lock(&SysEventThreadMutex); | |
291 | ||
292 | if (!SysEventRunloop) | |
293 | pthread_cond_wait(&SysEventThreadCond, &SysEventThreadMutex); | |
294 | ||
295 | rl = SysEventRunloop; | |
296 | SysEventRunloop = NULL; | |
297 | ||
298 | pthread_mutex_unlock(&SysEventThreadMutex); | |
299 | ||
300 | if (rl) | |
301 | CFRunLoopStop(rl); | |
302 | ||
303 | pthread_join(SysEventThread, NULL); | |
304 | pthread_mutex_destroy(&SysEventThreadMutex); | |
305 | pthread_cond_destroy(&SysEventThreadCond); | |
306 | } | |
307 | ||
308 | if (SysEventPipes[0] >= 0) | |
309 | { | |
f7deaa1a | 310 | cupsdRemoveSelect(SysEventPipes[0]); |
09ec0018 | 311 | cupsdClosePipe(SysEventPipes); |
312 | } | |
313 | } | |
314 | ||
315 | ||
316 | /* | |
317 | * 'cupsdUpdateSystemMonitor()' - Update the current system state. | |
318 | */ | |
319 | ||
320 | void | |
321 | cupsdUpdateSystemMonitor(void) | |
322 | { | |
757d2cad | 323 | int i; /* Looping var */ |
09ec0018 | 324 | cupsd_sysevent_t sysevent; /* The system event */ |
325 | cupsd_printer_t *p; /* Printer information */ | |
326 | ||
327 | ||
328 | /* | |
329 | * Drain the event pipe... | |
330 | */ | |
331 | ||
332 | while (read((int)SysEventPipes[0], &sysevent, sizeof(sysevent)) | |
333 | == sizeof(sysevent)) | |
334 | { | |
335 | if (sysevent.event & SYSEVENT_CANSLEEP) | |
336 | { | |
337 | /* | |
757d2cad | 338 | * If there are active printers that don't have the connecting-to-device |
339 | * printer-state-reason then cancel the sleep request (i.e. this reason | |
340 | * indicates a job that is not yet connected to the printer)... | |
09ec0018 | 341 | */ |
342 | ||
343 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
344 | p; | |
345 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
757d2cad | 346 | { |
09ec0018 | 347 | if (p->job) |
757d2cad | 348 | { |
349 | for (i = 0; i < p->num_reasons; i ++) | |
350 | if (!strcmp(p->reasons[i], "connecting-to-device")) | |
351 | break; | |
352 | ||
353 | if (!p->num_reasons || i >= p->num_reasons) | |
354 | break; | |
355 | } | |
356 | } | |
09ec0018 | 357 | |
358 | if (p) | |
359 | { | |
360 | cupsdLogMessage(CUPSD_LOG_INFO, | |
361 | "System sleep canceled because printer %s is active", | |
362 | p->name); | |
363 | IOCancelPowerChange(sysevent.powerKernelPort, | |
364 | sysevent.powerNotificationID); | |
365 | } | |
366 | else | |
367 | { | |
368 | cupsdLogMessage(CUPSD_LOG_DEBUG, "System wants to sleep"); | |
369 | IOAllowPowerChange(sysevent.powerKernelPort, | |
370 | sysevent.powerNotificationID); | |
371 | } | |
372 | } | |
373 | ||
374 | if (sysevent.event & SYSEVENT_WILLSLEEP) | |
375 | { | |
376 | cupsdLogMessage(CUPSD_LOG_DEBUG, "System going to sleep"); | |
377 | ||
378 | Sleeping = 1; | |
379 | ||
d09495fa | 380 | cupsdStopAllJobs(0); |
09ec0018 | 381 | |
382 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
383 | p; | |
384 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
385 | { | |
a41f09e2 | 386 | if (p->type & CUPS_PRINTER_DISCOVERED) |
09ec0018 | 387 | { |
388 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
389 | "Deleting remote destination \"%s\"", p->name); | |
390 | cupsArraySave(Printers); | |
391 | cupsdDeletePrinter(p, 0); | |
392 | cupsArrayRestore(Printers); | |
393 | } | |
394 | else | |
395 | { | |
09ec0018 | 396 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
397 | "Deregistering local printer \"%s\"", p->name); | |
f7deaa1a | 398 | cupsdDeregisterPrinter(p, 0); |
09ec0018 | 399 | } |
400 | } | |
401 | ||
3dfe78b3 MS |
402 | cupsdCleanDirty(); |
403 | ||
09ec0018 | 404 | IOAllowPowerChange(sysevent.powerKernelPort, |
405 | sysevent.powerNotificationID); | |
406 | } | |
407 | ||
408 | if (sysevent.event & SYSEVENT_WOKE) | |
409 | { | |
410 | cupsdLogMessage(CUPSD_LOG_DEBUG, "System woke from sleep"); | |
411 | IOAllowPowerChange(sysevent.powerKernelPort, | |
412 | sysevent.powerNotificationID); | |
413 | Sleeping = 0; | |
414 | cupsdCheckJobs(); | |
415 | } | |
416 | ||
417 | if (sysevent.event & SYSEVENT_NETCHANGED) | |
418 | { | |
419 | if (!Sleeping) | |
420 | { | |
421 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
422 | "System network configuration changed"); | |
423 | ||
09ec0018 | 424 | /* |
425 | * Resetting browse_time before calling cupsdSendBrowseList causes | |
426 | * browse packets to be sent for local shared printers. | |
427 | */ | |
428 | ||
429 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
430 | p; | |
431 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
432 | p->browse_time = 0; | |
433 | ||
434 | cupsdSendBrowseList(); | |
d09495fa | 435 | cupsdRestartPolling(); |
09ec0018 | 436 | } |
437 | else | |
438 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
439 | "System network configuration changed; " | |
440 | "ignored while sleeping"); | |
441 | } | |
442 | ||
443 | if (sysevent.event & SYSEVENT_NAMECHANGED) | |
444 | { | |
445 | if (!Sleeping) | |
446 | { | |
447 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Computer name changed"); | |
448 | ||
449 | /* | |
450 | * De-register the individual printers... | |
451 | */ | |
452 | ||
453 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
454 | p; | |
455 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
f7deaa1a | 456 | cupsdDeregisterPrinter(p, 1); |
09ec0018 | 457 | |
458 | /* | |
459 | * Now re-register them... | |
09ec0018 | 460 | */ |
461 | ||
462 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
463 | p; | |
464 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
f7deaa1a | 465 | { |
09ec0018 | 466 | p->browse_time = 0; |
f7deaa1a | 467 | cupsdRegisterPrinter(p); |
468 | } | |
09ec0018 | 469 | } |
470 | else | |
471 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
472 | "Computer name changed; ignored while sleeping"); | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | ||
478 | /* | |
479 | * 'sysEventThreadEntry()' - A thread to receive power and computer name | |
480 | * change notifications. | |
481 | */ | |
482 | ||
483 | static void * /* O - Return status/value */ | |
484 | sysEventThreadEntry(void) | |
485 | { | |
486 | io_object_t powerNotifierObj; | |
487 | /* Power notifier object */ | |
488 | IONotificationPortRef powerNotifierPort; | |
489 | /* Power notifier port */ | |
490 | SCDynamicStoreRef store = NULL;/* System Config dynamic store */ | |
491 | CFRunLoopSourceRef powerRLS = NULL,/* Power runloop source */ | |
492 | storeRLS = NULL;/* System Config runloop source */ | |
411affcf | 493 | CFStringRef key[5], /* System Config keys */ |
494 | pattern[2]; /* System Config patterns */ | |
09ec0018 | 495 | CFArrayRef keys = NULL, /* System Config key array*/ |
496 | patterns = NULL;/* System Config pattern array */ | |
497 | SCDynamicStoreContext storeContext; /* Dynamic store context */ | |
498 | CFRunLoopTimerContext timerContext; /* Timer context */ | |
499 | cupsd_thread_data_t threadData; /* Thread context data for the * | |
500 | * runloop notifiers */ | |
501 | ||
502 | ||
503 | /* | |
504 | * Register for power state change notifications | |
505 | */ | |
506 | ||
507 | bzero(&threadData, sizeof(threadData)); | |
508 | ||
509 | threadData.sysevent.powerKernelPort = | |
510 | IORegisterForSystemPower(&threadData, &powerNotifierPort, | |
511 | sysEventPowerNotifier, &powerNotifierObj); | |
512 | ||
513 | if (threadData.sysevent.powerKernelPort) | |
514 | { | |
515 | powerRLS = IONotificationPortGetRunLoopSource(powerNotifierPort); | |
516 | CFRunLoopAddSource(CFRunLoopGetCurrent(), powerRLS, kCFRunLoopDefaultMode); | |
517 | } | |
518 | else | |
519 | DEBUG_puts("sysEventThreadEntry: error registering for system power " | |
520 | "notifications"); | |
521 | ||
522 | /* | |
523 | * Register for system configuration change notifications | |
524 | */ | |
525 | ||
526 | bzero(&storeContext, sizeof(storeContext)); | |
527 | storeContext.info = &threadData; | |
528 | ||
529 | store = SCDynamicStoreCreate(NULL, CFSTR("cupsd"), | |
530 | sysEventConfigurationNotifier, &storeContext); | |
531 | ||
532 | if (!ComputerNameKey) | |
533 | ComputerNameKey = SCDynamicStoreKeyCreateComputerName(NULL); | |
534 | ||
411affcf | 535 | if (!NetworkGlobalKeyIPv4) |
536 | NetworkGlobalKeyIPv4 = | |
09ec0018 | 537 | SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, |
538 | kSCDynamicStoreDomainState, | |
539 | kSCEntNetIPv4); | |
540 | ||
411affcf | 541 | if (!NetworkGlobalKeyIPv6) |
542 | NetworkGlobalKeyIPv6 = | |
543 | SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, | |
544 | kSCDynamicStoreDomainState, | |
545 | kSCEntNetIPv6); | |
546 | ||
547 | if (!NetworkGlobalKeyDNS) | |
548 | NetworkGlobalKeyDNS = | |
549 | SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, | |
550 | kSCDynamicStoreDomainState, | |
551 | kSCEntNetDNS); | |
552 | ||
09ec0018 | 553 | if (!HostNamesKey) |
554 | HostNamesKey = SCDynamicStoreKeyCreateHostNames(NULL); | |
555 | ||
411affcf | 556 | if (!NetworkInterfaceKeyIPv4) |
557 | NetworkInterfaceKeyIPv4 = | |
09ec0018 | 558 | SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL, |
559 | kSCDynamicStoreDomainState, | |
560 | kSCCompAnyRegex, | |
561 | kSCEntNetIPv4); | |
562 | ||
411affcf | 563 | if (!NetworkInterfaceKeyIPv6) |
564 | NetworkInterfaceKeyIPv6 = | |
565 | SCDynamicStoreKeyCreateNetworkInterfaceEntity(NULL, | |
566 | kSCDynamicStoreDomainState, | |
567 | kSCCompAnyRegex, | |
568 | kSCEntNetIPv6); | |
569 | ||
570 | if (store && ComputerNameKey && HostNamesKey && | |
571 | NetworkGlobalKeyIPv4 && NetworkGlobalKeyIPv6 && NetworkGlobalKeyDNS && | |
572 | NetworkInterfaceKeyIPv4 && NetworkInterfaceKeyIPv6) | |
09ec0018 | 573 | { |
574 | key[0] = ComputerNameKey; | |
411affcf | 575 | key[1] = NetworkGlobalKeyIPv4; |
576 | key[2] = NetworkGlobalKeyIPv6; | |
577 | key[3] = NetworkGlobalKeyDNS; | |
578 | key[4] = HostNamesKey; | |
579 | ||
580 | pattern[0] = NetworkInterfaceKeyIPv4; | |
581 | pattern[1] = NetworkInterfaceKeyIPv6; | |
09ec0018 | 582 | |
583 | keys = CFArrayCreate(NULL, (const void **)key, | |
584 | sizeof(key) / sizeof(key[0]), | |
585 | &kCFTypeArrayCallBacks); | |
411affcf | 586 | |
09ec0018 | 587 | patterns = CFArrayCreate(NULL, (const void **)pattern, |
588 | sizeof(pattern) / sizeof(pattern[0]), | |
589 | &kCFTypeArrayCallBacks); | |
590 | ||
591 | if (keys && patterns && | |
592 | SCDynamicStoreSetNotificationKeys(store, keys, patterns)) | |
593 | { | |
594 | if ((storeRLS = SCDynamicStoreCreateRunLoopSource(NULL, store, 0)) | |
595 | != NULL) | |
596 | { | |
597 | CFRunLoopAddSource(CFRunLoopGetCurrent(), storeRLS, | |
598 | kCFRunLoopDefaultMode); | |
599 | } | |
600 | else | |
601 | DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreateRunLoopSource " | |
602 | "failed: %s\n", SCErrorString(SCError()))); | |
603 | } | |
604 | else | |
605 | DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreSetNotificationKeys " | |
606 | "failed: %s\n", SCErrorString(SCError()))); | |
607 | } | |
608 | else | |
609 | DEBUG_printf(("sysEventThreadEntry: SCDynamicStoreCreate failed: %s\n", | |
610 | SCErrorString(SCError()))); | |
611 | ||
612 | if (keys) | |
613 | CFRelease(keys); | |
614 | ||
615 | if (patterns) | |
616 | CFRelease(patterns); | |
617 | ||
618 | /* | |
619 | * Set up a timer to delay the wake change notifications. | |
620 | * | |
621 | * The initial time is set a decade or so into the future, we'll adjust | |
622 | * this later. | |
623 | */ | |
624 | ||
625 | bzero(&timerContext, sizeof(timerContext)); | |
626 | timerContext.info = &threadData; | |
627 | ||
628 | threadData.timerRef = | |
629 | CFRunLoopTimerCreate(NULL, | |
630 | CFAbsoluteTimeGetCurrent() + (86400L * 365L * 10L), | |
631 | 86400L * 365L * 10L, 0, 0, sysEventTimerNotifier, | |
632 | &timerContext); | |
633 | CFRunLoopAddTimer(CFRunLoopGetCurrent(), threadData.timerRef, | |
634 | kCFRunLoopDefaultMode); | |
635 | ||
636 | /* | |
637 | * Store our runloop in a global so the main thread can use it to stop us. | |
638 | */ | |
639 | ||
640 | pthread_mutex_lock(&SysEventThreadMutex); | |
641 | ||
642 | SysEventRunloop = CFRunLoopGetCurrent(); | |
643 | ||
644 | pthread_cond_signal(&SysEventThreadCond); | |
645 | pthread_mutex_unlock(&SysEventThreadMutex); | |
646 | ||
647 | /* | |
648 | * Disappear into the runloop until it's stopped by the main thread. | |
649 | */ | |
650 | ||
651 | CFRunLoopRun(); | |
652 | ||
653 | /* | |
654 | * Clean up before exiting. | |
655 | */ | |
656 | ||
657 | if (threadData.timerRef) | |
658 | { | |
659 | CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), threadData.timerRef, | |
660 | kCFRunLoopDefaultMode); | |
661 | CFRelease(threadData.timerRef); | |
662 | } | |
663 | ||
664 | if (threadData.sysevent.powerKernelPort) | |
665 | { | |
666 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), powerRLS, | |
667 | kCFRunLoopDefaultMode); | |
668 | IODeregisterForSystemPower(&powerNotifierObj); | |
669 | IOServiceClose(threadData.sysevent.powerKernelPort); | |
670 | IONotificationPortDestroy(powerNotifierPort); | |
671 | } | |
672 | ||
673 | if (storeRLS) | |
674 | { | |
675 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), storeRLS, | |
676 | kCFRunLoopDefaultMode); | |
677 | CFRunLoopSourceInvalidate(storeRLS); | |
678 | CFRelease(storeRLS); | |
679 | } | |
680 | ||
681 | if (store) | |
682 | CFRelease(store); | |
683 | ||
684 | pthread_exit(NULL); | |
685 | } | |
686 | ||
687 | ||
688 | /* | |
689 | * 'sysEventPowerNotifier()' - Handle power notification events. | |
690 | */ | |
691 | ||
692 | static void | |
693 | sysEventPowerNotifier( | |
694 | void *context, /* I - Thread context data */ | |
695 | io_service_t service, /* I - Unused service info */ | |
696 | natural_t messageType, /* I - Type of message */ | |
697 | void *messageArgument) /* I - Message data */ | |
698 | { | |
699 | int sendit = 1; /* Send event to main thread? * | |
700 | * (0 = no, 1 = yes, 2 = delayed */ | |
701 | cupsd_thread_data_t *threadData; /* Thread context data */ | |
702 | ||
703 | ||
704 | threadData = (cupsd_thread_data_t *)context; | |
705 | ||
706 | (void)service; /* anti-compiler-warning-code */ | |
707 | ||
708 | switch (messageType) | |
709 | { | |
710 | case kIOMessageCanSystemPowerOff: | |
711 | case kIOMessageCanSystemSleep: | |
712 | threadData->sysevent.event |= SYSEVENT_CANSLEEP; | |
713 | break; | |
714 | ||
715 | case kIOMessageSystemWillRestart: | |
716 | case kIOMessageSystemWillPowerOff: | |
717 | case kIOMessageSystemWillSleep: | |
718 | threadData->sysevent.event |= SYSEVENT_WILLSLEEP; | |
719 | break; | |
720 | ||
721 | case kIOMessageSystemHasPoweredOn: | |
722 | /* | |
723 | * Because powered on is followed by a net-changed event, delay | |
724 | * before sending it. | |
725 | */ | |
726 | ||
727 | sendit = 2; | |
728 | threadData->sysevent.event |= SYSEVENT_WOKE; | |
729 | break; | |
730 | ||
731 | case kIOMessageSystemWillNotPowerOff: | |
732 | case kIOMessageSystemWillNotSleep: | |
4400e98d | 733 | #ifdef kIOMessageSystemWillPowerOn |
09ec0018 | 734 | case kIOMessageSystemWillPowerOn: |
4400e98d | 735 | #endif /* kIOMessageSystemWillPowerOn */ |
09ec0018 | 736 | default: |
737 | sendit = 0; | |
738 | break; | |
739 | } | |
740 | ||
741 | if (sendit == 0) | |
742 | IOAllowPowerChange(threadData->sysevent.powerKernelPort, | |
743 | (long)messageArgument); | |
744 | else | |
745 | { | |
746 | threadData->sysevent.powerNotificationID = (long)messageArgument; | |
747 | ||
748 | if (sendit == 1) | |
749 | { | |
750 | /* | |
751 | * Send the event to the main thread now. | |
752 | */ | |
753 | ||
754 | write(SysEventPipes[1], &threadData->sysevent, | |
755 | sizeof(threadData->sysevent)); | |
756 | threadData->sysevent.event = 0; | |
757 | } | |
758 | else | |
759 | { | |
760 | /* | |
761 | * Send the event to the main thread after 1 to 2 seconds. | |
762 | */ | |
763 | ||
764 | CFRunLoopTimerSetNextFireDate(threadData->timerRef, | |
765 | CFAbsoluteTimeGetCurrent() + 2); | |
766 | } | |
767 | } | |
768 | } | |
769 | ||
770 | ||
771 | /* | |
772 | * 'sysEventConfigurationNotifier()' - Computer name changed notification | |
773 | * callback. | |
774 | */ | |
775 | ||
776 | static void | |
777 | sysEventConfigurationNotifier( | |
778 | SCDynamicStoreRef store, /* I - System data (unused) */ | |
779 | CFArrayRef changedKeys, /* I - Changed data */ | |
780 | void *context) /* I - Thread context data */ | |
781 | { | |
782 | cupsd_thread_data_t *threadData; /* Thread context data */ | |
783 | ||
784 | ||
785 | threadData = (cupsd_thread_data_t *)context; | |
786 | ||
787 | (void)store; /* anti-compiler-warning-code */ | |
788 | ||
789 | CFRange range = CFRangeMake(0, CFArrayGetCount(changedKeys)); | |
790 | ||
791 | if (CFArrayContainsValue(changedKeys, range, ComputerNameKey)) | |
792 | threadData->sysevent.event |= SYSEVENT_NAMECHANGED; | |
411affcf | 793 | else |
794 | { | |
09ec0018 | 795 | threadData->sysevent.event |= SYSEVENT_NETCHANGED; |
796 | ||
411affcf | 797 | /* |
798 | * Indicate the network interface list needs updating... | |
799 | */ | |
800 | ||
801 | NetIFUpdate = 1; | |
802 | } | |
803 | ||
09ec0018 | 804 | /* |
805 | * Because we registered for several different kinds of change notifications | |
806 | * this callback usually gets called several times in a row. We use a timer to | |
807 | * de-bounce these so we only end up generating one event for the main thread. | |
808 | */ | |
809 | ||
810 | CFRunLoopTimerSetNextFireDate(threadData->timerRef, | |
411affcf | 811 | CFAbsoluteTimeGetCurrent() + 5); |
09ec0018 | 812 | } |
813 | ||
814 | ||
815 | /* | |
816 | * 'sysEventTimerNotifier()' - Handle delayed event notifications. | |
817 | */ | |
818 | ||
819 | static void | |
820 | sysEventTimerNotifier( | |
821 | CFRunLoopTimerRef timer, /* I - Timer information */ | |
822 | void *context) /* I - Thread context data */ | |
823 | { | |
824 | cupsd_thread_data_t *threadData; /* Thread context data */ | |
825 | ||
826 | ||
827 | threadData = (cupsd_thread_data_t *)context; | |
828 | ||
829 | /* | |
830 | * If an event is still pending send it to the main thread. | |
831 | */ | |
832 | ||
833 | if (threadData->sysevent.event) | |
834 | { | |
835 | write(SysEventPipes[1], &threadData->sysevent, | |
836 | sizeof(threadData->sysevent)); | |
837 | threadData->sysevent.event = 0; | |
838 | } | |
839 | } | |
a4d04587 | 840 | #endif /* __APPLE__ */ |
09ec0018 | 841 | |
842 | ||
843 | /* | |
bc44d920 | 844 | * End of "$Id: sysman.c 6649 2007-07-11 21:46:42Z mike $". |
09ec0018 | 845 | */ |