root / pykota / trunk / bin / cupspykota @ 2313

Revision 2313, 37.1 kB (checked in by jerome, 19 years ago)

Improved IPP parser.
Severity : Jamuel, you don't even need to give it a look ;-)

  • 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
[2303]21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[1177]22#
23# $Id$
24#
[2066]25#
[1177]26
27import sys
28import os
[2164]29import errno
30import tempfile
[1182]31import popen2
[1178]32import cStringIO
33import shlex
[1182]34import select
35import signal
[1291]36import time
[1177]37
[1546]38from pykota.tool import PyKotaFilterOrBackend, PyKotaToolError, crashed
[1177]39from pykota.config import PyKotaConfigError
40from pykota.storage import PyKotaStorageError
[1196]41from pykota.accounter import PyKotaAccounterError
[2313]42from pykota.ipp import IPPRequest, IPPError
[1271]43   
[1478]44class PyKotaPopen4(popen2.Popen4) :
[1182]45    """Our own class to execute real backends.
46   
47       Their first argument is different from their path so using
48       native popen2.Popen3 would not be feasible.
49    """
[1478]50    def __init__(self, cmd, bufsize=-1, arg0=None) :
[1182]51        self.arg0 = arg0
[1478]52        popen2.Popen4.__init__(self, cmd, bufsize)
[1182]53       
54    def _run_child(self, cmd):
[1704]55        try :
56            MAXFD = os.sysconf("SC_OPEN_MAX")
57        except (AttributeError, ValueError) :   
58            MAXFD = 256
59        for i in range(3, MAXFD) : 
[1182]60            try:
61                os.close(i)
62            except OSError:
63                pass
64        try:
65            os.execvpe(cmd[0], [self.arg0 or cmd[0]] + cmd[1:], os.environ)
66        finally:
67            os._exit(1)
68   
[1271]69class PyKotaBackend(PyKotaFilterOrBackend) :       
70    """A class for the pykota backend."""
71    def acceptJob(self) :       
72        """Returns the appropriate exit code to tell CUPS all is OK."""
73        return 0
74           
75    def removeJob(self) :           
76        """Returns the appropriate exit code to let CUPS think all is OK.
[1177]77       
[1271]78           Returning 0 (success) prevents CUPS from stopping the print queue.
79        """   
80        return 0
[1222]81       
[2060]82    def genBanner(self, bannerfileorcommand) :
83        """Reads a banner or generates one through an external command.
84       
85           Returns the banner's content in a format which MUST be accepted
86           by the printer.
87        """
88        if bannerfileorcommand :
89            banner = "" # no banner by default
90            if os.access(bannerfileorcommand, os.X_OK) or not os.path.isfile(bannerfileorcommand) :
91                self.logdebug("Launching %s to generate a banner." % bannerfileorcommand)
92                child = popen2.Popen3(bannerfileorcommand, capturestderr=1)
93                banner = child.fromchild.read()
94                child.tochild.close()
95                child.childerr.close()
96                child.fromchild.close()
97                status = child.wait()
98                if os.WIFEXITED(status) :
99                    status = os.WEXITSTATUS(status)
100                self.printInfo(_("Banner generator %s exit code is %s") % (bannerfileorcommand, str(status)))
101            else :
102                self.logdebug("Using %s as the banner." % bannerfileorcommand)
103                try :
104                    fh = open(bannerfileorcommand, 'r')
105                except IOError, msg :   
106                    self.printInfo("Impossible to open %s : %s" % (bannerfileorcommand, msg), "error")
107                else :   
108                    banner = fh.read()
109                    fh.close()
110            if banner :       
111                return cStringIO.StringIO(banner)
112   
113    def startingBanner(self, printername) :
114        """Retrieves a starting banner for current printer and returns its content."""
115        self.logdebug("Retrieving starting banner...")
116        return self.genBanner(self.config.getStartingBanner(printername))
117   
118    def endingBanner(self, printername) :
119        """Retrieves an ending banner for current printer and returns its content."""
120        self.logdebug("Retrieving ending banner...")
121        return self.genBanner(self.config.getEndingBanner(printername))
122       
[1901]123    def getCupsConfigDirectives(self, directives=[]) :
[1902]124        """Retrieves some CUPS directives from its configuration file.
125       
126           Returns a mapping with lowercased directives as keys and
127           their setting as values.
128        """
[1901]129        dirvalues = {} 
[1502]130        cupsroot = os.environ.get("CUPS_SERVERROOT", "/etc/cups")
131        cupsdconf = os.path.join(cupsroot, "cupsd.conf")
132        try :
133            conffile = open(cupsdconf, "r")
134        except IOError :   
[1503]135            self.logdebug("Unable to open %s" % cupsdconf)
[1502]136        else :   
137            for line in conffile.readlines() :
138                linecopy = line.strip().lower()
[1901]139                for di in [d.lower() for d in directives] :
140                    if linecopy.startswith("%s " % di) :
141                        try :
142                            val = line.split()[1]
143                        except :   
144                            pass # ignore errors, we take the last value in any case.
145                        else :   
146                            dirvalues[di] = val
[1502]147            conffile.close()           
[1901]148        return dirvalues       
[1502]149           
[2217]150    def getJobInfosFromPageLog(self, cupsconfig, printername, username, jobid) :
151        """Retrieves the job-originating-hostname and job-billing attributes from the CUPS page_log file if possible."""
[1901]152        pagelogpath = cupsconfig.get("pagelog", "/var/log/cups/page_log")
153        self.logdebug("Trying to extract job-originating-host-name from %s" % pagelogpath)
[1502]154        try :
155            pagelog = open(pagelogpath, "r")
156        except IOError :   
[1503]157            self.logdebug("Unable to open %s" % pagelogpath)
[2217]158            return (None, None) # no page log or can't read it, originating hostname unknown yet
[1502]159        else :   
160            # TODO : read backward so we could take first value seen
161            # TODO : here we read forward so we must take the last value seen
[1819]162            prefix = ("%s %s %s " % (printername, username, jobid)).lower()
[1502]163            matchingline = None
164            while 1 :
165                line = pagelog.readline()
166                if not line :
167                    break
168                else :
169                    line = line.strip()
[1530]170                    if line.lower().startswith(prefix) :   
[1502]171                        matchingline = line # no break, because we read forward
172            pagelog.close()       
173            if matchingline is None :
[1606]174                self.logdebug("No matching line found in %s" % pagelogpath)
[2217]175                return (None, None) # correct line not found, job-originating-host-name unknown
[1502]176            else :   
[2217]177                (jobbilling, hostname) = matchingline.split()[-2:]
178                if jobbilling == "-" :
179                    jobbilling = ""
180                return (jobbilling, hostname)   
[1502]181               
[1271]182    def doWork(self, policy, printer, user, userpquota) :   
183        """Most of the work is done here."""
184        # Two different values possible for policy here :
185        # ALLOW means : Either printer, user or user print quota doesn't exist,
186        #               but the job should be allowed anyway.
187        # OK means : Both printer, user and user print quota exist, job should
188        #            be allowed if current user is allowed to print on this printer
189        if policy == "OK" :
[1372]190            # exports user information with initial values
191            self.exportUserInfo(userpquota)
192           
[1901]193            # tries to extract job-originating-host-name and other information
[2177]194            self.regainPriv()
[1901]195            cupsdconf = self.getCupsConfigDirectives(["PageLog", "RequestRoot"])
196            requestroot = cupsdconf.get("requestroot", "/var/spool/cups")
197            if (len(self.jobid) < 5) and self.jobid.isdigit() :
198                ippmessagefile = "c%05i" % int(self.jobid)
199            else :   
200                ippmessagefile = "c%s" % self.jobid
201            ippmessagefile = os.path.join(requestroot, ippmessagefile)
202            ippmessage = {}
203            try :
204                ippdatafile = open(ippmessagefile)
205            except :   
206                self.printInfo("Unable to open IPP message file %s" % ippmessagefile, "warn")
207            else :   
208                self.logdebug("Parsing of IPP message file %s begins." % ippmessagefile)
[1902]209                try :
[2313]210                    ippmessage = IPPRequest(ippdatafile.read())
[2311]211                    ippmessage.parse()
212                except IPPError, msg :   
[1902]213                    self.printInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn")
214                else :   
215                    self.logdebug("Parsing of IPP message file %s ends." % ippmessagefile)
[1901]216                ippdatafile.close()
[2007]217            self.dropPriv()   
[2254]218           
219            try :
220                (chtype, clienthost) = ippmessage.operation_attributes.get("job-originating-host-name", \
221                                          ippmessage.job_attributes.get("job-originating-host-name", (None, None)))
222                (jbtype, billingcode) = ippmessage.job_attributes.get("job-billing", (None, None))
223            except AttributeError :   
224                clienthost = None
225                billingcode = None
[2217]226            if clienthost is None :
227                (billingcode, clienthost) = self.getJobInfosFromPageLog(cupsdconf, printer.Name, user.Name, self.jobid)
[1503]228            self.logdebug("Client Hostname : %s" % (clienthost or "Unknown"))   
[2217]229            self.logdebug("Billing Code : %s" % (billingcode or "None"))   
[2254]230           
[2239]231            os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "")
232            os.environ["PYKOTAJOBBILLING"] = str(billingcode or "")
[1502]233           
[1375]234            # enters first phase
[1517]235            os.environ["PYKOTAPHASE"] = "BEFORE"
[1375]236           
[2060]237            # precomputes the job's price
238            self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize)
[1517]239            os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice)
[2060]240            self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice))
[1519]241           
[2308]242            denyduplicates = self.config.getDenyDuplicates(printer.Name)
[1519]243            if not self.jobSizeBytes :
[2066]244                # if no data to pass to real backend, probably a filter
245                # higher in the chain failed because of a misconfiguration.
246                # we deny the job in this case (nothing to print anyway)
[1820]247                self.printMoreInfo(user, printer, _("Job contains no data. Printing is denied."), "warn")
[1519]248                action = "DENY"
[2308]249            elif denyduplicates \
[2066]250                 and printer.LastJob.Exists \
251                 and (printer.LastJob.UserName == user.Name) \
252                 and (printer.LastJob.JobMD5Sum == self.checksum) :
[2308]253                if denyduplicates == 1 :
254                    self.printMoreInfo(user, printer, _("Job is a duplicate. Printing is denied."), "warn")
255                    action = "DENY"
256                else :   
257                    self.logdebug("Launching subprocess [%s] to see if dupes should be allowed or not." % denyduplicates)
258                    fanswer = os.popen(denyduplicates, "r")
259                    action = fanswer.read().strip().upper()
260                    fanswer.close()
261                    if action == "DENY" :     
262                        self.printMoreInfo(user, printer, _("Job is a duplicate. Printing is denied by subprocess."), "warn")
263                    else :   
264                        self.printMoreInfo(user, printer, _("Job is a duplicate. Printing is allowed by subprocess."), "warn")
265                        action = "ALLOW" # just to be sure, in the case the external command returns something else...
[1606]266            else :   
267                # checks the user's quota
268                action = self.warnUserPQuota(userpquota)
[1519]269           
[1372]270            # exports some new environment variables
[1517]271            os.environ["PYKOTAACTION"] = action
[1372]272           
273            # launches the pre hook
274            self.prehook(userpquota)
[1918]275
276            # saves the size of banners which have to be accounted for
277            # this is needed in the case of software accounting
278            bannersize = 0
[1372]279           
[1918]280            # handle starting banner pages before accounting
281            accountbanner = self.config.getAccountBanner(printer.Name)
282            if accountbanner in ["ENDING", "NONE"] :
[2066]283                if (action == 'DENY') and (userpquota.WarnCount >= self.config.getMaxDenyBanners(printer.Name)) :
[2054]284                    self.printInfo(_("Banner won't be printed : maximum number of deny banners reached."), "warn")
285                else :
286                    if action == 'DENY' :
[2066]287                        self.logdebug("Incrementing the number of deny banners for user %s on printer %s" % (user.Name, printer.Name))
288                        userpquota.incDenyBannerCounter() # increments the warning counter
[2054]289                        self.exportUserInfo(userpquota)
290                    banner = self.startingBanner(printer.Name)
291                    if banner :
292                        self.logdebug("Printing starting banner before accounting begins.")
293                        self.handleData(banner)
[1918]294 
[1820]295            self.printMoreInfo(user, printer, _("Job accounting begins."))
[1624]296            self.accounter.beginJob(printer)
[1918]297           
298            # handle starting banner pages during accounting
299            if accountbanner in ["STARTING", "BOTH"] :
[2066]300                if (action == 'DENY') and (userpquota.WarnCount >= self.config.getMaxDenyBanners(printer.Name)) :
[2054]301                    self.printInfo(_("Banner won't be printed : maximum number of deny banners reached."), "warn")
302                else :
303                    if action == 'DENY' :
[2066]304                        self.logdebug("Incrementing the number of deny banners for user %s on printer %s" % (user.Name, printer.Name))
305                        userpquota.incDenyBannerCounter() # increments the warning counter
[2054]306                        self.exportUserInfo(userpquota)
307                    banner = self.startingBanner(printer.Name)
308                    if banner :
309                        self.logdebug("Printing starting banner during accounting.")
310                        self.handleData(banner)
311                        if self.accounter.isSoftware :
312                            bannersize += 1 # TODO : fix this by passing the banner's content through PDLAnalyzer
[1280]313        else :   
314            action = "ALLOW"
[1517]315            os.environ["PYKOTAACTION"] = action
[1271]316           
317        # pass the job's data to the real backend   
[1280]318        if action in ["ALLOW", "WARN"] :
[1291]319            if self.gotSigTerm :
[1280]320                retcode = self.removeJob()
321            else :   
322                retcode = self.handleData()       
323        else :       
[1271]324            retcode = self.removeJob()
325       
326        if policy == "OK" :       
[1374]327            # indicate phase change
[1517]328            os.environ["PYKOTAPHASE"] = "AFTER"
[1374]329           
[1918]330            # handle ending banner pages during accounting
331            if accountbanner in ["ENDING", "BOTH"] :
[2066]332                if (action == 'DENY') and (userpquota.WarnCount >= self.config.getMaxDenyBanners(printer.Name)) :
[2054]333                    self.printInfo(_("Banner won't be printed : maximum number of deny banners reached."), "warn")
334                else :
335                    if action == 'DENY' :
[2066]336                        self.logdebug("Incrementing the number of deny banners for user %s on printer %s" % (user.Name, printer.Name))
337                        userpquota.incDenyBannerCounter() # increments the warning counter
[2054]338                        self.exportUserInfo(userpquota)
339                    banner = self.endingBanner(printer.Name)
340                    if banner :
341                        self.logdebug("Printing ending banner during accounting.")
342                        self.handleData(banner)
343                        if self.accounter.isSoftware :
344                            bannersize += 1 # TODO : fix this by passing the banner's content through PDLAnalyzer
[1918]345 
[1271]346            # stops accounting.
[1624]347            self.accounter.endJob(printer)
[1820]348            self.printMoreInfo(user, printer, _("Job accounting ends."))
[1271]349               
350            # retrieve the job size   
[1321]351            if action == "DENY" :
352                jobsize = 0
[1820]353                self.printMoreInfo(user, printer, _("Job size forced to 0 because printing is denied."))
[1321]354            else :   
[2066]355                userpquota.resetDenyBannerCounter()
[2007]356                jobsize = self.accounter.getJobSize(printer)
[1974]357                if self.softwareJobSize and (jobsize != self.softwareJobSize) :
358                    self.printInfo(_("Beware : computed job size (%s) != precomputed job size (%s)") % (jobsize, self.softwareJobSize), "error")
[2062]359                    (limit, replacement) = self.config.getTrustJobSize(printer.Name)
360                    if limit is None :
361                        self.printInfo(_("The job size will be trusted anyway according to the 'trustjobsize' directive"), "warn")
362                    else :
363                        if jobsize <= limit :
364                            self.printInfo(_("The job size will be trusted because it is inferior to the 'trustjobsize' directive's limit %s") % limit, "warn")
365                        else :
366                            self.printInfo(_("The job size will be modified according to the 'trustjobsize' directive : %s") % replacement, "warn")
367                            if replacement == "PRECOMPUTED" :
368                                jobsize = self.softwareJobSize
369                            else :   
370                                jobsize = replacement
[2007]371                jobsize += bannersize   
[1820]372            self.printMoreInfo(user, printer, _("Job size : %i") % jobsize)
[1271]373           
374            # update the quota for the current user on this printer
[1606]375            self.printInfo(_("Updating user %s's quota on printer %s") % (user.Name, printer.Name))
[1285]376            jobprice = userpquota.increasePagesUsage(jobsize)
[1271]377           
378            # adds the current job to history   
[2057]379            printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), \
380                                    action, jobsize, jobprice, self.preserveinputfile, \
381                                    self.title, self.copies, self.options, clienthost, \
[2217]382                                    self.jobSizeBytes, self.checksum, None, billingcode)
[1820]383            self.printMoreInfo(user, printer, _("Job added to history."))
[1271]384           
[1372]385            # exports some new environment variables
[1517]386            os.environ["PYKOTAJOBSIZE"] = str(jobsize)
387            os.environ["PYKOTAJOBPRICE"] = str(jobprice)
[1372]388           
[1517]389            # then re-export user information with new value
[1372]390            self.exportUserInfo(userpquota)
391           
[1923]392            # handle ending banner pages after accounting ends
393            if accountbanner in ["STARTING", "NONE"] :
[2066]394                if (action == 'DENY') and (userpquota.WarnCount >= self.config.getMaxDenyBanners(printer.Name)) :
[2054]395                    self.printInfo(_("Banner won't be printed : maximum number of deny banners reached."), "warn")
396                else :
397                    if action == 'DENY' :
[2066]398                        self.logdebug("Incrementing the number of deny banners for user %s on printer %s" % (user.Name, printer.Name))
399                        userpquota.incDenyBannerCounter() # increments the warning counter
[2054]400                        self.exportUserInfo(userpquota)
401                    banner = self.endingBanner(printer.Name)
402                    if banner :
403                        self.logdebug("Printing ending banner after accounting ends.")
404                        self.handleData(banner)
405                       
[1372]406            # Launches the post hook
407            self.posthook(userpquota)
408           
[1271]409        return retcode   
[1478]410               
[1458]411    def unregisterFileNo(self, pollobj, fileno) :               
412        """Removes a file handle from the polling object."""
413        try :
414            pollobj.unregister(fileno)
415        except KeyError :   
[1584]416            self.printInfo(_("File number %s unregistered twice from polling object, ignored.") % fileno, "warn")
[1494]417        except :   
418            self.logdebug("Error while unregistering file number %s from polling object." % fileno)
[1458]419        else :   
420            self.logdebug("File number %s unregistered from polling object." % fileno)
421           
[1495]422    def formatFileEvent(self, fd, mask) :       
[1478]423        """Formats file debug info."""
[1495]424        maskval = []
425        if mask & select.POLLIN :
426            maskval.append("POLLIN")
427        if mask & select.POLLOUT :
428            maskval.append("POLLOUT")
429        if mask & select.POLLPRI :
430            maskval.append("POLLPRI")
431        if mask & select.POLLERR :
432            maskval.append("POLLERR")
433        if mask & select.POLLHUP :
434            maskval.append("POLLHUP")
435        if mask & select.POLLNVAL :
436            maskval.append("POLLNVAL")
437        return "%s (%s)" % (fd, " | ".join(maskval))
[1478]438       
[1918]439    def handleData(self, filehandle=None) :
[1271]440        """Pass the job's data to the real backend."""
[1222]441        # Find the real backend pathname   
[1271]442        realbackend = os.path.join(os.path.split(sys.argv[0])[0], self.originalbackend)
[1222]443       
444        # And launch it
[1923]445        if filehandle is None :
446            arguments = sys.argv
447        else :   
448            # Here we absolutely WANT to remove any filename from the command line !
449            arguments = [ "Fake this because we are printing a banner" ] + sys.argv[1:6]
[2006]450           
451        self.regainPriv()   
452       
[1924]453        self.logdebug("Starting real backend %s with args %s" % (realbackend, " ".join(['"%s"' % a for a in ([os.environ["DEVICE_URI"]] + arguments[1:])])))
454        subprocess = PyKotaPopen4([realbackend] + arguments[1:], bufsize=0, arg0=os.environ["DEVICE_URI"])
[1222]455       
456        # Save file descriptors, we will need them later.
457        stderrfno = sys.stderr.fileno()
458        fromcfno = subprocess.fromchild.fileno()
[1494]459        tocfno = subprocess.tochild.fileno()
[1222]460       
461        # We will have to be careful when dealing with I/O
462        # So we use a poll object to know when to read or write
463        pollster = select.poll()
464        pollster.register(fromcfno, select.POLLIN | select.POLLPRI)
[1494]465        pollster.register(stderrfno, select.POLLOUT)
466        pollster.register(tocfno, select.POLLOUT)
[1222]467       
[1494]468        # Initialize our buffers
469        indata = ""
470        outdata = ""
471        endinput = endoutput = 0
472        inputclosed = outputclosed = 0
[1897]473        totaltochild = totalfromcups = 0
474        totalfromchild = totaltocups = 0
[1494]475       
[1918]476        if filehandle is None:
477            if self.preserveinputfile is None :
478               # this is not a real file, we read the job's data
479                # from our temporary file which is a copy of stdin
480                infno = self.jobdatastream.fileno()
481                self.jobdatastream.seek(0)
482                pollster.register(infno, select.POLLIN | select.POLLPRI)
483            else :   
484                # job's data is in a file, no need to pass the data
485                # to the real backend
486                self.logdebug("Job's data is in %s" % self.preserveinputfile)
487                infno = None
488                endinput = 1
489        else:
490            self.logdebug("Printing data passed from filehandle")
491            indata = filehandle.read()
[1494]492            infno = None
493            endinput = 1
[1918]494            filehandle.close()
[1494]495       
[1606]496        self.logdebug("Entering streams polling loop...")
[1495]497        MEGABYTE = 1024*1024
[1494]498        killed = 0
499        status = -1
[1495]500        while (status == -1) and (not killed) and not (inputclosed and outputclosed) :
[1494]501            # First check if original backend is still alive
502            status = subprocess.poll()
503           
504            # Now if we got SIGTERM, we have
505            # to kill -TERM the original backend
506            if self.gotSigTerm and not killed :
507                try :
[1222]508                    os.kill(subprocess.pid, signal.SIGTERM)
[1495]509                except OSError, msg : # ignore but logs if process was already killed.
[1514]510                    self.logdebug("Error while sending signal to pid %s : %s" % (subprocess.pid, msg))
[1495]511                else :   
[1606]512                    self.printInfo(_("SIGTERM was sent to real backend %s (pid: %s)") % (realbackend, subprocess.pid))
[1291]513                    killed = 1
[1494]514           
515            # In any case, deal with any remaining I/O
[1498]516            try :
517                availablefds = pollster.poll(5000)
518            except select.error, msg :   
519                self.logdebug("Interrupted poll : %s" % msg)
520                availablefds = []
[1495]521            if not availablefds :
[1606]522                self.logdebug("Nothing to do, sleeping a bit...")
[1495]523                time.sleep(0.01) # give some time to the system
524            else :
525                for (fd, mask) in availablefds :
526                    # self.logdebug(self.formatFileEvent(fd, mask))
527                    try :
528                        if mask & select.POLLOUT :
529                            # We can write
530                            if fd == tocfno :
531                                if indata :
532                                    try :
[1897]533                                        nbwritten = os.write(fd, indata)   
[1515]534                                    except (OSError, IOError), msg :   
[1495]535                                        self.logdebug("Error while writing to real backend's stdin %s : %s" % (fd, msg))
536                                    else :   
[1897]537                                        if len(indata) != nbwritten :
538                                            self.logdebug("Short write to real backend's input !")
539                                        totaltochild += nbwritten   
540                                        self.logdebug("%s bytes sent to real backend so far..." % totaltochild)
541                                        indata = indata[nbwritten:]
[1498]542                                else :       
[1606]543                                    self.logdebug("No data to send to real backend yet, sleeping a bit...")
[1498]544                                    time.sleep(0.01)
545                                   
[1495]546                                if endinput :   
547                                    self.unregisterFileNo(pollster, tocfno)       
[1606]548                                    self.logdebug("Closing real backend's stdin.")
[1495]549                                    os.close(tocfno)
550                                    inputclosed = 1
551                            elif fd == stderrfno :
552                                if outdata :
553                                    try :
[1897]554                                        nbwritten = os.write(fd, outdata)
[1515]555                                    except (OSError, IOError), msg :   
[1495]556                                        self.logdebug("Error while writing to CUPS back channel (stderr) %s : %s" % (fd, msg))
557                                    else :
[1897]558                                        if len(outdata) != nbwritten :
559                                            self.logdebug("Short write to stderr (CUPS) !")
560                                        totaltocups += nbwritten   
561                                        self.logdebug("%s bytes sent back to CUPS so far..." % totaltocups)
562                                        outdata = outdata[nbwritten:]
[1498]563                                else :       
564                                    # self.logdebug("No data to send back to CUPS yet, sleeping a bit...") # Uncommenting this fills your logs
565                                    time.sleep(0.01) # Give some time to the system, stderr is ALWAYS writeable it seems.
566                                   
[1495]567                                if endoutput :   
568                                    self.unregisterFileNo(pollster, stderrfno)       
569                                    outputclosed = 1
[1498]570                            else :   
571                                self.logdebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask))
572                                time.sleep(0.01)
573                               
[1495]574                        if mask & (select.POLLIN | select.POLLPRI) :     
575                            # We have something to read
576                            try :
577                                data = os.read(fd, MEGABYTE)
578                            except (IOError, OSError), msg :   
579                                self.logdebug("Error while reading file %s : %s" % (fd, msg))
580                            else :
581                                if fd == infno :
582                                    if not data :    # If yes, then no more input data
583                                        self.unregisterFileNo(pollster, infno)
[1606]584                                        self.logdebug("Input data ends.")
[1495]585                                        endinput = 1 # this happens with real files.
[1498]586                                    else :   
587                                        indata += data
[1897]588                                        totalfromcups += len(data)
589                                        self.logdebug("%s bytes read from CUPS so far..." % totalfromcups)
[1495]590                                elif fd == fromcfno :
[1498]591                                    if not data :
[1606]592                                        self.logdebug("No back channel data to read from real backend yet, sleeping a bit...")
[1498]593                                        time.sleep(0.01)
594                                    else :
595                                        outdata += data
[1897]596                                        totalfromchild += len(data)
597                                        self.logdebug("%s bytes read from real backend so far..." % totalfromchild)
[1498]598                                else :   
599                                    self.logdebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask))
600                                    time.sleep(0.01)
601                                   
[1495]602                        if mask & (select.POLLHUP | select.POLLERR) :
603                            # Treat POLLERR as an EOF.
604                            # Some standard I/O stream has no more datas
605                            self.unregisterFileNo(pollster, fd)
[1494]606                            if fd == infno :
[1495]607                                # Here we are in the case where the input file is stdin.
608                                # which has no more data to be read.
[1606]609                                self.logdebug("Input data ends.")
[1495]610                                endinput = 1
611                            elif fd == fromcfno :   
612                                # We are no more interested in this file descriptor       
[1606]613                                self.logdebug("Closing real backend's stdout+stderr.")
[1495]614                                os.close(fromcfno)
615                                endoutput = 1
[1498]616                            else :   
617                                self.logdebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask))
618                                time.sleep(0.01)
[1495]619                               
620                        if mask & select.POLLNVAL :       
[1606]621                            self.logdebug("File %s was closed. Unregistering from polling object." % fd)
[1495]622                            self.unregisterFileNo(pollster, fd)
623                    except IOError, msg :           
624                        self.logdebug("Got an IOError : %s" % msg) # we got signalled during an I/O
[1191]625               
[1494]626        # We must close the real backend's input stream
627        if killed and not inputclosed :
[1606]628            self.logdebug("Forcing close of real backend's stdin.")
[1494]629            os.close(tocfno)
630       
[1606]631        self.logdebug("Exiting streams polling loop...")
[1492]632       
[1897]633        self.logdebug("input data's final length : %s" % len(indata))
634        self.logdebug("back-channel data's final length : %s" % len(outdata))
635       
636        self.logdebug("Total bytes read from CUPS (job's datas) : %s" % totalfromcups)
637        self.logdebug("Total bytes sent to real backend (job's datas) : %s" % totaltochild)
638       
639        self.logdebug("Total bytes read from real backend (back-channel datas) : %s" % totalfromchild)
640        self.logdebug("Total bytes sent back to CUPS (back-channel datas) : %s" % totaltocups)
641       
[1494]642        # Check exit code of original CUPS backend.   
643        if status == -1 :
644            # we exited the loop before the real backend exited
645            # now we have to wait for it to finish and get its status
[1606]646            self.logdebug("Waiting for real backend to exit...")
[1494]647            try :
648                status = subprocess.wait()
[2054]649            except OSError : # already dead : TODO : detect when abnormal
[1494]650                status = 0
[1222]651        if os.WIFEXITED(status) :
652            retcode = os.WEXITSTATUS(status)
[1291]653        elif not killed :   
[1606]654            self.sendBackChannelData(_("CUPS backend %s died abnormally.") % realbackend, "error")
[1222]655            retcode = -1
[1291]656        else :   
657            retcode = self.removeJob()
[2006]658           
659        self.dropPriv()   
660       
[1271]661        return retcode   
[1222]662   
[1177]663if __name__ == "__main__" :   
664    # This is a CUPS backend, we should act and die like a CUPS backend
[1542]665    retcode = 0
[1177]666    if len(sys.argv) == 1 :
[1178]667        (directory, myname) = os.path.split(sys.argv[0])
[2164]668        tmpdir = tempfile.gettempdir()
669        lockfilename = os.path.join(tmpdir, "%s..LCK" % myname)
670        if os.path.exists(lockfilename) :
671            # there's already a lockfile, see if still used
672            lockfile = open(lockfilename, "r")
673            pid = int(lockfile.read())
674            lockfile.close()
[1178]675            try :
[2164]676                # see if the pid contained in the lock file is still running
677                os.kill(pid, 0)
678            except OSError, e :   
679                if e.errno != errno.EPERM :
680                    # process doesn't exist anymore, remove the lock
681                    os.remove(lockfilename)
682           
683        if not os.path.exists(lockfilename) :
684            lockfile = open(lockfilename, "w")
685            lockfile.write("%i" % os.getpid())
686            lockfile.close()
687            # we will execute each existing backend in device enumeration mode
688            # and generate their PyKota accounting counterpart
[2165]689            allbackends = [ os.path.join(directory, b) \
690                                for b in os.listdir(directory) 
691                                    if os.access(os.path.join(directory, b), os.X_OK) \
692                                        and (b != myname)] 
693            for backend in allbackends :                           
[2164]694                answer = os.popen(backend, "r")
695                try :
696                    devices = [line.strip() for line in answer.readlines()]
697                except :   
698                    devices = []
699                status = answer.close()
700                if status is None :
701                    for d in devices :
702                        # each line is of the form : 'xxxx xxxx "xxxx xxx" "xxxx xxx"'
703                        # so we have to decompose it carefully
704                        fdevice = cStringIO.StringIO("%s" % d)
705                        tokenizer = shlex.shlex(fdevice)
706                        tokenizer.wordchars = tokenizer.wordchars + r".:,?!~/\_$*-+={}[]()#"
707                        arguments = []
708                        while 1 :
709                            token = tokenizer.get_token()
710                            if token :
711                                arguments.append(token)
712                            else :
713                                break
714                        fdevice.close()
715                        try :
716                            (devicetype, device, name, fullname) = arguments
717                        except ValueError :   
718                            pass    # ignore this 'bizarre' device
719                        else :   
720                            if name.startswith('"') and name.endswith('"') :
721                                name = name[1:-1]
722                            if fullname.startswith('"') and fullname.endswith('"') :
723                                fullname = fullname[1:-1]
724                            print '%s cupspykota:%s "PyKota+%s" "PyKota managed %s"' % (devicetype, device, name, fullname)
725            os.remove(lockfilename)
[1542]726        retcode = 0               
[1177]727    elif len(sys.argv) not in (6, 7) :   
728        sys.stderr.write("ERROR: %s job-id user title copies options [file]\n" % sys.argv[0])
729        retcode = 1
730    else :   
731        try :
[2210]732            # Initializes the backend
733            kotabackend = PyKotaBackend()   
734            kotabackend.deferredInit()
735            retcode = kotabackend.mainWork()
736            kotabackend.storage.close()
737            kotabackend.closeJobDataStream()   
738        except SystemExit :   
739            retcode = -1
[1513]740        except :
[1517]741            try :
742                kotabackend.crashed("cupspykota backend failed")
743            except :   
[1542]744                crashed("cupspykota backend failed")
745            retcode = 1   
[1177]746       
747    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.