root / pykota / trunk / bin / cupspykota @ 2028

Revision 2028, 38.5 kB (checked in by jalet, 19 years ago)

Modified copyright years

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[1177]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3
4# CUPSPyKota accounting backend
5#
6# PyKota - Print Quotas for CUPS and LPRng
7#
[2028]8# (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com>
[1177]9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22#
23# $Id$
24#
25# $Log$
[2028]26# Revision 1.84  2005/01/17 08:44:23  jalet
27# Modified copyright years
28#
[2007]29# Revision 1.83  2005/01/06 23:23:35  jalet
30# Regain priviledge the time to read the job control file to extract the client
31# hostname
32#
[2006]33# Revision 1.82  2005/01/06 22:52:53  jalet
34# Implemented the dropping of priviledges. Beware, beware...
35#
[1974]36# Revision 1.81  2004/12/07 16:54:02  jalet
37# Now logs as errors differences between computed and precomputed job's sizes
38#
[1924]39# Revision 1.80  2004/11/15 22:12:46  jalet
40# Fix for the Fix !!!
41#
[1923]42# Revision 1.79  2004/11/15 22:01:34  jalet
43# Improved banner handling.
44# Fix for raw printing and banners.
45#
[1918]46# Revision 1.78  2004/11/15 19:59:34  jalet
47# PyKota banners now basically work !
48#
[1902]49# Revision 1.77  2004/11/06 22:40:57  jalet
50# Safer code
51#
[1901]52# Revision 1.76  2004/11/06 22:35:58  jalet
53# Added a miniparser for IPP messages (RFC 2910). The job-originating-host-name
54# retrieval is now fiable, unless the CUPS developpers change something...
55#
[1897]56# Revision 1.75  2004/11/01 15:45:25  jalet
57# Added many debug messages.
58# Added some code to prevent short writes.
59#
[1820]60# Revision 1.74  2004/10/13 20:51:27  jalet
61# Made debugging levels be the same in cupspykota and lprngpykota.
62# Now outputs more information in informational messages : user, printer, jobid
63#
[1819]64# Revision 1.73  2004/10/13 16:56:45  jalet
65# Added a space to the pattern to differentiate jobs which id begins with
66# the same digits, like jobs 87 and 879 for example : if printed by same
67# user on same printer, but from a different host this could have broken
68# the result. (In reality this couldn't happen because 879 would be the
69# last line to match anyway because of job ordering, but we never know
70# if the page_log file gets corrupt somewhat)
71#
[1713]72# Revision 1.72  2004/09/13 16:02:44  jalet
73# Added fix for incorrect job's size when hardware accounting fails
74#
[1704]75# Revision 1.71  2004/09/06 17:05:06  jalet
76# Fix for autodetection of SC_OPEN_MAX
77#
[1643]78# Revision 1.70  2004/07/26 09:20:27  jalet
79# Unneeded module
80#
[1624]81# Revision 1.69  2004/07/22 22:41:47  jalet
82# Hardware accounting for LPRng should be OK now. UNTESTED.
83#
[1606]84# Revision 1.68  2004/07/20 22:19:44  jalet
85# Sanitized a bit + use of gettext
86#
[1600]87# Revision 1.67  2004/07/16 12:22:45  jalet
88# LPRng support early version
89#
[1584]90# Revision 1.66  2004/07/01 19:56:25  jalet
91# Better dispatching of error messages
92#
[1562]93# Revision 1.65  2004/06/22 09:31:17  jalet
94# Always send some debug info to CUPS' back channel stream (stderr) as
95# informationnal messages.
96#
[1546]97# Revision 1.64  2004/06/18 13:34:46  jalet
98# Now all tracebacks include PyKota's version number
99#
[1542]100# Revision 1.63  2004/06/17 13:26:50  jalet
101# Better exception handling code
102#
[1541]103# Revision 1.62  2004/06/16 20:56:34  jalet
104# Smarter initialisation code
105#
[1530]106# Revision 1.61  2004/06/08 09:00:04  jalet
107# Fixed problem when username was passed in uppercase from Samba and we
108# tried to find correct line in CUPS page_log to extract the hostname.
109#
[1520]110# Revision 1.60  2004/06/03 23:14:08  jalet
111# Now stores the job's size in bytes in the database.
112# Preliminary work on payments storage : database schemas are OK now,
113# but no code to store payments yet.
114# Removed schema picture, not relevant anymore.
115#
[1519]116# Revision 1.59  2004/06/03 22:12:53  jalet
117# Now denies empty jobs
118#
[1517]119# Revision 1.58  2004/06/03 21:50:33  jalet
120# Improved error logging.
121# crashrecipient directive added.
122# Now exports the job's size in bytes too.
123#
[1515]124# Revision 1.57  2004/06/02 22:18:07  jalet
125# I think the bug when cancelling jobs should be fixed right now
126#
[1514]127# Revision 1.56  2004/06/02 21:50:56  jalet
128# Moved the sigterm capturing elsewhere
129#
[1513]130# Revision 1.55  2004/06/02 14:25:07  jalet
131# Should correctly capture ALL errors now
132#
[1503]133# Revision 1.54  2004/05/26 16:44:48  jalet
134# Now logs something when client hostname can't be extracted
135#
[1502]136# Revision 1.53  2004/05/26 14:49:35  jalet
137# First try at saving the job-originating-hostname in the database
138#
[1499]139# Revision 1.52  2004/05/25 09:15:13  jalet
140# accounter.py : old code deleted
141# the rest : now exports PYKOTAPRECOMPUTEDJOBSIZE and PYKOTAPRECOMPUTEDJOBPRICE
142#
[1498]143# Revision 1.51  2004/05/25 08:31:16  jalet
144# Heavy CPU usage seems to be fixed at least !
145#
[1497]146# Revision 1.50  2004/05/25 05:17:50  jalet
147# Now precomputes the job's size only if current printer's enforcement
148# is "STRICT"
149#
[1495]150# Revision 1.49  2004/05/24 22:45:48  jalet
151# New 'enforcement' directive added
152# Polling loop improvements
153#
[1494]154# Revision 1.48  2004/05/24 14:36:24  jalet
155# Revert to old polling loop. Will need optimisations
156#
[1493]157# Revision 1.47  2004/05/24 11:59:46  jalet
158# More robust (?) code
159#
[1492]160# Revision 1.46  2004/05/21 22:02:51  jalet
161# Preliminary work on pre-accounting
162#
[1484]163# Revision 1.45  2004/05/19 07:15:32  jalet
164# Could the 'misterious' bug in my loop be finally fixed ???
165#
[1483]166# Revision 1.44  2004/05/18 14:48:47  jalet
167# Big code changes to completely remove the need for "requester" directives,
168# jsut use "hardware(... your previous requester directive's content ...)"
169#
[1478]170# Revision 1.43  2004/05/17 11:46:05  jalet
171# First try at cupspykota's main loop rewrite
172#
[1467]173# Revision 1.42  2004/05/10 11:22:28  jalet
174# Typo
175#
[1466]176# Revision 1.41  2004/05/10 10:07:30  jalet
177# Catches OSError while reading
178#
[1465]179# Revision 1.40  2004/05/10 09:29:48  jalet
180# Should be more robust if we receive a SIGTERM during an I/O operation
181#
[1458]182# Revision 1.39  2004/05/07 14:44:53  jalet
183# Fix for file handles unregistered twice from the polling object
184#
[1433]185# Revision 1.38  2004/04/09 22:24:46  jalet
186# Began work on correct handling of child processes when jobs are cancelled by
187# the user. Especially important when an external requester is running for a
188# long time.
189#
[1411]190# Revision 1.37  2004/03/18 19:11:25  jalet
191# Fix for raw jobs in cupspykota
192#
[1410]193# Revision 1.36  2004/03/18 14:03:18  jalet
194# Added fsync() calls
195#
[1405]196# Revision 1.35  2004/03/16 12:05:01  jalet
197# Small fix for new waitprinter.sh : when job was denied, would wait forever
198# for printer being in printing mode.
199#
[1400]200# Revision 1.34  2004/03/15 10:47:56  jalet
201# This time the traceback formatting should be correct !
202#
[1391]203# Revision 1.33  2004/03/05 12:46:07  jalet
204# Improve tracebacks
205#
[1390]206# Revision 1.32  2004/03/05 12:31:35  jalet
207# Now should output full traceback when crashing
208#
[1375]209# Revision 1.31  2004/03/01 14:35:56  jalet
210# PYKOTAPHASE wasn't set soon enough at the start of the job
211#
[1374]212# Revision 1.30  2004/03/01 14:34:15  jalet
213# PYKOTAPHASE wasn't set at the right time at the end of data transmission
214# to underlying layer (real backend)
215#
[1372]216# Revision 1.29  2004/03/01 11:23:25  jalet
217# Pre and Post hooks to external commands are available in the cupspykota
218# backend. Forthe pykota filter they will be implemented real soon now.
219#
[1365]220# Revision 1.28  2004/02/26 14:18:07  jalet
221# Should fix the remaining bugs wrt printers groups and users groups.
222#
[1335]223# Revision 1.27  2004/02/04 23:41:27  jalet
224# Should fix the incorrect "backend died abnormally" problem.
225#
[1321]226# Revision 1.26  2004/01/30 16:35:03  jalet
227# Fixes stupid software accounting bug in CUPS backend
228#
[1302]229# Revision 1.25  2004/01/16 17:51:46  jalet
230# Fuck Fuck Fuck !!!
231#
[1291]232# Revision 1.24  2004/01/14 15:52:01  jalet
233# Small fix for job cancelling code.
234#
[1289]235# Revision 1.23  2004/01/13 10:48:28  jalet
236# Small streams polling loop modification.
237#
[1285]238# Revision 1.22  2004/01/12 22:43:40  jalet
239# New formula to compute a job's price
240#
[1280]241# Revision 1.21  2004/01/12 18:17:36  jalet
242# Denied jobs weren't stored into the history anymore, this is now fixed.
243#
[1271]244# Revision 1.20  2004/01/11 23:22:42  jalet
245# Major code refactoring, it's way cleaner, and now allows automated addition
246# of printers on first print.
247#
[1257]248# Revision 1.19  2004/01/08 14:10:32  jalet
249# Copyright year changed.
250#
[1256]251# Revision 1.18  2004/01/07 16:16:32  jalet
252# Better debugging information
253#
[1240]254# Revision 1.17  2003/12/27 16:49:25  uid67467
255# Should be ok now.
256#
257# Revision 1.17  2003/12/06 08:54:29  jalet
258# Code simplifications.
259# Added many debugging messages.
260#
[1222]261# Revision 1.16  2003/11/26 20:43:29  jalet
262# Inadvertantly introduced a bug, which is fixed.
263#
[1221]264# Revision 1.15  2003/11/26 19:17:35  jalet
265# Printing on a printer not present in the Quota Storage now results
266# in the job being stopped or cancelled depending on the system.
267#
[1210]268# Revision 1.14  2003/11/25 13:25:45  jalet
269# Boolean problem with old Python, replaced with 0
270#
[1203]271# Revision 1.13  2003/11/23 19:01:35  jalet
272# Job price added to history
273#
[1200]274# Revision 1.12  2003/11/21 14:28:43  jalet
275# More complete job history.
276#
[1196]277# Revision 1.11  2003/11/19 23:19:35  jalet
278# Code refactoring work.
279# Explicit redirection to /dev/null has to be set in external policy now, just
280# like in external mailto.
281#
[1191]282# Revision 1.10  2003/11/18 17:54:24  jalet
283# SIGTERMs are now transmitted to original backends.
284#
[1190]285# Revision 1.9  2003/11/18 14:11:07  jalet
286# Small fix for bizarre urls
287#
[1186]288# Revision 1.8  2003/11/15 14:26:44  jalet
289# General improvements to the documentation.
290# Email address changed in sample configuration file, because
291# I receive low quota messages almost every day...
292#
[1185]293# Revision 1.7  2003/11/14 22:05:12  jalet
294# New CUPS backend fully functionnal.
295# Old CUPS configuration method is now officially deprecated.
296#
[1184]297# Revision 1.6  2003/11/14 20:13:11  jalet
298# We exit the loop too soon.
299#
[1183]300# Revision 1.5  2003/11/14 18:31:27  jalet
301# Not perfect, but seems to work with the poll() loop.
302#
[1182]303# Revision 1.4  2003/11/14 17:04:15  jalet
304# More (untested) work on the CUPS backend.
305#
[1180]306# Revision 1.3  2003/11/12 23:27:44  jalet
307# More work on new backend. This commit may be unstable.
308#
[1178]309# Revision 1.2  2003/11/12 09:33:34  jalet
310# New CUPS backend supports device enumeration
311#
[1177]312# Revision 1.1  2003/11/08 16:05:31  jalet
313# CUPS backend added for people to experiment.
314#
315#
316#
317
318import sys
319import os
[1182]320import popen2
[1178]321import cStringIO
322import shlex
[1182]323import select
324import signal
[1291]325import time
[1177]326
[1546]327from pykota.tool import PyKotaFilterOrBackend, PyKotaToolError, crashed
[1177]328from pykota.config import PyKotaConfigError
329from pykota.storage import PyKotaStorageError
[1196]330from pykota.accounter import PyKotaAccounterError
[1901]331from pykota.ipp import IPPMessage, PyKotaIPPError
[1271]332   
[1478]333class PyKotaPopen4(popen2.Popen4) :
[1182]334    """Our own class to execute real backends.
335   
336       Their first argument is different from their path so using
337       native popen2.Popen3 would not be feasible.
338    """
[1478]339    def __init__(self, cmd, bufsize=-1, arg0=None) :
[1182]340        self.arg0 = arg0
[1478]341        popen2.Popen4.__init__(self, cmd, bufsize)
[1182]342       
343    def _run_child(self, cmd):
[1704]344        try :
345            MAXFD = os.sysconf("SC_OPEN_MAX")
346        except (AttributeError, ValueError) :   
347            MAXFD = 256
348        for i in range(3, MAXFD) : 
[1182]349            try:
350                os.close(i)
351            except OSError:
352                pass
353        try:
354            os.execvpe(cmd[0], [self.arg0 or cmd[0]] + cmd[1:], os.environ)
355        finally:
356            os._exit(1)
357   
[1271]358class PyKotaBackend(PyKotaFilterOrBackend) :       
359    """A class for the pykota backend."""
360    def acceptJob(self) :       
361        """Returns the appropriate exit code to tell CUPS all is OK."""
362        return 0
363           
364    def removeJob(self) :           
365        """Returns the appropriate exit code to let CUPS think all is OK.
[1177]366       
[1271]367           Returning 0 (success) prevents CUPS from stopping the print queue.
368        """   
369        return 0
[1222]370       
[1901]371    def getCupsConfigDirectives(self, directives=[]) :
[1902]372        """Retrieves some CUPS directives from its configuration file.
373       
374           Returns a mapping with lowercased directives as keys and
375           their setting as values.
376        """
[1901]377        dirvalues = {} 
[1502]378        cupsroot = os.environ.get("CUPS_SERVERROOT", "/etc/cups")
379        cupsdconf = os.path.join(cupsroot, "cupsd.conf")
380        try :
381            conffile = open(cupsdconf, "r")
382        except IOError :   
[1503]383            self.logdebug("Unable to open %s" % cupsdconf)
[1502]384        else :   
385            for line in conffile.readlines() :
386                linecopy = line.strip().lower()
[1901]387                for di in [d.lower() for d in directives] :
388                    if linecopy.startswith("%s " % di) :
389                        try :
390                            val = line.split()[1]
391                        except :   
392                            pass # ignore errors, we take the last value in any case.
393                        else :   
394                            dirvalues[di] = val
[1502]395            conffile.close()           
[1901]396        return dirvalues       
[1502]397           
[1901]398    def getJobOriginatingHostnameFromPageLog(self, cupsconfig, printername, username, jobid) :
[1502]399        """Retrieves the job-originating-hostname from the CUPS page_log file if possible."""
[1901]400        pagelogpath = cupsconfig.get("pagelog", "/var/log/cups/page_log")
401        self.logdebug("Trying to extract job-originating-host-name from %s" % pagelogpath)
[1502]402        try :
403            pagelog = open(pagelogpath, "r")
404        except IOError :   
[1503]405            self.logdebug("Unable to open %s" % pagelogpath)
[1502]406            return # no page log or can't read it, originating hostname unknown yet
407        else :   
408            # TODO : read backward so we could take first value seen
409            # TODO : here we read forward so we must take the last value seen
[1819]410            prefix = ("%s %s %s " % (printername, username, jobid)).lower()
[1502]411            matchingline = None
412            while 1 :
413                line = pagelog.readline()
414                if not line :
415                    break
416                else :
417                    line = line.strip()
[1530]418                    if line.lower().startswith(prefix) :   
[1502]419                        matchingline = line # no break, because we read forward
420            pagelog.close()       
421            if matchingline is None :
[1606]422                self.logdebug("No matching line found in %s" % pagelogpath)
[1901]423                return # correct line not found, job-originating-host-name unknown
[1502]424            else :   
425                return matchingline.split()[-1]
426               
[1271]427    def doWork(self, policy, printer, user, userpquota) :   
428        """Most of the work is done here."""
429        # Two different values possible for policy here :
430        # ALLOW means : Either printer, user or user print quota doesn't exist,
431        #               but the job should be allowed anyway.
432        # OK means : Both printer, user and user print quota exist, job should
433        #            be allowed if current user is allowed to print on this printer
434        if policy == "OK" :
[1372]435            # exports user information with initial values
436            self.exportUserInfo(userpquota)
437           
[1901]438            # tries to extract job-originating-host-name and other information
439            cupsdconf = self.getCupsConfigDirectives(["PageLog", "RequestRoot"])
440            requestroot = cupsdconf.get("requestroot", "/var/spool/cups")
441            if (len(self.jobid) < 5) and self.jobid.isdigit() :
442                ippmessagefile = "c%05i" % int(self.jobid)
443            else :   
444                ippmessagefile = "c%s" % self.jobid
445            ippmessagefile = os.path.join(requestroot, ippmessagefile)
446            ippmessage = {}
[2007]447            self.regainPriv()
[1901]448            try :
449                ippdatafile = open(ippmessagefile)
450            except :   
451                self.printInfo("Unable to open IPP message file %s" % ippmessagefile, "warn")
452            else :   
453                self.logdebug("Parsing of IPP message file %s begins." % ippmessagefile)
[1902]454                try :
455                    ippmessage = IPPMessage(ippdatafile.read())
456                except PyKotaIPPError, msg :   
457                    self.printInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn")
458                else :   
459                    self.logdebug("Parsing of IPP message file %s ends." % ippmessagefile)
[1901]460                ippdatafile.close()
[2007]461            self.dropPriv()   
[1901]462            clienthost = ippmessage.get("job-originating-host-name") \
463                         or self.getJobOriginatingHostnameFromPageLog(cupsdconf, printer.Name, user.Name, self.jobid)
[1503]464            self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))   
[1517]465            os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "")
[1502]466           
[1901]467            # TODO : extract username (double check ?) and billing code too
468           
[1375]469            # enters first phase
[1517]470            os.environ["PYKOTAPHASE"] = "BEFORE"
[1375]471           
[1519]472            # do we want strict or laxist quota enforcement ?
[1497]473            if self.config.getPrinterEnforcement(printer.Name) == "STRICT" :
474                self.softwareJobSize = self.precomputeJobSize()
475                self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize)
[1606]476                self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice))
[1517]477            os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize)
478            os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice)
[1519]479           
480            # if no data to pass to real backend, probably a filter
481            # higher in the chain failed because of a misconfiguration.
482            # we deny the job in this case (nothing to print anyway)
483            if not self.jobSizeBytes :
[1820]484                self.printMoreInfo(user, printer, _("Job contains no data. Printing is denied."), "warn")
[1519]485                action = "DENY"
[1606]486            else :   
487                # checks the user's quota
488                action = self.warnUserPQuota(userpquota)
[1519]489           
[1372]490            # exports some new environment variables
[1517]491            os.environ["PYKOTAACTION"] = action
[1372]492           
493            # launches the pre hook
494            self.prehook(userpquota)
[1918]495
496            # saves the size of banners which have to be accounted for
497            # this is needed in the case of software accounting
498            bannersize = 0
[1372]499           
[1918]500            # handle starting banner pages before accounting
501            accountbanner = self.config.getAccountBanner(printer.Name)
502            if accountbanner in ["ENDING", "NONE"] :
503                banner = self.startingBanner(printer.Name)
504                if banner :
505                    self.logdebug("Printing starting banner before accounting begins.")
506                    self.handleData(banner)
507 
[1820]508            self.printMoreInfo(user, printer, _("Job accounting begins."))
[1624]509            self.accounter.beginJob(printer)
[1918]510           
511            # handle starting banner pages during accounting
512            if accountbanner in ["STARTING", "BOTH"] :
513                banner = self.startingBanner(printer.Name)
514                if banner :
515                    self.logdebug("Printing starting banner during accounting.")
516                    self.handleData(banner)
517                    if self.accounter.isSoftware :
518                        bannersize += 1 # TODO : fix this by passing the banner's content through PDLAnalyzer
[1280]519        else :   
520            action = "ALLOW"
[1517]521            os.environ["PYKOTAACTION"] = action
[1271]522           
523        # pass the job's data to the real backend   
[1280]524        if action in ["ALLOW", "WARN"] :
[1291]525            if self.gotSigTerm :
[1280]526                retcode = self.removeJob()
527            else :   
528                retcode = self.handleData()       
529        else :       
[1271]530            retcode = self.removeJob()
531       
532        if policy == "OK" :       
[1374]533            # indicate phase change
[1517]534            os.environ["PYKOTAPHASE"] = "AFTER"
[1374]535           
[1918]536            # handle ending banner pages during accounting
537            if accountbanner in ["ENDING", "BOTH"] :
538                banner = self.endingBanner(printer.Name)
539                if banner :
540                    self.logdebug("Printing ending banner during accounting.")
541                    self.handleData(banner)
542                    if self.accounter.isSoftware :
543                        bannersize += 1 # TODO : fix this by passing the banner's content through PDLAnalyzer
544 
[1271]545            # stops accounting.
[1624]546            self.accounter.endJob(printer)
[1820]547            self.printMoreInfo(user, printer, _("Job accounting ends."))
[1271]548               
549            # retrieve the job size   
[1321]550            if action == "DENY" :
551                jobsize = 0
[1820]552                self.printMoreInfo(user, printer, _("Job size forced to 0 because printing is denied."))
[1321]553            else :   
[2007]554                jobsize = self.accounter.getJobSize(printer)
[1974]555                if self.softwareJobSize and (jobsize != self.softwareJobSize) :
556                    self.printInfo(_("Beware : computed job size (%s) != precomputed job size (%s)") % (jobsize, self.softwareJobSize), "error")
[2007]557                jobsize += bannersize   
[1820]558            self.printMoreInfo(user, printer, _("Job size : %i") % jobsize)
[1271]559           
560            # update the quota for the current user on this printer
[1606]561            self.printInfo(_("Updating user %s's quota on printer %s") % (user.Name, printer.Name))
[1285]562            jobprice = userpquota.increasePagesUsage(jobsize)
[1271]563           
564            # adds the current job to history   
[1520]565            printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes)
[1820]566            self.printMoreInfo(user, printer, _("Job added to history."))
[1271]567           
[1372]568            # exports some new environment variables
[1517]569            os.environ["PYKOTAJOBSIZE"] = str(jobsize)
570            os.environ["PYKOTAJOBPRICE"] = str(jobprice)
[1372]571           
[1517]572            # then re-export user information with new value
[1372]573            self.exportUserInfo(userpquota)
574           
[1923]575            # handle ending banner pages after accounting ends
576            if accountbanner in ["STARTING", "NONE"] :
577                banner = self.endingBanner(printer.Name)
578                if banner :
579                    self.logdebug("Printing ending banner after accounting ends.")
580                    self.handleData(banner)
581           
[1372]582            # Launches the post hook
583            self.posthook(userpquota)
584           
[1271]585        return retcode   
[1478]586               
[1458]587    def unregisterFileNo(self, pollobj, fileno) :               
588        """Removes a file handle from the polling object."""
589        try :
590            pollobj.unregister(fileno)
591        except KeyError :   
[1584]592            self.printInfo(_("File number %s unregistered twice from polling object, ignored.") % fileno, "warn")
[1494]593        except :   
594            self.logdebug("Error while unregistering file number %s from polling object." % fileno)
[1458]595        else :   
596            self.logdebug("File number %s unregistered from polling object." % fileno)
597           
[1495]598    def formatFileEvent(self, fd, mask) :       
[1478]599        """Formats file debug info."""
[1495]600        maskval = []
601        if mask & select.POLLIN :
602            maskval.append("POLLIN")
603        if mask & select.POLLOUT :
604            maskval.append("POLLOUT")
605        if mask & select.POLLPRI :
606            maskval.append("POLLPRI")
607        if mask & select.POLLERR :
608            maskval.append("POLLERR")
609        if mask & select.POLLHUP :
610            maskval.append("POLLHUP")
611        if mask & select.POLLNVAL :
612            maskval.append("POLLNVAL")
613        return "%s (%s)" % (fd, " | ".join(maskval))
[1478]614       
[1918]615    def handleData(self, filehandle=None) :
[1271]616        """Pass the job's data to the real backend."""
[1222]617        # Find the real backend pathname   
[1271]618        realbackend = os.path.join(os.path.split(sys.argv[0])[0], self.originalbackend)
[1222]619       
620        # And launch it
[1923]621        if filehandle is None :
622            arguments = sys.argv
623        else :   
624            # Here we absolutely WANT to remove any filename from the command line !
625            arguments = [ "Fake this because we are printing a banner" ] + sys.argv[1:6]
[2006]626           
627        self.regainPriv()   
628       
[1924]629        self.logdebug("Starting real backend %s with args %s" % (realbackend, " ".join(['"%s"' % a for a in ([os.environ["DEVICE_URI"]] + arguments[1:])])))
630        subprocess = PyKotaPopen4([realbackend] + arguments[1:], bufsize=0, arg0=os.environ["DEVICE_URI"])
[1222]631       
632        # Save file descriptors, we will need them later.
633        stderrfno = sys.stderr.fileno()
634        fromcfno = subprocess.fromchild.fileno()
[1494]635        tocfno = subprocess.tochild.fileno()
[1222]636       
637        # We will have to be careful when dealing with I/O
638        # So we use a poll object to know when to read or write
639        pollster = select.poll()
640        pollster.register(fromcfno, select.POLLIN | select.POLLPRI)
[1494]641        pollster.register(stderrfno, select.POLLOUT)
642        pollster.register(tocfno, select.POLLOUT)
[1222]643       
[1494]644        # Initialize our buffers
645        indata = ""
646        outdata = ""
647        endinput = endoutput = 0
648        inputclosed = outputclosed = 0
[1897]649        totaltochild = totalfromcups = 0
650        totalfromchild = totaltocups = 0
[1494]651       
[1918]652        if filehandle is None:
653            if self.preserveinputfile is None :
654               # this is not a real file, we read the job's data
655                # from our temporary file which is a copy of stdin
656                infno = self.jobdatastream.fileno()
657                self.jobdatastream.seek(0)
658                pollster.register(infno, select.POLLIN | select.POLLPRI)
659            else :   
660                # job's data is in a file, no need to pass the data
661                # to the real backend
662                self.logdebug("Job's data is in %s" % self.preserveinputfile)
663                infno = None
664                endinput = 1
665        else:
666            self.logdebug("Printing data passed from filehandle")
667            indata = filehandle.read()
[1494]668            infno = None
669            endinput = 1
[1918]670            filehandle.close()
[1494]671       
[1606]672        self.logdebug("Entering streams polling loop...")
[1495]673        MEGABYTE = 1024*1024
[1494]674        killed = 0
675        status = -1
[1495]676        while (status == -1) and (not killed) and not (inputclosed and outputclosed) :
[1494]677            # First check if original backend is still alive
678            status = subprocess.poll()
679           
680            # Now if we got SIGTERM, we have
681            # to kill -TERM the original backend
682            if self.gotSigTerm and not killed :
683                try :
[1222]684                    os.kill(subprocess.pid, signal.SIGTERM)
[1495]685                except OSError, msg : # ignore but logs if process was already killed.
[1514]686                    self.logdebug("Error while sending signal to pid %s : %s" % (subprocess.pid, msg))
[1495]687                else :   
[1606]688                    self.printInfo(_("SIGTERM was sent to real backend %s (pid: %s)") % (realbackend, subprocess.pid))
[1291]689                    killed = 1
[1494]690           
691            # In any case, deal with any remaining I/O
[1498]692            try :
693                availablefds = pollster.poll(5000)
694            except select.error, msg :   
695                self.logdebug("Interrupted poll : %s" % msg)
696                availablefds = []
[1495]697            if not availablefds :
[1606]698                self.logdebug("Nothing to do, sleeping a bit...")
[1495]699                time.sleep(0.01) # give some time to the system
700            else :
701                for (fd, mask) in availablefds :
702                    # self.logdebug(self.formatFileEvent(fd, mask))
703                    try :
704                        if mask & select.POLLOUT :
705                            # We can write
706                            if fd == tocfno :
707                                if indata :
708                                    try :
[1897]709                                        nbwritten = os.write(fd, indata)   
[1515]710                                    except (OSError, IOError), msg :   
[1495]711                                        self.logdebug("Error while writing to real backend's stdin %s : %s" % (fd, msg))
712                                    else :   
[1897]713                                        if len(indata) != nbwritten :
714                                            self.logdebug("Short write to real backend's input !")
715                                        totaltochild += nbwritten   
716                                        self.logdebug("%s bytes sent to real backend so far..." % totaltochild)
717                                        indata = indata[nbwritten:]
[1498]718                                else :       
[1606]719                                    self.logdebug("No data to send to real backend yet, sleeping a bit...")
[1498]720                                    time.sleep(0.01)
721                                   
[1495]722                                if endinput :   
723                                    self.unregisterFileNo(pollster, tocfno)       
[1606]724                                    self.logdebug("Closing real backend's stdin.")
[1495]725                                    os.close(tocfno)
726                                    inputclosed = 1
727                            elif fd == stderrfno :
728                                if outdata :
729                                    try :
[1897]730                                        nbwritten = os.write(fd, outdata)
[1515]731                                    except (OSError, IOError), msg :   
[1495]732                                        self.logdebug("Error while writing to CUPS back channel (stderr) %s : %s" % (fd, msg))
733                                    else :
[1897]734                                        if len(outdata) != nbwritten :
735                                            self.logdebug("Short write to stderr (CUPS) !")
736                                        totaltocups += nbwritten   
737                                        self.logdebug("%s bytes sent back to CUPS so far..." % totaltocups)
738                                        outdata = outdata[nbwritten:]
[1498]739                                else :       
740                                    # self.logdebug("No data to send back to CUPS yet, sleeping a bit...") # Uncommenting this fills your logs
741                                    time.sleep(0.01) # Give some time to the system, stderr is ALWAYS writeable it seems.
742                                   
[1495]743                                if endoutput :   
744                                    self.unregisterFileNo(pollster, stderrfno)       
745                                    outputclosed = 1
[1498]746                            else :   
747                                self.logdebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask))
748                                time.sleep(0.01)
749                               
[1495]750                        if mask & (select.POLLIN | select.POLLPRI) :     
751                            # We have something to read
752                            try :
753                                data = os.read(fd, MEGABYTE)
754                            except (IOError, OSError), msg :   
755                                self.logdebug("Error while reading file %s : %s" % (fd, msg))
756                            else :
757                                if fd == infno :
758                                    if not data :    # If yes, then no more input data
759                                        self.unregisterFileNo(pollster, infno)
[1606]760                                        self.logdebug("Input data ends.")
[1495]761                                        endinput = 1 # this happens with real files.
[1498]762                                    else :   
763                                        indata += data
[1897]764                                        totalfromcups += len(data)
765                                        self.logdebug("%s bytes read from CUPS so far..." % totalfromcups)
[1495]766                                elif fd == fromcfno :
[1498]767                                    if not data :
[1606]768                                        self.logdebug("No back channel data to read from real backend yet, sleeping a bit...")
[1498]769                                        time.sleep(0.01)
770                                    else :
771                                        outdata += data
[1897]772                                        totalfromchild += len(data)
773                                        self.logdebug("%s bytes read from real backend so far..." % totalfromchild)
[1498]774                                else :   
775                                    self.logdebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask))
776                                    time.sleep(0.01)
777                                   
[1495]778                        if mask & (select.POLLHUP | select.POLLERR) :
779                            # Treat POLLERR as an EOF.
780                            # Some standard I/O stream has no more datas
781                            self.unregisterFileNo(pollster, fd)
[1494]782                            if fd == infno :
[1495]783                                # Here we are in the case where the input file is stdin.
784                                # which has no more data to be read.
[1606]785                                self.logdebug("Input data ends.")
[1495]786                                endinput = 1
787                            elif fd == fromcfno :   
788                                # We are no more interested in this file descriptor       
[1606]789                                self.logdebug("Closing real backend's stdout+stderr.")
[1495]790                                os.close(fromcfno)
791                                endoutput = 1
[1498]792                            else :   
793                                self.logdebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask))
794                                time.sleep(0.01)
[1495]795                               
796                        if mask & select.POLLNVAL :       
[1606]797                            self.logdebug("File %s was closed. Unregistering from polling object." % fd)
[1495]798                            self.unregisterFileNo(pollster, fd)
799                    except IOError, msg :           
800                        self.logdebug("Got an IOError : %s" % msg) # we got signalled during an I/O
[1191]801               
[1494]802        # We must close the real backend's input stream
803        if killed and not inputclosed :
[1606]804            self.logdebug("Forcing close of real backend's stdin.")
[1494]805            os.close(tocfno)
806       
[1606]807        self.logdebug("Exiting streams polling loop...")
[1492]808       
[1897]809        self.logdebug("input data's final length : %s" % len(indata))
810        self.logdebug("back-channel data's final length : %s" % len(outdata))
811       
812        self.logdebug("Total bytes read from CUPS (job's datas) : %s" % totalfromcups)
813        self.logdebug("Total bytes sent to real backend (job's datas) : %s" % totaltochild)
814       
815        self.logdebug("Total bytes read from real backend (back-channel datas) : %s" % totalfromchild)
816        self.logdebug("Total bytes sent back to CUPS (back-channel datas) : %s" % totaltocups)
817       
[1494]818        # Check exit code of original CUPS backend.   
819        if status == -1 :
820            # we exited the loop before the real backend exited
821            # now we have to wait for it to finish and get its status
[1606]822            self.logdebug("Waiting for real backend to exit...")
[1494]823            try :
824                status = subprocess.wait()
825            except OSError : # already dead   
826                status = 0
[1222]827        if os.WIFEXITED(status) :
828            retcode = os.WEXITSTATUS(status)
[1291]829        elif not killed :   
[1606]830            self.sendBackChannelData(_("CUPS backend %s died abnormally.") % realbackend, "error")
[1222]831            retcode = -1
[1291]832        else :   
833            retcode = self.removeJob()
[2006]834           
835        self.dropPriv()   
836       
[1271]837        return retcode   
[1222]838   
[1177]839if __name__ == "__main__" :   
840    # This is a CUPS backend, we should act and die like a CUPS backend
[1542]841    retcode = 0
[1177]842    if len(sys.argv) == 1 :
[1178]843        # we will execute each existing backend in device enumeration mode
844        # and generate their PyKota accounting counterpart
845        (directory, myname) = os.path.split(sys.argv[0])
846        for backend in [os.path.join(directory, b) for b in os.listdir(directory) if os.path.isfile(os.path.join(directory, b)) and (b != myname)] :
847            answer = os.popen(backend, "r")
848            try :
849                devices = [line.strip() for line in answer.readlines()]
850            except :   
851                devices = []
852            status = answer.close()
853            if status is None :
854                for d in devices :
[1180]855                    # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"'
856                    # so we have to decompose it carefully
[1178]857                    fdevice = cStringIO.StringIO("%s" % d)
858                    tokenizer = shlex.shlex(fdevice)
859                    tokenizer.wordchars = tokenizer.wordchars + r".:,?!~/\_$*-+={}[]()#"
860                    arguments = []
861                    while 1 :
862                        token = tokenizer.get_token()
863                        if token :
864                            arguments.append(token)
865                        else :
866                            break
867                    fdevice.close()
[1180]868                    try :
869                        (devicetype, device, name, fullname) = arguments
870                    except ValueError :   
871                        pass    # ignore this 'bizarre' device
872                    else :   
873                        if name.startswith('"') and name.endswith('"') :
874                            name = name[1:-1]
875                        if fullname.startswith('"') and fullname.endswith('"') :
876                            fullname = fullname[1:-1]
[1191]877                        print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)
[1542]878        retcode = 0               
[1177]879    elif len(sys.argv) not in (6, 7) :   
880        sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0])
881        retcode = 1
882    else :   
883        try :
[1542]884            try :
885                # Initializes the backend
886                kotabackend = PyKotaBackend()   
887            except SystemExit :   
888                retcode = -1
889            except :   
890                crashed("cupspykota backend initialization failed")
891                retcode = 1
892            else :   
893                retcode = kotabackend.mainWork()
894                kotabackend.storage.close()
895                kotabackend.closeJobDataStream()   
[1513]896        except :
[1517]897            try :
898                kotabackend.crashed("cupspykota backend failed")
899            except :   
[1542]900                crashed("cupspykota backend failed")
901            retcode = 1   
[1177]902       
903    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.