4 * Subscription routines for the CUPS scheduler.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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 * Include necessary headers...
22 # include <dbus/dbus.h>
23 # ifdef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND
24 # define dbus_message_append_iter_init dbus_message_iter_init_append
25 # define dbus_message_iter_append_string(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &(v))
26 # define dbus_message_iter_append_uint32(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &(v))
27 # endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */
28 #endif /* HAVE_DBUS */
35 static int cupsd_compare_subscriptions(cupsd_subscription_t
*first
,
36 cupsd_subscription_t
*second
,
38 static void cupsd_delete_event(cupsd_event_t
*event
);
40 static void cupsd_send_dbus(cupsd_eventmask_t event
, cupsd_printer_t
*dest
,
42 #endif /* HAVE_DBUS */
43 static void cupsd_send_notification(cupsd_subscription_t
*sub
,
44 cupsd_event_t
*event
);
45 static void cupsd_start_notifier(cupsd_subscription_t
*sub
);
46 static void cupsd_update_notifier(void);
50 * 'cupsdAddEvent()' - Add an event to the global event cache.
55 cupsd_eventmask_t event
, /* I - Event */
56 cupsd_printer_t
*dest
, /* I - Printer associated with event */
57 cupsd_job_t
*job
, /* I - Job associated with event */
58 const char *text
, /* I - Notification text */
59 ...) /* I - Additional arguments as needed */
61 va_list ap
; /* Pointer to additional arguments */
62 char ftext
[1024]; /* Formatted text buffer */
63 ipp_attribute_t
*attr
; /* Printer/job attribute */
64 cupsd_event_t
*temp
; /* New event pointer */
65 cupsd_subscription_t
*sub
; /* Current subscription */
68 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
69 "cupsdAddEvent(event=%s, dest=%p(%s), job=%p(%d), text=\"%s\", ...)",
70 cupsdEventName(event
), dest
, dest
? dest
->name
: "",
71 job
, job
? job
->id
: 0, text
);
74 * Keep track of events with any OS-supplied notification mechanisms...
80 cupsd_send_dbus(event
, dest
, job
);
81 #endif /* HAVE_DBUS */
84 * Return if we aren't keeping events...
89 cupsdLogMessage(CUPSD_LOG_WARN
,
90 "cupsdAddEvent: Discarding %s event since MaxEvents is %d!",
91 cupsdEventName(event
), MaxEvents
);
96 * Then loop through the subscriptions and add the event to the corresponding
100 for (temp
= NULL
, sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
102 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
105 * Check if this subscription requires this event...
108 if ((sub
->mask
& event
) != 0 && (sub
->dest
== dest
|| !sub
->dest
|| sub
->job
== job
))
111 * Need this event, so create a new event record...
114 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
116 cupsdLogMessage(CUPSD_LOG_CRIT
,
117 "Unable to allocate memory for event - %s",
123 temp
->time
= time(NULL
);
124 temp
->attrs
= ippNew();
130 temp
->dest
= dest
= cupsdFindPrinter(job
->dest
);
133 * Add common event notification attributes...
136 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_CHARSET
,
137 "notify-charset", NULL
, "utf-8");
139 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_LANGUAGE
,
140 "notify-natural-language", NULL
, "en-US");
142 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
143 "notify-subscription-id", sub
->id
);
145 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
146 "notify-sequence-number", sub
->next_event_id
);
148 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
149 "notify-subscribed-event", NULL
, cupsdEventName(event
));
151 if (sub
->user_data_len
> 0)
152 ippAddOctetString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
153 "notify-user-data", sub
->user_data
,
156 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
157 "printer-up-time", time(NULL
));
160 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
163 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
164 "notify-text", NULL
, ftext
);
169 * Add printer attributes...
172 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
173 "notify-printer-uri", NULL
, dest
->uri
);
175 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
176 "printer-name", NULL
, dest
->name
);
178 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
179 "printer-state", dest
->state
);
181 if (dest
->num_reasons
== 0)
182 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
183 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
184 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
186 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
187 IPP_TAG_KEYWORD
, "printer-state-reasons",
188 dest
->num_reasons
, NULL
,
189 (const char * const *)dest
->reasons
);
191 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
192 "printer-is-accepting-jobs", (char)dest
->accepting
);
198 * Add job attributes...
201 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
202 "notify-job-id", job
->id
);
203 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
204 "job-state", job
->state_value
);
206 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
207 IPP_TAG_NAME
)) != NULL
)
208 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
209 "job-name", NULL
, attr
->values
[0].string
.text
);
211 switch (job
->state_value
)
213 case IPP_JOB_PENDING
:
214 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
215 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
216 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
219 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
220 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
225 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
226 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
227 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
228 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
229 "job-hold-until-specified");
231 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
232 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
236 case IPP_JOB_PROCESSING
:
237 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
238 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
242 case IPP_JOB_STOPPED
:
243 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
244 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
248 case IPP_JOB_CANCELED
:
249 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
250 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
251 "job-canceled-by-user");
254 case IPP_JOB_ABORTED
:
255 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
256 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
257 "aborted-by-system");
260 case IPP_JOB_COMPLETED
:
261 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
262 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
263 "job-completed-successfully");
267 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
268 "job-impressions-completed",
269 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
273 * Send the notification for this subscription...
276 cupsd_send_notification(sub
, temp
);
281 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
283 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
284 cupsdEventName(event
));
289 * 'cupsdAddSubscription()' - Add a new subscription object.
292 cupsd_subscription_t
* /* O - New subscription object */
293 cupsdAddSubscription(
294 unsigned mask
, /* I - Event mask */
295 cupsd_printer_t
*dest
, /* I - Printer, if any */
296 cupsd_job_t
*job
, /* I - Job, if any */
297 const char *uri
, /* I - notify-recipient-uri, if any */
298 int sub_id
) /* I - notify-subscription-id or 0 */
300 cupsd_subscription_t
*temp
; /* New subscription object */
303 cupsdLogMessage(CUPSD_LOG_DEBUG
,
304 "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
306 mask
, dest
, dest
? dest
->name
: "", job
, job
? job
->id
: 0,
307 uri
? uri
: "(null)");
310 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
315 cupsdLogMessage(CUPSD_LOG_CRIT
,
316 "Unable to allocate memory for subscriptions - %s",
322 * Limit the number of subscriptions...
325 if (MaxSubscriptions
> 0 && cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
327 cupsdLogMessage(CUPSD_LOG_DEBUG
,
328 "cupsdAddSubscription: Reached MaxSubscriptions %d "
329 "(count=%d)", MaxSubscriptions
,
330 cupsArrayCount(Subscriptions
));
334 if (MaxSubscriptionsPerJob
> 0 && job
)
336 int count
; /* Number of job subscriptions */
338 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
341 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
342 if (temp
->job
== job
)
345 if (count
>= MaxSubscriptionsPerJob
)
347 cupsdLogMessage(CUPSD_LOG_DEBUG
,
348 "cupsdAddSubscription: Reached MaxSubscriptionsPerJob %d "
349 "for job #%d (count=%d)", MaxSubscriptionsPerJob
,
355 if (MaxSubscriptionsPerPrinter
> 0 && dest
)
357 int count
; /* Number of printer subscriptions */
359 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
362 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
363 if (temp
->dest
== dest
)
366 if (count
>= MaxSubscriptionsPerPrinter
)
368 cupsdLogMessage(CUPSD_LOG_DEBUG
,
369 "cupsdAddSubscription: Reached "
370 "MaxSubscriptionsPerPrinter %d for %s (count=%d)",
371 MaxSubscriptionsPerPrinter
, dest
->name
, count
);
377 * Allocate memory for this subscription...
380 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
382 cupsdLogMessage(CUPSD_LOG_CRIT
,
383 "Unable to allocate memory for subscription object - %s",
389 * Fill in common data...
396 if (sub_id
>= NextSubscriptionId
)
397 NextSubscriptionId
= sub_id
+ 1;
401 temp
->id
= NextSubscriptionId
;
403 NextSubscriptionId
++;
410 temp
->first_event_id
= 1;
411 temp
->next_event_id
= 1;
413 cupsdSetString(&(temp
->recipient
), uri
);
416 * Add the subscription to the array...
419 cupsArrayAdd(Subscriptions
, temp
);
422 * For RSS subscriptions, run the notifier immediately...
425 if (uri
&& !strncmp(uri
, "rss:", 4))
426 cupsd_start_notifier(temp
);
433 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
437 cupsdDeleteAllSubscriptions(void)
439 cupsd_subscription_t
*sub
; /* Subscription */
445 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
447 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
448 cupsdDeleteSubscription(sub
, 0);
450 cupsArrayDelete(Subscriptions
);
451 Subscriptions
= NULL
;
456 * 'cupsdDeleteSubscription()' - Delete a subscription object.
460 cupsdDeleteSubscription(
461 cupsd_subscription_t
*sub
, /* I - Subscription object */
462 int update
) /* I - 1 = update subscriptions.conf */
465 * Close the pipe to the notifier as needed...
472 * Remove subscription from array...
475 cupsArrayRemove(Subscriptions
, sub
);
481 cupsdClearString(&(sub
->owner
));
482 cupsdClearString(&(sub
->recipient
));
484 cupsArrayDelete(sub
->events
);
489 * Update the subscriptions as needed...
493 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
498 * 'cupsdEventName()' - Return a single event name.
501 const char * /* O - Event name */
503 cupsd_eventmask_t event
) /* I - Event value */
510 case CUPSD_EVENT_PRINTER_RESTARTED
:
511 return ("printer-restarted");
513 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
514 return ("printer-shutdown");
516 case CUPSD_EVENT_PRINTER_STOPPED
:
517 return ("printer-stopped");
519 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
520 return ("printer-finishings-changed");
522 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
523 return ("printer-media-changed");
525 case CUPSD_EVENT_PRINTER_ADDED
:
526 return ("printer-added");
528 case CUPSD_EVENT_PRINTER_DELETED
:
529 return ("printer-deleted");
531 case CUPSD_EVENT_PRINTER_MODIFIED
:
532 return ("printer-modified");
534 case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
:
535 return ("printer-queue-order-changed");
537 case CUPSD_EVENT_PRINTER_STATE
:
538 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
539 return ("printer-state-changed");
541 case CUPSD_EVENT_PRINTER_CONFIG
:
542 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
543 return ("printer-config-changed");
545 case CUPSD_EVENT_PRINTER_CHANGED
:
546 return ("printer-changed");
548 case CUPSD_EVENT_JOB_CREATED
:
549 return ("job-created");
551 case CUPSD_EVENT_JOB_COMPLETED
:
552 return ("job-completed");
554 case CUPSD_EVENT_JOB_STOPPED
:
555 return ("job-stopped");
557 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
558 return ("job-config-changed");
560 case CUPSD_EVENT_JOB_PROGRESS
:
561 return ("job-progress");
563 case CUPSD_EVENT_JOB_STATE
:
564 case CUPSD_EVENT_JOB_STATE_CHANGED
:
565 return ("job-state-changed");
567 case CUPSD_EVENT_SERVER_RESTARTED
:
568 return ("server-restarted");
570 case CUPSD_EVENT_SERVER_STARTED
:
571 return ("server-started");
573 case CUPSD_EVENT_SERVER_STOPPED
:
574 return ("server-stopped");
576 case CUPSD_EVENT_SERVER_AUDIT
:
577 return ("server-audit");
579 case CUPSD_EVENT_ALL
:
586 * 'cupsdEventValue()' - Return the event mask value for a name.
589 cupsd_eventmask_t
/* O - Event mask value */
590 cupsdEventValue(const char *name
) /* I - Name of event */
592 if (!strcmp(name
, "all"))
593 return (CUPSD_EVENT_ALL
);
594 else if (!strcmp(name
, "printer-restarted"))
595 return (CUPSD_EVENT_PRINTER_RESTARTED
);
596 else if (!strcmp(name
, "printer-shutdown"))
597 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
598 else if (!strcmp(name
, "printer-stopped"))
599 return (CUPSD_EVENT_PRINTER_STOPPED
);
600 else if (!strcmp(name
, "printer-finishings-changed"))
601 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
602 else if (!strcmp(name
, "printer-media-changed"))
603 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
604 else if (!strcmp(name
, "printer-added"))
605 return (CUPSD_EVENT_PRINTER_ADDED
);
606 else if (!strcmp(name
, "printer-deleted"))
607 return (CUPSD_EVENT_PRINTER_DELETED
);
608 else if (!strcmp(name
, "printer-modified"))
609 return (CUPSD_EVENT_PRINTER_MODIFIED
);
610 else if (!strcmp(name
, "printer-queue-order-changed"))
611 return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
);
612 else if (!strcmp(name
, "printer-state-changed"))
613 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
614 else if (!strcmp(name
, "printer-config-changed"))
615 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
616 else if (!strcmp(name
, "printer-changed"))
617 return (CUPSD_EVENT_PRINTER_CHANGED
);
618 else if (!strcmp(name
, "job-created"))
619 return (CUPSD_EVENT_JOB_CREATED
);
620 else if (!strcmp(name
, "job-completed"))
621 return (CUPSD_EVENT_JOB_COMPLETED
);
622 else if (!strcmp(name
, "job-stopped"))
623 return (CUPSD_EVENT_JOB_STOPPED
);
624 else if (!strcmp(name
, "job-config-changed"))
625 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
626 else if (!strcmp(name
, "job-progress"))
627 return (CUPSD_EVENT_JOB_PROGRESS
);
628 else if (!strcmp(name
, "job-state-changed"))
629 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
630 else if (!strcmp(name
, "server-restarted"))
631 return (CUPSD_EVENT_SERVER_RESTARTED
);
632 else if (!strcmp(name
, "server-started"))
633 return (CUPSD_EVENT_SERVER_STARTED
);
634 else if (!strcmp(name
, "server-stopped"))
635 return (CUPSD_EVENT_SERVER_STOPPED
);
636 else if (!strcmp(name
, "server-audit"))
637 return (CUPSD_EVENT_SERVER_AUDIT
);
639 return (CUPSD_EVENT_NONE
);
644 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
648 cupsdExpireSubscriptions(
649 cupsd_printer_t
*dest
, /* I - Printer, if any */
650 cupsd_job_t
*job
) /* I - Job, if any */
652 cupsd_subscription_t
*sub
; /* Current subscription */
653 int update
; /* Update subscriptions.conf? */
654 time_t curtime
; /* Current time */
657 curtime
= time(NULL
);
660 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
662 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
663 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
664 (dest
&& sub
->dest
== dest
) ||
665 (job
&& sub
->job
== job
))
667 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
670 cupsdDeleteSubscription(sub
, 0);
676 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
681 * 'cupsdFindSubscription()' - Find a subscription by ID.
684 cupsd_subscription_t
* /* O - Subscription object */
685 cupsdFindSubscription(int id
) /* I - Subscription ID */
687 cupsd_subscription_t sub
; /* Subscription template */
692 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
697 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
701 cupsdLoadAllSubscriptions(void)
703 int i
; /* Looping var */
704 cups_file_t
*fp
; /* subscriptions.conf file */
705 int linenum
; /* Current line number */
706 char line
[1024], /* Line from file */
707 *value
, /* Pointer to value */
708 *valueptr
; /* Pointer into value */
709 cupsd_subscription_t
*sub
; /* Current subscription */
710 int hex
; /* Non-zero if reading hex data */
711 int delete_sub
; /* Delete subscription? */
715 * Open the subscriptions.conf file...
718 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
719 if ((fp
= cupsdOpenConfFile(line
)) == NULL
)
723 * Read all of the lines from the file...
730 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
732 if (!_cups_strcasecmp(line
, "NextSubscriptionId") && value
)
735 * NextSubscriptionId NNN
739 if (i
>= NextSubscriptionId
&& i
> 0)
740 NextSubscriptionId
= i
;
742 else if (!_cups_strcasecmp(line
, "<Subscription"))
748 if (!sub
&& value
&& isdigit(value
[0] & 255))
750 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
755 cupsdLogMessage(CUPSD_LOG_ERROR
,
756 "Syntax error on line %d of subscriptions.conf.",
761 else if (!_cups_strcasecmp(line
, "</Subscription>"))
765 cupsdLogMessage(CUPSD_LOG_ERROR
,
766 "Syntax error on line %d of subscriptions.conf.",
772 cupsdDeleteSubscription(sub
, 0);
779 cupsdLogMessage(CUPSD_LOG_ERROR
,
780 "Syntax error on line %d of subscriptions.conf.",
783 else if (!_cups_strcasecmp(line
, "Events"))
787 * Events name name name ...
792 cupsdLogMessage(CUPSD_LOG_ERROR
,
793 "Syntax error on line %d of subscriptions.conf.",
801 * Separate event names...
804 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
806 while (isspace(*valueptr
& 255))
810 * See if the name exists...
813 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
815 cupsdLogMessage(CUPSD_LOG_ERROR
,
816 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
824 else if (!_cups_strcasecmp(line
, "Owner"))
831 cupsdSetString(&sub
->owner
, value
);
834 cupsdLogMessage(CUPSD_LOG_ERROR
,
835 "Syntax error on line %d of subscriptions.conf.",
840 else if (!_cups_strcasecmp(line
, "Recipient"))
847 cupsdSetString(&sub
->recipient
, value
);
850 cupsdLogMessage(CUPSD_LOG_ERROR
,
851 "Syntax error on line %d of subscriptions.conf.",
856 else if (!_cups_strcasecmp(line
, "JobId"))
862 if (value
&& isdigit(*value
& 255))
864 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
866 cupsdLogMessage(CUPSD_LOG_ERROR
,
867 "Job %s not found on line %d of subscriptions.conf.",
874 cupsdLogMessage(CUPSD_LOG_ERROR
,
875 "Syntax error on line %d of subscriptions.conf.",
880 else if (!_cups_strcasecmp(line
, "PrinterName"))
888 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
890 cupsdLogMessage(CUPSD_LOG_ERROR
,
891 "Printer \'%s\' not found on line %d of subscriptions.conf.",
898 cupsdLogMessage(CUPSD_LOG_ERROR
,
899 "Syntax error on line %d of subscriptions.conf.",
904 else if (!_cups_strcasecmp(line
, "UserData"))
907 * UserData encoded-string
912 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
914 if (*valueptr
== '<' && !hex
)
922 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
924 if (isdigit(valueptr
[0]))
925 sub
->user_data
[i
] = (unsigned char)((valueptr
[0] - '0') << 4);
927 sub
->user_data
[i
] = (unsigned char)((tolower(valueptr
[0]) - 'a' + 10) << 4);
929 if (isdigit(valueptr
[1]))
930 sub
->user_data
[i
] |= valueptr
[1] - '0';
932 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
936 if (*valueptr
== '>')
946 sub
->user_data
[i
] = (unsigned char)*valueptr
++;
951 cupsdLogMessage(CUPSD_LOG_ERROR
,
952 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
956 sub
->user_data_len
= i
;
960 cupsdLogMessage(CUPSD_LOG_ERROR
,
961 "Syntax error on line %d of subscriptions.conf.",
966 else if (!_cups_strcasecmp(line
, "LeaseDuration"))
972 if (value
&& isdigit(*value
& 255))
974 sub
->lease
= atoi(value
);
975 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
979 cupsdLogMessage(CUPSD_LOG_ERROR
,
980 "Syntax error on line %d of subscriptions.conf.",
985 else if (!_cups_strcasecmp(line
, "Interval"))
991 if (value
&& isdigit(*value
& 255))
992 sub
->interval
= atoi(value
);
995 cupsdLogMessage(CUPSD_LOG_ERROR
,
996 "Syntax error on line %d of subscriptions.conf.",
1001 else if (!_cups_strcasecmp(line
, "ExpirationTime"))
1007 if (value
&& isdigit(*value
& 255))
1008 sub
->expire
= atoi(value
);
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "Syntax error on line %d of subscriptions.conf.",
1017 else if (!_cups_strcasecmp(line
, "NextEventId"))
1023 if (value
&& isdigit(*value
& 255))
1024 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1027 cupsdLogMessage(CUPSD_LOG_ERROR
,
1028 "Syntax error on line %d of subscriptions.conf.",
1036 * Something else we don't understand...
1039 cupsdLogMessage(CUPSD_LOG_ERROR
,
1040 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1050 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1054 cupsdSaveAllSubscriptions(void)
1056 int i
; /* Looping var */
1057 cups_file_t
*fp
; /* subscriptions.conf file */
1058 char filename
[1024], /* subscriptions.conf filename */
1059 temp
[1024]; /* Temporary string */
1060 cupsd_subscription_t
*sub
; /* Current subscription */
1061 time_t curtime
; /* Current time */
1062 struct tm
*curdate
; /* Current date */
1063 unsigned mask
; /* Current event mask */
1064 const char *name
; /* Current event name */
1065 int hex
; /* Non-zero if we are writing hex data */
1069 * Create the subscriptions.conf file...
1072 snprintf(filename
, sizeof(filename
), "%s/subscriptions.conf", ServerRoot
);
1074 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
1077 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1080 * Write a small header to the file...
1083 curtime
= time(NULL
);
1084 curdate
= localtime(&curtime
);
1085 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1087 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1088 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1090 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1093 * Write every subscription known to the system...
1096 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1098 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1100 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1102 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1105 * Simple event list...
1108 cupsFilePrintf(fp
, "Events %s\n", name
);
1113 * Complex event list...
1116 cupsFilePuts(fp
, "Events");
1118 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1119 if (sub
->mask
& mask
)
1120 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1122 cupsFilePuts(fp
, "\n");
1126 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1128 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1130 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1132 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1134 if (sub
->user_data_len
> 0)
1136 cupsFilePuts(fp
, "UserData ");
1138 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1140 if (sub
->user_data
[i
] < ' ' ||
1141 sub
->user_data
[i
] > 0x7f ||
1142 sub
->user_data
[i
] == '<')
1146 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1150 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1156 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1160 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1165 cupsFilePuts(fp
, ">\n");
1167 cupsFilePutChar(fp
, '\n');
1170 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1171 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1172 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1173 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1175 cupsFilePuts(fp
, "</Subscription>\n");
1178 cupsdCloseCreatedConfFile(fp
, filename
);
1183 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1187 cupsdStopAllNotifiers(void)
1189 cupsd_subscription_t
*sub
; /* Current subscription */
1193 * See if we have started any notifiers...
1196 if (!NotifierStatusBuffer
)
1200 * Yes, kill any processes that are left...
1203 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1205 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1208 cupsdEndProcess(sub
->pid
, 0);
1215 * Close the status pipes...
1218 if (NotifierPipes
[0] >= 0)
1220 cupsdRemoveSelect(NotifierPipes
[0]);
1222 cupsdStatBufDelete(NotifierStatusBuffer
);
1224 close(NotifierPipes
[0]);
1225 close(NotifierPipes
[1]);
1227 NotifierPipes
[0] = -1;
1228 NotifierPipes
[1] = -1;
1229 NotifierStatusBuffer
= NULL
;
1235 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1238 static int /* O - Result of comparison */
1239 cupsd_compare_subscriptions(
1240 cupsd_subscription_t
*first
, /* I - First subscription object */
1241 cupsd_subscription_t
*second
, /* I - Second subscription object */
1242 void *unused
) /* I - Unused user data pointer */
1246 return (first
->id
- second
->id
);
1251 * 'cupsd_delete_event()' - Delete a single event...
1253 * Oldest events must be deleted first, otherwise the subscription cache
1254 * flushing code will not work properly.
1258 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1264 ippDelete(event
->attrs
);
1271 * 'cupsd_send_dbus()' - Send a DBUS notification...
1275 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1276 cupsd_printer_t
*dest
,/* I - Destination, if any */
1277 cupsd_job_t
*job
) /* I - Job, if any */
1279 DBusError error
; /* Error, if any */
1280 DBusMessage
*message
; /* Message to send */
1281 DBusMessageIter iter
; /* Iterator for message data */
1282 const char *what
; /* What to send */
1283 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1287 * Figure out what to send, if anything...
1290 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1291 what
= "PrinterAdded";
1292 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1293 what
= "PrinterRemoved";
1294 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1295 what
= "QueueChanged";
1296 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1297 what
= "JobQueuedLocal";
1298 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1299 job
->state_value
== IPP_JOB_PROCESSING
)
1300 what
= "JobStartedLocal";
1305 * Verify connection to DBUS server...
1308 if (con
&& !dbus_connection_get_is_connected(con
))
1310 dbus_connection_unref(con
);
1316 dbus_error_init(&error
);
1318 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1321 dbus_error_free(&error
);
1327 * Create and send the new message...
1330 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1331 "com.redhat.PrinterSpooler", what
);
1333 dbus_message_append_iter_init(message
, &iter
);
1335 dbus_message_iter_append_string(&iter
, dest
->name
);
1338 dbus_message_iter_append_uint32(&iter
, job
->id
);
1339 dbus_message_iter_append_string(&iter
, job
->username
);
1342 dbus_connection_send(con
, message
, NULL
);
1343 dbus_connection_flush(con
);
1344 dbus_message_unref(message
);
1346 #endif /* HAVE_DBUS */
1350 * 'cupsd_send_notification()' - Send a notification for the specified event.
1354 cupsd_send_notification(
1355 cupsd_subscription_t
*sub
, /* I - Subscription object */
1356 cupsd_event_t
*event
) /* I - Event to send */
1358 ipp_state_t state
; /* IPP event state */
1361 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1362 "cupsd_send_notification(sub=%p(%d), event=%p(%s))",
1363 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1366 * Allocate the events array as needed...
1371 sub
->events
= cupsArrayNew3((cups_array_func_t
)NULL
, NULL
,
1372 (cups_ahash_func_t
)NULL
, 0,
1373 (cups_acopy_func_t
)NULL
,
1374 (cups_afree_func_t
)cupsd_delete_event
);
1378 cupsdLogMessage(CUPSD_LOG_CRIT
,
1379 "Unable to allocate memory for subscription #%d!",
1386 * Purge an old event as needed...
1389 if (cupsArrayCount(sub
->events
) >= MaxEvents
)
1392 * Purge the oldest event in the cache...
1395 cupsArrayRemove(sub
->events
, cupsArrayFirst(sub
->events
));
1397 sub
->first_event_id
++;
1401 * Add the event to the subscription. Since the events array is
1402 * always MaxEvents in length, and since we will have already
1403 * removed an event from the subscription cache if we hit the
1404 * event cache limit, we don't need to check for overflow here...
1407 cupsArrayAdd(sub
->events
, event
);
1410 * Deliver the event...
1418 cupsd_start_notifier(sub
);
1420 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "sub->pipe=%d", sub
->pipe
);
1425 event
->attrs
->state
= IPP_IDLE
;
1427 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1428 if (state
== IPP_ERROR
)
1431 if (state
== IPP_ERROR
)
1436 * Notifier died, try restarting it...
1439 cupsdLogMessage(CUPSD_LOG_WARN
,
1440 "Notifier for subscription %d (%s) went away, "
1442 sub
->id
, sub
->recipient
);
1443 cupsdEndProcess(sub
->pid
, 0);
1450 cupsdLogMessage(CUPSD_LOG_ERROR
,
1451 "Unable to send event for subscription %d (%s)!",
1452 sub
->id
, sub
->recipient
);
1456 * If we get this far, break out of the loop...
1464 * Bump the event sequence number...
1467 sub
->next_event_id
++;
1472 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1476 cupsd_start_notifier(
1477 cupsd_subscription_t
*sub
) /* I - Subscription object */
1479 int pid
; /* Notifier process ID */
1480 int fds
[2]; /* Pipe file descriptors */
1481 char *argv
[4], /* Command-line arguments */
1482 *envp
[MAX_ENV
], /* Environment variables */
1483 user_data
[128], /* Base-64 encoded user data */
1484 scheme
[256], /* notify-recipient-uri scheme */
1485 *ptr
, /* Pointer into scheme */
1486 command
[1024]; /* Notifier command */
1490 * Extract the scheme name from the recipient URI and point to the
1491 * notifier program...
1494 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1495 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1498 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1501 * Base-64 encode the user data...
1504 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1505 sub
->user_data_len
);
1508 * Setup the argument array...
1512 argv
[1] = sub
->recipient
;
1513 argv
[2] = user_data
;
1517 * Setup the environment...
1520 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1523 * Create pipes as needed...
1526 if (!NotifierStatusBuffer
)
1529 * Create the status pipe...
1532 if (cupsdOpenPipe(NotifierPipes
))
1534 cupsdLogMessage(CUPSD_LOG_ERROR
,
1535 "Unable to create pipes for notifier status - %s",
1540 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1542 cupsdAddSelect(NotifierPipes
[0], (cupsd_selfunc_t
)cupsd_update_notifier
,
1546 if (cupsdOpenPipe(fds
))
1548 cupsdLogMessage(CUPSD_LOG_ERROR
,
1549 "Unable to create pipes for notifier %s - %s",
1550 scheme
, strerror(errno
));
1555 * Make sure the delivery pipe is non-blocking...
1558 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1561 * Create the notifier process...
1564 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1565 -1, -1, 0, DefaultProfile
, NULL
, &pid
) < 0)
1568 * Error - can't fork!
1571 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1572 scheme
, strerror(errno
));
1574 cupsdClosePipe(fds
);
1579 * Fork successful - return the PID...
1582 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1595 * 'cupsd_update_notifier()' - Read messages from notifiers.
1599 cupsd_update_notifier(void)
1601 char message
[1024]; /* Pointer to message text */
1602 int loglevel
; /* Log level for message */
1605 while (cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1606 message
, sizeof(message
)))
1608 if (loglevel
== CUPSD_LOG_INFO
)
1609 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
1611 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))