7 #define RDNETIMEOUT 60000
8 #define WRNETIMEOUT 60000
21 #define fprint fprintf
22 #define sprint sprintf
26 #define OTRUNC O_TRUNC
29 #define RDNETIMEOUT 60
30 #define WRNETIMEOUT 60
34 #define MIN(a,b) ((a<b)?a:b)
36 #define ACK(a) write(a, "", 1)
37 #define NAK(a) write(a, "\001", 1)
39 #define LPDAEMONLOG "/tmp/lpdaemonl"
44 char *dbgstrings[] = {
56 error(int level, char *s1, ...)
66 chartime = ctime(thetime);
67 fprint(stderr, "%.15s ", &(chartime[4]));
70 while(args[argno++] = va_arg(ap, char*));
72 fprint(stderr, s1, *args);
77 alarmhandler(void *foo, char *note) {
79 if(strcmp(note, "alarm")==0) {
80 fprint(stderr, "alarm at %d - %s\n", dbgstate, dbgstrings[dbgstate]);
88 error(int level, char *s1, ...)
95 chartime = ctime(&thetime);
96 fprintf(stderr, "%.15s ", &(chartime[4]));
98 fprintf(stderr, s1, (&s1+1));
104 fprintf(stderr, "alarm at %d - %s\n", dbgstate, dbgstrings[dbgstate]);
109 /* get a line from inpfd using nonbuffered input. The line is truncated if it is too
110 * long for the buffer. The result is left in lnbuf and the number of characters
111 * read in is returned.
122 if (read(inpfd, ap, 1) != 1) {
123 error(0, "read error in readline, fd=%d\n", inpfd);
126 } while ((++i < LNBFSZ - 2) && *ap++ != '\n');
127 if (i == LNBFSZ - 2) {
139 pass(int inpfd, int outfd, int bsize)
144 for(bcnt=bsize; bcnt > 0; bcnt -= rv) {
145 alarm(WRNETIMEOUT); /* to break hanging */
146 if((rv=read(inpfd, jobbuf, MIN(bcnt,RDSIZE))) < 0) {
147 error(0, "read error during pass, %d remaining\n", bcnt);
149 } else if((write(outfd, jobbuf, rv)) != rv) {
150 error(0, "write error during pass, %d remaining\n", bcnt);
158 /* get whatever stdin has and put it into the temporary file.
159 * return the file size.
162 prereadfile(int inpfd)
168 if((rv=read(0, jobbuf, RDSIZE))<0) {
169 error(0, "read error while making temp file\n");
170 exits("read error while making temp file");
171 } else if((write(inpfd, jobbuf, rv)) != rv) {
172 error(0, "write error while making temp file\n");
173 exits("write error while making temp file");
183 static int tindx = 0;
187 sprint(tmpf, "/var/tmp/lp%d.%d", getpid(), tindx++);
188 if((tmpfd=create(tmpf,
197 error(0, "cannot create temp file %s\n", tmpf);
198 exits("cannot create temp file");
201 if((tmpfd=open(tmpf, ORDWR
210 error(0, "cannot open temp file %s\n", tmpf);
211 exits("cannot open temp file");
223 if (read(netfd, jobbuf, 1)!=1 || *jobbuf!='\0') {
224 error(0, "failed to receive ACK, ");
226 error(1, "read failed\n");
228 error(1, "received <0x%x> instead\n", *jobbuf);
236 main(int argc, char *argv[])
239 int i, rv, netfd, bsize;
244 void (*oldhandler)();
249 /* make connection */
251 fprint(stderr, "usage: %s network!destination!service\n", argv[0]);
252 exits("incorrect number of arguments");
255 /* read options line from stdin into lnbuf */
258 /* read stdin into tempfile to get size */
260 bsize = prereadfile(datafd);
262 /* network connection is opened after data is in to avoid timeout */
263 if ((netfd=dial(argv[1], 0, 0, 0)) < 0) {
264 fprint(stderr, "dialing %s\n", devdir);
269 /* write out the options we read above */
270 if (write(netfd, lnbuf, i) != i) {
271 error(0, "write error while sending options\n");
272 exits("write error while sending options");
275 /* send the size of the file to be sent */
276 sprint(lnbuf, "%d\n", bsize);
278 if ((rv=write(netfd, lnbuf, i)) != i) {
279 perror("write error while sending size");
280 error(0, "write returned %d\n", rv);
281 exits("write error while sending size");
284 if (seek(datafd, 0L, 0) < 0) {
285 error(0, "error seeking temp file\n");
288 /* mirror performance in readfile() in lpdaemon */
292 atnotify(alarmhandler, 1);
296 oldhandler = signal(SIGALRM, alarmhandler);
301 if(!recvACK(netfd)) {
302 error(0, "failed to receive ACK before sending data\n");
303 exits("recv ack1 failed");
306 if ((i=pass(datafd, netfd, bsize)) != 0) {
308 error(0, "failed to send %d bytes\n", i);
309 exits("send data failed");
313 if(!recvACK(netfd)) {
314 error(0, "failed to receive ACK after sending data\n");
315 exits("recv ack2 failed");
318 /* get response, as from lp -q */
320 while((rv=read(netfd, jobbuf, RDSIZE)) > 0) {
321 if((write(1, jobbuf, rv)) != rv) {
322 error(0, "write error while sending to stdout\n");
323 exits("write error while sending to stdout");
330 atnotify(alarmhandler, 0);
331 /* close down network connections and go away */
336 signal(SIGALRM, oldhandler);