2 * "$Id: subscriptions.c 11558 2014-02-06 18:33:34Z msweet $"
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 &&
109 (sub
->dest
== dest
|| !sub
->dest
) &&
110 (sub
->job
== job
|| !sub
->job
))
113 * Need this event, so create a new event record...
116 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
118 cupsdLogMessage(CUPSD_LOG_CRIT
,
119 "Unable to allocate memory for event - %s",
125 temp
->time
= time(NULL
);
126 temp
->attrs
= ippNew();
132 temp
->dest
= dest
= cupsdFindPrinter(job
->dest
);
135 * Add common event notification attributes...
138 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_CHARSET
,
139 "notify-charset", NULL
, "utf-8");
141 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_LANGUAGE
,
142 "notify-natural-language", NULL
, "en-US");
144 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
145 "notify-subscription-id", sub
->id
);
147 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
148 "notify-sequence-number", sub
->next_event_id
);
150 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
151 "notify-subscribed-event", NULL
, cupsdEventName(event
));
153 if (sub
->user_data_len
> 0)
154 ippAddOctetString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
155 "notify-user-data", sub
->user_data
,
158 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
159 "printer-up-time", time(NULL
));
162 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
165 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
166 "notify-text", NULL
, ftext
);
171 * Add printer attributes...
174 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
175 "notify-printer-uri", NULL
, dest
->uri
);
177 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
178 "printer-name", NULL
, dest
->name
);
180 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
181 "printer-state", dest
->state
);
183 if (dest
->num_reasons
== 0)
184 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
185 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
186 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
188 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
189 IPP_TAG_KEYWORD
, "printer-state-reasons",
190 dest
->num_reasons
, NULL
,
191 (const char * const *)dest
->reasons
);
193 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
194 "printer-is-accepting-jobs", (char)dest
->accepting
);
200 * Add job attributes...
203 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
204 "notify-job-id", job
->id
);
205 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
206 "job-state", job
->state_value
);
208 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
209 IPP_TAG_NAME
)) != NULL
)
210 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
211 "job-name", NULL
, attr
->values
[0].string
.text
);
213 switch (job
->state_value
)
215 case IPP_JOB_PENDING
:
216 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
217 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
218 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
221 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
222 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
227 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
228 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
229 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
230 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
231 "job-hold-until-specified");
233 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
234 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
238 case IPP_JOB_PROCESSING
:
239 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
240 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
244 case IPP_JOB_STOPPED
:
245 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
246 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
250 case IPP_JOB_CANCELED
:
251 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
252 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
253 "job-canceled-by-user");
256 case IPP_JOB_ABORTED
:
257 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
258 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
259 "aborted-by-system");
262 case IPP_JOB_COMPLETED
:
263 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
264 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
265 "job-completed-successfully");
269 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
270 "job-impressions-completed",
271 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
275 * Send the notification for this subscription...
278 cupsd_send_notification(sub
, temp
);
283 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
285 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
286 cupsdEventName(event
));
291 * 'cupsdAddSubscription()' - Add a new subscription object.
294 cupsd_subscription_t
* /* O - New subscription object */
295 cupsdAddSubscription(
296 unsigned mask
, /* I - Event mask */
297 cupsd_printer_t
*dest
, /* I - Printer, if any */
298 cupsd_job_t
*job
, /* I - Job, if any */
299 const char *uri
, /* I - notify-recipient-uri, if any */
300 int sub_id
) /* I - notify-subscription-id or 0 */
302 cupsd_subscription_t
*temp
; /* New subscription object */
305 cupsdLogMessage(CUPSD_LOG_DEBUG
,
306 "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
308 mask
, dest
, dest
? dest
->name
: "", job
, job
? job
->id
: 0,
309 uri
? uri
: "(null)");
312 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
317 cupsdLogMessage(CUPSD_LOG_CRIT
,
318 "Unable to allocate memory for subscriptions - %s",
324 * Limit the number of subscriptions...
327 if (MaxSubscriptions
> 0 && cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
329 cupsdLogMessage(CUPSD_LOG_DEBUG
,
330 "cupsdAddSubscription: Reached MaxSubscriptions %d "
331 "(count=%d)", MaxSubscriptions
,
332 cupsArrayCount(Subscriptions
));
336 if (MaxSubscriptionsPerJob
> 0 && job
)
338 int count
; /* Number of job subscriptions */
340 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
343 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
344 if (temp
->job
== job
)
347 if (count
>= MaxSubscriptionsPerJob
)
349 cupsdLogMessage(CUPSD_LOG_DEBUG
,
350 "cupsdAddSubscription: Reached MaxSubscriptionsPerJob %d "
351 "for job #%d (count=%d)", MaxSubscriptionsPerJob
,
357 if (MaxSubscriptionsPerPrinter
> 0 && dest
)
359 int count
; /* Number of printer subscriptions */
361 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
364 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
365 if (temp
->dest
== dest
)
368 if (count
>= MaxSubscriptionsPerPrinter
)
370 cupsdLogMessage(CUPSD_LOG_DEBUG
,
371 "cupsdAddSubscription: Reached "
372 "MaxSubscriptionsPerPrinter %d for %s (count=%d)",
373 MaxSubscriptionsPerPrinter
, dest
->name
, count
);
379 * Allocate memory for this subscription...
382 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
384 cupsdLogMessage(CUPSD_LOG_CRIT
,
385 "Unable to allocate memory for subscription object - %s",
391 * Fill in common data...
398 if (sub_id
>= NextSubscriptionId
)
399 NextSubscriptionId
= sub_id
+ 1;
403 temp
->id
= NextSubscriptionId
;
405 NextSubscriptionId
++;
412 temp
->first_event_id
= 1;
413 temp
->next_event_id
= 1;
415 cupsdSetString(&(temp
->recipient
), uri
);
418 * Add the subscription to the array...
421 cupsArrayAdd(Subscriptions
, temp
);
424 * For RSS subscriptions, run the notifier immediately...
427 if (uri
&& !strncmp(uri
, "rss:", 4))
428 cupsd_start_notifier(temp
);
435 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
439 cupsdDeleteAllSubscriptions(void)
441 cupsd_subscription_t
*sub
; /* Subscription */
447 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
449 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
450 cupsdDeleteSubscription(sub
, 0);
452 cupsArrayDelete(Subscriptions
);
453 Subscriptions
= NULL
;
458 * 'cupsdDeleteSubscription()' - Delete a subscription object.
462 cupsdDeleteSubscription(
463 cupsd_subscription_t
*sub
, /* I - Subscription object */
464 int update
) /* I - 1 = update subscriptions.conf */
467 * Close the pipe to the notifier as needed...
474 * Remove subscription from array...
477 cupsArrayRemove(Subscriptions
, sub
);
483 cupsdClearString(&(sub
->owner
));
484 cupsdClearString(&(sub
->recipient
));
486 cupsArrayDelete(sub
->events
);
491 * Update the subscriptions as needed...
495 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
500 * 'cupsdEventName()' - Return a single event name.
503 const char * /* O - Event name */
505 cupsd_eventmask_t event
) /* I - Event value */
512 case CUPSD_EVENT_PRINTER_RESTARTED
:
513 return ("printer-restarted");
515 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
516 return ("printer-shutdown");
518 case CUPSD_EVENT_PRINTER_STOPPED
:
519 return ("printer-stopped");
521 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
522 return ("printer-finishings-changed");
524 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
525 return ("printer-media-changed");
527 case CUPSD_EVENT_PRINTER_ADDED
:
528 return ("printer-added");
530 case CUPSD_EVENT_PRINTER_DELETED
:
531 return ("printer-deleted");
533 case CUPSD_EVENT_PRINTER_MODIFIED
:
534 return ("printer-modified");
536 case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
:
537 return ("printer-queue-order-changed");
539 case CUPSD_EVENT_PRINTER_STATE
:
540 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
541 return ("printer-state-changed");
543 case CUPSD_EVENT_PRINTER_CONFIG
:
544 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
545 return ("printer-config-changed");
547 case CUPSD_EVENT_PRINTER_CHANGED
:
548 return ("printer-changed");
550 case CUPSD_EVENT_JOB_CREATED
:
551 return ("job-created");
553 case CUPSD_EVENT_JOB_COMPLETED
:
554 return ("job-completed");
556 case CUPSD_EVENT_JOB_STOPPED
:
557 return ("job-stopped");
559 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
560 return ("job-config-changed");
562 case CUPSD_EVENT_JOB_PROGRESS
:
563 return ("job-progress");
565 case CUPSD_EVENT_JOB_STATE
:
566 case CUPSD_EVENT_JOB_STATE_CHANGED
:
567 return ("job-state-changed");
569 case CUPSD_EVENT_SERVER_RESTARTED
:
570 return ("server-restarted");
572 case CUPSD_EVENT_SERVER_STARTED
:
573 return ("server-started");
575 case CUPSD_EVENT_SERVER_STOPPED
:
576 return ("server-stopped");
578 case CUPSD_EVENT_SERVER_AUDIT
:
579 return ("server-audit");
581 case CUPSD_EVENT_ALL
:
588 * 'cupsdEventValue()' - Return the event mask value for a name.
591 cupsd_eventmask_t
/* O - Event mask value */
592 cupsdEventValue(const char *name
) /* I - Name of event */
594 if (!strcmp(name
, "all"))
595 return (CUPSD_EVENT_ALL
);
596 else if (!strcmp(name
, "printer-restarted"))
597 return (CUPSD_EVENT_PRINTER_RESTARTED
);
598 else if (!strcmp(name
, "printer-shutdown"))
599 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
600 else if (!strcmp(name
, "printer-stopped"))
601 return (CUPSD_EVENT_PRINTER_STOPPED
);
602 else if (!strcmp(name
, "printer-finishings-changed"))
603 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
604 else if (!strcmp(name
, "printer-media-changed"))
605 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
606 else if (!strcmp(name
, "printer-added"))
607 return (CUPSD_EVENT_PRINTER_ADDED
);
608 else if (!strcmp(name
, "printer-deleted"))
609 return (CUPSD_EVENT_PRINTER_DELETED
);
610 else if (!strcmp(name
, "printer-modified"))
611 return (CUPSD_EVENT_PRINTER_MODIFIED
);
612 else if (!strcmp(name
, "printer-queue-order-changed"))
613 return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
);
614 else if (!strcmp(name
, "printer-state-changed"))
615 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
616 else if (!strcmp(name
, "printer-config-changed"))
617 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
618 else if (!strcmp(name
, "printer-changed"))
619 return (CUPSD_EVENT_PRINTER_CHANGED
);
620 else if (!strcmp(name
, "job-created"))
621 return (CUPSD_EVENT_JOB_CREATED
);
622 else if (!strcmp(name
, "job-completed"))
623 return (CUPSD_EVENT_JOB_COMPLETED
);
624 else if (!strcmp(name
, "job-stopped"))
625 return (CUPSD_EVENT_JOB_STOPPED
);
626 else if (!strcmp(name
, "job-config-changed"))
627 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
628 else if (!strcmp(name
, "job-progress"))
629 return (CUPSD_EVENT_JOB_PROGRESS
);
630 else if (!strcmp(name
, "job-state-changed"))
631 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
632 else if (!strcmp(name
, "server-restarted"))
633 return (CUPSD_EVENT_SERVER_RESTARTED
);
634 else if (!strcmp(name
, "server-started"))
635 return (CUPSD_EVENT_SERVER_STARTED
);
636 else if (!strcmp(name
, "server-stopped"))
637 return (CUPSD_EVENT_SERVER_STOPPED
);
638 else if (!strcmp(name
, "server-audit"))
639 return (CUPSD_EVENT_SERVER_AUDIT
);
641 return (CUPSD_EVENT_NONE
);
646 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
650 cupsdExpireSubscriptions(
651 cupsd_printer_t
*dest
, /* I - Printer, if any */
652 cupsd_job_t
*job
) /* I - Job, if any */
654 cupsd_subscription_t
*sub
; /* Current subscription */
655 int update
; /* Update subscriptions.conf? */
656 time_t curtime
; /* Current time */
659 curtime
= time(NULL
);
662 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
664 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
665 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
666 (dest
&& sub
->dest
== dest
) ||
667 (job
&& sub
->job
== job
))
669 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
672 cupsdDeleteSubscription(sub
, 0);
678 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
683 * 'cupsdFindSubscription()' - Find a subscription by ID.
686 cupsd_subscription_t
* /* O - Subscription object */
687 cupsdFindSubscription(int id
) /* I - Subscription ID */
689 cupsd_subscription_t sub
; /* Subscription template */
694 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
699 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
703 cupsdLoadAllSubscriptions(void)
705 int i
; /* Looping var */
706 cups_file_t
*fp
; /* subscriptions.conf file */
707 int linenum
; /* Current line number */
708 char line
[1024], /* Line from file */
709 *value
, /* Pointer to value */
710 *valueptr
; /* Pointer into value */
711 cupsd_subscription_t
*sub
; /* Current subscription */
712 int hex
; /* Non-zero if reading hex data */
713 int delete_sub
; /* Delete subscription? */
717 * Open the subscriptions.conf file...
720 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
721 if ((fp
= cupsdOpenConfFile(line
)) == NULL
)
725 * Read all of the lines from the file...
732 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
734 if (!_cups_strcasecmp(line
, "NextSubscriptionId") && value
)
737 * NextSubscriptionId NNN
741 if (i
>= NextSubscriptionId
&& i
> 0)
742 NextSubscriptionId
= i
;
744 else if (!_cups_strcasecmp(line
, "<Subscription"))
750 if (!sub
&& value
&& isdigit(value
[0] & 255))
752 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
757 cupsdLogMessage(CUPSD_LOG_ERROR
,
758 "Syntax error on line %d of subscriptions.conf.",
763 else if (!_cups_strcasecmp(line
, "</Subscription>"))
767 cupsdLogMessage(CUPSD_LOG_ERROR
,
768 "Syntax error on line %d of subscriptions.conf.",
774 cupsdDeleteSubscription(sub
, 0);
781 cupsdLogMessage(CUPSD_LOG_ERROR
,
782 "Syntax error on line %d of subscriptions.conf.",
785 else if (!_cups_strcasecmp(line
, "Events"))
789 * Events name name name ...
794 cupsdLogMessage(CUPSD_LOG_ERROR
,
795 "Syntax error on line %d of subscriptions.conf.",
803 * Separate event names...
806 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
808 while (isspace(*valueptr
& 255))
812 * See if the name exists...
815 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
817 cupsdLogMessage(CUPSD_LOG_ERROR
,
818 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
826 else if (!_cups_strcasecmp(line
, "Owner"))
833 cupsdSetString(&sub
->owner
, value
);
836 cupsdLogMessage(CUPSD_LOG_ERROR
,
837 "Syntax error on line %d of subscriptions.conf.",
842 else if (!_cups_strcasecmp(line
, "Recipient"))
849 cupsdSetString(&sub
->recipient
, value
);
852 cupsdLogMessage(CUPSD_LOG_ERROR
,
853 "Syntax error on line %d of subscriptions.conf.",
858 else if (!_cups_strcasecmp(line
, "JobId"))
864 if (value
&& isdigit(*value
& 255))
866 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
868 cupsdLogMessage(CUPSD_LOG_ERROR
,
869 "Job %s not found on line %d of subscriptions.conf.",
876 cupsdLogMessage(CUPSD_LOG_ERROR
,
877 "Syntax error on line %d of subscriptions.conf.",
882 else if (!_cups_strcasecmp(line
, "PrinterName"))
890 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
892 cupsdLogMessage(CUPSD_LOG_ERROR
,
893 "Printer \'%s\' not found on line %d of subscriptions.conf.",
900 cupsdLogMessage(CUPSD_LOG_ERROR
,
901 "Syntax error on line %d of subscriptions.conf.",
906 else if (!_cups_strcasecmp(line
, "UserData"))
909 * UserData encoded-string
914 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
916 if (*valueptr
== '<' && !hex
)
924 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
926 if (isdigit(valueptr
[0]))
927 sub
->user_data
[i
] = (unsigned char)((valueptr
[0] - '0') << 4);
929 sub
->user_data
[i
] = (unsigned char)((tolower(valueptr
[0]) - 'a' + 10) << 4);
931 if (isdigit(valueptr
[1]))
932 sub
->user_data
[i
] |= valueptr
[1] - '0';
934 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
938 if (*valueptr
== '>')
948 sub
->user_data
[i
] = (unsigned char)*valueptr
++;
953 cupsdLogMessage(CUPSD_LOG_ERROR
,
954 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
958 sub
->user_data_len
= i
;
962 cupsdLogMessage(CUPSD_LOG_ERROR
,
963 "Syntax error on line %d of subscriptions.conf.",
968 else if (!_cups_strcasecmp(line
, "LeaseDuration"))
974 if (value
&& isdigit(*value
& 255))
976 sub
->lease
= atoi(value
);
977 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
981 cupsdLogMessage(CUPSD_LOG_ERROR
,
982 "Syntax error on line %d of subscriptions.conf.",
987 else if (!_cups_strcasecmp(line
, "Interval"))
993 if (value
&& isdigit(*value
& 255))
994 sub
->interval
= atoi(value
);
997 cupsdLogMessage(CUPSD_LOG_ERROR
,
998 "Syntax error on line %d of subscriptions.conf.",
1003 else if (!_cups_strcasecmp(line
, "ExpirationTime"))
1009 if (value
&& isdigit(*value
& 255))
1010 sub
->expire
= atoi(value
);
1013 cupsdLogMessage(CUPSD_LOG_ERROR
,
1014 "Syntax error on line %d of subscriptions.conf.",
1019 else if (!_cups_strcasecmp(line
, "NextEventId"))
1025 if (value
&& isdigit(*value
& 255))
1026 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1029 cupsdLogMessage(CUPSD_LOG_ERROR
,
1030 "Syntax error on line %d of subscriptions.conf.",
1038 * Something else we don't understand...
1041 cupsdLogMessage(CUPSD_LOG_ERROR
,
1042 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1052 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1056 cupsdSaveAllSubscriptions(void)
1058 int i
; /* Looping var */
1059 cups_file_t
*fp
; /* subscriptions.conf file */
1060 char filename
[1024], /* subscriptions.conf filename */
1061 temp
[1024]; /* Temporary string */
1062 cupsd_subscription_t
*sub
; /* Current subscription */
1063 time_t curtime
; /* Current time */
1064 struct tm
*curdate
; /* Current date */
1065 unsigned mask
; /* Current event mask */
1066 const char *name
; /* Current event name */
1067 int hex
; /* Non-zero if we are writing hex data */
1071 * Create the subscriptions.conf file...
1074 snprintf(filename
, sizeof(filename
), "%s/subscriptions.conf", ServerRoot
);
1076 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
1079 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1082 * Write a small header to the file...
1085 curtime
= time(NULL
);
1086 curdate
= localtime(&curtime
);
1087 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1089 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1090 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1092 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1095 * Write every subscription known to the system...
1098 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1100 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1102 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1104 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1107 * Simple event list...
1110 cupsFilePrintf(fp
, "Events %s\n", name
);
1115 * Complex event list...
1118 cupsFilePuts(fp
, "Events");
1120 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1121 if (sub
->mask
& mask
)
1122 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1124 cupsFilePuts(fp
, "\n");
1128 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1130 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1132 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1134 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1136 if (sub
->user_data_len
> 0)
1138 cupsFilePuts(fp
, "UserData ");
1140 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1142 if (sub
->user_data
[i
] < ' ' ||
1143 sub
->user_data
[i
] > 0x7f ||
1144 sub
->user_data
[i
] == '<')
1148 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1152 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1158 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1162 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1167 cupsFilePuts(fp
, ">\n");
1169 cupsFilePutChar(fp
, '\n');
1172 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1173 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1174 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1175 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1177 cupsFilePuts(fp
, "</Subscription>\n");
1180 cupsdCloseCreatedConfFile(fp
, filename
);
1185 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1189 cupsdStopAllNotifiers(void)
1191 cupsd_subscription_t
*sub
; /* Current subscription */
1195 * See if we have started any notifiers...
1198 if (!NotifierStatusBuffer
)
1202 * Yes, kill any processes that are left...
1205 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1207 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1210 cupsdEndProcess(sub
->pid
, 0);
1217 * Close the status pipes...
1220 if (NotifierPipes
[0] >= 0)
1222 cupsdRemoveSelect(NotifierPipes
[0]);
1224 cupsdStatBufDelete(NotifierStatusBuffer
);
1226 close(NotifierPipes
[0]);
1227 close(NotifierPipes
[1]);
1229 NotifierPipes
[0] = -1;
1230 NotifierPipes
[1] = -1;
1231 NotifierStatusBuffer
= NULL
;
1237 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1240 static int /* O - Result of comparison */
1241 cupsd_compare_subscriptions(
1242 cupsd_subscription_t
*first
, /* I - First subscription object */
1243 cupsd_subscription_t
*second
, /* I - Second subscription object */
1244 void *unused
) /* I - Unused user data pointer */
1248 return (first
->id
- second
->id
);
1253 * 'cupsd_delete_event()' - Delete a single event...
1255 * Oldest events must be deleted first, otherwise the subscription cache
1256 * flushing code will not work properly.
1260 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1266 ippDelete(event
->attrs
);
1273 * 'cupsd_send_dbus()' - Send a DBUS notification...
1277 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1278 cupsd_printer_t
*dest
,/* I - Destination, if any */
1279 cupsd_job_t
*job
) /* I - Job, if any */
1281 DBusError error
; /* Error, if any */
1282 DBusMessage
*message
; /* Message to send */
1283 DBusMessageIter iter
; /* Iterator for message data */
1284 const char *what
; /* What to send */
1285 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1289 * Figure out what to send, if anything...
1292 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1293 what
= "PrinterAdded";
1294 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1295 what
= "PrinterRemoved";
1296 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1297 what
= "QueueChanged";
1298 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1299 what
= "JobQueuedLocal";
1300 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1301 job
->state_value
== IPP_JOB_PROCESSING
)
1302 what
= "JobStartedLocal";
1307 * Verify connection to DBUS server...
1310 if (con
&& !dbus_connection_get_is_connected(con
))
1312 dbus_connection_unref(con
);
1318 dbus_error_init(&error
);
1320 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1323 dbus_error_free(&error
);
1329 * Create and send the new message...
1332 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1333 "com.redhat.PrinterSpooler", what
);
1335 dbus_message_append_iter_init(message
, &iter
);
1337 dbus_message_iter_append_string(&iter
, dest
->name
);
1340 dbus_message_iter_append_uint32(&iter
, job
->id
);
1341 dbus_message_iter_append_string(&iter
, job
->username
);
1344 dbus_connection_send(con
, message
, NULL
);
1345 dbus_connection_flush(con
);
1346 dbus_message_unref(message
);
1348 #endif /* HAVE_DBUS */
1352 * 'cupsd_send_notification()' - Send a notification for the specified event.
1356 cupsd_send_notification(
1357 cupsd_subscription_t
*sub
, /* I - Subscription object */
1358 cupsd_event_t
*event
) /* I - Event to send */
1360 ipp_state_t state
; /* IPP event state */
1363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1364 "cupsd_send_notification(sub=%p(%d), event=%p(%s))",
1365 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1368 * Allocate the events array as needed...
1373 sub
->events
= cupsArrayNew3((cups_array_func_t
)NULL
, NULL
,
1374 (cups_ahash_func_t
)NULL
, 0,
1375 (cups_acopy_func_t
)NULL
,
1376 (cups_afree_func_t
)cupsd_delete_event
);
1380 cupsdLogMessage(CUPSD_LOG_CRIT
,
1381 "Unable to allocate memory for subscription #%d!",
1388 * Purge an old event as needed...
1391 if (cupsArrayCount(sub
->events
) >= MaxEvents
)
1394 * Purge the oldest event in the cache...
1397 cupsArrayRemove(sub
->events
, cupsArrayFirst(sub
->events
));
1399 sub
->first_event_id
++;
1403 * Add the event to the subscription. Since the events array is
1404 * always MaxEvents in length, and since we will have already
1405 * removed an event from the subscription cache if we hit the
1406 * event cache limit, we don't need to check for overflow here...
1409 cupsArrayAdd(sub
->events
, event
);
1412 * Deliver the event...
1420 cupsd_start_notifier(sub
);
1422 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "sub->pipe=%d", sub
->pipe
);
1427 event
->attrs
->state
= IPP_IDLE
;
1429 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1430 if (state
== IPP_ERROR
)
1433 if (state
== IPP_ERROR
)
1438 * Notifier died, try restarting it...
1441 cupsdLogMessage(CUPSD_LOG_WARN
,
1442 "Notifier for subscription %d (%s) went away, "
1444 sub
->id
, sub
->recipient
);
1445 cupsdEndProcess(sub
->pid
, 0);
1452 cupsdLogMessage(CUPSD_LOG_ERROR
,
1453 "Unable to send event for subscription %d (%s)!",
1454 sub
->id
, sub
->recipient
);
1458 * If we get this far, break out of the loop...
1466 * Bump the event sequence number...
1469 sub
->next_event_id
++;
1474 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1478 cupsd_start_notifier(
1479 cupsd_subscription_t
*sub
) /* I - Subscription object */
1481 int pid
; /* Notifier process ID */
1482 int fds
[2]; /* Pipe file descriptors */
1483 char *argv
[4], /* Command-line arguments */
1484 *envp
[MAX_ENV
], /* Environment variables */
1485 user_data
[128], /* Base-64 encoded user data */
1486 scheme
[256], /* notify-recipient-uri scheme */
1487 *ptr
, /* Pointer into scheme */
1488 command
[1024]; /* Notifier command */
1492 * Extract the scheme name from the recipient URI and point to the
1493 * notifier program...
1496 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1497 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1500 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1503 * Base-64 encode the user data...
1506 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1507 sub
->user_data_len
);
1510 * Setup the argument array...
1514 argv
[1] = sub
->recipient
;
1515 argv
[2] = user_data
;
1519 * Setup the environment...
1522 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1525 * Create pipes as needed...
1528 if (!NotifierStatusBuffer
)
1531 * Create the status pipe...
1534 if (cupsdOpenPipe(NotifierPipes
))
1536 cupsdLogMessage(CUPSD_LOG_ERROR
,
1537 "Unable to create pipes for notifier status - %s",
1542 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1544 cupsdAddSelect(NotifierPipes
[0], (cupsd_selfunc_t
)cupsd_update_notifier
,
1548 if (cupsdOpenPipe(fds
))
1550 cupsdLogMessage(CUPSD_LOG_ERROR
,
1551 "Unable to create pipes for notifier %s - %s",
1552 scheme
, strerror(errno
));
1557 * Make sure the delivery pipe is non-blocking...
1560 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1563 * Create the notifier process...
1566 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1567 -1, -1, 0, DefaultProfile
, NULL
, &pid
) < 0)
1570 * Error - can't fork!
1573 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1574 scheme
, strerror(errno
));
1576 cupsdClosePipe(fds
);
1581 * Fork successful - return the PID...
1584 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1597 * 'cupsd_update_notifier()' - Read messages from notifiers.
1601 cupsd_update_notifier(void)
1603 char message
[1024]; /* Pointer to message text */
1604 int loglevel
; /* Log level for message */
1607 while (cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1608 message
, sizeof(message
)))
1610 if (loglevel
== CUPSD_LOG_INFO
)
1611 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
1613 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1620 * End of "$Id: subscriptions.c 11558 2014-02-06 18:33:34Z msweet $".