]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: subscriptions.c 7824 2008-08-01 21:11:55Z mike $" |
ef416fc2 | 3 | * |
10d09e33 | 4 | * Subscription routines for the CUPS scheduler. |
ef416fc2 | 5 | * |
0268488e | 6 | * Copyright 2007-2011 by Apple Inc. |
f7deaa1a | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * cupsdAddEvent() - Add an event to the global event cache. | |
18 | * cupsdAddSubscription() - Add a new subscription object. | |
ef416fc2 | 19 | * cupsdDeleteAllSubscriptions() - Delete all subscriptions. |
20 | * cupsdDeleteSubscription() - Delete a subscription object. | |
21 | * cupsdEventName() - Return a single event name. | |
22 | * cupsdEventValue() - Return the event mask value for a name. | |
23 | * cupsdExpireSubscriptions() - Expire old subscription objects. | |
24 | * cupsdFindSubscription() - Find a subscription by ID. | |
25 | * cupsdLoadAllSubscriptions() - Load all subscriptions from the .conf file. | |
26 | * cupsdSaveAllSubscriptions() - Save all subscriptions to the .conf file. | |
ef416fc2 | 27 | * cupsdStopAllNotifiers() - Stop all notifier processes. |
ef416fc2 | 28 | * cupsd_compare_subscriptions() - Compare two subscriptions. |
29 | * cupsd_delete_event() - Delete a single event... | |
e00b005a | 30 | * cupsd_send_dbus() - Send a DBUS notification... |
e1d6a774 | 31 | * cupsd_send_notification() - Send a notification for the specified |
32 | * event. | |
ef416fc2 | 33 | * cupsd_start_notifier() - Start a notifier subprocess... |
f899b121 | 34 | * cupsd_update_notifier() - Read messages from notifiers. |
ef416fc2 | 35 | */ |
36 | ||
37 | /* | |
38 | * Include necessary headers... | |
39 | */ | |
40 | ||
41 | #include "cupsd.h" | |
e00b005a | 42 | #ifdef HAVE_DBUS |
43 | # include <dbus/dbus.h> | |
2abf387c | 44 | # ifdef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND |
45 | # define dbus_message_append_iter_init dbus_message_iter_init_append | |
f7deaa1a | 46 | # define dbus_message_iter_append_string(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &(v)) |
47 | # define dbus_message_iter_append_uint32(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &(v)) | |
2abf387c | 48 | # endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */ |
e00b005a | 49 | #endif /* HAVE_DBUS */ |
ef416fc2 | 50 | |
51 | ||
52 | /* | |
53 | * Local functions... | |
54 | */ | |
55 | ||
56 | static int cupsd_compare_subscriptions(cupsd_subscription_t *first, | |
57 | cupsd_subscription_t *second, | |
58 | void *unused); | |
59 | static void cupsd_delete_event(cupsd_event_t *event); | |
e00b005a | 60 | #ifdef HAVE_DBUS |
61 | static void cupsd_send_dbus(cupsd_eventmask_t event, cupsd_printer_t *dest, | |
62 | cupsd_job_t *job); | |
63 | #endif /* HAVE_DBUS */ | |
e1d6a774 | 64 | static void cupsd_send_notification(cupsd_subscription_t *sub, |
65 | cupsd_event_t *event); | |
ef416fc2 | 66 | static void cupsd_start_notifier(cupsd_subscription_t *sub); |
f899b121 | 67 | static void cupsd_update_notifier(void); |
ef416fc2 | 68 | |
69 | ||
70 | /* | |
71 | * 'cupsdAddEvent()' - Add an event to the global event cache. | |
72 | */ | |
73 | ||
74 | void | |
75 | cupsdAddEvent( | |
76 | cupsd_eventmask_t event, /* I - Event */ | |
77 | cupsd_printer_t *dest, /* I - Printer associated with event */ | |
78 | cupsd_job_t *job, /* I - Job associated with event */ | |
79 | const char *text, /* I - Notification text */ | |
80 | ...) /* I - Additional arguments as needed */ | |
81 | { | |
82 | va_list ap; /* Pointer to additional arguments */ | |
83 | char ftext[1024]; /* Formatted text buffer */ | |
fa73b229 | 84 | ipp_attribute_t *attr; /* Printer/job attribute */ |
ef416fc2 | 85 | cupsd_event_t *temp; /* New event pointer */ |
86 | cupsd_subscription_t *sub; /* Current subscription */ | |
87 | ||
88 | ||
8ca02f3c | 89 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
90 | "cupsdAddEvent(event=%s, dest=%p(%s), job=%p(%d), text=\"%s\", ...)", | |
91 | cupsdEventName(event), dest, dest ? dest->name : "", | |
92 | job, job ? job->id : 0, text); | |
93 | ||
e00b005a | 94 | /* |
95 | * Keep track of events with any OS-supplied notification mechanisms... | |
96 | */ | |
97 | ||
fa73b229 | 98 | LastEvent |= event; |
99 | ||
e00b005a | 100 | #ifdef HAVE_DBUS |
101 | cupsd_send_dbus(event, dest, job); | |
102 | #endif /* HAVE_DBUS */ | |
103 | ||
ef416fc2 | 104 | /* |
105 | * Return if we aren't keeping events... | |
106 | */ | |
107 | ||
108 | if (MaxEvents <= 0) | |
109 | { | |
110 | cupsdLogMessage(CUPSD_LOG_WARN, | |
111 | "cupsdAddEvent: Discarding %s event since MaxEvents is %d!", | |
112 | cupsdEventName(event), MaxEvents); | |
113 | return; | |
114 | } | |
115 | ||
ef416fc2 | 116 | /* |
117 | * Then loop through the subscriptions and add the event to the corresponding | |
118 | * caches... | |
119 | */ | |
120 | ||
ed486911 | 121 | for (temp = NULL, sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); |
ef416fc2 | 122 | sub; |
123 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
124 | { | |
125 | /* | |
126 | * Check if this subscription requires this event... | |
127 | */ | |
128 | ||
129 | if ((sub->mask & event) != 0 && | |
130 | (sub->dest == dest || !sub->dest) && | |
131 | (sub->job == job || !sub->job)) | |
132 | { | |
133 | /* | |
ed486911 | 134 | * Need this event, so create a new event record... |
ef416fc2 | 135 | */ |
136 | ||
ed486911 | 137 | if ((temp = (cupsd_event_t *)calloc(1, sizeof(cupsd_event_t))) == NULL) |
ef416fc2 | 138 | { |
ed486911 | 139 | cupsdLogMessage(CUPSD_LOG_CRIT, |
140 | "Unable to allocate memory for event - %s", | |
141 | strerror(errno)); | |
142 | return; | |
143 | } | |
ef416fc2 | 144 | |
ed486911 | 145 | temp->event = event; |
146 | temp->time = time(NULL); | |
147 | temp->attrs = ippNew(); | |
148 | temp->job = job; | |
149 | temp->dest = dest; | |
ef416fc2 | 150 | |
ed486911 | 151 | /* |
152 | * Add common event notification attributes... | |
153 | */ | |
ef416fc2 | 154 | |
ed486911 | 155 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_CHARSET, |
156 | "notify-charset", NULL, "utf-8"); | |
ef416fc2 | 157 | |
ed486911 | 158 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_LANGUAGE, |
0268488e | 159 | "notify-natural-language", NULL, "en-US"); |
ef416fc2 | 160 | |
ed486911 | 161 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER, |
162 | "notify-subscription-id", sub->id); | |
ef416fc2 | 163 | |
ed486911 | 164 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER, |
165 | "notify-sequence-number", sub->next_event_id); | |
ef416fc2 | 166 | |
ed486911 | 167 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_KEYWORD, |
168 | "notify-subscribed-event", NULL, cupsdEventName(event)); | |
ef416fc2 | 169 | |
ed486911 | 170 | if (sub->user_data_len > 0) |
171 | ippAddOctetString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
172 | "notify-user-data", sub->user_data, | |
173 | sub->user_data_len); | |
ef416fc2 | 174 | |
ed486911 | 175 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER, |
176 | "printer-up-time", time(NULL)); | |
ef416fc2 | 177 | |
ed486911 | 178 | va_start(ap, text); |
179 | vsnprintf(ftext, sizeof(ftext), text, ap); | |
180 | va_end(ap); | |
ef416fc2 | 181 | |
ed486911 | 182 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_TEXT, |
183 | "notify-text", NULL, ftext); | |
ef416fc2 | 184 | |
ed486911 | 185 | if (dest) |
186 | { | |
187 | /* | |
188 | * Add printer attributes... | |
189 | */ | |
ef416fc2 | 190 | |
ed486911 | 191 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_URI, |
192 | "notify-printer-uri", NULL, dest->uri); | |
ef416fc2 | 193 | |
ed486911 | 194 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_NAME, |
195 | "printer-name", NULL, dest->name); | |
ef416fc2 | 196 | |
ed486911 | 197 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_ENUM, |
198 | "printer-state", dest->state); | |
ef416fc2 | 199 | |
ed486911 | 200 | if (dest->num_reasons == 0) |
201 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
202 | IPP_TAG_KEYWORD, "printer-state-reasons", NULL, | |
203 | dest->state == IPP_PRINTER_STOPPED ? "paused" : "none"); | |
204 | else | |
205 | ippAddStrings(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
206 | IPP_TAG_KEYWORD, "printer-state-reasons", | |
207 | dest->num_reasons, NULL, | |
208 | (const char * const *)dest->reasons); | |
ef416fc2 | 209 | |
ed486911 | 210 | ippAddBoolean(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, |
211 | "printer-is-accepting-jobs", dest->accepting); | |
212 | } | |
fa73b229 | 213 | |
ed486911 | 214 | if (job) |
215 | { | |
216 | /* | |
217 | * Add job attributes... | |
218 | */ | |
ef416fc2 | 219 | |
ed486911 | 220 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER, |
221 | "notify-job-id", job->id); | |
222 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_ENUM, | |
223 | "job-state", job->state_value); | |
224 | ||
225 | if ((attr = ippFindAttribute(job->attrs, "job-name", | |
226 | IPP_TAG_NAME)) != NULL) | |
227 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_NAME, | |
228 | "job-name", NULL, attr->values[0].string.text); | |
229 | ||
230 | switch (job->state_value) | |
231 | { | |
232 | case IPP_JOB_PENDING : | |
233 | if (dest && dest->state == IPP_PRINTER_STOPPED) | |
ef416fc2 | 234 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, |
235 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
ed486911 | 236 | "printer-stopped"); |
237 | else | |
ef416fc2 | 238 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, |
239 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
ed486911 | 240 | "none"); |
241 | break; | |
ef416fc2 | 242 | |
ed486911 | 243 | case IPP_JOB_HELD : |
244 | if (ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_KEYWORD) != NULL || | |
245 | ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME) != NULL) | |
ef416fc2 | 246 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, |
247 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
ed486911 | 248 | "job-hold-until-specified"); |
249 | else | |
ef416fc2 | 250 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, |
251 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
ed486911 | 252 | "job-incoming"); |
253 | break; | |
254 | ||
255 | case IPP_JOB_PROCESSING : | |
256 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
257 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
258 | "job-printing"); | |
259 | break; | |
260 | ||
261 | case IPP_JOB_STOPPED : | |
262 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
263 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
264 | "job-stopped"); | |
265 | break; | |
266 | ||
d09495fa | 267 | case IPP_JOB_CANCELED : |
ed486911 | 268 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, |
269 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
270 | "job-canceled-by-user"); | |
271 | break; | |
272 | ||
273 | case IPP_JOB_ABORTED : | |
274 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
275 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
276 | "aborted-by-system"); | |
277 | break; | |
278 | ||
279 | case IPP_JOB_COMPLETED : | |
280 | ippAddString(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, | |
281 | IPP_TAG_KEYWORD, "job-state-reasons", NULL, | |
282 | "job-completed-successfully"); | |
283 | break; | |
ef416fc2 | 284 | } |
285 | ||
ed486911 | 286 | ippAddInteger(temp->attrs, IPP_TAG_EVENT_NOTIFICATION, IPP_TAG_INTEGER, |
287 | "job-impressions-completed", | |
288 | job->sheets ? job->sheets->values[0].integer : 0); | |
8ca02f3c | 289 | } |
ef416fc2 | 290 | |
8ca02f3c | 291 | /* |
292 | * Send the notification for this subscription... | |
293 | */ | |
ef416fc2 | 294 | |
8ca02f3c | 295 | cupsd_send_notification(sub, temp); |
ef416fc2 | 296 | } |
297 | } | |
298 | ||
299 | if (temp) | |
3dfe78b3 | 300 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
ef416fc2 | 301 | else |
302 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Discarding unused %s event...", | |
303 | cupsdEventName(event)); | |
304 | } | |
305 | ||
306 | ||
307 | /* | |
308 | * 'cupsdAddSubscription()' - Add a new subscription object. | |
309 | */ | |
310 | ||
311 | cupsd_subscription_t * /* O - New subscription object */ | |
312 | cupsdAddSubscription( | |
313 | unsigned mask, /* I - Event mask */ | |
314 | cupsd_printer_t *dest, /* I - Printer, if any */ | |
315 | cupsd_job_t *job, /* I - Job, if any */ | |
316 | const char *uri, /* I - notify-recipient-uri, if any */ | |
317 | int sub_id) /* I - notify-subscription-id or 0 */ | |
318 | { | |
319 | cupsd_subscription_t *temp; /* New subscription object */ | |
320 | ||
321 | ||
322 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
e1d6a774 | 323 | "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), " |
324 | "uri=\"%s\")", | |
325 | mask, dest, dest ? dest->name : "", job, job ? job->id : 0, | |
d9bca400 | 326 | uri ? uri : "(null)"); |
ef416fc2 | 327 | |
328 | if (!Subscriptions) | |
329 | Subscriptions = cupsArrayNew((cups_array_func_t)cupsd_compare_subscriptions, | |
330 | NULL); | |
331 | ||
332 | if (!Subscriptions) | |
333 | { | |
334 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
335 | "Unable to allocate memory for subscriptions - %s", | |
336 | strerror(errno)); | |
337 | return (NULL); | |
338 | } | |
339 | ||
340 | /* | |
341 | * Limit the number of subscriptions... | |
342 | */ | |
343 | ||
52f6f666 MS |
344 | if (MaxSubscriptions > 0 && cupsArrayCount(Subscriptions) >= MaxSubscriptions) |
345 | { | |
346 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
347 | "cupsdAddSubscription: Reached MaxSubscriptions %d " | |
348 | "(count=%d)", MaxSubscriptions, | |
349 | cupsArrayCount(Subscriptions)); | |
ef416fc2 | 350 | return (NULL); |
52f6f666 MS |
351 | } |
352 | ||
353 | if (MaxSubscriptionsPerJob > 0 && job) | |
354 | { | |
355 | int count; /* Number of job subscriptions */ | |
356 | ||
357 | for (temp = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), | |
358 | count = 0; | |
359 | temp; | |
360 | temp = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
361 | if (temp->job == job) | |
362 | count ++; | |
363 | ||
364 | if (count >= MaxSubscriptionsPerJob) | |
365 | { | |
366 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
367 | "cupsdAddSubscription: Reached MaxSubscriptionsPerJob %d " | |
368 | "for job #%d (count=%d)", MaxSubscriptionsPerJob, | |
369 | job->id, count); | |
370 | return (NULL); | |
371 | } | |
372 | } | |
373 | ||
374 | if (MaxSubscriptionsPerPrinter > 0 && dest) | |
375 | { | |
376 | int count; /* Number of printer subscriptions */ | |
377 | ||
378 | for (temp = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), | |
379 | count = 0; | |
380 | temp; | |
381 | temp = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
382 | if (temp->dest == dest) | |
383 | count ++; | |
384 | ||
385 | if (count >= MaxSubscriptionsPerPrinter) | |
386 | { | |
387 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
388 | "cupsdAddSubscription: Reached " | |
389 | "MaxSubscriptionsPerPrinter %d for %s (count=%d)", | |
390 | MaxSubscriptionsPerPrinter, dest->name, count); | |
391 | return (NULL); | |
392 | } | |
393 | } | |
ef416fc2 | 394 | |
395 | /* | |
396 | * Allocate memory for this subscription... | |
397 | */ | |
398 | ||
399 | if ((temp = calloc(1, sizeof(cupsd_subscription_t))) == NULL) | |
400 | { | |
401 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
402 | "Unable to allocate memory for subscription object - %s", | |
403 | strerror(errno)); | |
404 | return (NULL); | |
405 | } | |
406 | ||
407 | /* | |
408 | * Fill in common data... | |
409 | */ | |
410 | ||
411 | if (sub_id) | |
412 | { | |
413 | temp->id = sub_id; | |
414 | ||
415 | if (sub_id >= NextSubscriptionId) | |
416 | NextSubscriptionId = sub_id + 1; | |
417 | } | |
418 | else | |
419 | { | |
420 | temp->id = NextSubscriptionId; | |
421 | ||
422 | NextSubscriptionId ++; | |
423 | } | |
424 | ||
425 | temp->mask = mask; | |
426 | temp->dest = dest; | |
427 | temp->job = job; | |
428 | temp->pipe = -1; | |
429 | temp->first_event_id = 1; | |
430 | temp->next_event_id = 1; | |
431 | ||
432 | cupsdSetString(&(temp->recipient), uri); | |
433 | ||
434 | /* | |
435 | * Add the subscription to the array... | |
436 | */ | |
437 | ||
438 | cupsArrayAdd(Subscriptions, temp); | |
439 | ||
ee571f26 MS |
440 | /* |
441 | * For RSS subscriptions, run the notifier immediately... | |
442 | */ | |
443 | ||
444 | if (uri && !strncmp(uri, "rss:", 4)) | |
445 | cupsd_start_notifier(temp); | |
446 | ||
ef416fc2 | 447 | return (temp); |
448 | } | |
449 | ||
450 | ||
ef416fc2 | 451 | /* |
452 | * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions. | |
453 | */ | |
454 | ||
455 | void | |
456 | cupsdDeleteAllSubscriptions(void) | |
457 | { | |
458 | cupsd_subscription_t *sub; /* Subscription */ | |
459 | ||
460 | ||
461 | if (!Subscriptions) | |
462 | return; | |
463 | ||
464 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
465 | sub; | |
466 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
467 | cupsdDeleteSubscription(sub, 0); | |
468 | ||
469 | cupsArrayDelete(Subscriptions); | |
470 | Subscriptions = NULL; | |
471 | } | |
472 | ||
473 | ||
474 | /* | |
475 | * 'cupsdDeleteSubscription()' - Delete a subscription object. | |
476 | */ | |
477 | ||
478 | void | |
479 | cupsdDeleteSubscription( | |
480 | cupsd_subscription_t *sub, /* I - Subscription object */ | |
481 | int update) /* I - 1 = update subscriptions.conf */ | |
482 | { | |
483 | /* | |
484 | * Close the pipe to the notifier as needed... | |
485 | */ | |
486 | ||
487 | if (sub->pipe >= 0) | |
488 | close(sub->pipe); | |
489 | ||
490 | /* | |
491 | * Remove subscription from array... | |
492 | */ | |
493 | ||
494 | cupsArrayRemove(Subscriptions, sub); | |
495 | ||
496 | /* | |
497 | * Free memory... | |
498 | */ | |
499 | ||
500 | cupsdClearString(&(sub->owner)); | |
501 | cupsdClearString(&(sub->recipient)); | |
502 | ||
10d09e33 | 503 | cupsArrayDelete(sub->events); |
ef416fc2 | 504 | |
505 | free(sub); | |
506 | ||
507 | /* | |
508 | * Update the subscriptions as needed... | |
509 | */ | |
510 | ||
511 | if (update) | |
3dfe78b3 | 512 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
ef416fc2 | 513 | } |
514 | ||
515 | ||
516 | /* | |
517 | * 'cupsdEventName()' - Return a single event name. | |
518 | */ | |
519 | ||
520 | const char * /* O - Event name */ | |
521 | cupsdEventName( | |
522 | cupsd_eventmask_t event) /* I - Event value */ | |
523 | { | |
524 | switch (event) | |
525 | { | |
526 | default : | |
527 | return (NULL); | |
528 | ||
529 | case CUPSD_EVENT_PRINTER_RESTARTED : | |
530 | return ("printer-restarted"); | |
531 | ||
532 | case CUPSD_EVENT_PRINTER_SHUTDOWN : | |
533 | return ("printer-shutdown"); | |
534 | ||
535 | case CUPSD_EVENT_PRINTER_STOPPED : | |
536 | return ("printer-stopped"); | |
537 | ||
538 | case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED : | |
539 | return ("printer-finishings-changed"); | |
540 | ||
541 | case CUPSD_EVENT_PRINTER_MEDIA_CHANGED : | |
542 | return ("printer-media-changed"); | |
543 | ||
544 | case CUPSD_EVENT_PRINTER_ADDED : | |
545 | return ("printer-added"); | |
546 | ||
547 | case CUPSD_EVENT_PRINTER_DELETED : | |
548 | return ("printer-deleted"); | |
549 | ||
550 | case CUPSD_EVENT_PRINTER_MODIFIED : | |
551 | return ("printer-modified"); | |
552 | ||
d9bca400 MS |
553 | case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED : |
554 | return ("printer-queue-order-changed"); | |
555 | ||
556 | case CUPSD_EVENT_PRINTER_STATE : | |
ef416fc2 | 557 | case CUPSD_EVENT_PRINTER_STATE_CHANGED : |
558 | return ("printer-state-changed"); | |
559 | ||
d9bca400 | 560 | case CUPSD_EVENT_PRINTER_CONFIG : |
ef416fc2 | 561 | case CUPSD_EVENT_PRINTER_CONFIG_CHANGED : |
562 | return ("printer-config-changed"); | |
563 | ||
564 | case CUPSD_EVENT_PRINTER_CHANGED : | |
565 | return ("printer-changed"); | |
566 | ||
567 | case CUPSD_EVENT_JOB_CREATED : | |
568 | return ("job-created"); | |
569 | ||
570 | case CUPSD_EVENT_JOB_COMPLETED : | |
571 | return ("job-completed"); | |
572 | ||
573 | case CUPSD_EVENT_JOB_STOPPED : | |
574 | return ("job-stopped"); | |
575 | ||
576 | case CUPSD_EVENT_JOB_CONFIG_CHANGED : | |
577 | return ("job-config-changed"); | |
578 | ||
579 | case CUPSD_EVENT_JOB_PROGRESS : | |
580 | return ("job-progress"); | |
581 | ||
e1d6a774 | 582 | case CUPSD_EVENT_JOB_STATE : |
ef416fc2 | 583 | case CUPSD_EVENT_JOB_STATE_CHANGED : |
584 | return ("job-state-changed"); | |
585 | ||
586 | case CUPSD_EVENT_SERVER_RESTARTED : | |
587 | return ("server-restarted"); | |
588 | ||
589 | case CUPSD_EVENT_SERVER_STARTED : | |
590 | return ("server-started"); | |
591 | ||
592 | case CUPSD_EVENT_SERVER_STOPPED : | |
593 | return ("server-stopped"); | |
594 | ||
595 | case CUPSD_EVENT_SERVER_AUDIT : | |
596 | return ("server-audit"); | |
597 | ||
598 | case CUPSD_EVENT_ALL : | |
599 | return ("all"); | |
600 | } | |
601 | } | |
602 | ||
603 | ||
604 | /* | |
605 | * 'cupsdEventValue()' - Return the event mask value for a name. | |
606 | */ | |
607 | ||
608 | cupsd_eventmask_t /* O - Event mask value */ | |
609 | cupsdEventValue(const char *name) /* I - Name of event */ | |
610 | { | |
611 | if (!strcmp(name, "all")) | |
612 | return (CUPSD_EVENT_ALL); | |
613 | else if (!strcmp(name, "printer-restarted")) | |
614 | return (CUPSD_EVENT_PRINTER_RESTARTED); | |
615 | else if (!strcmp(name, "printer-shutdown")) | |
616 | return (CUPSD_EVENT_PRINTER_SHUTDOWN); | |
617 | else if (!strcmp(name, "printer-stopped")) | |
618 | return (CUPSD_EVENT_PRINTER_STOPPED); | |
619 | else if (!strcmp(name, "printer-finishings-changed")) | |
620 | return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED); | |
621 | else if (!strcmp(name, "printer-media-changed")) | |
622 | return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED); | |
623 | else if (!strcmp(name, "printer-added")) | |
624 | return (CUPSD_EVENT_PRINTER_ADDED); | |
625 | else if (!strcmp(name, "printer-deleted")) | |
626 | return (CUPSD_EVENT_PRINTER_DELETED); | |
627 | else if (!strcmp(name, "printer-modified")) | |
628 | return (CUPSD_EVENT_PRINTER_MODIFIED); | |
d9bca400 MS |
629 | else if (!strcmp(name, "printer-queue-order-changed")) |
630 | return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED); | |
ef416fc2 | 631 | else if (!strcmp(name, "printer-state-changed")) |
632 | return (CUPSD_EVENT_PRINTER_STATE_CHANGED); | |
633 | else if (!strcmp(name, "printer-config-changed")) | |
634 | return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED); | |
635 | else if (!strcmp(name, "printer-changed")) | |
636 | return (CUPSD_EVENT_PRINTER_CHANGED); | |
637 | else if (!strcmp(name, "job-created")) | |
638 | return (CUPSD_EVENT_JOB_CREATED); | |
639 | else if (!strcmp(name, "job-completed")) | |
640 | return (CUPSD_EVENT_JOB_COMPLETED); | |
641 | else if (!strcmp(name, "job-stopped")) | |
642 | return (CUPSD_EVENT_JOB_STOPPED); | |
643 | else if (!strcmp(name, "job-config-changed")) | |
644 | return (CUPSD_EVENT_JOB_CONFIG_CHANGED); | |
645 | else if (!strcmp(name, "job-progress")) | |
646 | return (CUPSD_EVENT_JOB_PROGRESS); | |
647 | else if (!strcmp(name, "job-state-changed")) | |
648 | return (CUPSD_EVENT_JOB_STATE_CHANGED); | |
649 | else if (!strcmp(name, "server-restarted")) | |
650 | return (CUPSD_EVENT_SERVER_RESTARTED); | |
651 | else if (!strcmp(name, "server-started")) | |
652 | return (CUPSD_EVENT_SERVER_STARTED); | |
653 | else if (!strcmp(name, "server-stopped")) | |
654 | return (CUPSD_EVENT_SERVER_STOPPED); | |
655 | else if (!strcmp(name, "server-audit")) | |
656 | return (CUPSD_EVENT_SERVER_AUDIT); | |
657 | else | |
658 | return (CUPSD_EVENT_NONE); | |
659 | } | |
660 | ||
661 | ||
662 | /* | |
663 | * 'cupsdExpireSubscriptions()' - Expire old subscription objects. | |
664 | */ | |
665 | ||
666 | void | |
667 | cupsdExpireSubscriptions( | |
668 | cupsd_printer_t *dest, /* I - Printer, if any */ | |
669 | cupsd_job_t *job) /* I - Job, if any */ | |
670 | { | |
671 | cupsd_subscription_t *sub; /* Current subscription */ | |
672 | int update; /* Update subscriptions.conf? */ | |
673 | time_t curtime; /* Current time */ | |
674 | ||
675 | ||
676 | curtime = time(NULL); | |
677 | update = 0; | |
678 | ||
679 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
680 | sub; | |
681 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
bd7854cb | 682 | if ((!sub->job && !dest && sub->expire && sub->expire <= curtime) || |
683 | (dest && sub->dest == dest) || | |
ef416fc2 | 684 | (job && sub->job == job)) |
685 | { | |
bd7854cb | 686 | cupsdLogMessage(CUPSD_LOG_INFO, "Subscription %d has expired...", |
687 | sub->id); | |
ef416fc2 | 688 | |
689 | cupsdDeleteSubscription(sub, 0); | |
690 | ||
691 | update = 1; | |
692 | } | |
693 | ||
694 | if (update) | |
3dfe78b3 | 695 | cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS); |
ef416fc2 | 696 | } |
697 | ||
698 | ||
699 | /* | |
700 | * 'cupsdFindSubscription()' - Find a subscription by ID. | |
701 | */ | |
702 | ||
703 | cupsd_subscription_t * /* O - Subscription object */ | |
704 | cupsdFindSubscription(int id) /* I - Subscription ID */ | |
705 | { | |
706 | cupsd_subscription_t sub; /* Subscription template */ | |
707 | ||
708 | ||
709 | sub.id = id; | |
710 | ||
711 | return ((cupsd_subscription_t *)cupsArrayFind(Subscriptions, &sub)); | |
712 | } | |
713 | ||
714 | ||
715 | /* | |
716 | * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file. | |
717 | */ | |
718 | ||
719 | void | |
720 | cupsdLoadAllSubscriptions(void) | |
721 | { | |
722 | int i; /* Looping var */ | |
723 | cups_file_t *fp; /* subscriptions.conf file */ | |
724 | int linenum; /* Current line number */ | |
725 | char line[1024], /* Line from file */ | |
726 | *value, /* Pointer to value */ | |
727 | *valueptr; /* Pointer into value */ | |
728 | cupsd_subscription_t *sub; /* Current subscription */ | |
729 | int hex; /* Non-zero if reading hex data */ | |
730 | int delete_sub; /* Delete subscription? */ | |
731 | ||
732 | ||
733 | /* | |
734 | * Open the subscriptions.conf file... | |
735 | */ | |
736 | ||
737 | snprintf(line, sizeof(line), "%s/subscriptions.conf", ServerRoot); | |
321d8d57 | 738 | if ((fp = cupsdOpenConfFile(line)) == NULL) |
ef416fc2 | 739 | return; |
ef416fc2 | 740 | |
741 | /* | |
742 | * Read all of the lines from the file... | |
743 | */ | |
744 | ||
745 | linenum = 0; | |
746 | sub = NULL; | |
747 | delete_sub = 0; | |
748 | ||
749 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
750 | { | |
88f9aafc | 751 | if (!_cups_strcasecmp(line, "NextSubscriptionId") && value) |
480ef0fe | 752 | { |
753 | /* | |
754 | * NextSubscriptionId NNN | |
755 | */ | |
756 | ||
757 | i = atoi(value); | |
758 | if (i >= NextSubscriptionId && i > 0) | |
759 | NextSubscriptionId = i; | |
760 | } | |
88f9aafc | 761 | else if (!_cups_strcasecmp(line, "<Subscription")) |
ef416fc2 | 762 | { |
763 | /* | |
764 | * <Subscription #> | |
765 | */ | |
766 | ||
767 | if (!sub && value && isdigit(value[0] & 255)) | |
768 | { | |
769 | sub = cupsdAddSubscription(CUPSD_EVENT_NONE, NULL, NULL, NULL, | |
770 | atoi(value)); | |
771 | } | |
772 | else | |
773 | { | |
774 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
775 | "Syntax error on line %d of subscriptions.conf.", | |
776 | linenum); | |
91c84a35 | 777 | break; |
ef416fc2 | 778 | } |
779 | } | |
88f9aafc | 780 | else if (!_cups_strcasecmp(line, "</Subscription>")) |
ef416fc2 | 781 | { |
782 | if (!sub) | |
783 | { | |
784 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
785 | "Syntax error on line %d of subscriptions.conf.", | |
786 | linenum); | |
91c84a35 | 787 | break; |
ef416fc2 | 788 | } |
789 | ||
790 | if (delete_sub) | |
791 | cupsdDeleteSubscription(sub, 0); | |
792 | ||
793 | sub = NULL; | |
794 | delete_sub = 0; | |
795 | } | |
796 | else if (!sub) | |
797 | { | |
798 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
799 | "Syntax error on line %d of subscriptions.conf.", | |
800 | linenum); | |
ef416fc2 | 801 | } |
88f9aafc | 802 | else if (!_cups_strcasecmp(line, "Events")) |
ef416fc2 | 803 | { |
804 | /* | |
805 | * Events name | |
806 | * Events name name name ... | |
807 | */ | |
808 | ||
809 | if (!value) | |
810 | { | |
811 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
812 | "Syntax error on line %d of subscriptions.conf.", | |
813 | linenum); | |
91c84a35 | 814 | break; |
ef416fc2 | 815 | } |
816 | ||
817 | while (*value) | |
818 | { | |
819 | /* | |
820 | * Separate event names... | |
821 | */ | |
822 | ||
823 | for (valueptr = value; !isspace(*valueptr) && *valueptr; valueptr ++); | |
824 | ||
825 | while (isspace(*valueptr & 255)) | |
826 | *valueptr++ = '\0'; | |
827 | ||
828 | /* | |
829 | * See if the name exists... | |
830 | */ | |
831 | ||
832 | if ((sub->mask |= cupsdEventValue(value)) == CUPSD_EVENT_NONE) | |
833 | { | |
834 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
835 | "Unknown event name \'%s\' on line %d of subscriptions.conf.", | |
836 | value, linenum); | |
91c84a35 | 837 | break; |
ef416fc2 | 838 | } |
839 | ||
840 | value = valueptr; | |
841 | } | |
842 | } | |
88f9aafc | 843 | else if (!_cups_strcasecmp(line, "Owner")) |
ef416fc2 | 844 | { |
845 | /* | |
846 | * Owner | |
847 | */ | |
848 | ||
849 | if (value) | |
850 | cupsdSetString(&sub->owner, value); | |
851 | else | |
852 | { | |
853 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
854 | "Syntax error on line %d of subscriptions.conf.", | |
855 | linenum); | |
91c84a35 | 856 | break; |
ef416fc2 | 857 | } |
858 | } | |
88f9aafc | 859 | else if (!_cups_strcasecmp(line, "Recipient")) |
ef416fc2 | 860 | { |
861 | /* | |
862 | * Recipient uri | |
863 | */ | |
864 | ||
865 | if (value) | |
866 | cupsdSetString(&sub->recipient, value); | |
867 | else | |
868 | { | |
869 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
870 | "Syntax error on line %d of subscriptions.conf.", | |
871 | linenum); | |
91c84a35 | 872 | break; |
ef416fc2 | 873 | } |
874 | } | |
88f9aafc | 875 | else if (!_cups_strcasecmp(line, "JobId")) |
ef416fc2 | 876 | { |
877 | /* | |
878 | * JobId # | |
879 | */ | |
880 | ||
881 | if (value && isdigit(*value & 255)) | |
882 | { | |
883 | if ((sub->job = cupsdFindJob(atoi(value))) == NULL) | |
884 | { | |
885 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
886 | "Job %s not found on line %d of subscriptions.conf.", | |
887 | value, linenum); | |
888 | delete_sub = 1; | |
889 | } | |
890 | } | |
891 | else | |
892 | { | |
893 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
894 | "Syntax error on line %d of subscriptions.conf.", | |
895 | linenum); | |
91c84a35 | 896 | break; |
ef416fc2 | 897 | } |
898 | } | |
88f9aafc | 899 | else if (!_cups_strcasecmp(line, "PrinterName")) |
ef416fc2 | 900 | { |
901 | /* | |
902 | * PrinterName name | |
903 | */ | |
904 | ||
905 | if (value) | |
906 | { | |
907 | if ((sub->dest = cupsdFindDest(value)) == NULL) | |
908 | { | |
909 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
910 | "Printer \'%s\' not found on line %d of subscriptions.conf.", | |
911 | value, linenum); | |
912 | delete_sub = 1; | |
913 | } | |
914 | } | |
915 | else | |
916 | { | |
917 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
918 | "Syntax error on line %d of subscriptions.conf.", | |
919 | linenum); | |
91c84a35 | 920 | break; |
ef416fc2 | 921 | } |
922 | } | |
88f9aafc | 923 | else if (!_cups_strcasecmp(line, "UserData")) |
ef416fc2 | 924 | { |
925 | /* | |
926 | * UserData encoded-string | |
927 | */ | |
928 | ||
929 | if (value) | |
930 | { | |
931 | for (i = 0, valueptr = value, hex = 0; i < 63 && *valueptr; i ++) | |
932 | { | |
933 | if (*valueptr == '<' && !hex) | |
934 | { | |
935 | hex = 1; | |
936 | valueptr ++; | |
937 | } | |
938 | ||
939 | if (hex) | |
940 | { | |
941 | if (isxdigit(valueptr[0]) && isxdigit(valueptr[1])) | |
942 | { | |
943 | if (isdigit(valueptr[0])) | |
944 | sub->user_data[i] = (valueptr[0] - '0') << 4; | |
945 | else | |
946 | sub->user_data[i] = (tolower(valueptr[0]) - 'a' + 10) << 4; | |
947 | ||
948 | if (isdigit(valueptr[1])) | |
949 | sub->user_data[i] |= valueptr[1] - '0'; | |
950 | else | |
951 | sub->user_data[i] |= tolower(valueptr[1]) - 'a' + 10; | |
952 | ||
953 | valueptr += 2; | |
954 | ||
955 | if (*valueptr == '>') | |
956 | { | |
957 | hex = 0; | |
958 | valueptr ++; | |
959 | } | |
960 | } | |
961 | else | |
962 | break; | |
963 | } | |
964 | else | |
965 | sub->user_data[i] = *valueptr++; | |
966 | } | |
967 | ||
968 | if (*valueptr) | |
969 | { | |
970 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
971 | "Bad UserData \'%s\' on line %d of subscriptions.conf.", | |
972 | value, linenum); | |
973 | } | |
974 | else | |
975 | sub->user_data_len = i; | |
976 | } | |
977 | else | |
978 | { | |
979 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
980 | "Syntax error on line %d of subscriptions.conf.", | |
981 | linenum); | |
91c84a35 | 982 | break; |
ef416fc2 | 983 | } |
984 | } | |
88f9aafc | 985 | else if (!_cups_strcasecmp(line, "LeaseDuration")) |
ef416fc2 | 986 | { |
987 | /* | |
988 | * LeaseDuration # | |
989 | */ | |
990 | ||
991 | if (value && isdigit(*value & 255)) | |
bd7854cb | 992 | { |
993 | sub->lease = atoi(value); | |
994 | sub->expire = sub->lease ? time(NULL) + sub->lease : 0; | |
995 | } | |
ef416fc2 | 996 | else |
997 | { | |
998 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
999 | "Syntax error on line %d of subscriptions.conf.", | |
1000 | linenum); | |
91c84a35 | 1001 | break; |
ef416fc2 | 1002 | } |
1003 | } | |
88f9aafc | 1004 | else if (!_cups_strcasecmp(line, "Interval")) |
ef416fc2 | 1005 | { |
1006 | /* | |
1007 | * Interval # | |
1008 | */ | |
1009 | ||
1010 | if (value && isdigit(*value & 255)) | |
1011 | sub->interval = atoi(value); | |
1012 | else | |
1013 | { | |
1014 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1015 | "Syntax error on line %d of subscriptions.conf.", | |
1016 | linenum); | |
91c84a35 | 1017 | break; |
ef416fc2 | 1018 | } |
1019 | } | |
88f9aafc | 1020 | else if (!_cups_strcasecmp(line, "ExpirationTime")) |
ef416fc2 | 1021 | { |
1022 | /* | |
1023 | * ExpirationTime # | |
1024 | */ | |
1025 | ||
1026 | if (value && isdigit(*value & 255)) | |
1027 | sub->expire = atoi(value); | |
1028 | else | |
1029 | { | |
1030 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1031 | "Syntax error on line %d of subscriptions.conf.", | |
1032 | linenum); | |
91c84a35 | 1033 | break; |
ef416fc2 | 1034 | } |
1035 | } | |
88f9aafc | 1036 | else if (!_cups_strcasecmp(line, "NextEventId")) |
ef416fc2 | 1037 | { |
1038 | /* | |
1039 | * NextEventId # | |
1040 | */ | |
1041 | ||
1042 | if (value && isdigit(*value & 255)) | |
1043 | sub->next_event_id = sub->first_event_id = atoi(value); | |
1044 | else | |
1045 | { | |
1046 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1047 | "Syntax error on line %d of subscriptions.conf.", | |
1048 | linenum); | |
91c84a35 | 1049 | break; |
ef416fc2 | 1050 | } |
1051 | } | |
1052 | else | |
1053 | { | |
1054 | /* | |
1055 | * Something else we don't understand... | |
1056 | */ | |
1057 | ||
1058 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1059 | "Unknown configuration directive %s on line %d of subscriptions.conf.", | |
1060 | line, linenum); | |
1061 | } | |
1062 | } | |
1063 | ||
1064 | cupsFileClose(fp); | |
1065 | } | |
1066 | ||
1067 | ||
1068 | /* | |
1069 | * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file. | |
1070 | */ | |
1071 | ||
1072 | void | |
1073 | cupsdSaveAllSubscriptions(void) | |
1074 | { | |
1075 | int i; /* Looping var */ | |
1076 | cups_file_t *fp; /* subscriptions.conf file */ | |
321d8d57 MS |
1077 | char filename[1024], /* subscriptions.conf filename */ |
1078 | temp[1024]; /* Temporary string */ | |
ef416fc2 | 1079 | cupsd_subscription_t *sub; /* Current subscription */ |
1080 | time_t curtime; /* Current time */ | |
1081 | struct tm *curdate; /* Current date */ | |
1082 | unsigned mask; /* Current event mask */ | |
1083 | const char *name; /* Current event name */ | |
1084 | int hex; /* Non-zero if we are writing hex data */ | |
1085 | ||
1086 | ||
1087 | /* | |
1088 | * Create the subscriptions.conf file... | |
1089 | */ | |
1090 | ||
321d8d57 | 1091 | snprintf(filename, sizeof(filename), "%s/subscriptions.conf", ServerRoot); |
ef416fc2 | 1092 | |
321d8d57 | 1093 | if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm)) == NULL) |
ef416fc2 | 1094 | return; |
ef416fc2 | 1095 | |
321d8d57 | 1096 | cupsdLogMessage(CUPSD_LOG_INFO, "Saving subscriptions.conf..."); |
ef416fc2 | 1097 | |
1098 | /* | |
1099 | * Write a small header to the file... | |
1100 | */ | |
1101 | ||
1102 | curtime = time(NULL); | |
1103 | curdate = localtime(&curtime); | |
1104 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
1105 | ||
1106 | cupsFilePuts(fp, "# Subscription configuration file for " CUPS_SVERSION "\n"); | |
1107 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
1108 | ||
480ef0fe | 1109 | cupsFilePrintf(fp, "NextSubscriptionId %d\n", NextSubscriptionId); |
1110 | ||
ef416fc2 | 1111 | /* |
1112 | * Write every subscription known to the system... | |
1113 | */ | |
1114 | ||
1115 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
1116 | sub; | |
1117 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
1118 | { | |
1119 | cupsFilePrintf(fp, "<Subscription %d>\n", sub->id); | |
1120 | ||
1121 | if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL) | |
1122 | { | |
1123 | /* | |
1124 | * Simple event list... | |
1125 | */ | |
1126 | ||
1127 | cupsFilePrintf(fp, "Events %s\n", name); | |
1128 | } | |
1129 | else | |
1130 | { | |
1131 | /* | |
1132 | * Complex event list... | |
1133 | */ | |
1134 | ||
1135 | cupsFilePuts(fp, "Events"); | |
1136 | ||
1137 | for (mask = 1; mask < CUPSD_EVENT_ALL; mask <<= 1) | |
1138 | if (sub->mask & mask) | |
1139 | cupsFilePrintf(fp, " %s", cupsdEventName((cupsd_eventmask_t)mask)); | |
1140 | ||
1141 | cupsFilePuts(fp, "\n"); | |
1142 | } | |
1143 | ||
1144 | if (sub->owner) | |
1145 | cupsFilePrintf(fp, "Owner %s\n", sub->owner); | |
1146 | if (sub->recipient) | |
1147 | cupsFilePrintf(fp, "Recipient %s\n", sub->recipient); | |
1148 | if (sub->job) | |
1149 | cupsFilePrintf(fp, "JobId %d\n", sub->job->id); | |
1150 | if (sub->dest) | |
1151 | cupsFilePrintf(fp, "PrinterName %s\n", sub->dest->name); | |
1152 | ||
1153 | if (sub->user_data_len > 0) | |
1154 | { | |
1155 | cupsFilePuts(fp, "UserData "); | |
1156 | ||
1157 | for (i = 0, hex = 0; i < sub->user_data_len; i ++) | |
1158 | { | |
1159 | if (sub->user_data[i] < ' ' || | |
1160 | sub->user_data[i] > 0x7f || | |
1161 | sub->user_data[i] == '<') | |
1162 | { | |
1163 | if (!hex) | |
1164 | { | |
1165 | cupsFilePrintf(fp, "<%02X", sub->user_data[i]); | |
1166 | hex = 1; | |
1167 | } | |
1168 | else | |
1169 | cupsFilePrintf(fp, "%02X", sub->user_data[i]); | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | if (hex) | |
1174 | { | |
1175 | cupsFilePrintf(fp, ">%c", sub->user_data[i]); | |
1176 | hex = 0; | |
1177 | } | |
1178 | else | |
1179 | cupsFilePutChar(fp, sub->user_data[i]); | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | if (hex) | |
1184 | cupsFilePuts(fp, ">\n"); | |
1185 | else | |
1186 | cupsFilePutChar(fp, '\n'); | |
1187 | } | |
1188 | ||
1189 | cupsFilePrintf(fp, "LeaseDuration %d\n", sub->lease); | |
1190 | cupsFilePrintf(fp, "Interval %d\n", sub->interval); | |
1191 | cupsFilePrintf(fp, "ExpirationTime %ld\n", (long)sub->expire); | |
1192 | cupsFilePrintf(fp, "NextEventId %d\n", sub->next_event_id); | |
1193 | ||
1194 | cupsFilePuts(fp, "</Subscription>\n"); | |
1195 | } | |
1196 | ||
321d8d57 | 1197 | cupsdCloseCreatedConfFile(fp, filename); |
ef416fc2 | 1198 | } |
1199 | ||
1200 | ||
ef416fc2 | 1201 | /* |
1202 | * 'cupsdStopAllNotifiers()' - Stop all notifier processes. | |
1203 | */ | |
1204 | ||
1205 | void | |
1206 | cupsdStopAllNotifiers(void) | |
1207 | { | |
1208 | cupsd_subscription_t *sub; /* Current subscription */ | |
1209 | ||
1210 | ||
1211 | /* | |
1212 | * See if we have started any notifiers... | |
1213 | */ | |
1214 | ||
1215 | if (!NotifierStatusBuffer) | |
1216 | return; | |
1217 | ||
1218 | /* | |
fa73b229 | 1219 | * Yes, kill any processes that are left... |
ef416fc2 | 1220 | */ |
1221 | ||
1222 | for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions); | |
1223 | sub; | |
1224 | sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions)) | |
1225 | if (sub->pid) | |
1226 | { | |
1227 | cupsdEndProcess(sub->pid, 0); | |
1228 | ||
1229 | close(sub->pipe); | |
1230 | sub->pipe = -1; | |
1231 | } | |
1232 | ||
1233 | /* | |
1234 | * Close the status pipes... | |
1235 | */ | |
1236 | ||
1237 | if (NotifierPipes[0] >= 0) | |
1238 | { | |
f7deaa1a | 1239 | cupsdRemoveSelect(NotifierPipes[0]); |
ef416fc2 | 1240 | |
1241 | cupsdStatBufDelete(NotifierStatusBuffer); | |
1242 | ||
1243 | close(NotifierPipes[0]); | |
1244 | close(NotifierPipes[1]); | |
1245 | ||
1246 | NotifierPipes[0] = -1; | |
1247 | NotifierPipes[1] = -1; | |
1248 | NotifierStatusBuffer = NULL; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | ||
ef416fc2 | 1253 | /* |
1254 | * 'cupsd_compare_subscriptions()' - Compare two subscriptions. | |
1255 | */ | |
1256 | ||
1257 | static int /* O - Result of comparison */ | |
1258 | cupsd_compare_subscriptions( | |
1259 | cupsd_subscription_t *first, /* I - First subscription object */ | |
1260 | cupsd_subscription_t *second, /* I - Second subscription object */ | |
1261 | void *unused) /* I - Unused user data pointer */ | |
1262 | { | |
1263 | (void)unused; | |
1264 | ||
1265 | return (first->id - second->id); | |
1266 | } | |
1267 | ||
1268 | ||
1269 | /* | |
1270 | * 'cupsd_delete_event()' - Delete a single event... | |
1271 | * | |
1272 | * Oldest events must be deleted first, otherwise the subscription cache | |
1273 | * flushing code will not work properly. | |
1274 | */ | |
1275 | ||
1276 | static void | |
1277 | cupsd_delete_event(cupsd_event_t *event)/* I - Event to delete */ | |
1278 | { | |
ef416fc2 | 1279 | /* |
1280 | * Free memory... | |
1281 | */ | |
1282 | ||
1283 | ippDelete(event->attrs); | |
1284 | free(event); | |
1285 | } | |
1286 | ||
1287 | ||
e00b005a | 1288 | #ifdef HAVE_DBUS |
1289 | /* | |
1290 | * 'cupsd_send_dbus()' - Send a DBUS notification... | |
1291 | */ | |
1292 | ||
1293 | static void | |
1294 | cupsd_send_dbus(cupsd_eventmask_t event,/* I - Event to send */ | |
1295 | cupsd_printer_t *dest,/* I - Destination, if any */ | |
1296 | cupsd_job_t *job) /* I - Job, if any */ | |
1297 | { | |
1298 | DBusError error; /* Error, if any */ | |
1299 | DBusMessage *message; /* Message to send */ | |
1300 | DBusMessageIter iter; /* Iterator for message data */ | |
1301 | const char *what; /* What to send */ | |
1302 | static DBusConnection *con = NULL; /* Connection to DBUS server */ | |
1303 | ||
1304 | ||
1305 | /* | |
1306 | * Figure out what to send, if anything... | |
1307 | */ | |
1308 | ||
1309 | if (event & CUPSD_EVENT_PRINTER_ADDED) | |
1310 | what = "PrinterAdded"; | |
1311 | else if (event & CUPSD_EVENT_PRINTER_DELETED) | |
1312 | what = "PrinterRemoved"; | |
1313 | else if (event & CUPSD_EVENT_PRINTER_CHANGED) | |
1314 | what = "QueueChanged"; | |
1315 | else if (event & CUPSD_EVENT_JOB_CREATED) | |
1316 | what = "JobQueuedLocal"; | |
1317 | else if ((event & CUPSD_EVENT_JOB_STATE) && job && | |
bd7854cb | 1318 | job->state_value == IPP_JOB_PROCESSING) |
e00b005a | 1319 | what = "JobStartedLocal"; |
1320 | else | |
1321 | return; | |
1322 | ||
1323 | /* | |
1324 | * Verify connection to DBUS server... | |
1325 | */ | |
1326 | ||
1327 | if (con && !dbus_connection_get_is_connected(con)) | |
1328 | { | |
1329 | dbus_connection_unref(con); | |
1330 | con = NULL; | |
1331 | } | |
1332 | ||
1333 | if (!con) | |
1334 | { | |
1335 | dbus_error_init(&error); | |
1336 | ||
1337 | con = dbus_bus_get(getuid() ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error); | |
1338 | if (!con) | |
1339 | { | |
1340 | dbus_error_free(&error); | |
1341 | return; | |
1342 | } | |
1343 | } | |
1344 | ||
1345 | /* | |
1346 | * Create and send the new message... | |
1347 | */ | |
1348 | ||
1349 | message = dbus_message_new_signal("/com/redhat/PrinterSpooler", | |
1350 | "com.redhat.PrinterSpooler", what); | |
1351 | ||
2abf387c | 1352 | dbus_message_append_iter_init(message, &iter); |
e00b005a | 1353 | if (dest) |
f7deaa1a | 1354 | dbus_message_iter_append_string(&iter, dest->name); |
e00b005a | 1355 | if (job) |
1356 | { | |
f7deaa1a | 1357 | dbus_message_iter_append_uint32(&iter, job->id); |
1358 | dbus_message_iter_append_string(&iter, job->username); | |
e00b005a | 1359 | } |
1360 | ||
1361 | dbus_connection_send(con, message, NULL); | |
1362 | dbus_connection_flush(con); | |
1363 | dbus_message_unref(message); | |
1364 | } | |
1365 | #endif /* HAVE_DBUS */ | |
1366 | ||
1367 | ||
e1d6a774 | 1368 | /* |
1369 | * 'cupsd_send_notification()' - Send a notification for the specified event. | |
1370 | */ | |
1371 | ||
1372 | static void | |
1373 | cupsd_send_notification( | |
1374 | cupsd_subscription_t *sub, /* I - Subscription object */ | |
1375 | cupsd_event_t *event) /* I - Event to send */ | |
1376 | { | |
1377 | ipp_state_t state; /* IPP event state */ | |
1378 | ||
1379 | ||
8ca02f3c | 1380 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
75bd9771 | 1381 | "cupsd_send_notification(sub=%p(%d), event=%p(%s))", |
e1d6a774 | 1382 | sub, sub->id, event, cupsdEventName(event->event)); |
1383 | ||
1384 | /* | |
1385 | * Allocate the events array as needed... | |
1386 | */ | |
1387 | ||
1388 | if (!sub->events) | |
1389 | { | |
10d09e33 MS |
1390 | sub->events = cupsArrayNew3((cups_array_func_t)NULL, NULL, |
1391 | (cups_ahash_func_t)NULL, 0, | |
1392 | (cups_acopy_func_t)NULL, | |
1393 | (cups_afree_func_t)cupsd_delete_event); | |
e1d6a774 | 1394 | |
1395 | if (!sub->events) | |
1396 | { | |
1397 | cupsdLogMessage(CUPSD_LOG_CRIT, | |
1398 | "Unable to allocate memory for subscription #%d!", | |
1399 | sub->id); | |
1400 | return; | |
1401 | } | |
1402 | } | |
1403 | ||
ed486911 | 1404 | /* |
1405 | * Purge an old event as needed... | |
1406 | */ | |
1407 | ||
10d09e33 | 1408 | if (cupsArrayCount(sub->events) >= MaxEvents) |
ed486911 | 1409 | { |
1410 | /* | |
1411 | * Purge the oldest event in the cache... | |
1412 | */ | |
1413 | ||
10d09e33 | 1414 | cupsArrayRemove(sub->events, cupsArrayFirst(sub->events)); |
ed486911 | 1415 | |
ed486911 | 1416 | sub->first_event_id ++; |
ed486911 | 1417 | } |
1418 | ||
e1d6a774 | 1419 | /* |
1420 | * Add the event to the subscription. Since the events array is | |
1421 | * always MaxEvents in length, and since we will have already | |
1422 | * removed an event from the subscription cache if we hit the | |
1423 | * event cache limit, we don't need to check for overflow here... | |
1424 | */ | |
1425 | ||
10d09e33 | 1426 | cupsArrayAdd(sub->events, event); |
e1d6a774 | 1427 | |
1428 | /* | |
1429 | * Deliver the event... | |
1430 | */ | |
1431 | ||
1432 | if (sub->recipient) | |
1433 | { | |
8ca02f3c | 1434 | for (;;) |
1435 | { | |
1436 | if (sub->pipe < 0) | |
1437 | cupsd_start_notifier(sub); | |
e1d6a774 | 1438 | |
8ca02f3c | 1439 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "sub->pipe=%d", sub->pipe); |
1440 | ||
1441 | if (sub->pipe < 0) | |
1442 | break; | |
e1d6a774 | 1443 | |
e1d6a774 | 1444 | event->attrs->state = IPP_IDLE; |
1445 | ||
1446 | while ((state = ippWriteFile(sub->pipe, event->attrs)) != IPP_DATA) | |
1447 | if (state == IPP_ERROR) | |
1448 | break; | |
1449 | ||
1450 | if (state == IPP_ERROR) | |
8ca02f3c | 1451 | { |
1452 | if (errno == EPIPE) | |
1453 | { | |
1454 | /* | |
1455 | * Notifier died, try restarting it... | |
1456 | */ | |
1457 | ||
1458 | cupsdLogMessage(CUPSD_LOG_WARN, | |
1459 | "Notifier for subscription %d (%s) went away, " | |
1460 | "retrying!", | |
1461 | sub->id, sub->recipient); | |
1462 | cupsdEndProcess(sub->pid, 0); | |
1463 | ||
1464 | close(sub->pipe); | |
1465 | sub->pipe = -1; | |
1466 | continue; | |
1467 | } | |
1468 | ||
e1d6a774 | 1469 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1470 | "Unable to send event for subscription %d (%s)!", | |
1471 | sub->id, sub->recipient); | |
8ca02f3c | 1472 | } |
1473 | ||
1474 | /* | |
1475 | * If we get this far, break out of the loop... | |
1476 | */ | |
1477 | ||
1478 | break; | |
e1d6a774 | 1479 | } |
1480 | } | |
1481 | ||
1482 | /* | |
1483 | * Bump the event sequence number... | |
1484 | */ | |
1485 | ||
1486 | sub->next_event_id ++; | |
1487 | } | |
1488 | ||
1489 | ||
ef416fc2 | 1490 | /* |
1491 | * 'cupsd_start_notifier()' - Start a notifier subprocess... | |
1492 | */ | |
1493 | ||
1494 | static void | |
1495 | cupsd_start_notifier( | |
1496 | cupsd_subscription_t *sub) /* I - Subscription object */ | |
1497 | { | |
1498 | int pid; /* Notifier process ID */ | |
1499 | int fds[2]; /* Pipe file descriptors */ | |
ef416fc2 | 1500 | char *argv[4], /* Command-line arguments */ |
e00b005a | 1501 | *envp[MAX_ENV], /* Environment variables */ |
ef416fc2 | 1502 | user_data[128], /* Base-64 encoded user data */ |
1503 | scheme[256], /* notify-recipient-uri scheme */ | |
1504 | *ptr, /* Pointer into scheme */ | |
1505 | command[1024]; /* Notifier command */ | |
1506 | ||
1507 | ||
1508 | /* | |
1509 | * Extract the scheme name from the recipient URI and point to the | |
1510 | * notifier program... | |
1511 | */ | |
1512 | ||
1513 | strlcpy(scheme, sub->recipient, sizeof(scheme)); | |
1514 | if ((ptr = strchr(scheme, ':')) != NULL) | |
1515 | *ptr = '\0'; | |
1516 | ||
1517 | snprintf(command, sizeof(command), "%s/notifier/%s", ServerBin, scheme); | |
1518 | ||
1519 | /* | |
1520 | * Base-64 encode the user data... | |
1521 | */ | |
1522 | ||
1523 | httpEncode64_2(user_data, sizeof(user_data), (char *)sub->user_data, | |
1524 | sub->user_data_len); | |
1525 | ||
1526 | /* | |
1527 | * Setup the argument array... | |
1528 | */ | |
1529 | ||
1530 | argv[0] = command; | |
1531 | argv[1] = sub->recipient; | |
1532 | argv[2] = user_data; | |
1533 | argv[3] = NULL; | |
1534 | ||
1535 | /* | |
1536 | * Setup the environment... | |
1537 | */ | |
1538 | ||
d09495fa | 1539 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
ef416fc2 | 1540 | |
1541 | /* | |
1542 | * Create pipes as needed... | |
1543 | */ | |
1544 | ||
1545 | if (!NotifierStatusBuffer) | |
1546 | { | |
1547 | /* | |
1548 | * Create the status pipe... | |
1549 | */ | |
1550 | ||
1551 | if (cupsdOpenPipe(NotifierPipes)) | |
1552 | { | |
1553 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1554 | "Unable to create pipes for notifier status - %s", | |
1555 | strerror(errno)); | |
1556 | return; | |
1557 | } | |
1558 | ||
1559 | NotifierStatusBuffer = cupsdStatBufNew(NotifierPipes[0], "[Notifier]"); | |
1560 | ||
f899b121 | 1561 | cupsdAddSelect(NotifierPipes[0], (cupsd_selfunc_t)cupsd_update_notifier, |
f7deaa1a | 1562 | NULL, NULL); |
ef416fc2 | 1563 | } |
1564 | ||
1565 | if (cupsdOpenPipe(fds)) | |
1566 | { | |
1567 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1568 | "Unable to create pipes for notifier %s - %s", | |
1569 | scheme, strerror(errno)); | |
1570 | return; | |
1571 | } | |
1572 | ||
1573 | /* | |
1574 | * Make sure the delivery pipe is non-blocking... | |
1575 | */ | |
1576 | ||
1577 | fcntl(fds[1], F_SETFL, fcntl(fds[1], F_GETFL) | O_NONBLOCK); | |
1578 | ||
1579 | /* | |
1580 | * Create the notifier process... | |
1581 | */ | |
1582 | ||
1583 | if (cupsdStartProcess(command, argv, envp, fds[0], -1, NotifierPipes[1], | |
38e73f87 | 1584 | -1, -1, 0, DefaultProfile, NULL, &pid) < 0) |
ef416fc2 | 1585 | { |
1586 | /* | |
1587 | * Error - can't fork! | |
1588 | */ | |
1589 | ||
1590 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to fork for notifier %s - %s", | |
1591 | scheme, strerror(errno)); | |
1592 | ||
1593 | cupsdClosePipe(fds); | |
1594 | } | |
1595 | else | |
1596 | { | |
1597 | /* | |
1598 | * Fork successful - return the PID... | |
1599 | */ | |
1600 | ||
1601 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Notifier %s started - PID = %d", | |
1602 | scheme, pid); | |
1603 | ||
1604 | sub->pid = pid; | |
1605 | sub->pipe = fds[1]; | |
1606 | sub->status = 0; | |
1607 | ||
1608 | close(fds[0]); | |
1609 | } | |
1610 | } | |
1611 | ||
1612 | ||
1613 | /* | |
f899b121 | 1614 | * 'cupsd_update_notifier()' - Read messages from notifiers. |
1615 | */ | |
1616 | ||
1617 | void | |
1618 | cupsd_update_notifier(void) | |
1619 | { | |
1620 | char message[1024]; /* Pointer to message text */ | |
1621 | int loglevel; /* Log level for message */ | |
1622 | ||
1623 | ||
1624 | while (cupsdStatBufUpdate(NotifierStatusBuffer, &loglevel, | |
1625 | message, sizeof(message))) | |
f0ab5bff MS |
1626 | { |
1627 | if (loglevel == CUPSD_LOG_INFO) | |
1628 | cupsdLogMessage(CUPSD_LOG_INFO, "%s", message); | |
1629 | ||
f899b121 | 1630 | if (!strchr(NotifierStatusBuffer->buffer, '\n')) |
1631 | break; | |
f0ab5bff | 1632 | } |
f899b121 | 1633 | } |
1634 | ||
1635 | ||
1636 | /* | |
b19ccc9e | 1637 | * End of "$Id: subscriptions.c 7824 2008-08-01 21:11:55Z mike $". |
ef416fc2 | 1638 | */ |