1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved. 28*0Sstevel@tonic-gate * Use is subject to license terms. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate /* LINTLIBRARY */ 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate # include <unistd.h> 35*0Sstevel@tonic-gate # include <fcntl.h> 36*0Sstevel@tonic-gate # include <errno.h> 37*0Sstevel@tonic-gate # include <sys/utsname.h> 38*0Sstevel@tonic-gate # include <stdlib.h> 39*0Sstevel@tonic-gate # include <sys/types.h> 40*0Sstevel@tonic-gate # include <sys/stat.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #include "lp.h" 43*0Sstevel@tonic-gate #include "msgs.h" 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #define TURN_OFF(X,F) (void)Fcntl(X, F_SETFL, (Fcntl(X, F_GETFL, 0) & ~(F))) 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate #if defined(__STDC__) 48*0Sstevel@tonic-gate static MESG * connect3_2 ( void ); 49*0Sstevel@tonic-gate static int checklock ( void ); 50*0Sstevel@tonic-gate #else 51*0Sstevel@tonic-gate static MESG * connect3_2(); 52*0Sstevel@tonic-gate static int checklock(); 53*0Sstevel@tonic-gate #endif 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate /* 56*0Sstevel@tonic-gate ** mconnect() - OPEN A MESSAGE PATH 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #if defined(__STDC__) 60*0Sstevel@tonic-gate MESG * mconnect ( char * path, int id1, int id2 ) 61*0Sstevel@tonic-gate #else 62*0Sstevel@tonic-gate MESG * mconnect () 63*0Sstevel@tonic-gate char *path; 64*0Sstevel@tonic-gate int id1; 65*0Sstevel@tonic-gate int id2; 66*0Sstevel@tonic-gate #endif 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate int fd; 69*0Sstevel@tonic-gate int wronly = 0; 70*0Sstevel@tonic-gate int count = 0; 71*0Sstevel@tonic-gate MESG *md; 72*0Sstevel@tonic-gate struct stat stbuf; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate /* 75*0Sstevel@tonic-gate ** invoked as mconnect(path, 0, 0) 76*0Sstevel@tonic-gate ** 77*0Sstevel@tonic-gate ** Open <path>, if isastream() is true for the returned file 78*0Sstevel@tonic-gate ** descriptor, then we're done. If not, proceed with the 3.2 79*0Sstevel@tonic-gate ** handshaking. 80*0Sstevel@tonic-gate */ 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate if (path) 83*0Sstevel@tonic-gate { 84*0Sstevel@tonic-gate /* 85*0Sstevel@tonic-gate ** Verify that the spooler is running and that the 86*0Sstevel@tonic-gate ** <path> identifies a pipe. 87*0Sstevel@tonic-gate ** This prevents us from getting hung in the open 88*0Sstevel@tonic-gate ** and from thinking the <path> is a non-streams pipe. 89*0Sstevel@tonic-gate */ 90*0Sstevel@tonic-gate if (checklock() == -1) 91*0Sstevel@tonic-gate return(NULL); 92*0Sstevel@tonic-gate Again: if (stat(path, &stbuf) == -1) 93*0Sstevel@tonic-gate return(NULL); 94*0Sstevel@tonic-gate if ((stbuf.st_mode & S_IFMT) != S_IFIFO) { 95*0Sstevel@tonic-gate if (count++ > 20) 96*0Sstevel@tonic-gate return (NULL); 97*0Sstevel@tonic-gate sleep(1); 98*0Sstevel@tonic-gate goto Again; 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if ((fd = Open(path, O_RDWR, 0)) == -1) 102*0Sstevel@tonic-gate if ((fd = Open(path, O_WRONLY, 0)) == -1) 103*0Sstevel@tonic-gate return(NULL); 104*0Sstevel@tonic-gate else 105*0Sstevel@tonic-gate wronly = 1; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate if (isastream(fd) && !wronly) 108*0Sstevel@tonic-gate { 109*0Sstevel@tonic-gate #if defined(NOCONNLD) 110*0Sstevel@tonic-gate int fds[2]; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate if (pipe(fds) != 0) 113*0Sstevel@tonic-gate return(NULL); 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate if (ioctl(fd, I_SENDFD, fds[1]) != 0) 116*0Sstevel@tonic-gate return(NULL); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate (void)_Close(fd); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate fd = fds[0]; 121*0Sstevel@tonic-gate (void)_Close(fds[1]); 122*0Sstevel@tonic-gate #endif 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate if ((md = (MESG *)Malloc(MDSIZE)) == NULL) 125*0Sstevel@tonic-gate { 126*0Sstevel@tonic-gate errno = ENOMEM; 127*0Sstevel@tonic-gate return(NULL); 128*0Sstevel@tonic-gate } 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate memset(md, 0, sizeof (MESG)); 131*0Sstevel@tonic-gate md->gid = getgid(); 132*0Sstevel@tonic-gate md->on_discon = NULL; 133*0Sstevel@tonic-gate md->readfd = fd; 134*0Sstevel@tonic-gate md->state = MDS_IDLE; 135*0Sstevel@tonic-gate md->type = MD_STREAM; 136*0Sstevel@tonic-gate md->uid = getuid(); 137*0Sstevel@tonic-gate md->writefd = fd; 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate ResetFifoBuffer (md->readfd); 140*0Sstevel@tonic-gate return(md); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate return(connect3_2()); 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate if (id1 > 0 && id2 > 0) 147*0Sstevel@tonic-gate { 148*0Sstevel@tonic-gate if ((md = (MESG *)Malloc(MDSIZE)) == NULL) 149*0Sstevel@tonic-gate { 150*0Sstevel@tonic-gate errno = ENOMEM; 151*0Sstevel@tonic-gate return(NULL); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate memset(md, 0, sizeof (MESG)); 155*0Sstevel@tonic-gate md->gid = getgid(); 156*0Sstevel@tonic-gate md->on_discon = NULL; 157*0Sstevel@tonic-gate md->readfd = id1; 158*0Sstevel@tonic-gate md->state = MDS_IDLE; 159*0Sstevel@tonic-gate md->type = MD_BOUND; 160*0Sstevel@tonic-gate md->uid = getuid(); 161*0Sstevel@tonic-gate md->writefd = id2; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate ResetFifoBuffer (md->readfd); 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate return(md); 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate errno = EINVAL; 169*0Sstevel@tonic-gate return(NULL); 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate #if defined(__STDC__) 173*0Sstevel@tonic-gate static MESG * connect3_2 ( void ) 174*0Sstevel@tonic-gate #else 175*0Sstevel@tonic-gate static MESG * connect3_2() 176*0Sstevel@tonic-gate #endif 177*0Sstevel@tonic-gate { 178*0Sstevel@tonic-gate char *msgbuf = 0, 179*0Sstevel@tonic-gate *fifo_name = "UUUUUUUUNNNNN"; 180*0Sstevel@tonic-gate int tmp_fd = -1, 181*0Sstevel@tonic-gate size; 182*0Sstevel@tonic-gate short status; 183*0Sstevel@tonic-gate struct utsname ubuf; 184*0Sstevel@tonic-gate MESG *md; 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate if ((md = (MESG *)Malloc(MDSIZE)) == NULL) 187*0Sstevel@tonic-gate return(NULL); 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate memset(md, 0, sizeof (MESG)); 190*0Sstevel@tonic-gate md->gid = getgid(); 191*0Sstevel@tonic-gate md->state = MDS_IDLE; 192*0Sstevel@tonic-gate md->type = MD_USR_FIFO; 193*0Sstevel@tonic-gate md->uid = getuid(); 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate if ((md->writefd = Open(Lp_FIFO, O_WRONLY, 0222)) == -1) 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate errno = ENOENT; 198*0Sstevel@tonic-gate return (NULL); 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* 202*0Sstevel@tonic-gate ** Combine the machine node-name with the process ID to 203*0Sstevel@tonic-gate ** get a name that will be unique across the network. 204*0Sstevel@tonic-gate ** The 99999 is just a safety precaution against over-running 205*0Sstevel@tonic-gate ** the buffer. 206*0Sstevel@tonic-gate */ 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate (void)uname (&ubuf); 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate sprintf (fifo_name, "%.8s%u", ubuf.nodename, (getpid() & 99999)); 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate if (!(md->file = makepath(Lp_Public_FIFOs, fifo_name, (char *)0))) 213*0Sstevel@tonic-gate { 214*0Sstevel@tonic-gate errno = ENOMEM; 215*0Sstevel@tonic-gate goto Error; 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate (void) Unlink(md->file); 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate if (Mknod(md->file, S_IFIFO | S_IRUSR, 0) == -1) 221*0Sstevel@tonic-gate goto Error; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate if ((md->readfd = Open(md->file, O_RDONLY|O_NDELAY, S_IRUSR)) == -1) 224*0Sstevel@tonic-gate goto Error; 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate TURN_OFF (md->readfd, O_NDELAY); 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate size = putmessage((char *)0, S_NEW_QUEUE, 0, fifo_name, ubuf.nodename); 229*0Sstevel@tonic-gate if (!(msgbuf = Malloc((unsigned)size))) 230*0Sstevel@tonic-gate { 231*0Sstevel@tonic-gate errno = ENOMEM; 232*0Sstevel@tonic-gate goto Error; 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate (void) putmessage(msgbuf, S_NEW_QUEUE, 0, fifo_name, ubuf.nodename); 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate if ( 237*0Sstevel@tonic-gate mwrite(md, msgbuf) == -1 238*0Sstevel@tonic-gate || mread(md, msgbuf, size) == -1 239*0Sstevel@tonic-gate || getmessage(msgbuf, R_NEW_QUEUE, &status) != R_NEW_QUEUE 240*0Sstevel@tonic-gate ) 241*0Sstevel@tonic-gate { 242*0Sstevel@tonic-gate Free (msgbuf); 243*0Sstevel@tonic-gate goto Error; 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate else 246*0Sstevel@tonic-gate if (status != MOK) 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate Free(msgbuf); 249*0Sstevel@tonic-gate errno = ENOSPC; 250*0Sstevel@tonic-gate goto Error; 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate Free (msgbuf); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate /* 256*0Sstevel@tonic-gate * Prepare to use the fifo the Spooler created. This new FIFO can be 257*0Sstevel@tonic-gate * read ONLY by who we said we are, so if we lied, tough luck for us! 258*0Sstevel@tonic-gate */ 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate (void)Unlink (md->file); /* must exist to get here */ 261*0Sstevel@tonic-gate tmp_fd = md->readfd; /* save the old fd */ 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate Free(md->file); 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate if (!(md->file = makepath(Lp_Private_FIFOs, fifo_name, (char *)0))) 266*0Sstevel@tonic-gate { 267*0Sstevel@tonic-gate errno = ENOMEM; 268*0Sstevel@tonic-gate goto Error; 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gate if ((md->readfd = Open(md->file, O_RDONLY|O_NDELAY, S_IRUSR)) == -1) 272*0Sstevel@tonic-gate goto Error; 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate TURN_OFF (md->readfd, O_NDELAY); 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate size = putmessage((char *)0, S_NEW_QUEUE, 1, fifo_name, ubuf.nodename); 277*0Sstevel@tonic-gate if (!(msgbuf = Malloc((unsigned)size))) 278*0Sstevel@tonic-gate { 279*0Sstevel@tonic-gate errno = ENOMEM; 280*0Sstevel@tonic-gate goto Error; 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate (void) putmessage(msgbuf, S_NEW_QUEUE, 1, fifo_name, ubuf.nodename); 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate if ( 285*0Sstevel@tonic-gate mwrite(md, msgbuf) == -1 286*0Sstevel@tonic-gate || mread(md, msgbuf, size) == -1 287*0Sstevel@tonic-gate || getmessage(msgbuf, R_NEW_QUEUE, &status) != R_NEW_QUEUE 288*0Sstevel@tonic-gate ) 289*0Sstevel@tonic-gate { 290*0Sstevel@tonic-gate Free (msgbuf); 291*0Sstevel@tonic-gate goto Error; 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate else 294*0Sstevel@tonic-gate if (status != MOK) 295*0Sstevel@tonic-gate { 296*0Sstevel@tonic-gate Free(msgbuf); 297*0Sstevel@tonic-gate errno = ENOSPC; 298*0Sstevel@tonic-gate goto Error; 299*0Sstevel@tonic-gate } 300*0Sstevel@tonic-gate 301*0Sstevel@tonic-gate Free (msgbuf); 302*0Sstevel@tonic-gate (void) Close(tmp_fd); 303*0Sstevel@tonic-gate return (md); 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate Error: 306*0Sstevel@tonic-gate if (md->writefd != -1) 307*0Sstevel@tonic-gate (void) Close (md->writefd); 308*0Sstevel@tonic-gate if (md->writefd != -1) 309*0Sstevel@tonic-gate (void) Close (md->readfd); 310*0Sstevel@tonic-gate if (md->file) 311*0Sstevel@tonic-gate { 312*0Sstevel@tonic-gate (void) Unlink (md->file); 313*0Sstevel@tonic-gate Free (md->file); 314*0Sstevel@tonic-gate } 315*0Sstevel@tonic-gate if (tmp_fd != -1) 316*0Sstevel@tonic-gate (void) Close(tmp_fd); 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate Free(md); 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate return(NULL); 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate #if defined(__STDC__) 325*0Sstevel@tonic-gate static int checklock ( void ) 326*0Sstevel@tonic-gate #else 327*0Sstevel@tonic-gate static int checklock() 328*0Sstevel@tonic-gate #endif 329*0Sstevel@tonic-gate { 330*0Sstevel@tonic-gate int fd; 331*0Sstevel@tonic-gate struct flock lock; 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate if ((fd = Open(Lp_Schedlock, O_RDONLY, 0666)) == -1) 334*0Sstevel@tonic-gate return (-1); 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate /* 337*0Sstevel@tonic-gate * Now, we try to read-lock the lock file. This can only succeed if 338*0Sstevel@tonic-gate * the Spooler (lpsched) is down. 339*0Sstevel@tonic-gate */ 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate lock.l_type = F_RDLCK; 342*0Sstevel@tonic-gate lock.l_whence = 0; 343*0Sstevel@tonic-gate lock.l_start = 0; 344*0Sstevel@tonic-gate lock.l_len = 0; /* till end of file */ 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate if (Fcntl(fd, F_SETLK, &lock) != -1 || errno != EAGAIN) 347*0Sstevel@tonic-gate { 348*0Sstevel@tonic-gate (void)Close (fd); 349*0Sstevel@tonic-gate return (-1); 350*0Sstevel@tonic-gate } 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate /* 353*0Sstevel@tonic-gate * We can get here only when fcntl() == -1 && errno == EAGAIN, 354*0Sstevel@tonic-gate * i.e., spooler (lpsched) is running. 355*0Sstevel@tonic-gate */ 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate (void)Close (fd); 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate return(0); 360*0Sstevel@tonic-gate } 361