1
0

generic 3.9 KB

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