2 * "$Id: subscriptions.c 5108 2006-02-15 19:33:09Z mike $"
4 * Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddEvent() - Add an event to the global event cache.
27 * cupsdAddSubscription() - Add a new subscription object.
28 * cupsdDeleteAllEvents() - Delete all cached events.
29 * cupsdDeleteAllSubscriptions() - Delete all subscriptions.
30 * cupsdDeleteSubscription() - Delete a subscription object.
31 * cupsdEventName() - Return a single event name.
32 * cupsdEventValue() - Return the event mask value for a name.
33 * cupsdExpireSubscriptions() - Expire old subscription objects.
34 * cupsdFindSubscription() - Find a subscription by ID.
35 * cupsdLoadAllSubscriptions() - Load all subscriptions from the .conf file.
36 * cupsdSaveAllSubscriptions() - Save all subscriptions to the .conf file.
37 * cupsdSendNotification() - Send a notification for the specified event.
38 * cupsdStopAllNotifiers() - Stop all notifier processes.
39 * cupsdUpdateNotifierStatus() - Read messages from notifiers.
40 * cupsd_compare_subscriptions() - Compare two subscriptions.
41 * cupsd_delete_event() - Delete a single event...
42 * cupsd_send_dbus() - Send a DBUS notification...
43 * cupsd_start_notifier() - Start a notifier subprocess...
47 * Include necessary headers...
52 # include <dbus/dbus.h>
53 #endif /* HAVE_DBUS */
60 static int cupsd_compare_subscriptions(cupsd_subscription_t
*first
,
61 cupsd_subscription_t
*second
,
63 static void cupsd_delete_event(cupsd_event_t
*event
);
65 static void cupsd_send_dbus(cupsd_eventmask_t event
, cupsd_printer_t
*dest
,
67 #endif /* HAVE_DBUS */
68 static void cupsd_start_notifier(cupsd_subscription_t
*sub
);
72 * 'cupsdAddEvent()' - Add an event to the global event cache.
77 cupsd_eventmask_t event
, /* I - Event */
78 cupsd_printer_t
*dest
, /* I - Printer associated with event */
79 cupsd_job_t
*job
, /* I - Job associated with event */
80 const char *text
, /* I - Notification text */
81 ...) /* I - Additional arguments as needed */
83 va_list ap
; /* Pointer to additional arguments */
84 char ftext
[1024]; /* Formatted text buffer */
85 ipp_attribute_t
*attr
; /* Printer/job attribute */
86 cupsd_event_t
*temp
; /* New event pointer */
87 cupsd_subscription_t
*sub
; /* Current subscription */
91 * Keep track of events with any OS-supplied notification mechanisms...
97 cupsd_send_dbus(event
, dest
, job
);
98 #endif /* HAVE_DBUS */
101 * Return if we aren't keeping events...
106 cupsdLogMessage(CUPSD_LOG_WARN
,
107 "cupsdAddEvent: Discarding %s event since MaxEvents is %d!",
108 cupsdEventName(event
), MaxEvents
);
113 * Allocate memory for the event cache as needed...
118 Events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
123 cupsdLogMessage(CUPSD_LOG_CRIT
,
124 "Unable to allocate memory for event cache - %s",
131 * Then loop through the subscriptions and add the event to the corresponding
135 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), temp
= NULL
;
137 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
140 * Check if this subscription requires this event...
143 if ((sub
->mask
& event
) != 0 &&
144 (sub
->dest
== dest
|| !sub
->dest
) &&
145 (sub
->job
== job
|| !sub
->job
))
154 * Create the new event record...
157 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
159 cupsdLogMessage(CUPSD_LOG_CRIT
,
160 "Unable to allocate memory for event - %s",
166 temp
->time
= time(NULL
);
167 temp
->attrs
= ippNew();
172 * Add common event notification attributes...
175 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
176 "notify-subscription-id", sub
->id
);
178 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
179 "notify-subscribed-event", NULL
, cupsdEventName(event
));
181 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
182 "printer-up-time", time(NULL
));
185 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
188 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
189 "notify-text", NULL
, ftext
);
194 * Add printer attributes...
197 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
198 "notify-printer-uri", NULL
, dest
->uri
);
200 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
201 "printer-name", NULL
, dest
->name
);
203 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
204 "printer-state", dest
->state
);
206 if (dest
->num_reasons
== 0)
207 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
208 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
209 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
211 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
212 IPP_TAG_KEYWORD
, "printer-state-reasons",
213 dest
->num_reasons
, NULL
,
214 (const char * const *)dest
->reasons
);
216 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
217 "printer-is-accepting-jobs", dest
->accepting
);
223 * Add job attributes...
226 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
227 "notify-job-id", job
->id
);
228 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
229 "job-state", (int)job
->state
);
231 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
232 IPP_TAG_NAME
)) != NULL
)
233 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
234 "job-name", NULL
, attr
->values
[0].string
.text
);
236 switch (job
->state_value
)
238 case IPP_JOB_PENDING
:
239 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
240 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
241 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
244 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
245 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
250 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
251 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
252 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
253 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
254 "job-hold-until-specified");
256 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
257 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
261 case IPP_JOB_PROCESSING
:
262 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
263 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
267 case IPP_JOB_STOPPED
:
268 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
269 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
273 case IPP_JOB_CANCELLED
:
274 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
275 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
276 "job-canceled-by-user");
279 case IPP_JOB_ABORTED
:
280 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
281 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
282 "aborted-by-system");
285 case IPP_JOB_COMPLETED
:
286 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
287 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
288 "job-completed-successfully");
292 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
293 "job-impressions-completed",
294 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
298 * Purge an old event as needed...
301 if (NumEvents
>= MaxEvents
)
304 * Purge the oldest event in the cache...
307 cupsd_delete_event(Events
[0]);
311 memmove(Events
, Events
+ 1, NumEvents
* sizeof(cupsd_event_t
*));
315 * Add the new event to the main cache...
318 Events
[NumEvents
] = temp
;
323 * Send the notification for this subscription...
326 cupsdSendNotification(sub
, temp
);
331 cupsdSaveAllSubscriptions();
333 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
334 cupsdEventName(event
));
339 * 'cupsdAddSubscription()' - Add a new subscription object.
342 cupsd_subscription_t
* /* O - New subscription object */
343 cupsdAddSubscription(
344 unsigned mask
, /* I - Event mask */
345 cupsd_printer_t
*dest
, /* I - Printer, if any */
346 cupsd_job_t
*job
, /* I - Job, if any */
347 const char *uri
, /* I - notify-recipient-uri, if any */
348 int sub_id
) /* I - notify-subscription-id or 0 */
350 cupsd_subscription_t
*temp
; /* New subscription object */
353 cupsdLogMessage(CUPSD_LOG_DEBUG
,
354 "cupsdAddSubscription(mask=%x(%s), dest=%p(%s), job=%p(%d), uri=\"%s\")",
355 mask
, cupsdEventName(mask
), dest
, dest
? dest
->name
: "",
356 job
, job
? job
->id
: 0, uri
);
359 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
364 cupsdLogMessage(CUPSD_LOG_CRIT
,
365 "Unable to allocate memory for subscriptions - %s",
371 * Limit the number of subscriptions...
374 if (cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
378 * Allocate memory for this subscription...
381 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
383 cupsdLogMessage(CUPSD_LOG_CRIT
,
384 "Unable to allocate memory for subscription object - %s",
390 * Fill in common data...
397 if (sub_id
>= NextSubscriptionId
)
398 NextSubscriptionId
= sub_id
+ 1;
402 temp
->id
= NextSubscriptionId
;
404 NextSubscriptionId
++;
411 temp
->first_event_id
= 1;
412 temp
->next_event_id
= 1;
414 cupsdSetString(&(temp
->recipient
), uri
);
417 * Add the subscription to the array...
420 cupsArrayAdd(Subscriptions
, temp
);
427 * 'cupsdDeleteAllEvents()' - Delete all cached events.
431 cupsdDeleteAllEvents(void)
433 int i
; /* Looping var */
436 if (MaxEvents
<= 0 || !Events
)
439 for (i
= 0; i
< NumEvents
; i
++)
440 cupsd_delete_event(Events
[i
]);
448 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
452 cupsdDeleteAllSubscriptions(void)
454 cupsd_subscription_t
*sub
; /* Subscription */
460 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
462 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
463 cupsdDeleteSubscription(sub
, 0);
465 cupsArrayDelete(Subscriptions
);
466 Subscriptions
= NULL
;
471 * 'cupsdDeleteSubscription()' - Delete a subscription object.
475 cupsdDeleteSubscription(
476 cupsd_subscription_t
*sub
, /* I - Subscription object */
477 int update
) /* I - 1 = update subscriptions.conf */
480 * Close the pipe to the notifier as needed...
487 * Remove subscription from array...
490 cupsArrayRemove(Subscriptions
, sub
);
496 cupsdClearString(&(sub
->owner
));
497 cupsdClearString(&(sub
->recipient
));
505 * Update the subscriptions as needed...
509 cupsdSaveAllSubscriptions();
514 * 'cupsdEventName()' - Return a single event name.
517 const char * /* O - Event name */
519 cupsd_eventmask_t event
) /* I - Event value */
526 case CUPSD_EVENT_PRINTER_RESTARTED
:
527 return ("printer-restarted");
529 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
530 return ("printer-shutdown");
532 case CUPSD_EVENT_PRINTER_STOPPED
:
533 return ("printer-stopped");
535 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
536 return ("printer-finishings-changed");
538 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
539 return ("printer-media-changed");
541 case CUPSD_EVENT_PRINTER_ADDED
:
542 return ("printer-added");
544 case CUPSD_EVENT_PRINTER_DELETED
:
545 return ("printer-deleted");
547 case CUPSD_EVENT_PRINTER_MODIFIED
:
548 return ("printer-modified");
550 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
551 return ("printer-state-changed");
553 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
554 return ("printer-config-changed");
556 case CUPSD_EVENT_PRINTER_CHANGED
:
557 return ("printer-changed");
559 case CUPSD_EVENT_JOB_CREATED
:
560 return ("job-created");
562 case CUPSD_EVENT_JOB_COMPLETED
:
563 return ("job-completed");
565 case CUPSD_EVENT_JOB_STOPPED
:
566 return ("job-stopped");
568 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
569 return ("job-config-changed");
571 case CUPSD_EVENT_JOB_PROGRESS
:
572 return ("job-progress");
574 case CUPSD_EVENT_JOB_STATE_CHANGED
:
575 return ("job-state-changed");
577 case CUPSD_EVENT_SERVER_RESTARTED
:
578 return ("server-restarted");
580 case CUPSD_EVENT_SERVER_STARTED
:
581 return ("server-started");
583 case CUPSD_EVENT_SERVER_STOPPED
:
584 return ("server-stopped");
586 case CUPSD_EVENT_SERVER_AUDIT
:
587 return ("server-audit");
589 case CUPSD_EVENT_ALL
:
596 * 'cupsdEventValue()' - Return the event mask value for a name.
599 cupsd_eventmask_t
/* O - Event mask value */
600 cupsdEventValue(const char *name
) /* I - Name of event */
602 if (!strcmp(name
, "all"))
603 return (CUPSD_EVENT_ALL
);
604 else if (!strcmp(name
, "printer-restarted"))
605 return (CUPSD_EVENT_PRINTER_RESTARTED
);
606 else if (!strcmp(name
, "printer-shutdown"))
607 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
608 else if (!strcmp(name
, "printer-stopped"))
609 return (CUPSD_EVENT_PRINTER_STOPPED
);
610 else if (!strcmp(name
, "printer-finishings-changed"))
611 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
612 else if (!strcmp(name
, "printer-media-changed"))
613 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
614 else if (!strcmp(name
, "printer-added"))
615 return (CUPSD_EVENT_PRINTER_ADDED
);
616 else if (!strcmp(name
, "printer-deleted"))
617 return (CUPSD_EVENT_PRINTER_DELETED
);
618 else if (!strcmp(name
, "printer-modified"))
619 return (CUPSD_EVENT_PRINTER_MODIFIED
);
620 else if (!strcmp(name
, "printer-state-changed"))
621 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
622 else if (!strcmp(name
, "printer-config-changed"))
623 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
624 else if (!strcmp(name
, "printer-changed"))
625 return (CUPSD_EVENT_PRINTER_CHANGED
);
626 else if (!strcmp(name
, "job-created"))
627 return (CUPSD_EVENT_JOB_CREATED
);
628 else if (!strcmp(name
, "job-completed"))
629 return (CUPSD_EVENT_JOB_COMPLETED
);
630 else if (!strcmp(name
, "job-stopped"))
631 return (CUPSD_EVENT_JOB_STOPPED
);
632 else if (!strcmp(name
, "job-config-changed"))
633 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
634 else if (!strcmp(name
, "job-progress"))
635 return (CUPSD_EVENT_JOB_PROGRESS
);
636 else if (!strcmp(name
, "job-state-changed"))
637 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
638 else if (!strcmp(name
, "server-restarted"))
639 return (CUPSD_EVENT_SERVER_RESTARTED
);
640 else if (!strcmp(name
, "server-started"))
641 return (CUPSD_EVENT_SERVER_STARTED
);
642 else if (!strcmp(name
, "server-stopped"))
643 return (CUPSD_EVENT_SERVER_STOPPED
);
644 else if (!strcmp(name
, "server-audit"))
645 return (CUPSD_EVENT_SERVER_AUDIT
);
647 return (CUPSD_EVENT_NONE
);
652 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
656 cupsdExpireSubscriptions(
657 cupsd_printer_t
*dest
, /* I - Printer, if any */
658 cupsd_job_t
*job
) /* I - Job, if any */
660 cupsd_subscription_t
*sub
; /* Current subscription */
661 int update
; /* Update subscriptions.conf? */
662 time_t curtime
; /* Current time */
665 curtime
= time(NULL
);
668 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
670 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
671 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
672 (dest
&& sub
->dest
== dest
) ||
673 (job
&& sub
->job
== job
))
675 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
678 cupsdDeleteSubscription(sub
, 0);
684 cupsdSaveAllSubscriptions();
689 * 'cupsdFindSubscription()' - Find a subscription by ID.
692 cupsd_subscription_t
* /* O - Subscription object */
693 cupsdFindSubscription(int id
) /* I - Subscription ID */
695 cupsd_subscription_t sub
; /* Subscription template */
700 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
705 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
709 cupsdLoadAllSubscriptions(void)
711 int i
; /* Looping var */
712 cups_file_t
*fp
; /* subscriptions.conf file */
713 int linenum
; /* Current line number */
714 char line
[1024], /* Line from file */
715 *value
, /* Pointer to value */
716 *valueptr
; /* Pointer into value */
717 cupsd_subscription_t
*sub
; /* Current subscription */
718 int hex
; /* Non-zero if reading hex data */
719 int delete_sub
; /* Delete subscription? */
723 * Open the subscriptions.conf file...
726 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
727 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
730 cupsdLogMessage(CUPSD_LOG_ERROR
,
731 "LoadAllSubscriptions: Unable to open %s - %s", line
,
737 * Read all of the lines from the file...
744 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
746 if (!strcasecmp(line
, "<Subscription"))
752 if (!sub
&& value
&& isdigit(value
[0] & 255))
754 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
759 cupsdLogMessage(CUPSD_LOG_ERROR
,
760 "Syntax error on line %d of subscriptions.conf.",
765 else if (!strcasecmp(line
, "</Subscription>"))
769 cupsdLogMessage(CUPSD_LOG_ERROR
,
770 "Syntax error on line %d of subscriptions.conf.",
776 cupsdDeleteSubscription(sub
, 0);
783 cupsdLogMessage(CUPSD_LOG_ERROR
,
784 "Syntax error on line %d of subscriptions.conf.",
788 else if (!strcasecmp(line
, "Events"))
792 * Events name name name ...
797 cupsdLogMessage(CUPSD_LOG_ERROR
,
798 "Syntax error on line %d of subscriptions.conf.",
806 * Separate event names...
809 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
811 while (isspace(*valueptr
& 255))
815 * See if the name exists...
818 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
820 cupsdLogMessage(CUPSD_LOG_ERROR
,
821 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
829 else if (!strcasecmp(line
, "Owner"))
836 cupsdSetString(&sub
->owner
, value
);
839 cupsdLogMessage(CUPSD_LOG_ERROR
,
840 "Syntax error on line %d of subscriptions.conf.",
845 else if (!strcasecmp(line
, "Recipient"))
852 cupsdSetString(&sub
->recipient
, value
);
855 cupsdLogMessage(CUPSD_LOG_ERROR
,
856 "Syntax error on line %d of subscriptions.conf.",
861 else if (!strcasecmp(line
, "JobId"))
867 if (value
&& isdigit(*value
& 255))
869 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
871 cupsdLogMessage(CUPSD_LOG_ERROR
,
872 "Job %s not found on line %d of subscriptions.conf.",
879 cupsdLogMessage(CUPSD_LOG_ERROR
,
880 "Syntax error on line %d of subscriptions.conf.",
885 else if (!strcasecmp(line
, "PrinterName"))
893 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
895 cupsdLogMessage(CUPSD_LOG_ERROR
,
896 "Printer \'%s\' not found on line %d of subscriptions.conf.",
903 cupsdLogMessage(CUPSD_LOG_ERROR
,
904 "Syntax error on line %d of subscriptions.conf.",
909 else if (!strcasecmp(line
, "UserData"))
912 * UserData encoded-string
917 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
919 if (*valueptr
== '<' && !hex
)
927 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
929 if (isdigit(valueptr
[0]))
930 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
932 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
934 if (isdigit(valueptr
[1]))
935 sub
->user_data
[i
] |= valueptr
[1] - '0';
937 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
941 if (*valueptr
== '>')
951 sub
->user_data
[i
] = *valueptr
++;
956 cupsdLogMessage(CUPSD_LOG_ERROR
,
957 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
961 sub
->user_data_len
= i
;
965 cupsdLogMessage(CUPSD_LOG_ERROR
,
966 "Syntax error on line %d of subscriptions.conf.",
971 else if (!strcasecmp(line
, "LeaseDuration"))
977 if (value
&& isdigit(*value
& 255))
979 sub
->lease
= atoi(value
);
980 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
984 cupsdLogMessage(CUPSD_LOG_ERROR
,
985 "Syntax error on line %d of subscriptions.conf.",
990 else if (!strcasecmp(line
, "Interval"))
996 if (value
&& isdigit(*value
& 255))
997 sub
->interval
= atoi(value
);
1000 cupsdLogMessage(CUPSD_LOG_ERROR
,
1001 "Syntax error on line %d of subscriptions.conf.",
1006 else if (!strcasecmp(line
, "ExpirationTime"))
1012 if (value
&& isdigit(*value
& 255))
1013 sub
->expire
= atoi(value
);
1016 cupsdLogMessage(CUPSD_LOG_ERROR
,
1017 "Syntax error on line %d of subscriptions.conf.",
1022 else if (!strcasecmp(line
, "NextEventId"))
1028 if (value
&& isdigit(*value
& 255))
1029 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1032 cupsdLogMessage(CUPSD_LOG_ERROR
,
1033 "Syntax error on line %d of subscriptions.conf.",
1041 * Something else we don't understand...
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1055 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1059 cupsdSaveAllSubscriptions(void)
1061 int i
; /* Looping var */
1062 cups_file_t
*fp
; /* subscriptions.conf file */
1063 char temp
[1024]; /* Temporary string */
1064 char backup
[1024]; /* subscriptions.conf.O file */
1065 cupsd_subscription_t
*sub
; /* Current subscription */
1066 time_t curtime
; /* Current time */
1067 struct tm
*curdate
; /* Current date */
1068 unsigned mask
; /* Current event mask */
1069 const char *name
; /* Current event name */
1070 int hex
; /* Non-zero if we are writing hex data */
1074 * Create the subscriptions.conf file...
1077 snprintf(temp
, sizeof(temp
), "%s/subscriptions.conf", ServerRoot
);
1078 snprintf(backup
, sizeof(backup
), "%s/subscriptions.conf.O", ServerRoot
);
1080 if (rename(temp
, backup
))
1082 if (errno
!= ENOENT
)
1083 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup subscriptions.conf - %s",
1087 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1089 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save subscriptions.conf - %s",
1092 if (rename(backup
, temp
))
1093 cupsdLogMessage(CUPSD_LOG_ERROR
,
1094 "Unable to restore subscriptions.conf - %s",
1099 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1102 * Restrict access to the file...
1105 fchown(cupsFileNumber(fp
), getuid(), Group
);
1106 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1109 * Write a small header to the file...
1112 curtime
= time(NULL
);
1113 curdate
= localtime(&curtime
);
1114 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1116 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1117 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1120 * Write every subscription known to the system...
1123 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1125 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1127 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1129 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1132 * Simple event list...
1135 cupsFilePrintf(fp
, "Events %s\n", name
);
1140 * Complex event list...
1143 cupsFilePuts(fp
, "Events");
1145 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1146 if (sub
->mask
& mask
)
1147 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1149 cupsFilePuts(fp
, "\n");
1153 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1155 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1157 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1159 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1161 if (sub
->user_data_len
> 0)
1163 cupsFilePuts(fp
, "UserData ");
1165 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1167 if (sub
->user_data
[i
] < ' ' ||
1168 sub
->user_data
[i
] > 0x7f ||
1169 sub
->user_data
[i
] == '<')
1173 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1177 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1183 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1187 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1192 cupsFilePuts(fp
, ">\n");
1194 cupsFilePutChar(fp
, '\n');
1197 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1198 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1199 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1200 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1202 cupsFilePuts(fp
, "</Subscription>\n");
1210 * 'cupsdSendNotification()' - Send a notification for the specified event.
1214 cupsdSendNotification(
1215 cupsd_subscription_t
*sub
, /* I - Subscription object */
1216 cupsd_event_t
*event
) /* I - Event to send */
1218 ipp_state_t state
; /* IPP event state */
1221 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1222 "cupsdSendNotification(sub=%p(%d), event=%p(%s))\n",
1223 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1226 * Allocate the events array as needed...
1231 sub
->events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
1235 cupsdLogMessage(CUPSD_LOG_CRIT
,
1236 "Unable to allocate memory for subscription #%d!",
1243 * Add the event to the subscription. Since the events array is
1244 * always MaxEvents in length, and since we will have already
1245 * removed an event from the subscription cache if we hit the
1246 * event cache limit, we don't need to check for overflow here...
1249 sub
->events
[sub
->num_events
] = event
;
1253 * Deliver the event...
1259 cupsd_start_notifier(sub
);
1261 cupsdLogMessage(CUPSD_LOG_DEBUG
, "sub->pipe=%d", sub
->pipe
);
1265 event
->attrs
->state
= IPP_IDLE
;
1267 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1268 if (state
== IPP_ERROR
)
1271 if (state
== IPP_ERROR
)
1272 cupsdLogMessage(CUPSD_LOG_ERROR
,
1273 "Unable to send event for subscription %d (%s)!",
1274 sub
->id
, sub
->recipient
);
1279 * Bump the event sequence number...
1282 sub
->next_event_id
++;
1287 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1291 cupsdStopAllNotifiers(void)
1293 cupsd_subscription_t
*sub
; /* Current subscription */
1297 * See if we have started any notifiers...
1300 if (!NotifierStatusBuffer
)
1304 * Yes, kill any processes that are left...
1307 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1309 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1312 cupsdEndProcess(sub
->pid
, 0);
1319 * Close the status pipes...
1322 if (NotifierPipes
[0] >= 0)
1324 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1325 "cupsdStopAllNotifiers: Removing fd %d from InputSet...",
1327 FD_CLR(NotifierPipes
[0], InputSet
);
1329 cupsdStatBufDelete(NotifierStatusBuffer
);
1331 close(NotifierPipes
[0]);
1332 close(NotifierPipes
[1]);
1334 NotifierPipes
[0] = -1;
1335 NotifierPipes
[1] = -1;
1336 NotifierStatusBuffer
= NULL
;
1342 * 'cupsdUpdateNotifierStatus()' - Read messages from notifiers.
1346 cupsdUpdateNotifierStatus(void)
1348 char *ptr
, /* Pointer to end of line in buffer */
1349 message
[1024]; /* Pointer to message text */
1350 int loglevel
; /* Log level for message */
1353 while ((ptr
= cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1354 message
, sizeof(message
))) != NULL
)
1355 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1361 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1364 static int /* O - Result of comparison */
1365 cupsd_compare_subscriptions(
1366 cupsd_subscription_t
*first
, /* I - First subscription object */
1367 cupsd_subscription_t
*second
, /* I - Second subscription object */
1368 void *unused
) /* I - Unused user data pointer */
1372 return (first
->id
- second
->id
);
1377 * 'cupsd_delete_event()' - Delete a single event...
1379 * Oldest events must be deleted first, otherwise the subscription cache
1380 * flushing code will not work properly.
1384 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1386 cupsd_subscription_t
*sub
; /* Current subscription */
1390 * Loop through the subscriptions and look for the event in the cache...
1393 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1395 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1398 * Only check the first event in the subscription cache, since the
1399 * caller will only delete the oldest event in the cache...
1402 if (sub
->num_events
> 0 && sub
->events
[0] == event
)
1405 * Remove this event...
1409 sub
->first_event_id
++;
1411 if (sub
->num_events
> 0)
1414 * Shift other events upward in cache...
1417 memmove(sub
->events
, sub
->events
+ 1,
1418 sub
->num_events
* sizeof(cupsd_event_t
*));
1427 ippDelete(event
->attrs
);
1434 * 'cupsd_send_dbus()' - Send a DBUS notification...
1438 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1439 cupsd_printer_t
*dest
,/* I - Destination, if any */
1440 cupsd_job_t
*job
) /* I - Job, if any */
1442 DBusError error
; /* Error, if any */
1443 DBusMessage
*message
; /* Message to send */
1444 DBusMessageIter iter
; /* Iterator for message data */
1445 const char *what
; /* What to send */
1446 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1450 * Figure out what to send, if anything...
1453 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1454 what
= "PrinterAdded";
1455 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1456 what
= "PrinterRemoved";
1457 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1458 what
= "QueueChanged";
1459 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1460 what
= "JobQueuedLocal";
1461 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1462 job
->state_value
== IPP_JOB_PROCESSING
)
1463 what
= "JobStartedLocal";
1468 * Verify connection to DBUS server...
1471 if (con
&& !dbus_connection_get_is_connected(con
))
1473 dbus_connection_unref(con
);
1479 dbus_error_init(&error
);
1481 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1484 dbus_error_free(&error
);
1490 * Create and send the new message...
1493 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1494 "com.redhat.PrinterSpooler", what
);
1496 dbus_message_iter_init_append(message
, &iter
);
1498 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &(dest
->name
));
1501 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_UINT32
, &(job
->id
));
1502 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &(job
->username
));
1505 dbus_connection_send(con
, message
, NULL
);
1506 dbus_connection_flush(con
);
1507 dbus_message_unref(message
);
1509 #endif /* HAVE_DBUS */
1513 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1517 cupsd_start_notifier(
1518 cupsd_subscription_t
*sub
) /* I - Subscription object */
1520 int pid
; /* Notifier process ID */
1521 int fds
[2]; /* Pipe file descriptors */
1522 int envc
; /* Number of environment variables */
1523 char *argv
[4], /* Command-line arguments */
1524 *envp
[MAX_ENV
], /* Environment variables */
1525 user_data
[128], /* Base-64 encoded user data */
1526 scheme
[256], /* notify-recipient-uri scheme */
1527 *ptr
, /* Pointer into scheme */
1528 command
[1024]; /* Notifier command */
1532 * Extract the scheme name from the recipient URI and point to the
1533 * notifier program...
1536 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1537 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1540 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1543 * Base-64 encode the user data...
1546 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1547 sub
->user_data_len
);
1550 * Setup the argument array...
1554 argv
[1] = sub
->recipient
;
1555 argv
[2] = user_data
;
1559 * Setup the environment...
1562 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1565 * Create pipes as needed...
1568 if (!NotifierStatusBuffer
)
1571 * Create the status pipe...
1574 if (cupsdOpenPipe(NotifierPipes
))
1576 cupsdLogMessage(CUPSD_LOG_ERROR
,
1577 "Unable to create pipes for notifier status - %s",
1582 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1584 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1585 "start_notifier: Adding fd %d to InputSet...",
1588 FD_SET(NotifierPipes
[0], InputSet
);
1591 if (cupsdOpenPipe(fds
))
1593 cupsdLogMessage(CUPSD_LOG_ERROR
,
1594 "Unable to create pipes for notifier %s - %s",
1595 scheme
, strerror(errno
));
1600 * Make sure the delivery pipe is non-blocking...
1603 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1606 * Create the notifier process...
1609 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1613 * Error - can't fork!
1616 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1617 scheme
, strerror(errno
));
1619 cupsdClosePipe(fds
);
1624 * Fork successful - return the PID...
1627 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1640 * End of "$Id: subscriptions.c 5108 2006-02-15 19:33:09Z mike $".