]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/quotas.c
2 * "$Id: quotas.c 6649 2007-07-11 21:46:42Z mike $"
4 * Quota routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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 * cupsdFindQuota() - Find a quota record.
18 * cupsdFreeQuotas() - Free quotas for a printer.
19 * cupsdUpdateQuota() - Update quota data for the specified printer and user.
20 * add_quota() - Add a quota record for this printer and user.
21 * compare_quotas() - Compare two quota records...
25 * Include necessary headers...
35 static cupsd_quota_t
*add_quota(cupsd_printer_t
*p
, const char *username
);
36 static int compare_quotas(const cupsd_quota_t
*q1
,
37 const cupsd_quota_t
*q2
);
41 * 'cupsdFindQuota()' - Find a quota record.
44 cupsd_quota_t
* /* O - Quota data */
46 cupsd_printer_t
*p
, /* I - Printer */
47 const char *username
) /* I - User */
49 cupsd_quota_t
*q
, /* Quota data pointer */
50 match
; /* Search data */
56 strlcpy(match
.username
, username
, sizeof(match
.username
));
58 if ((q
= (cupsd_quota_t
*)cupsArrayFind(p
->quotas
, &match
)) != NULL
)
61 return (add_quota(p
, username
));
66 * 'cupsdFreeQuotas()' - Free quotas for a printer.
70 cupsdFreeQuotas(cupsd_printer_t
*p
) /* I - Printer */
72 cupsd_quota_t
*q
; /* Current quota record */
78 for (q
= (cupsd_quota_t
*)cupsArrayFirst(p
->quotas
);
80 q
= (cupsd_quota_t
*)cupsArrayNext(p
->quotas
))
83 cupsArrayDelete(p
->quotas
);
90 * 'cupsdUpdateQuota()' - Update quota data for the specified printer and user.
93 cupsd_quota_t
* /* O - Quota data */
95 cupsd_printer_t
*p
, /* I - Printer */
96 const char *username
, /* I - User */
97 int pages
, /* I - Number of pages */
98 int k
) /* I - Number of kilobytes */
100 cupsd_quota_t
*q
; /* Quota data */
101 cupsd_job_t
*job
; /* Current job */
102 time_t curtime
; /* Current time */
103 ipp_attribute_t
*attr
; /* Job attribute */
109 if (!p
->k_limit
&& !p
->page_limit
)
112 if ((q
= cupsdFindQuota(p
, username
)) == NULL
)
115 cupsdLogMessage(CUPSD_LOG_DEBUG
,
116 "cupsdUpdateQuota: p=%s username=%s pages=%d k=%d",
117 p
->name
, username
, pages
, k
);
119 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
121 * Use Apple PrintService quota enforcement if installed (X Server only)
124 if (AppleQuotas
&& PSQUpdateQuotaProc
)
126 q
->page_count
= (*PSQUpdateQuotaProc
)(p
->name
, p
->info
, username
, pages
, 0);
130 #endif /* __APPLE__ && HAVE_DLFCN_H */
132 curtime
= time(NULL
);
134 if (curtime
< q
->next_update
)
136 q
->page_count
+= pages
;
143 curtime
-= p
->quota_period
;
151 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
153 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
155 if (strcasecmp(job
->dest
, p
->name
) != 0 ||
156 strcasecmp(job
->username
, q
->username
) != 0)
159 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-completion",
160 IPP_TAG_INTEGER
)) == NULL
)
161 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-processing",
162 IPP_TAG_INTEGER
)) == NULL
)
163 attr
= ippFindAttribute(job
->attrs
, "time-at-creation",
169 if (attr
->values
[0].integer
< curtime
)
172 cupsdCancelJob(job
, 1, IPP_JOB_CANCELED
);
177 if (q
->next_update
== 0)
178 q
->next_update
= attr
->values
[0].integer
+ p
->quota_period
;
180 if ((attr
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
181 IPP_TAG_INTEGER
)) != NULL
)
182 q
->page_count
+= attr
->values
[0].integer
;
184 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
185 IPP_TAG_INTEGER
)) != NULL
)
186 q
->k_count
+= attr
->values
[0].integer
;
194 * 'add_quota()' - Add a quota record for this printer and user.
197 static cupsd_quota_t
* /* O - Quota data */
198 add_quota(cupsd_printer_t
*p
, /* I - Printer */
199 const char *username
) /* I - User */
201 cupsd_quota_t
*q
; /* New quota data */
208 p
->quotas
= cupsArrayNew((cups_array_func_t
)compare_quotas
, NULL
);
213 if ((q
= calloc(1, sizeof(cupsd_quota_t
))) == NULL
)
216 strlcpy(q
->username
, username
, sizeof(q
->username
));
218 cupsArrayAdd(p
->quotas
, q
);
225 * 'compare_quotas()' - Compare two quota records...
228 static int /* O - Result of comparison */
229 compare_quotas(const cupsd_quota_t
*q1
, /* I - First quota record */
230 const cupsd_quota_t
*q2
) /* I - Second quota record */
232 return (strcasecmp(q1
->username
, q2
->username
));
237 * End of "$Id: quotas.c 6649 2007-07-11 21:46:42Z mike $".