]>
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-2008 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 */
126 cups_ptype_t type
, /* Class type */
127 oldtype
; /* Old class type */
131 * See if the printer is in the class...
134 for (i
= 0; i
< c
->num_printers
; i
++)
135 if (p
== c
->printers
[i
])
139 * If it is, remove it from the list...
142 if (i
< c
->num_printers
)
145 * Yes, remove the printer...
149 if (i
< c
->num_printers
)
150 memmove(c
->printers
+ i
, c
->printers
+ i
+ 1,
151 (c
->num_printers
- i
) * sizeof(cupsd_printer_t
*));
157 * Recompute the printer type mask as needed...
160 if (c
->num_printers
> 0)
163 type
= c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
);
164 c
->type
= ~CUPS_PRINTER_REMOTE
;
166 for (i
= 0; i
< c
->num_printers
; i
++)
167 c
->type
&= c
->printers
[i
]->type
;
172 * Update the IPP attributes...
175 if (c
->type
!= oldtype
)
176 cupsdSetPrinterAttrs(c
);
182 * 'cupsdDeletePrinterFromClasses()' - Delete a printer from all classes.
186 cupsdDeletePrinterFromClasses(
187 cupsd_printer_t
*p
) /* I - Printer to delete */
189 cupsd_printer_t
*c
; /* Pointer to current class */
193 * Loop through the printer/class list and remove the printer
194 * from each class listed...
197 for (c
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
199 c
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
200 if (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
201 cupsdDeletePrinterFromClass(c
, p
);
204 * Then clean out any empty implicit classes...
207 for (c
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
209 c
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
210 if (c
->num_printers
== 0)
212 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Deleting implicit class \"%s\"...",
214 cupsdDeletePrinter(c
, 0);
220 * 'cupsdFindAvailablePrinter()' - Find an available printer in a class.
223 cupsd_printer_t
* /* O - Available printer or NULL */
224 cupsdFindAvailablePrinter(
225 const char *name
) /* I - Class to check */
227 int i
; /* Looping var */
228 cupsd_printer_t
*c
; /* Printer class */
235 if ((c
= cupsdFindClass(name
)) == NULL
)
237 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to find class \"%s\"!", name
);
241 if (c
->num_printers
== 0)
245 * Make sure that the last printer is also a valid index into the printer
246 * array. If not, reset the last printer to 0...
249 if (c
->last_printer
>= c
->num_printers
)
253 * Loop through the printers in the class and return the first idle
254 * printer... We keep track of the last printer that we used so that
255 * a "round robin" type of scheduling is realized (otherwise the first
256 * server might be saturated with print jobs...)
258 * Thanks to Joel Fredrikson for helping us get this right!
261 for (i
= c
->last_printer
+ 1; ; i
++)
263 if (i
>= c
->num_printers
)
266 if (c
->printers
[i
]->accepting
&&
267 (c
->printers
[i
]->state
== IPP_PRINTER_IDLE
||
268 ((c
->printers
[i
]->type
& CUPS_PRINTER_REMOTE
) && !c
->printers
[i
]->job
)))
271 return (c
->printers
[i
]);
274 if (i
== c
->last_printer
)
283 * 'cupsdFindClass()' - Find the named class.
286 cupsd_printer_t
* /* O - Matching class or NULL */
287 cupsdFindClass(const char *name
) /* I - Name of class */
289 cupsd_printer_t
*c
; /* Current class/printer */
292 if ((c
= cupsdFindDest(name
)) != NULL
&&
293 (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
301 * 'cupsdLoadAllClasses()' - Load classes from the classes.conf file.
305 cupsdLoadAllClasses(void)
307 cups_file_t
*fp
; /* classes.conf file */
308 int linenum
; /* Current line number */
309 char line
[1024], /* Line from file */
310 *value
, /* Pointer to value */
311 *valueptr
; /* Pointer into value */
312 cupsd_printer_t
*p
, /* Current printer class */
313 *temp
; /* Temporary pointer to printer */
317 * Open the classes.conf file...
320 snprintf(line
, sizeof(line
), "%s/classes.conf", ServerRoot
);
321 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
324 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
330 * Read class configurations until we hit EOF...
336 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
339 * Decode the directive...
342 if (!strcasecmp(line
, "<Class") ||
343 !strcasecmp(line
, "<DefaultClass"))
346 * <Class name> or <DefaultClass name>
349 if (p
== NULL
&& value
)
351 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading class %s...", value
);
354 * Since prior classes may have implicitly defined this class,
355 * see if it already exists...
358 if ((p
= cupsdFindDest(value
)) != NULL
)
360 p
->type
= CUPS_PRINTER_CLASS
;
361 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/classes/%s", ServerName
,
363 cupsdSetString(&p
->error_policy
, "retry-job");
366 p
= cupsdAddClass(value
);
369 p
->state
= IPP_PRINTER_IDLE
;
371 if (!strcasecmp(line
, "<DefaultClass"))
375 cupsdLogMessage(CUPSD_LOG_ERROR
,
376 "Syntax error on line %d of classes.conf.", linenum
);
378 else if (!strcasecmp(line
, "</Class>"))
382 cupsdSetPrinterAttrs(p
);
386 cupsdLogMessage(CUPSD_LOG_ERROR
,
387 "Syntax error on line %d of classes.conf.", linenum
);
391 cupsdLogMessage(CUPSD_LOG_ERROR
,
392 "Syntax error on line %d of classes.conf.", linenum
);
394 else if (!strcasecmp(line
, "AuthInfoRequired"))
396 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
397 cupsdLogMessage(CUPSD_LOG_ERROR
,
398 "Bad AuthInfoRequired on line %d of classes.conf.",
401 else if (!strcasecmp(line
, "Info"))
404 cupsdSetString(&p
->info
, value
);
406 else if (!strcasecmp(line
, "Location"))
409 cupsdSetString(&p
->location
, value
);
411 else if (!strcasecmp(line
, "Option") && value
)
417 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
420 cupsdLogMessage(CUPSD_LOG_ERROR
,
421 "Syntax error on line %d of classes.conf.", linenum
);
424 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
426 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
430 else if (!strcasecmp(line
, "Printer"))
434 cupsdLogMessage(CUPSD_LOG_ERROR
,
435 "Syntax error on line %d of classes.conf.", linenum
);
438 else if ((temp
= cupsdFindPrinter(value
)) == NULL
)
440 cupsdLogMessage(CUPSD_LOG_WARN
,
441 "Unknown printer %s on line %d of classes.conf.",
445 * Add the missing remote printer...
448 if ((temp
= cupsdAddPrinter(value
)) != NULL
)
450 cupsdSetString(&temp
->make_model
, "Remote Printer on unknown");
452 temp
->state
= IPP_PRINTER_STOPPED
;
453 temp
->type
|= CUPS_PRINTER_REMOTE
;
454 temp
->browse_time
= 2147483647;
456 cupsdSetString(&temp
->location
, "Location Unknown");
457 cupsdSetString(&temp
->info
, "No Information Available");
458 temp
->hostname
[0] = '\0';
460 cupsdSetPrinterAttrs(temp
);
465 cupsdAddPrinterToClass(p
, temp
);
467 else if (!strcasecmp(line
, "State"))
470 * Set the initial queue state...
473 if (!strcasecmp(value
, "idle"))
474 p
->state
= IPP_PRINTER_IDLE
;
475 else if (!strcasecmp(value
, "stopped"))
476 p
->state
= IPP_PRINTER_STOPPED
;
478 cupsdLogMessage(CUPSD_LOG_ERROR
,
479 "Syntax error on line %d of classes.conf.",
482 else if (!strcasecmp(line
, "StateMessage"))
485 * Set the initial queue state message...
489 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
491 else if (!strcasecmp(line
, "StateTime"))
494 * Set the state time...
498 p
->state_time
= atoi(value
);
500 else if (!strcasecmp(line
, "Accepting"))
503 * Set the initial accepting state...
507 (!strcasecmp(value
, "yes") ||
508 !strcasecmp(value
, "on") ||
509 !strcasecmp(value
, "true")))
512 (!strcasecmp(value
, "no") ||
513 !strcasecmp(value
, "off") ||
514 !strcasecmp(value
, "false")))
517 cupsdLogMessage(CUPSD_LOG_ERROR
,
518 "Syntax error on line %d of classes.conf.",
521 else if (!strcasecmp(line
, "Shared"))
524 * Set the initial shared state...
528 (!strcasecmp(value
, "yes") ||
529 !strcasecmp(value
, "on") ||
530 !strcasecmp(value
, "true")))
533 (!strcasecmp(value
, "no") ||
534 !strcasecmp(value
, "off") ||
535 !strcasecmp(value
, "false")))
538 cupsdLogMessage(CUPSD_LOG_ERROR
,
539 "Syntax error on line %d of classes.conf.",
542 else if (!strcasecmp(line
, "JobSheets"))
545 * Set the initial job sheets...
550 for (valueptr
= value
;
551 *valueptr
&& !isspace(*valueptr
& 255);
557 cupsdSetString(&p
->job_sheets
[0], value
);
559 while (isspace(*valueptr
& 255))
564 for (value
= valueptr
;
565 *valueptr
&& !isspace(*valueptr
& 255);
571 cupsdSetString(&p
->job_sheets
[1], value
);
575 cupsdLogMessage(CUPSD_LOG_ERROR
,
576 "Syntax error on line %d of classes.conf.", linenum
);
578 else if (!strcasecmp(line
, "AllowUser"))
583 cupsdAddPrinterUser(p
, value
);
586 cupsdLogMessage(CUPSD_LOG_ERROR
,
587 "Syntax error on line %d of classes.conf.", linenum
);
589 else if (!strcasecmp(line
, "DenyUser"))
594 cupsdAddPrinterUser(p
, value
);
597 cupsdLogMessage(CUPSD_LOG_ERROR
,
598 "Syntax error on line %d of classes.conf.", linenum
);
600 else if (!strcasecmp(line
, "QuotaPeriod"))
603 p
->quota_period
= atoi(value
);
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "Syntax error on line %d of classes.conf.", linenum
);
608 else if (!strcasecmp(line
, "PageLimit"))
611 p
->page_limit
= atoi(value
);
613 cupsdLogMessage(CUPSD_LOG_ERROR
,
614 "Syntax error on line %d of classes.conf.", linenum
);
616 else if (!strcasecmp(line
, "KLimit"))
619 p
->k_limit
= atoi(value
);
621 cupsdLogMessage(CUPSD_LOG_ERROR
,
622 "Syntax error on line %d of classes.conf.", linenum
);
624 else if (!strcasecmp(line
, "OpPolicy"))
628 cupsd_policy_t
*pol
; /* Policy */
631 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
633 cupsdSetString(&p
->op_policy
, value
);
634 p
->op_policy_ptr
= pol
;
637 cupsdLogMessage(CUPSD_LOG_ERROR
,
638 "Bad policy \"%s\" on line %d of classes.conf",
642 cupsdLogMessage(CUPSD_LOG_ERROR
,
643 "Syntax error on line %d of classes.conf.", linenum
);
645 else if (!strcasecmp(line
, "ErrorPolicy"))
648 cupsdSetString(&p
->error_policy
, value
);
650 cupsdLogMessage(CUPSD_LOG_ERROR
,
651 "Syntax error on line %d of classes.conf.", linenum
);
656 * Something else we don't understand...
659 cupsdLogMessage(CUPSD_LOG_ERROR
,
660 "Unknown configuration directive %s on line %d of classes.conf.",
670 * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file.
674 cupsdSaveAllClasses(void)
676 cups_file_t
*fp
; /* classes.conf file */
677 char temp
[1024], /* Temporary string */
678 backup
[1024], /* printers.conf.O file */
679 value
[2048]; /* Value string */
680 cupsd_printer_t
*pclass
; /* Current printer class */
681 int i
; /* Looping var */
682 time_t curtime
; /* Current time */
683 struct tm
*curdate
; /* Current date */
684 cups_option_t
*option
; /* Current option */
688 * Create the classes.conf file...
691 snprintf(temp
, sizeof(temp
), "%s/classes.conf", ServerRoot
);
692 snprintf(backup
, sizeof(backup
), "%s/classes.conf.O", ServerRoot
);
694 if (rename(temp
, backup
))
697 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup classes.conf - %s",
701 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
703 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save classes.conf - %s",
706 if (rename(backup
, temp
))
707 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to restore classes.conf - %s",
712 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving classes.conf...");
715 * Restrict access to the file...
718 fchown(cupsFileNumber(fp
), RunUser
, Group
);
719 fchmod(cupsFileNumber(fp
), 0600);
722 * Write a small header to the file...
725 curtime
= time(NULL
);
726 curdate
= localtime(&curtime
);
727 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
729 cupsFilePuts(fp
, "# Class configuration file for " CUPS_SVERSION
"\n");
730 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
733 * Write each local class known to the system...
736 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
738 pclass
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
741 * Skip remote destinations and regular printers...
744 if ((pclass
->type
& CUPS_PRINTER_REMOTE
) ||
745 (pclass
->type
& CUPS_PRINTER_IMPLICIT
) ||
746 !(pclass
->type
& CUPS_PRINTER_CLASS
))
750 * Write printers as needed...
753 if (pclass
== DefaultPrinter
)
754 cupsFilePrintf(fp
, "<DefaultClass %s>\n", pclass
->name
);
756 cupsFilePrintf(fp
, "<Class %s>\n", pclass
->name
);
758 if (pclass
->num_auth_info_required
> 0)
760 switch (pclass
->num_auth_info_required
)
763 strlcpy(value
, pclass
->auth_info_required
[0], sizeof(value
));
767 snprintf(value
, sizeof(value
), "%s,%s",
768 pclass
->auth_info_required
[0],
769 pclass
->auth_info_required
[1]);
774 snprintf(value
, sizeof(value
), "%s,%s,%s",
775 pclass
->auth_info_required
[0],
776 pclass
->auth_info_required
[1],
777 pclass
->auth_info_required
[2]);
781 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
785 cupsFilePutConf(fp
, "Info", pclass
->info
);
787 if (pclass
->location
)
788 cupsFilePutConf(fp
, "Location", pclass
->location
);
790 if (pclass
->state
== IPP_PRINTER_STOPPED
)
791 cupsFilePuts(fp
, "State Stopped\n");
793 cupsFilePuts(fp
, "State Idle\n");
795 cupsFilePrintf(fp
, "StateTime %d\n", (int)pclass
->state_time
);
797 if (pclass
->accepting
)
798 cupsFilePuts(fp
, "Accepting Yes\n");
800 cupsFilePuts(fp
, "Accepting No\n");
803 cupsFilePuts(fp
, "Shared Yes\n");
805 cupsFilePuts(fp
, "Shared No\n");
807 snprintf(value
, sizeof(value
), "%s %s", pclass
->job_sheets
[0],
808 pclass
->job_sheets
[1]);
809 cupsFilePutConf(fp
, "JobSheets", value
);
811 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
812 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
813 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
815 for (i
= 0; i
< pclass
->num_users
; i
++)
816 cupsFilePutConf(fp
, pclass
->deny_users
? "DenyUser" : "AllowUser",
819 if (pclass
->op_policy
)
820 cupsFilePutConf(fp
, "OpPolicy", pclass
->op_policy
);
821 if (pclass
->error_policy
)
822 cupsFilePutConf(fp
, "ErrorPolicy", pclass
->error_policy
);
824 for (i
= pclass
->num_options
, option
= pclass
->options
;
828 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
829 cupsFilePutConf(fp
, "Option", value
);
832 cupsFilePuts(fp
, "</Class>\n");
840 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
845 cupsdUpdateImplicitClasses(void)
847 int i
; /* Looping var */
848 cupsd_printer_t
*pclass
; /* Current class */
849 int accepting
; /* printer-is-accepting-jobs value */
852 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
854 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
857 * Loop through the printers to come up with a composite state...
860 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
861 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
864 pclass
->accepting
= accepting
;
870 * End of "$Id: classes.c 7724 2008-07-14 06:06:06Z mike $".