]>
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
[4096], /* 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"))
458 p
->state
= IPP_PRINTER_STOPPED
;
459 cupsdSetPrinterReasons(p
, "+paused");
462 cupsdLogMessage(CUPSD_LOG_ERROR
,
463 "Syntax error on line %d of classes.conf.",
466 else if (!strcasecmp(line
, "StateMessage"))
469 * Set the initial queue state message...
473 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
475 else if (!strcasecmp(line
, "StateTime"))
478 * Set the state time...
482 p
->state_time
= atoi(value
);
484 else if (!strcasecmp(line
, "Accepting"))
487 * Set the initial accepting state...
491 (!strcasecmp(value
, "yes") ||
492 !strcasecmp(value
, "on") ||
493 !strcasecmp(value
, "true")))
496 (!strcasecmp(value
, "no") ||
497 !strcasecmp(value
, "off") ||
498 !strcasecmp(value
, "false")))
501 cupsdLogMessage(CUPSD_LOG_ERROR
,
502 "Syntax error on line %d of classes.conf.",
505 else if (!strcasecmp(line
, "Shared"))
508 * Set the initial shared state...
512 (!strcasecmp(value
, "yes") ||
513 !strcasecmp(value
, "on") ||
514 !strcasecmp(value
, "true")))
517 (!strcasecmp(value
, "no") ||
518 !strcasecmp(value
, "off") ||
519 !strcasecmp(value
, "false")))
522 cupsdLogMessage(CUPSD_LOG_ERROR
,
523 "Syntax error on line %d of classes.conf.",
526 else if (!strcasecmp(line
, "JobSheets"))
529 * Set the initial job sheets...
534 for (valueptr
= value
;
535 *valueptr
&& !isspace(*valueptr
& 255);
541 cupsdSetString(&p
->job_sheets
[0], value
);
543 while (isspace(*valueptr
& 255))
548 for (value
= valueptr
;
549 *valueptr
&& !isspace(*valueptr
& 255);
555 cupsdSetString(&p
->job_sheets
[1], value
);
559 cupsdLogMessage(CUPSD_LOG_ERROR
,
560 "Syntax error on line %d of classes.conf.", linenum
);
562 else if (!strcasecmp(line
, "AllowUser"))
567 cupsdAddPrinterUser(p
, value
);
570 cupsdLogMessage(CUPSD_LOG_ERROR
,
571 "Syntax error on line %d of classes.conf.", linenum
);
573 else if (!strcasecmp(line
, "DenyUser"))
578 cupsdAddPrinterUser(p
, value
);
581 cupsdLogMessage(CUPSD_LOG_ERROR
,
582 "Syntax error on line %d of classes.conf.", linenum
);
584 else if (!strcasecmp(line
, "QuotaPeriod"))
587 p
->quota_period
= atoi(value
);
589 cupsdLogMessage(CUPSD_LOG_ERROR
,
590 "Syntax error on line %d of classes.conf.", linenum
);
592 else if (!strcasecmp(line
, "PageLimit"))
595 p
->page_limit
= atoi(value
);
597 cupsdLogMessage(CUPSD_LOG_ERROR
,
598 "Syntax error on line %d of classes.conf.", linenum
);
600 else if (!strcasecmp(line
, "KLimit"))
603 p
->k_limit
= atoi(value
);
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "Syntax error on line %d of classes.conf.", linenum
);
608 else if (!strcasecmp(line
, "OpPolicy"))
612 cupsd_policy_t
*pol
; /* Policy */
615 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
617 cupsdSetString(&p
->op_policy
, value
);
618 p
->op_policy_ptr
= pol
;
621 cupsdLogMessage(CUPSD_LOG_ERROR
,
622 "Bad policy \"%s\" on line %d of classes.conf",
626 cupsdLogMessage(CUPSD_LOG_ERROR
,
627 "Syntax error on line %d of classes.conf.", linenum
);
629 else if (!strcasecmp(line
, "ErrorPolicy"))
632 cupsdSetString(&p
->error_policy
, value
);
634 cupsdLogMessage(CUPSD_LOG_ERROR
,
635 "Syntax error on line %d of classes.conf.", linenum
);
640 * Something else we don't understand...
643 cupsdLogMessage(CUPSD_LOG_ERROR
,
644 "Unknown configuration directive %s on line %d of classes.conf.",
654 * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file.
658 cupsdSaveAllClasses(void)
660 cups_file_t
*fp
; /* classes.conf file */
661 char temp
[1024], /* Temporary string */
662 backup
[1024], /* printers.conf.O file */
663 value
[2048]; /* Value string */
664 cupsd_printer_t
*pclass
; /* Current printer class */
665 int i
; /* Looping var */
666 time_t curtime
; /* Current time */
667 struct tm
*curdate
; /* Current date */
668 cups_option_t
*option
; /* Current option */
672 * Create the classes.conf file...
675 snprintf(temp
, sizeof(temp
), "%s/classes.conf", ServerRoot
);
676 snprintf(backup
, sizeof(backup
), "%s/classes.conf.O", ServerRoot
);
678 if (rename(temp
, backup
))
681 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup classes.conf - %s",
685 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
687 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save classes.conf - %s",
690 if (rename(backup
, temp
))
691 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to restore classes.conf - %s",
696 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving classes.conf...");
699 * Restrict access to the file...
702 fchown(cupsFileNumber(fp
), RunUser
, Group
);
703 fchmod(cupsFileNumber(fp
), 0600);
706 * Write a small header to the file...
709 curtime
= time(NULL
);
710 curdate
= localtime(&curtime
);
711 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
713 cupsFilePuts(fp
, "# Class configuration file for " CUPS_SVERSION
"\n");
714 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
717 * Write each local class known to the system...
720 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
722 pclass
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
725 * Skip remote destinations and regular printers...
728 if ((pclass
->type
& CUPS_PRINTER_REMOTE
) ||
729 (pclass
->type
& CUPS_PRINTER_IMPLICIT
) ||
730 !(pclass
->type
& CUPS_PRINTER_CLASS
))
734 * Write printers as needed...
737 if (pclass
== DefaultPrinter
)
738 cupsFilePrintf(fp
, "<DefaultClass %s>\n", pclass
->name
);
740 cupsFilePrintf(fp
, "<Class %s>\n", pclass
->name
);
742 if (pclass
->num_auth_info_required
> 0)
744 switch (pclass
->num_auth_info_required
)
747 strlcpy(value
, pclass
->auth_info_required
[0], sizeof(value
));
751 snprintf(value
, sizeof(value
), "%s,%s",
752 pclass
->auth_info_required
[0],
753 pclass
->auth_info_required
[1]);
758 snprintf(value
, sizeof(value
), "%s,%s,%s",
759 pclass
->auth_info_required
[0],
760 pclass
->auth_info_required
[1],
761 pclass
->auth_info_required
[2]);
765 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
769 cupsFilePutConf(fp
, "Info", pclass
->info
);
771 if (pclass
->location
)
772 cupsFilePutConf(fp
, "Location", pclass
->location
);
774 if (pclass
->state
== IPP_PRINTER_STOPPED
)
775 cupsFilePuts(fp
, "State Stopped\n");
777 cupsFilePuts(fp
, "State Idle\n");
779 cupsFilePrintf(fp
, "StateTime %d\n", (int)pclass
->state_time
);
781 if (pclass
->accepting
)
782 cupsFilePuts(fp
, "Accepting Yes\n");
784 cupsFilePuts(fp
, "Accepting No\n");
787 cupsFilePuts(fp
, "Shared Yes\n");
789 cupsFilePuts(fp
, "Shared No\n");
791 snprintf(value
, sizeof(value
), "%s %s", pclass
->job_sheets
[0],
792 pclass
->job_sheets
[1]);
793 cupsFilePutConf(fp
, "JobSheets", value
);
795 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
796 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
797 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
799 for (i
= 0; i
< pclass
->num_users
; i
++)
800 cupsFilePutConf(fp
, pclass
->deny_users
? "DenyUser" : "AllowUser",
803 if (pclass
->op_policy
)
804 cupsFilePutConf(fp
, "OpPolicy", pclass
->op_policy
);
805 if (pclass
->error_policy
)
806 cupsFilePutConf(fp
, "ErrorPolicy", pclass
->error_policy
);
808 for (i
= pclass
->num_options
, option
= pclass
->options
;
812 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
813 cupsFilePutConf(fp
, "Option", value
);
816 cupsFilePuts(fp
, "</Class>\n");
824 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
829 cupsdUpdateImplicitClasses(void)
831 int i
; /* Looping var */
832 cupsd_printer_t
*pclass
; /* Current class */
833 int accepting
; /* printer-is-accepting-jobs value */
836 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
838 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
841 * Loop through the printers to come up with a composite state...
844 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
845 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
848 pclass
->accepting
= accepting
;
854 * End of "$Id: classes.c 7724 2008-07-14 06:06:06Z mike $".