2 * "$Id: subscriptions.c 6649 2007-07-11 21:46:42Z mike $"
4 * Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 2007-2008 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-langugage", 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 (cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
348 * Allocate memory for this subscription...
351 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
353 cupsdLogMessage(CUPSD_LOG_CRIT
,
354 "Unable to allocate memory for subscription object - %s",
360 * Fill in common data...
367 if (sub_id
>= NextSubscriptionId
)
368 NextSubscriptionId
= sub_id
+ 1;
372 temp
->id
= NextSubscriptionId
;
374 NextSubscriptionId
++;
381 temp
->first_event_id
= 1;
382 temp
->next_event_id
= 1;
384 cupsdSetString(&(temp
->recipient
), uri
);
387 * Add the subscription to the array...
390 cupsArrayAdd(Subscriptions
, temp
);
397 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
401 cupsdDeleteAllSubscriptions(void)
403 cupsd_subscription_t
*sub
; /* Subscription */
409 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
411 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
412 cupsdDeleteSubscription(sub
, 0);
414 cupsArrayDelete(Subscriptions
);
415 Subscriptions
= NULL
;
420 * 'cupsdDeleteSubscription()' - Delete a subscription object.
424 cupsdDeleteSubscription(
425 cupsd_subscription_t
*sub
, /* I - Subscription object */
426 int update
) /* I - 1 = update subscriptions.conf */
428 int i
; /* Looping var */
432 * Close the pipe to the notifier as needed...
439 * Remove subscription from array...
442 cupsArrayRemove(Subscriptions
, sub
);
448 cupsdClearString(&(sub
->owner
));
449 cupsdClearString(&(sub
->recipient
));
453 for (i
= 0; i
< sub
->num_events
; i
++)
454 cupsd_delete_event(sub
->events
[i
]);
462 * Update the subscriptions as needed...
466 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
471 * 'cupsdEventName()' - Return a single event name.
474 const char * /* O - Event name */
476 cupsd_eventmask_t event
) /* I - Event value */
483 case CUPSD_EVENT_PRINTER_RESTARTED
:
484 return ("printer-restarted");
486 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
487 return ("printer-shutdown");
489 case CUPSD_EVENT_PRINTER_STOPPED
:
490 return ("printer-stopped");
492 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
493 return ("printer-finishings-changed");
495 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
496 return ("printer-media-changed");
498 case CUPSD_EVENT_PRINTER_ADDED
:
499 return ("printer-added");
501 case CUPSD_EVENT_PRINTER_DELETED
:
502 return ("printer-deleted");
504 case CUPSD_EVENT_PRINTER_MODIFIED
:
505 return ("printer-modified");
507 case CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
:
508 return ("printer-queue-order-changed");
510 case CUPSD_EVENT_PRINTER_STATE
:
511 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
512 return ("printer-state-changed");
514 case CUPSD_EVENT_PRINTER_CONFIG
:
515 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
516 return ("printer-config-changed");
518 case CUPSD_EVENT_PRINTER_CHANGED
:
519 return ("printer-changed");
521 case CUPSD_EVENT_JOB_CREATED
:
522 return ("job-created");
524 case CUPSD_EVENT_JOB_COMPLETED
:
525 return ("job-completed");
527 case CUPSD_EVENT_JOB_STOPPED
:
528 return ("job-stopped");
530 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
531 return ("job-config-changed");
533 case CUPSD_EVENT_JOB_PROGRESS
:
534 return ("job-progress");
536 case CUPSD_EVENT_JOB_STATE
:
537 case CUPSD_EVENT_JOB_STATE_CHANGED
:
538 return ("job-state-changed");
540 case CUPSD_EVENT_SERVER_RESTARTED
:
541 return ("server-restarted");
543 case CUPSD_EVENT_SERVER_STARTED
:
544 return ("server-started");
546 case CUPSD_EVENT_SERVER_STOPPED
:
547 return ("server-stopped");
549 case CUPSD_EVENT_SERVER_AUDIT
:
550 return ("server-audit");
552 case CUPSD_EVENT_ALL
:
559 * 'cupsdEventValue()' - Return the event mask value for a name.
562 cupsd_eventmask_t
/* O - Event mask value */
563 cupsdEventValue(const char *name
) /* I - Name of event */
565 if (!strcmp(name
, "all"))
566 return (CUPSD_EVENT_ALL
);
567 else if (!strcmp(name
, "printer-restarted"))
568 return (CUPSD_EVENT_PRINTER_RESTARTED
);
569 else if (!strcmp(name
, "printer-shutdown"))
570 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
571 else if (!strcmp(name
, "printer-stopped"))
572 return (CUPSD_EVENT_PRINTER_STOPPED
);
573 else if (!strcmp(name
, "printer-finishings-changed"))
574 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
575 else if (!strcmp(name
, "printer-media-changed"))
576 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
577 else if (!strcmp(name
, "printer-added"))
578 return (CUPSD_EVENT_PRINTER_ADDED
);
579 else if (!strcmp(name
, "printer-deleted"))
580 return (CUPSD_EVENT_PRINTER_DELETED
);
581 else if (!strcmp(name
, "printer-modified"))
582 return (CUPSD_EVENT_PRINTER_MODIFIED
);
583 else if (!strcmp(name
, "printer-queue-order-changed"))
584 return (CUPSD_EVENT_PRINTER_QUEUE_ORDER_CHANGED
);
585 else if (!strcmp(name
, "printer-state-changed"))
586 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
587 else if (!strcmp(name
, "printer-config-changed"))
588 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
589 else if (!strcmp(name
, "printer-changed"))
590 return (CUPSD_EVENT_PRINTER_CHANGED
);
591 else if (!strcmp(name
, "job-created"))
592 return (CUPSD_EVENT_JOB_CREATED
);
593 else if (!strcmp(name
, "job-completed"))
594 return (CUPSD_EVENT_JOB_COMPLETED
);
595 else if (!strcmp(name
, "job-stopped"))
596 return (CUPSD_EVENT_JOB_STOPPED
);
597 else if (!strcmp(name
, "job-config-changed"))
598 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
599 else if (!strcmp(name
, "job-progress"))
600 return (CUPSD_EVENT_JOB_PROGRESS
);
601 else if (!strcmp(name
, "job-state-changed"))
602 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
603 else if (!strcmp(name
, "server-restarted"))
604 return (CUPSD_EVENT_SERVER_RESTARTED
);
605 else if (!strcmp(name
, "server-started"))
606 return (CUPSD_EVENT_SERVER_STARTED
);
607 else if (!strcmp(name
, "server-stopped"))
608 return (CUPSD_EVENT_SERVER_STOPPED
);
609 else if (!strcmp(name
, "server-audit"))
610 return (CUPSD_EVENT_SERVER_AUDIT
);
612 return (CUPSD_EVENT_NONE
);
617 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
621 cupsdExpireSubscriptions(
622 cupsd_printer_t
*dest
, /* I - Printer, if any */
623 cupsd_job_t
*job
) /* I - Job, if any */
625 cupsd_subscription_t
*sub
; /* Current subscription */
626 int update
; /* Update subscriptions.conf? */
627 time_t curtime
; /* Current time */
630 curtime
= time(NULL
);
633 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
635 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
636 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
637 (dest
&& sub
->dest
== dest
) ||
638 (job
&& sub
->job
== job
))
640 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
643 cupsdDeleteSubscription(sub
, 0);
649 cupsdMarkDirty(CUPSD_DIRTY_SUBSCRIPTIONS
);
654 * 'cupsdFindSubscription()' - Find a subscription by ID.
657 cupsd_subscription_t
* /* O - Subscription object */
658 cupsdFindSubscription(int id
) /* I - Subscription ID */
660 cupsd_subscription_t sub
; /* Subscription template */
665 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
670 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
674 cupsdLoadAllSubscriptions(void)
676 int i
; /* Looping var */
677 cups_file_t
*fp
; /* subscriptions.conf file */
678 int linenum
; /* Current line number */
679 char line
[1024], /* Line from file */
680 *value
, /* Pointer to value */
681 *valueptr
; /* Pointer into value */
682 cupsd_subscription_t
*sub
; /* Current subscription */
683 int hex
; /* Non-zero if reading hex data */
684 int delete_sub
; /* Delete subscription? */
688 * Open the subscriptions.conf file...
691 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
692 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
695 cupsdLogMessage(CUPSD_LOG_ERROR
,
696 "LoadAllSubscriptions: Unable to open %s - %s", line
,
702 * Read all of the lines from the file...
709 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
711 if (!strcasecmp(line
, "NextSubscriptionId") && value
)
714 * NextSubscriptionId NNN
718 if (i
>= NextSubscriptionId
&& i
> 0)
719 NextSubscriptionId
= i
;
721 else if (!strcasecmp(line
, "<Subscription"))
727 if (!sub
&& value
&& isdigit(value
[0] & 255))
729 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
734 cupsdLogMessage(CUPSD_LOG_ERROR
,
735 "Syntax error on line %d of subscriptions.conf.",
740 else if (!strcasecmp(line
, "</Subscription>"))
744 cupsdLogMessage(CUPSD_LOG_ERROR
,
745 "Syntax error on line %d of subscriptions.conf.",
751 cupsdDeleteSubscription(sub
, 0);
758 cupsdLogMessage(CUPSD_LOG_ERROR
,
759 "Syntax error on line %d of subscriptions.conf.",
763 else if (!strcasecmp(line
, "Events"))
767 * Events name name name ...
772 cupsdLogMessage(CUPSD_LOG_ERROR
,
773 "Syntax error on line %d of subscriptions.conf.",
781 * Separate event names...
784 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
786 while (isspace(*valueptr
& 255))
790 * See if the name exists...
793 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
795 cupsdLogMessage(CUPSD_LOG_ERROR
,
796 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
804 else if (!strcasecmp(line
, "Owner"))
811 cupsdSetString(&sub
->owner
, value
);
814 cupsdLogMessage(CUPSD_LOG_ERROR
,
815 "Syntax error on line %d of subscriptions.conf.",
820 else if (!strcasecmp(line
, "Recipient"))
827 cupsdSetString(&sub
->recipient
, value
);
830 cupsdLogMessage(CUPSD_LOG_ERROR
,
831 "Syntax error on line %d of subscriptions.conf.",
836 else if (!strcasecmp(line
, "JobId"))
842 if (value
&& isdigit(*value
& 255))
844 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
846 cupsdLogMessage(CUPSD_LOG_ERROR
,
847 "Job %s not found on line %d of subscriptions.conf.",
854 cupsdLogMessage(CUPSD_LOG_ERROR
,
855 "Syntax error on line %d of subscriptions.conf.",
860 else if (!strcasecmp(line
, "PrinterName"))
868 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
870 cupsdLogMessage(CUPSD_LOG_ERROR
,
871 "Printer \'%s\' not found on line %d of subscriptions.conf.",
878 cupsdLogMessage(CUPSD_LOG_ERROR
,
879 "Syntax error on line %d of subscriptions.conf.",
884 else if (!strcasecmp(line
, "UserData"))
887 * UserData encoded-string
892 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
894 if (*valueptr
== '<' && !hex
)
902 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
904 if (isdigit(valueptr
[0]))
905 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
907 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
909 if (isdigit(valueptr
[1]))
910 sub
->user_data
[i
] |= valueptr
[1] - '0';
912 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
916 if (*valueptr
== '>')
926 sub
->user_data
[i
] = *valueptr
++;
931 cupsdLogMessage(CUPSD_LOG_ERROR
,
932 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
936 sub
->user_data_len
= i
;
940 cupsdLogMessage(CUPSD_LOG_ERROR
,
941 "Syntax error on line %d of subscriptions.conf.",
946 else if (!strcasecmp(line
, "LeaseDuration"))
952 if (value
&& isdigit(*value
& 255))
954 sub
->lease
= atoi(value
);
955 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
959 cupsdLogMessage(CUPSD_LOG_ERROR
,
960 "Syntax error on line %d of subscriptions.conf.",
965 else if (!strcasecmp(line
, "Interval"))
971 if (value
&& isdigit(*value
& 255))
972 sub
->interval
= atoi(value
);
975 cupsdLogMessage(CUPSD_LOG_ERROR
,
976 "Syntax error on line %d of subscriptions.conf.",
981 else if (!strcasecmp(line
, "ExpirationTime"))
987 if (value
&& isdigit(*value
& 255))
988 sub
->expire
= atoi(value
);
991 cupsdLogMessage(CUPSD_LOG_ERROR
,
992 "Syntax error on line %d of subscriptions.conf.",
997 else if (!strcasecmp(line
, "NextEventId"))
1003 if (value
&& isdigit(*value
& 255))
1004 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1007 cupsdLogMessage(CUPSD_LOG_ERROR
,
1008 "Syntax error on line %d of subscriptions.conf.",
1016 * Something else we don't understand...
1019 cupsdLogMessage(CUPSD_LOG_ERROR
,
1020 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1030 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1034 cupsdSaveAllSubscriptions(void)
1036 int i
; /* Looping var */
1037 cups_file_t
*fp
; /* subscriptions.conf file */
1038 char temp
[1024]; /* Temporary string */
1039 char backup
[1024]; /* subscriptions.conf.O file */
1040 cupsd_subscription_t
*sub
; /* Current subscription */
1041 time_t curtime
; /* Current time */
1042 struct tm
*curdate
; /* Current date */
1043 unsigned mask
; /* Current event mask */
1044 const char *name
; /* Current event name */
1045 int hex
; /* Non-zero if we are writing hex data */
1049 * Create the subscriptions.conf file...
1052 snprintf(temp
, sizeof(temp
), "%s/subscriptions.conf", ServerRoot
);
1053 snprintf(backup
, sizeof(backup
), "%s/subscriptions.conf.O", ServerRoot
);
1055 if (rename(temp
, backup
))
1057 if (errno
!= ENOENT
)
1058 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup subscriptions.conf - %s",
1062 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1064 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save subscriptions.conf - %s",
1067 if (rename(backup
, temp
))
1068 cupsdLogMessage(CUPSD_LOG_ERROR
,
1069 "Unable to restore subscriptions.conf - %s",
1074 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1077 * Restrict access to the file...
1080 fchown(cupsFileNumber(fp
), getuid(), Group
);
1081 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1084 * Write a small header to the file...
1087 curtime
= time(NULL
);
1088 curdate
= localtime(&curtime
);
1089 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1091 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1092 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1094 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1097 * Write every subscription known to the system...
1100 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1102 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1104 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1106 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1109 * Simple event list...
1112 cupsFilePrintf(fp
, "Events %s\n", name
);
1117 * Complex event list...
1120 cupsFilePuts(fp
, "Events");
1122 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1123 if (sub
->mask
& mask
)
1124 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1126 cupsFilePuts(fp
, "\n");
1130 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1132 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1134 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1136 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1138 if (sub
->user_data_len
> 0)
1140 cupsFilePuts(fp
, "UserData ");
1142 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1144 if (sub
->user_data
[i
] < ' ' ||
1145 sub
->user_data
[i
] > 0x7f ||
1146 sub
->user_data
[i
] == '<')
1150 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1154 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1160 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1164 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1169 cupsFilePuts(fp
, ">\n");
1171 cupsFilePutChar(fp
, '\n');
1174 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1175 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1176 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1177 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1179 cupsFilePuts(fp
, "</Subscription>\n");
1187 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1191 cupsdStopAllNotifiers(void)
1193 cupsd_subscription_t
*sub
; /* Current subscription */
1197 * See if we have started any notifiers...
1200 if (!NotifierStatusBuffer
)
1204 * Yes, kill any processes that are left...
1207 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1209 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1212 cupsdEndProcess(sub
->pid
, 0);
1219 * Close the status pipes...
1222 if (NotifierPipes
[0] >= 0)
1224 cupsdRemoveSelect(NotifierPipes
[0]);
1226 cupsdStatBufDelete(NotifierStatusBuffer
);
1228 close(NotifierPipes
[0]);
1229 close(NotifierPipes
[1]);
1231 NotifierPipes
[0] = -1;
1232 NotifierPipes
[1] = -1;
1233 NotifierStatusBuffer
= NULL
;
1239 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1242 static int /* O - Result of comparison */
1243 cupsd_compare_subscriptions(
1244 cupsd_subscription_t
*first
, /* I - First subscription object */
1245 cupsd_subscription_t
*second
, /* I - Second subscription object */
1246 void *unused
) /* I - Unused user data pointer */
1250 return (first
->id
- second
->id
);
1255 * 'cupsd_delete_event()' - Delete a single event...
1257 * Oldest events must be deleted first, otherwise the subscription cache
1258 * flushing code will not work properly.
1262 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1268 ippDelete(event
->attrs
);
1275 * 'cupsd_send_dbus()' - Send a DBUS notification...
1279 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1280 cupsd_printer_t
*dest
,/* I - Destination, if any */
1281 cupsd_job_t
*job
) /* I - Job, if any */
1283 DBusError error
; /* Error, if any */
1284 DBusMessage
*message
; /* Message to send */
1285 DBusMessageIter iter
; /* Iterator for message data */
1286 const char *what
; /* What to send */
1287 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1291 * Figure out what to send, if anything...
1294 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1295 what
= "PrinterAdded";
1296 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1297 what
= "PrinterRemoved";
1298 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1299 what
= "QueueChanged";
1300 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1301 what
= "JobQueuedLocal";
1302 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1303 job
->state_value
== IPP_JOB_PROCESSING
)
1304 what
= "JobStartedLocal";
1309 * Verify connection to DBUS server...
1312 if (con
&& !dbus_connection_get_is_connected(con
))
1314 dbus_connection_unref(con
);
1320 dbus_error_init(&error
);
1322 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1325 dbus_error_free(&error
);
1331 * Create and send the new message...
1334 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1335 "com.redhat.PrinterSpooler", what
);
1337 dbus_message_append_iter_init(message
, &iter
);
1339 dbus_message_iter_append_string(&iter
, dest
->name
);
1342 dbus_message_iter_append_uint32(&iter
, job
->id
);
1343 dbus_message_iter_append_string(&iter
, job
->username
);
1346 dbus_connection_send(con
, message
, NULL
);
1347 dbus_connection_flush(con
);
1348 dbus_message_unref(message
);
1350 #endif /* HAVE_DBUS */
1354 * 'cupsd_send_notification()' - Send a notification for the specified event.
1358 cupsd_send_notification(
1359 cupsd_subscription_t
*sub
, /* I - Subscription object */
1360 cupsd_event_t
*event
) /* I - Event to send */
1362 ipp_state_t state
; /* IPP event state */
1365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1366 "cupsd_send_notification(sub=%p(%d), event=%p(%s))\n",
1367 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1370 * Allocate the events array as needed...
1375 sub
->events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
1379 cupsdLogMessage(CUPSD_LOG_CRIT
,
1380 "Unable to allocate memory for subscription #%d!",
1387 * Purge an old event as needed...
1390 if (sub
->num_events
>= MaxEvents
)
1393 * Purge the oldest event in the cache...
1396 cupsd_delete_event(sub
->events
[0]);
1399 sub
->first_event_id
++;
1401 memmove(sub
->events
, sub
->events
+ 1,
1402 sub
->num_events
* sizeof(cupsd_event_t
*));
1406 * Add the event to the subscription. Since the events array is
1407 * always MaxEvents in length, and since we will have already
1408 * removed an event from the subscription cache if we hit the
1409 * event cache limit, we don't need to check for overflow here...
1412 sub
->events
[sub
->num_events
] = event
;
1416 * Deliver the event...
1424 cupsd_start_notifier(sub
);
1426 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "sub->pipe=%d", sub
->pipe
);
1431 event
->attrs
->state
= IPP_IDLE
;
1433 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1434 if (state
== IPP_ERROR
)
1437 if (state
== IPP_ERROR
)
1442 * Notifier died, try restarting it...
1445 cupsdLogMessage(CUPSD_LOG_WARN
,
1446 "Notifier for subscription %d (%s) went away, "
1448 sub
->id
, sub
->recipient
);
1449 cupsdEndProcess(sub
->pid
, 0);
1456 cupsdLogMessage(CUPSD_LOG_ERROR
,
1457 "Unable to send event for subscription %d (%s)!",
1458 sub
->id
, sub
->recipient
);
1462 * If we get this far, break out of the loop...
1470 * Bump the event sequence number...
1473 sub
->next_event_id
++;
1478 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1482 cupsd_start_notifier(
1483 cupsd_subscription_t
*sub
) /* I - Subscription object */
1485 int pid
; /* Notifier process ID */
1486 int fds
[2]; /* Pipe file descriptors */
1487 char *argv
[4], /* Command-line arguments */
1488 *envp
[MAX_ENV
], /* Environment variables */
1489 user_data
[128], /* Base-64 encoded user data */
1490 scheme
[256], /* notify-recipient-uri scheme */
1491 *ptr
, /* Pointer into scheme */
1492 command
[1024]; /* Notifier command */
1496 * Extract the scheme name from the recipient URI and point to the
1497 * notifier program...
1500 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1501 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1504 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1507 * Base-64 encode the user data...
1510 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1511 sub
->user_data_len
);
1514 * Setup the argument array...
1518 argv
[1] = sub
->recipient
;
1519 argv
[2] = user_data
;
1523 * Setup the environment...
1526 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1529 * Create pipes as needed...
1532 if (!NotifierStatusBuffer
)
1535 * Create the status pipe...
1538 if (cupsdOpenPipe(NotifierPipes
))
1540 cupsdLogMessage(CUPSD_LOG_ERROR
,
1541 "Unable to create pipes for notifier status - %s",
1546 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1548 cupsdAddSelect(NotifierPipes
[0], (cupsd_selfunc_t
)cupsd_update_notifier
,
1552 if (cupsdOpenPipe(fds
))
1554 cupsdLogMessage(CUPSD_LOG_ERROR
,
1555 "Unable to create pipes for notifier %s - %s",
1556 scheme
, strerror(errno
));
1561 * Make sure the delivery pipe is non-blocking...
1564 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1567 * Create the notifier process...
1570 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1571 -1, -1, 0, DefaultProfile
, &pid
) < 0)
1574 * Error - can't fork!
1577 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1578 scheme
, strerror(errno
));
1580 cupsdClosePipe(fds
);
1585 * Fork successful - return the PID...
1588 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1601 * 'cupsd_update_notifier()' - Read messages from notifiers.
1605 cupsd_update_notifier(void)
1607 char message
[1024]; /* Pointer to message text */
1608 int loglevel
; /* Log level for message */
1611 while (cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1612 message
, sizeof(message
)))
1613 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1619 * End of "$Id: subscriptions.c 6649 2007-07-11 21:46:42Z mike $".