xref: /plan9/sys/lib/lp/daemon/generic (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
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	STARTIME=`{date}
41	if (! test -s $LPDEST/$FILE(1)) {
42		echo $SCHEDLINE(2)$SCHEDLINE(4)'	'$SCHEDLINE(3)	$SCHEDLINE(5)' ! '$LSLINE(6) $STARTIME(4) >> $LPLOG
43		rm -f $LPDEST/$FILE $LPDEST/$FILE^id
44	}
45	if not {
46		if (~ $THIS_HOST $DEST_HOST) {
47			if (test -f $LPLIB/perm/$LPDEST && grep -s $SCHEDLINE(2) $LPLIB/perm/$LPDEST) {
48				echo you are not allowed to use printer $LPDEST | mail $SCHEDLINE(2)
49				rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
50			}
51			if not {
52				> $PRINTLOG
53				STARTIME=`{date}
54				eval $LOCAL_CMD
55				rv=$status
56				ENDTIME=`{date}
57				status=$rv
58				if () {
59					echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' + '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)
60					eval $SUCCESS_CMD
61					rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
62				}
63				if not {
64					echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' - '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)' status='$rv
65					THISTRY=`{echo $SCHEDLINE(4) + 1|hoc}
66					echo $SCHEDLINE(1) $SCHEDLINE(2) $SCHEDLINE(3) $THISTRY >$LPDEST/$FILE(1)^id
67					if (test $THISTRY -ge $MAXTRY) {
68						if (test -d $LPLIB/prob/$LPDEST) {
69							cp $LPDEST/$FILE(1) $LPLIB/prob/$LPDEST
70							cp $LPDEST/$FILE(1)^id $LPLIB/prob/$LPDEST
71							cp $PRINTLOG $LPLIB/prob/$LPDEST/$FILE(1)^log
72						}
73						upasname=daemon
74						mail $SCHEDLINE(2) <<endmail1
75						rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
76					}
77					if not {
78						touch $LPDEST/$FILE(1)
79						sleep $LPDELAY
80					}
81				}
82			}
83		}
84		if not {
85# we don't want to be here
86#			trap '' 13	# temp hack: ignore SIGPIPE
87			STARTIME=`{date}
88			eval $REMOTE_CMD 
89			rv=$status
90			ENDTIME=`{date}
91			if (~ $rv '') {
92				echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' + '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)
93				rm -f $LPDEST/$FILE(1) $LPDEST/$FILE(1)^id
94			}
95			if not {
96				echo $SCHEDLINE(1)^$SCHEDLINE(3)'	'$SCHEDLINE(2)'	'$SCHEDLINE(4)' - '$LSLINE(6)' '$STARTIME(4)' '$ENDTIME(4)
97				echo status $rv from '$REMOTE_CMD'
98				THISTRY=`{echo $SCHEDLINE(4) + 1|hoc}
99				echo $SCHEDLINE(1) $SCHEDLINE(2) $SCHEDLINE(3) $THISTRY >$LPDEST/$FILE(1)^id
100				if (~ $THISTRY $MAXTRY) {
101					upasname=daemon
102					mail $SCHEDLINE(2) <<endmail2
103				}
104				if not {
105					touch $LPDEST/$FILE(1)
106					sleep $LPDELAY
107
108				# this is here to deal with running out of
109				# Datakit channels or other stupid problems
110				# backoff connection attempts but not too long
111					LPDELAY=`{echo $LPDELAY '*' 2|hoc}
112					if (test $LPDELAY -gt 960)
113						LPDELAY=60
114				}
115			}
116		}
117	}
118}
119Your job has failed to print on $LPDEST after $MAXTRY attempt(s).
120If the directory $LPLIB/prob/$LPDEST exists on $DEST_HOST,
121the file has been put there along with the printer log file.
122			Your friendly printer daemon
123endmail1
124Your job has failed to print on $LPDEST after $MAXTRY attempt(s).
125There is a problem in sending the job to $DEST_HOST,
126but I'll keep trying.  Have a nice day.
127			Your enthusiastic printer daemon
128endmail2
129UNLOCK $LPSPOOL/$LPDEST
130rm $LPSPOOL/$LPDEST >[2]/dev/null
131