#!/usr/bin/perl # beh - Backend Error Handler # A wrapper for CUPS backends to make error handling configurable # (C) Till Kamppeter # This is free software under the terms of the GNU GPL # Usage: # # cp beh /usr/lib/cups/backend/ # chmod 755 /usr/lib/cups/backend/beh # killall -HUP cupsd (or "/etc/init.d/cups restart") # lpadmin -p -E -v beh:/
/// # # with # : The name of your print queue #
: Don't Disable, if "1", beh always exits with zero # status, so the queue gets never disabled when the # original backend exits with an error. "0" carries # the error status of the last call of the backend # (after retries) on to CUPS, so the queue # usually gets disabled. # : Attempts, number of attempts to recall the backend # in case of an error. "0" means infinite retries. In # this case
gets meaningless. # : Delay between two attempts to call the beckend, to # be given in seconds and as an integer number. # Meaningless if is one. # : The original URI, which your queue had before. # # All parameters, especially,
, , and have always to # be specified, even if one of them is meaningless due to the setting of # the others. # # beh works with every backend # # Example URIs: # # beh:/1/3/5/socket://printer:9100 # # On the network printer with host name "printer" it is tried to access # 3 times with 5 second delays between the attempts. If the job still # fails, the queue is not disabled (and the job discarded). # # beh:/0/10/60/socket://printer:9100 # # Retry 10 times in one minute intervals, disable the queue when still # not succeeding. # # beh:/1/0/60/usb://Brother/HL-5040%20series # # On a Brother HL-5040 on the USB try infinitely often until the printer # comes back, in intervals of one minute. This way the job does not get # lost when the printer is turned off and one can intendedly delay # printing by simply switching off the printer. The ideal configuration # for desktop printers and/or home users. # Acknowledgement # # Thanks to Jeff Hardy (hardyjm at potsdam dot edu) for writing the # "accsnmp" wrapper backend (http://fritz.potsdam.edu/projects/cupsapps/). # This backend showed me the trick how to write a universal wrapper # backend in a scripting language. use strict; $0 =~ m!/([^/]+)\s*$!; my $progname = ($1 || $0); if (!$ARGV[0]){ print "network $progname \"Unknown\" \"Backend Error Handler\"\n"; exit 0; } if (scalar(@ARGV) < 5 || scalar(@ARGV) > 6){ print STDERR "ERROR: Usage: $progname job-id user title copies options [file]\n"; exit 1; } my ($jobID, $userName, $jobTitle, $copies, $printOptions, $printFile) = @ARGV; my $tempFile; if (!$printFile) { my $jid = $jobID; my $uid = $userName; $jid =~ s/\W//g; #sanity check $uid =~ s/\W//g; #sanity check $tempFile = "$ENV{TMPDIR}/$jid-$uid-cupsjob$$"; open (OUT, ">$tempFile") or die "ERROR: Cannot write $tempFile: $!\n"; while(){ print OUT "$_"; } close OUT; $printFile = $tempFile; } my $uri = $ENV{DEVICE_URI}; $uri =~ m!^$progname:/(\d+)/(\d+)/(\d+)/(\S+)$! or die "URI must be \"beh:/
///\"!\n"; my $dontdisable = $1; my $attempts = $2; my $delay = $3; $uri = $4; $uri =~ m!^([^:\s]+):!; my $backend = $1; $ENV{DEVICE_URI} = $uri; # Control by "lpr" command line options, commented out for security # reasons (user could intendedly make queues being disabled) #$printOptions =~ m/\bBackendErrorDisableQueue=(\S*)\b/ && # ($dontdisable = ($1 =~ /no/i ? 1 : 0)); #$printOptions =~ m/\bBackendErrorRetries=(\S*)\b/ && ($attempts = $1); #$printOptions =~ m/\bBackendErrorRetryDelay=(\S*)\b/ && ($delay = $1); #$printOptions =~ m/\bBackendErrorRetryForever=(\S*)\b/ && # ($delay = ($1 =~ /yes/i ? 0 : $delay)); my $exitvalue; while($exitvalue = (($uri !~ m!^file:(.*)$!) && ($uri !~ m!^(/.*)$!) ? system {"/usr/lib/cups/backend/$backend"} ($uri, $jobID, $userName, $jobTitle, $copies, $printOptions, $printFile) : system ("cat $printFile > $1")) >> 8) { if ($attempts > 0) { $attempts --; last if $attempts == 0; } sleep $delay if $delay > 0; } unlink $tempFile if $tempFile; $exitvalue = 0 if $dontdisable; exit $exitvalue;