xref: /onnv-gate/usr/src/cmd/lp/lib/msgs/mconnect.c (revision 2660:1b2ba42db5f2)
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*2660Sjacobs  * Common Development and Distribution License (the "License").
6*2660Sjacobs  * 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  */
21*2660Sjacobs 
22*2660Sjacobs /*
23*2660Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2660Sjacobs  * Use is subject to license terms.
25*2660Sjacobs  */
26*2660Sjacobs 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate /* LINTLIBRARY */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate # include	<unistd.h>
340Sstevel@tonic-gate # include	<fcntl.h>
350Sstevel@tonic-gate # include	<errno.h>
360Sstevel@tonic-gate # include	<sys/utsname.h>
370Sstevel@tonic-gate # include	<stdlib.h>
380Sstevel@tonic-gate # include	<sys/types.h>
390Sstevel@tonic-gate # include	<sys/stat.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "lp.h"
420Sstevel@tonic-gate #include "msgs.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define TURN_OFF(X,F)	(void)Fcntl(X, F_SETFL, (Fcntl(X, F_GETFL, 0) & ~(F)))
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #if	defined(__STDC__)
470Sstevel@tonic-gate static int	checklock ( void );
480Sstevel@tonic-gate #else
490Sstevel@tonic-gate static int	checklock();
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate ** mconnect() - OPEN A MESSAGE PATH
540Sstevel@tonic-gate */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #if	defined(__STDC__)
mconnect(char * path,int id1,int id2)570Sstevel@tonic-gate MESG * mconnect ( char * path, int id1, int id2 )
580Sstevel@tonic-gate #else
590Sstevel@tonic-gate MESG * mconnect ()
600Sstevel@tonic-gate     char	*path;
610Sstevel@tonic-gate     int		id1;
620Sstevel@tonic-gate     int		id2;
630Sstevel@tonic-gate #endif
640Sstevel@tonic-gate {
650Sstevel@tonic-gate     int		fd;
660Sstevel@tonic-gate     int		wronly = 0;
670Sstevel@tonic-gate     int		count = 0;
680Sstevel@tonic-gate     MESG	*md;
690Sstevel@tonic-gate     struct stat	stbuf;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate     /*
720Sstevel@tonic-gate     **	invoked as mconnect(path, 0, 0)
730Sstevel@tonic-gate     **
740Sstevel@tonic-gate     **	Open <path>, if isastream() is true for the returned file
75*2660Sjacobs     **	descriptor, then we're done.
760Sstevel@tonic-gate     */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate     if (path)
790Sstevel@tonic-gate     {
800Sstevel@tonic-gate 	/*
810Sstevel@tonic-gate 	**	Verify that the spooler is running and that the
820Sstevel@tonic-gate 	**	<path> identifies a pipe.
830Sstevel@tonic-gate 	**	This prevents us from getting hung in the open
840Sstevel@tonic-gate 	**	and from thinking the <path> is a non-streams pipe.
850Sstevel@tonic-gate 	*/
860Sstevel@tonic-gate 	if (checklock() == -1)
870Sstevel@tonic-gate 	    return(NULL);
880Sstevel@tonic-gate Again:	if (stat(path, &stbuf) == -1)
890Sstevel@tonic-gate 	    return(NULL);
900Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) != S_IFIFO) {
910Sstevel@tonic-gate             if (count++ > 20)
920Sstevel@tonic-gate 		return (NULL);
930Sstevel@tonic-gate 	    sleep(1);
940Sstevel@tonic-gate 	    goto Again;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if ((fd = Open(path, O_RDWR, 0)) == -1)
980Sstevel@tonic-gate 	    if ((fd = Open(path, O_WRONLY, 0)) == -1)
990Sstevel@tonic-gate 		return(NULL);
1000Sstevel@tonic-gate 	    else
1010Sstevel@tonic-gate 		wronly = 1;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (isastream(fd) && !wronly)
1040Sstevel@tonic-gate 	{
1050Sstevel@tonic-gate #if	defined(NOCONNLD)
1060Sstevel@tonic-gate 	    int		fds[2];
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	    if (pipe(fds) != 0)
1090Sstevel@tonic-gate 		return(NULL);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	    if (ioctl(fd, I_SENDFD, fds[1]) != 0)
1120Sstevel@tonic-gate 		return(NULL);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	    (void)_Close(fd);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	    fd = fds[0];
1170Sstevel@tonic-gate 	    (void)_Close(fds[1]);
1180Sstevel@tonic-gate #endif
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	    if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
1210Sstevel@tonic-gate 	    {
1220Sstevel@tonic-gate 		errno = ENOMEM;
1230Sstevel@tonic-gate 		return(NULL);
1240Sstevel@tonic-gate 	    }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	    memset(md, 0, sizeof (MESG));
1270Sstevel@tonic-gate 	    md->gid = getgid();
1280Sstevel@tonic-gate 	    md->on_discon = NULL;
1290Sstevel@tonic-gate 	    md->readfd = fd;
1300Sstevel@tonic-gate 	    md->state = MDS_IDLE;
1310Sstevel@tonic-gate 	    md->type = MD_STREAM;
1320Sstevel@tonic-gate 	    md->uid = getuid();
1330Sstevel@tonic-gate 	    md->writefd = fd;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	    ResetFifoBuffer (md->readfd);
1360Sstevel@tonic-gate 	    return(md);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 
139*2660Sjacobs 	return(NULL);
1400Sstevel@tonic-gate     }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate     if (id1 > 0 && id2 > 0)
1430Sstevel@tonic-gate     {
1440Sstevel@tonic-gate 	if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
1450Sstevel@tonic-gate 	{
1460Sstevel@tonic-gate 	    errno = ENOMEM;
1470Sstevel@tonic-gate 	    return(NULL);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	memset(md, 0, sizeof (MESG));
1510Sstevel@tonic-gate 	md->gid = getgid();
1520Sstevel@tonic-gate 	md->on_discon = NULL;
1530Sstevel@tonic-gate 	md->readfd = id1;
1540Sstevel@tonic-gate 	md->state = MDS_IDLE;
1550Sstevel@tonic-gate 	md->type = MD_BOUND;
1560Sstevel@tonic-gate 	md->uid = getuid();
1570Sstevel@tonic-gate 	md->writefd = id2;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	ResetFifoBuffer (md->readfd);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	return(md);
1620Sstevel@tonic-gate     }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate     errno = EINVAL;
1650Sstevel@tonic-gate     return(NULL);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate #if	defined(__STDC__)
checklock(void)1690Sstevel@tonic-gate static int checklock ( void )
1700Sstevel@tonic-gate #else
1710Sstevel@tonic-gate static int checklock()
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate     int			fd;
1750Sstevel@tonic-gate     struct flock	lock;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate     if ((fd = Open(Lp_Schedlock, O_RDONLY, 0666)) == -1)
1780Sstevel@tonic-gate 	return (-1);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate     /*
1810Sstevel@tonic-gate      * Now, we try to read-lock the lock file. This can only succeed if
1820Sstevel@tonic-gate      * the Spooler (lpsched) is down.
1830Sstevel@tonic-gate      */
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate     lock.l_type = F_RDLCK;
1860Sstevel@tonic-gate     lock.l_whence = 0;
1870Sstevel@tonic-gate     lock.l_start = 0;
1880Sstevel@tonic-gate     lock.l_len = 0;	/* till end of file */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate     if (Fcntl(fd, F_SETLK, &lock) != -1 || errno != EAGAIN)
1910Sstevel@tonic-gate     {
1920Sstevel@tonic-gate 	(void)Close (fd);
1930Sstevel@tonic-gate 	return (-1);
1940Sstevel@tonic-gate     }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate     /*
1970Sstevel@tonic-gate      * We can get here only when fcntl() == -1 && errno == EAGAIN,
1980Sstevel@tonic-gate      * i.e., spooler (lpsched) is running.
1990Sstevel@tonic-gate      */
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate     (void)Close (fd);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate     return(0);
2040Sstevel@tonic-gate }
205