2 * "$Id: subscriptions.c 4840 2005-11-14 21:53:30Z mike $"
4 * Subscription routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-2005 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_start_notifier() - Start a notifier subprocess...
46 * Include necessary headers...
56 static int cupsd_compare_subscriptions(cupsd_subscription_t
*first
,
57 cupsd_subscription_t
*second
,
59 static void cupsd_delete_event(cupsd_event_t
*event
);
60 static void cupsd_start_notifier(cupsd_subscription_t
*sub
);
64 * 'cupsdAddEvent()' - Add an event to the global event cache.
69 cupsd_eventmask_t event
, /* I - Event */
70 cupsd_printer_t
*dest
, /* I - Printer associated with event */
71 cupsd_job_t
*job
, /* I - Job associated with event */
72 const char *text
, /* I - Notification text */
73 ...) /* I - Additional arguments as needed */
75 va_list ap
; /* Pointer to additional arguments */
76 char ftext
[1024]; /* Formatted text buffer */
77 cupsd_event_t
*temp
; /* New event pointer */
78 cupsd_subscription_t
*sub
; /* Current subscription */
82 * Return if we aren't keeping events...
87 cupsdLogMessage(CUPSD_LOG_WARN
,
88 "cupsdAddEvent: Discarding %s event since MaxEvents is %d!",
89 cupsdEventName(event
), MaxEvents
);
94 * Allocate memory for the event cache as needed...
99 Events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
104 cupsdLogMessage(CUPSD_LOG_CRIT
,
105 "Unable to allocate memory for event cache - %s",
112 * Then loop through the subscriptions and add the event to the corresponding
116 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
), temp
= NULL
;
118 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
121 * Check if this subscription requires this event...
124 if ((sub
->mask
& event
) != 0 &&
125 (sub
->dest
== dest
|| !sub
->dest
) &&
126 (sub
->job
== job
|| !sub
->job
))
135 * Create the new event record...
138 if ((temp
= (cupsd_event_t
*)calloc(1, sizeof(cupsd_event_t
))) == NULL
)
140 cupsdLogMessage(CUPSD_LOG_CRIT
,
141 "Unable to allocate memory for event - %s",
147 temp
->time
= time(NULL
);
148 temp
->attrs
= ippNew();
153 * Add common event notification attributes...
156 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
157 "notify-subscription-id", sub
->id
);
159 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
160 "notify-subscribed-event", NULL
, cupsdEventName(event
));
162 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
163 "printer-up-time", time(NULL
));
166 vsnprintf(ftext
, sizeof(ftext
), text
, ap
);
169 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_KEYWORD
,
170 "notify-text", NULL
, ftext
);
175 * Add printer attributes...
178 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_URI
,
179 "notify-printer-uri", NULL
, dest
->uri
);
181 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_NAME
,
182 "printer-name", NULL
, dest
->name
);
184 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
185 "printer-state", dest
->state
);
187 if (dest
->num_reasons
== 0)
188 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
189 IPP_TAG_KEYWORD
, "printer-state-reasons", NULL
,
190 dest
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
192 ippAddStrings(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
193 IPP_TAG_KEYWORD
, "printer-state-reasons",
194 dest
->num_reasons
, NULL
,
195 (const char * const *)dest
->reasons
);
197 ippAddBoolean(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
198 "printer-is-accepting-jobs", dest
->accepting
);
204 * Add job attributes...
207 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
209 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_ENUM
,
210 "job-state", (int)job
->state
);
212 switch (job
->state
->values
[0].integer
)
214 case IPP_JOB_PENDING
:
215 if (dest
&& dest
->state
== IPP_PRINTER_STOPPED
)
216 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
217 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
220 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
221 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
226 if (ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_KEYWORD
) != NULL
||
227 ippFindAttribute(job
->attrs
, "job-hold-until", IPP_TAG_NAME
) != NULL
)
228 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
229 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
230 "job-hold-until-specified");
232 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
233 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
237 case IPP_JOB_PROCESSING
:
238 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
239 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
243 case IPP_JOB_STOPPED
:
244 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
245 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
249 case IPP_JOB_CANCELLED
:
250 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
251 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
252 "job-canceled-by-user");
255 case IPP_JOB_ABORTED
:
256 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
257 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
258 "aborted-by-system");
261 case IPP_JOB_COMPLETED
:
262 ippAddString(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
,
263 IPP_TAG_KEYWORD
, "job-state-reasons", NULL
,
264 "job-completed-successfully");
268 ippAddInteger(temp
->attrs
, IPP_TAG_EVENT_NOTIFICATION
, IPP_TAG_INTEGER
,
269 "job-impressions-completed",
270 job
->sheets
? job
->sheets
->values
[0].integer
: 0);
274 * Purge an old event as needed...
277 if (NumEvents
>= MaxEvents
)
280 * Purge the oldest event in the cache...
283 cupsd_delete_event(Events
[0]);
287 memmove(Events
, Events
+ 1, NumEvents
* sizeof(cupsd_event_t
*));
291 * Add the new event to the main cache...
294 Events
[NumEvents
] = temp
;
299 * Send the notification for this subscription...
302 cupsdSendNotification(sub
, temp
);
307 cupsdSaveAllSubscriptions();
309 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Discarding unused %s event...",
310 cupsdEventName(event
));
315 * 'cupsdAddSubscription()' - Add a new subscription object.
318 cupsd_subscription_t
* /* O - New subscription object */
319 cupsdAddSubscription(
320 unsigned mask
, /* I - Event mask */
321 cupsd_printer_t
*dest
, /* I - Printer, if any */
322 cupsd_job_t
*job
, /* I - Job, if any */
323 const char *uri
, /* I - notify-recipient-uri, if any */
324 int sub_id
) /* I - notify-subscription-id or 0 */
326 cupsd_subscription_t
*temp
; /* New subscription object */
329 cupsdLogMessage(CUPSD_LOG_DEBUG
,
330 "cupsdAddSubscription(mask=%x(%s), dest=%p(%s), job=%p(%d), uri=\"%s\")",
331 mask
, cupsdEventName(mask
), dest
, dest
? dest
->name
: "",
332 job
, job
? job
->id
: 0, uri
);
335 Subscriptions
= cupsArrayNew((cups_array_func_t
)cupsd_compare_subscriptions
,
340 cupsdLogMessage(CUPSD_LOG_CRIT
,
341 "Unable to allocate memory for subscriptions - %s",
347 * Limit the number of subscriptions...
350 if (cupsArrayCount(Subscriptions
) >= MaxSubscriptions
)
354 * Allocate memory for this subscription...
357 if ((temp
= calloc(1, sizeof(cupsd_subscription_t
))) == NULL
)
359 cupsdLogMessage(CUPSD_LOG_CRIT
,
360 "Unable to allocate memory for subscription object - %s",
366 * Fill in common data...
373 if (sub_id
>= NextSubscriptionId
)
374 NextSubscriptionId
= sub_id
+ 1;
378 temp
->id
= NextSubscriptionId
;
380 NextSubscriptionId
++;
387 temp
->first_event_id
= 1;
388 temp
->next_event_id
= 1;
390 cupsdSetString(&(temp
->recipient
), uri
);
393 * Add the subscription to the array...
396 cupsArrayAdd(Subscriptions
, temp
);
403 * 'cupsdDeleteAllEvents()' - Delete all cached events.
407 cupsdDeleteAllEvents(void)
409 int i
; /* Looping var */
412 if (MaxEvents
<= 0 || !Events
)
415 for (i
= 0; i
< NumEvents
; i
++)
416 cupsd_delete_event(Events
[i
]);
424 * 'cupsdDeleteAllSubscriptions()' - Delete all subscriptions.
428 cupsdDeleteAllSubscriptions(void)
430 cupsd_subscription_t
*sub
; /* Subscription */
436 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
438 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
439 cupsdDeleteSubscription(sub
, 0);
441 cupsArrayDelete(Subscriptions
);
442 Subscriptions
= NULL
;
447 * 'cupsdDeleteSubscription()' - Delete a subscription object.
451 cupsdDeleteSubscription(
452 cupsd_subscription_t
*sub
, /* I - Subscription object */
453 int update
) /* I - 1 = update subscriptions.conf */
456 * Close the pipe to the notifier as needed...
463 * Remove subscription from array...
466 cupsArrayRemove(Subscriptions
, sub
);
472 cupsdClearString(&(sub
->owner
));
473 cupsdClearString(&(sub
->recipient
));
481 * Update the subscriptions as needed...
485 cupsdSaveAllSubscriptions();
490 * 'cupsdEventName()' - Return a single event name.
493 const char * /* O - Event name */
495 cupsd_eventmask_t event
) /* I - Event value */
502 case CUPSD_EVENT_PRINTER_RESTARTED
:
503 return ("printer-restarted");
505 case CUPSD_EVENT_PRINTER_SHUTDOWN
:
506 return ("printer-shutdown");
508 case CUPSD_EVENT_PRINTER_STOPPED
:
509 return ("printer-stopped");
511 case CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
:
512 return ("printer-finishings-changed");
514 case CUPSD_EVENT_PRINTER_MEDIA_CHANGED
:
515 return ("printer-media-changed");
517 case CUPSD_EVENT_PRINTER_ADDED
:
518 return ("printer-added");
520 case CUPSD_EVENT_PRINTER_DELETED
:
521 return ("printer-deleted");
523 case CUPSD_EVENT_PRINTER_MODIFIED
:
524 return ("printer-modified");
526 case CUPSD_EVENT_PRINTER_STATE_CHANGED
:
527 return ("printer-state-changed");
529 case CUPSD_EVENT_PRINTER_CONFIG_CHANGED
:
530 return ("printer-config-changed");
532 case CUPSD_EVENT_PRINTER_CHANGED
:
533 return ("printer-changed");
535 case CUPSD_EVENT_JOB_CREATED
:
536 return ("job-created");
538 case CUPSD_EVENT_JOB_COMPLETED
:
539 return ("job-completed");
541 case CUPSD_EVENT_JOB_STOPPED
:
542 return ("job-stopped");
544 case CUPSD_EVENT_JOB_CONFIG_CHANGED
:
545 return ("job-config-changed");
547 case CUPSD_EVENT_JOB_PROGRESS
:
548 return ("job-progress");
550 case CUPSD_EVENT_JOB_STATE_CHANGED
:
551 return ("job-state-changed");
553 case CUPSD_EVENT_SERVER_RESTARTED
:
554 return ("server-restarted");
556 case CUPSD_EVENT_SERVER_STARTED
:
557 return ("server-started");
559 case CUPSD_EVENT_SERVER_STOPPED
:
560 return ("server-stopped");
562 case CUPSD_EVENT_SERVER_AUDIT
:
563 return ("server-audit");
565 case CUPSD_EVENT_ALL
:
572 * 'cupsdEventValue()' - Return the event mask value for a name.
575 cupsd_eventmask_t
/* O - Event mask value */
576 cupsdEventValue(const char *name
) /* I - Name of event */
578 if (!strcmp(name
, "all"))
579 return (CUPSD_EVENT_ALL
);
580 else if (!strcmp(name
, "printer-restarted"))
581 return (CUPSD_EVENT_PRINTER_RESTARTED
);
582 else if (!strcmp(name
, "printer-shutdown"))
583 return (CUPSD_EVENT_PRINTER_SHUTDOWN
);
584 else if (!strcmp(name
, "printer-stopped"))
585 return (CUPSD_EVENT_PRINTER_STOPPED
);
586 else if (!strcmp(name
, "printer-finishings-changed"))
587 return (CUPSD_EVENT_PRINTER_FINISHINGS_CHANGED
);
588 else if (!strcmp(name
, "printer-media-changed"))
589 return (CUPSD_EVENT_PRINTER_MEDIA_CHANGED
);
590 else if (!strcmp(name
, "printer-added"))
591 return (CUPSD_EVENT_PRINTER_ADDED
);
592 else if (!strcmp(name
, "printer-deleted"))
593 return (CUPSD_EVENT_PRINTER_DELETED
);
594 else if (!strcmp(name
, "printer-modified"))
595 return (CUPSD_EVENT_PRINTER_MODIFIED
);
596 else if (!strcmp(name
, "printer-state-changed"))
597 return (CUPSD_EVENT_PRINTER_STATE_CHANGED
);
598 else if (!strcmp(name
, "printer-config-changed"))
599 return (CUPSD_EVENT_PRINTER_CONFIG_CHANGED
);
600 else if (!strcmp(name
, "printer-changed"))
601 return (CUPSD_EVENT_PRINTER_CHANGED
);
602 else if (!strcmp(name
, "job-created"))
603 return (CUPSD_EVENT_JOB_CREATED
);
604 else if (!strcmp(name
, "job-completed"))
605 return (CUPSD_EVENT_JOB_COMPLETED
);
606 else if (!strcmp(name
, "job-stopped"))
607 return (CUPSD_EVENT_JOB_STOPPED
);
608 else if (!strcmp(name
, "job-config-changed"))
609 return (CUPSD_EVENT_JOB_CONFIG_CHANGED
);
610 else if (!strcmp(name
, "job-progress"))
611 return (CUPSD_EVENT_JOB_PROGRESS
);
612 else if (!strcmp(name
, "job-state-changed"))
613 return (CUPSD_EVENT_JOB_STATE_CHANGED
);
614 else if (!strcmp(name
, "server-restarted"))
615 return (CUPSD_EVENT_SERVER_RESTARTED
);
616 else if (!strcmp(name
, "server-started"))
617 return (CUPSD_EVENT_SERVER_STARTED
);
618 else if (!strcmp(name
, "server-stopped"))
619 return (CUPSD_EVENT_SERVER_STOPPED
);
620 else if (!strcmp(name
, "server-audit"))
621 return (CUPSD_EVENT_SERVER_AUDIT
);
623 return (CUPSD_EVENT_NONE
);
628 * 'cupsdExpireSubscriptions()' - Expire old subscription objects.
632 cupsdExpireSubscriptions(
633 cupsd_printer_t
*dest
, /* I - Printer, if any */
634 cupsd_job_t
*job
) /* I - Job, if any */
636 cupsd_subscription_t
*sub
; /* Current subscription */
637 int update
; /* Update subscriptions.conf? */
638 time_t curtime
; /* Current time */
641 curtime
= time(NULL
);
644 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
646 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
647 if (sub
->expire
<= curtime
||
648 (dest
&& sub
->dest
== dest
) ||
649 (job
&& sub
->job
== job
))
651 cupsdLogMessage(CUPSD_LOG_INFO
, "Subscription %d has expired...", sub
->id
);
653 cupsdDeleteSubscription(sub
, 0);
659 cupsdSaveAllSubscriptions();
664 * 'cupsdFindSubscription()' - Find a subscription by ID.
667 cupsd_subscription_t
* /* O - Subscription object */
668 cupsdFindSubscription(int id
) /* I - Subscription ID */
670 cupsd_subscription_t sub
; /* Subscription template */
675 return ((cupsd_subscription_t
*)cupsArrayFind(Subscriptions
, &sub
));
680 * 'cupsdLoadAllSubscriptions()' - Load all subscriptions from the .conf file.
684 cupsdLoadAllSubscriptions(void)
686 int i
; /* Looping var */
687 cups_file_t
*fp
; /* subscriptions.conf file */
688 int linenum
; /* Current line number */
689 char line
[1024], /* Line from file */
690 *value
, /* Pointer to value */
691 *valueptr
; /* Pointer into value */
692 cupsd_subscription_t
*sub
; /* Current subscription */
693 int hex
; /* Non-zero if reading hex data */
694 int delete_sub
; /* Delete subscription? */
698 * Open the subscriptions.conf file...
701 snprintf(line
, sizeof(line
), "%s/subscriptions.conf", ServerRoot
);
702 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
704 cupsdLogMessage(CUPSD_LOG_ERROR
,
705 "LoadAllSubscriptions: Unable to open %s - %s", line
,
711 * Read all of the lines from the file...
718 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
720 if (!strcasecmp(line
, "<Subscription"))
726 if (!sub
&& value
&& isdigit(value
[0] & 255))
728 sub
= cupsdAddSubscription(CUPSD_EVENT_NONE
, NULL
, NULL
, NULL
,
733 cupsdLogMessage(CUPSD_LOG_ERROR
,
734 "Syntax error on line %d of subscriptions.conf.",
739 else if (!strcasecmp(line
, "</Subscription>"))
743 cupsdLogMessage(CUPSD_LOG_ERROR
,
744 "Syntax error on line %d of subscriptions.conf.",
750 cupsdDeleteSubscription(sub
, 0);
757 cupsdLogMessage(CUPSD_LOG_ERROR
,
758 "Syntax error on line %d of subscriptions.conf.",
762 else if (!strcasecmp(line
, "Events"))
766 * Events name name name ...
771 cupsdLogMessage(CUPSD_LOG_ERROR
,
772 "Syntax error on line %d of subscriptions.conf.",
780 * Separate event names...
783 for (valueptr
= value
; !isspace(*valueptr
) && *valueptr
; valueptr
++);
785 while (isspace(*valueptr
& 255))
789 * See if the name exists...
792 if ((sub
->mask
|= cupsdEventValue(value
)) == CUPSD_EVENT_NONE
)
794 cupsdLogMessage(CUPSD_LOG_ERROR
,
795 "Unknown event name \'%s\' on line %d of subscriptions.conf.",
803 else if (!strcasecmp(line
, "Owner"))
810 cupsdSetString(&sub
->owner
, value
);
813 cupsdLogMessage(CUPSD_LOG_ERROR
,
814 "Syntax error on line %d of subscriptions.conf.",
819 else if (!strcasecmp(line
, "Recipient"))
826 cupsdSetString(&sub
->recipient
, value
);
829 cupsdLogMessage(CUPSD_LOG_ERROR
,
830 "Syntax error on line %d of subscriptions.conf.",
835 else if (!strcasecmp(line
, "JobId"))
841 if (value
&& isdigit(*value
& 255))
843 if ((sub
->job
= cupsdFindJob(atoi(value
))) == NULL
)
845 cupsdLogMessage(CUPSD_LOG_ERROR
,
846 "Job %s not found on line %d of subscriptions.conf.",
853 cupsdLogMessage(CUPSD_LOG_ERROR
,
854 "Syntax error on line %d of subscriptions.conf.",
859 else if (!strcasecmp(line
, "PrinterName"))
867 if ((sub
->dest
= cupsdFindDest(value
)) == NULL
)
869 cupsdLogMessage(CUPSD_LOG_ERROR
,
870 "Printer \'%s\' not found on line %d of subscriptions.conf.",
877 cupsdLogMessage(CUPSD_LOG_ERROR
,
878 "Syntax error on line %d of subscriptions.conf.",
883 else if (!strcasecmp(line
, "UserData"))
886 * UserData encoded-string
891 for (i
= 0, valueptr
= value
, hex
= 0; i
< 63 && *valueptr
; i
++)
893 if (*valueptr
== '<' && !hex
)
901 if (isxdigit(valueptr
[0]) && isxdigit(valueptr
[1]))
903 if (isdigit(valueptr
[0]))
904 sub
->user_data
[i
] = (valueptr
[0] - '0') << 4;
906 sub
->user_data
[i
] = (tolower(valueptr
[0]) - 'a' + 10) << 4;
908 if (isdigit(valueptr
[1]))
909 sub
->user_data
[i
] |= valueptr
[1] - '0';
911 sub
->user_data
[i
] |= tolower(valueptr
[1]) - 'a' + 10;
915 if (*valueptr
== '>')
925 sub
->user_data
[i
] = *valueptr
++;
930 cupsdLogMessage(CUPSD_LOG_ERROR
,
931 "Bad UserData \'%s\' on line %d of subscriptions.conf.",
935 sub
->user_data_len
= i
;
939 cupsdLogMessage(CUPSD_LOG_ERROR
,
940 "Syntax error on line %d of subscriptions.conf.",
945 else if (!strcasecmp(line
, "LeaseDuration"))
951 if (value
&& isdigit(*value
& 255))
952 sub
->lease
= atoi(value
);
955 cupsdLogMessage(CUPSD_LOG_ERROR
,
956 "Syntax error on line %d of subscriptions.conf.",
961 else if (!strcasecmp(line
, "Interval"))
967 if (value
&& isdigit(*value
& 255))
968 sub
->interval
= atoi(value
);
971 cupsdLogMessage(CUPSD_LOG_ERROR
,
972 "Syntax error on line %d of subscriptions.conf.",
977 else if (!strcasecmp(line
, "ExpirationTime"))
983 if (value
&& isdigit(*value
& 255))
984 sub
->expire
= atoi(value
);
987 cupsdLogMessage(CUPSD_LOG_ERROR
,
988 "Syntax error on line %d of subscriptions.conf.",
993 else if (!strcasecmp(line
, "NextEventId"))
999 if (value
&& isdigit(*value
& 255))
1000 sub
->next_event_id
= sub
->first_event_id
= atoi(value
);
1003 cupsdLogMessage(CUPSD_LOG_ERROR
,
1004 "Syntax error on line %d of subscriptions.conf.",
1012 * Something else we don't understand...
1015 cupsdLogMessage(CUPSD_LOG_ERROR
,
1016 "Unknown configuration directive %s on line %d of subscriptions.conf.",
1026 * 'cupsdSaveAllSubscriptions()' - Save all subscriptions to the .conf file.
1030 cupsdSaveAllSubscriptions(void)
1032 int i
; /* Looping var */
1033 cups_file_t
*fp
; /* subscriptions.conf file */
1034 char temp
[1024]; /* Temporary string */
1035 char backup
[1024]; /* subscriptions.conf.O file */
1036 cupsd_subscription_t
*sub
; /* Current subscription */
1037 time_t curtime
; /* Current time */
1038 struct tm
*curdate
; /* Current date */
1039 unsigned mask
; /* Current event mask */
1040 const char *name
; /* Current event name */
1041 int hex
; /* Non-zero if we are writing hex data */
1045 * Create the subscriptions.conf file...
1048 snprintf(temp
, sizeof(temp
), "%s/subscriptions.conf", ServerRoot
);
1049 snprintf(backup
, sizeof(backup
), "%s/subscriptions.conf.O", ServerRoot
);
1051 if (rename(temp
, backup
))
1053 if (errno
!= ENOENT
)
1054 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup subscriptions.conf - %s",
1058 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1060 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save subscriptions.conf - %s",
1063 if (rename(backup
, temp
))
1064 cupsdLogMessage(CUPSD_LOG_ERROR
,
1065 "Unable to restore subscriptions.conf - %s",
1070 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving subscriptions.conf...");
1073 * Restrict access to the file...
1076 fchown(cupsFileNumber(fp
), getuid(), Group
);
1077 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
1080 * Write a small header to the file...
1083 curtime
= time(NULL
);
1084 curdate
= localtime(&curtime
);
1085 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1087 cupsFilePuts(fp
, "# Subscription configuration file for " CUPS_SVERSION
"\n");
1088 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1091 * Write every subscription known to the system...
1094 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1096 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1098 cupsFilePrintf(fp
, "<Subscription %d>\n", sub
->id
);
1100 if ((name
= cupsdEventName((cupsd_eventmask_t
)sub
->mask
)) != NULL
)
1103 * Simple event list...
1106 cupsFilePrintf(fp
, "Events %s\n", name
);
1111 * Complex event list...
1114 cupsFilePuts(fp
, "Events");
1116 for (mask
= 1; mask
< CUPSD_EVENT_ALL
; mask
<<= 1)
1117 if (sub
->mask
& mask
)
1118 cupsFilePrintf(fp
, " %s", cupsdEventName((cupsd_eventmask_t
)mask
));
1120 cupsFilePuts(fp
, "\n");
1124 cupsFilePrintf(fp
, "Owner %s\n", sub
->owner
);
1126 cupsFilePrintf(fp
, "Recipient %s\n", sub
->recipient
);
1128 cupsFilePrintf(fp
, "JobId %d\n", sub
->job
->id
);
1130 cupsFilePrintf(fp
, "PrinterName %s\n", sub
->dest
->name
);
1132 if (sub
->user_data_len
> 0)
1134 cupsFilePuts(fp
, "UserData ");
1136 for (i
= 0, hex
= 0; i
< sub
->user_data_len
; i
++)
1138 if (sub
->user_data
[i
] < ' ' ||
1139 sub
->user_data
[i
] > 0x7f ||
1140 sub
->user_data
[i
] == '<')
1144 cupsFilePrintf(fp
, "<%02X", sub
->user_data
[i
]);
1148 cupsFilePrintf(fp
, "%02X", sub
->user_data
[i
]);
1154 cupsFilePrintf(fp
, ">%c", sub
->user_data
[i
]);
1158 cupsFilePutChar(fp
, sub
->user_data
[i
]);
1163 cupsFilePuts(fp
, ">\n");
1165 cupsFilePutChar(fp
, '\n');
1168 cupsFilePrintf(fp
, "LeaseDuration %d\n", sub
->lease
);
1169 cupsFilePrintf(fp
, "Interval %d\n", sub
->interval
);
1170 cupsFilePrintf(fp
, "ExpirationTime %ld\n", (long)sub
->expire
);
1171 cupsFilePrintf(fp
, "NextEventId %d\n", sub
->next_event_id
);
1173 cupsFilePuts(fp
, "</Subscription>\n");
1181 * 'cupsdSendNotification()' - Send a notification for the specified event.
1185 cupsdSendNotification(
1186 cupsd_subscription_t
*sub
, /* I - Subscription object */
1187 cupsd_event_t
*event
) /* I - Event to send */
1189 ipp_state_t state
; /* IPP event state */
1192 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1193 "cupsdSendNotification(sub=%p(%d), event=%p(%s))\n",
1194 sub
, sub
->id
, event
, cupsdEventName(event
->event
));
1197 * Allocate the events array as needed...
1202 sub
->events
= calloc(MaxEvents
, sizeof(cupsd_event_t
*));
1206 cupsdLogMessage(CUPSD_LOG_CRIT
,
1207 "Unable to allocate memory for subscription #%d!",
1214 * Add the event to the subscription. Since the events array is
1215 * always MaxEvents in length, and since we will have already
1216 * removed an event from the subscription cache if we hit the
1217 * event cache limit, we don't need to check for overflow here...
1220 sub
->events
[sub
->num_events
] = event
;
1224 * Deliver the event...
1230 cupsd_start_notifier(sub
);
1234 event
->attrs
->state
= IPP_IDLE
;
1236 while ((state
= ippWriteFile(sub
->pipe
, event
->attrs
)) != IPP_DATA
)
1237 if (state
== IPP_ERROR
)
1240 if (state
== IPP_ERROR
)
1241 cupsdLogMessage(CUPSD_LOG_ERROR
,
1242 "Unable to send event for subscription %d (%s)!",
1243 sub
->id
, sub
->recipient
);
1248 * Bump the event sequence number...
1251 sub
->next_event_id
++;
1256 * 'cupsdStopAllNotifiers()' - Stop all notifier processes.
1260 cupsdStopAllNotifiers(void)
1262 cupsd_subscription_t
*sub
; /* Current subscription */
1266 * See if we have started any notifiers...
1269 if (!NotifierStatusBuffer
)
1273 * Yes, kill and processes that are left...
1276 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1278 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1281 cupsdEndProcess(sub
->pid
, 0);
1288 * Close the status pipes...
1291 if (NotifierPipes
[0] >= 0)
1293 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1294 "cupsdStopAllNotifiers: Removing fd %d from InputSet...",
1296 FD_CLR(NotifierPipes
[0], InputSet
);
1298 cupsdStatBufDelete(NotifierStatusBuffer
);
1300 close(NotifierPipes
[0]);
1301 close(NotifierPipes
[1]);
1303 NotifierPipes
[0] = -1;
1304 NotifierPipes
[1] = -1;
1305 NotifierStatusBuffer
= NULL
;
1311 * 'cupsdUpdateNotifierStatus()' - Read messages from notifiers.
1315 cupsdUpdateNotifierStatus(void)
1317 char *ptr
, /* Pointer to end of line in buffer */
1318 message
[1024]; /* Pointer to message text */
1319 int loglevel
; /* Log level for message */
1322 while ((ptr
= cupsdStatBufUpdate(NotifierStatusBuffer
, &loglevel
,
1323 message
, sizeof(message
))) != NULL
)
1324 if (!strchr(NotifierStatusBuffer
->buffer
, '\n'))
1330 * 'cupsd_compare_subscriptions()' - Compare two subscriptions.
1333 static int /* O - Result of comparison */
1334 cupsd_compare_subscriptions(
1335 cupsd_subscription_t
*first
, /* I - First subscription object */
1336 cupsd_subscription_t
*second
, /* I - Second subscription object */
1337 void *unused
) /* I - Unused user data pointer */
1341 return (first
->id
- second
->id
);
1346 * 'cupsd_delete_event()' - Delete a single event...
1348 * Oldest events must be deleted first, otherwise the subscription cache
1349 * flushing code will not work properly.
1353 cupsd_delete_event(cupsd_event_t
*event
)/* I - Event to delete */
1355 cupsd_subscription_t
*sub
; /* Current subscription */
1359 * Loop through the subscriptions and look for the event in the cache...
1362 for (sub
= (cupsd_subscription_t
*)cupsArrayFirst(Subscriptions
);
1364 sub
= (cupsd_subscription_t
*)cupsArrayNext(Subscriptions
))
1367 * Only check the first event in the subscription cache, since the
1368 * caller will only delete the oldest event in the cache...
1371 if (sub
->num_events
> 0 && sub
->events
[0] == event
)
1374 * Remove this event...
1378 sub
->first_event_id
++;
1380 if (sub
->num_events
> 0)
1383 * Shift other events upward in cache...
1386 memmove(sub
->events
, sub
->events
+ 1,
1387 sub
->num_events
* sizeof(cupsd_event_t
*));
1396 ippDelete(event
->attrs
);
1402 * 'cupsd_start_notifier()' - Start a notifier subprocess...
1406 cupsd_start_notifier(
1407 cupsd_subscription_t
*sub
) /* I - Subscription object */
1409 int pid
; /* Notifier process ID */
1410 int fds
[2]; /* Pipe file descriptors */
1411 int envc
; /* Number of environment variables */
1412 char *argv
[4], /* Command-line arguments */
1413 *envp
[100], /* Environment variables */
1414 user_data
[128], /* Base-64 encoded user data */
1415 scheme
[256], /* notify-recipient-uri scheme */
1416 *ptr
, /* Pointer into scheme */
1417 command
[1024]; /* Notifier command */
1421 * Extract the scheme name from the recipient URI and point to the
1422 * notifier program...
1425 strlcpy(scheme
, sub
->recipient
, sizeof(scheme
));
1426 if ((ptr
= strchr(scheme
, ':')) != NULL
)
1429 snprintf(command
, sizeof(command
), "%s/notifier/%s", ServerBin
, scheme
);
1432 * Base-64 encode the user data...
1435 httpEncode64_2(user_data
, sizeof(user_data
), (char *)sub
->user_data
,
1436 sub
->user_data_len
);
1439 * Setup the argument array...
1443 argv
[1] = sub
->recipient
;
1444 argv
[2] = user_data
;
1448 * Setup the environment...
1451 envc
= cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1454 * Create pipes as needed...
1457 if (!NotifierStatusBuffer
)
1460 * Create the status pipe...
1463 if (cupsdOpenPipe(NotifierPipes
))
1465 cupsdLogMessage(CUPSD_LOG_ERROR
,
1466 "Unable to create pipes for notifier status - %s",
1471 NotifierStatusBuffer
= cupsdStatBufNew(NotifierPipes
[0], "[Notifier]");
1473 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1474 "start_notifier: Adding fd %d to InputSet...",
1477 FD_SET(NotifierPipes
[0], InputSet
);
1480 if (cupsdOpenPipe(fds
))
1482 cupsdLogMessage(CUPSD_LOG_ERROR
,
1483 "Unable to create pipes for notifier %s - %s",
1484 scheme
, strerror(errno
));
1489 * Make sure the delivery pipe is non-blocking...
1492 fcntl(fds
[1], F_SETFL
, fcntl(fds
[1], F_GETFL
) | O_NONBLOCK
);
1495 * Create the notifier process...
1498 if (cupsdStartProcess(command
, argv
, envp
, fds
[0], -1, NotifierPipes
[1],
1502 * Error - can't fork!
1505 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to fork for notifier %s - %s",
1506 scheme
, strerror(errno
));
1508 cupsdClosePipe(fds
);
1513 * Fork successful - return the PID...
1516 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Notifier %s started - PID = %d",
1529 * End of "$Id: subscriptions.c 4840 2005-11-14 21:53:30Z mike $".