]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/quotas.c
2 * "$Id: quotas.c 6947 2007-09-12 21:09:49Z mike $"
4 * Quota routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 */
51 char *ptr
; /* Pointer into username */
57 strlcpy(match
.username
, username
, sizeof(match
.username
));
58 if ((ptr
= strchr(match
.username
, '@')) != NULL
)
59 *ptr
= '\0'; /* Strip @domain/@KDC */
61 if ((q
= (cupsd_quota_t
*)cupsArrayFind(p
->quotas
, &match
)) != NULL
)
64 return (add_quota(p
, username
));
69 * 'cupsdFreeQuotas()' - Free quotas for a printer.
73 cupsdFreeQuotas(cupsd_printer_t
*p
) /* I - Printer */
75 cupsd_quota_t
*q
; /* Current quota record */
81 for (q
= (cupsd_quota_t
*)cupsArrayFirst(p
->quotas
);
83 q
= (cupsd_quota_t
*)cupsArrayNext(p
->quotas
))
86 cupsArrayDelete(p
->quotas
);
93 * 'cupsdUpdateQuota()' - Update quota data for the specified printer and user.
96 cupsd_quota_t
* /* O - Quota data */
98 cupsd_printer_t
*p
, /* I - Printer */
99 const char *username
, /* I - User */
100 int pages
, /* I - Number of pages */
101 int k
) /* I - Number of kilobytes */
103 cupsd_quota_t
*q
; /* Quota data */
104 cupsd_job_t
*job
; /* Current job */
105 time_t curtime
; /* Current time */
106 ipp_attribute_t
*attr
; /* Job attribute */
112 if (!p
->k_limit
&& !p
->page_limit
)
115 if ((q
= cupsdFindQuota(p
, username
)) == NULL
)
118 cupsdLogMessage(CUPSD_LOG_DEBUG
,
119 "cupsdUpdateQuota: p=%s username=%s pages=%d k=%d",
120 p
->name
, username
, pages
, k
);
122 #if defined(__APPLE__) && defined(HAVE_DLFCN_H)
124 * Use Apple PrintService quota enforcement if installed (X Server only)
127 if (AppleQuotas
&& PSQUpdateQuotaProc
)
129 q
->page_count
= (*PSQUpdateQuotaProc
)(p
->name
, p
->info
, username
, pages
, 0);
133 #endif /* __APPLE__ && HAVE_DLFCN_H */
135 curtime
= time(NULL
);
137 if (curtime
< q
->next_update
)
139 q
->page_count
+= pages
;
146 curtime
-= p
->quota_period
;
154 for (job
= (cupsd_job_t
*)cupsArrayFirst(Jobs
);
156 job
= (cupsd_job_t
*)cupsArrayNext(Jobs
))
159 * We only care about the current printer/class and user...
162 if (strcasecmp(job
->dest
, p
->name
) != 0 ||
163 strcasecmp(job
->username
, q
->username
) != 0)
167 * Make sure attributes are loaded; we always call cupsdLoadJob() to ensure
168 * the access_time member is updated so the job isn't unloaded right away...
171 if (!cupsdLoadJob(job
))
174 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-completion",
175 IPP_TAG_INTEGER
)) == NULL
)
176 if ((attr
= ippFindAttribute(job
->attrs
, "time-at-processing",
177 IPP_TAG_INTEGER
)) == NULL
)
178 attr
= ippFindAttribute(job
->attrs
, "time-at-creation",
181 if (attr
->values
[0].integer
< curtime
)
184 * This job is too old to count towards the quota, ignore it...
187 if (JobAutoPurge
&& !job
->printer
&& job
->state_value
> IPP_JOB_STOPPED
)
188 cupsdDeleteJob(job
, CUPSD_JOB_PURGE
);
193 if (q
->next_update
== 0)
194 q
->next_update
= attr
->values
[0].integer
+ p
->quota_period
;
196 if ((attr
= ippFindAttribute(job
->attrs
, "job-media-sheets-completed",
197 IPP_TAG_INTEGER
)) != NULL
)
198 q
->page_count
+= attr
->values
[0].integer
;
200 if ((attr
= ippFindAttribute(job
->attrs
, "job-k-octets",
201 IPP_TAG_INTEGER
)) != NULL
)
202 q
->k_count
+= attr
->values
[0].integer
;
210 * 'add_quota()' - Add a quota record for this printer and user.
213 static cupsd_quota_t
* /* O - Quota data */
214 add_quota(cupsd_printer_t
*p
, /* I - Printer */
215 const char *username
) /* I - User */
217 cupsd_quota_t
*q
; /* New quota data */
218 char *ptr
; /* Pointer into username */
225 p
->quotas
= cupsArrayNew((cups_array_func_t
)compare_quotas
, NULL
);
230 if ((q
= calloc(1, sizeof(cupsd_quota_t
))) == NULL
)
233 strlcpy(q
->username
, username
, sizeof(q
->username
));
234 if ((ptr
= strchr(q
->username
, '@')) != NULL
)
235 *ptr
= '\0'; /* Strip @domain/@KDC */
237 cupsArrayAdd(p
->quotas
, q
);
244 * 'compare_quotas()' - Compare two quota records...
247 static int /* O - Result of comparison */
248 compare_quotas(const cupsd_quota_t
*q1
, /* I - First quota record */
249 const cupsd_quota_t
*q2
) /* I - Second quota record */
251 return (strcasecmp(q1
->username
, q2
->username
));
256 * End of "$Id: quotas.c 6947 2007-09-12 21:09:49Z mike $".