xref: /illumos-gate/usr/src/cmd/lp/model/netpr/net.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*23310c00Skeerthi  * Common Development and Distribution License (the "License").
6*23310c00Skeerthi  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*23310c00Skeerthi  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <sys/file.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include "netpr.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	TIMEOUT		1
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int netpr_send_message(int, char *, ...);
477c478bd9Sstevel@tonic-gate static int xfer_cfAfile(int, char *, char *, uint);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int
bsd_print(int sockfd,caddr_t pa,np_bsdjob_t * bsdjob)507c478bd9Sstevel@tonic-gate bsd_print(int sockfd, caddr_t pa, np_bsdjob_t * bsdjob)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	int filesize;
537c478bd9Sstevel@tonic-gate 	int xfer;
547c478bd9Sstevel@tonic-gate 	int net;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "bsd_print");
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	filesize = bsdjob->np_data->np_data_size;
597c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "filesize is %d", filesize);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (netpr_send_message(sockfd, "%c%s\n", XFER_REQUEST,
637c478bd9Sstevel@tonic-gate 		bsdjob->np_printer) != 0) {
647c478bd9Sstevel@tonic-gate 		return (NETWORK_ERROR_SEND_RESPONSE);
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/*
687c478bd9Sstevel@tonic-gate 	 * control file
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (bsdjob->np_print_order == CONTROL_FIRST) {
727c478bd9Sstevel@tonic-gate 		if ((xfer_cfAfile(sockfd, bsdjob->np_cfAfile,
737c478bd9Sstevel@tonic-gate 		    bsdjob->np_cfAfilename,
747c478bd9Sstevel@tonic-gate 		    bsdjob->np_cfAfilesize)) != 0) {
757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
767c478bd9Sstevel@tonic-gate 			    gettext("Netpr: Error sending control file\n"));
777c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Error sending control file");
787c478bd9Sstevel@tonic-gate 			    return (NETWORK_ERROR_UNKNOWN);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	/* send msg - get ready for transfer */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if ((netpr_send_message(sockfd, "%c%d %s\n", XFER_DATA, filesize,
867c478bd9Sstevel@tonic-gate 	    bsdjob->np_data->np_dfAfilename)) != 0) {
877c478bd9Sstevel@tonic-gate 		return (NETWORK_ERROR_SEND_RESPONSE);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * send the file
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if ((xfer = xfer_file(sockfd, pa, filesize, bsdjob->np_timeout)) != 0) {
957c478bd9Sstevel@tonic-gate 		return (xfer);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* send msg - done */
997c478bd9Sstevel@tonic-gate 	if ((net = netpr_send_message(sockfd, "", NULL)) != 0) {
1007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1017c478bd9Sstevel@tonic-gate 		gettext("Netpr: network error transfering %s returns: %d\n"),
1027c478bd9Sstevel@tonic-gate 			bsdjob->np_filename, net);
1037c478bd9Sstevel@tonic-gate 		syslog(LOG_DEBUG,
1047c478bd9Sstevel@tonic-gate 			"network error transfering %s returns: %d",
1057c478bd9Sstevel@tonic-gate 			bsdjob->np_filename, net);
1067c478bd9Sstevel@tonic-gate 		return (NETWORK_ERROR_WRITE_FAILED);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * control file
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (bsdjob->np_print_order == DATA_FIRST) {
1147c478bd9Sstevel@tonic-gate 		if ((xfer_cfAfile(sockfd, bsdjob->np_cfAfile,
1157c478bd9Sstevel@tonic-gate 		    bsdjob->np_cfAfilename,
1167c478bd9Sstevel@tonic-gate 		    bsdjob->np_cfAfilesize)) != 0) {
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1197c478bd9Sstevel@tonic-gate 			    gettext("Netpr: Error sending control file\n"));
1207c478bd9Sstevel@tonic-gate 			    syslog(LOG_DEBUG, "Error sending control file");
1217c478bd9Sstevel@tonic-gate 			    return (NETWORK_ERROR_UNKNOWN);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	return (0);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate int
xfer_file(int sockfd,caddr_t pa,int filesize,int seed)1297c478bd9Sstevel@tonic-gate xfer_file(int sockfd, caddr_t pa, int filesize, int seed)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int ctr;
1327c478bd9Sstevel@tonic-gate 	int timeout;
1337c478bd9Sstevel@tonic-gate 	int nw;
1347c478bd9Sstevel@tonic-gate 	int error_msg = 0;
1357c478bd9Sstevel@tonic-gate 	int pause = 0;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file");
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* send file */
1407c478bd9Sstevel@tonic-gate 	ctr = filesize;
1417c478bd9Sstevel@tonic-gate 	timeout = seed = seed ? seed : 10;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	while (ctr > 0) {
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file: write while loop => ctr = %d", ctr);
1467c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file: timeout = %d", timeout);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
149*23310c00Skeerthi 		(void) alarm(10);
1507c478bd9Sstevel@tonic-gate 		nw = write(sockfd, pa, ctr);
1517c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_file: write while loop => nw = %d", nw);
1527c478bd9Sstevel@tonic-gate 		(void) alarm(0);
1537c478bd9Sstevel@tonic-gate 		if ((nw == 0) || (nw < 0)) {
1547c478bd9Sstevel@tonic-gate 			if (timeout < (seed * 4)) {
1557c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
1567c478bd9Sstevel@tonic-gate 				timeout *= 2;
1577c478bd9Sstevel@tonic-gate 			} else if (timeout == (seed * 4)) {
1587c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
1597c478bd9Sstevel@tonic-gate 				timeout *= 2;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 				/*
1627c478bd9Sstevel@tonic-gate 				 * Send message to user once
1637c478bd9Sstevel@tonic-gate 				 */
1647c478bd9Sstevel@tonic-gate 				if (error_msg == 0) {
1657c478bd9Sstevel@tonic-gate 					error_msg++;
1667c478bd9Sstevel@tonic-gate 					tell_lptell(ERRORMSG,
1677c478bd9Sstevel@tonic-gate 					gettext("Printer not accepting input;"
1687c478bd9Sstevel@tonic-gate 					"possibly offline or out of paper."));
1697c478bd9Sstevel@tonic-gate 				}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			} else if (timeout > (seed * 4)) {
1727c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
1737c478bd9Sstevel@tonic-gate 				if (pause++ > 3)
1747c478bd9Sstevel@tonic-gate 					timeout = (seed * 10);
1757c478bd9Sstevel@tonic-gate 			}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		} else {
1787c478bd9Sstevel@tonic-gate 			ctr -= nw;
1797c478bd9Sstevel@tonic-gate 			pa += nw;
1807c478bd9Sstevel@tonic-gate 			if (error_msg) {
1817c478bd9Sstevel@tonic-gate 				tell_lptell(OKMSG, "Current");
1827c478bd9Sstevel@tonic-gate 				error_msg = 0;
1837c478bd9Sstevel@tonic-gate 				pause = 0;
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 			timeout = seed;
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (E_SUCCESS);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static int
xfer_cfAfile(int sockfd,char * cfAfile,char * cfAname,uint size)1937c478bd9Sstevel@tonic-gate xfer_cfAfile(int sockfd, char * cfAfile, char * cfAname, uint size)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int ctr;
1967c478bd9Sstevel@tonic-gate 	caddr_t pa;
1977c478bd9Sstevel@tonic-gate 	int nw = 0;
1987c478bd9Sstevel@tonic-gate 	int timeout;
1997c478bd9Sstevel@tonic-gate 	int printererr;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_cfAfile");
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if ((netpr_send_message(sockfd, "%c%d %s\n", XFER_CONTROL,
2047c478bd9Sstevel@tonic-gate 		size, cfAname)) != 0) {
2057c478bd9Sstevel@tonic-gate 		return (NETWORK_ERROR_MSG_FAILED);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* send the control file */
2097c478bd9Sstevel@tonic-gate 	pa = cfAfile;
2107c478bd9Sstevel@tonic-gate 	ctr = size;
2117c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_cfAfile : cfAfile %s", pa);
2127c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "xfer_cfAfile : size %d", size);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/* send control file */
2157c478bd9Sstevel@tonic-gate 	timeout = TIMEOUT;
2167c478bd9Sstevel@tonic-gate 	printererr = 0;
2177c478bd9Sstevel@tonic-gate 	while (ctr > 0) {
2187c478bd9Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
2197c478bd9Sstevel@tonic-gate 		(void) alarm(2);
2207c478bd9Sstevel@tonic-gate 		nw = write(sockfd, pa, size);
2217c478bd9Sstevel@tonic-gate 		(void) alarm(0);
2227c478bd9Sstevel@tonic-gate 		if (nw <= 0) {
2237c478bd9Sstevel@tonic-gate 			if (timeout < 16) {
2247c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
2257c478bd9Sstevel@tonic-gate 				timeout *= 2;
2267c478bd9Sstevel@tonic-gate 			} else if (timeout == 16) {
2277c478bd9Sstevel@tonic-gate 			/* talk with the printer and see what's happening */
2287c478bd9Sstevel@tonic-gate 				/* send message back to caller */
2297c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
2307c478bd9Sstevel@tonic-gate 				timeout *= 2;
2317c478bd9Sstevel@tonic-gate 				printererr = 1;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 				tell_lptell(ERRORMSG,
2347c478bd9Sstevel@tonic-gate 				gettext("Printer not accepting input;"
2357c478bd9Sstevel@tonic-gate 				"possibly offline or out of paper."));
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 			} else if (timeout > 16) {
2387c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
2397c478bd9Sstevel@tonic-gate 			}
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 		ctr -= nw;
2427c478bd9Sstevel@tonic-gate 		pa += nw;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (printererr == 1) {
2467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Printer status ok\n"));
2477c478bd9Sstevel@tonic-gate 		tell_lptell(OKMSG, "Current");
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* send msg - done */
2527c478bd9Sstevel@tonic-gate 	if (netpr_send_message(sockfd, "", NULL) != 0) {
2537c478bd9Sstevel@tonic-gate 		return (NETWORK_ERROR_MSG_FAILED);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  *  netpr_response() reads in a byte from the network printer
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static int
netpr_response(int nd)2637c478bd9Sstevel@tonic-gate netpr_response(int nd)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	char    c;
2667c478bd9Sstevel@tonic-gate 	int msg_given = 0;
2677c478bd9Sstevel@tonic-gate 	int firstloop = 0;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_response");
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	(void) signal(SIGALRM, null_sighandler);
2727c478bd9Sstevel@tonic-gate 	(void) alarm(2);
2737c478bd9Sstevel@tonic-gate 	while (1) {
2747c478bd9Sstevel@tonic-gate 		errno = 0;
2757c478bd9Sstevel@tonic-gate 		if ((read(nd, &c, 1) != 1)) {
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			if (firstloop == 0) {
2787c478bd9Sstevel@tonic-gate 				(void) alarm(0);
2797c478bd9Sstevel@tonic-gate 				firstloop++;
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 			if (errno == EINTR) {
2837c478bd9Sstevel@tonic-gate 				if (msg_given == 0) {
2847c478bd9Sstevel@tonic-gate 				    tell_lptell(ERRORMSG,
2857c478bd9Sstevel@tonic-gate 				    gettext("Printer not responding;"
2867c478bd9Sstevel@tonic-gate 				    "Either warming up or needs attention"));
2877c478bd9Sstevel@tonic-gate 				    msg_given++;
2887c478bd9Sstevel@tonic-gate 				    syslog(LOG_DEBUG,
2897c478bd9Sstevel@tonic-gate 					"read hanging in netpr_response: %m");
2907c478bd9Sstevel@tonic-gate 				}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 			} else {
2937c478bd9Sstevel@tonic-gate 				syslog(LOG_DEBUG,
2947c478bd9Sstevel@tonic-gate 					"read in netpr_response failed: %m");
2957c478bd9Sstevel@tonic-gate 				return (NETWORK_READ_RESPONSE_FAILED);
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		} else {
2997c478bd9Sstevel@tonic-gate 			if (c) {
3007c478bd9Sstevel@tonic-gate 				syslog(LOG_DEBUG,
3017c478bd9Sstevel@tonic-gate 					"Printer returned error: %m");
3027c478bd9Sstevel@tonic-gate 				return (NETWORK_PRINTER_REFUSED_CONN);
3037c478bd9Sstevel@tonic-gate 			} else {
3047c478bd9Sstevel@tonic-gate 				if (msg_given)
3057c478bd9Sstevel@tonic-gate 					tell_lptell(OKMSG, "Current");
3067c478bd9Sstevel@tonic-gate 				return (0);
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate static int
netpr_send_message(int nd,char * fmt,...)3147c478bd9Sstevel@tonic-gate netpr_send_message(int nd, char *fmt, ...)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	char    buf[BUFSIZ];
3177c478bd9Sstevel@tonic-gate 	int ctr;
3187c478bd9Sstevel@tonic-gate 	char * pa;
3197c478bd9Sstevel@tonic-gate 	va_list ap;
3207c478bd9Sstevel@tonic-gate 	int timeout = 1;
3217c478bd9Sstevel@tonic-gate 	int nw;
3227c478bd9Sstevel@tonic-gate 	int err_msg = 0;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_send_message");
3257c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
3267c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
3277c478bd9Sstevel@tonic-gate 	va_end(ap);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	pa = buf;
3307c478bd9Sstevel@tonic-gate 	ctr = (strlen(buf) != 0) ? strlen(buf) : 1;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_send_message : ctr = %d", ctr);
3337c478bd9Sstevel@tonic-gate 	while (ctr > 0) {
3347c478bd9Sstevel@tonic-gate 		(void) signal(SIGALRM, null_sighandler);
3357c478bd9Sstevel@tonic-gate 		(void) alarm(2);
3367c478bd9Sstevel@tonic-gate 		nw = write(nd, pa, ctr);
3377c478bd9Sstevel@tonic-gate 	syslog(LOG_DEBUG, "netpr_send_message : nw = %d", nw);
3387c478bd9Sstevel@tonic-gate 		(void) alarm(0);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		if (nw <= 0) {
3417c478bd9Sstevel@tonic-gate 			if (timeout < 16) {
3427c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
3437c478bd9Sstevel@tonic-gate 				timeout *= 2;
3447c478bd9Sstevel@tonic-gate 			} else if (timeout == 16) {
3457c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
3467c478bd9Sstevel@tonic-gate 				timeout *= 2;
3477c478bd9Sstevel@tonic-gate 				if (err_msg == 0) {
3487c478bd9Sstevel@tonic-gate 					err_msg++;
3497c478bd9Sstevel@tonic-gate 					tell_lptell(ERRORMSG,
3507c478bd9Sstevel@tonic-gate 					gettext("Printer not accepting input;"
3517c478bd9Sstevel@tonic-gate 					"possibly offline or out of paper."));
3527c478bd9Sstevel@tonic-gate 				}
3537c478bd9Sstevel@tonic-gate 			} else
3547c478bd9Sstevel@tonic-gate 				(void) sleep(timeout);
3557c478bd9Sstevel@tonic-gate 		} else {
3567c478bd9Sstevel@tonic-gate 			ctr -= nw;
3577c478bd9Sstevel@tonic-gate 			pa += nw;
3587c478bd9Sstevel@tonic-gate 			if (err_msg)
3597c478bd9Sstevel@tonic-gate 				tell_lptell(OKMSG, "Current");
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	return (netpr_response(nd));
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate  *  null() is to be used as a signal handler that does nothing.  It is used in
3687c478bd9Sstevel@tonic-gate  *      place of SIG_IGN, because we want the signal to be delivered and
3697c478bd9Sstevel@tonic-gate  *      interupt the current system call.
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3727c478bd9Sstevel@tonic-gate void
null_sighandler(int i)3737c478bd9Sstevel@tonic-gate null_sighandler(int i)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate }
376