xref: /onnv-gate/usr/src/cmd/lp/cmd/lpsched/ports.c (revision 9354:9559ac454e7e)
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*9354STim.Marsland@Sun.COM  * Common Development and Distribution License (the "License").
6*9354STim.Marsland@Sun.COM  * 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*9354STim.Marsland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include "termio.h"
310Sstevel@tonic-gate #include "dial.h"
320Sstevel@tonic-gate #include "unistd.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "lpsched.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/ioccom.h>
370Sstevel@tonic-gate #include <sys/ecppsys.h>
380Sstevel@tonic-gate 
39*9354STim.Marsland@Sun.COM static void		sigalrm(int);
40*9354STim.Marsland@Sun.COM static int		push_module(int, char *, char *);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static int		SigAlrm;
430Sstevel@tonic-gate 
44*9354STim.Marsland@Sun.COM /*
45*9354STim.Marsland@Sun.COM  * open_dialup() - OPEN A PORT TO A ``DIAL-UP'' PRINTER
46*9354STim.Marsland@Sun.COM  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate int
open_dialup(char * ptype,PRINTER * pp)49*9354STim.Marsland@Sun.COM open_dialup(char *ptype, PRINTER *pp)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	static char		*baud_table[]	= {
52*9354STim.Marsland@Sun.COM 		0,
53*9354STim.Marsland@Sun.COM 		"50",
54*9354STim.Marsland@Sun.COM 		"75",
55*9354STim.Marsland@Sun.COM 		"110",
56*9354STim.Marsland@Sun.COM 		"134",
57*9354STim.Marsland@Sun.COM 		"150",
58*9354STim.Marsland@Sun.COM 		"200",
59*9354STim.Marsland@Sun.COM 		"300",
60*9354STim.Marsland@Sun.COM 		"600",
61*9354STim.Marsland@Sun.COM 		"1200",
62*9354STim.Marsland@Sun.COM 		"1800",
63*9354STim.Marsland@Sun.COM 		"2400",
64*9354STim.Marsland@Sun.COM 		"4800",
65*9354STim.Marsland@Sun.COM 		"9600",
660Sstevel@tonic-gate 		"19200",
670Sstevel@tonic-gate 		"38400",
680Sstevel@tonic-gate 		"57600",
690Sstevel@tonic-gate 		"76800",
700Sstevel@tonic-gate 		"115200",
710Sstevel@tonic-gate 		"153600",
720Sstevel@tonic-gate 		"230400",
730Sstevel@tonic-gate 		"307200",
74*9354STim.Marsland@Sun.COM 		"460800",
75*9354STim.Marsland@Sun.COM 		"921600"
760Sstevel@tonic-gate 	};
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	struct termio		tio;
790Sstevel@tonic-gate 	struct termios		tios;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	CALL			call;
820Sstevel@tonic-gate 
83*9354STim.Marsland@Sun.COM 	int			speed, fd;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	char			*sspeed;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (pp->speed == NULL || (speed = atoi(pp->speed)) <= 0)
890Sstevel@tonic-gate 		speed = -1;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	call.attr = 0;
920Sstevel@tonic-gate 	call.speed = speed;
930Sstevel@tonic-gate 	call.line = 0;
940Sstevel@tonic-gate 	call.telno = pp->dial_info;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if ((fd = dial(call)) < 0)
970Sstevel@tonic-gate 		return (EXEC_EXIT_NDIAL | (~EXEC_EXIT_NMASK & abs(fd)));
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * "dial()" doesn't guarantee which file descriptor
1010Sstevel@tonic-gate 	 * it uses when it opens the port, so we probably have to
1020Sstevel@tonic-gate 	 * move it.
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	if (fd != 1) {
105*9354STim.Marsland@Sun.COM 		dup2(fd, 1);
106*9354STim.Marsland@Sun.COM 		Close(fd);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * The "printermgmt()" routines move out of ".stty"
1110Sstevel@tonic-gate 	 * anything that looks like a baud rate, and puts it
1120Sstevel@tonic-gate 	 * in ".speed", if the printer port is dialed. Thus
1130Sstevel@tonic-gate 	 * we are saved the task of cleaning out spurious
1140Sstevel@tonic-gate 	 * baud rates from ".stty".
1150Sstevel@tonic-gate 	 *
1160Sstevel@tonic-gate 	 * However, we must determine the baud rate and
1170Sstevel@tonic-gate 	 * concatenate it onto ".stty" so that that we can
1180Sstevel@tonic-gate 	 * override the default in the interface progam.
1190Sstevel@tonic-gate 	 * Putting the override in ".stty" allows the user
1200Sstevel@tonic-gate 	 * to override us (although it would be probably be
1210Sstevel@tonic-gate 	 * silly for him or her to do so.)
1220Sstevel@tonic-gate 	 */
123*9354STim.Marsland@Sun.COM 	if (ioctl(1, TCGETS, &tios) < 0) {
124*9354STim.Marsland@Sun.COM 		ioctl(1, TCGETA, &tio);
1250Sstevel@tonic-gate 		tios.c_cflag = tio.c_cflag;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	if ((sspeed = baud_table[cfgetospeed(&tios)]) != NULL) {
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		if (pp->stty == NULL)
1300Sstevel@tonic-gate 			pp->stty = "";
131*9354STim.Marsland@Sun.COM 
1320Sstevel@tonic-gate 		{
133*9354STim.Marsland@Sun.COM 			char *new_stty = Malloc(
134*9354STim.Marsland@Sun.COM 			    strlen(pp->stty) + 1 + strlen(sspeed) + 1);
1350Sstevel@tonic-gate 
136*9354STim.Marsland@Sun.COM 			sprintf(new_stty, "%s %s", pp->stty, sspeed);
1370Sstevel@tonic-gate 
138*9354STim.Marsland@Sun.COM 			/*
139*9354STim.Marsland@Sun.COM 			 * We can trash "pp->stty" because
140*9354STim.Marsland@Sun.COM 			 * the parent process has the good copy.
141*9354STim.Marsland@Sun.COM 			 */
142*9354STim.Marsland@Sun.COM 			pp->stty = new_stty;
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	return (0);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate 
149*9354STim.Marsland@Sun.COM /*
150*9354STim.Marsland@Sun.COM  * open_direct() - OPEN A PORT TO A DIRECTLY CONNECTED PRINTER
151*9354STim.Marsland@Sun.COM  */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
open_direct(char * ptype,PRINTER * pp)1540Sstevel@tonic-gate open_direct(char *ptype, PRINTER *pp)
1550Sstevel@tonic-gate {
156*9354STim.Marsland@Sun.COM 	short bufsz = -1, cps = -1;
157*9354STim.Marsland@Sun.COM 	int open_mode, fd;
158*9354STim.Marsland@Sun.COM 	register unsigned int oldalarm, newalarm = 0;
1590Sstevel@tonic-gate 	char *device;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	struct ecpp_transfer_parms ecpp_params;	/* for ECPP port checking */
162*9354STim.Marsland@Sun.COM 	char **modules = NULL;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	struct flock		lck;
1650Sstevel@tonic-gate 	struct stat		buf;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	register void		(*oldsig)() = signal(SIGALRM, sigalrm);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Set an alarm to wake us from trying to open the port.
1720Sstevel@tonic-gate 	 * We'll try at least 60 seconds, or more if the printer
1730Sstevel@tonic-gate 	 * has a huge buffer that, in the worst case, would take
1740Sstevel@tonic-gate 	 * a long time to drain.
1750Sstevel@tonic-gate 	 */
176*9354STim.Marsland@Sun.COM 	tidbit(ptype, "bufsz", &bufsz);
177*9354STim.Marsland@Sun.COM 	tidbit(ptype, "cps", &cps);
1780Sstevel@tonic-gate 	if (bufsz > 0 && cps > 0)
1790Sstevel@tonic-gate 		newalarm = (((long)bufsz * 1100) / cps) / 1000;
1800Sstevel@tonic-gate 	if (newalarm < 60)
1810Sstevel@tonic-gate 		newalarm = 60;
1820Sstevel@tonic-gate 	oldalarm = alarm(newalarm);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	device = pp->device;
1850Sstevel@tonic-gate 	if (is_printer_uri(device) == 0) {
1860Sstevel@tonic-gate 		/*
1870Sstevel@tonic-gate 		 * if it's a device uri and the endpoint contains a valid
1880Sstevel@tonic-gate 		 * path, that path should be opened/locked by lpsched for
1890Sstevel@tonic-gate 		 * the backend.  If not, the uri isn't associated with a
1900Sstevel@tonic-gate 		 * local device, so use /dev/null.
1910Sstevel@tonic-gate 		 */
1920Sstevel@tonic-gate 		device = strstr(device, "://");
1930Sstevel@tonic-gate 		if (device != NULL)
1940Sstevel@tonic-gate 			device = strchr(device + 3, '/');
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		if ((device == NULL) || (access(device, F_OK) < 0))
1970Sstevel@tonic-gate 			device = "/dev/null";
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * The following open must be interruptable.
2020Sstevel@tonic-gate 	 * O_APPEND is set in case the ``port'' is a file.
2030Sstevel@tonic-gate 	 * O_RDWR is set in case the interface program wants
2040Sstevel@tonic-gate 	 * to get input from the printer. Don't fail, though,
2050Sstevel@tonic-gate 	 * just because we can't get read access.
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	open_mode = O_WRONLY;
2090Sstevel@tonic-gate 	if (access(device, R_OK) == 0)
2100Sstevel@tonic-gate 		open_mode = O_RDWR;
2110Sstevel@tonic-gate 	open_mode |= O_APPEND;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	SigAlrm = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	while ((fd = open(device, open_mode, 0)) == -1) {
2160Sstevel@tonic-gate 		if (errno != EINTR)
2170Sstevel@tonic-gate 			return (EXEC_EXIT_NPORT);
2180Sstevel@tonic-gate 		else if (SigAlrm)
2190Sstevel@tonic-gate 			return (EXEC_EXIT_TMOUT);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
222*9354STim.Marsland@Sun.COM 	alarm(oldalarm);
223*9354STim.Marsland@Sun.COM 	signal(SIGALRM, oldsig);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/*
2260Sstevel@tonic-gate 	 * Lock the file in case two "printers" are defined on the
2270Sstevel@tonic-gate 	 * same port.  Don't lock /dev/null.
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	lck.l_type = F_WRLCK;
231*9354STim.Marsland@Sun.COM 	lck.l_whence = 0;
2320Sstevel@tonic-gate 	lck.l_start = 0L;
2330Sstevel@tonic-gate 	lck.l_len = 0L;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (strcmp(device, "/dev/null") && Fcntl(fd, F_SETLKW, &lck) < 0) {
2360Sstevel@tonic-gate 		execlog("lock error: %s\n", pp->device);
2370Sstevel@tonic-gate 		return (EXEC_EXIT_NPORT);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * We should get the correct channel number (1), but just
2420Sstevel@tonic-gate 	 * in case....
2430Sstevel@tonic-gate 	 */
2440Sstevel@tonic-gate 	if (fd != 1) {
245*9354STim.Marsland@Sun.COM 		dup2(fd, 1);
246*9354STim.Marsland@Sun.COM 		Close(fd);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/*
2500Sstevel@tonic-gate 	 * Handle streams modules:
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	if (fstat(1, &buf))
2530Sstevel@tonic-gate 		buf.st_mode = 0;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * for some unknown reason, lpsched appears to pop the streams
2570Sstevel@tonic-gate 	 * modules off the device and push back some "default" ones,
2580Sstevel@tonic-gate 	 * unless a specific set were specified with the printer configuration.
2590Sstevel@tonic-gate 	 * This behaviour causes problems with the ECPP port, so if we have
2600Sstevel@tonic-gate 	 * an ECPP port, and nobody specified a set of modules to use, we
2610Sstevel@tonic-gate 	 * should leave it alone.  Normally, we would not bother to play with
2620Sstevel@tonic-gate 	 * the streams modules, but it is possible that someone has come
2630Sstevel@tonic-gate 	 * to rely on this behaviour for other devices.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if ((pp->modules != NULL) && (pp->modules[0] != NULL) &&
2660Sstevel@tonic-gate 	    (strcmp(pp->modules[0], "default") != 0))
2670Sstevel@tonic-gate 		modules = pp->modules;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if ((modules == NULL) && (ioctl(1, ECPPIOC_GETPARMS, &ecpp_params) < 0))
2700Sstevel@tonic-gate 		modules = getlist(DEFMODULES, LP_WS, LP_SEP);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/* if "nopush" is supplied, leave the modules alone */
2730Sstevel@tonic-gate 	if ((modules != NULL) && (modules[0] != NULL) &&
2740Sstevel@tonic-gate 	    (strcasecmp(modules[0], "nopush") == 0))
2750Sstevel@tonic-gate 		modules = NULL;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * If we have a stream and a list of modules to use, then pop the old
2790Sstevel@tonic-gate 	 * modules and push the new ones.
2800Sstevel@tonic-gate 	 */
281871Scasper 	if ((modules != NULL) && !S_ISFIFO(buf.st_mode) && isastream(1)) {
2820Sstevel@tonic-gate 		/*
2830Sstevel@tonic-gate 		 * First, pop all current modules off, unless
2840Sstevel@tonic-gate 		 * instructed not to.
2850Sstevel@tonic-gate 		 */
2860Sstevel@tonic-gate 		while (ioctl(1, I_POP, 0) == 0)
287*9354STim.Marsland@Sun.COM 			;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * Now push either the administrator specified modules
2910Sstevel@tonic-gate 		 * or the standard modules, unless instructed to push
2920Sstevel@tonic-gate 		 * nothing.
2930Sstevel@tonic-gate 		 */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		if ((modules[1] == NULL) &&
2960Sstevel@tonic-gate 		    (strcasecmp(modules[0], "none") == 0))
2970Sstevel@tonic-gate 			return (0);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		while (*modules)
3000Sstevel@tonic-gate 			if (push_module(1, device, *modules++) == -1)
3010Sstevel@tonic-gate 				return (EXEC_EXIT_NPUSH);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	return (0);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
307*9354STim.Marsland@Sun.COM /*
308*9354STim.Marsland@Sun.COM  * sigalrm()
309*9354STim.Marsland@Sun.COM  */
3100Sstevel@tonic-gate static void
sigalrm(int ignore)3110Sstevel@tonic-gate sigalrm(int ignore)
3120Sstevel@tonic-gate {
313*9354STim.Marsland@Sun.COM 	signal(SIGALRM, SIG_IGN);
3140Sstevel@tonic-gate 	SigAlrm = 1;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 
318*9354STim.Marsland@Sun.COM /*
319*9354STim.Marsland@Sun.COM  * push_module()
320*9354STim.Marsland@Sun.COM  */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate static int
push_module(int fd,char * device,char * module)3230Sstevel@tonic-gate push_module(int fd, char *device, char *module)
3240Sstevel@tonic-gate {
325*9354STim.Marsland@Sun.COM 	int ret	= ioctl(fd, I_PUSH, module);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (ret == -1)
328*9354STim.Marsland@Sun.COM 		note("push (%s) on %s failed (%s)\n", module, device, PERROR);
3290Sstevel@tonic-gate 	return (ret);
3300Sstevel@tonic-gate }
331