]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/classes.c
2 * "$Id: classes.c 6649 2007-07-11 21:46:42Z mike $"
4 * Printer class routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddClass() - Add a class to the system.
18 * cupsdAddPrinterToClass() - Add a printer to a class...
19 * cupsdDeletePrinterFromClass() - Delete a printer from a class.
20 * cupsdDeletePrinterFromClasses() - Delete a printer from all classes.
21 * cupsdDeleteAllClasses() - Remove all classes from the system.
22 * cupsdFindAvailablePrinter() - Find an available printer in a class.
23 * cupsdFindClass() - Find the named class.
24 * cupsdLoadAllClasses() - Load classes from the classes.conf file.
25 * cupsdSaveAllClasses() - Save classes to the classes.conf file.
26 * cupsdUpdateImplicitClasses() - Update the accepting state of implicit
31 * Include necessary headers...
38 * 'cupsdAddClass()' - Add a class to the system.
41 cupsd_printer_t
* /* O - New class */
42 cupsdAddClass(const char *name
) /* I - Name of class */
44 cupsd_printer_t
*c
; /* New class */
48 * Add the printer and set the type to "class"...
51 if ((c
= cupsdAddPrinter(name
)) != NULL
)
54 * Change from a printer to a class...
57 c
->type
= CUPS_PRINTER_CLASS
;
59 cupsdSetStringf(&c
->uri
, "ipp://%s:%d/classes/%s", ServerName
, LocalPort
,
61 cupsdSetString(&c
->error_policy
, "retry-job");
69 * 'cupsdAddPrinterToClass()' - Add a printer to a class...
73 cupsdAddPrinterToClass(
74 cupsd_printer_t
*c
, /* I - Class to add to */
75 cupsd_printer_t
*p
) /* I - Printer to add */
77 int i
; /* Looping var */
78 cupsd_printer_t
**temp
; /* Pointer to printer array */
82 * See if this printer is already a member of the class...
85 for (i
= 0; i
< c
->num_printers
; i
++)
86 if (c
->printers
[i
] == p
)
90 * Allocate memory as needed...
93 if (c
->num_printers
== 0)
94 temp
= malloc(sizeof(cupsd_printer_t
*));
96 temp
= realloc(c
->printers
, sizeof(cupsd_printer_t
*) * (c
->num_printers
+ 1));
100 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add printer %s to class %s!",
106 * Add the printer to the end of the array and update the number of printers.
110 temp
+= c
->num_printers
;
118 * 'cupsdDeletePrinterFromClass()' - Delete a printer from a class.
122 cupsdDeletePrinterFromClass(
123 cupsd_printer_t
*c
, /* I - Class to delete from */
124 cupsd_printer_t
*p
) /* I - Printer to delete */
126 int i
; /* Looping var */
127 cups_ptype_t type
, /* Class type */
128 oldtype
; /* Old class type */
132 * See if the printer is in the class...
135 for (i
= 0; i
< c
->num_printers
; i
++)
136 if (p
== c
->printers
[i
])
140 * If it is, remove it from the list...
143 if (i
< c
->num_printers
)
146 * Yes, remove the printer...
150 if (i
< c
->num_printers
)
151 memmove(c
->printers
+ i
, c
->printers
+ i
+ 1,
152 (c
->num_printers
- i
) * sizeof(cupsd_printer_t
*));
158 * Recompute the printer type mask as needed...
161 if (c
->num_printers
> 0)
164 type
= c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
);
165 c
->type
= ~CUPS_PRINTER_REMOTE
;
167 for (i
= 0; i
< c
->num_printers
; i
++)
168 c
->type
&= c
->printers
[i
]->type
;
173 * Update the IPP attributes...
176 if (c
->type
!= oldtype
)
177 cupsdSetPrinterAttrs(c
);
183 * 'cupsdDeletePrinterFromClasses()' - Delete a printer from all classes.
187 cupsdDeletePrinterFromClasses(
188 cupsd_printer_t
*p
) /* I - Printer to delete */
190 cupsd_printer_t
*c
; /* Pointer to current class */
194 * Loop through the printer/class list and remove the printer
195 * from each class listed...
198 for (c
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
200 c
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
201 if (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
202 cupsdDeletePrinterFromClass(c
, p
);
205 * Then clean out any empty implicit classes...
208 for (c
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
210 c
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
211 if (c
->num_printers
== 0)
213 cupsArrayRemove(ImplicitPrinters
, c
);
214 cupsdDeletePrinter(c
, 0);
220 * 'cupsdDeleteAllClasses()' - Remove all classes from the system.
224 cupsdDeleteAllClasses(void)
226 cupsd_printer_t
*c
; /* Pointer to current printer/class */
229 for (c
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
231 c
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
232 if (c
->type
& CUPS_PRINTER_CLASS
)
233 cupsdDeletePrinter(c
, 0);
238 * 'cupsdFindAvailablePrinter()' - Find an available printer in a class.
241 cupsd_printer_t
* /* O - Available printer or NULL */
242 cupsdFindAvailablePrinter(
243 const char *name
) /* I - Class to check */
245 int i
; /* Looping var */
246 cupsd_printer_t
*c
; /* Printer class */
253 if ((c
= cupsdFindClass(name
)) == NULL
)
255 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to find class \"%s\"!", name
);
260 * Make sure that the last printer is also a valid index into the printer
261 * array. If not, reset the last printer to 0...
264 if (c
->last_printer
>= c
->num_printers
)
268 * Loop through the printers in the class and return the first idle
269 * printer... We keep track of the last printer that we used so that
270 * a "round robin" type of scheduling is realized (otherwise the first
271 * server might be saturated with print jobs...)
273 * Thanks to Joel Fredrikson for helping us get this right!
276 for (i
= c
->last_printer
+ 1; ; i
++)
278 if (i
>= c
->num_printers
)
281 if (c
->printers
[i
]->accepting
&&
282 (c
->printers
[i
]->state
== IPP_PRINTER_IDLE
||
283 ((c
->printers
[i
]->type
& CUPS_PRINTER_REMOTE
) && !c
->printers
[i
]->job
)))
286 return (c
->printers
[i
]);
289 if (i
== c
->last_printer
)
298 * 'cupsdFindClass()' - Find the named class.
301 cupsd_printer_t
* /* O - Matching class or NULL */
302 cupsdFindClass(const char *name
) /* I - Name of class */
304 cupsd_printer_t
*c
; /* Current class/printer */
307 if ((c
= cupsdFindDest(name
)) != NULL
&&
308 (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
316 * 'cupsdLoadAllClasses()' - Load classes from the classes.conf file.
320 cupsdLoadAllClasses(void)
322 cups_file_t
*fp
; /* classes.conf file */
323 int linenum
; /* Current line number */
324 char line
[1024], /* Line from file */
325 *value
, /* Pointer to value */
326 *valueptr
; /* Pointer into value */
327 cupsd_printer_t
*p
, /* Current printer class */
328 *temp
; /* Temporary pointer to printer */
332 * Open the classes.conf file...
335 snprintf(line
, sizeof(line
), "%s/classes.conf", ServerRoot
);
336 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
339 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
345 * Read class configurations until we hit EOF...
351 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
354 * Decode the directive...
357 if (!strcasecmp(line
, "<Class") ||
358 !strcasecmp(line
, "<DefaultClass"))
361 * <Class name> or <DefaultClass name>
364 if (p
== NULL
&& value
)
366 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading class %s...", value
);
369 * Since prior classes may have implicitly defined this class,
370 * see if it already exists...
373 if ((p
= cupsdFindDest(value
)) != NULL
)
375 p
->type
= CUPS_PRINTER_CLASS
;
376 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/classes/%s", ServerName
,
378 cupsdSetString(&p
->error_policy
, "retry-job");
381 p
= cupsdAddClass(value
);
384 p
->state
= IPP_PRINTER_IDLE
;
386 if (!strcasecmp(line
, "<DefaultClass"))
391 cupsdLogMessage(CUPSD_LOG_ERROR
,
392 "Syntax error on line %d of classes.conf.", linenum
);
396 else if (!strcasecmp(line
, "</Class>"))
400 cupsdSetPrinterAttrs(p
);
405 cupsdLogMessage(CUPSD_LOG_ERROR
,
406 "Syntax error on line %d of classes.conf.", linenum
);
412 cupsdLogMessage(CUPSD_LOG_ERROR
,
413 "Syntax error on line %d of classes.conf.", linenum
);
416 else if (!strcasecmp(line
, "AuthInfoRequired"))
418 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
419 cupsdLogMessage(CUPSD_LOG_ERROR
,
420 "Bad AuthInfoRequired on line %d of classes.conf.",
423 else if (!strcasecmp(line
, "Info"))
426 cupsdSetString(&p
->info
, value
);
428 else if (!strcasecmp(line
, "Location"))
431 cupsdSetString(&p
->location
, value
);
433 else if (!strcasecmp(line
, "Option") && value
)
439 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
442 cupsdLogMessage(CUPSD_LOG_ERROR
,
443 "Syntax error on line %d of classes.conf.", linenum
);
446 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
448 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
452 else if (!strcasecmp(line
, "Printer"))
456 cupsdLogMessage(CUPSD_LOG_ERROR
,
457 "Syntax error on line %d of classes.conf.", linenum
);
460 else if ((temp
= cupsdFindPrinter(value
)) == NULL
)
462 cupsdLogMessage(CUPSD_LOG_WARN
,
463 "Unknown printer %s on line %d of classes.conf.",
467 * Add the missing remote printer...
470 if ((temp
= cupsdAddPrinter(value
)) != NULL
)
472 cupsdSetString(&temp
->make_model
, "Remote Printer on unknown");
474 temp
->state
= IPP_PRINTER_STOPPED
;
475 temp
->type
|= CUPS_PRINTER_REMOTE
;
476 temp
->browse_time
= 2147483647;
478 cupsdSetString(&temp
->location
, "Location Unknown");
479 cupsdSetString(&temp
->info
, "No Information Available");
480 temp
->hostname
[0] = '\0';
482 cupsdSetPrinterAttrs(temp
);
487 cupsdAddPrinterToClass(p
, temp
);
489 else if (!strcasecmp(line
, "State"))
492 * Set the initial queue state...
495 if (!strcasecmp(value
, "idle"))
496 p
->state
= IPP_PRINTER_IDLE
;
497 else if (!strcasecmp(value
, "stopped"))
498 p
->state
= IPP_PRINTER_STOPPED
;
501 cupsdLogMessage(CUPSD_LOG_ERROR
,
502 "Syntax error on line %d of classes.conf.",
507 else if (!strcasecmp(line
, "StateMessage"))
510 * Set the initial queue state message...
514 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
516 else if (!strcasecmp(line
, "StateTime"))
519 * Set the state time...
523 p
->state_time
= atoi(value
);
525 else if (!strcasecmp(line
, "Accepting"))
528 * Set the initial accepting state...
532 (!strcasecmp(value
, "yes") ||
533 !strcasecmp(value
, "on") ||
534 !strcasecmp(value
, "true")))
537 (!strcasecmp(value
, "no") ||
538 !strcasecmp(value
, "off") ||
539 !strcasecmp(value
, "false")))
543 cupsdLogMessage(CUPSD_LOG_ERROR
,
544 "Syntax error on line %d of classes.conf.",
549 else if (!strcasecmp(line
, "Shared"))
552 * Set the initial shared state...
556 (!strcasecmp(value
, "yes") ||
557 !strcasecmp(value
, "on") ||
558 !strcasecmp(value
, "true")))
561 (!strcasecmp(value
, "no") ||
562 !strcasecmp(value
, "off") ||
563 !strcasecmp(value
, "false")))
567 cupsdLogMessage(CUPSD_LOG_ERROR
,
568 "Syntax error on line %d of printers.conf.",
573 else if (!strcasecmp(line
, "JobSheets"))
576 * Set the initial job sheets...
581 for (valueptr
= value
;
582 *valueptr
&& !isspace(*valueptr
& 255);
588 cupsdSetString(&p
->job_sheets
[0], value
);
590 while (isspace(*valueptr
& 255))
595 for (value
= valueptr
;
596 *valueptr
&& !isspace(*valueptr
& 255);
602 cupsdSetString(&p
->job_sheets
[1], value
);
607 cupsdLogMessage(CUPSD_LOG_ERROR
,
608 "Syntax error on line %d of classes.conf.", linenum
);
612 else if (!strcasecmp(line
, "AllowUser"))
617 cupsdAddPrinterUser(p
, value
);
621 cupsdLogMessage(CUPSD_LOG_ERROR
,
622 "Syntax error on line %d of classes.conf.", linenum
);
626 else if (!strcasecmp(line
, "DenyUser"))
631 cupsdAddPrinterUser(p
, value
);
635 cupsdLogMessage(CUPSD_LOG_ERROR
,
636 "Syntax error on line %d of classes.conf.", linenum
);
640 else if (!strcasecmp(line
, "QuotaPeriod"))
643 p
->quota_period
= atoi(value
);
646 cupsdLogMessage(CUPSD_LOG_ERROR
,
647 "Syntax error on line %d of classes.conf.", linenum
);
651 else if (!strcasecmp(line
, "PageLimit"))
654 p
->page_limit
= atoi(value
);
657 cupsdLogMessage(CUPSD_LOG_ERROR
,
658 "Syntax error on line %d of classes.conf.", linenum
);
662 else if (!strcasecmp(line
, "KLimit"))
665 p
->k_limit
= atoi(value
);
668 cupsdLogMessage(CUPSD_LOG_ERROR
,
669 "Syntax error on line %d of classes.conf.", linenum
);
673 else if (!strcasecmp(line
, "OpPolicy"))
677 cupsd_policy_t
*pol
; /* Policy */
680 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
682 cupsdSetString(&p
->op_policy
, value
);
683 p
->op_policy_ptr
= pol
;
686 cupsdLogMessage(CUPSD_LOG_ERROR
,
687 "Bad policy \"%s\" on line %d of classes.conf",
692 cupsdLogMessage(CUPSD_LOG_ERROR
,
693 "Syntax error on line %d of classes.conf.", linenum
);
697 else if (!strcasecmp(line
, "ErrorPolicy"))
700 cupsdSetString(&p
->error_policy
, value
);
703 cupsdLogMessage(CUPSD_LOG_ERROR
,
704 "Syntax error on line %d of classes.conf.", linenum
);
711 * Something else we don't understand...
714 cupsdLogMessage(CUPSD_LOG_ERROR
,
715 "Unknown configuration directive %s on line %d of classes.conf.",
725 * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file.
729 cupsdSaveAllClasses(void)
731 cups_file_t
*fp
; /* classes.conf file */
732 char temp
[1024]; /* Temporary string */
733 char backup
[1024]; /* classes.conf.O file */
734 cupsd_printer_t
*pclass
; /* Current printer class */
735 int i
; /* Looping var */
736 time_t curtime
; /* Current time */
737 struct tm
*curdate
; /* Current date */
738 cups_option_t
*option
; /* Current option */
739 const char *ptr
; /* Pointer into info/location */
743 * Create the classes.conf file...
746 snprintf(temp
, sizeof(temp
), "%s/classes.conf", ServerRoot
);
747 snprintf(backup
, sizeof(backup
), "%s/classes.conf.O", ServerRoot
);
749 if (rename(temp
, backup
))
752 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup classes.conf - %s",
756 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
758 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save classes.conf - %s",
761 if (rename(backup
, temp
))
762 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to restore classes.conf - %s",
767 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving classes.conf...");
770 * Restrict access to the file...
773 fchown(cupsFileNumber(fp
), RunUser
, Group
);
774 fchmod(cupsFileNumber(fp
), 0600);
777 * Write a small header to the file...
780 curtime
= time(NULL
);
781 curdate
= localtime(&curtime
);
782 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
784 cupsFilePuts(fp
, "# Class configuration file for " CUPS_SVERSION
"\n");
785 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
788 * Write each local class known to the system...
791 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
793 pclass
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
796 * Skip remote destinations and regular printers...
799 if ((pclass
->type
& CUPS_PRINTER_REMOTE
) ||
800 (pclass
->type
& CUPS_PRINTER_IMPLICIT
) ||
801 !(pclass
->type
& CUPS_PRINTER_CLASS
))
805 * Write printers as needed...
808 if (pclass
== DefaultPrinter
)
809 cupsFilePrintf(fp
, "<DefaultClass %s>\n", pclass
->name
);
811 cupsFilePrintf(fp
, "<Class %s>\n", pclass
->name
);
813 if (pclass
->num_auth_info_required
> 0)
815 cupsFilePrintf(fp
, "AuthInfoRequired %s", pclass
->auth_info_required
[0]);
816 for (i
= 1; i
< pclass
->num_auth_info_required
; i
++)
817 cupsFilePrintf(fp
, ",%s", pclass
->auth_info_required
[i
]);
818 cupsFilePutChar(fp
, '\n');
823 if ((ptr
= strchr(pclass
->info
, '#')) != NULL
)
826 * Need to quote the first # in the info string...
829 cupsFilePuts(fp
, "Info ");
830 cupsFileWrite(fp
, pclass
->info
, ptr
- pclass
->info
);
831 cupsFilePutChar(fp
, '\\');
832 cupsFilePuts(fp
, ptr
);
833 cupsFilePutChar(fp
, '\n');
836 cupsFilePrintf(fp
, "Info %s\n", pclass
->info
);
839 if (pclass
->location
)
841 if ((ptr
= strchr(pclass
->info
, '#')) != NULL
)
844 * Need to quote the first # in the location string...
847 cupsFilePuts(fp
, "Location ");
848 cupsFileWrite(fp
, pclass
->location
, ptr
- pclass
->location
);
849 cupsFilePutChar(fp
, '\\');
850 cupsFilePuts(fp
, ptr
);
851 cupsFilePutChar(fp
, '\n');
854 cupsFilePrintf(fp
, "Location %s\n", pclass
->location
);
857 if (pclass
->state
== IPP_PRINTER_STOPPED
)
859 cupsFilePuts(fp
, "State Stopped\n");
860 cupsFilePrintf(fp
, "StateMessage %s\n", pclass
->state_message
);
863 cupsFilePuts(fp
, "State Idle\n");
865 cupsFilePrintf(fp
, "StateTime %d\n", (int)pclass
->state_time
);
867 if (pclass
->accepting
)
868 cupsFilePuts(fp
, "Accepting Yes\n");
870 cupsFilePuts(fp
, "Accepting No\n");
873 cupsFilePuts(fp
, "Shared Yes\n");
875 cupsFilePuts(fp
, "Shared No\n");
877 cupsFilePrintf(fp
, "JobSheets %s %s\n", pclass
->job_sheets
[0],
878 pclass
->job_sheets
[1]);
880 for (i
= 0; i
< pclass
->num_printers
; i
++)
881 cupsFilePrintf(fp
, "Printer %s\n", pclass
->printers
[i
]->name
);
883 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
884 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
885 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
887 for (i
= 0; i
< pclass
->num_users
; i
++)
888 cupsFilePrintf(fp
, "%sUser %s\n", pclass
->deny_users
? "Deny" : "Allow",
891 if (pclass
->op_policy
)
892 cupsFilePrintf(fp
, "OpPolicy %s\n", pclass
->op_policy
);
893 if (pclass
->error_policy
)
894 cupsFilePrintf(fp
, "ErrorPolicy %s\n", pclass
->error_policy
);
896 for (i
= pclass
->num_options
, option
= pclass
->options
;
899 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
901 cupsFilePuts(fp
, "</Class>\n");
909 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
914 cupsdUpdateImplicitClasses(void)
916 int i
; /* Looping var */
917 cupsd_printer_t
*pclass
; /* Current class */
918 int accepting
; /* printer-is-accepting-jobs value */
921 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
923 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
926 * Loop through the printers to come up with a composite state...
929 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
930 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
933 pclass
->accepting
= accepting
;
939 * End of "$Id: classes.c 6649 2007-07-11 21:46:42Z mike $".