4 * Subscription routines for the CUPS scheduler.
6 * Copyright 2007-2011 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 * cupsdAddEvent() - Add an event to the global event cache.
18 * cupsdAddSubscription() - Add a new subscription object.
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.
27 * cupsdStopAllNotifiers() - Stop all notifier processes.
28 * cupsd_compare_subscriptions() - Compare two subscriptions.
29 * cupsd_delete_event() - Delete a single event...
30 * cupsd_send_dbus() - Send a DBUS notification...
31 * cupsd_send_notification() - Send a notification for the specified
33 * cupsd_start_notifier() - Start a notifier subprocess...
34 * cupsd_update_notifier() - Read messages from notifiers.
38 * Include necessary headers...
43 # include <dbus/dbus.h>
44 # ifdef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND
45 # define dbus_message_append_iter_init dbus_message_iter_init_append
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))
48 # endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */
49 #endif /* HAVE_DBUS */
56 static int cupsd_compare_subscriptions(cupsd_subscription_t
*first
,
57 cupsd_subscription_t
*second
,
59 static void cupsd_delete_event(cupsd_event_t
*event
);
61 static void cupsd_send_dbus(cupsd_eventmask_t event
, cupsd_printer_t
*dest
,
63 #endif /* HAVE_DBUS */
64 static void cupsd_send_notification(cupsd_subscription_t
*sub
,
65 cupsd_event_t
*event
);
66 static void cupsd_start_notifier(cupsd_subscription_t
*sub
);
67 static void cupsd_update_notifier(void);
71 * 'cupsdAddEvent()' - Add an event to the global event cache.
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 */
82 va_list ap
; /* Pointer to additional arguments */
83 char ftext
[1024]; /* Formatted text buffer */
84 ipp_attribute_t
*attr
; /* Printer/job attribute */
85 cupsd_event_t
*temp
; /* New event pointer */
86 cupsd_subscription_t
*sub
; /* Current subscription */
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
);
95 * Keep track of events with any OS-supplied notification mechanisms...
101 cupsd_send_dbus(event
, dest
, job
);
102 #endif /* HAVE_DBUS */
105 * Return if we aren't keeping events...
110 cupsdLogMessage(CUPSD_LOG_WARN
,
111 "cupsdAddEvent: Discarding %s event since MaxEvents is %d!",
112 cupsdEventName(event
), MaxEvents
);
117 * Then loop through the subscriptions and add the event to the corresponding
121 for (temp
= NULL
, sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
123 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
126 * Check if this subscription requires this event...
129 if ((sub
->mask
& event
) != 0 &&
130 (sub
->dest
== dest
|| !sub
->dest
) &&
131 (sub
->job
== job
|| !sub
->job
))
134 * Need this event, so create a new event record...
137 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
139 cupsdLogMessage(CUPSD_LOG_CRIT
,
140 "Unable to allocate memory for event - %s",
146 temp
->time
= time(NULL
);
147 temp
->attrs
= ippNew();
153 temp
->dest
= dest
= cupsdFindPrinter(job
->dest
);
156 * Add common event notification attributes...
159 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_CHARSET
,
160 "notify-charset", NULL
, "utf-8");
162 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_LANGUAGE
,
163 "notify-natural-language", NULL
, "en-US");
165 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
166 "notify-subscription-id", sub
->id
);
168 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
169 "notify-sequence-number", sub
->next_event_id
);
171 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
172 "notify-subscribed-event", NULL
, cupsdEventName(event
));
174 if (sub
->user_data_len
> 0)
175 ippAddOctetString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
176 "notify-user-data", sub
->user_data
,
179 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
180 "printer-up-time", time(NULL
));
183 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
186 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
187 "notify-text", NULL
, ftext
);
192 * Add printer attributes...
195 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
196 "notify-printer-uri", NULL
, dest
->uri
);
198 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
199 "printer-name", NULL
, dest
->name
);
201 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
202 "printer-state", dest
->state
);
204 if (dest
->num_reasons
== 0)
205 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
206 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
207 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
209 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
210 IPP_TAG_KEYWORD
, "printer-state-reasons",
211 dest
->num_reasons
, NULL
,
212 (const char * const *)dest
->reasons
);
214 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
215 "printer-is-accepting-jobs", dest
->accepting
);
221 * Add job attributes...
224 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
225 "notify-job-id", job
->id
);
226 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
227 "job-state", job
->state_value
);
229 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
230 IPP_TAG_NAME
)) != NULL
)
231 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
232 "job-name", NULL
, attr
->values
[0].string
.text
);
234 switch (job
->state_value
)
236 case IPP_JOB_PENDING
:
237 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
238 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
239 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
242 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
243 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
248 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
249 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
250 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
251 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
252 "job-hold-until-specified");
254 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
255 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
259 case IPP_JOB_PROCESSING
:
260 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
261 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
265 case IPP_JOB_STOPPED
:
266 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
267 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
271 case IPP_JOB_CANCELED
:
272 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
273 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
274 "job-canceled-by-user");
277 case IPP_JOB_ABORTED
:
278 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
279 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
280 "aborted-by-system");
283 case IPP_JOB_COMPLETED
:
284 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
285 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
286 "job-completed-successfully");
290 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
291 "job-impressions-completed",
292 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
296 * Send the notification for this subscription...
299 cupsd_send_notification(sub
, temp
);
304 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
306 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
307 cupsdEventName(event
));
312 * 'cupsdAddSubscription()' - Add a new subscription object.
315 cupsd_subscription_t
* /* O - New subscription object */
316 cupsdAddSubscription(
317 unsigned mask
, /* I - Event mask */
318 cupsd_printer_t
*dest
, /* I - Printer, if any */
319 cupsd_job_t
*job
, /* I - Job, if any */
320 const char *uri
, /* I - notify-recipient-uri, if any */
321 int sub_id
) /* I - notify-subscription-id or 0 */
323 cupsd_subscription_t
*temp
; /* New subscription object */
326 cupsdLogMessage(CUPSD_LOG_DEBUG
,
327 "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
329 mask
, dest
, dest
? dest
->name
: "", job
, job
? job
->id
: 0,
330 uri
? uri
: "(null)");
333 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
338 cupsdLogMessage(CUPSD_LOG_CRIT
,
339 "Unable to allocate memory for subscriptions - %s",
345 * Limit the number of subscriptions...
348 if (MaxSubscriptions
> 0 && cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
350 cupsdLogMessage(CUPSD_LOG_DEBUG
,
351 "cupsdAddSubscription: Reached MaxSubscriptions %d "
352 "(count=%d)", MaxSubscriptions
,
353 cupsArrayCount(Subscriptions
));
357 if (MaxSubscriptionsPerJob
> 0 && job
)
359 int count
; /* Number of job subscriptions */
361 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
364 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
365 if (temp
->job
== job
)
368 if (count
>= MaxSubscriptionsPerJob
)
370 cupsdLogMessage(CUPSD_LOG_DEBUG
,
371 "cupsdAddSubscription: Reached MaxSubscriptionsPerJob %d "
372 "for job #%d (count=%d)", MaxSubscriptionsPerJob
,
378 if (MaxSubscriptionsPerPrinter
> 0 && dest
)
380 int count
; /* Number of printer subscriptions */
382 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
385 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
386 if (temp
->dest
== dest
)
389 if (count
>= MaxSubscriptionsPerPrinter
)
391 cupsdLogMessage(CUPSD_LOG_DEBUG
,
392 "cupsdAddSubscription: Reached "
393 "MaxSubscriptionsPerPrinter %d for %s (count=%d)",
394 MaxSubscriptionsPerPrinter
, dest
->name
, count
);
400 * Allocate memory for this subscription...
403 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
405 cupsdLogMessage(CUPSD_LOG_CRIT
,
406 "Unable to allocate memory for subscription object - %s",
412 * Fill in common data...
419 if (sub_id
>= NextSubscriptionId
)
420 NextSubscriptionId
= sub_id
+ 1;
424 temp
->id
= NextSubscriptionId
;
426 NextSubscriptionId
++;
433 temp
->first_event_id
= 1;
434 temp
->next_event_id
= 1;
436 cupsdSetString(&(temp
->recipient
), uri
);
439 * Add the subscription to the array...
442 cupsArrayAdd(Subscriptions
, temp
);
445 * For RSS subscriptions, run the notifier immediately...
448 if (uri
&& !strncmp(uri
, "rss:", 4))
449 cupsd_start_notifier(temp
);
456 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
460 cupsdDeleteAllSubscriptions(void)
462 cupsd_subscription_t
*sub
; /* Subscription */
468 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
470 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
471 cupsdDeleteSubscription(sub
, 0);
473 cupsArrayDelete(Subscriptions
);
474 Subscriptions
= NULL
;
479 * 'cupsdDeleteSubscription()' - Delete a subscription object.
483 cupsdDeleteSubscription(
484 cupsd_subscription_t
*sub
, /* I - Subscription object */
485 int update
) /* I - 1 = update subscriptions.conf */
488 * Close the pipe to the notifier as needed...
495 * Remove subscription from array...
498 cupsArrayRemove(Subscriptions
, sub
);
504 cupsdClearString(&(sub
->owner
));
505 cupsdClearString(&(sub
->recipient
));
507 cupsArrayDelete(sub
->events
);
512 * Update the subscriptions as needed...
516 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
521 * 'cupsdEventName()' - Return a single event name.
524 const char * /* O - Event name */
526 cupsd_eventmask_t event
) /* I - Event value */
533 case CUPSD_EVENT_PRINTER_RESTARTED
:
534 return ("printer-restarted");
536 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
537 return ("printer-shutdown");
539 case CUPSD_EVENT_PRINTER_STOPPED
:
540 return ("printer-stopped");
542 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
543 return ("printer-finishings-changed");
545 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
546 return ("printer-media-changed");
548 case CUPSD_EVENT_PRINTER_ADDED
:
549 return ("printer-added");
551 case CUPSD_EVENT_PRINTER_DELETED
:
552 return ("printer-deleted");
554 case CUPSD_EVENT_PRINTER_MODIFIED
:
555 return ("printer-modified");
557 case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
:
558 return ("printer-queue-order-changed");
560 case CUPSD_EVENT_PRINTER_STATE
:
561 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
562 return ("printer-state-changed");
564 case CUPSD_EVENT_PRINTER_CONFIG
:
565 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
566 return ("printer-config-changed");
568 case CUPSD_EVENT_PRINTER_CHANGED
:
569 return ("printer-changed");
571 case CUPSD_EVENT_JOB_CREATED
:
572 return ("job-created");
574 case CUPSD_EVENT_JOB_COMPLETED
:
575 return ("job-completed");
577 case CUPSD_EVENT_JOB_STOPPED
:
578 return ("job-stopped");
580 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
581 return ("job-config-changed");
583 case CUPSD_EVENT_JOB_PROGRESS
:
584 return ("job-progress");
586 case CUPSD_EVENT_JOB_STATE
:
587 case CUPSD_EVENT_JOB_STATE_CHANGED
:
588 return ("job-state-changed");
590 case CUPSD_EVENT_SERVER_RESTARTED
:
591 return ("server-restarted");
593 case CUPSD_EVENT_SERVER_STARTED
:
594 return ("server-started");
596 case CUPSD_EVENT_SERVER_STOPPED
:
597 return ("server-stopped");
599 case CUPSD_EVENT_SERVER_AUDIT
:
600 return ("server-audit");
602 case CUPSD_EVENT_ALL
:
609 * 'cupsdEventValue()' - Return the event mask value for a name.
612 cupsd_eventmask_t
/* O - Event mask value */
613 cupsdEventValue(const char *name
) /* I - Name of event */
615 if (!strcmp(name
, "all"))
616 return (CUPSD_EVENT_ALL
);
617 else if (!strcmp(name
, "printer-restarted"))
618 return (CUPSD_EVENT_PRINTER_RESTARTED
);
619 else if (!strcmp(name
, "printer-shutdown"))
620 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
621 else if (!strcmp(name
, "printer-stopped"))
622 return (CUPSD_EVENT_PRINTER_STOPPED
);
623 else if (!strcmp(name
, "printer-finishings-changed"))
624 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
625 else if (!strcmp(name
, "printer-media-changed"))
626 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
627 else if (!strcmp(name
, "printer-added"))
628 return (CUPSD_EVENT_PRINTER_ADDED
);
629 else if (!strcmp(name
, "printer-deleted"))
630 return (CUPSD_EVENT_PRINTER_DELETED
);
631 else if (!strcmp(name
, "printer-modified"))
632 return (CUPSD_EVENT_PRINTER_MODIFIED
);
633 else if (!strcmp(name
, "printer-queue-order-changed"))
634 return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
);
635 else if (!strcmp(name
, "printer-state-changed"))
636 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
637 else if (!strcmp(name
, "printer-config-changed"))
638 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
639 else if (!strcmp(name
, "printer-changed"))
640 return (CUPSD_EVENT_PRINTER_CHANGED
);
641 else if (!strcmp(name
, "job-created"))
642 return (CUPSD_EVENT_JOB_CREATED
);
643 else if (!strcmp(name
, "job-completed"))
644 return (CUPSD_EVENT_JOB_COMPLETED
);
645 else if (!strcmp(name
, "job-stopped"))
646 return (CUPSD_EVENT_JOB_STOPPED
);
647 else if (!strcmp(name
, "job-config-changed"))
648 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
649 else if (!strcmp(name
, "job-progress"))
650 return (CUPSD_EVENT_JOB_PROGRESS
);
651 else if (!strcmp(name
, "job-state-changed"))
652 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
653 else if (!strcmp(name
, "server-restarted"))
654 return (CUPSD_EVENT_SERVER_RESTARTED
);
655 else if (!strcmp(name
, "server-started"))
656 return (CUPSD_EVENT_SERVER_STARTED
);
657 else if (!strcmp(name
, "server-stopped"))
658 return (CUPSD_EVENT_SERVER_STOPPED
);
659 else if (!strcmp(name
, "server-audit"))
660 return (CUPSD_EVENT_SERVER_AUDIT
);
662 return (CUPSD_EVENT_NONE
);
667 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
671 cupsdExpireSubscriptions(
672 cupsd_printer_t
*dest
, /* I - Printer, if any */
673 cupsd_job_t
*job
) /* I - Job, if any */
675 cupsd_subscription_t
*sub
; /* Current subscription */
676 int update
; /* Update subscriptions.conf? */
677 time_t curtime
; /* Current time */
680 curtime
= time(NULL
);
683 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
685 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
686 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
687 (dest
&& sub
->dest
== dest
) ||
688 (job
&& sub
->job
== job
))
690 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
693 cupsdDeleteSubscription(sub
, 0);
699 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
704 * 'cupsdFindSubscription()' - Find a subscription by ID.
707 cupsd_subscription_t
* /* O - Subscription object */
708 cupsdFindSubscription(int id
) /* I - Subscription ID */
710 cupsd_subscription_t sub
; /* Subscription template */
715 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
720 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
724 cupsdLoadAllSubscriptions(void)
726 int i
; /* Looping var */
727 cups_file_t
*fp
; /* subscriptions.conf file */
728 int linenum
; /* Current line number */
729 char line
[1024], /* Line from file */
730 *value
, /* Pointer to value */
731 *valueptr
; /* Pointer into value */
732 cupsd_subscription_t
*sub
; /* Current subscription */
733 int hex
; /* Non-zero if reading hex data */
734 int delete_sub
; /* Delete subscription? */
738 * Open the subscriptions.conf file...
741 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
742 if ((fp
= cupsdOpenConfFile(line
)) == NULL
)
746 * Read all of the lines from the file...
753 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
755 if (!_cups_strcasecmp(line
, "NextSubscriptionId") && value
)
758 * NextSubscriptionId NNN
762 if (i
>= NextSubscriptionId
&& i
> 0)
763 NextSubscriptionId
= i
;
765 else if (!_cups_strcasecmp(line
, "<Subscription"))
771 if (!sub
&& value
&& isdigit(value
[0] & 255))
773 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
778 cupsdLogMessage(CUPSD_LOG_ERROR
,
779 "Syntax error on line %d of subscriptions.conf.",
784 else if (!_cups_strcasecmp(line
, "</Subscription>"))
788 cupsdLogMessage(CUPSD_LOG_ERROR
,
789 "Syntax error on line %d of subscriptions.conf.",
795 cupsdDeleteSubscription(sub
, 0);
802 cupsdLogMessage(CUPSD_LOG_ERROR
,
803 "Syntax error on line %d of subscriptions.conf.",
806 else if (!_cups_strcasecmp(line
, "Events"))
810 * Events name name name ...
815 cupsdLogMessage(CUPSD_LOG_ERROR
,
816 "Syntax error on line %d of subscriptions.conf.",
824 * Separate event names...
827 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
829 while (isspace(*valueptr
& 255))
833 * See if the name exists...
836 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
838 cupsdLogMessage(CUPSD_LOG_ERROR
,
839 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
847 else if (!_cups_strcasecmp(line
, "Owner"))
854 cupsdSetString(&sub
->owner
, value
);
857 cupsdLogMessage(CUPSD_LOG_ERROR
,
858 "Syntax error on line %d of subscriptions.conf.",
863 else if (!_cups_strcasecmp(line
, "Recipient"))
870 cupsdSetString(&sub
->recipient
, value
);
873 cupsdLogMessage(CUPSD_LOG_ERROR
,
874 "Syntax error on line %d of subscriptions.conf.",
879 else if (!_cups_strcasecmp(line
, "JobId"))
885 if (value
&& isdigit(*value
& 255))
887 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
889 cupsdLogMessage(CUPSD_LOG_ERROR
,
890 "Job %s not found on line %d of subscriptions.conf.",
897 cupsdLogMessage(CUPSD_LOG_ERROR
,
898 "Syntax error on line %d of subscriptions.conf.",
903 else if (!_cups_strcasecmp(line
, "PrinterName"))
911 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
913 cupsdLogMessage(CUPSD_LOG_ERROR
,
914 "Printer \'%s\' not found on line %d of subscriptions.conf.",
921 cupsdLogMessage(CUPSD_LOG_ERROR
,
922 "Syntax error on line %d of subscriptions.conf.",
927 else if (!_cups_strcasecmp(line
, "UserData"))
930 * UserData encoded-string
935 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
937 if (*valueptr
== '<' && !hex
)
945 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
947 if (isdigit(valueptr
[0]))
948 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
950 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
952 if (isdigit(valueptr
[1]))
953 sub
->user_data
[i
] |= valueptr
[1] - '0';
955 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
959 if (*valueptr
== '>')
969 sub
->user_data
[i
] = *valueptr
++;
974 cupsdLogMessage(CUPSD_LOG_ERROR
,
975 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
979 sub
->user_data_len
= i
;
983 cupsdLogMessage(CUPSD_LOG_ERROR
,
984 "Syntax error on line %d of subscriptions.conf.",
989 else if (!_cups_strcasecmp(line
, "LeaseDuration"))
995 if (value
&& isdigit(*value
& 255))
997 sub
->lease
= atoi(value
);
998 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
1002 cupsdLogMessage(CUPSD_LOG_ERROR
,
1003 "Syntax error on line %d of subscriptions.conf.",
1008 else if (!_cups_strcasecmp(line
, "Interval"))
1014 if (value
&& isdigit(*value
& 255))
1015 sub
->interval
= atoi(value
);
1018 cupsdLogMessage(CUPSD_LOG_ERROR
,
1019 "Syntax error on line %d of subscriptions.conf.",
1024 else if (!_cups_strcasecmp(line
, "ExpirationTime"))
1030 if (value
&& isdigit(*value
& 255))
1031 sub
->expire
= atoi(value
);
1034 cupsdLogMessage(CUPSD_LOG_ERROR
,
1035 "Syntax error on line %d of subscriptions.conf.",
1040 else if (!_cups_strcasecmp(line
, "NextEventId"))
1046 if (value
&& isdigit(*value
& 255))
1047 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1050 cupsdLogMessage(CUPSD_LOG_ERROR
,
1051 "Syntax error on line %d of subscriptions.conf.",
1059 * Something else we don't understand...
1062 cupsdLogMessage(CUPSD_LOG_ERROR
,
1063 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1073 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1077 cupsdSaveAllSubscriptions(void)
1079 int i
; /* Looping var */
1080 cups_file_t
*fp
; /* subscriptions.conf file */
1081 char filename
[1024], /* subscriptions.conf filename */
1082 temp
[1024]; /* Temporary string */
1083 cupsd_subscription_t
*sub
; /* Current subscription */
1084 time_t curtime
; /* Current time */
1085 struct tm
*curdate
; /* Current date */
1086 unsigned mask
; /* Current event mask */
1087 const char *name
; /* Current event name */
1088 int hex
; /* Non-zero if we are writing hex data */
1092 * Create the subscriptions.conf file...
1095 snprintf(filename
, sizeof(filename
), "%s/subscriptions.conf", ServerRoot
);
1097 if ((fp
= cupsdCreateConfFile(filename
, ConfigFilePerm
)) == NULL
)
1100 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1103 * Write a small header to the file...
1106 curtime
= time(NULL
);
1107 curdate
= localtime(&curtime
);
1108 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1110 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1111 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1113 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1116 * Write every subscription known to the system...
1119 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1121 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1123 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1125 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1128 * Simple event list...
1131 cupsFilePrintf(fp
, "Events %s\n", name
);
1136 * Complex event list...
1139 cupsFilePuts(fp
, "Events");
1141 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1142 if (sub
->mask
& mask
)
1143 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1145 cupsFilePuts(fp
, "\n");
1149 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1151 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1153 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1155 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1157 if (sub
->user_data_len
> 0)
1159 cupsFilePuts(fp
, "UserData ");
1161 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1163 if (sub
->user_data
[i
] < ' ' ||
1164 sub
->user_data
[i
] > 0x7f ||
1165 sub
->user_data
[i
] == '<')
1169 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1173 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1179 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1183 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1188 cupsFilePuts(fp
, ">\n");
1190 cupsFilePutChar(fp
, '\n');
1193 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1194 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1195 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1196 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1198 cupsFilePuts(fp
, "</Subscription>\n");
1201 cupsdCloseCreatedConfFile(fp
, filename
);
1206 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1210 cupsdStopAllNotifiers(void)
1212 cupsd_subscription_t
*sub
; /* Current subscription */
1216 * See if we have started any notifiers...
1219 if (!NotifierStatusBuffer
)
1223 * Yes, kill any processes that are left...
1226 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1228 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1231 cupsdEndProcess(sub
->pid
, 0);
1238 * Close the status pipes...
1241 if (NotifierPipes
[0] >= 0)
1243 cupsdRemoveSelect(NotifierPipes
[0]);
1245 cupsdStatBufDelete(NotifierStatusBuffer
);
1247 close(NotifierPipes
[0]);
1248 close(NotifierPipes
[1]);
1250 NotifierPipes
[0] = -1;
1251 NotifierPipes
[1] = -1;
1252 NotifierStatusBuffer
= NULL
;
1258 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1261 static int /* O - Result of comparison */
1262 cupsd_compare_subscriptions(
1263 cupsd_subscription_t
*first
, /* I - First subscription object */
1264 cupsd_subscription_t
*second
, /* I - Second subscription object */
1265 void *unused
) /* I - Unused user data pointer */
1269 return (first
->id
- second
->id
);
1274 * 'cupsd_delete_event()' - Delete a single event...
1276 * Oldest events must be deleted first, otherwise the subscription cache
1277 * flushing code will not work properly.
1281 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1287 ippDelete(event
->attrs
);
1294 * 'cupsd_send_dbus()' - Send a DBUS notification...
1298 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1299 cupsd_printer_t
*dest
,/* I - Destination, if any */
1300 cupsd_job_t
*job
) /* I - Job, if any */
1302 DBusError error
; /* Error, if any */
1303 DBusMessage
*message
; /* Message to send */
1304 DBusMessageIter iter
; /* Iterator for message data */
1305 const char *what
; /* What to send */
1306 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1310 * Figure out what to send, if anything...
1313 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1314 what
= "PrinterAdded";
1315 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1316 what
= "PrinterRemoved";
1317 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1318 what
= "QueueChanged";
1319 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1320 what
= "JobQueuedLocal";
1321 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1322 job
->state_value
== IPP_JOB_PROCESSING
)
1323 what
= "JobStartedLocal";
1328 * Verify connection to DBUS server...
1331 if (con
&& !dbus_connection_get_is_connected(con
))
1333 dbus_connection_unref(con
);
1339 dbus_error_init(&error
);
1341 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1344 dbus_error_free(&error
);
1350 * Create and send the new message...
1353 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1354 "com.redhat.PrinterSpooler", what
);
1356 dbus_message_append_iter_init(message
, &iter
);
1358 dbus_message_iter_append_string(&iter
, dest
->name
);
1361 dbus_message_iter_append_uint32(&iter
, job
->id
);
1362 dbus_message_iter_append_string(&iter
, job
->username
);
1365 dbus_connection_send(con
, message
, NULL
);
1366 dbus_connection_flush(con
);
1367 dbus_message_unref(message
);
1369 #endif /* HAVE_DBUS */
1373 * 'cupsd_send_notification()' - Send a notification for the specified event.
1377 cupsd_send_notification(
1378 cupsd_subscription_t
*sub
, /* I - Subscription object */
1379 cupsd_event_t
*event
) /* I - Event to send */
1381 ipp_state_t state
; /* IPP event state */
1384 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1385 "cupsd_send_notification(sub=%p(%d), event=%p(%s))",
1386 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1389 * Allocate the events array as needed...
1394 sub
->events
= cupsArrayNew3((cups_array_func_t
)NULL
, NULL
,
1395 (cups_ahash_func_t
)NULL
, 0,
1396 (cups_acopy_func_t
)NULL
,
1397 (cups_afree_func_t
)cupsd_delete_event
);
1401 cupsdLogMessage(CUPSD_LOG_CRIT
,
1402 "Unable to allocate memory for subscription #%d!",
1409 * Purge an old event as needed...
1412 if (cupsArrayCount(sub
->events
) >= MaxEvents
)
1415 * Purge the oldest event in the cache...
1418 cupsArrayRemove(sub
->events
, cupsArrayFirst(sub
->events
));
1420 sub
->first_event_id
++;
1424 * Add the event to the subscription. Since the events array is
1425 * always MaxEvents in length, and since we will have already
1426 * removed an event from the subscription cache if we hit the
1427 * event cache limit, we don't need to check for overflow here...
1430 cupsArrayAdd(sub
->events
, event
);
1433 * Deliver the event...
1441 cupsd_start_notifier(sub
);
1443 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "sub->pipe=%d", sub
->pipe
);
1448 event
->attrs
->state
= IPP_IDLE
;
1450 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1451 if (state
== IPP_ERROR
)
1454 if (state
== IPP_ERROR
)
1459 * Notifier died, try restarting it...
1462 cupsdLogMessage(CUPSD_LOG_WARN
,
1463 "Notifier for subscription %d (%s) went away, "
1465 sub
->id
, sub
->recipient
);
1466 cupsdEndProcess(sub
->pid
, 0);
1473 cupsdLogMessage(CUPSD_LOG_ERROR
,
1474 "Unable to send event for subscription %d (%s)!",
1475 sub
->id
, sub
->recipient
);
1479 * If we get this far, break out of the loop...
1487 * Bump the event sequence number...
1490 sub
->next_event_id
++;
1495 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1499 cupsd_start_notifier(
1500 cupsd_subscription_t
*sub
) /* I - Subscription object */
1502 int pid
; /* Notifier process ID */
1503 int fds
[2]; /* Pipe file descriptors */
1504 char *argv
[4], /* Command-line arguments */
1505 *envp
[MAX_ENV
], /* Environment variables */
1506 user_data
[128], /* Base-64 encoded user data */
1507 scheme
[256], /* notify-recipient-uri scheme */
1508 *ptr
, /* Pointer into scheme */
1509 command
[1024]; /* Notifier command */
1513 * Extract the scheme name from the recipient URI and point to the
1514 * notifier program...
1517 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1518 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1521 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1524 * Base-64 encode the user data...
1527 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1528 sub
->user_data_len
);
1531 * Setup the argument array...
1535 argv
[1] = sub
->recipient
;
1536 argv
[2] = user_data
;
1540 * Setup the environment...
1543 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1546 * Create pipes as needed...
1549 if (!NotifierStatusBuffer
)
1552 * Create the status pipe...
1555 if (cupsdOpenPipe(NotifierPipes
))
1557 cupsdLogMessage(CUPSD_LOG_ERROR
,
1558 "Unable to create pipes for notifier status - %s",
1563 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1565 cupsdAddSelect(NotifierPipes
[0], (cupsd_selfunc_t
)cupsd_update_notifier
,
1569 if (cupsdOpenPipe(fds
))
1571 cupsdLogMessage(CUPSD_LOG_ERROR
,
1572 "Unable to create pipes for notifier %s - %s",
1573 scheme
, strerror(errno
));
1578 * Make sure the delivery pipe is non-blocking...
1581 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1584 * Create the notifier process...
1587 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1588 -1, -1, 0, DefaultProfile
, NULL
, &pid
) < 0)
1591 * Error - can't fork!
1594 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1595 scheme
, strerror(errno
));
1597 cupsdClosePipe(fds
);
1602 * Fork successful - return the PID...
1605 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1618 * 'cupsd_update_notifier()' - Read messages from notifiers.
1622 cupsd_update_notifier(void)
1624 char message
[1024]; /* Pointer to message text */
1625 int loglevel
; /* Log level for message */
1628 while (cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1629 message
, sizeof(message
)))
1631 if (loglevel
== CUPSD_LOG_INFO
)
1632 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
1634 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))