1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996, by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All Rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 27*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */ 31*7c478bd9Sstevel@tonic-gate 32*7c478bd9Sstevel@tonic-gate #include "uucp.h" 33*7c478bd9Sstevel@tonic-gate #include <rpc/trace.h> 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #include <unistd.h> 36*7c478bd9Sstevel@tonic-gate /* #include <sys/types.h> */ 37*7c478bd9Sstevel@tonic-gate /* #include <sys/stat.h> */ 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate #ifdef V7 40*7c478bd9Sstevel@tonic-gate #define O_RDONLY 0 41*7c478bd9Sstevel@tonic-gate #endif 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate #undef _STAT 44*7c478bd9Sstevel@tonic-gate #undef _FSTAT 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #define _STAT stat64 47*7c478bd9Sstevel@tonic-gate #define _FSTAT fstat64 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate static void stlock(); 50*7c478bd9Sstevel@tonic-gate static int onelock(); 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate /* 53*7c478bd9Sstevel@tonic-gate * make a lock file with given 'name' 54*7c478bd9Sstevel@tonic-gate * If one already exists, send a signal 0 to the process--if 55*7c478bd9Sstevel@tonic-gate * it fails, then unlink it and make a new one. 56*7c478bd9Sstevel@tonic-gate * 57*7c478bd9Sstevel@tonic-gate * input: 58*7c478bd9Sstevel@tonic-gate * name - name of the lock file to make 59*7c478bd9Sstevel@tonic-gate * 60*7c478bd9Sstevel@tonic-gate * return: 61*7c478bd9Sstevel@tonic-gate * 0 -> success 62*7c478bd9Sstevel@tonic-gate * FAIL -> failure 63*7c478bd9Sstevel@tonic-gate */ 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate GLOBAL int 66*7c478bd9Sstevel@tonic-gate mklock(name) 67*7c478bd9Sstevel@tonic-gate register char *name; 68*7c478bd9Sstevel@tonic-gate { 69*7c478bd9Sstevel@tonic-gate static char pid[SIZEOFPID+2] = { '\0' }; /* +2 for '\n' and NULL */ 70*7c478bd9Sstevel@tonic-gate static char *tempfile; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate #ifdef V8 73*7c478bd9Sstevel@tonic-gate char *cp; 74*7c478bd9Sstevel@tonic-gate #endif 75*7c478bd9Sstevel@tonic-gate trace1(TR_mklock, 0); 76*7c478bd9Sstevel@tonic-gate if (pid[0] == '\0') { 77*7c478bd9Sstevel@tonic-gate tempfile = (char *)malloc(MAXNAMESIZE); 78*7c478bd9Sstevel@tonic-gate if (tempfile == NULL) 79*7c478bd9Sstevel@tonic-gate return (FAIL); 80*7c478bd9Sstevel@tonic-gate (void) sprintf(pid, "%*ld\n", SIZEOFPID, (long) getpid()); 81*7c478bd9Sstevel@tonic-gate (void) sprintf(tempfile, "%s/LTMP.%ld", X_LOCKDIR, (long) getpid()); 82*7c478bd9Sstevel@tonic-gate } 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate #ifdef V8 /* this wouldn't be a problem if we used lock directories */ 85*7c478bd9Sstevel@tonic-gate /* some day the truncation of system names will bite us */ 86*7c478bd9Sstevel@tonic-gate cp = rindex(name, '/'); 87*7c478bd9Sstevel@tonic-gate if (cp++ != CNULL) 88*7c478bd9Sstevel@tonic-gate if (strlen(cp) > MAXBASENAME) 89*7c478bd9Sstevel@tonic-gate *(cp+MAXBASENAME) = NULLCHAR; 90*7c478bd9Sstevel@tonic-gate #endif /* V8 */ 91*7c478bd9Sstevel@tonic-gate if (onelock(pid, tempfile, name) == -1) { 92*7c478bd9Sstevel@tonic-gate (void) unlink(tempfile); 93*7c478bd9Sstevel@tonic-gate if (cklock(name)) { 94*7c478bd9Sstevel@tonic-gate trace1(TR_mklock, 1); 95*7c478bd9Sstevel@tonic-gate return (FAIL); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate else { 98*7c478bd9Sstevel@tonic-gate if (onelock(pid, tempfile, name)) { 99*7c478bd9Sstevel@tonic-gate (void) unlink(tempfile); 100*7c478bd9Sstevel@tonic-gate DEBUG(4,"ulockf failed in onelock()\n%s", ""); 101*7c478bd9Sstevel@tonic-gate trace1(TR_mklock, 1); 102*7c478bd9Sstevel@tonic-gate return (FAIL); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate } 106*7c478bd9Sstevel@tonic-gate stlock(name); 107*7c478bd9Sstevel@tonic-gate trace1(TR_mklock, 1); 108*7c478bd9Sstevel@tonic-gate return (0); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate /* 112*7c478bd9Sstevel@tonic-gate * check to see if the lock file exists and is still active 113*7c478bd9Sstevel@tonic-gate * - use kill(pid,0) 114*7c478bd9Sstevel@tonic-gate * 115*7c478bd9Sstevel@tonic-gate * return: 116*7c478bd9Sstevel@tonic-gate * 0 -> success (lock file removed - no longer active 117*7c478bd9Sstevel@tonic-gate * FAIL -> lock file still active 118*7c478bd9Sstevel@tonic-gate */ 119*7c478bd9Sstevel@tonic-gate GLOBAL int 120*7c478bd9Sstevel@tonic-gate cklock(name) 121*7c478bd9Sstevel@tonic-gate register char *name; 122*7c478bd9Sstevel@tonic-gate { 123*7c478bd9Sstevel@tonic-gate register int ret; 124*7c478bd9Sstevel@tonic-gate pid_t lpid = -1; 125*7c478bd9Sstevel@tonic-gate char alpid[SIZEOFPID+2]; /* +2 for '\n' and NULL */ 126*7c478bd9Sstevel@tonic-gate int fd; 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate trace1(TR_cklock, 0); 129*7c478bd9Sstevel@tonic-gate fd = open(name, O_RDONLY); 130*7c478bd9Sstevel@tonic-gate DEBUG(4, "ulockf name %s\n", name); 131*7c478bd9Sstevel@tonic-gate if (fd == -1) { 132*7c478bd9Sstevel@tonic-gate if (errno == ENOENT) { /* file does not exist -- OK */ 133*7c478bd9Sstevel@tonic-gate trace1(TR_cklock, 1); 134*7c478bd9Sstevel@tonic-gate return (0); 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate DEBUG(4,"Lock File--can't read (errno %d) --remove it!\n", errno); 137*7c478bd9Sstevel@tonic-gate goto unlk; 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate ret = read(fd, (char *) alpid, SIZEOFPID+1); /* +1 for '\n' */ 140*7c478bd9Sstevel@tonic-gate (void) close(fd); 141*7c478bd9Sstevel@tonic-gate if (ret != (SIZEOFPID+1)) { 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate DEBUG(4, "Lock File--bad format--remove it!\n%s", ""); 144*7c478bd9Sstevel@tonic-gate goto unlk; 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate lpid = (pid_t) strtol(alpid, (char **) NULL, 10); 147*7c478bd9Sstevel@tonic-gate if ((ret=kill(lpid, 0)) == 0 || errno == EPERM) { 148*7c478bd9Sstevel@tonic-gate DEBUG(4, "Lock File--process still active--not removed\n%s", ""); 149*7c478bd9Sstevel@tonic-gate trace1(TR_cklock, 1); 150*7c478bd9Sstevel@tonic-gate return (FAIL); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate else { /* process no longer active */ 153*7c478bd9Sstevel@tonic-gate /*EMPTY*/ 154*7c478bd9Sstevel@tonic-gate DEBUG(4, "kill pid (%ld), ", (long) lpid); 155*7c478bd9Sstevel@tonic-gate DEBUG(4, "returned %d", ret); 156*7c478bd9Sstevel@tonic-gate DEBUG(4, "--ok to remove lock file (%s)\n", name); 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate unlk: 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate if (unlink(name) != 0) { 161*7c478bd9Sstevel@tonic-gate DEBUG(4,"ulockf failed in unlink()\n%s", ""); 162*7c478bd9Sstevel@tonic-gate trace1(TR_cklock, 1); 163*7c478bd9Sstevel@tonic-gate return (FAIL); 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate trace1(TR_cklock, 1); 166*7c478bd9Sstevel@tonic-gate return (0); 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate #define MAXLOCKS 10 /* maximum number of lock files */ 170*7c478bd9Sstevel@tonic-gate static char *Lockfile[MAXLOCKS]; 171*7c478bd9Sstevel@tonic-gate GLOBAL int Nlocks = 0; 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate /* 174*7c478bd9Sstevel@tonic-gate * put name in list of lock files 175*7c478bd9Sstevel@tonic-gate * return: 176*7c478bd9Sstevel@tonic-gate * none 177*7c478bd9Sstevel@tonic-gate */ 178*7c478bd9Sstevel@tonic-gate static void 179*7c478bd9Sstevel@tonic-gate stlock(name) 180*7c478bd9Sstevel@tonic-gate char *name; 181*7c478bd9Sstevel@tonic-gate { 182*7c478bd9Sstevel@tonic-gate register int i; 183*7c478bd9Sstevel@tonic-gate char *p; 184*7c478bd9Sstevel@tonic-gate 185*7c478bd9Sstevel@tonic-gate trace1(TR_stlock, 0); 186*7c478bd9Sstevel@tonic-gate for (i = 0; i < Nlocks; i++) { 187*7c478bd9Sstevel@tonic-gate if (Lockfile[i] == NULL) 188*7c478bd9Sstevel@tonic-gate break; 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate ASSERT(i < MAXLOCKS, "TOO MANY LOCKS", "", i); 191*7c478bd9Sstevel@tonic-gate if (i >= Nlocks) 192*7c478bd9Sstevel@tonic-gate i = Nlocks++; 193*7c478bd9Sstevel@tonic-gate p = (char*) calloc((unsigned) strlen(name) + 1, sizeof (char)); 194*7c478bd9Sstevel@tonic-gate ASSERT(p != NULL, "CAN NOT ALLOCATE FOR", name, 0); 195*7c478bd9Sstevel@tonic-gate (void) strcpy(p, name); 196*7c478bd9Sstevel@tonic-gate Lockfile[i] = p; 197*7c478bd9Sstevel@tonic-gate trace1(TR_stlock, 1); 198*7c478bd9Sstevel@tonic-gate return; 199*7c478bd9Sstevel@tonic-gate } 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate /* 202*7c478bd9Sstevel@tonic-gate * remove the named lock. If named lock is NULL, 203*7c478bd9Sstevel@tonic-gate * then remove all locks currently in list. 204*7c478bd9Sstevel@tonic-gate * return: 205*7c478bd9Sstevel@tonic-gate * none 206*7c478bd9Sstevel@tonic-gate */ 207*7c478bd9Sstevel@tonic-gate GLOBAL void 208*7c478bd9Sstevel@tonic-gate rmlock(name) 209*7c478bd9Sstevel@tonic-gate register char *name; 210*7c478bd9Sstevel@tonic-gate { 211*7c478bd9Sstevel@tonic-gate register int i; 212*7c478bd9Sstevel@tonic-gate #ifdef V8 213*7c478bd9Sstevel@tonic-gate char *cp; 214*7c478bd9Sstevel@tonic-gate #endif /* V8 */ 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate trace1(TR_rmlock, 0); 217*7c478bd9Sstevel@tonic-gate #ifdef V8 218*7c478bd9Sstevel@tonic-gate cp = rindex(name, '/'); 219*7c478bd9Sstevel@tonic-gate if (cp++ != CNULL) 220*7c478bd9Sstevel@tonic-gate if (strlen(cp) > MAXBASENAME) 221*7c478bd9Sstevel@tonic-gate *(cp+MAXBASENAME) = NULLCHAR; 222*7c478bd9Sstevel@tonic-gate #endif /* V8 */ 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate for (i = 0; i < Nlocks; i++) { 225*7c478bd9Sstevel@tonic-gate if (Lockfile[i] == NULL) 226*7c478bd9Sstevel@tonic-gate continue; 227*7c478bd9Sstevel@tonic-gate if (name == NULL || EQUALS(name, Lockfile[i])) { 228*7c478bd9Sstevel@tonic-gate (void) unlink(Lockfile[i]); 229*7c478bd9Sstevel@tonic-gate free(Lockfile[i]); 230*7c478bd9Sstevel@tonic-gate Lockfile[i] = NULL; 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate trace1(TR_rmlock, 1); 234*7c478bd9Sstevel@tonic-gate return; 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate /* 238*7c478bd9Sstevel@tonic-gate * remove a lock file 239*7c478bd9Sstevel@tonic-gate * 240*7c478bd9Sstevel@tonic-gate * Parameters: 241*7c478bd9Sstevel@tonic-gate * pre - Path and first part of file name of the lock file to be 242*7c478bd9Sstevel@tonic-gate * removed. 243*7c478bd9Sstevel@tonic-gate * s - The suffix part of the lock file. The name of the lock file 244*7c478bd9Sstevel@tonic-gate * will be derrived by concatenating pre, a period, and s. 245*7c478bd9Sstevel@tonic-gate * 246*7c478bd9Sstevel@tonic-gate * return: 247*7c478bd9Sstevel@tonic-gate * none 248*7c478bd9Sstevel@tonic-gate */ 249*7c478bd9Sstevel@tonic-gate GLOBAL void 250*7c478bd9Sstevel@tonic-gate delock(pre, s) 251*7c478bd9Sstevel@tonic-gate char * pre; 252*7c478bd9Sstevel@tonic-gate char *s; 253*7c478bd9Sstevel@tonic-gate { 254*7c478bd9Sstevel@tonic-gate char ln[MAXNAMESIZE]; 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate trace1(TR_delock, 0); 257*7c478bd9Sstevel@tonic-gate (void) sprintf(ln, "%s.%s", pre, s); 258*7c478bd9Sstevel@tonic-gate BASENAME(ln, '/')[MAXBASENAME] = '\0'; 259*7c478bd9Sstevel@tonic-gate rmlock(ln); 260*7c478bd9Sstevel@tonic-gate trace1(TR_delock, 1); 261*7c478bd9Sstevel@tonic-gate return; 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate 264*7c478bd9Sstevel@tonic-gate /* 265*7c478bd9Sstevel@tonic-gate * create lock file 266*7c478bd9Sstevel@tonic-gate * 267*7c478bd9Sstevel@tonic-gate * Parameters: 268*7c478bd9Sstevel@tonic-gate * pre - Path and first part of file name of the lock file to be 269*7c478bd9Sstevel@tonic-gate * created. 270*7c478bd9Sstevel@tonic-gate * name - The suffix part of the lock file. The name of the lock file 271*7c478bd9Sstevel@tonic-gate * will be derrived by concatenating pre, a period, and name. 272*7c478bd9Sstevel@tonic-gate * 273*7c478bd9Sstevel@tonic-gate * return: 274*7c478bd9Sstevel@tonic-gate * 0 -> success 275*7c478bd9Sstevel@tonic-gate * FAIL -> failure 276*7c478bd9Sstevel@tonic-gate */ 277*7c478bd9Sstevel@tonic-gate GLOBAL int 278*7c478bd9Sstevel@tonic-gate mlock(pre, name) 279*7c478bd9Sstevel@tonic-gate char * pre; 280*7c478bd9Sstevel@tonic-gate char *name; 281*7c478bd9Sstevel@tonic-gate { 282*7c478bd9Sstevel@tonic-gate char lname[MAXNAMESIZE]; 283*7c478bd9Sstevel@tonic-gate int dummy; 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate trace1(TR_mlock, 0); 286*7c478bd9Sstevel@tonic-gate /* 287*7c478bd9Sstevel@tonic-gate * if name has a '/' in it, then it's a device name and it's 288*7c478bd9Sstevel@tonic-gate * not in /dev (i.e., it's a remotely-mounted device or it's 289*7c478bd9Sstevel@tonic-gate * in a subdirectory of /dev). in either case, creating our normal 290*7c478bd9Sstevel@tonic-gate * lockfile (/var/spool/locks/LCK..<dev>) is going to bomb if 291*7c478bd9Sstevel@tonic-gate * <dev> is "/remote/dev/term/14" or "/dev/net/foo/clone", so never 292*7c478bd9Sstevel@tonic-gate * mind. since we're using advisory filelocks on the devices 293*7c478bd9Sstevel@tonic-gate * themselves, it'll be safe. 294*7c478bd9Sstevel@tonic-gate * 295*7c478bd9Sstevel@tonic-gate * of course, programs and people who are used to looking at the 296*7c478bd9Sstevel@tonic-gate * lockfiles to find out what's going on are going to be a trifle 297*7c478bd9Sstevel@tonic-gate * misled. we really need to re-consider the lockfile naming structure 298*7c478bd9Sstevel@tonic-gate * to accomodate devices in directories other than /dev ... maybe in 299*7c478bd9Sstevel@tonic-gate * the next release. 300*7c478bd9Sstevel@tonic-gate */ 301*7c478bd9Sstevel@tonic-gate if (strchr(name, '/') != NULL) { 302*7c478bd9Sstevel@tonic-gate trace1(TR_mlock, 1); 303*7c478bd9Sstevel@tonic-gate return (0); 304*7c478bd9Sstevel@tonic-gate } 305*7c478bd9Sstevel@tonic-gate (void) sprintf(lname, "%s.%s", pre, BASENAME(name, '/')); 306*7c478bd9Sstevel@tonic-gate BASENAME(lname, '/')[MAXBASENAME] = '\0'; 307*7c478bd9Sstevel@tonic-gate dummy = mklock(lname); 308*7c478bd9Sstevel@tonic-gate trace1(TR_mlock, 1); 309*7c478bd9Sstevel@tonic-gate return (dummy); 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate /* 313*7c478bd9Sstevel@tonic-gate * makes a lock on behalf of pid. 314*7c478bd9Sstevel@tonic-gate * input: 315*7c478bd9Sstevel@tonic-gate * pid - process id 316*7c478bd9Sstevel@tonic-gate * tempfile - name of a temporary in the same file system 317*7c478bd9Sstevel@tonic-gate * name - lock file name (full path name) 318*7c478bd9Sstevel@tonic-gate * return: 319*7c478bd9Sstevel@tonic-gate * -1 - failed 320*7c478bd9Sstevel@tonic-gate * 0 - lock made successfully 321*7c478bd9Sstevel@tonic-gate */ 322*7c478bd9Sstevel@tonic-gate static int 323*7c478bd9Sstevel@tonic-gate onelock(pid,tempfile,name) 324*7c478bd9Sstevel@tonic-gate char *pid; 325*7c478bd9Sstevel@tonic-gate char *tempfile, *name; 326*7c478bd9Sstevel@tonic-gate { 327*7c478bd9Sstevel@tonic-gate register int fd; 328*7c478bd9Sstevel@tonic-gate char cb[100]; 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate trace1(TR_onelock, 0); 331*7c478bd9Sstevel@tonic-gate fd=creat(tempfile, (mode_t) 0444); 332*7c478bd9Sstevel@tonic-gate if (fd < 0){ 333*7c478bd9Sstevel@tonic-gate (void) sprintf(cb, "%s %s %d",tempfile, name, errno); 334*7c478bd9Sstevel@tonic-gate logent("ULOCKC", cb); 335*7c478bd9Sstevel@tonic-gate if ((errno == EMFILE) || (errno == ENFILE)) 336*7c478bd9Sstevel@tonic-gate (void) unlink(tempfile); 337*7c478bd9Sstevel@tonic-gate trace1(TR_onelock, 1); 338*7c478bd9Sstevel@tonic-gate return (-1); 339*7c478bd9Sstevel@tonic-gate } 340*7c478bd9Sstevel@tonic-gate /* +1 for '\n' */ 341*7c478bd9Sstevel@tonic-gate if (write(fd, pid, SIZEOFPID+1) != (SIZEOFPID+1)) { 342*7c478bd9Sstevel@tonic-gate (void) sprintf(cb, "%s %s %d",tempfile, name, errno); 343*7c478bd9Sstevel@tonic-gate logent("ULOCKW", cb); 344*7c478bd9Sstevel@tonic-gate (void) unlink(tempfile); 345*7c478bd9Sstevel@tonic-gate return (-1); 346*7c478bd9Sstevel@tonic-gate } 347*7c478bd9Sstevel@tonic-gate (void) chmod(tempfile, (mode_t) 0444); 348*7c478bd9Sstevel@tonic-gate (void) chown(tempfile, UUCPUID, UUCPGID); 349*7c478bd9Sstevel@tonic-gate (void) close(fd); 350*7c478bd9Sstevel@tonic-gate if (link(tempfile,name)<0){ 351*7c478bd9Sstevel@tonic-gate DEBUG(4, "%s: ", strerror(errno)); 352*7c478bd9Sstevel@tonic-gate DEBUG(4, "link(%s, ", tempfile); 353*7c478bd9Sstevel@tonic-gate DEBUG(4, "%s)\n", name); 354*7c478bd9Sstevel@tonic-gate if (unlink(tempfile)< 0){ 355*7c478bd9Sstevel@tonic-gate (void) sprintf(cb, "ULK err %s %d", tempfile, errno); 356*7c478bd9Sstevel@tonic-gate logent("ULOCKLNK", cb); 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate trace1(TR_onelock, 1); 359*7c478bd9Sstevel@tonic-gate return (-1); 360*7c478bd9Sstevel@tonic-gate } 361*7c478bd9Sstevel@tonic-gate if (unlink(tempfile)<0){ 362*7c478bd9Sstevel@tonic-gate (void) sprintf(cb, "%s %d",tempfile,errno); 363*7c478bd9Sstevel@tonic-gate logent("ULOCKF", cb); 364*7c478bd9Sstevel@tonic-gate } 365*7c478bd9Sstevel@tonic-gate trace1(TR_onelock, 1); 366*7c478bd9Sstevel@tonic-gate return (0); 367*7c478bd9Sstevel@tonic-gate } 368*7c478bd9Sstevel@tonic-gate 369*7c478bd9Sstevel@tonic-gate /* 370*7c478bd9Sstevel@tonic-gate * fd_mklock(fd) - lock the device indicated by fd is possible 371*7c478bd9Sstevel@tonic-gate * 372*7c478bd9Sstevel@tonic-gate * return - 373*7c478bd9Sstevel@tonic-gate * SUCCESS - this process now has the fd locked 374*7c478bd9Sstevel@tonic-gate * FAIL - this process was not able to lock the fd 375*7c478bd9Sstevel@tonic-gate */ 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate GLOBAL int 378*7c478bd9Sstevel@tonic-gate fd_mklock(fd) 379*7c478bd9Sstevel@tonic-gate int fd; 380*7c478bd9Sstevel@tonic-gate { 381*7c478bd9Sstevel@tonic-gate int tries = 0; 382*7c478bd9Sstevel@tonic-gate struct stat64 _st_buf; 383*7c478bd9Sstevel@tonic-gate char lockname[BUFSIZ]; 384*7c478bd9Sstevel@tonic-gate 385*7c478bd9Sstevel@tonic-gate trace2(TR_fd_mklock, 0, fd); 386*7c478bd9Sstevel@tonic-gate if (_FSTAT(fd, &_st_buf) != 0) { 387*7c478bd9Sstevel@tonic-gate trace1(TR_fd_mklock, 1); 388*7c478bd9Sstevel@tonic-gate return (FAIL); 389*7c478bd9Sstevel@tonic-gate } 390*7c478bd9Sstevel@tonic-gate 391*7c478bd9Sstevel@tonic-gate (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK, 392*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_dev), 393*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_rdev), 394*7c478bd9Sstevel@tonic-gate (unsigned long) minor(_st_buf.st_rdev)); 395*7c478bd9Sstevel@tonic-gate 396*7c478bd9Sstevel@tonic-gate if (mklock(lockname) == FAIL) { 397*7c478bd9Sstevel@tonic-gate trace1(TR_fd_mklock, 1); 398*7c478bd9Sstevel@tonic-gate return (FAIL); 399*7c478bd9Sstevel@tonic-gate } 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate while (lockf(fd, F_TLOCK, 0L) != 0) { 402*7c478bd9Sstevel@tonic-gate DEBUG(7, "fd_mklock: lockf returns %d\n", errno); 403*7c478bd9Sstevel@tonic-gate if ((++tries >= MAX_LOCKTRY) || (errno != EAGAIN)) { 404*7c478bd9Sstevel@tonic-gate rmlock(lockname); 405*7c478bd9Sstevel@tonic-gate logent("fd_mklock","lockf failed"); 406*7c478bd9Sstevel@tonic-gate trace1(TR_fd_mklock, 1); 407*7c478bd9Sstevel@tonic-gate return (FAIL); 408*7c478bd9Sstevel@tonic-gate } 409*7c478bd9Sstevel@tonic-gate sleep(2); 410*7c478bd9Sstevel@tonic-gate } 411*7c478bd9Sstevel@tonic-gate DEBUG(7, "fd_mklock: ok\n%s", ""); 412*7c478bd9Sstevel@tonic-gate trace1(TR_fd_mklock, 1); 413*7c478bd9Sstevel@tonic-gate return (SUCCESS); 414*7c478bd9Sstevel@tonic-gate } 415*7c478bd9Sstevel@tonic-gate 416*7c478bd9Sstevel@tonic-gate /* 417*7c478bd9Sstevel@tonic-gate * fn_cklock(name) - determine if the device indicated by name is locked 418*7c478bd9Sstevel@tonic-gate * 419*7c478bd9Sstevel@tonic-gate * return - 420*7c478bd9Sstevel@tonic-gate * SUCCESS - the name is not locked 421*7c478bd9Sstevel@tonic-gate * FAIL - the name is locked by another process 422*7c478bd9Sstevel@tonic-gate */ 423*7c478bd9Sstevel@tonic-gate 424*7c478bd9Sstevel@tonic-gate GLOBAL int 425*7c478bd9Sstevel@tonic-gate fn_cklock(name) 426*7c478bd9Sstevel@tonic-gate char *name; 427*7c478bd9Sstevel@tonic-gate { 428*7c478bd9Sstevel@tonic-gate int dummy; 429*7c478bd9Sstevel@tonic-gate struct stat64 _st_buf; 430*7c478bd9Sstevel@tonic-gate char lockname[BUFSIZ]; 431*7c478bd9Sstevel@tonic-gate 432*7c478bd9Sstevel@tonic-gate trace1(TR_fn_cklock, 0); 433*7c478bd9Sstevel@tonic-gate /* we temporarily use lockname to hold full path name */ 434*7c478bd9Sstevel@tonic-gate (void) sprintf(lockname, "%s%s", (*name == '/' ? "" : "/dev/"), name); 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate if (_STAT(lockname, &_st_buf) != 0) { 437*7c478bd9Sstevel@tonic-gate trace1(TR_fn_cklock, 1); 438*7c478bd9Sstevel@tonic-gate return (FAIL); 439*7c478bd9Sstevel@tonic-gate } 440*7c478bd9Sstevel@tonic-gate 441*7c478bd9Sstevel@tonic-gate (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK, 442*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_dev), 443*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_rdev), 444*7c478bd9Sstevel@tonic-gate (unsigned long) minor(_st_buf.st_rdev)); 445*7c478bd9Sstevel@tonic-gate 446*7c478bd9Sstevel@tonic-gate dummy = cklock(lockname); 447*7c478bd9Sstevel@tonic-gate trace1(TR_fn_cklock, 1); 448*7c478bd9Sstevel@tonic-gate return (dummy); 449*7c478bd9Sstevel@tonic-gate } 450*7c478bd9Sstevel@tonic-gate 451*7c478bd9Sstevel@tonic-gate /* 452*7c478bd9Sstevel@tonic-gate * fd_cklock(fd) - determine if the device indicated by fd is locked 453*7c478bd9Sstevel@tonic-gate * 454*7c478bd9Sstevel@tonic-gate * return - 455*7c478bd9Sstevel@tonic-gate * SUCCESS - the fd is not locked 456*7c478bd9Sstevel@tonic-gate * FAIL - the fd is locked by another process 457*7c478bd9Sstevel@tonic-gate */ 458*7c478bd9Sstevel@tonic-gate 459*7c478bd9Sstevel@tonic-gate GLOBAL int 460*7c478bd9Sstevel@tonic-gate fd_cklock(fd) 461*7c478bd9Sstevel@tonic-gate int fd; 462*7c478bd9Sstevel@tonic-gate { 463*7c478bd9Sstevel@tonic-gate struct stat64 _st_buf; 464*7c478bd9Sstevel@tonic-gate char lockname[BUFSIZ]; 465*7c478bd9Sstevel@tonic-gate 466*7c478bd9Sstevel@tonic-gate trace2(TR_fd_cklock, 0, fd); 467*7c478bd9Sstevel@tonic-gate if (_FSTAT(fd, &_st_buf) != 0) { 468*7c478bd9Sstevel@tonic-gate trace1(TR_fd_cklock, 1); 469*7c478bd9Sstevel@tonic-gate return (FAIL); 470*7c478bd9Sstevel@tonic-gate } 471*7c478bd9Sstevel@tonic-gate 472*7c478bd9Sstevel@tonic-gate (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK, 473*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_dev), 474*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_rdev), 475*7c478bd9Sstevel@tonic-gate (unsigned long) minor(_st_buf.st_rdev)); 476*7c478bd9Sstevel@tonic-gate 477*7c478bd9Sstevel@tonic-gate if (cklock(lockname) == FAIL) { 478*7c478bd9Sstevel@tonic-gate trace1(TR_fd_cklock, 1); 479*7c478bd9Sstevel@tonic-gate return (FAIL); 480*7c478bd9Sstevel@tonic-gate } else { 481*7c478bd9Sstevel@tonic-gate trace1(TR_fd_cklock, 1); 482*7c478bd9Sstevel@tonic-gate return (lockf(fd, F_TEST, 0L)); 483*7c478bd9Sstevel@tonic-gate } 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate 486*7c478bd9Sstevel@tonic-gate /* 487*7c478bd9Sstevel@tonic-gate * remove the locks associated with the device file descriptor 488*7c478bd9Sstevel@tonic-gate * 489*7c478bd9Sstevel@tonic-gate * return - 490*7c478bd9Sstevel@tonic-gate * SUCCESS - both BNU lock file and advisory locks removed 491*7c478bd9Sstevel@tonic-gate * FAIL - 492*7c478bd9Sstevel@tonic-gate */ 493*7c478bd9Sstevel@tonic-gate 494*7c478bd9Sstevel@tonic-gate GLOBAL void 495*7c478bd9Sstevel@tonic-gate fd_rmlock(fd) 496*7c478bd9Sstevel@tonic-gate int fd; 497*7c478bd9Sstevel@tonic-gate { 498*7c478bd9Sstevel@tonic-gate struct stat64 _st_buf; 499*7c478bd9Sstevel@tonic-gate char lockname[BUFSIZ]; 500*7c478bd9Sstevel@tonic-gate 501*7c478bd9Sstevel@tonic-gate trace2(TR_fd_rmlock, 0, fd); 502*7c478bd9Sstevel@tonic-gate if (_FSTAT(fd, &_st_buf) == 0) { 503*7c478bd9Sstevel@tonic-gate (void) sprintf(lockname, "%s.%3.3lu.%3.3lu.%3.3lu", L_LOCK, 504*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_dev), 505*7c478bd9Sstevel@tonic-gate (unsigned long) major(_st_buf.st_rdev), 506*7c478bd9Sstevel@tonic-gate (unsigned long) minor(_st_buf.st_rdev)); 507*7c478bd9Sstevel@tonic-gate rmlock(lockname); 508*7c478bd9Sstevel@tonic-gate } 509*7c478bd9Sstevel@tonic-gate (void) lockf(fd, F_ULOCK, 0L); 510*7c478bd9Sstevel@tonic-gate trace1(TR_fd_rmlock, 1); 511*7c478bd9Sstevel@tonic-gate return; 512*7c478bd9Sstevel@tonic-gate } 513