]>
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
, RemotePort
,
60 cupsdSetString(&c
->error_policy
, "retry-current-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 int i
; /* Looping var */
289 cups_file_t
*fp
; /* classes.conf file */
290 int linenum
; /* Current line number */
291 char line
[4096], /* Line from file */
292 *value
, /* Pointer to value */
293 *valueptr
; /* Pointer into value */
294 cupsd_printer_t
*p
, /* Current printer class */
295 *temp
; /* Temporary pointer to printer */
299 * Open the classes.conf file...
302 snprintf(line
, sizeof(line
), "%s/classes.conf", ServerRoot
);
303 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
306 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
312 * Read class configurations until we hit EOF...
318 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
321 * Decode the directive...
324 if (!strcasecmp(line
, "<Class") ||
325 !strcasecmp(line
, "<DefaultClass"))
328 * <Class name> or <DefaultClass name>
331 if (p
== NULL
&& value
)
333 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading class %s...", value
);
336 * Since prior classes may have implicitly defined this class,
337 * see if it already exists...
340 if ((p
= cupsdFindDest(value
)) != NULL
)
342 p
->type
= CUPS_PRINTER_CLASS
;
343 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/classes/%s", ServerName
,
345 cupsdSetString(&p
->error_policy
, "retry-job");
348 p
= cupsdAddClass(value
);
351 p
->state
= IPP_PRINTER_IDLE
;
353 if (!strcasecmp(line
, "<DefaultClass"))
357 cupsdLogMessage(CUPSD_LOG_ERROR
,
358 "Syntax error on line %d of classes.conf.", linenum
);
360 else if (!strcasecmp(line
, "</Class>"))
364 cupsdSetPrinterAttrs(p
);
368 cupsdLogMessage(CUPSD_LOG_ERROR
,
369 "Syntax error on line %d of classes.conf.", linenum
);
373 cupsdLogMessage(CUPSD_LOG_ERROR
,
374 "Syntax error on line %d of classes.conf.", linenum
);
376 else if (!strcasecmp(line
, "AuthInfoRequired"))
378 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
379 cupsdLogMessage(CUPSD_LOG_ERROR
,
380 "Bad AuthInfoRequired on line %d of classes.conf.",
383 else if (!strcasecmp(line
, "Info"))
386 cupsdSetString(&p
->info
, value
);
388 else if (!strcasecmp(line
, "Location"))
391 cupsdSetString(&p
->location
, value
);
393 else if (!strcasecmp(line
, "Option") && value
)
399 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
402 cupsdLogMessage(CUPSD_LOG_ERROR
,
403 "Syntax error on line %d of classes.conf.", linenum
);
406 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
408 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
412 else if (!strcasecmp(line
, "Printer"))
416 cupsdLogMessage(CUPSD_LOG_ERROR
,
417 "Syntax error on line %d of classes.conf.", linenum
);
420 else if ((temp
= cupsdFindPrinter(value
)) == NULL
)
422 cupsdLogMessage(CUPSD_LOG_WARN
,
423 "Unknown printer %s on line %d of classes.conf.",
427 * Add the missing remote printer...
430 if ((temp
= cupsdAddPrinter(value
)) != NULL
)
432 cupsdSetString(&temp
->make_model
, "Remote Printer on unknown");
434 temp
->state
= IPP_PRINTER_STOPPED
;
435 temp
->type
|= CUPS_PRINTER_REMOTE
;
436 temp
->browse_time
= 2147483647;
438 cupsdSetString(&temp
->location
, "Location Unknown");
439 cupsdSetString(&temp
->info
, "No Information Available");
440 temp
->hostname
[0] = '\0';
442 cupsdSetPrinterAttrs(temp
);
447 cupsdAddPrinterToClass(p
, temp
);
449 else if (!strcasecmp(line
, "State"))
452 * Set the initial queue state...
455 if (!strcasecmp(value
, "idle"))
456 p
->state
= IPP_PRINTER_IDLE
;
457 else if (!strcasecmp(value
, "stopped"))
459 p
->state
= IPP_PRINTER_STOPPED
;
461 for (i
= 0 ; i
< p
->num_reasons
; i
++)
462 if (!strcmp("paused", p
->reasons
[i
]))
465 if (i
>= p
->num_reasons
&&
466 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
468 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc("paused");
473 cupsdLogMessage(CUPSD_LOG_ERROR
,
474 "Syntax error on line %d of classes.conf.",
477 else if (!strcasecmp(line
, "StateMessage"))
480 * Set the initial queue state message...
484 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
486 else if (!strcasecmp(line
, "StateTime"))
489 * Set the state time...
493 p
->state_time
= atoi(value
);
495 else if (!strcasecmp(line
, "Accepting"))
498 * Set the initial accepting state...
502 (!strcasecmp(value
, "yes") ||
503 !strcasecmp(value
, "on") ||
504 !strcasecmp(value
, "true")))
507 (!strcasecmp(value
, "no") ||
508 !strcasecmp(value
, "off") ||
509 !strcasecmp(value
, "false")))
512 cupsdLogMessage(CUPSD_LOG_ERROR
,
513 "Syntax error on line %d of classes.conf.",
516 else if (!strcasecmp(line
, "Shared"))
519 * Set the initial shared state...
523 (!strcasecmp(value
, "yes") ||
524 !strcasecmp(value
, "on") ||
525 !strcasecmp(value
, "true")))
528 (!strcasecmp(value
, "no") ||
529 !strcasecmp(value
, "off") ||
530 !strcasecmp(value
, "false")))
533 cupsdLogMessage(CUPSD_LOG_ERROR
,
534 "Syntax error on line %d of classes.conf.",
537 else if (!strcasecmp(line
, "JobSheets"))
540 * Set the initial job sheets...
545 for (valueptr
= value
;
546 *valueptr
&& !isspace(*valueptr
& 255);
552 cupsdSetString(&p
->job_sheets
[0], value
);
554 while (isspace(*valueptr
& 255))
559 for (value
= valueptr
;
560 *valueptr
&& !isspace(*valueptr
& 255);
566 cupsdSetString(&p
->job_sheets
[1], value
);
570 cupsdLogMessage(CUPSD_LOG_ERROR
,
571 "Syntax error on line %d of classes.conf.", linenum
);
573 else if (!strcasecmp(line
, "AllowUser"))
578 cupsdAddPrinterUser(p
, value
);
581 cupsdLogMessage(CUPSD_LOG_ERROR
,
582 "Syntax error on line %d of classes.conf.", linenum
);
584 else if (!strcasecmp(line
, "DenyUser"))
589 cupsdAddPrinterUser(p
, value
);
592 cupsdLogMessage(CUPSD_LOG_ERROR
,
593 "Syntax error on line %d of classes.conf.", linenum
);
595 else if (!strcasecmp(line
, "QuotaPeriod"))
598 p
->quota_period
= atoi(value
);
600 cupsdLogMessage(CUPSD_LOG_ERROR
,
601 "Syntax error on line %d of classes.conf.", linenum
);
603 else if (!strcasecmp(line
, "PageLimit"))
606 p
->page_limit
= atoi(value
);
608 cupsdLogMessage(CUPSD_LOG_ERROR
,
609 "Syntax error on line %d of classes.conf.", linenum
);
611 else if (!strcasecmp(line
, "KLimit"))
614 p
->k_limit
= atoi(value
);
616 cupsdLogMessage(CUPSD_LOG_ERROR
,
617 "Syntax error on line %d of classes.conf.", linenum
);
619 else if (!strcasecmp(line
, "OpPolicy"))
623 cupsd_policy_t
*pol
; /* Policy */
626 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
628 cupsdSetString(&p
->op_policy
, value
);
629 p
->op_policy_ptr
= pol
;
632 cupsdLogMessage(CUPSD_LOG_ERROR
,
633 "Bad policy \"%s\" on line %d of classes.conf",
637 cupsdLogMessage(CUPSD_LOG_ERROR
,
638 "Syntax error on line %d of classes.conf.", linenum
);
640 else if (!strcasecmp(line
, "ErrorPolicy"))
644 if (strcmp(value
, "retry-current-job") && strcmp(value
, "retry-job"))
645 cupsdLogMessage(CUPSD_LOG_WARN
,
646 "ErrorPolicy %s ignored on line %d of classes.conf",
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 for (i
= 0; i
< pclass
->num_printers
; i
++)
812 cupsFilePrintf(fp
, "Printer %s\n", pclass
->printers
[i
]->name
);
814 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
815 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
816 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
818 for (i
= 0; i
< pclass
->num_users
; i
++)
819 cupsFilePutConf(fp
, pclass
->deny_users
? "DenyUser" : "AllowUser",
822 if (pclass
->op_policy
)
823 cupsFilePutConf(fp
, "OpPolicy", pclass
->op_policy
);
824 if (pclass
->error_policy
)
825 cupsFilePutConf(fp
, "ErrorPolicy", pclass
->error_policy
);
827 for (i
= pclass
->num_options
, option
= pclass
->options
;
831 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
832 cupsFilePutConf(fp
, "Option", value
);
835 cupsFilePuts(fp
, "</Class>\n");
843 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
848 cupsdUpdateImplicitClasses(void)
850 int i
; /* Looping var */
851 cupsd_printer_t
*pclass
; /* Current class */
852 int accepting
; /* printer-is-accepting-jobs value */
855 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
857 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
860 * Loop through the printers to come up with a composite state...
863 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
864 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
867 pclass
->accepting
= accepting
;
873 * End of "$Id: classes.c 7724 2008-07-14 06:06:06Z mike $".