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_all_events() - Delete all cached events.
40 * cupsd_delete_event() - Delete a single event...
41 * cupsd_send_dbus() - Send a DBUS notification...
42 * cupsd_send_notification() - Send a notification for the specified
44 * cupsd_start_notifier() - Start a notifier subprocess...
48 * Include necessary headers...
53 # include <dbus/dbus.h>
54 #endif /* HAVE_DBUS */
61 static int cupsd_compare_subscriptions(cupsd_subscription_t
*first
,
62 cupsd_subscription_t
*second
,
64 static void cupsd_delete_all_events(void);
65 static void cupsd_delete_event(cupsd_event_t
*event
);
67 static void cupsd_send_dbus(cupsd_eventmask_t event
, cupsd_printer_t
*dest
,
69 #endif /* HAVE_DBUS */
70 static void cupsd_send_notification(cupsd_subscription_t
*sub
,
71 cupsd_event_t
*event
);
72 static void cupsd_start_notifier(cupsd_subscription_t
*sub
);
76 * 'cupsdAddEvent()' - Add an event to the global event cache.
81 cupsd_eventmask_t event
, /* I - Event */
82 cupsd_printer_t
*dest
, /* I - Printer associated with event */
83 cupsd_job_t
*job
, /* I - Job associated with event */
84 const char *text
, /* I - Notification text */
85 ...) /* I - Additional arguments as needed */
87 va_list ap
; /* Pointer to additional arguments */
88 char ftext
[1024]; /* Formatted text buffer */
89 ipp_attribute_t
*attr
; /* Printer/job attribute */
90 cupsd_event_t
*temp
; /* New event pointer */
91 cupsd_subscription_t
*sub
; /* Current subscription */
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 * Allocate memory for the event cache as needed...
122 Events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
127 cupsdLogMessage(CUPSD_LOG_CRIT
,
128 "Unable to allocate memory for event cache - %s",
135 * Then loop through the subscriptions and add the event to the corresponding
139 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), temp
= NULL
;
141 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
144 * Check if this subscription requires this event...
147 if ((sub
->mask
& event
) != 0 &&
148 (sub
->dest
== dest
|| !sub
->dest
) &&
149 (sub
->job
== job
|| !sub
->job
))
158 * Create the new event record...
161 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
163 cupsdLogMessage(CUPSD_LOG_CRIT
,
164 "Unable to allocate memory for event - %s",
170 temp
->time
= time(NULL
);
171 temp
->attrs
= ippNew();
176 * Add common event notification attributes...
179 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
180 "notify-subscription-id", sub
->id
);
182 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
183 "notify-subscribed-event", NULL
, cupsdEventName(event
));
185 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
186 "printer-up-time", time(NULL
));
189 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
192 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_TEXT
,
193 "notify-text", NULL
, ftext
);
198 * Add printer attributes...
201 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
202 "notify-printer-uri", NULL
, dest
->uri
);
204 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
205 "printer-name", NULL
, dest
->name
);
207 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
208 "printer-state", dest
->state
);
210 if (dest
->num_reasons
== 0)
211 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
212 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
213 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
215 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
216 IPP_TAG_KEYWORD
, "printer-state-reasons",
217 dest
->num_reasons
, NULL
,
218 (const char * const *)dest
->reasons
);
220 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
221 "printer-is-accepting-jobs", dest
->accepting
);
227 * Add job attributes...
230 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
231 "notify-job-id", job
->id
);
232 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
233 "job-state", job
->state_value
);
235 if ((attr
= ippFindAttribute(job
->attrs
, "job-name",
236 IPP_TAG_NAME
)) != NULL
)
237 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
238 "job-name", NULL
, attr
->values
[0].string
.text
);
240 switch (job
->state_value
)
242 case IPP_JOB_PENDING
:
243 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
244 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
245 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
248 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
249 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
254 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
255 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
256 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
257 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
258 "job-hold-until-specified");
260 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
261 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
265 case IPP_JOB_PROCESSING
:
266 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
267 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
271 case IPP_JOB_STOPPED
:
272 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
273 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
277 case IPP_JOB_CANCELLED
:
278 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
279 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
280 "job-canceled-by-user");
283 case IPP_JOB_ABORTED
:
284 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
285 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
286 "aborted-by-system");
289 case IPP_JOB_COMPLETED
:
290 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
291 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
292 "job-completed-successfully");
296 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
297 "job-impressions-completed",
298 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
302 * Purge an old event as needed...
305 if (NumEvents
>= MaxEvents
)
308 * Purge the oldest event in the cache...
311 cupsd_delete_event(Events
[0]);
315 memmove(Events
, Events
+ 1, NumEvents
* sizeof(cupsd_event_t
*));
319 * Add the new event to the main cache...
322 Events
[NumEvents
] = temp
;
327 * Send the notification for this subscription...
330 cupsd_send_notification(sub
, temp
);
335 cupsdSaveAllSubscriptions();
337 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
338 cupsdEventName(event
));
343 * 'cupsdAddSubscription()' - Add a new subscription object.
346 cupsd_subscription_t
* /* O - New subscription object */
347 cupsdAddSubscription(
348 unsigned mask
, /* I - Event mask */
349 cupsd_printer_t
*dest
, /* I - Printer, if any */
350 cupsd_job_t
*job
, /* I - Job, if any */
351 const char *uri
, /* I - notify-recipient-uri, if any */
352 int sub_id
) /* I - notify-subscription-id or 0 */
354 cupsd_subscription_t
*temp
; /* New subscription object */
357 cupsdLogMessage(CUPSD_LOG_DEBUG
,
358 "cupsdAddSubscription(mask=%x, dest=%p(%s), job=%p(%d), "
360 mask
, dest
, dest
? dest
->name
: "", job
, job
? job
->id
: 0,
364 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
369 cupsdLogMessage(CUPSD_LOG_CRIT
,
370 "Unable to allocate memory for subscriptions - %s",
376 * Limit the number of subscriptions...
379 if (cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
383 * Allocate memory for this subscription...
386 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
388 cupsdLogMessage(CUPSD_LOG_CRIT
,
389 "Unable to allocate memory for subscription object - %s",
395 * Fill in common data...
402 if (sub_id
>= NextSubscriptionId
)
403 NextSubscriptionId
= sub_id
+ 1;
407 temp
->id
= NextSubscriptionId
;
409 NextSubscriptionId
++;
416 temp
->first_event_id
= 1;
417 temp
->next_event_id
= 1;
419 cupsdSetString(&(temp
->recipient
), uri
);
422 * Add the subscription to the array...
425 cupsArrayAdd(Subscriptions
, temp
);
432 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
436 cupsdDeleteAllSubscriptions(void)
438 cupsd_subscription_t
*sub
; /* Subscription */
441 cupsd_delete_all_events();
446 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
448 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
449 cupsdDeleteSubscription(sub
, 0);
451 cupsArrayDelete(Subscriptions
);
452 Subscriptions
= NULL
;
457 * 'cupsdDeleteSubscription()' - Delete a subscription object.
461 cupsdDeleteSubscription(
462 cupsd_subscription_t
*sub
, /* I - Subscription object */
463 int update
) /* I - 1 = update subscriptions.conf */
466 * Close the pipe to the notifier as needed...
473 * Remove subscription from array...
476 cupsArrayRemove(Subscriptions
, sub
);
482 cupsdClearString(&(sub
->owner
));
483 cupsdClearString(&(sub
->recipient
));
491 * Update the subscriptions as needed...
495 cupsdSaveAllSubscriptions();
500 * 'cupsdEventName()' - Return a single event name.
503 const char * /* O - Event name */
505 cupsd_eventmask_t event
) /* I - Event value */
512 case CUPSD_EVENT_PRINTER_RESTARTED
:
513 return ("printer-restarted");
515 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
516 return ("printer-shutdown");
518 case CUPSD_EVENT_PRINTER_STOPPED
:
519 return ("printer-stopped");
521 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
522 return ("printer-finishings-changed");
524 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
525 return ("printer-media-changed");
527 case CUPSD_EVENT_PRINTER_ADDED
:
528 return ("printer-added");
530 case CUPSD_EVENT_PRINTER_DELETED
:
531 return ("printer-deleted");
533 case CUPSD_EVENT_PRINTER_MODIFIED
:
534 return ("printer-modified");
536 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
537 return ("printer-state-changed");
539 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
540 return ("printer-config-changed");
542 case CUPSD_EVENT_PRINTER_CHANGED
:
543 return ("printer-changed");
545 case CUPSD_EVENT_JOB_CREATED
:
546 return ("job-created");
548 case CUPSD_EVENT_JOB_COMPLETED
:
549 return ("job-completed");
551 case CUPSD_EVENT_JOB_STOPPED
:
552 return ("job-stopped");
554 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
555 return ("job-config-changed");
557 case CUPSD_EVENT_JOB_PROGRESS
:
558 return ("job-progress");
560 case CUPSD_EVENT_JOB_STATE
:
561 return ("job-state");
563 case CUPSD_EVENT_JOB_STATE_CHANGED
:
564 return ("job-state-changed");
566 case CUPSD_EVENT_SERVER_RESTARTED
:
567 return ("server-restarted");
569 case CUPSD_EVENT_SERVER_STARTED
:
570 return ("server-started");
572 case CUPSD_EVENT_SERVER_STOPPED
:
573 return ("server-stopped");
575 case CUPSD_EVENT_SERVER_AUDIT
:
576 return ("server-audit");
578 case CUPSD_EVENT_ALL
:
585 * 'cupsdEventValue()' - Return the event mask value for a name.
588 cupsd_eventmask_t
/* O - Event mask value */
589 cupsdEventValue(const char *name
) /* I - Name of event */
591 if (!strcmp(name
, "all"))
592 return (CUPSD_EVENT_ALL
);
593 else if (!strcmp(name
, "printer-restarted"))
594 return (CUPSD_EVENT_PRINTER_RESTARTED
);
595 else if (!strcmp(name
, "printer-shutdown"))
596 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
597 else if (!strcmp(name
, "printer-stopped"))
598 return (CUPSD_EVENT_PRINTER_STOPPED
);
599 else if (!strcmp(name
, "printer-finishings-changed"))
600 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
601 else if (!strcmp(name
, "printer-media-changed"))
602 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
603 else if (!strcmp(name
, "printer-added"))
604 return (CUPSD_EVENT_PRINTER_ADDED
);
605 else if (!strcmp(name
, "printer-deleted"))
606 return (CUPSD_EVENT_PRINTER_DELETED
);
607 else if (!strcmp(name
, "printer-modified"))
608 return (CUPSD_EVENT_PRINTER_MODIFIED
);
609 else if (!strcmp(name
, "printer-state-changed"))
610 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
611 else if (!strcmp(name
, "printer-config-changed"))
612 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
613 else if (!strcmp(name
, "printer-changed"))
614 return (CUPSD_EVENT_PRINTER_CHANGED
);
615 else if (!strcmp(name
, "job-state"))
616 return (CUPSD_EVENT_JOB_STATE
);
617 else if (!strcmp(name
, "job-created"))
618 return (CUPSD_EVENT_JOB_CREATED
);
619 else if (!strcmp(name
, "job-completed"))
620 return (CUPSD_EVENT_JOB_COMPLETED
);
621 else if (!strcmp(name
, "job-stopped"))
622 return (CUPSD_EVENT_JOB_STOPPED
);
623 else if (!strcmp(name
, "job-config-changed"))
624 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
625 else if (!strcmp(name
, "job-progress"))
626 return (CUPSD_EVENT_JOB_PROGRESS
);
627 else if (!strcmp(name
, "job-state-changed"))
628 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
629 else if (!strcmp(name
, "server-restarted"))
630 return (CUPSD_EVENT_SERVER_RESTARTED
);
631 else if (!strcmp(name
, "server-started"))
632 return (CUPSD_EVENT_SERVER_STARTED
);
633 else if (!strcmp(name
, "server-stopped"))
634 return (CUPSD_EVENT_SERVER_STOPPED
);
635 else if (!strcmp(name
, "server-audit"))
636 return (CUPSD_EVENT_SERVER_AUDIT
);
638 return (CUPSD_EVENT_NONE
);
643 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
647 cupsdExpireSubscriptions(
648 cupsd_printer_t
*dest
, /* I - Printer, if any */
649 cupsd_job_t
*job
) /* I - Job, if any */
651 cupsd_subscription_t
*sub
; /* Current subscription */
652 int update
; /* Update subscriptions.conf? */
653 time_t curtime
; /* Current time */
656 curtime
= time(NULL
);
659 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
661 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
662 if ((!sub
->job
&& !dest
&& sub
->expire
&& sub
->expire
<= curtime
) ||
663 (dest
&& sub
->dest
== dest
) ||
664 (job
&& sub
->job
== job
))
666 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...",
669 cupsdDeleteSubscription(sub
, 0);
675 cupsdSaveAllSubscriptions();
680 * 'cupsdFindSubscription()' - Find a subscription by ID.
683 cupsd_subscription_t
* /* O - Subscription object */
684 cupsdFindSubscription(int id
) /* I - Subscription ID */
686 cupsd_subscription_t sub
; /* Subscription template */
691 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
696 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
700 cupsdLoadAllSubscriptions(void)
702 int i
; /* Looping var */
703 cups_file_t
*fp
; /* subscriptions.conf file */
704 int linenum
; /* Current line number */
705 char line
[1024], /* Line from file */
706 *value
, /* Pointer to value */
707 *valueptr
; /* Pointer into value */
708 cupsd_subscription_t
*sub
; /* Current subscription */
709 int hex
; /* Non-zero if reading hex data */
710 int delete_sub
; /* Delete subscription? */
714 * Open the subscriptions.conf file...
717 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
718 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
721 cupsdLogMessage(CUPSD_LOG_ERROR
,
722 "LoadAllSubscriptions: Unable to open %s - %s", line
,
728 * Read all of the lines from the file...
735 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
737 if (!strcasecmp(line
, "NextSubscriptionId") && value
)
740 * NextSubscriptionId NNN
744 if (i
>= NextSubscriptionId
&& i
> 0)
745 NextSubscriptionId
= i
;
747 else if (!strcasecmp(line
, "<Subscription"))
753 if (!sub
&& value
&& isdigit(value
[0] & 255))
755 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
760 cupsdLogMessage(CUPSD_LOG_ERROR
,
761 "Syntax error on line %d of subscriptions.conf.",
766 else if (!strcasecmp(line
, "</Subscription>"))
770 cupsdLogMessage(CUPSD_LOG_ERROR
,
771 "Syntax error on line %d of subscriptions.conf.",
777 cupsdDeleteSubscription(sub
, 0);
784 cupsdLogMessage(CUPSD_LOG_ERROR
,
785 "Syntax error on line %d of subscriptions.conf.",
789 else if (!strcasecmp(line
, "Events"))
793 * Events name name name ...
798 cupsdLogMessage(CUPSD_LOG_ERROR
,
799 "Syntax error on line %d of subscriptions.conf.",
807 * Separate event names...
810 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
812 while (isspace(*valueptr
& 255))
816 * See if the name exists...
819 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
821 cupsdLogMessage(CUPSD_LOG_ERROR
,
822 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
830 else if (!strcasecmp(line
, "Owner"))
837 cupsdSetString(&sub
->owner
, value
);
840 cupsdLogMessage(CUPSD_LOG_ERROR
,
841 "Syntax error on line %d of subscriptions.conf.",
846 else if (!strcasecmp(line
, "Recipient"))
853 cupsdSetString(&sub
->recipient
, value
);
856 cupsdLogMessage(CUPSD_LOG_ERROR
,
857 "Syntax error on line %d of subscriptions.conf.",
862 else if (!strcasecmp(line
, "JobId"))
868 if (value
&& isdigit(*value
& 255))
870 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
872 cupsdLogMessage(CUPSD_LOG_ERROR
,
873 "Job %s not found on line %d of subscriptions.conf.",
880 cupsdLogMessage(CUPSD_LOG_ERROR
,
881 "Syntax error on line %d of subscriptions.conf.",
886 else if (!strcasecmp(line
, "PrinterName"))
894 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
896 cupsdLogMessage(CUPSD_LOG_ERROR
,
897 "Printer \'%s\' not found on line %d of subscriptions.conf.",
904 cupsdLogMessage(CUPSD_LOG_ERROR
,
905 "Syntax error on line %d of subscriptions.conf.",
910 else if (!strcasecmp(line
, "UserData"))
913 * UserData encoded-string
918 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
920 if (*valueptr
== '<' && !hex
)
928 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
930 if (isdigit(valueptr
[0]))
931 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
933 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
935 if (isdigit(valueptr
[1]))
936 sub
->user_data
[i
] |= valueptr
[1] - '0';
938 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
942 if (*valueptr
== '>')
952 sub
->user_data
[i
] = *valueptr
++;
957 cupsdLogMessage(CUPSD_LOG_ERROR
,
958 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
962 sub
->user_data_len
= i
;
966 cupsdLogMessage(CUPSD_LOG_ERROR
,
967 "Syntax error on line %d of subscriptions.conf.",
972 else if (!strcasecmp(line
, "LeaseDuration"))
978 if (value
&& isdigit(*value
& 255))
980 sub
->lease
= atoi(value
);
981 sub
->expire
= sub
->lease
? time(NULL
) + sub
->lease
: 0;
985 cupsdLogMessage(CUPSD_LOG_ERROR
,
986 "Syntax error on line %d of subscriptions.conf.",
991 else if (!strcasecmp(line
, "Interval"))
997 if (value
&& isdigit(*value
& 255))
998 sub
->interval
= atoi(value
);
1001 cupsdLogMessage(CUPSD_LOG_ERROR
,
1002 "Syntax error on line %d of subscriptions.conf.",
1007 else if (!strcasecmp(line
, "ExpirationTime"))
1013 if (value
&& isdigit(*value
& 255))
1014 sub
->expire
= atoi(value
);
1017 cupsdLogMessage(CUPSD_LOG_ERROR
,
1018 "Syntax error on line %d of subscriptions.conf.",
1023 else if (!strcasecmp(line
, "NextEventId"))
1029 if (value
&& isdigit(*value
& 255))
1030 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1033 cupsdLogMessage(CUPSD_LOG_ERROR
,
1034 "Syntax error on line %d of subscriptions.conf.",
1042 * Something else we don't understand...
1045 cupsdLogMessage(CUPSD_LOG_ERROR
,
1046 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1056 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1060 cupsdSaveAllSubscriptions(void)
1062 int i
; /* Looping var */
1063 cups_file_t
*fp
; /* subscriptions.conf file */
1064 char temp
[1024]; /* Temporary string */
1065 char backup
[1024]; /* subscriptions.conf.O file */
1066 cupsd_subscription_t
*sub
; /* Current subscription */
1067 time_t curtime
; /* Current time */
1068 struct tm
*curdate
; /* Current date */
1069 unsigned mask
; /* Current event mask */
1070 const char *name
; /* Current event name */
1071 int hex
; /* Non-zero if we are writing hex data */
1075 * Create the subscriptions.conf file...
1078 snprintf(temp
, sizeof(temp
), "%s/subscriptions.conf", ServerRoot
);
1079 snprintf(backup
, sizeof(backup
), "%s/subscriptions.conf.O", ServerRoot
);
1081 if (rename(temp
, backup
))
1083 if (errno
!= ENOENT
)
1084 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup subscriptions.conf - %s",
1088 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1090 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save subscriptions.conf - %s",
1093 if (rename(backup
, temp
))
1094 cupsdLogMessage(CUPSD_LOG_ERROR
,
1095 "Unable to restore subscriptions.conf - %s",
1100 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1103 * Restrict access to the file...
1106 fchown(cupsFileNumber(fp
), getuid(), Group
);
1107 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1110 * Write a small header to the file...
1113 curtime
= time(NULL
);
1114 curdate
= localtime(&curtime
);
1115 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1117 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1118 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1120 cupsFilePrintf(fp
, "NextSubscriptionId %d\n", NextSubscriptionId
);
1123 * Write every subscription known to the system...
1126 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1128 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1130 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1132 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1135 * Simple event list...
1138 cupsFilePrintf(fp
, "Events %s\n", name
);
1143 * Complex event list...
1146 cupsFilePuts(fp
, "Events");
1148 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1149 if (sub
->mask
& mask
)
1150 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1152 cupsFilePuts(fp
, "\n");
1156 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1158 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1160 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1162 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1164 if (sub
->user_data_len
> 0)
1166 cupsFilePuts(fp
, "UserData ");
1168 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1170 if (sub
->user_data
[i
] < ' ' ||
1171 sub
->user_data
[i
] > 0x7f ||
1172 sub
->user_data
[i
] == '<')
1176 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1180 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1186 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1190 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1195 cupsFilePuts(fp
, ">\n");
1197 cupsFilePutChar(fp
, '\n');
1200 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1201 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1202 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1203 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1205 cupsFilePuts(fp
, "</Subscription>\n");
1213 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1217 cupsdStopAllNotifiers(void)
1219 cupsd_subscription_t
*sub
; /* Current subscription */
1223 * See if we have started any notifiers...
1226 if (!NotifierStatusBuffer
)
1230 * Yes, kill any processes that are left...
1233 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1235 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1238 cupsdEndProcess(sub
->pid
, 0);
1245 * Close the status pipes...
1248 if (NotifierPipes
[0] >= 0)
1250 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1251 "cupsdStopAllNotifiers: Removing fd %d from InputSet...",
1253 FD_CLR(NotifierPipes
[0], InputSet
);
1255 cupsdStatBufDelete(NotifierStatusBuffer
);
1257 close(NotifierPipes
[0]);
1258 close(NotifierPipes
[1]);
1260 NotifierPipes
[0] = -1;
1261 NotifierPipes
[1] = -1;
1262 NotifierStatusBuffer
= NULL
;
1268 * 'cupsdUpdateNotifierStatus()' - Read messages from notifiers.
1272 cupsdUpdateNotifierStatus(void)
1274 char *ptr
, /* Pointer to end of line in buffer */
1275 message
[1024]; /* Pointer to message text */
1276 int loglevel
; /* Log level for message */
1279 while ((ptr
= cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1280 message
, sizeof(message
))) != NULL
)
1281 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1287 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1290 static int /* O - Result of comparison */
1291 cupsd_compare_subscriptions(
1292 cupsd_subscription_t
*first
, /* I - First subscription object */
1293 cupsd_subscription_t
*second
, /* I - Second subscription object */
1294 void *unused
) /* I - Unused user data pointer */
1298 return (first
->id
- second
->id
);
1303 * 'cupsd_delete_all_events()' - Delete all cached events.
1307 cupsd_delete_all_events(void)
1309 int i
; /* Looping var */
1312 if (MaxEvents
<= 0 || !Events
)
1315 for (i
= 0; i
< NumEvents
; i
++)
1316 cupsd_delete_event(Events
[i
]);
1324 * 'cupsd_delete_event()' - Delete a single event...
1326 * Oldest events must be deleted first, otherwise the subscription cache
1327 * flushing code will not work properly.
1331 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1333 cupsd_subscription_t
*sub
; /* Current subscription */
1337 * Loop through the subscriptions and look for the event in the cache...
1340 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1342 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1345 * Only check the first event in the subscription cache, since the
1346 * caller will only delete the oldest event in the cache...
1349 if (sub
->num_events
> 0 && sub
->events
[0] == event
)
1352 * Remove this event...
1356 sub
->first_event_id
++;
1358 if (sub
->num_events
> 0)
1361 * Shift other events upward in cache...
1364 memmove(sub
->events
, sub
->events
+ 1,
1365 sub
->num_events
* sizeof(cupsd_event_t
*));
1374 ippDelete(event
->attrs
);
1381 * 'cupsd_send_dbus()' - Send a DBUS notification...
1385 cupsd_send_dbus(cupsd_eventmask_t event
,/* I - Event to send */
1386 cupsd_printer_t
*dest
,/* I - Destination, if any */
1387 cupsd_job_t
*job
) /* I - Job, if any */
1389 DBusError error
; /* Error, if any */
1390 DBusMessage
*message
; /* Message to send */
1391 DBusMessageIter iter
; /* Iterator for message data */
1392 const char *what
; /* What to send */
1393 static DBusConnection
*con
= NULL
; /* Connection to DBUS server */
1397 * Figure out what to send, if anything...
1400 if (event
& CUPSD_EVENT_PRINTER_ADDED
)
1401 what
= "PrinterAdded";
1402 else if (event
& CUPSD_EVENT_PRINTER_DELETED
)
1403 what
= "PrinterRemoved";
1404 else if (event
& CUPSD_EVENT_PRINTER_CHANGED
)
1405 what
= "QueueChanged";
1406 else if (event
& CUPSD_EVENT_JOB_CREATED
)
1407 what
= "JobQueuedLocal";
1408 else if ((event
& CUPSD_EVENT_JOB_STATE
) && job
&&
1409 job
->state_value
== IPP_JOB_PROCESSING
)
1410 what
= "JobStartedLocal";
1415 * Verify connection to DBUS server...
1418 if (con
&& !dbus_connection_get_is_connected(con
))
1420 dbus_connection_unref(con
);
1426 dbus_error_init(&error
);
1428 con
= dbus_bus_get(getuid() ? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
);
1431 dbus_error_free(&error
);
1437 * Create and send the new message...
1440 message
= dbus_message_new_signal("/com/redhat/PrinterSpooler",
1441 "com.redhat.PrinterSpooler", what
);
1443 dbus_message_iter_init_append(message
, &iter
);
1445 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &(dest
->name
));
1448 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_UINT32
, &(job
->id
));
1449 dbus_message_iter_append_basic(&iter
, DBUS_TYPE_STRING
, &(job
->username
));
1452 dbus_connection_send(con
, message
, NULL
);
1453 dbus_connection_flush(con
);
1454 dbus_message_unref(message
);
1456 #endif /* HAVE_DBUS */
1460 * 'cupsd_send_notification()' - Send a notification for the specified event.
1464 cupsd_send_notification(
1465 cupsd_subscription_t
*sub
, /* I - Subscription object */
1466 cupsd_event_t
*event
) /* I - Event to send */
1468 ipp_state_t state
; /* IPP event state */
1471 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1472 "cupsd_send_notification(sub=%p(%d), event=%p(%s))\n",
1473 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1476 * Allocate the events array as needed...
1481 sub
->events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
1485 cupsdLogMessage(CUPSD_LOG_CRIT
,
1486 "Unable to allocate memory for subscription #%d!",
1493 * Add the event to the subscription. Since the events array is
1494 * always MaxEvents in length, and since we will have already
1495 * removed an event from the subscription cache if we hit the
1496 * event cache limit, we don't need to check for overflow here...
1499 sub
->events
[sub
->num_events
] = event
;
1503 * Deliver the event...
1509 cupsd_start_notifier(sub
);
1511 cupsdLogMessage(CUPSD_LOG_DEBUG
, "sub->pipe=%d", sub
->pipe
);
1515 event
->attrs
->state
= IPP_IDLE
;
1517 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1518 if (state
== IPP_ERROR
)
1521 if (state
== IPP_ERROR
)
1522 cupsdLogMessage(CUPSD_LOG_ERROR
,
1523 "Unable to send event for subscription %d (%s)!",
1524 sub
->id
, sub
->recipient
);
1529 * Bump the event sequence number...
1532 sub
->next_event_id
++;
1537 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1541 cupsd_start_notifier(
1542 cupsd_subscription_t
*sub
) /* I - Subscription object */
1544 int pid
; /* Notifier process ID */
1545 int fds
[2]; /* Pipe file descriptors */
1546 int envc
; /* Number of environment variables */
1547 char *argv
[4], /* Command-line arguments */
1548 *envp
[MAX_ENV
], /* Environment variables */
1549 user_data
[128], /* Base-64 encoded user data */
1550 scheme
[256], /* notify-recipient-uri scheme */
1551 *ptr
, /* Pointer into scheme */
1552 command
[1024]; /* Notifier command */
1556 * Extract the scheme name from the recipient URI and point to the
1557 * notifier program...
1560 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1561 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1564 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1567 * Base-64 encode the user data...
1570 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1571 sub
->user_data_len
);
1574 * Setup the argument array...
1578 argv
[1] = sub
->recipient
;
1579 argv
[2] = user_data
;
1583 * Setup the environment...
1586 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1589 * Create pipes as needed...
1592 if (!NotifierStatusBuffer
)
1595 * Create the status pipe...
1598 if (cupsdOpenPipe(NotifierPipes
))
1600 cupsdLogMessage(CUPSD_LOG_ERROR
,
1601 "Unable to create pipes for notifier status - %s",
1606 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1608 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1609 "start_notifier: Adding fd %d to InputSet...",
1612 FD_SET(NotifierPipes
[0], InputSet
);
1615 if (cupsdOpenPipe(fds
))
1617 cupsdLogMessage(CUPSD_LOG_ERROR
,
1618 "Unable to create pipes for notifier %s - %s",
1619 scheme
, strerror(errno
));
1624 * Make sure the delivery pipe is non-blocking...
1627 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1630 * Create the notifier process...
1633 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1637 * Error - can't fork!
1640 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1641 scheme
, strerror(errno
));
1643 cupsdClosePipe(fds
);
1648 * Fork successful - return the PID...
1651 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",