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