xref: /onnv-gate/usr/src/cmd/lp/model/netpr/net.c (revision 6854:3022d4fc4450)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6854Skeerthi  * Common Development and Distribution License (the "License").
6*6854Skeerthi  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*6854Skeerthi  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate #include <signal.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <fcntl.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <strings.h>
390Sstevel@tonic-gate #include <syslog.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/socket.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #include "netpr.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	TIMEOUT		1
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static int netpr_send_message(int, char *, ...);
490Sstevel@tonic-gate static int xfer_cfAfile(int, char *, char *, uint);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int
bsd_print(int sockfd,caddr_t pa,np_bsdjob_t * bsdjob)520Sstevel@tonic-gate bsd_print(int sockfd, caddr_t pa, np_bsdjob_t * bsdjob)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	int filesize;
550Sstevel@tonic-gate 	int xfer;
560Sstevel@tonic-gate 	int net;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	syslog(LOG_DEBUG, "bsd_print");
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	filesize = bsdjob->np_data->np_data_size;
610Sstevel@tonic-gate 	syslog(LOG_DEBUG, "filesize is %d", filesize);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (netpr_send_message(sockfd, "%c%s\n", XFER_REQUEST,
650Sstevel@tonic-gate 		bsdjob->np_printer) != 0) {
660Sstevel@tonic-gate 		return (NETWORK_ERROR_SEND_RESPONSE);
670Sstevel@tonic-gate 	}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	/*
700Sstevel@tonic-gate 	 * control file
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (bsdjob->np_print_order == CONTROL_FIRST) {
740Sstevel@tonic-gate 		if ((xfer_cfAfile(sockfd, bsdjob->np_cfAfile,
750Sstevel@tonic-gate 		    bsdjob->np_cfAfilename,
760Sstevel@tonic-gate 		    bsdjob->np_cfAfilesize)) != 0) {
770Sstevel@tonic-gate 			(void) fprintf(stderr,
780Sstevel@tonic-gate 			    gettext("Netpr: Error sending control file\n"));
790Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Error sending control file");
800Sstevel@tonic-gate 			    return (NETWORK_ERROR_UNKNOWN);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	/* send msg - get ready for transfer */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if ((netpr_send_message(sockfd, "%c%d %s\n", XFER_DATA, filesize,
880Sstevel@tonic-gate 	    bsdjob->np_data->np_dfAfilename)) != 0) {
890Sstevel@tonic-gate 		return (NETWORK_ERROR_SEND_RESPONSE);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * send the file
940Sstevel@tonic-gate 	 */
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if ((xfer = xfer_file(sockfd, pa, filesize, bsdjob->np_timeout)) != 0) {
970Sstevel@tonic-gate 		return (xfer);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/* send msg - done */
1010Sstevel@tonic-gate 	if ((net = netpr_send_message(sockfd, "", NULL)) != 0) {
1020Sstevel@tonic-gate 		(void) fprintf(stderr,
1030Sstevel@tonic-gate 		gettext("Netpr: network error transfering %s returns: %d\n"),
1040Sstevel@tonic-gate 			bsdjob->np_filename, net);
1050Sstevel@tonic-gate 		syslog(LOG_DEBUG,
1060Sstevel@tonic-gate 			"network error transfering %s returns: %d",
1070Sstevel@tonic-gate 			bsdjob->np_filename, net);
1080Sstevel@tonic-gate 		return (NETWORK_ERROR_WRITE_FAILED);
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * control file
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (bsdjob->np_print_order == DATA_FIRST) {
1160Sstevel@tonic-gate 		if ((xfer_cfAfile(sockfd, bsdjob->np_cfAfile,
1170Sstevel@tonic-gate 		    bsdjob->np_cfAfilename,
1180Sstevel@tonic-gate 		    bsdjob->np_cfAfilesize)) != 0) {
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 			(void) fprintf(stderr,
1210Sstevel@tonic-gate 			    gettext("Netpr: Error sending control file\n"));
1220Sstevel@tonic-gate 			    syslog(LOG_DEBUG, "Error sending control file");
1230Sstevel@tonic-gate 			    return (NETWORK_ERROR_UNKNOWN);
1240Sstevel@tonic-gate 		}
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (0);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate int
xfer_file(int sockfd,caddr_t pa,int filesize,int seed)1310Sstevel@tonic-gate xfer_file(int sockfd, caddr_t pa, int filesize, int seed)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int ctr;
1340Sstevel@tonic-gate 	int timeout;
1350Sstevel@tonic-gate 	int nw;
1360Sstevel@tonic-gate 	int error_msg = 0;
1370Sstevel@tonic-gate 	int pause = 0;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file");
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/* send file */
1420Sstevel@tonic-gate 	ctr = filesize;
1430Sstevel@tonic-gate 	timeout = seed = seed ? seed : 10;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	while (ctr > 0) {
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file: write while loop => ctr = %d", ctr);
1480Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file: timeout = %d", timeout);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
151*6854Skeerthi 		(void) alarm(10);
1520Sstevel@tonic-gate 		nw = write(sockfd, pa, ctr);
1530Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file: write while loop => nw = %d", nw);
1540Sstevel@tonic-gate 		(void) alarm(0);
1550Sstevel@tonic-gate 		if ((nw == 0) || (nw < 0)) {
1560Sstevel@tonic-gate 			if (timeout < (seed * 4)) {
1570Sstevel@tonic-gate 				(void) sleep(timeout);
1580Sstevel@tonic-gate 				timeout *= 2;
1590Sstevel@tonic-gate 			} else if (timeout == (seed * 4)) {
1600Sstevel@tonic-gate 				(void) sleep(timeout);
1610Sstevel@tonic-gate 				timeout *= 2;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 				/*
1640Sstevel@tonic-gate 				 * Send message to user once
1650Sstevel@tonic-gate 				 */
1660Sstevel@tonic-gate 				if (error_msg == 0) {
1670Sstevel@tonic-gate 					error_msg++;
1680Sstevel@tonic-gate 					tell_lptell(ERRORMSG,
1690Sstevel@tonic-gate 					gettext("Printer not accepting input;"
1700Sstevel@tonic-gate 					"possibly offline or out of paper."));
1710Sstevel@tonic-gate 				}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 			} else if (timeout > (seed * 4)) {
1740Sstevel@tonic-gate 				(void) sleep(timeout);
1750Sstevel@tonic-gate 				if (pause++ > 3)
1760Sstevel@tonic-gate 					timeout = (seed * 10);
1770Sstevel@tonic-gate 			}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		} else {
1800Sstevel@tonic-gate 			ctr -= nw;
1810Sstevel@tonic-gate 			pa += nw;
1820Sstevel@tonic-gate 			if (error_msg) {
1830Sstevel@tonic-gate 				tell_lptell(OKMSG, "Current");
1840Sstevel@tonic-gate 				error_msg = 0;
1850Sstevel@tonic-gate 				pause = 0;
1860Sstevel@tonic-gate 			}
1870Sstevel@tonic-gate 			timeout = seed;
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (E_SUCCESS);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
xfer_cfAfile(int sockfd,char * cfAfile,char * cfAname,uint size)1950Sstevel@tonic-gate xfer_cfAfile(int sockfd, char * cfAfile, char * cfAname, uint size)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	int ctr;
1980Sstevel@tonic-gate 	caddr_t pa;
1990Sstevel@tonic-gate 	int nw = 0;
2000Sstevel@tonic-gate 	int timeout;
2010Sstevel@tonic-gate 	int printererr;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_cfAfile");
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if ((netpr_send_message(sockfd, "%c%d %s\n", XFER_CONTROL,
2060Sstevel@tonic-gate 		size, cfAname)) != 0) {
2070Sstevel@tonic-gate 		return (NETWORK_ERROR_MSG_FAILED);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* send the control file */
2110Sstevel@tonic-gate 	pa = cfAfile;
2120Sstevel@tonic-gate 	ctr = size;
2130Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_cfAfile : cfAfile %s", pa);
2140Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_cfAfile : size %d", size);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/* send control file */
2170Sstevel@tonic-gate 	timeout = TIMEOUT;
2180Sstevel@tonic-gate 	printererr = 0;
2190Sstevel@tonic-gate 	while (ctr > 0) {
2200Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
2210Sstevel@tonic-gate 		(void) alarm(2);
2220Sstevel@tonic-gate 		nw = write(sockfd, pa, size);
2230Sstevel@tonic-gate 		(void) alarm(0);
2240Sstevel@tonic-gate 		if (nw <= 0) {
2250Sstevel@tonic-gate 			if (timeout < 16) {
2260Sstevel@tonic-gate 				(void) sleep(timeout);
2270Sstevel@tonic-gate 				timeout *= 2;
2280Sstevel@tonic-gate 			} else if (timeout == 16) {
2290Sstevel@tonic-gate 			/* talk with the printer and see what's happening */
2300Sstevel@tonic-gate 				/* send message back to caller */
2310Sstevel@tonic-gate 				(void) sleep(timeout);
2320Sstevel@tonic-gate 				timeout *= 2;
2330Sstevel@tonic-gate 				printererr = 1;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 				tell_lptell(ERRORMSG,
2360Sstevel@tonic-gate 				gettext("Printer not accepting input;"
2370Sstevel@tonic-gate 				"possibly offline or out of paper."));
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 			} else if (timeout > 16) {
2400Sstevel@tonic-gate 				(void) sleep(timeout);
2410Sstevel@tonic-gate 			}
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 		ctr -= nw;
2440Sstevel@tonic-gate 		pa += nw;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (printererr == 1) {
2480Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Printer status ok\n"));
2490Sstevel@tonic-gate 		tell_lptell(OKMSG, "Current");
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* send msg - done */
2540Sstevel@tonic-gate 	if (netpr_send_message(sockfd, "", NULL) != 0) {
2550Sstevel@tonic-gate 		return (NETWORK_ERROR_MSG_FAILED);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	return (0);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  *  netpr_response() reads in a byte from the network printer
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate static int
netpr_response(int nd)2650Sstevel@tonic-gate netpr_response(int nd)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	char    c;
2680Sstevel@tonic-gate 	int msg_given = 0;
2690Sstevel@tonic-gate 	int firstloop = 0;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_response");
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	(void) signal(SIGALRM, null_sighandler);
2740Sstevel@tonic-gate 	(void) alarm(2);
2750Sstevel@tonic-gate 	while (1) {
2760Sstevel@tonic-gate 		errno = 0;
2770Sstevel@tonic-gate 		if ((read(nd, &c, 1) != 1)) {
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 			if (firstloop == 0) {
2800Sstevel@tonic-gate 				(void) alarm(0);
2810Sstevel@tonic-gate 				firstloop++;
2820Sstevel@tonic-gate 			}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 			if (errno == EINTR) {
2850Sstevel@tonic-gate 				if (msg_given == 0) {
2860Sstevel@tonic-gate 				    tell_lptell(ERRORMSG,
2870Sstevel@tonic-gate 				    gettext("Printer not responding;"
2880Sstevel@tonic-gate 				    "Either warming up or needs attention"));
2890Sstevel@tonic-gate 				    msg_given++;
2900Sstevel@tonic-gate 				    syslog(LOG_DEBUG,
2910Sstevel@tonic-gate 					"read hanging in netpr_response: %m");
2920Sstevel@tonic-gate 				}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 			} else {
2950Sstevel@tonic-gate 				syslog(LOG_DEBUG,
2960Sstevel@tonic-gate 					"read in netpr_response failed: %m");
2970Sstevel@tonic-gate 				return (NETWORK_READ_RESPONSE_FAILED);
2980Sstevel@tonic-gate 			}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		} else {
3010Sstevel@tonic-gate 			if (c) {
3020Sstevel@tonic-gate 				syslog(LOG_DEBUG,
3030Sstevel@tonic-gate 					"Printer returned error: %m");
3040Sstevel@tonic-gate 				return (NETWORK_PRINTER_REFUSED_CONN);
3050Sstevel@tonic-gate 			} else {
3060Sstevel@tonic-gate 				if (msg_given)
3070Sstevel@tonic-gate 					tell_lptell(OKMSG, "Current");
3080Sstevel@tonic-gate 				return (0);
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int
netpr_send_message(int nd,char * fmt,...)3160Sstevel@tonic-gate netpr_send_message(int nd, char *fmt, ...)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	char    buf[BUFSIZ];
3190Sstevel@tonic-gate 	int ctr;
3200Sstevel@tonic-gate 	char * pa;
3210Sstevel@tonic-gate 	va_list ap;
3220Sstevel@tonic-gate 	int timeout = 1;
3230Sstevel@tonic-gate 	int nw;
3240Sstevel@tonic-gate 	int err_msg = 0;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_send_message");
3270Sstevel@tonic-gate 	va_start(ap, fmt);
3280Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
3290Sstevel@tonic-gate 	va_end(ap);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	pa = buf;
3320Sstevel@tonic-gate 	ctr = (strlen(buf) != 0) ? strlen(buf) : 1;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_send_message : ctr = %d", ctr);
3350Sstevel@tonic-gate 	while (ctr > 0) {
3360Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
3370Sstevel@tonic-gate 		(void) alarm(2);
3380Sstevel@tonic-gate 		nw = write(nd, pa, ctr);
3390Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_send_message : nw = %d", nw);
3400Sstevel@tonic-gate 		(void) alarm(0);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		if (nw <= 0) {
3430Sstevel@tonic-gate 			if (timeout < 16) {
3440Sstevel@tonic-gate 				(void) sleep(timeout);
3450Sstevel@tonic-gate 				timeout *= 2;
3460Sstevel@tonic-gate 			} else if (timeout == 16) {
3470Sstevel@tonic-gate 				(void) sleep(timeout);
3480Sstevel@tonic-gate 				timeout *= 2;
3490Sstevel@tonic-gate 				if (err_msg == 0) {
3500Sstevel@tonic-gate 					err_msg++;
3510Sstevel@tonic-gate 					tell_lptell(ERRORMSG,
3520Sstevel@tonic-gate 					gettext("Printer not accepting input;"
3530Sstevel@tonic-gate 					"possibly offline or out of paper."));
3540Sstevel@tonic-gate 				}
3550Sstevel@tonic-gate 			} else
3560Sstevel@tonic-gate 				(void) sleep(timeout);
3570Sstevel@tonic-gate 		} else {
3580Sstevel@tonic-gate 			ctr -= nw;
3590Sstevel@tonic-gate 			pa += nw;
3600Sstevel@tonic-gate 			if (err_msg)
3610Sstevel@tonic-gate 				tell_lptell(OKMSG, "Current");
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	return (netpr_response(nd));
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  *  null() is to be used as a signal handler that does nothing.  It is used in
3700Sstevel@tonic-gate  *      place of SIG_IGN, because we want the signal to be delivered and
3710Sstevel@tonic-gate  *      interupt the current system call.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate /*ARGSUSED*/
3740Sstevel@tonic-gate void
null_sighandler(int i)3750Sstevel@tonic-gate null_sighandler(int i)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate }
378