2 * "$Id: subscriptions.c 7824 2008-08-01 21:11:55Z mike $"
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();
152 * Add common event notification attributes...
155 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_CHARSET
,
156 "notify-charset", NULL
, "utf-8");
158 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_LANGUAGE
,
159 "notify-natural-language", NULL
, "en-US");
161 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
162 "notify-subscription-id", sub
->id
);
164 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
165 "notify-sequence-number", sub
->next_event_id
);
167 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
168 "notify-subscribed-event", NULL
, cupsdEventName(event
));
170 if (sub
->user_data_len
> 0)
171 ippAddOctetString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
172 "notify-user-data", sub
->user_data
,
175 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
176 "printer-up-time", time(NULL
));
179 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
182 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
183 "notify-text", NULL
, ftext
);
188 * Add printer attributes...
191 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
192 "notify-printer-uri", NULL
, dest
->uri
);
194 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
195 "printer-name", NULL
, dest
->name
);
197 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
198 "printer-state", dest
->state
);
200 if (dest
->num_reasons
== 0)
201 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
202 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
203 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
205 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
206 IPP_TAG_KEYWORD
, "printer-state-reasons",
207 dest
->num_reasons
, NULL
,
208 (const char * const *)dest
->reasons
);
210 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
211 "printer-is-accepting-jobs", dest
->accepting
);
217 * Add job attributes...
220 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
221 "notify-job-id", job
->id
);
222 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
223 "job-state", job
->state_value
);
225 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
226 IPP_TAG_NAME
)) != NULL
)
227 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
228 "job-name", NULL
, attr
->values
[0].string
.text
);
230 switch (job
->state_value
)
232 case IPP_JOB_PENDING
:
233 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
234 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
235 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
238 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
239 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
244 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
245 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
246 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
247 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
248 "job-hold-until-specified");
250 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
251 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
255 case IPP_JOB_PROCESSING
:
256 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
257 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
261 case IPP_JOB_STOPPED
:
262 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
263 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
267 case IPP_JOB_CANCELED
:
268 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
269 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
270 "job-canceled-by-user");
273 case IPP_JOB_ABORTED
:
274 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
275 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
276 "aborted-by-system");
279 case IPP_JOB_COMPLETED
:
280 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
281 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
282 "job-completed-successfully");
286 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
287 "job-impressions-completed",
288 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
292 * Send the notification for this subscription...
295 cupsd_send_notification(sub
, temp
);
300 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
302 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
303 cupsdEventName(event
));
308 * 'cupsdAddSubscription()' - Add a new subscription object.
311 cupsd_subscription_t
* /* O - New subscription object */
312 cupsdAddSubscription(
313 unsigned mask
, /* I - Event mask */
314 cupsd_printer_t
*dest
, /* I - Printer, if any */
315 cupsd_job_t
*job
, /* I - Job, if any */
316 const char *uri
, /* I - notify-recipient-uri, if any */
317 int sub_id
) /* I - notify-subscription-id or 0 */
319 cupsd_subscription_t
*temp
; /* New subscription object */
322 cupsdLogMessage(CUPSD_LOG_DEBUG
,
323 "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
325 mask
, dest
, dest
? dest
->name
: "", job
, job
? job
->id
: 0,
326 uri
? uri
: "(null)");
329 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
334 cupsdLogMessage(CUPSD_LOG_CRIT
,
335 "Unable to allocate memory for subscriptions - %s",
341 * Limit the number of subscriptions...
344 if (MaxSubscriptions
> 0 && cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
346 cupsdLogMessage(CUPSD_LOG_DEBUG
,
347 "cupsdAddSubscription: Reached MaxSubscriptions %d "
348 "(count=%d)", MaxSubscriptions
,
349 cupsArrayCount(Subscriptions
));
353 if (MaxSubscriptionsPerJob
> 0 && job
)
355 int count
; /* Number of job subscriptions */
357 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
360 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
361 if (temp
->job
== job
)
364 if (count
>= MaxSubscriptionsPerJob
)
366 cupsdLogMessage(CUPSD_LOG_DEBUG
,
367 "cupsdAddSubscription: Reached MaxSubscriptionsPerJob %d "
368 "for job #%d (count=%d)", MaxSubscriptionsPerJob
,
374 if (MaxSubscriptionsPerPrinter
> 0 && dest
)
376 int count
; /* Number of printer subscriptions */
378 for (temp
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
),
381 temp
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
382 if (temp
->dest
== dest
)
385 if (count
>= MaxSubscriptionsPerPrinter
)
387 cupsdLogMessage(CUPSD_LOG_DEBUG
,
388 "cupsdAddSubscription: Reached "
389 "MaxSubscriptionsPerPrinter %d for %s (count=%d)",
390 MaxSubscriptionsPerPrinter
, dest
->name
, count
);
396 * Allocate memory for this subscription...
399 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
401 cupsdLogMessage(CUPSD_LOG_CRIT
,
402 "Unable to allocate memory for subscription object - %s",
408 * Fill in common data...
415 if (sub_id
>= NextSubscriptionId
)
416 NextSubscriptionId
= sub_id
+ 1;
420 temp
->id
= NextSubscriptionId
;
422 NextSubscriptionId
++;
429 temp
->first_event_id
= 1;
430 temp
->next_event_id
= 1;
432 cupsdSetString(&(temp
->recipient
), uri
);
435 * Add the subscription to the array...
438 cupsArrayAdd(Subscriptions
, temp
);
441 * For RSS subscriptions, run the notifier immediately...
444 if (uri
&& !strncmp(uri
, "rss:", 4))
445 cupsd_start_notifier(temp
);
452 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
456 cupsdDeleteAllSubscriptions(void)
458 cupsd_subscription_t
*sub
; /* Subscription */
464 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
466 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
467 cupsdDeleteSubscription(sub
, 0);
469 cupsArrayDelete(Subscriptions
);
470 Subscriptions
= NULL
;
475 * 'cupsdDeleteSubscription()' - Delete a subscription object.
479 cupsdDeleteSubscription(
480 cupsd_subscription_t
*sub
, /* I - Subscription object */
481 int update
) /* I - 1 = update subscriptions.conf */
484 * Close the pipe to the notifier as needed...
491 * Remove subscription from array...
494 cupsArrayRemove(Subscriptions
, sub
);
500 cupsdClearString(&(sub
->owner
));
501 cupsdClearString(&(sub
->recipient
));
503 cupsArrayDelete(sub
->events
);
508 * Update the subscriptions as needed...
512 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
517 * 'cupsdEventName()' - Return a single event name.
520 const char * /* O - Event name */
522 cupsd_eventmask_t event
) /* I - Event value */
529 case CUPSD_EVENT_PRINTER_RESTARTED
:
530 return ("printer-restarted");
532 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
533 return ("printer-shutdown");
535 case CUPSD_EVENT_PRINTER_STOPPED
:
536 return ("printer-stopped");
538 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
539 return ("printer-finishings-changed");
541 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
542 return ("printer-media-changed");
544 case CUPSD_EVENT_PRINTER_ADDED
:
545 return ("printer-added");
547 case CUPSD_EVENT_PRINTER_DELETED
:
548 return ("printer-deleted");
550 case CUPSD_EVENT_PRINTER_MODIFIED
:
551 return ("printer-modified");
553 case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
:
554 return ("printer-queue-order-changed");
556 case CUPSD_EVENT_PRINTER_STATE
:
557 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
558 return ("printer-state-changed");
560 case CUPSD_EVENT_PRINTER_CONFIG
:
561 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
562 return ("printer-config-changed");
564 case CUPSD_EVENT_PRINTER_CHANGED
:
565 return ("printer-changed");
567 case CUPSD_EVENT_JOB_CREATED
:
568 return ("job-created");
570 case CUPSD_EVENT_JOB_COMPLETED
:
571 return ("job-completed");
573 case CUPSD_EVENT_JOB_STOPPED
:
574 return ("job-stopped");
576 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
577 return ("job-config-changed");
579 case CUPSD_EVENT_JOB_PROGRESS
:
580 return ("job-progress");
582 case CUPSD_EVENT_JOB_STATE
:
583 case CUPSD_EVENT_JOB_STATE_CHANGED
:
584 return ("job-state-changed");
586 case CUPSD_EVENT_SERVER_RESTARTED
:
587 return ("server-restarted");
589 case CUPSD_EVENT_SERVER_STARTED
:
590 return ("server-started");
592 case CUPSD_EVENT_SERVER_STOPPED
:
593 return ("server-stopped");
595 case CUPSD_EVENT_SERVER_AUDIT
:
596 return ("server-audit");
598 case CUPSD_EVENT_ALL
:
605 * 'cupsdEventValue()' - Return the event mask value for a name.
608 cupsd_eventmask_t
/* O - Event mask value */
609 cupsdEventValue(const char *name
) /* I - Name of event */
611 if (!strcmp(name
, "all"))
612 return (CUPSD_EVENT_ALL
);
613 else if (!strcmp(name
, "printer-restarted"))
614 return (CUPSD_EVENT_PRINTER_RESTARTED
);
615 else if (!strcmp(name
, "printer-shutdown"))
616 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
617 else if (!strcmp(name
, "printer-stopped"))
618 return (CUPSD_EVENT_PRINTER_STOPPED
);
619 else if (!strcmp(name
, "printer-finishings-changed"))
620 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
621 else if (!strcmp(name
, "printer-media-changed"))
622 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
623 else if (!strcmp(name
, "printer-added"))
624 return (CUPSD_EVENT_PRINTER_ADDED
);
625 else if (!strcmp(name
, "printer-deleted"))
626 return (CUPSD_EVENT_PRINTER_DELETED
);
627 else if (!strcmp(name
, "printer-modified"))
628 return (CUPSD_EVENT_PRINTER_MODIFIED
);
629 else if (!strcmp(name
, "printer-queue-order-changed"))
630 return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
);
631 else if (!strcmp(name
, "printer-state-changed"))
632 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
633 else if (!strcmp(name
, "printer-config-changed"))
634 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
635 else if (!strcmp(name
, "printer-changed"))
636 return (CUPSD_EVENT_PRINTER_CHANGED
);
637 else if (!strcmp(name
, "job-created"))
638 return (CUPSD_EVENT_JOB_CREATED
);
639 else if (!strcmp(name
, "job-completed"))
640 return (CUPSD_EVENT_JOB_COMPLETED
);
641 else if (!strcmp(name
, "job-stopped"))
642 return (CUPSD_EVENT_JOB_STOPPED
);
643 else if (!strcmp(name
, "job-config-changed"))
644 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
645 else if (!strcmp(name
, "job-progress"))
646 return (CUPSD_EVENT_JOB_PROGRESS
);
647 else if (!strcmp(name
, "job-state-changed"))
648 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
649 else if (!strcmp(name
, "server-restarted"))
650 return (CUPSD_EVENT_SERVER_RESTARTED
);
651 else if (!strcmp(name
, "server-started"))
652 return (CUPSD_EVENT_SERVER_STARTED
);
653 else if (!strcmp(name
, "server-stopped"))
654 return (CUPSD_EVENT_SERVER_STOPPED
);
655 else if (!strcmp(name
, "server-audit"))
656 return (CUPSD_EVENT_SERVER_AUDIT
);
658 return (CUPSD_EVENT_NONE
);
663 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
667 cupsdExpireSubscriptions(
668 cupsd_printer_t
*dest
, /* I - Printer, if any */
669 cupsd_job_t
*job
) /* I - Job, if any */
671 cupsd_subscription_t
*sub
; /* Current subscription */
672 int update
; /* Update subscriptions.conf? */
673 time_t curtime
; /* Current time */
676 curtime
= time(NULL
);
679 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
681 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
682 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
683 (dest
&& sub
->dest
== dest
) ||
684 (job
&& sub
->job
== job
))
686 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
689 cupsdDeleteSubscription(sub
, 0);
695 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
700 * 'cupsdFindSubscription()' - Find a subscription by ID.
703 cupsd_subscription_t
* /* O - Subscription object */
704 cupsdFindSubscription(int id
) /* I - Subscription ID */
706 cupsd_subscription_t sub
; /* Subscription template */
711 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
716 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
720 cupsdLoadAllSubscriptions(void)
722 int i
; /* Looping var */
723 cups_file_t
*fp
; /* subscriptions.conf file */
724 int linenum
; /* Current line number */
725 char line
[1024], /* Line from file */
726 *value
, /* Pointer to value */
727 *valueptr
; /* Pointer into value */
728 cupsd_subscription_t
*sub
; /* Current subscription */
729 int hex
; /* Non-zero if reading hex data */
730 int delete_sub
; /* Delete subscription? */
734 * Open the subscriptions.conf file...
737 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
738 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
741 cupsdLogMessage(CUPSD_LOG_ERROR
,
742 "LoadAllSubscriptions: Unable to open %s - %s", line
,
748 * Read all of the lines from the file...
755 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
757 if (!strcasecmp(line
, "NextSubscriptionId") && value
)
760 * NextSubscriptionId NNN
764 if (i
>= NextSubscriptionId
&& i
> 0)
765 NextSubscriptionId
= i
;
767 else if (!strcasecmp(line
, "<Subscription"))
773 if (!sub
&& value
&& isdigit(value
[0] & 255))
775 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
780 cupsdLogMessage(CUPSD_LOG_ERROR
,
781 "Syntax error on line %d of subscriptions.conf.",
786 else if (!strcasecmp(line
, "</Subscription>"))
790 cupsdLogMessage(CUPSD_LOG_ERROR
,
791 "Syntax error on line %d of subscriptions.conf.",
797 cupsdDeleteSubscription(sub
, 0);
804 cupsdLogMessage(CUPSD_LOG_ERROR
,
805 "Syntax error on line %d of subscriptions.conf.",
808 else if (!strcasecmp(line
, "Events"))
812 * Events name name name ...
817 cupsdLogMessage(CUPSD_LOG_ERROR
,
818 "Syntax error on line %d of subscriptions.conf.",
826 * Separate event names...
829 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
831 while (isspace(*valueptr
& 255))
835 * See if the name exists...
838 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
840 cupsdLogMessage(CUPSD_LOG_ERROR
,
841 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
849 else if (!strcasecmp(line
, "Owner"))
856 cupsdSetString(&sub
->owner
, value
);
859 cupsdLogMessage(CUPSD_LOG_ERROR
,
860 "Syntax error on line %d of subscriptions.conf.",
865 else if (!strcasecmp(line
, "Recipient"))
872 cupsdSetString(&sub
->recipient
, value
);
875 cupsdLogMessage(CUPSD_LOG_ERROR
,
876 "Syntax error on line %d of subscriptions.conf.",
881 else if (!strcasecmp(line
, "JobId"))
887 if (value
&& isdigit(*value
& 255))
889 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
891 cupsdLogMessage(CUPSD_LOG_ERROR
,
892 "Job %s not found on line %d of subscriptions.conf.",
899 cupsdLogMessage(CUPSD_LOG_ERROR
,
900 "Syntax error on line %d of subscriptions.conf.",
905 else if (!strcasecmp(line
, "PrinterName"))
913 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
915 cupsdLogMessage(CUPSD_LOG_ERROR
,
916 "Printer \'%s\' not found on line %d of subscriptions.conf.",
923 cupsdLogMessage(CUPSD_LOG_ERROR
,
924 "Syntax error on line %d of subscriptions.conf.",
929 else if (!strcasecmp(line
, "UserData"))
932 * UserData encoded-string
937 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
939 if (*valueptr
== '<' && !hex
)
947 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
949 if (isdigit(valueptr
[0]))
950 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
952 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
954 if (isdigit(valueptr
[1]))
955 sub
->user_data
[i
] |= valueptr
[1] - '0';
957 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
961 if (*valueptr
== '>')
971 sub
->user_data
[i
] = *valueptr
++;
976 cupsdLogMessage(CUPSD_LOG_ERROR
,
977 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
981 sub
->user_data_len
= i
;
985 cupsdLogMessage(CUPSD_LOG_ERROR
,
986 "Syntax error on line %d of subscriptions.conf.",
991 else if (!strcasecmp(line
, "LeaseDuration"))
997 if (value
&& isdigit(*value
& 255))
999 sub
->lease
= atoi(value
);
1000 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
1004 cupsdLogMessage(CUPSD_LOG_ERROR
,
1005 "Syntax error on line %d of subscriptions.conf.",
1010 else if (!strcasecmp(line
, "Interval"))
1016 if (value
&& isdigit(*value
& 255))
1017 sub
->interval
= atoi(value
);
1020 cupsdLogMessage(CUPSD_LOG_ERROR
,
1021 "Syntax error on line %d of subscriptions.conf.",
1026 else if (!strcasecmp(line
, "ExpirationTime"))
1032 if (value
&& isdigit(*value
& 255))
1033 sub
->expire
= atoi(value
);
1036 cupsdLogMessage(CUPSD_LOG_ERROR
,
1037 "Syntax error on line %d of subscriptions.conf.",
1042 else if (!strcasecmp(line
, "NextEventId"))
1048 if (value
&& isdigit(*value
& 255))
1049 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1052 cupsdLogMessage(CUPSD_LOG_ERROR
,
1053 "Syntax error on line %d of subscriptions.conf.",
1061 * Something else we don't understand...
1064 cupsdLogMessage(CUPSD_LOG_ERROR
,
1065 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1075 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1079 cupsdSaveAllSubscriptions(void)
1081 int i
; /* Looping var */
1082 cups_file_t
*fp
; /* subscriptions.conf file */
1083 char temp
[1024]; /* Temporary string */
1084 char backup
[1024]; /* subscriptions.conf.O file */
1085 cupsd_subscription_t
*sub
; /* Current subscription */
1086 time_t curtime
; /* Current time */
1087 struct tm
*curdate
; /* Current date */
1088 unsigned mask
; /* Current event mask */
1089 const char *name
; /* Current event name */
1090 int hex
; /* Non-zero if we are writing hex data */
1094 * Create the subscriptions.conf file...
1097 snprintf(temp
, sizeof(temp
), "%s/subscriptions.conf", ServerRoot
);
1098 snprintf(backup
, sizeof(backup
), "%s/subscriptions.conf.O", ServerRoot
);
1100 if (rename(temp
, backup
))
1102 if (errno
!= ENOENT
)
1103 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup subscriptions.conf - %s",
1107 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1109 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save subscriptions.conf - %s",
1112 if (rename(backup
, temp
))
1113 cupsdLogMessage(CUPSD_LOG_ERROR
,
1114 "Unable to restore subscriptions.conf - %s",
1119 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1122 * Restrict access to the file...
1125 fchown(cupsFileNumber(fp
), getuid(), Group
);
1126 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1129 * Write a small header to the file...
1132 curtime
= time(NULL
);
1133 curdate
= localtime(&curtime
);
1134 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1136 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1137 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1139 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1142 * Write every subscription known to the system...
1145 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1147 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1149 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1151 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1154 * Simple event list...
1157 cupsFilePrintf(fp
, "Events %s\n", name
);
1162 * Complex event list...
1165 cupsFilePuts(fp
, "Events");
1167 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1168 if (sub
->mask
& mask
)
1169 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1171 cupsFilePuts(fp
, "\n");
1175 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1177 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1179 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1181 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1183 if (sub
->user_data_len
> 0)
1185 cupsFilePuts(fp
, "UserData ");
1187 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1189 if (sub
->user_data
[i
] < ' ' ||
1190 sub
->user_data
[i
] > 0x7f ||
1191 sub
->user_data
[i
] == '<')
1195 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1199 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1205 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1209 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1214 cupsFilePuts(fp
, ">\n");
1216 cupsFilePutChar(fp
, '\n');
1219 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1220 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1221 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1222 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1224 cupsFilePuts(fp
, "</Subscription>\n");
1232 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1236 cupsdStopAllNotifiers(void)
1238 cupsd_subscription_t
*sub
; /* Current subscription */
1242 * See if we have started any notifiers...
1245 if (!NotifierStatusBuffer
)
1249 * Yes, kill any processes that are left...
1252 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1254 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1257 cupsdEndProcess(sub
->pid
, 0);
1264 * Close the status pipes...
1267 if (NotifierPipes
[0] >= 0)
1269 cupsdRemoveSelect(NotifierPipes
[0]);
1271 cupsdStatBufDelete(NotifierStatusBuffer
);
1273 close(NotifierPipes
[0]);
1274 close(NotifierPipes
[1]);
1276 NotifierPipes
[0] = -1;
1277 NotifierPipes
[1] = -1;
1278 NotifierStatusBuffer
= NULL
;
1284 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1287 static int /* O - Result of comparison */
1288 cupsd_compare_subscriptions(
1289 cupsd_subscription_t
*first
, /* I - First subscription object */
1290 cupsd_subscription_t
*second
, /* I - Second subscription object */
1291 void *unused
) /* I - Unused user data pointer */
1295 return (first
->id
- second
->id
);
1300 * 'cupsd_delete_event()' - Delete a single event...
1302 * Oldest events must be deleted first, otherwise the subscription cache
1303 * flushing code will not work properly.
1307 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1313 ippDelete(event
->attrs
);
1320 * 'cupsd_send_dbus()' - Send a DBUS notification...
1324 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1325 cupsd_printer_t
*dest
,/* I - Destination, if any */
1326 cupsd_job_t
*job
) /* I - Job, if any */
1328 DBusError error
; /* Error, if any */
1329 DBusMessage
*message
; /* Message to send */
1330 DBusMessageIter iter
; /* Iterator for message data */
1331 const char *what
; /* What to send */
1332 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1336 * Figure out what to send, if anything...
1339 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1340 what
= "PrinterAdded";
1341 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1342 what
= "PrinterRemoved";
1343 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1344 what
= "QueueChanged";
1345 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1346 what
= "JobQueuedLocal";
1347 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1348 job
->state_value
== IPP_JOB_PROCESSING
)
1349 what
= "JobStartedLocal";
1354 * Verify connection to DBUS server...
1357 if (con
&& !dbus_connection_get_is_connected(con
))
1359 dbus_connection_unref(con
);
1365 dbus_error_init(&error
);
1367 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1370 dbus_error_free(&error
);
1376 * Create and send the new message...
1379 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1380 "com.redhat.PrinterSpooler", what
);
1382 dbus_message_append_iter_init(message
, &iter
);
1384 dbus_message_iter_append_string(&iter
, dest
->name
);
1387 dbus_message_iter_append_uint32(&iter
, job
->id
);
1388 dbus_message_iter_append_string(&iter
, job
->username
);
1391 dbus_connection_send(con
, message
, NULL
);
1392 dbus_connection_flush(con
);
1393 dbus_message_unref(message
);
1395 #endif /* HAVE_DBUS */
1399 * 'cupsd_send_notification()' - Send a notification for the specified event.
1403 cupsd_send_notification(
1404 cupsd_subscription_t
*sub
, /* I - Subscription object */
1405 cupsd_event_t
*event
) /* I - Event to send */
1407 ipp_state_t state
; /* IPP event state */
1410 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1411 "cupsd_send_notification(sub=%p(%d), event=%p(%s))",
1412 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1415 * Allocate the events array as needed...
1420 sub
->events
= cupsArrayNew3((cups_array_func_t
)NULL
, NULL
,
1421 (cups_ahash_func_t
)NULL
, 0,
1422 (cups_acopy_func_t
)NULL
,
1423 (cups_afree_func_t
)cupsd_delete_event
);
1427 cupsdLogMessage(CUPSD_LOG_CRIT
,
1428 "Unable to allocate memory for subscription #%d!",
1435 * Purge an old event as needed...
1438 if (cupsArrayCount(sub
->events
) >= MaxEvents
)
1441 * Purge the oldest event in the cache...
1444 cupsArrayRemove(sub
->events
, cupsArrayFirst(sub
->events
));
1446 sub
->first_event_id
++;
1450 * Add the event to the subscription. Since the events array is
1451 * always MaxEvents in length, and since we will have already
1452 * removed an event from the subscription cache if we hit the
1453 * event cache limit, we don't need to check for overflow here...
1456 cupsArrayAdd(sub
->events
, event
);
1459 * Deliver the event...
1467 cupsd_start_notifier(sub
);
1469 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "sub->pipe=%d", sub
->pipe
);
1474 event
->attrs
->state
= IPP_IDLE
;
1476 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1477 if (state
== IPP_ERROR
)
1480 if (state
== IPP_ERROR
)
1485 * Notifier died, try restarting it...
1488 cupsdLogMessage(CUPSD_LOG_WARN
,
1489 "Notifier for subscription %d (%s) went away, "
1491 sub
->id
, sub
->recipient
);
1492 cupsdEndProcess(sub
->pid
, 0);
1499 cupsdLogMessage(CUPSD_LOG_ERROR
,
1500 "Unable to send event for subscription %d (%s)!",
1501 sub
->id
, sub
->recipient
);
1505 * If we get this far, break out of the loop...
1513 * Bump the event sequence number...
1516 sub
->next_event_id
++;
1521 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1525 cupsd_start_notifier(
1526 cupsd_subscription_t
*sub
) /* I - Subscription object */
1528 int pid
; /* Notifier process ID */
1529 int fds
[2]; /* Pipe file descriptors */
1530 char *argv
[4], /* Command-line arguments */
1531 *envp
[MAX_ENV
], /* Environment variables */
1532 user_data
[128], /* Base-64 encoded user data */
1533 scheme
[256], /* notify-recipient-uri scheme */
1534 *ptr
, /* Pointer into scheme */
1535 command
[1024]; /* Notifier command */
1539 * Extract the scheme name from the recipient URI and point to the
1540 * notifier program...
1543 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1544 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1547 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1550 * Base-64 encode the user data...
1553 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1554 sub
->user_data_len
);
1557 * Setup the argument array...
1561 argv
[1] = sub
->recipient
;
1562 argv
[2] = user_data
;
1566 * Setup the environment...
1569 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1572 * Create pipes as needed...
1575 if (!NotifierStatusBuffer
)
1578 * Create the status pipe...
1581 if (cupsdOpenPipe(NotifierPipes
))
1583 cupsdLogMessage(CUPSD_LOG_ERROR
,
1584 "Unable to create pipes for notifier status - %s",
1589 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1591 cupsdAddSelect(NotifierPipes
[0], (cupsd_selfunc_t
)cupsd_update_notifier
,
1595 if (cupsdOpenPipe(fds
))
1597 cupsdLogMessage(CUPSD_LOG_ERROR
,
1598 "Unable to create pipes for notifier %s - %s",
1599 scheme
, strerror(errno
));
1604 * Make sure the delivery pipe is non-blocking...
1607 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1610 * Create the notifier process...
1613 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1614 -1, -1, 0, DefaultProfile
, NULL
, &pid
) < 0)
1617 * Error - can't fork!
1620 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1621 scheme
, strerror(errno
));
1623 cupsdClosePipe(fds
);
1628 * Fork successful - return the PID...
1631 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1644 * 'cupsd_update_notifier()' - Read messages from notifiers.
1648 cupsd_update_notifier(void)
1650 char message
[1024]; /* Pointer to message text */
1651 int loglevel
; /* Log level for message */
1654 while (cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1655 message
, sizeof(message
)))
1657 if (loglevel
== CUPSD_LOG_INFO
)
1658 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
1660 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1667 * End of "$Id: subscriptions.c 7824 2008-08-01 21:11:55Z mike $".