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