331 | | try : |
332 | | name = ins.get(fd, outs.get(fd))["name"] |
333 | | except KeyError : |
334 | | self.logdebug("File %s not found in %s or %s" % (fd, repr(ins), repr(outs))) |
335 | | else : |
336 | | maskval = [] |
337 | | if mask & select.POLLIN : |
338 | | maskval.append("POLLIN") |
339 | | if mask & select.POLLOUT : |
340 | | maskval.append("POLLOUT") |
341 | | if mask & select.POLLPRI : |
342 | | maskval.append("POLLPRI") |
343 | | if mask & select.POLLERR : |
344 | | maskval.append("POLLERR") |
345 | | if mask & select.POLLHUP : |
346 | | maskval.append("POLLHUP") |
347 | | if mask & select.POLLNVAL : |
348 | | maskval.append("POLLNVAL") |
349 | | return "%s (%s)" % (name, " | ".join(maskval)) |
| 331 | maskval = [] |
| 332 | if mask & select.POLLIN : |
| 333 | maskval.append("POLLIN") |
| 334 | if mask & select.POLLOUT : |
| 335 | maskval.append("POLLOUT") |
| 336 | if mask & select.POLLPRI : |
| 337 | maskval.append("POLLPRI") |
| 338 | if mask & select.POLLERR : |
| 339 | maskval.append("POLLERR") |
| 340 | if mask & select.POLLHUP : |
| 341 | maskval.append("POLLHUP") |
| 342 | if mask & select.POLLNVAL : |
| 343 | maskval.append("POLLNVAL") |
| 344 | return "%s (%s)" % (fd, " | ".join(maskval)) |
413 | | for (fd, mask) in availablefds : |
414 | | # self.logdebug("file: %i mask: %04x" % (fd, mask)) |
415 | | try : |
416 | | if mask & select.POLLOUT : |
417 | | # We can write |
418 | | if fd == tocfno : |
419 | | if indata : |
420 | | os.write(fd, indata) |
421 | | try : |
422 | | os.fsync(fd) |
423 | | except OSError : |
424 | | pass |
425 | | indata = "" |
426 | | if endinput : |
427 | | self.unregisterFileNo(pollster, tocfno) |
428 | | self.logdebug("Closing real backend's stdin.") |
429 | | os.close(tocfno) |
430 | | inputclosed = 1 |
431 | | elif fd == stderrfno : |
432 | | if outdata : |
433 | | os.write(fd, outdata) |
434 | | try : |
435 | | os.fsync(fd) |
436 | | except OSError : |
437 | | pass |
438 | | outdata = "" |
439 | | if endoutput : |
440 | | self.unregisterFileNo(pollster, stderrfno) |
441 | | outputclosed = 1 |
442 | | if (mask & select.POLLIN) or (mask & select.POLLPRI) : |
443 | | # We have something to read |
444 | | try : |
445 | | data = os.read(fd, 256 * 1024) |
446 | | except OSError, msg : |
447 | | self.logdebug("Error while reading file %s : %s" % (fd, msg)) |
448 | | else : |
| 410 | if not availablefds : |
| 411 | self.logdebug("Nothing to do, sleeping a bit...") |
| 412 | time.sleep(0.01) # give some time to the system |
| 413 | else : |
| 414 | for (fd, mask) in availablefds : |
| 415 | # self.logdebug(self.formatFileEvent(fd, mask)) |
| 416 | try : |
| 417 | if mask & select.POLLOUT : |
| 418 | # We can write |
| 419 | if fd == tocfno : |
| 420 | if indata : |
| 421 | try : |
| 422 | os.write(fd, indata) |
| 423 | except IOError, msg : |
| 424 | self.logdebug("Error while writing to real backend's stdin %s : %s" % (fd, msg)) |
| 425 | else : |
| 426 | indata = "" |
| 427 | if endinput : |
| 428 | self.unregisterFileNo(pollster, tocfno) |
| 429 | self.logdebug("Closing real backend's stdin.") |
| 430 | os.close(tocfno) |
| 431 | inputclosed = 1 |
| 432 | elif fd == stderrfno : |
| 433 | if outdata : |
| 434 | try : |
| 435 | os.write(fd, outdata) |
| 436 | except IOError, msg : |
| 437 | self.logdebug("Error while writing to CUPS back channel (stderr) %s : %s" % (fd, msg)) |
| 438 | else : |
| 439 | outdata = "" |
| 440 | if endoutput : |
| 441 | self.unregisterFileNo(pollster, stderrfno) |
| 442 | outputclosed = 1 |
| 443 | if mask & (select.POLLIN | select.POLLPRI) : |
| 444 | # We have something to read |
| 445 | try : |
| 446 | data = os.read(fd, MEGABYTE) |
| 447 | except (IOError, OSError), msg : |
| 448 | self.logdebug("Error while reading file %s : %s" % (fd, msg)) |
| 449 | else : |
| 450 | if fd == infno : |
| 451 | indata += data |
| 452 | if not data : # If yes, then no more input data |
| 453 | self.unregisterFileNo(pollster, infno) |
| 454 | self.logdebug("Input data ends.") |
| 455 | endinput = 1 # this happens with real files. |
| 456 | elif fd == fromcfno : |
| 457 | outdata += data |
| 458 | if mask & (select.POLLHUP | select.POLLERR) : |
| 459 | # Treat POLLERR as an EOF. |
| 460 | # Some standard I/O stream has no more datas |
| 461 | self.unregisterFileNo(pollster, fd) |
450 | | indata += data |
451 | | if not data : # If yes, then no more input data |
452 | | self.unregisterFileNo(pollster, infno) |
453 | | self.logdebug("Input data ends.") |
454 | | endinput = 1 # this happens with real files. |
455 | | elif fd == fromcfno : |
456 | | outdata += data |
457 | | if (mask & select.POLLHUP) or (mask & select.POLLERR) : |
458 | | # I've never seen POLLERR myself, but this probably |
459 | | # can't hurt to treat an error condition just like |
460 | | # an EOF. |
461 | | # |
462 | | # Some standard I/O stream has no more datas |
463 | | self.unregisterFileNo(pollster, fd) |
464 | | if fd == infno : |
465 | | # Here we are in the case where the input file is stdin. |
466 | | # which has no more data to be read. |
467 | | self.logdebug("Input data ends.") |
468 | | endinput = 1 |
469 | | elif fd == fromcfno : |
470 | | # We are no more interested in this file descriptor |
471 | | self.logdebug("Closing real backend's stdout+stderr.") |
472 | | os.close(fromcfno) |
473 | | endoutput = 1 |
474 | | except IOError : |
475 | | pass # we got signalled during an I/O it seems |
476 | | if killed or (inputclosed and outputclosed) : |
477 | | break |
| 463 | # Here we are in the case where the input file is stdin. |
| 464 | # which has no more data to be read. |
| 465 | self.logdebug("Input data ends.") |
| 466 | endinput = 1 |
| 467 | elif fd == fromcfno : |
| 468 | # We are no more interested in this file descriptor |
| 469 | self.logdebug("Closing real backend's stdout+stderr.") |
| 470 | os.close(fromcfno) |
| 471 | endoutput = 1 |
| 472 | |
| 473 | if mask & select.POLLNVAL : |
| 474 | self.logdebug("File %s was closed. Unregistering from polling object." % fd) |
| 475 | self.unregisterFileNo(pollster, fd) |
| 476 | except IOError, msg : |
| 477 | self.logdebug("Got an IOError : %s" % msg) # we got signalled during an I/O |