xref: /titanic_41/usr/src/cmd/lp/lib/msgs/mconnect.c (revision e2738c5e21a9e5d9a6525e48af4738deda3df455)
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*e2738c5eSjacobs  * Common Development and Distribution License (the "License").
6*e2738c5eSjacobs  * 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*e2738c5eSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*e2738c5eSjacobs /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*e2738c5eSjacobs /*	  All Rights Reserved  	*/
29*e2738c5eSjacobs 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate # include	<unistd.h>
347c478bd9Sstevel@tonic-gate # include	<fcntl.h>
357c478bd9Sstevel@tonic-gate # include	<errno.h>
367c478bd9Sstevel@tonic-gate # include	<sys/utsname.h>
377c478bd9Sstevel@tonic-gate # include	<stdlib.h>
387c478bd9Sstevel@tonic-gate # include	<sys/types.h>
397c478bd9Sstevel@tonic-gate # include	<sys/stat.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "lp.h"
427c478bd9Sstevel@tonic-gate #include "msgs.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define TURN_OFF(X,F)	(void)Fcntl(X, F_SETFL, (Fcntl(X, F_GETFL, 0) & ~(F)))
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
477c478bd9Sstevel@tonic-gate static int	checklock ( void );
487c478bd9Sstevel@tonic-gate #else
497c478bd9Sstevel@tonic-gate static int	checklock();
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate ** mconnect() - OPEN A MESSAGE PATH
547c478bd9Sstevel@tonic-gate */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
mconnect(char * path,int id1,int id2)577c478bd9Sstevel@tonic-gate MESG * mconnect ( char * path, int id1, int id2 )
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate MESG * mconnect ()
607c478bd9Sstevel@tonic-gate     char	*path;
617c478bd9Sstevel@tonic-gate     int		id1;
627c478bd9Sstevel@tonic-gate     int		id2;
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate     int		fd;
667c478bd9Sstevel@tonic-gate     int		wronly = 0;
677c478bd9Sstevel@tonic-gate     int		count = 0;
687c478bd9Sstevel@tonic-gate     MESG	*md;
697c478bd9Sstevel@tonic-gate     struct stat	stbuf;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate     /*
727c478bd9Sstevel@tonic-gate     **	invoked as mconnect(path, 0, 0)
737c478bd9Sstevel@tonic-gate     **
747c478bd9Sstevel@tonic-gate     **	Open <path>, if isastream() is true for the returned file
75*e2738c5eSjacobs     **	descriptor, then we're done.
767c478bd9Sstevel@tonic-gate     */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate     if (path)
797c478bd9Sstevel@tonic-gate     {
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	**	Verify that the spooler is running and that the
827c478bd9Sstevel@tonic-gate 	**	<path> identifies a pipe.
837c478bd9Sstevel@tonic-gate 	**	This prevents us from getting hung in the open
847c478bd9Sstevel@tonic-gate 	**	and from thinking the <path> is a non-streams pipe.
857c478bd9Sstevel@tonic-gate 	*/
867c478bd9Sstevel@tonic-gate 	if (checklock() == -1)
877c478bd9Sstevel@tonic-gate 	    return(NULL);
887c478bd9Sstevel@tonic-gate Again:	if (stat(path, &stbuf) == -1)
897c478bd9Sstevel@tonic-gate 	    return(NULL);
907c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) != S_IFIFO) {
917c478bd9Sstevel@tonic-gate             if (count++ > 20)
927c478bd9Sstevel@tonic-gate 		return (NULL);
937c478bd9Sstevel@tonic-gate 	    sleep(1);
947c478bd9Sstevel@tonic-gate 	    goto Again;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if ((fd = Open(path, O_RDWR, 0)) == -1)
987c478bd9Sstevel@tonic-gate 	    if ((fd = Open(path, O_WRONLY, 0)) == -1)
997c478bd9Sstevel@tonic-gate 		return(NULL);
1007c478bd9Sstevel@tonic-gate 	    else
1017c478bd9Sstevel@tonic-gate 		wronly = 1;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (isastream(fd) && !wronly)
1047c478bd9Sstevel@tonic-gate 	{
1057c478bd9Sstevel@tonic-gate #if	defined(NOCONNLD)
1067c478bd9Sstevel@tonic-gate 	    int		fds[2];
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	    if (pipe(fds) != 0)
1097c478bd9Sstevel@tonic-gate 		return(NULL);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	    if (ioctl(fd, I_SENDFD, fds[1]) != 0)
1127c478bd9Sstevel@tonic-gate 		return(NULL);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	    (void)_Close(fd);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	    fd = fds[0];
1177c478bd9Sstevel@tonic-gate 	    (void)_Close(fds[1]);
1187c478bd9Sstevel@tonic-gate #endif
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	    if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
1217c478bd9Sstevel@tonic-gate 	    {
1227c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1237c478bd9Sstevel@tonic-gate 		return(NULL);
1247c478bd9Sstevel@tonic-gate 	    }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	    memset(md, 0, sizeof (MESG));
1277c478bd9Sstevel@tonic-gate 	    md->gid = getgid();
1287c478bd9Sstevel@tonic-gate 	    md->on_discon = NULL;
1297c478bd9Sstevel@tonic-gate 	    md->readfd = fd;
1307c478bd9Sstevel@tonic-gate 	    md->state = MDS_IDLE;
1317c478bd9Sstevel@tonic-gate 	    md->type = MD_STREAM;
1327c478bd9Sstevel@tonic-gate 	    md->uid = getuid();
1337c478bd9Sstevel@tonic-gate 	    md->writefd = fd;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	    ResetFifoBuffer (md->readfd);
1367c478bd9Sstevel@tonic-gate 	    return(md);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
139*e2738c5eSjacobs 	return(NULL);
1407c478bd9Sstevel@tonic-gate     }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate     if (id1 > 0 && id2 > 0)
1437c478bd9Sstevel@tonic-gate     {
1447c478bd9Sstevel@tonic-gate 	if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
1457c478bd9Sstevel@tonic-gate 	{
1467c478bd9Sstevel@tonic-gate 	    errno = ENOMEM;
1477c478bd9Sstevel@tonic-gate 	    return(NULL);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	memset(md, 0, sizeof (MESG));
1517c478bd9Sstevel@tonic-gate 	md->gid = getgid();
1527c478bd9Sstevel@tonic-gate 	md->on_discon = NULL;
1537c478bd9Sstevel@tonic-gate 	md->readfd = id1;
1547c478bd9Sstevel@tonic-gate 	md->state = MDS_IDLE;
1557c478bd9Sstevel@tonic-gate 	md->type = MD_BOUND;
1567c478bd9Sstevel@tonic-gate 	md->uid = getuid();
1577c478bd9Sstevel@tonic-gate 	md->writefd = id2;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	ResetFifoBuffer (md->readfd);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	return(md);
1627c478bd9Sstevel@tonic-gate     }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate     errno = EINVAL;
1657c478bd9Sstevel@tonic-gate     return(NULL);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
checklock(void)1697c478bd9Sstevel@tonic-gate static int checklock ( void )
1707c478bd9Sstevel@tonic-gate #else
1717c478bd9Sstevel@tonic-gate static int checklock()
1727c478bd9Sstevel@tonic-gate #endif
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate     int			fd;
1757c478bd9Sstevel@tonic-gate     struct flock	lock;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate     if ((fd = Open(Lp_Schedlock, O_RDONLY, 0666)) == -1)
1787c478bd9Sstevel@tonic-gate 	return (-1);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate     /*
1817c478bd9Sstevel@tonic-gate      * Now, we try to read-lock the lock file. This can only succeed if
1827c478bd9Sstevel@tonic-gate      * the Spooler (lpsched) is down.
1837c478bd9Sstevel@tonic-gate      */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate     lock.l_type = F_RDLCK;
1867c478bd9Sstevel@tonic-gate     lock.l_whence = 0;
1877c478bd9Sstevel@tonic-gate     lock.l_start = 0;
1887c478bd9Sstevel@tonic-gate     lock.l_len = 0;	/* till end of file */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate     if (Fcntl(fd, F_SETLK, &lock) != -1 || errno != EAGAIN)
1917c478bd9Sstevel@tonic-gate     {
1927c478bd9Sstevel@tonic-gate 	(void)Close (fd);
1937c478bd9Sstevel@tonic-gate 	return (-1);
1947c478bd9Sstevel@tonic-gate     }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate     /*
1977c478bd9Sstevel@tonic-gate      * We can get here only when fcntl() == -1 && errno == EAGAIN,
1987c478bd9Sstevel@tonic-gate      * i.e., spooler (lpsched) is running.
1997c478bd9Sstevel@tonic-gate      */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate     (void)Close (fd);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate     return(0);
2047c478bd9Sstevel@tonic-gate }
205