8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lp / lib / msgs / mconnect.c
blob139c9d8ccbd116fd1a57fbd331c46ba482cde5b4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
31 /* LINTLIBRARY */
33 # include <unistd.h>
34 # include <fcntl.h>
35 # include <errno.h>
36 # include <sys/utsname.h>
37 # include <stdlib.h>
38 # include <sys/types.h>
39 # include <sys/stat.h>
41 #include "lp.h"
42 #include "msgs.h"
44 #define TURN_OFF(X,F) (void)Fcntl(X, F_SETFL, (Fcntl(X, F_GETFL, 0) & ~(F)))
46 #if defined(__STDC__)
47 static int checklock ( void );
48 #else
49 static int checklock();
50 #endif
53 ** mconnect() - OPEN A MESSAGE PATH
56 #if defined(__STDC__)
57 MESG * mconnect ( char * path, int id1, int id2 )
58 #else
59 MESG * mconnect ()
60 char *path;
61 int id1;
62 int id2;
63 #endif
65 int fd;
66 int wronly = 0;
67 int count = 0;
68 MESG *md;
69 struct stat stbuf;
72 ** invoked as mconnect(path, 0, 0)
74 ** Open <path>, if isastream() is true for the returned file
75 ** descriptor, then we're done.
78 if (path)
81 ** Verify that the spooler is running and that the
82 ** <path> identifies a pipe.
83 ** This prevents us from getting hung in the open
84 ** and from thinking the <path> is a non-streams pipe.
86 if (checklock() == -1)
87 return(NULL);
88 Again: if (stat(path, &stbuf) == -1)
89 return(NULL);
90 if ((stbuf.st_mode & S_IFMT) != S_IFIFO) {
91 if (count++ > 20)
92 return (NULL);
93 sleep(1);
94 goto Again;
97 if ((fd = Open(path, O_RDWR, 0)) == -1)
98 if ((fd = Open(path, O_WRONLY, 0)) == -1)
99 return(NULL);
100 else
101 wronly = 1;
103 if (isastream(fd) && !wronly)
105 #if defined(NOCONNLD)
106 int fds[2];
108 if (pipe(fds) != 0)
109 return(NULL);
111 if (ioctl(fd, I_SENDFD, fds[1]) != 0)
112 return(NULL);
114 (void)_Close(fd);
116 fd = fds[0];
117 (void)_Close(fds[1]);
118 #endif
120 if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
122 errno = ENOMEM;
123 return(NULL);
126 memset(md, 0, sizeof (MESG));
127 md->gid = getgid();
128 md->on_discon = NULL;
129 md->readfd = fd;
130 md->state = MDS_IDLE;
131 md->type = MD_STREAM;
132 md->uid = getuid();
133 md->writefd = fd;
135 ResetFifoBuffer (md->readfd);
136 return(md);
139 return(NULL);
142 if (id1 > 0 && id2 > 0)
144 if ((md = (MESG *)Malloc(MDSIZE)) == NULL)
146 errno = ENOMEM;
147 return(NULL);
150 memset(md, 0, sizeof (MESG));
151 md->gid = getgid();
152 md->on_discon = NULL;
153 md->readfd = id1;
154 md->state = MDS_IDLE;
155 md->type = MD_BOUND;
156 md->uid = getuid();
157 md->writefd = id2;
159 ResetFifoBuffer (md->readfd);
161 return(md);
164 errno = EINVAL;
165 return(NULL);
168 #if defined(__STDC__)
169 static int checklock ( void )
170 #else
171 static int checklock()
172 #endif
174 int fd;
175 struct flock lock;
177 if ((fd = Open(Lp_Schedlock, O_RDONLY, 0666)) == -1)
178 return (-1);
181 * Now, we try to read-lock the lock file. This can only succeed if
182 * the Spooler (lpsched) is down.
185 lock.l_type = F_RDLCK;
186 lock.l_whence = 0;
187 lock.l_start = 0;
188 lock.l_len = 0; /* till end of file */
190 if (Fcntl(fd, F_SETLK, &lock) != -1 || errno != EAGAIN)
192 (void)Close (fd);
193 return (-1);
197 * We can get here only when fcntl() == -1 && errno == EAGAIN,
198 * i.e., spooler (lpsched) is running.
201 (void)Close (fd);
203 return(0);