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