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