]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/classes.c
2 * "$Id: classes.c 5083 2006-02-06 02:57:43Z mike $"
4 * Printer class routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddClass() - Add a class to the system.
27 * cupsdAddPrinterToClass() - Add a printer to a class...
28 * cupsdDeletePrinterFromClass() - Delete a printer from a class.
29 * cupsdDeletePrinterFromClasses() - Delete a printer from all classes.
30 * cupsdDeleteAllClasses() - Remove all classes from the system.
31 * cupsdFindAvailablePrinter() - Find an available printer in a class.
32 * cupsdFindClass() - Find the named class.
33 * cupsdLoadAllClasses() - Load classes from the classes.conf file.
34 * cupsdSaveAllClasses() - Save classes to the classes.conf file.
35 * cupsdUpdateImplicitClasses() - Update the accepting state of implicit
40 * Include necessary headers...
47 * 'cupsdAddClass()' - Add a class to the system.
50 cupsd_printer_t
* /* O - New class */
51 cupsdAddClass(const char *name
) /* I - Name of class */
53 cupsd_printer_t
*c
; /* New class */
57 * Add the printer and set the type to "class"...
60 if ((c
= cupsdAddPrinter(name
)) != NULL
)
63 * Change from a printer to a class...
66 c
->type
= CUPS_PRINTER_CLASS
;
68 cupsdSetStringf(&c
->uri
, "ipp://%s:%d/classes/%s", ServerName
, LocalPort
,
70 cupsdSetString(&c
->error_policy
, "retry-job");
78 * 'cupsdAddPrinterToClass()' - Add a printer to a class...
82 cupsdAddPrinterToClass(
83 cupsd_printer_t
*c
, /* I - Class to add to */
84 cupsd_printer_t
*p
) /* I - Printer to add */
86 int i
; /* Looping var */
87 cupsd_printer_t
**temp
; /* Pointer to printer array */
91 * See if this printer is already a member of the class...
94 for (i
= 0; i
< c
->num_printers
; i
++)
95 if (c
->printers
[i
] == p
)
99 * Allocate memory as needed...
102 if (c
->num_printers
== 0)
103 temp
= malloc(sizeof(cupsd_printer_t
*));
105 temp
= realloc(c
->printers
, sizeof(cupsd_printer_t
*) * (c
->num_printers
+ 1));
109 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add printer %s to class %s!",
115 * Add the printer to the end of the array and update the number of printers.
119 temp
+= c
->num_printers
;
127 * 'cupsdDeletePrinterFromClass()' - Delete a printer from a class.
131 cupsdDeletePrinterFromClass(
132 cupsd_printer_t
*c
, /* I - Class to delete from */
133 cupsd_printer_t
*p
) /* I - Printer to delete */
135 int i
; /* Looping var */
136 cups_ptype_t type
, /* Class type */
137 oldtype
; /* Old class type */
141 * See if the printer is in the class...
144 for (i
= 0; i
< c
->num_printers
; i
++)
145 if (p
== c
->printers
[i
])
149 * If it is, remove it from the list...
152 if (i
< c
->num_printers
)
155 * Yes, remove the printer...
159 if (i
< c
->num_printers
)
160 memmove(c
->printers
+ i
, c
->printers
+ i
+ 1,
161 (c
->num_printers
- i
) * sizeof(cupsd_printer_t
*));
167 * Recompute the printer type mask as needed...
170 if (c
->num_printers
> 0)
173 type
= c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
);
174 c
->type
= ~CUPS_PRINTER_REMOTE
;
176 for (i
= 0; i
< c
->num_printers
; i
++)
177 c
->type
&= c
->printers
[i
]->type
;
182 * Update the IPP attributes...
185 if (c
->type
!= oldtype
)
186 cupsdSetPrinterAttrs(c
);
192 * 'cupsdDeletePrinterFromClasses()' - Delete a printer from all classes.
196 cupsdDeletePrinterFromClasses(
197 cupsd_printer_t
*p
) /* I - Printer to delete */
199 cupsd_printer_t
*c
; /* Pointer to current class */
203 * Loop through the printer/class list and remove the printer
204 * from each class listed...
207 for (c
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
209 c
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
210 if (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
211 cupsdDeletePrinterFromClass(c
, p
);
214 * Then clean out any empty implicit classes...
217 for (c
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
219 c
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
220 if (c
->num_printers
== 0)
222 cupsArrayRemove(ImplicitPrinters
, c
);
223 cupsdDeletePrinter(c
, 0);
229 * 'cupsdDeleteAllClasses()' - Remove all classes from the system.
233 cupsdDeleteAllClasses(void)
235 cupsd_printer_t
*c
; /* Pointer to current printer/class */
238 for (c
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
240 c
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
241 if (c
->type
& CUPS_PRINTER_CLASS
)
242 cupsdDeletePrinter(c
, 0);
247 * 'cupsdFindAvailablePrinter()' - Find an available printer in a class.
250 cupsd_printer_t
* /* O - Available printer or NULL */
251 cupsdFindAvailablePrinter(
252 const char *name
) /* I - Class to check */
254 int i
; /* Looping var */
255 cupsd_printer_t
*c
; /* Printer class */
262 if ((c
= cupsdFindClass(name
)) == NULL
)
264 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to find class \"%s\"!", name
);
269 * Loop through the printers in the class and return the first idle
270 * printer... We keep track of the last printer that we used so that
271 * a "round robin" type of scheduling is realized (otherwise the first
272 * server might be saturated with print jobs...)
274 * Thanks to Joel Fredrikson for helping us get this right!
277 for (i
= c
->last_printer
+ 1; ; i
++)
279 if (i
>= c
->num_printers
)
282 if (c
->printers
[i
]->accepting
&&
283 (c
->printers
[i
]->state
== IPP_PRINTER_IDLE
||
284 ((c
->printers
[i
]->type
& CUPS_PRINTER_REMOTE
) && !c
->printers
[i
]->job
)))
287 return (c
->printers
[i
]);
290 if (i
== c
->last_printer
)
299 * 'cupsdFindClass()' - Find the named class.
302 cupsd_printer_t
* /* O - Matching class or NULL */
303 cupsdFindClass(const char *name
) /* I - Name of class */
305 cupsd_printer_t
*c
; /* Current class/printer */
308 if ((c
= cupsdFindDest(name
)) != NULL
&&
309 (c
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
)))
317 * 'cupsdLoadAllClasses()' - Load classes from the classes.conf file.
321 cupsdLoadAllClasses(void)
323 cups_file_t
*fp
; /* classes.conf file */
324 int linenum
; /* Current line number */
325 char line
[1024], /* Line from file */
326 *value
, /* Pointer to value */
327 *valueptr
; /* Pointer into value */
328 cupsd_printer_t
*p
, /* Current printer class */
329 *temp
; /* Temporary pointer to printer */
333 * Open the classes.conf file...
336 snprintf(line
, sizeof(line
), "%s/classes.conf", ServerRoot
);
337 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
340 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
346 * Read class configurations until we hit EOF...
352 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
355 * Decode the directive...
358 if (!strcasecmp(line
, "<Class") ||
359 !strcasecmp(line
, "<DefaultClass"))
362 * <Class name> or <DefaultClass name>
365 if (p
== NULL
&& value
)
367 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading class %s...", value
);
369 p
= cupsdAddClass(value
);
371 p
->state
= IPP_PRINTER_IDLE
;
373 if (!strcasecmp(line
, "<DefaultClass"))
378 cupsdLogMessage(CUPSD_LOG_ERROR
,
379 "Syntax error on line %d of classes.conf.", linenum
);
383 else if (!strcasecmp(line
, "</Class>"))
387 cupsdSetPrinterAttrs(p
);
392 cupsdLogMessage(CUPSD_LOG_ERROR
,
393 "Syntax error on line %d of classes.conf.", linenum
);
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Syntax error on line %d of classes.conf.", linenum
);
403 else if (!strcasecmp(line
, "Info"))
406 cupsdSetString(&p
->info
, value
);
408 else if (!strcasecmp(line
, "Location"))
411 cupsdSetString(&p
->location
, value
);
413 else if (!strcasecmp(line
, "Printer"))
417 cupsdLogMessage(CUPSD_LOG_ERROR
,
418 "Syntax error on line %d of classes.conf.", linenum
);
421 else if ((temp
= cupsdFindPrinter(value
)) == NULL
)
423 cupsdLogMessage(CUPSD_LOG_WARN
,
424 "Unknown printer %s on line %d of classes.conf.",
428 * Add the missing remote printer...
431 if ((temp
= cupsdAddPrinter(value
)) != NULL
)
433 cupsdSetString(&temp
->make_model
, "Remote Printer on unknown");
435 temp
->state
= IPP_PRINTER_STOPPED
;
436 temp
->type
|= CUPS_PRINTER_REMOTE
;
437 temp
->browse_time
= 2147483647;
439 cupsdSetString(&temp
->location
, "Location Unknown");
440 cupsdSetString(&temp
->info
, "No Information Available");
441 temp
->hostname
[0] = '\0';
443 cupsdSetPrinterAttrs(temp
);
448 cupsdAddPrinterToClass(p
, temp
);
450 else if (!strcasecmp(line
, "State"))
453 * Set the initial queue state...
456 if (!strcasecmp(value
, "idle"))
457 p
->state
= IPP_PRINTER_IDLE
;
458 else if (!strcasecmp(value
, "stopped"))
459 p
->state
= IPP_PRINTER_STOPPED
;
462 cupsdLogMessage(CUPSD_LOG_ERROR
,
463 "Syntax error on line %d of classes.conf.",
468 else if (!strcasecmp(line
, "StateMessage"))
471 * Set the initial queue state message...
475 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
477 else if (!strcasecmp(line
, "StateTime"))
480 * Set the state time...
484 p
->state_time
= atoi(value
);
486 else if (!strcasecmp(line
, "Accepting"))
489 * Set the initial accepting state...
493 (!strcasecmp(value
, "yes") ||
494 !strcasecmp(value
, "on") ||
495 !strcasecmp(value
, "true")))
498 (!strcasecmp(value
, "no") ||
499 !strcasecmp(value
, "off") ||
500 !strcasecmp(value
, "false")))
504 cupsdLogMessage(CUPSD_LOG_ERROR
,
505 "Syntax error on line %d of classes.conf.",
510 else if (!strcasecmp(line
, "Shared"))
513 * Set the initial shared state...
517 (!strcasecmp(value
, "yes") ||
518 !strcasecmp(value
, "on") ||
519 !strcasecmp(value
, "true")))
522 (!strcasecmp(value
, "no") ||
523 !strcasecmp(value
, "off") ||
524 !strcasecmp(value
, "false")))
528 cupsdLogMessage(CUPSD_LOG_ERROR
,
529 "Syntax error on line %d of printers.conf.",
534 else if (!strcasecmp(line
, "JobSheets"))
537 * Set the initial job sheets...
542 for (valueptr
= value
;
543 *valueptr
&& !isspace(*valueptr
& 255);
549 cupsdSetString(&p
->job_sheets
[0], value
);
551 while (isspace(*valueptr
& 255))
556 for (value
= valueptr
;
557 *valueptr
&& !isspace(*valueptr
& 255);
563 cupsdSetString(&p
->job_sheets
[1], value
);
568 cupsdLogMessage(CUPSD_LOG_ERROR
,
569 "Syntax error on line %d of classes.conf.", linenum
);
573 else if (!strcasecmp(line
, "AllowUser"))
578 cupsdAddPrinterUser(p
, value
);
582 cupsdLogMessage(CUPSD_LOG_ERROR
,
583 "Syntax error on line %d of classes.conf.", linenum
);
587 else if (!strcasecmp(line
, "DenyUser"))
592 cupsdAddPrinterUser(p
, value
);
596 cupsdLogMessage(CUPSD_LOG_ERROR
,
597 "Syntax error on line %d of classes.conf.", linenum
);
601 else if (!strcasecmp(line
, "QuotaPeriod"))
604 p
->quota_period
= atoi(value
);
607 cupsdLogMessage(CUPSD_LOG_ERROR
,
608 "Syntax error on line %d of classes.conf.", linenum
);
612 else if (!strcasecmp(line
, "PageLimit"))
615 p
->page_limit
= atoi(value
);
618 cupsdLogMessage(CUPSD_LOG_ERROR
,
619 "Syntax error on line %d of classes.conf.", linenum
);
623 else if (!strcasecmp(line
, "KLimit"))
626 p
->k_limit
= atoi(value
);
629 cupsdLogMessage(CUPSD_LOG_ERROR
,
630 "Syntax error on line %d of classes.conf.", linenum
);
634 else if (!strcasecmp(line
, "OpPolicy"))
637 cupsdSetString(&p
->op_policy
, value
);
640 cupsdLogMessage(CUPSD_LOG_ERROR
,
641 "Syntax error on line %d of classes.conf.", linenum
);
645 else if (!strcasecmp(line
, "ErrorPolicy"))
648 cupsdSetString(&p
->error_policy
, value
);
651 cupsdLogMessage(CUPSD_LOG_ERROR
,
652 "Syntax error on line %d of classes.conf.", linenum
);
659 * Something else we don't understand...
662 cupsdLogMessage(CUPSD_LOG_ERROR
,
663 "Unknown configuration directive %s on line %d of classes.conf.",
673 * 'cupsdSaveAllClasses()' - Save classes to the classes.conf file.
677 cupsdSaveAllClasses(void)
679 cups_file_t
*fp
; /* classes.conf file */
680 char temp
[1024]; /* Temporary string */
681 char backup
[1024]; /* classes.conf.O file */
682 cupsd_printer_t
*pclass
; /* Current printer class */
683 int i
; /* Looping var */
684 time_t curtime
; /* Current time */
685 struct tm
*curdate
; /* Current date */
689 * Create the classes.conf file...
692 snprintf(temp
, sizeof(temp
), "%s/classes.conf", ServerRoot
);
693 snprintf(backup
, sizeof(backup
), "%s/classes.conf.O", ServerRoot
);
695 if (rename(temp
, backup
))
698 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup classes.conf - %s",
702 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
704 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to save classes.conf - %s",
707 if (rename(backup
, temp
))
708 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to restore classes.conf - %s",
713 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving classes.conf...");
716 * Restrict access to the file...
719 fchown(cupsFileNumber(fp
), RunUser
, Group
);
720 fchmod(cupsFileNumber(fp
), 0600);
723 * Write a small header to the file...
726 curtime
= time(NULL
);
727 curdate
= localtime(&curtime
);
728 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
730 cupsFilePuts(fp
, "# Class configuration file for " CUPS_SVERSION
"\n");
731 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
734 * Write each local class known to the system...
737 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
739 pclass
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
742 * Skip remote destinations and regular printers...
745 if ((pclass
->type
& CUPS_PRINTER_REMOTE
) ||
746 (pclass
->type
& CUPS_PRINTER_IMPLICIT
) ||
747 !(pclass
->type
& CUPS_PRINTER_CLASS
))
751 * Write printers as needed...
754 if (pclass
== DefaultPrinter
)
755 cupsFilePrintf(fp
, "<DefaultClass %s>\n", pclass
->name
);
757 cupsFilePrintf(fp
, "<Class %s>\n", pclass
->name
);
760 cupsFilePrintf(fp
, "Info %s\n", pclass
->info
);
762 if (pclass
->location
)
763 cupsFilePrintf(fp
, "Location %s\n", pclass
->location
);
765 if (pclass
->state
== IPP_PRINTER_STOPPED
)
767 cupsFilePuts(fp
, "State Stopped\n");
768 cupsFilePrintf(fp
, "StateMessage %s\n", pclass
->state_message
);
771 cupsFilePuts(fp
, "State Idle\n");
773 cupsFilePrintf(fp
, "StateTime %d\n", (int)pclass
->state_time
);
775 if (pclass
->accepting
)
776 cupsFilePuts(fp
, "Accepting Yes\n");
778 cupsFilePuts(fp
, "Accepting No\n");
781 cupsFilePuts(fp
, "Shared Yes\n");
783 cupsFilePuts(fp
, "Shared No\n");
785 cupsFilePrintf(fp
, "JobSheets %s %s\n", pclass
->job_sheets
[0],
786 pclass
->job_sheets
[1]);
788 for (i
= 0; i
< pclass
->num_printers
; i
++)
789 cupsFilePrintf(fp
, "Printer %s\n", pclass
->printers
[i
]->name
);
791 cupsFilePrintf(fp
, "QuotaPeriod %d\n", pclass
->quota_period
);
792 cupsFilePrintf(fp
, "PageLimit %d\n", pclass
->page_limit
);
793 cupsFilePrintf(fp
, "KLimit %d\n", pclass
->k_limit
);
795 for (i
= 0; i
< pclass
->num_users
; i
++)
796 cupsFilePrintf(fp
, "%sUser %s\n", pclass
->deny_users
? "Deny" : "Allow",
799 if (pclass
->op_policy
)
800 cupsFilePrintf(fp
, "OpPolicy %s\n", pclass
->op_policy
);
801 if (pclass
->error_policy
)
802 cupsFilePrintf(fp
, "ErrorPolicy %s\n", pclass
->error_policy
);
804 cupsFilePuts(fp
, "</Class>\n");
812 * 'cupsdUpdateImplicitClasses()' - Update the accepting state of implicit
817 cupsdUpdateImplicitClasses(void)
819 int i
; /* Looping var */
820 cupsd_printer_t
*pclass
; /* Current class */
821 int accepting
; /* printer-is-accepting-jobs value */
824 for (pclass
= (cupsd_printer_t
*)cupsArrayFirst(ImplicitPrinters
);
826 pclass
= (cupsd_printer_t
*)cupsArrayNext(ImplicitPrinters
))
829 * Loop through the printers to come up with a composite state...
832 for (i
= 0, accepting
= 0; i
< pclass
->num_printers
; i
++)
833 if ((accepting
= pclass
->printers
[i
]->accepting
) != 0)
836 pclass
->accepting
= accepting
;
842 * End of "$Id: classes.c 5083 2006-02-06 02:57:43Z mike $".