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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* 27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999, by Sun Microsystems, Inc. 28*7c478bd9Sstevel@tonic-gate * All rights reserved. 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include "synonyms.h" 35*7c478bd9Sstevel@tonic-gate #include "maillock.h" 36*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 37*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 38*7c478bd9Sstevel@tonic-gate #include <stdio.h> 39*7c478bd9Sstevel@tonic-gate #include <string.h> 40*7c478bd9Sstevel@tonic-gate #include <unistd.h> 41*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 42*7c478bd9Sstevel@tonic-gate #include <utime.h> 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate static char *lockext = ".lock"; /* Lock suffix for mailname */ 47*7c478bd9Sstevel@tonic-gate static char curlock[PATHSIZE]; /* Last used name of lock */ 48*7c478bd9Sstevel@tonic-gate static int locked; /* To note that we locked it */ 49*7c478bd9Sstevel@tonic-gate static time_t locktime; /* time lock file was touched */ 50*7c478bd9Sstevel@tonic-gate static time_t lock1(char *, char *); 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate /* 53*7c478bd9Sstevel@tonic-gate * Lock the specified mail file by setting the file mailfile.lock. 54*7c478bd9Sstevel@tonic-gate * We must, of course, be careful to remove the lock file by a call 55*7c478bd9Sstevel@tonic-gate * to unlock before we stop. The algorithm used here is to see if 56*7c478bd9Sstevel@tonic-gate * the lock exists, and if it does, to check its modify time. If it 57*7c478bd9Sstevel@tonic-gate * is older than 5 minutes, we assume error and set our own file. 58*7c478bd9Sstevel@tonic-gate * Otherwise, we wait for 5 seconds and try again. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 62*7c478bd9Sstevel@tonic-gate int 63*7c478bd9Sstevel@tonic-gate maillock(char *user, int retrycnt) 64*7c478bd9Sstevel@tonic-gate { 65*7c478bd9Sstevel@tonic-gate time_t t; 66*7c478bd9Sstevel@tonic-gate struct stat sbuf; 67*7c478bd9Sstevel@tonic-gate int statfailed; 68*7c478bd9Sstevel@tonic-gate char locktmp[PATHSIZE]; /* Usable lock temporary */ 69*7c478bd9Sstevel@tonic-gate char file[PATHSIZE]; 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate if (locked) 72*7c478bd9Sstevel@tonic-gate return (0); 73*7c478bd9Sstevel@tonic-gate (void) strcpy(file, MAILDIR); 74*7c478bd9Sstevel@tonic-gate (void) strcat(file, user); 75*7c478bd9Sstevel@tonic-gate (void) strcpy(curlock, file); 76*7c478bd9Sstevel@tonic-gate (void) strcat(curlock, lockext); 77*7c478bd9Sstevel@tonic-gate (void) strcpy(locktmp, file); 78*7c478bd9Sstevel@tonic-gate (void) strcat(locktmp, "XXXXXX"); 79*7c478bd9Sstevel@tonic-gate (void) mktemp(locktmp); 80*7c478bd9Sstevel@tonic-gate (void) remove(locktmp); 81*7c478bd9Sstevel@tonic-gate statfailed = 0; 82*7c478bd9Sstevel@tonic-gate for (;;) { 83*7c478bd9Sstevel@tonic-gate t = lock1(locktmp, curlock); 84*7c478bd9Sstevel@tonic-gate if (t == (time_t)0) { 85*7c478bd9Sstevel@tonic-gate locked = 1; 86*7c478bd9Sstevel@tonic-gate locktime = time(0); 87*7c478bd9Sstevel@tonic-gate return (0); 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate if (stat(curlock, &sbuf) < 0) { 90*7c478bd9Sstevel@tonic-gate if (statfailed++ > 5) 91*7c478bd9Sstevel@tonic-gate return (-1); 92*7c478bd9Sstevel@tonic-gate (void) sleep(5); 93*7c478bd9Sstevel@tonic-gate continue; 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate statfailed = 0; 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate /* 98*7c478bd9Sstevel@tonic-gate * Compare the time of the temp file with the time 99*7c478bd9Sstevel@tonic-gate * of the lock file, rather than with the current 100*7c478bd9Sstevel@tonic-gate * time of day, since the files may reside on 101*7c478bd9Sstevel@tonic-gate * another machine whose time of day differs from 102*7c478bd9Sstevel@tonic-gate * ours. If the lock file is less than 5 minutes 103*7c478bd9Sstevel@tonic-gate * old, keep trying. 104*7c478bd9Sstevel@tonic-gate */ 105*7c478bd9Sstevel@tonic-gate if (t < sbuf.st_ctime + 300) { 106*7c478bd9Sstevel@tonic-gate (void) sleep(5); 107*7c478bd9Sstevel@tonic-gate continue; 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate (void) remove(curlock); 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * Remove the mail lock, and note that we no longer 115*7c478bd9Sstevel@tonic-gate * have it locked. 116*7c478bd9Sstevel@tonic-gate */ 117*7c478bd9Sstevel@tonic-gate void 118*7c478bd9Sstevel@tonic-gate mailunlock(void) 119*7c478bd9Sstevel@tonic-gate { 120*7c478bd9Sstevel@tonic-gate (void) remove(curlock); 121*7c478bd9Sstevel@tonic-gate locked = 0; 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* 125*7c478bd9Sstevel@tonic-gate * Attempt to set the lock by creating the temporary file, 126*7c478bd9Sstevel@tonic-gate * then doing a link/unlink. If it succeeds, return 0, 127*7c478bd9Sstevel@tonic-gate * else return a guess of the current time on the machine 128*7c478bd9Sstevel@tonic-gate * holding the file. 129*7c478bd9Sstevel@tonic-gate */ 130*7c478bd9Sstevel@tonic-gate static time_t 131*7c478bd9Sstevel@tonic-gate lock1(char tempfile[], char name[]) 132*7c478bd9Sstevel@tonic-gate { 133*7c478bd9Sstevel@tonic-gate int fd; 134*7c478bd9Sstevel@tonic-gate struct stat sbuf; 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate fd = open(tempfile, O_RDWR|O_CREAT|O_EXCL, 0600); 137*7c478bd9Sstevel@tonic-gate if (fd < 0) 138*7c478bd9Sstevel@tonic-gate return (time(0)); 139*7c478bd9Sstevel@tonic-gate (void) fstat(fd, &sbuf); 140*7c478bd9Sstevel@tonic-gate /* 141*7c478bd9Sstevel@tonic-gate * Write the string "0" into the lock file to give us some 142*7c478bd9Sstevel@tonic-gate * interoperability with SVR4 mailers. SVR4 mailers expect 143*7c478bd9Sstevel@tonic-gate * a process ID to be written into the lock file and then 144*7c478bd9Sstevel@tonic-gate * use kill() to see if the process is alive or not. We write 145*7c478bd9Sstevel@tonic-gate * 0 into it so that SVR4 mailers will always think our lock file 146*7c478bd9Sstevel@tonic-gate * is valid. 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate (void) write(fd, "0", 2); 149*7c478bd9Sstevel@tonic-gate (void) close(fd); 150*7c478bd9Sstevel@tonic-gate if (link(tempfile, name) < 0) { 151*7c478bd9Sstevel@tonic-gate (void) remove(tempfile); 152*7c478bd9Sstevel@tonic-gate return (sbuf.st_ctime); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate (void) remove(tempfile); 155*7c478bd9Sstevel@tonic-gate return ((time_t)0); 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate /* 159*7c478bd9Sstevel@tonic-gate * Update the change time on the lock file so 160*7c478bd9Sstevel@tonic-gate * others will know we're still using it. 161*7c478bd9Sstevel@tonic-gate */ 162*7c478bd9Sstevel@tonic-gate void 163*7c478bd9Sstevel@tonic-gate touchlock(void) 164*7c478bd9Sstevel@tonic-gate { 165*7c478bd9Sstevel@tonic-gate struct stat sbuf; 166*7c478bd9Sstevel@tonic-gate time_t t; 167*7c478bd9Sstevel@tonic-gate struct utimbuf tp; 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate if (!locked) 170*7c478bd9Sstevel@tonic-gate return; 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate /* if it hasn't been at least 3 minutes, don't bother */ 173*7c478bd9Sstevel@tonic-gate if (time(&t) < locktime + 180) 174*7c478bd9Sstevel@tonic-gate return; 175*7c478bd9Sstevel@tonic-gate locktime = t; 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate if (stat(curlock, &sbuf) < 0) 178*7c478bd9Sstevel@tonic-gate return; 179*7c478bd9Sstevel@tonic-gate /* 180*7c478bd9Sstevel@tonic-gate * Don't actually change the times, we just want the 181*7c478bd9Sstevel@tonic-gate * side effect that utime causes st_ctime to be set 182*7c478bd9Sstevel@tonic-gate * to the current time. 183*7c478bd9Sstevel@tonic-gate */ 184*7c478bd9Sstevel@tonic-gate tp.actime = sbuf.st_atime; 185*7c478bd9Sstevel@tonic-gate tp.modtime = sbuf.st_mtime; 186*7c478bd9Sstevel@tonic-gate (void) utime(curlock, &tp); 187*7c478bd9Sstevel@tonic-gate } 188