]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/classes.c
2 * "$Id: classes.c 7724 2008-07-14 06:06:06Z mike $"
4 * Printer class routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * cupsdFindAvailablePrinter() - Find an available printer in a class.
22 * cupsdFindClass() - Find the named class.
23 * cupsdLoadAllClasses() - Load classes from the classes.conf file.
24 * cupsdSaveAllClasses() - Save classes to the classes.conf file.
25 * cupsdUpdateImplicitClasses() - Update the accepting state of implicit
30 * Include necessary headers...
37 * 'cupsdAddClass()' - Add a class to the system.
40 cupsd_printer_t
* /* O - New class */
41 cupsdAddClass(const char *name
) /* I - Name of class */
43 cupsd_printer_t
*c
; /* New class */
47 * Add the printer and set the type to "class"...
50 if ((c
= cupsdAddPrinter(name
)) != NULL
)
53 * Change from a printer to a class...
56 c
->type
= CUPS_PRINTER_CLASS
;
58 cupsdSetStringf(&c
->uri
, "ipp://%s:%d/classes/%s", ServerName
, LocalPort
,
60 cupsdSetString(&c
->error_policy
, "retry-job");
68 * 'cupsdAddPrinterToClass()' - Add a printer to a class...
72 cupsdAddPrinterToClass(
73 cupsd_printer_t
*c
, /* I - Class to add to */
74 cupsd_printer_t
*p
) /* I - Printer to add */
76 int i
; /* Looping var */
77 cupsd_printer_t
**temp
; /* Pointer to printer array */
81 * See if this printer is already a member of the class...
84 for (i
= 0; i
< c
->num_printers
; i
++)
85 if (c
->printers
[i
] == p
)
89 * Allocate memory as needed...
92 if (c
->num_printers
== 0)
93 temp
= malloc(sizeof(cupsd_printer_t
*));
95 temp
= realloc(c
->printers
, sizeof(cupsd_printer_t
*) * (c
->num_printers
+ 1));
99 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add printer %s to class %s!",
105 * Add the printer to the end of the array and update the number of printers.
109 temp
+= c
->num_printers
;
117 * 'cupsdDeletePrinterFromClass()' - Delete a printer from a class.
121 cupsdDeletePrinterFromClass(
122 cupsd_printer_t
*c
, /* I - Class to delete from */
123 cupsd_printer_t
*p
) /* I - Printer to delete */
125 int i
; /* Looping var */
129 * See if the printer is in the class...
132 for (i
= 0; i
< c
->num_printers
; i
++)
133 if (p
== c
->printers
[i
])
137 * If it is, remove it from the list...
140 if (i
< c
->num_printers
)
143 * Yes, remove the printer...
147 if (i
< c
->num_printers
)
148 memmove(c
->printers
+ i
, c
->printers
+ i
+ 1,
149 (c
->num_printers
- i
) * sizeof(cupsd_printer_t
*));
155 * Update the IPP attributes (have to do this for member-names)...
158 cupsdSetPrinterAttrs(c
);
163 * 'cupsdDeletePrinterFromClasses()' - Delete a printer from all classes.
167 cupsdDeletePrinterFromClasses(
168 cupsd_printer_t
*p
) /* I - Printer to delete */
170 cupsd_printer_t
*c
; /* Pointer to current class */
174 * Loop through the printer/class list and remove the printer
175 * from each class listed...
178 for (c
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
180 c
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
181 if (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
182 cupsdDeletePrinterFromClass(c
, p
);
185 * Then clean out any empty implicit classes...
188 for (c
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
190 c
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
191 if (c
->num_printers
== 0)
193 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Deleting implicit class \"%s\"...",
195 cupsdDeletePrinter(c
, 0);
201 * 'cupsdFindAvailablePrinter()' - Find an available printer in a class.
204 cupsd_printer_t
* /* O - Available printer or NULL */
205 cupsdFindAvailablePrinter(
206 const char *name
) /* I - Class to check */
208 int i
; /* Looping var */
209 cupsd_printer_t
*c
; /* Printer class */
216 if ((c
= cupsdFindClass(name
)) == NULL
)
218 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to find class \"%s\"!", name
);
222 if (c
->num_printers
== 0)
226 * Make sure that the last printer is also a valid index into the printer
227 * array. If not, reset the last printer to 0...
230 if (c
->last_printer
>= c
->num_printers
)
234 * Loop through the printers in the class and return the first idle
235 * printer... We keep track of the last printer that we used so that
236 * a "round robin" type of scheduling is realized (otherwise the first
237 * server might be saturated with print jobs...)
239 * Thanks to Joel Fredrikson for helping us get this right!
242 for (i
= c
->last_printer
+ 1; ; i
++)
244 if (i
>= c
->num_printers
)
247 if (c
->printers
[i
]->accepting
&&
248 (c
->printers
[i
]->state
== IPP_PRINTER_IDLE
||
249 ((c
->printers
[i
]->type
& CUPS_PRINTER_REMOTE
) && !c
->printers
[i
]->job
)))
252 return (c
->printers
[i
]);
255 if (i
== c
->last_printer
)
264 * 'cupsdFindClass()' - Find the named class.
267 cupsd_printer_t
* /* O - Matching class or NULL */
268 cupsdFindClass(const char *name
) /* I - Name of class */
270 cupsd_printer_t
*c
; /* Current class/printer */
273 if ((c
= cupsdFindDest(name
)) != NULL
&&
274 (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
282 * 'cupsdLoadAllClasses()' - Load classes from the classes.conf file.
286 cupsdLoadAllClasses(void)
288 cups_file_t
*fp
; /* classes.conf file */
289 int linenum
; /* Current line number */
290 char line
[1024], /* Line from file */
291 *value
, /* Pointer to value */
292 *valueptr
; /* Pointer into value */
293 cupsd_printer_t
*p
, /* Current printer class */
294 *temp
; /* Temporary pointer to printer */
298 * Open the classes.conf file...
301 snprintf(line
, sizeof(line
), "%s/classes.conf", ServerRoot
);
302 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
305 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
311 * Read class configurations until we hit EOF...
317 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
320 * Decode the directive...
323 if (!strcasecmp(line
, "<Class") ||
324 !strcasecmp(line
, "<DefaultClass"))
327 * <Class name> or <DefaultClass name>
330 if (p
== NULL
&& value
)
332 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading class %s...", value
);
335 * Since prior classes may have implicitly defined this class,
336 * see if it already exists...
339 if ((p
= cupsdFindDest(value
)) != NULL
)
341 p
->type
= CUPS_PRINTER_CLASS
;
342 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/classes/%s", ServerName
,
344 cupsdSetString(&p
->error_policy
, "retry-job");
347 p
= cupsdAddClass(value
);
350 p
->state
= IPP_PRINTER_IDLE
;
352 if (!strcasecmp(line
, "<DefaultClass"))
356 cupsdLogMessage(CUPSD_LOG_ERROR
,
357 "Syntax error on line %d of classes.conf.", linenum
);
359 else if (!strcasecmp(line
, "</Class>"))
363 cupsdSetPrinterAttrs(p
);
367 cupsdLogMessage(CUPSD_LOG_ERROR
,
368 "Syntax error on line %d of classes.conf.", linenum
);
372 cupsdLogMessage(CUPSD_LOG_ERROR
,
373 "Syntax error on line %d of classes.conf.", linenum
);
375 else if (!strcasecmp(line
, "AuthInfoRequired"))
377 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
378 cupsdLogMessage(CUPSD_LOG_ERROR
,
379 "Bad AuthInfoRequired on line %d of classes.conf.",
382 else if (!strcasecmp(line
, "Info"))
385 cupsdSetString(&p
->info
, value
);
387 else if (!strcasecmp(line
, "Location"))
390 cupsdSetString(&p
->location
, value
);
392 else if (!strcasecmp(line
, "Option") && value
)
398 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
401 cupsdLogMessage(CUPSD_LOG_ERROR
,
402 "Syntax error on line %d of classes.conf.", linenum
);
405 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
407 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
411 else if (!strcasecmp(line
, "Printer"))
415 cupsdLogMessage(CUPSD_LOG_ERROR
,
416 "Syntax error on line %d of classes.conf.", linenum
);
419 else if ((temp
= cupsdFindPrinter(value
)) == NULL
)
421 cupsdLogMessage(CUPSD_LOG_WARN
,
422 "Unknown printer %s on line %d of classes.conf.",
426 * Add the missing remote printer...
429 if ((temp
= cupsdAddPrinter(value
)) != NULL
)
431 cupsdSetString(&temp
->make_model
, "Remote Printer on unknown");
433 temp
->state
= IPP_PRINTER_STOPPED
;
434 temp
->type
|= CUPS_PRINTER_REMOTE
;
435 temp
->browse_time
= 2147483647;
437 cupsdSetString(&temp
->location
, "Location Unknown");
438 cupsdSetString(&temp
->info
, "No Information Available");
439 temp
->hostname
[0] = '\0';
441 cupsdSetPrinterAttrs(temp
);
446 cupsdAddPrinterToClass(p
, temp
);
448 else if (!strcasecmp(line
, "State"))
451 * Set the initial queue state...
454 if (!strcasecmp(value
, "idle"))
455 p
->state
= IPP_PRINTER_IDLE
;
456 else if (!strcasecmp(value
, "stopped"))
457 p
->state
= IPP_PRINTER_STOPPED
;
459 cupsdLogMessage(CUPSD_LOG_ERROR
,
460 "Syntax error on line %d of classes.conf.",
463 else if (!strcasecmp(line
, "StateMessage"))
466 * Set the initial queue state message...
470 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
472 else if (!strcasecmp(line
, "StateTime"))
475 * Set the state time...
479 p
->state_time
= atoi(value
);
481 else if (!strcasecmp(line
, "Accepting"))
484 * Set the initial accepting state...
488 (!strcasecmp(value
, "yes") ||
489 !strcasecmp(value
, "on") ||
490 !strcasecmp(value
, "true")))
493 (!strcasecmp(value
, "no") ||
494 !strcasecmp(value
, "off") ||
495 !strcasecmp(value
, "false")))
498 cupsdLogMessage(CUPSD_LOG_ERROR
,
499 "Syntax error on line %d of classes.conf.",
502 else if (!strcasecmp(line
, "Shared"))
505 * Set the initial shared state...
509 (!strcasecmp(value
, "yes") ||
510 !strcasecmp(value
, "on") ||
511 !strcasecmp(value
, "true")))
514 (!strcasecmp(value
, "no") ||
515 !strcasecmp(value
, "off") ||
516 !strcasecmp(value
, "false")))
519 cupsdLogMessage(CUPSD_LOG_ERROR
,
520 "Syntax error on line %d of classes.conf.",
523 else if (!strcasecmp(line
, "JobSheets"))
526 * Set the initial job sheets...
531 for (valueptr
= value
;
532 *valueptr
&& !isspace(*valueptr
& 255);
538 cupsdSetString(&p
->job_sheets
[0], value
);
540 while (isspace(*valueptr
& 255))
545 for (value
= valueptr
;
546 *valueptr
&& !isspace(*valueptr
& 255);
552 cupsdSetString(&p
->job_sheets
[1], value
);
556 cupsdLogMessage(CUPSD_LOG_ERROR
,
557 "Syntax error on line %d of classes.conf.", linenum
);
559 else if (!strcasecmp(line
, "AllowUser"))
564 cupsdAddPrinterUser(p
, value
);
567 cupsdLogMessage(CUPSD_LOG_ERROR
,
568 "Syntax error on line %d of classes.conf.", linenum
);
570 else if (!strcasecmp(line
, "DenyUser"))
575 cupsdAddPrinterUser(p
, value
);
578 cupsdLogMessage(CUPSD_LOG_ERROR
,
579 "Syntax error on line %d of classes.conf.", linenum
);
581 else if (!strcasecmp(line
, "QuotaPeriod"))
584 p
->quota_period
= atoi(value
);
586 cupsdLogMessage(CUPSD_LOG_ERROR
,
587 "Syntax error on line %d of classes.conf.", linenum
);
589 else if (!strcasecmp(line
, "PageLimit"))
592 p
->page_limit
= atoi(value
);
594 cupsdLogMessage(CUPSD_LOG_ERROR
,
595 "Syntax error on line %d of classes.conf.", linenum
);
597 else if (!strcasecmp(line
, "KLimit"))
600 p
->k_limit
= atoi(value
);
602 cupsdLogMessage(CUPSD_LOG_ERROR
,
603 "Syntax error on line %d of classes.conf.", linenum
);
605 else if (!strcasecmp(line
, "OpPolicy"))
609 cupsd_policy_t
*pol
; /* Policy */
612 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
614 cupsdSetString(&p
->op_policy
, value
);
615 p
->op_policy_ptr
= pol
;
618 cupsdLogMessage(CUPSD_LOG_ERROR
,
619 "Bad policy \"%s\" on line %d of classes.conf",
623 cupsdLogMessage(CUPSD_LOG_ERROR
,
624 "Syntax error on line %d of classes.conf.", linenum
);
626 else if (!strcasecmp(line
, "ErrorPolicy"))
629 cupsdSetString(&p
->error_policy
, value
);
631 cupsdLogMessage(CUPSD_LOG_ERROR
,
632 "Syntax error on line %d of classes.conf.", linenum
);
637 * Something else we don't understand...
640 cupsdLogMessage(CUPSD_LOG_ERROR
,
641 "Unknown configuration directive %s on line %d of classes.conf.",
651 * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file.
655 cupsdSaveAllClasses(void)
657 cups_file_t
*fp
; /* classes.conf file */
658 char temp
[1024], /* Temporary string */
659 backup
[1024], /* printers.conf.O file */
660 value
[2048]; /* Value string */
661 cupsd_printer_t
*pclass
; /* Current printer class */
662 int i
; /* Looping var */
663 time_t curtime
; /* Current time */
664 struct tm
*curdate
; /* Current date */
665 cups_option_t
*option
; /* Current option */
669 * Create the classes.conf file...
672 snprintf(temp
, sizeof(temp
), "%s/classes.conf", ServerRoot
);
673 snprintf(backup
, sizeof(backup
), "%s/classes.conf.O", ServerRoot
);
675 if (rename(temp
, backup
))
678 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup classes.conf - %s",
682 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
684 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save classes.conf - %s",
687 if (rename(backup
, temp
))
688 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to restore classes.conf - %s",
693 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving classes.conf...");
696 * Restrict access to the file...
699 fchown(cupsFileNumber(fp
), RunUser
, Group
);
700 fchmod(cupsFileNumber(fp
), 0600);
703 * Write a small header to the file...
706 curtime
= time(NULL
);
707 curdate
= localtime(&curtime
);
708 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
710 cupsFilePuts(fp
, "# Class configuration file for " CUPS_SVERSION
"\n");
711 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
714 * Write each local class known to the system...
717 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
719 pclass
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
722 * Skip remote destinations and regular printers...
725 if ((pclass
->type
& CUPS_PRINTER_REMOTE
) ||
726 (pclass
->type
& CUPS_PRINTER_IMPLICIT
) ||
727 !(pclass
->type
& CUPS_PRINTER_CLASS
))
731 * Write printers as needed...
734 if (pclass
== DefaultPrinter
)
735 cupsFilePrintf(fp
, "<DefaultClass %s>\n", pclass
->name
);
737 cupsFilePrintf(fp
, "<Class %s>\n", pclass
->name
);
739 if (pclass
->num_auth_info_required
> 0)
741 switch (pclass
->num_auth_info_required
)
744 strlcpy(value
, pclass
->auth_info_required
[0], sizeof(value
));
748 snprintf(value
, sizeof(value
), "%s,%s",
749 pclass
->auth_info_required
[0],
750 pclass
->auth_info_required
[1]);
755 snprintf(value
, sizeof(value
), "%s,%s,%s",
756 pclass
->auth_info_required
[0],
757 pclass
->auth_info_required
[1],
758 pclass
->auth_info_required
[2]);
762 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
766 cupsFilePutConf(fp
, "Info", pclass
->info
);
768 if (pclass
->location
)
769 cupsFilePutConf(fp
, "Location", pclass
->location
);
771 if (pclass
->state
== IPP_PRINTER_STOPPED
)
772 cupsFilePuts(fp
, "State Stopped\n");
774 cupsFilePuts(fp
, "State Idle\n");
776 cupsFilePrintf(fp
, "StateTime %d\n", (int)pclass
->state_time
);
778 if (pclass
->accepting
)
779 cupsFilePuts(fp
, "Accepting Yes\n");
781 cupsFilePuts(fp
, "Accepting No\n");
784 cupsFilePuts(fp
, "Shared Yes\n");
786 cupsFilePuts(fp
, "Shared No\n");
788 snprintf(value
, sizeof(value
), "%s %s", pclass
->job_sheets
[0],
789 pclass
->job_sheets
[1]);
790 cupsFilePutConf(fp
, "JobSheets", value
);
792 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
793 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
794 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
796 for (i
= 0; i
< pclass
->num_users
; i
++)
797 cupsFilePutConf(fp
, pclass
->deny_users
? "DenyUser" : "AllowUser",
800 if (pclass
->op_policy
)
801 cupsFilePutConf(fp
, "OpPolicy", pclass
->op_policy
);
802 if (pclass
->error_policy
)
803 cupsFilePutConf(fp
, "ErrorPolicy", pclass
->error_policy
);
805 for (i
= pclass
->num_options
, option
= pclass
->options
;
809 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
810 cupsFilePutConf(fp
, "Option", value
);
813 cupsFilePuts(fp
, "</Class>\n");
821 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
826 cupsdUpdateImplicitClasses(void)
828 int i
; /* Looping var */
829 cupsd_printer_t
*pclass
; /* Current class */
830 int accepting
; /* printer-is-accepting-jobs value */
833 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
835 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
838 * Loop through the printers to come up with a composite state...
841 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
842 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
845 pclass
->accepting
= accepting
;
851 * End of "$Id: classes.c 7724 2008-07-14 06:06:06Z mike $".