plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

generic (4175B)


      1 #!/usr/local/plan9/bin/rc
      2 
      3 rfork s	
      4 
      5 if (! ~ $DEBUG '') flag x +
      6 
      7 LPDELAY=60
      8 
      9 if (! ~ $#* 2 && ! ~ $#* 3) {
     10 	echo 'usage generic local_cmd _remote_cmd [ success_cmd ]' >[1=2]
     11 	exit 'argument count'
     12 }
     13 LOCAL_CMD=$1
     14 REMOTE_CMD=$2
     15 SUCCESS_CMD=$3
     16 
     17 # we don't want to make a LOCK file unless we are at the destination
     18 # let the spooler take care of moving it there
     19 # this is necessary for clients and servers that share the
     20 # same lp queue directory
     21 if (! ~ $THIS_HOST $DEST_HOST || ! LOCK $LPSPOOL/$LPDEST/LOCK $THIS_HOST $pid) exit ''
     22 
     23 cd $LPSPOOL
     24 
     25 MAXTRY=2
     26 PRINTLOG=$LPLOGDIR/$LPDEST^.st
     27 
     28 fn schedule{
     29 	FILE=`{lpsub sched $SCHED $LPDEST}
     30 }
     31 
     32 while (schedule; ! ~ $#FILE 0) {
     33 	SCHEDLINE=`{cat $LPDEST/$FILE(1)^id}
     34 	LSLINE=`{ls -l $LPDEST/$FILE(1)}
     35 	echo `{date} start $SCHEDLINE
     36 	if (! test -s $LPDEST/.seqno) {
     37 		echo 0 > $LPDEST/.seqno
     38 	}
     39 	if not {
     40 		LPSEQNO = `{cat $LPDEST/.seqno | awk '{seqno = $1 + 1; if (seqno > 999) seqno = 0; print seqno}'}
     41 		echo $LPSEQNO > $LPDEST/.seqno
     42 	}
     43 	STARTIME=`{date}
     44 	if (! test -s $LPDEST/$FILE(1)) {
     45 		echo `{date} file $LPDEST/$FILE(1) disappeared or is empty
     46 		ls -l $LPDEST
     47 		echo $SCHEDLINE(2)$SCHEDLINE(4)'	'$SCHEDLINE(3)	$SCHEDLINE(5)' ! '$LSLINE(6) $STARTIME(4) >> $LPLOG
     48 		rm -f $LPDEST/$FILE $LPDEST/$FILE^id
     49 	}
     50 	if not {
     51 		if (~ $THIS_HOST $DEST_HOST) {
     52 			if (test -f $LPLIB/perm/$LPDEST && grep -s $SCHEDLINE(2) $LPLIB/perm/$LPDEST) {
     53 				echo `{date} permission denied
     54 				echo you are not allowed to use printer $LPDEST | mail $SCHEDLINE(2)
     55 				rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
     56 			}
     57 			if not {
     58 				echo `{date} sending to printer
     59 				> $PRINTLOG
     60 				STARTIME=`{date}
     61 				@{eval $LOCAL_CMD}
     62 				rv=$status
     63 				ENDTIME=`{date}
     64 				status=$rv
     65 				if () {
     66 					echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' + '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)
     67 					eval $SUCCESS_CMD
     68 					rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
     69 				}
     70 				if not {
     71 					echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' - '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)' status='$rv
     72 					THISTRY=`{echo $SCHEDLINE(4) + 1|hoc}
     73 					echo $SCHEDLINE(1) $SCHEDLINE(2) $SCHEDLINE(3) $THISTRY >$LPDEST/$FILE(1)^id
     74 					if (test $THISTRY -ge $MAXTRY) {
     75 						if (test -d $LPLIB/prob/$LPDEST) {
     76 							cp $LPDEST/$FILE(1) $LPLIB/prob/$LPDEST
     77 							cp $LPDEST/$FILE(1)^id $LPLIB/prob/$LPDEST
     78 							cp $PRINTLOG $LPLIB/prob/$LPDEST/$FILE(1)^log
     79 						}
     80 						upasname=daemon
     81 						mail $SCHEDLINE(2) <<endmail1
     82 						rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
     83 					}
     84 					if not {
     85 						touch $LPDEST/$FILE(1)
     86 						sleep $LPDELAY
     87 					}
     88 				}
     89 			}
     90 		}
     91 		if not {
     92 # we don't want to be here
     93 #			trap '' 13	# temp hack: ignore SIGPIPE
     94 			echo `{date} queuing - should not get here
     95 			STARTIME=`{date}
     96 			eval $REMOTE_CMD 
     97 			rv=$status
     98 			ENDTIME=`{date}
     99 			if (~ $rv '') {
    100 				echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' + '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)
    101 				rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
    102 			}
    103 			if not {
    104 				echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' - '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)
    105 				echo status $rv from '$REMOTE_CMD'
    106 				THISTRY=`{echo $SCHEDLINE(4) + 1|hoc}
    107 				echo $SCHEDLINE(1) $SCHEDLINE(2) $SCHEDLINE(3) $THISTRY >$LPDEST/$FILE(1)^id
    108 				if (~ $THISTRY $MAXTRY) {
    109 					upasname=daemon
    110 					mail $SCHEDLINE(2) <<endmail2
    111 				}
    112 				if not {
    113 					touch $LPDEST/$FILE(1)
    114 					sleep $LPDELAY
    115 
    116 				# this is here to deal with running out of
    117 				# Datakit channels or other stupid problems
    118 				# backoff connection attempts but not too long
    119 					LPDELAY=`{echo $LPDELAY '*' 2|hoc}
    120 					if (test $LPDELAY -gt 960)
    121 						LPDELAY=60
    122 				}
    123 			}
    124 		}
    125 	}
    126 }
    127 Your job has failed to print on $LPDEST after $MAXTRY attempt(s).
    128 If the directory $LPLIB/prob/$LPDEST exists on $DEST_HOST,
    129 the file has been put there along with the printer log file.
    130 			Your friendly printer daemon
    131 endmail1
    132 Your job has failed to print on $LPDEST after $MAXTRY attempt(s).
    133 There is a problem in sending the job to $DEST_HOST,
    134 but I'll keep trying.  Have a nice day.
    135 			Your enthusiastic printer daemon
    136 endmail2
    137 UNLOCK $LPSPOOL/$LPDEST
    138 rm $LPSPOOL/$LPDEST >[2]/dev/null