]>
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
);
259 if (c
->num_printers
== 0)
263 * Make sure that the last printer is also a valid index into the printer
264 * array. If not, reset the last printer to 0...
267 if (c
->last_printer
>= c
->num_printers
)
271 * Loop through the printers in the class and return the first idle
272 * printer... We keep track of the last printer that we used so that
273 * a "round robin" type of scheduling is realized (otherwise the first
274 * server might be saturated with print jobs...)
276 * Thanks to Joel Fredrikson for helping us get this right!
279 for (i
= c
->last_printer
+ 1; ; i
++)
281 if (i
>= c
->num_printers
)
284 if (c
->printers
[i
]->accepting
&&
285 (c
->printers
[i
]->state
== IPP_PRINTER_IDLE
||
286 ((c
->printers
[i
]->type
& CUPS_PRINTER_REMOTE
) && !c
->printers
[i
]->job
)))
289 return (c
->printers
[i
]);
292 if (i
== c
->last_printer
)
301 * 'cupsdFindClass()' - Find the named class.
304 cupsd_printer_t
* /* O - Matching class or NULL */
305 cupsdFindClass(const char *name
) /* I - Name of class */
307 cupsd_printer_t
*c
; /* Current class/printer */
310 if ((c
= cupsdFindDest(name
)) != NULL
&&
311 (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
319 * 'cupsdLoadAllClasses()' - Load classes from the classes.conf file.
323 cupsdLoadAllClasses(void)
325 cups_file_t
*fp
; /* classes.conf file */
326 int linenum
; /* Current line number */
327 char line
[1024], /* Line from file */
328 *value
, /* Pointer to value */
329 *valueptr
; /* Pointer into value */
330 cupsd_printer_t
*p
, /* Current printer class */
331 *temp
; /* Temporary pointer to printer */
335 * Open the classes.conf file...
338 snprintf(line
, sizeof(line
), "%s/classes.conf", ServerRoot
);
339 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
342 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
348 * Read class configurations until we hit EOF...
354 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
357 * Decode the directive...
360 if (!strcasecmp(line
, "<Class") ||
361 !strcasecmp(line
, "<DefaultClass"))
364 * <Class name> or <DefaultClass name>
367 if (p
== NULL
&& value
)
369 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading class %s...", value
);
372 * Since prior classes may have implicitly defined this class,
373 * see if it already exists...
376 if ((p
= cupsdFindDest(value
)) != NULL
)
378 p
->type
= CUPS_PRINTER_CLASS
;
379 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/classes/%s", ServerName
,
381 cupsdSetString(&p
->error_policy
, "retry-job");
384 p
= cupsdAddClass(value
);
387 p
->state
= IPP_PRINTER_IDLE
;
389 if (!strcasecmp(line
, "<DefaultClass"))
394 cupsdLogMessage(CUPSD_LOG_ERROR
,
395 "Syntax error on line %d of classes.conf.", linenum
);
399 else if (!strcasecmp(line
, "</Class>"))
403 cupsdSetPrinterAttrs(p
);
408 cupsdLogMessage(CUPSD_LOG_ERROR
,
409 "Syntax error on line %d of classes.conf.", linenum
);
415 cupsdLogMessage(CUPSD_LOG_ERROR
,
416 "Syntax error on line %d of classes.conf.", linenum
);
419 else if (!strcasecmp(line
, "AuthInfoRequired"))
421 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
422 cupsdLogMessage(CUPSD_LOG_ERROR
,
423 "Bad AuthInfoRequired on line %d of classes.conf.",
426 else if (!strcasecmp(line
, "Info"))
429 cupsdSetString(&p
->info
, value
);
431 else if (!strcasecmp(line
, "Location"))
434 cupsdSetString(&p
->location
, value
);
436 else if (!strcasecmp(line
, "Option") && value
)
442 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
445 cupsdLogMessage(CUPSD_LOG_ERROR
,
446 "Syntax error on line %d of classes.conf.", linenum
);
449 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
451 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
455 else if (!strcasecmp(line
, "Printer"))
459 cupsdLogMessage(CUPSD_LOG_ERROR
,
460 "Syntax error on line %d of classes.conf.", linenum
);
463 else if ((temp
= cupsdFindPrinter(value
)) == NULL
)
465 cupsdLogMessage(CUPSD_LOG_WARN
,
466 "Unknown printer %s on line %d of classes.conf.",
470 * Add the missing remote printer...
473 if ((temp
= cupsdAddPrinter(value
)) != NULL
)
475 cupsdSetString(&temp
->make_model
, "Remote Printer on unknown");
477 temp
->state
= IPP_PRINTER_STOPPED
;
478 temp
->type
|= CUPS_PRINTER_REMOTE
;
479 temp
->browse_time
= 2147483647;
481 cupsdSetString(&temp
->location
, "Location Unknown");
482 cupsdSetString(&temp
->info
, "No Information Available");
483 temp
->hostname
[0] = '\0';
485 cupsdSetPrinterAttrs(temp
);
490 cupsdAddPrinterToClass(p
, temp
);
492 else if (!strcasecmp(line
, "State"))
495 * Set the initial queue state...
498 if (!strcasecmp(value
, "idle"))
499 p
->state
= IPP_PRINTER_IDLE
;
500 else if (!strcasecmp(value
, "stopped"))
501 p
->state
= IPP_PRINTER_STOPPED
;
504 cupsdLogMessage(CUPSD_LOG_ERROR
,
505 "Syntax error on line %d of classes.conf.",
510 else if (!strcasecmp(line
, "StateMessage"))
513 * Set the initial queue state message...
517 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
519 else if (!strcasecmp(line
, "StateTime"))
522 * Set the state time...
526 p
->state_time
= atoi(value
);
528 else if (!strcasecmp(line
, "Accepting"))
531 * Set the initial accepting state...
535 (!strcasecmp(value
, "yes") ||
536 !strcasecmp(value
, "on") ||
537 !strcasecmp(value
, "true")))
540 (!strcasecmp(value
, "no") ||
541 !strcasecmp(value
, "off") ||
542 !strcasecmp(value
, "false")))
546 cupsdLogMessage(CUPSD_LOG_ERROR
,
547 "Syntax error on line %d of classes.conf.",
552 else if (!strcasecmp(line
, "Shared"))
555 * Set the initial shared state...
559 (!strcasecmp(value
, "yes") ||
560 !strcasecmp(value
, "on") ||
561 !strcasecmp(value
, "true")))
564 (!strcasecmp(value
, "no") ||
565 !strcasecmp(value
, "off") ||
566 !strcasecmp(value
, "false")))
570 cupsdLogMessage(CUPSD_LOG_ERROR
,
571 "Syntax error on line %d of classes.conf.",
576 else if (!strcasecmp(line
, "JobSheets"))
579 * Set the initial job sheets...
584 for (valueptr
= value
;
585 *valueptr
&& !isspace(*valueptr
& 255);
591 cupsdSetString(&p
->job_sheets
[0], value
);
593 while (isspace(*valueptr
& 255))
598 for (value
= valueptr
;
599 *valueptr
&& !isspace(*valueptr
& 255);
605 cupsdSetString(&p
->job_sheets
[1], value
);
610 cupsdLogMessage(CUPSD_LOG_ERROR
,
611 "Syntax error on line %d of classes.conf.", linenum
);
615 else if (!strcasecmp(line
, "AllowUser"))
620 cupsdAddPrinterUser(p
, value
);
624 cupsdLogMessage(CUPSD_LOG_ERROR
,
625 "Syntax error on line %d of classes.conf.", linenum
);
629 else if (!strcasecmp(line
, "DenyUser"))
634 cupsdAddPrinterUser(p
, value
);
638 cupsdLogMessage(CUPSD_LOG_ERROR
,
639 "Syntax error on line %d of classes.conf.", linenum
);
643 else if (!strcasecmp(line
, "QuotaPeriod"))
646 p
->quota_period
= atoi(value
);
649 cupsdLogMessage(CUPSD_LOG_ERROR
,
650 "Syntax error on line %d of classes.conf.", linenum
);
654 else if (!strcasecmp(line
, "PageLimit"))
657 p
->page_limit
= atoi(value
);
660 cupsdLogMessage(CUPSD_LOG_ERROR
,
661 "Syntax error on line %d of classes.conf.", linenum
);
665 else if (!strcasecmp(line
, "KLimit"))
668 p
->k_limit
= atoi(value
);
671 cupsdLogMessage(CUPSD_LOG_ERROR
,
672 "Syntax error on line %d of classes.conf.", linenum
);
676 else if (!strcasecmp(line
, "OpPolicy"))
680 cupsd_policy_t
*pol
; /* Policy */
683 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
685 cupsdSetString(&p
->op_policy
, value
);
686 p
->op_policy_ptr
= pol
;
689 cupsdLogMessage(CUPSD_LOG_ERROR
,
690 "Bad policy \"%s\" on line %d of classes.conf",
695 cupsdLogMessage(CUPSD_LOG_ERROR
,
696 "Syntax error on line %d of classes.conf.", linenum
);
700 else if (!strcasecmp(line
, "ErrorPolicy"))
703 cupsdSetString(&p
->error_policy
, value
);
706 cupsdLogMessage(CUPSD_LOG_ERROR
,
707 "Syntax error on line %d of classes.conf.", linenum
);
714 * Something else we don't understand...
717 cupsdLogMessage(CUPSD_LOG_ERROR
,
718 "Unknown configuration directive %s on line %d of classes.conf.",
728 * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file.
732 cupsdSaveAllClasses(void)
734 cups_file_t
*fp
; /* classes.conf file */
735 char temp
[1024]; /* Temporary string */
736 char backup
[1024]; /* classes.conf.O file */
737 cupsd_printer_t
*pclass
; /* Current printer class */
738 int i
; /* Looping var */
739 time_t curtime
; /* Current time */
740 struct tm
*curdate
; /* Current date */
741 cups_option_t
*option
; /* Current option */
742 const char *ptr
; /* Pointer into info/location */
746 * Create the classes.conf file...
749 snprintf(temp
, sizeof(temp
), "%s/classes.conf", ServerRoot
);
750 snprintf(backup
, sizeof(backup
), "%s/classes.conf.O", ServerRoot
);
752 if (rename(temp
, backup
))
755 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup classes.conf - %s",
759 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
761 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save classes.conf - %s",
764 if (rename(backup
, temp
))
765 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to restore classes.conf - %s",
770 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving classes.conf...");
773 * Restrict access to the file...
776 fchown(cupsFileNumber(fp
), RunUser
, Group
);
777 fchmod(cupsFileNumber(fp
), 0600);
780 * Write a small header to the file...
783 curtime
= time(NULL
);
784 curdate
= localtime(&curtime
);
785 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
787 cupsFilePuts(fp
, "# Class configuration file for " CUPS_SVERSION
"\n");
788 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
791 * Write each local class known to the system...
794 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
796 pclass
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
799 * Skip remote destinations and regular printers...
802 if ((pclass
->type
& CUPS_PRINTER_REMOTE
) ||
803 (pclass
->type
& CUPS_PRINTER_IMPLICIT
) ||
804 !(pclass
->type
& CUPS_PRINTER_CLASS
))
808 * Write printers as needed...
811 if (pclass
== DefaultPrinter
)
812 cupsFilePrintf(fp
, "<DefaultClass %s>\n", pclass
->name
);
814 cupsFilePrintf(fp
, "<Class %s>\n", pclass
->name
);
816 if (pclass
->num_auth_info_required
> 0)
818 cupsFilePrintf(fp
, "AuthInfoRequired %s", pclass
->auth_info_required
[0]);
819 for (i
= 1; i
< pclass
->num_auth_info_required
; i
++)
820 cupsFilePrintf(fp
, ",%s", pclass
->auth_info_required
[i
]);
821 cupsFilePutChar(fp
, '\n');
826 if ((ptr
= strchr(pclass
->info
, '#')) != NULL
)
829 * Need to quote the first # in the info string...
832 cupsFilePuts(fp
, "Info ");
833 cupsFileWrite(fp
, pclass
->info
, ptr
- pclass
->info
);
834 cupsFilePutChar(fp
, '\\');
835 cupsFilePuts(fp
, ptr
);
836 cupsFilePutChar(fp
, '\n');
839 cupsFilePrintf(fp
, "Info %s\n", pclass
->info
);
842 if (pclass
->location
)
844 if ((ptr
= strchr(pclass
->info
, '#')) != NULL
)
847 * Need to quote the first # in the location string...
850 cupsFilePuts(fp
, "Location ");
851 cupsFileWrite(fp
, pclass
->location
, ptr
- pclass
->location
);
852 cupsFilePutChar(fp
, '\\');
853 cupsFilePuts(fp
, ptr
);
854 cupsFilePutChar(fp
, '\n');
857 cupsFilePrintf(fp
, "Location %s\n", pclass
->location
);
860 if (pclass
->state
== IPP_PRINTER_STOPPED
)
862 cupsFilePuts(fp
, "State Stopped\n");
863 cupsFilePrintf(fp
, "StateMessage %s\n", pclass
->state_message
);
866 cupsFilePuts(fp
, "State Idle\n");
868 cupsFilePrintf(fp
, "StateTime %d\n", (int)pclass
->state_time
);
870 if (pclass
->accepting
)
871 cupsFilePuts(fp
, "Accepting Yes\n");
873 cupsFilePuts(fp
, "Accepting No\n");
876 cupsFilePuts(fp
, "Shared Yes\n");
878 cupsFilePuts(fp
, "Shared No\n");
880 cupsFilePrintf(fp
, "JobSheets %s %s\n", pclass
->job_sheets
[0],
881 pclass
->job_sheets
[1]);
883 for (i
= 0; i
< pclass
->num_users
; i
++)
885 if ((ptr
= strchr(pclass
->users
[i
], '#')) != NULL
)
888 * Need to quote the first # in the user string...
891 cupsFilePrintf(fp
, "%sUser ", pclass
->deny_users
? "Deny" : "Allow");
892 cupsFileWrite(fp
, pclass
->users
[i
], ptr
- pclass
->users
[i
]);
893 cupsFilePutChar(fp
, '\\');
894 cupsFilePuts(fp
, ptr
);
895 cupsFilePutChar(fp
, '\n');
898 cupsFilePrintf(fp
, "%sUser %s\n",
899 pclass
->deny_users
? "Deny" : "Allow",
903 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
904 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
905 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
907 for (i
= 0; i
< pclass
->num_users
; i
++)
908 cupsFilePrintf(fp
, "%sUser %s\n", pclass
->deny_users
? "Deny" : "Allow",
911 if (pclass
->op_policy
)
912 cupsFilePrintf(fp
, "OpPolicy %s\n", pclass
->op_policy
);
913 if (pclass
->error_policy
)
914 cupsFilePrintf(fp
, "ErrorPolicy %s\n", pclass
->error_policy
);
916 for (i
= pclass
->num_options
, option
= pclass
->options
;
919 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
921 cupsFilePuts(fp
, "</Class>\n");
929 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
934 cupsdUpdateImplicitClasses(void)
936 int i
; /* Looping var */
937 cupsd_printer_t
*pclass
; /* Current class */
938 int accepting
; /* printer-is-accepting-jobs value */
941 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
943 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
946 * Loop through the printers to come up with a composite state...
949 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
950 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
953 pclass
->accepting
= accepting
;
959 * End of "$Id: classes.c 6649 2007-07-11 21:46:42Z mike $".