2 * "$Id: subscriptions.c 5991 2006-09-29 02:26:29Z 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 * cupsdDeleteAllSubscriptions() - Delete all subscriptions.
29 * cupsdDeleteSubscription() - Delete a subscription object.
30 * cupsdEventName() - Return a single event name.
31 * cupsdEventValue() - Return the event mask value for a name.
32 * cupsdExpireSubscriptions() - Expire old subscription objects.
33 * cupsdFindSubscription() - Find a subscription by ID.
34 * cupsdLoadAllSubscriptions() - Load all subscriptions from the .conf file.
35 * cupsdSaveAllSubscriptions() - Save all subscriptions to the .conf file.
36 * cupsdStopAllNotifiers() - Stop all notifier processes.
37 * cupsdUpdateNotifierStatus() - Read messages from notifiers.
38 * cupsd_compare_subscriptions() - Compare two subscriptions.
39 * cupsd_delete_event() - Delete a single event...
40 * cupsd_send_dbus() - Send a DBUS notification...
41 * cupsd_send_notification() - Send a notification for the specified
43 * cupsd_start_notifier() - Start a notifier subprocess...
47 * Include necessary headers...
52 # include <dbus/dbus.h>
53 # ifdef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND
54 # define dbus_message_append_iter_init dbus_message_iter_init_append
55 # define dbus_message_iter_append_string(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, v)
56 # define dbus_message_iter_append_uint32(i,v) dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, v)
57 # endif /* HAVE_DBUS_MESSAGE_ITER_INIT_APPEND */
58 #endif /* HAVE_DBUS */
65 static int cupsd_compare_subscriptions(cupsd_subscription_t
*first
,
66 cupsd_subscription_t
*second
,
68 static void cupsd_delete_event(cupsd_event_t
*event
);
70 static void cupsd_send_dbus(cupsd_eventmask_t event
, cupsd_printer_t
*dest
,
72 #endif /* HAVE_DBUS */
73 static void cupsd_send_notification(cupsd_subscription_t
*sub
,
74 cupsd_event_t
*event
);
75 static void cupsd_start_notifier(cupsd_subscription_t
*sub
);
79 * 'cupsdAddEvent()' - Add an event to the global event cache.
84 cupsd_eventmask_t event
, /* I - Event */
85 cupsd_printer_t
*dest
, /* I - Printer associated with event */
86 cupsd_job_t
*job
, /* I - Job associated with event */
87 const char *text
, /* I - Notification text */
88 ...) /* I - Additional arguments as needed */
90 va_list ap
; /* Pointer to additional arguments */
91 char ftext
[1024]; /* Formatted text buffer */
92 ipp_attribute_t
*attr
; /* Printer/job attribute */
93 cupsd_event_t
*temp
; /* New event pointer */
94 cupsd_subscription_t
*sub
; /* Current subscription */
97 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
98 "cupsdAddEvent(event=%s, dest=%p(%s), job=%p(%d), text=\"%s\", ...)",
99 cupsdEventName(event
), dest
, dest
? dest
->name
: "",
100 job
, job
? job
->id
: 0, text
);
103 * Keep track of events with any OS-supplied notification mechanisms...
109 cupsd_send_dbus(event
, dest
, job
);
110 #endif /* HAVE_DBUS */
113 * Return if we aren't keeping events...
118 cupsdLogMessage(CUPSD_LOG_WARN
,
119 "cupsdAddEvent: Discarding %s event since MaxEvents is %d!",
120 cupsdEventName(event
), MaxEvents
);
125 * Then loop through the subscriptions and add the event to the corresponding
129 for (temp
= NULL
, sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
131 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
134 * Check if this subscription requires this event...
137 if ((sub
->mask
& event
) != 0 &&
138 (sub
->dest
== dest
|| !sub
->dest
) &&
139 (sub
->job
== job
|| !sub
->job
))
142 * Need this event, so create a new event record...
145 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
147 cupsdLogMessage(CUPSD_LOG_CRIT
,
148 "Unable to allocate memory for event - %s",
154 temp
->time
= time(NULL
);
155 temp
->attrs
= ippNew();
160 * Add common event notification attributes...
163 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_CHARSET
,
164 "notify-charset", NULL
, "utf-8");
166 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_LANGUAGE
,
167 "notify-natural-langugage", NULL
, "en-US");
169 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
170 "notify-subscription-id", sub
->id
);
172 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
173 "notify-sequence-number", sub
->next_event_id
);
175 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
176 "notify-subscribed-event", NULL
, cupsdEventName(event
));
178 if (sub
->user_data_len
> 0)
179 ippAddOctetString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
180 "notify-user-data", sub
->user_data
,
183 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
184 "printer-up-time", time(NULL
));
187 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
190 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
191 "notify-text", NULL
, ftext
);
196 * Add printer attributes...
199 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
200 "notify-printer-uri", NULL
, dest
->uri
);
202 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
203 "printer-name", NULL
, dest
->name
);
205 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
206 "printer-state", dest
->state
);
208 if (dest
->num_reasons
== 0)
209 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
210 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
211 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
213 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
214 IPP_TAG_KEYWORD
, "printer-state-reasons",
215 dest
->num_reasons
, NULL
,
216 (const char * const *)dest
->reasons
);
218 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
219 "printer-is-accepting-jobs", dest
->accepting
);
225 * Add job attributes...
228 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
229 "notify-job-id", job
->id
);
230 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
231 "job-state", job
->state_value
);
233 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
234 IPP_TAG_NAME
)) != NULL
)
235 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
236 "job-name", NULL
, attr
->values
[0].string
.text
);
238 switch (job
->state_value
)
240 case IPP_JOB_PENDING
:
241 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
242 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
243 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
246 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
247 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
252 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
253 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
254 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
255 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
256 "job-hold-until-specified");
258 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
259 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
263 case IPP_JOB_PROCESSING
:
264 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
265 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
269 case IPP_JOB_STOPPED
:
270 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
271 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
275 case IPP_JOB_CANCELED
:
276 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
277 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
278 "job-canceled-by-user");
281 case IPP_JOB_ABORTED
:
282 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
283 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
284 "aborted-by-system");
287 case IPP_JOB_COMPLETED
:
288 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
289 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
290 "job-completed-successfully");
294 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
295 "job-impressions-completed",
296 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
300 * Send the notification for this subscription...
303 cupsd_send_notification(sub
, temp
);
308 cupsdSaveAllSubscriptions();
310 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
311 cupsdEventName(event
));
316 * 'cupsdAddSubscription()' - Add a new subscription object.
319 cupsd_subscription_t
* /* O - New subscription object */
320 cupsdAddSubscription(
321 unsigned mask
, /* I - Event mask */
322 cupsd_printer_t
*dest
, /* I - Printer, if any */
323 cupsd_job_t
*job
, /* I - Job, if any */
324 const char *uri
, /* I - notify-recipient-uri, if any */
325 int sub_id
) /* I - notify-subscription-id or 0 */
327 cupsd_subscription_t
*temp
; /* New subscription object */
330 cupsdLogMessage(CUPSD_LOG_DEBUG
,
331 "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
333 mask
, dest
, dest
? dest
->name
: "", job
, job
? job
->id
: 0,
337 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
342 cupsdLogMessage(CUPSD_LOG_CRIT
,
343 "Unable to allocate memory for subscriptions - %s",
349 * Limit the number of subscriptions...
352 if (cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
356 * Allocate memory for this subscription...
359 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
361 cupsdLogMessage(CUPSD_LOG_CRIT
,
362 "Unable to allocate memory for subscription object - %s",
368 * Fill in common data...
375 if (sub_id
>= NextSubscriptionId
)
376 NextSubscriptionId
= sub_id
+ 1;
380 temp
->id
= NextSubscriptionId
;
382 NextSubscriptionId
++;
389 temp
->first_event_id
= 1;
390 temp
->next_event_id
= 1;
392 cupsdSetString(&(temp
->recipient
), uri
);
395 * Add the subscription to the array...
398 cupsArrayAdd(Subscriptions
, temp
);
405 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
409 cupsdDeleteAllSubscriptions(void)
411 cupsd_subscription_t
*sub
; /* Subscription */
417 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
419 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
420 cupsdDeleteSubscription(sub
, 0);
422 cupsArrayDelete(Subscriptions
);
423 Subscriptions
= NULL
;
428 * 'cupsdDeleteSubscription()' - Delete a subscription object.
432 cupsdDeleteSubscription(
433 cupsd_subscription_t
*sub
, /* I - Subscription object */
434 int update
) /* I - 1 = update subscriptions.conf */
436 int i
; /* Looping var */
440 * Close the pipe to the notifier as needed...
447 * Remove subscription from array...
450 cupsArrayRemove(Subscriptions
, sub
);
456 cupsdClearString(&(sub
->owner
));
457 cupsdClearString(&(sub
->recipient
));
461 for (i
= 0; i
< sub
->num_events
; i
++)
462 cupsd_delete_event(sub
->events
[i
]);
470 * Update the subscriptions as needed...
474 cupsdSaveAllSubscriptions();
479 * 'cupsdEventName()' - Return a single event name.
482 const char * /* O - Event name */
484 cupsd_eventmask_t event
) /* I - Event value */
491 case CUPSD_EVENT_PRINTER_RESTARTED
:
492 return ("printer-restarted");
494 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
495 return ("printer-shutdown");
497 case CUPSD_EVENT_PRINTER_STOPPED
:
498 return ("printer-stopped");
500 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
501 return ("printer-finishings-changed");
503 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
504 return ("printer-media-changed");
506 case CUPSD_EVENT_PRINTER_ADDED
:
507 return ("printer-added");
509 case CUPSD_EVENT_PRINTER_DELETED
:
510 return ("printer-deleted");
512 case CUPSD_EVENT_PRINTER_MODIFIED
:
513 return ("printer-modified");
515 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
516 return ("printer-state-changed");
518 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
519 return ("printer-config-changed");
521 case CUPSD_EVENT_PRINTER_CHANGED
:
522 return ("printer-changed");
524 case CUPSD_EVENT_JOB_CREATED
:
525 return ("job-created");
527 case CUPSD_EVENT_JOB_COMPLETED
:
528 return ("job-completed");
530 case CUPSD_EVENT_JOB_STOPPED
:
531 return ("job-stopped");
533 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
534 return ("job-config-changed");
536 case CUPSD_EVENT_JOB_PROGRESS
:
537 return ("job-progress");
539 case CUPSD_EVENT_JOB_STATE
:
540 return ("job-state");
542 case CUPSD_EVENT_JOB_STATE_CHANGED
:
543 return ("job-state-changed");
545 case CUPSD_EVENT_SERVER_RESTARTED
:
546 return ("server-restarted");
548 case CUPSD_EVENT_SERVER_STARTED
:
549 return ("server-started");
551 case CUPSD_EVENT_SERVER_STOPPED
:
552 return ("server-stopped");
554 case CUPSD_EVENT_SERVER_AUDIT
:
555 return ("server-audit");
557 case CUPSD_EVENT_ALL
:
564 * 'cupsdEventValue()' - Return the event mask value for a name.
567 cupsd_eventmask_t
/* O - Event mask value */
568 cupsdEventValue(const char *name
) /* I - Name of event */
570 if (!strcmp(name
, "all"))
571 return (CUPSD_EVENT_ALL
);
572 else if (!strcmp(name
, "printer-restarted"))
573 return (CUPSD_EVENT_PRINTER_RESTARTED
);
574 else if (!strcmp(name
, "printer-shutdown"))
575 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
576 else if (!strcmp(name
, "printer-stopped"))
577 return (CUPSD_EVENT_PRINTER_STOPPED
);
578 else if (!strcmp(name
, "printer-finishings-changed"))
579 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
580 else if (!strcmp(name
, "printer-media-changed"))
581 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
582 else if (!strcmp(name
, "printer-added"))
583 return (CUPSD_EVENT_PRINTER_ADDED
);
584 else if (!strcmp(name
, "printer-deleted"))
585 return (CUPSD_EVENT_PRINTER_DELETED
);
586 else if (!strcmp(name
, "printer-modified"))
587 return (CUPSD_EVENT_PRINTER_MODIFIED
);
588 else if (!strcmp(name
, "printer-state-changed"))
589 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
590 else if (!strcmp(name
, "printer-config-changed"))
591 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
592 else if (!strcmp(name
, "printer-changed"))
593 return (CUPSD_EVENT_PRINTER_CHANGED
);
594 else if (!strcmp(name
, "job-state"))
595 return (CUPSD_EVENT_JOB_STATE
);
596 else if (!strcmp(name
, "job-created"))
597 return (CUPSD_EVENT_JOB_CREATED
);
598 else if (!strcmp(name
, "job-completed"))
599 return (CUPSD_EVENT_JOB_COMPLETED
);
600 else if (!strcmp(name
, "job-stopped"))
601 return (CUPSD_EVENT_JOB_STOPPED
);
602 else if (!strcmp(name
, "job-config-changed"))
603 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
604 else if (!strcmp(name
, "job-progress"))
605 return (CUPSD_EVENT_JOB_PROGRESS
);
606 else if (!strcmp(name
, "job-state-changed"))
607 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
608 else if (!strcmp(name
, "server-restarted"))
609 return (CUPSD_EVENT_SERVER_RESTARTED
);
610 else if (!strcmp(name
, "server-started"))
611 return (CUPSD_EVENT_SERVER_STARTED
);
612 else if (!strcmp(name
, "server-stopped"))
613 return (CUPSD_EVENT_SERVER_STOPPED
);
614 else if (!strcmp(name
, "server-audit"))
615 return (CUPSD_EVENT_SERVER_AUDIT
);
617 return (CUPSD_EVENT_NONE
);
622 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
626 cupsdExpireSubscriptions(
627 cupsd_printer_t
*dest
, /* I - Printer, if any */
628 cupsd_job_t
*job
) /* I - Job, if any */
630 cupsd_subscription_t
*sub
; /* Current subscription */
631 int update
; /* Update subscriptions.conf? */
632 time_t curtime
; /* Current time */
635 curtime
= time(NULL
);
638 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
640 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
641 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
642 (dest
&& sub
->dest
== dest
) ||
643 (job
&& sub
->job
== job
))
645 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
648 cupsdDeleteSubscription(sub
, 0);
654 cupsdSaveAllSubscriptions();
659 * 'cupsdFindSubscription()' - Find a subscription by ID.
662 cupsd_subscription_t
* /* O - Subscription object */
663 cupsdFindSubscription(int id
) /* I - Subscription ID */
665 cupsd_subscription_t sub
; /* Subscription template */
670 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
675 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
679 cupsdLoadAllSubscriptions(void)
681 int i
; /* Looping var */
682 cups_file_t
*fp
; /* subscriptions.conf file */
683 int linenum
; /* Current line number */
684 char line
[1024], /* Line from file */
685 *value
, /* Pointer to value */
686 *valueptr
; /* Pointer into value */
687 cupsd_subscription_t
*sub
; /* Current subscription */
688 int hex
; /* Non-zero if reading hex data */
689 int delete_sub
; /* Delete subscription? */
693 * Open the subscriptions.conf file...
696 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
697 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
700 cupsdLogMessage(CUPSD_LOG_ERROR
,
701 "LoadAllSubscriptions: Unable to open %s - %s", line
,
707 * Read all of the lines from the file...
714 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
716 if (!strcasecmp(line
, "NextSubscriptionId") && value
)
719 * NextSubscriptionId NNN
723 if (i
>= NextSubscriptionId
&& i
> 0)
724 NextSubscriptionId
= i
;
726 else if (!strcasecmp(line
, "<Subscription"))
732 if (!sub
&& value
&& isdigit(value
[0] & 255))
734 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
739 cupsdLogMessage(CUPSD_LOG_ERROR
,
740 "Syntax error on line %d of subscriptions.conf.",
745 else if (!strcasecmp(line
, "</Subscription>"))
749 cupsdLogMessage(CUPSD_LOG_ERROR
,
750 "Syntax error on line %d of subscriptions.conf.",
756 cupsdDeleteSubscription(sub
, 0);
763 cupsdLogMessage(CUPSD_LOG_ERROR
,
764 "Syntax error on line %d of subscriptions.conf.",
768 else if (!strcasecmp(line
, "Events"))
772 * Events name name name ...
777 cupsdLogMessage(CUPSD_LOG_ERROR
,
778 "Syntax error on line %d of subscriptions.conf.",
786 * Separate event names...
789 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
791 while (isspace(*valueptr
& 255))
795 * See if the name exists...
798 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
800 cupsdLogMessage(CUPSD_LOG_ERROR
,
801 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
809 else if (!strcasecmp(line
, "Owner"))
816 cupsdSetString(&sub
->owner
, value
);
819 cupsdLogMessage(CUPSD_LOG_ERROR
,
820 "Syntax error on line %d of subscriptions.conf.",
825 else if (!strcasecmp(line
, "Recipient"))
832 cupsdSetString(&sub
->recipient
, value
);
835 cupsdLogMessage(CUPSD_LOG_ERROR
,
836 "Syntax error on line %d of subscriptions.conf.",
841 else if (!strcasecmp(line
, "JobId"))
847 if (value
&& isdigit(*value
& 255))
849 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
851 cupsdLogMessage(CUPSD_LOG_ERROR
,
852 "Job %s not found on line %d of subscriptions.conf.",
859 cupsdLogMessage(CUPSD_LOG_ERROR
,
860 "Syntax error on line %d of subscriptions.conf.",
865 else if (!strcasecmp(line
, "PrinterName"))
873 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
875 cupsdLogMessage(CUPSD_LOG_ERROR
,
876 "Printer \'%s\' not found on line %d of subscriptions.conf.",
883 cupsdLogMessage(CUPSD_LOG_ERROR
,
884 "Syntax error on line %d of subscriptions.conf.",
889 else if (!strcasecmp(line
, "UserData"))
892 * UserData encoded-string
897 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
899 if (*valueptr
== '<' && !hex
)
907 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
909 if (isdigit(valueptr
[0]))
910 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
912 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
914 if (isdigit(valueptr
[1]))
915 sub
->user_data
[i
] |= valueptr
[1] - '0';
917 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
921 if (*valueptr
== '>')
931 sub
->user_data
[i
] = *valueptr
++;
936 cupsdLogMessage(CUPSD_LOG_ERROR
,
937 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
941 sub
->user_data_len
= i
;
945 cupsdLogMessage(CUPSD_LOG_ERROR
,
946 "Syntax error on line %d of subscriptions.conf.",
951 else if (!strcasecmp(line
, "LeaseDuration"))
957 if (value
&& isdigit(*value
& 255))
959 sub
->lease
= atoi(value
);
960 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
964 cupsdLogMessage(CUPSD_LOG_ERROR
,
965 "Syntax error on line %d of subscriptions.conf.",
970 else if (!strcasecmp(line
, "Interval"))
976 if (value
&& isdigit(*value
& 255))
977 sub
->interval
= atoi(value
);
980 cupsdLogMessage(CUPSD_LOG_ERROR
,
981 "Syntax error on line %d of subscriptions.conf.",
986 else if (!strcasecmp(line
, "ExpirationTime"))
992 if (value
&& isdigit(*value
& 255))
993 sub
->expire
= atoi(value
);
996 cupsdLogMessage(CUPSD_LOG_ERROR
,
997 "Syntax error on line %d of subscriptions.conf.",
1002 else if (!strcasecmp(line
, "NextEventId"))
1008 if (value
&& isdigit(*value
& 255))
1009 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1012 cupsdLogMessage(CUPSD_LOG_ERROR
,
1013 "Syntax error on line %d of subscriptions.conf.",
1021 * Something else we don't understand...
1024 cupsdLogMessage(CUPSD_LOG_ERROR
,
1025 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1035 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1039 cupsdSaveAllSubscriptions(void)
1041 int i
; /* Looping var */
1042 cups_file_t
*fp
; /* subscriptions.conf file */
1043 char temp
[1024]; /* Temporary string */
1044 char backup
[1024]; /* subscriptions.conf.O file */
1045 cupsd_subscription_t
*sub
; /* Current subscription */
1046 time_t curtime
; /* Current time */
1047 struct tm
*curdate
; /* Current date */
1048 unsigned mask
; /* Current event mask */
1049 const char *name
; /* Current event name */
1050 int hex
; /* Non-zero if we are writing hex data */
1054 * Create the subscriptions.conf file...
1057 snprintf(temp
, sizeof(temp
), "%s/subscriptions.conf", ServerRoot
);
1058 snprintf(backup
, sizeof(backup
), "%s/subscriptions.conf.O", ServerRoot
);
1060 if (rename(temp
, backup
))
1062 if (errno
!= ENOENT
)
1063 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup subscriptions.conf - %s",
1067 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1069 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save subscriptions.conf - %s",
1072 if (rename(backup
, temp
))
1073 cupsdLogMessage(CUPSD_LOG_ERROR
,
1074 "Unable to restore subscriptions.conf - %s",
1079 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1082 * Restrict access to the file...
1085 fchown(cupsFileNumber(fp
), getuid(), Group
);
1086 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1089 * Write a small header to the file...
1092 curtime
= time(NULL
);
1093 curdate
= localtime(&curtime
);
1094 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1096 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1097 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1099 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1102 * Write every subscription known to the system...
1105 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1107 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1109 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1111 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1114 * Simple event list...
1117 cupsFilePrintf(fp
, "Events %s\n", name
);
1122 * Complex event list...
1125 cupsFilePuts(fp
, "Events");
1127 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1128 if (sub
->mask
& mask
)
1129 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1131 cupsFilePuts(fp
, "\n");
1135 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1137 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1139 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1141 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1143 if (sub
->user_data_len
> 0)
1145 cupsFilePuts(fp
, "UserData ");
1147 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1149 if (sub
->user_data
[i
] < ' ' ||
1150 sub
->user_data
[i
] > 0x7f ||
1151 sub
->user_data
[i
] == '<')
1155 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1159 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1165 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1169 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1174 cupsFilePuts(fp
, ">\n");
1176 cupsFilePutChar(fp
, '\n');
1179 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1180 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1181 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1182 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1184 cupsFilePuts(fp
, "</Subscription>\n");
1192 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1196 cupsdStopAllNotifiers(void)
1198 cupsd_subscription_t
*sub
; /* Current subscription */
1202 * See if we have started any notifiers...
1205 if (!NotifierStatusBuffer
)
1209 * Yes, kill any processes that are left...
1212 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1214 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1217 cupsdEndProcess(sub
->pid
, 0);
1224 * Close the status pipes...
1227 if (NotifierPipes
[0] >= 0)
1229 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1230 "cupsdStopAllNotifiers: Removing fd %d from InputSet...",
1232 FD_CLR(NotifierPipes
[0], InputSet
);
1234 cupsdStatBufDelete(NotifierStatusBuffer
);
1236 close(NotifierPipes
[0]);
1237 close(NotifierPipes
[1]);
1239 NotifierPipes
[0] = -1;
1240 NotifierPipes
[1] = -1;
1241 NotifierStatusBuffer
= NULL
;
1247 * 'cupsdUpdateNotifierStatus()' - Read messages from notifiers.
1251 cupsdUpdateNotifierStatus(void)
1253 char message
[1024]; /* Pointer to message text */
1254 int loglevel
; /* Log level for message */
1257 while (cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1258 message
, sizeof(message
)))
1259 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1265 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1268 static int /* O - Result of comparison */
1269 cupsd_compare_subscriptions(
1270 cupsd_subscription_t
*first
, /* I - First subscription object */
1271 cupsd_subscription_t
*second
, /* I - Second subscription object */
1272 void *unused
) /* I - Unused user data pointer */
1276 return (first
->id
- second
->id
);
1281 * 'cupsd_delete_event()' - Delete a single event...
1283 * Oldest events must be deleted first, otherwise the subscription cache
1284 * flushing code will not work properly.
1288 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1294 ippDelete(event
->attrs
);
1301 * 'cupsd_send_dbus()' - Send a DBUS notification...
1305 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1306 cupsd_printer_t
*dest
,/* I - Destination, if any */
1307 cupsd_job_t
*job
) /* I - Job, if any */
1309 DBusError error
; /* Error, if any */
1310 DBusMessage
*message
; /* Message to send */
1311 DBusMessageIter iter
; /* Iterator for message data */
1312 const char *what
; /* What to send */
1313 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1317 * Figure out what to send, if anything...
1320 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1321 what
= "PrinterAdded";
1322 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1323 what
= "PrinterRemoved";
1324 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1325 what
= "QueueChanged";
1326 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1327 what
= "JobQueuedLocal";
1328 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1329 job
->state_value
== IPP_JOB_PROCESSING
)
1330 what
= "JobStartedLocal";
1335 * Verify connection to DBUS server...
1338 if (con
&& !dbus_connection_get_is_connected(con
))
1340 dbus_connection_unref(con
);
1346 dbus_error_init(&error
);
1348 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1351 dbus_error_free(&error
);
1357 * Create and send the new message...
1360 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1361 "com.redhat.PrinterSpooler", what
);
1363 dbus_message_append_iter_init(message
, &iter
);
1365 dbus_message_iter_append_string(&iter
, &(dest
->name
));
1368 dbus_message_iter_append_uint32(&iter
, &(job
->id
));
1369 dbus_message_iter_append_string(&iter
, &(job
->username
));
1372 dbus_connection_send(con
, message
, NULL
);
1373 dbus_connection_flush(con
);
1374 dbus_message_unref(message
);
1376 #endif /* HAVE_DBUS */
1380 * 'cupsd_send_notification()' - Send a notification for the specified event.
1384 cupsd_send_notification(
1385 cupsd_subscription_t
*sub
, /* I - Subscription object */
1386 cupsd_event_t
*event
) /* I - Event to send */
1388 ipp_state_t state
; /* IPP event state */
1391 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1392 "cupsd_send_notification(sub=%p(%d), event=%p(%s))\n",
1393 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1396 * Allocate the events array as needed...
1401 sub
->events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
1405 cupsdLogMessage(CUPSD_LOG_CRIT
,
1406 "Unable to allocate memory for subscription #%d!",
1413 * Purge an old event as needed...
1416 if (sub
->num_events
>= MaxEvents
)
1419 * Purge the oldest event in the cache...
1422 cupsd_delete_event(sub
->events
[0]);
1425 sub
->first_event_id
++;
1427 memmove(sub
->events
, sub
->events
+ 1,
1428 sub
->num_events
* sizeof(cupsd_event_t
*));
1432 * Add the event to the subscription. Since the events array is
1433 * always MaxEvents in length, and since we will have already
1434 * removed an event from the subscription cache if we hit the
1435 * event cache limit, we don't need to check for overflow here...
1438 sub
->events
[sub
->num_events
] = event
;
1442 * Deliver the event...
1450 cupsd_start_notifier(sub
);
1452 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "sub->pipe=%d", sub
->pipe
);
1457 event
->attrs
->state
= IPP_IDLE
;
1459 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1460 if (state
== IPP_ERROR
)
1463 if (state
== IPP_ERROR
)
1468 * Notifier died, try restarting it...
1471 cupsdLogMessage(CUPSD_LOG_WARN
,
1472 "Notifier for subscription %d (%s) went away, "
1474 sub
->id
, sub
->recipient
);
1475 cupsdEndProcess(sub
->pid
, 0);
1482 cupsdLogMessage(CUPSD_LOG_ERROR
,
1483 "Unable to send event for subscription %d (%s)!",
1484 sub
->id
, sub
->recipient
);
1488 * If we get this far, break out of the loop...
1496 * Bump the event sequence number...
1499 sub
->next_event_id
++;
1504 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1508 cupsd_start_notifier(
1509 cupsd_subscription_t
*sub
) /* I - Subscription object */
1511 int pid
; /* Notifier process ID */
1512 int fds
[2]; /* Pipe file descriptors */
1513 char *argv
[4], /* Command-line arguments */
1514 *envp
[MAX_ENV
], /* Environment variables */
1515 user_data
[128], /* Base-64 encoded user data */
1516 scheme
[256], /* notify-recipient-uri scheme */
1517 *ptr
, /* Pointer into scheme */
1518 command
[1024]; /* Notifier command */
1522 * Extract the scheme name from the recipient URI and point to the
1523 * notifier program...
1526 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1527 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1530 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1533 * Base-64 encode the user data...
1536 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1537 sub
->user_data_len
);
1540 * Setup the argument array...
1544 argv
[1] = sub
->recipient
;
1545 argv
[2] = user_data
;
1549 * Setup the environment...
1552 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1555 * Create pipes as needed...
1558 if (!NotifierStatusBuffer
)
1561 * Create the status pipe...
1564 if (cupsdOpenPipe(NotifierPipes
))
1566 cupsdLogMessage(CUPSD_LOG_ERROR
,
1567 "Unable to create pipes for notifier status - %s",
1572 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1574 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1575 "start_notifier: Adding fd %d to InputSet...",
1578 FD_SET(NotifierPipes
[0], InputSet
);
1581 if (cupsdOpenPipe(fds
))
1583 cupsdLogMessage(CUPSD_LOG_ERROR
,
1584 "Unable to create pipes for notifier %s - %s",
1585 scheme
, strerror(errno
));
1590 * Make sure the delivery pipe is non-blocking...
1593 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1596 * Create the notifier process...
1599 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1603 * Error - can't fork!
1606 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1607 scheme
, strerror(errno
));
1609 cupsdClosePipe(fds
);
1614 * Fork successful - return the PID...
1617 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1630 * End of "$Id: subscriptions.c 5991 2006-09-29 02:26:29Z mike $".