|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <sys/types.h> ! 3: #include <sys/stat.h> ! 4: #include "string.h" ! 5: #include "mail.h" ! 6: ! 7: /* ! 8: * open a file for appending. if the file doesn't exist, create it. ! 9: * return the file descriptor. ! 10: */ ! 11: appendopen(file) ! 12: char *file; ! 13: { ! 14: int out; ! 15: long lseek(); ! 16: ! 17: lock(file); ! 18: out = open(file, 2); ! 19: if (out < 0) ! 20: out = creat(file, 0666); ! 21: else ! 22: chmod(file, 0666); ! 23: lseek(out, 0L, 2); ! 24: return out; ! 25: } ! 26: ! 27: /* ! 28: * close an append only file ! 29: */ ! 30: appendclose(fd) ! 31: int fd; ! 32: { ! 33: close(fd); ! 34: unlock(); ! 35: } ! 36: ! 37: /* ! 38: * lock and open file. If the file doesn't exist and mode!=0, create it. ! 39: * return the file descriptor. ! 40: */ ! 41: ! 42: FILE * ! 43: lockopen(file, omode, pmode, uid, gid) ! 44: char *file; ! 45: char *omode; ! 46: int pmode; ! 47: { ! 48: FILE *fp; ! 49: struct stat sbuf; ! 50: int newfile; ! 51: ! 52: lock(file); ! 53: if(stat(file, &sbuf)<0) ! 54: newfile = 1; ! 55: else ! 56: newfile = 0; ! 57: fp = fopen(file, omode); ! 58: if(fp==NULL){ ! 59: unlock(); ! 60: return NULL; ! 61: } ! 62: if(newfile){ ! 63: chown(file, uid, gid); ! 64: chmod(file, pmode); ! 65: } ! 66: return fp; ! 67: } ! 68: ! 69: /* ! 70: * close and unlock a file. ! 71: */ ! 72: lockclose(fp) ! 73: FILE *fp; ! 74: { ! 75: fclose(fp); ! 76: unlock(); ! 77: } ! 78: ! 79: /* ! 80: * reopen a file without losing the lock. ! 81: */ ! 82: FILE * ! 83: lockreopen(file, omode, fp) ! 84: char *file; ! 85: char *omode; ! 86: FILE *fp; ! 87: { ! 88: return freopen(file, omode, fp); ! 89: } ! 90: ! 91: /* ! 92: * remove ALL locks ! 93: */ ! 94: cleanlocks() ! 95: { ! 96: unlock(); ! 97: } ! 98: #ifndef lint ! 99: static char *sccsid = "@(#)lock.c 1.5 88/02/08 SMI"; /* from S5R2 1.1 */ ! 100: #endif ! 101: ! 102: /* ! 103: * mailx -- a modified version of a University of California at Berkeley ! 104: * mail program ! 105: * ! 106: * Stuff to do version 7 style locking. ! 107: */ ! 108: ! 109: ! 110: char *maillock = ".lock"; /* Lock suffix for mailname */ ! 111: #ifdef USG ! 112: char *lockname = "/usr/mail/tmXXXXXX"; ! 113: #else ! 114: char *lockname = "/usr/spool/mail/tmXXXXXX"; ! 115: #endif ! 116: char locktmp[30]; /* Usable lock temporary */ ! 117: static char curlock[50]; /* Last used name of lock */ ! 118: static int locked; /* To note that we locked it */ ! 119: ! 120: /* ! 121: * Lock the specified mail file by setting the file mailfile.lock. ! 122: * We must, of course, be careful to remove the lock file by a call ! 123: * to unlock before we stop. The algorithm used here is to see if ! 124: * the lock exists, and if it does, to check its modify time. If it ! 125: * is older than 5 minutes, we assume error and set our own file. ! 126: * Otherwise, we wait for 5 seconds and try again. ! 127: */ ! 128: ! 129: lock(file) ! 130: char *file; ! 131: { ! 132: register int f; ! 133: struct stat sbuf; ! 134: long curtime; ! 135: ! 136: if (file == (char *)0) { ! 137: printf("Locked = %d\n", locked); ! 138: return(0); ! 139: } ! 140: if (locked) ! 141: return(0); ! 142: strcpy(curlock, file); ! 143: strcat(curlock, maillock); ! 144: strcpy(locktmp, lockname); ! 145: mktemp(locktmp); ! 146: remove(locktmp); ! 147: for (;;) { ! 148: f = lock1(locktmp, curlock); ! 149: if (f == 0) { ! 150: locked = 1; ! 151: return(0); ! 152: } ! 153: if (stat(curlock, &sbuf) < 0) ! 154: return(0); ! 155: time(&curtime); ! 156: if (curtime < sbuf.st_ctime + 300) { ! 157: sleep(5); ! 158: continue; ! 159: } ! 160: remove(curlock); ! 161: } ! 162: } ! 163: ! 164: /* ! 165: * Remove the mail lock, and note that we no longer ! 166: * have it locked. ! 167: */ ! 168: ! 169: unlock() ! 170: { ! 171: ! 172: remove(curlock); ! 173: locked = 0; ! 174: } ! 175: ! 176: /* ! 177: * Attempt to set the lock by creating the temporary file, ! 178: * then doing a link/unlink. If it fails, return -1 else 0 ! 179: */ ! 180: ! 181: lock1(tempfile, name) ! 182: char tempfile[], name[]; ! 183: { ! 184: register int fd; ! 185: ! 186: fd = creat(tempfile, 0); ! 187: if (fd < 0) ! 188: return(-1); ! 189: close(fd); ! 190: if (link(tempfile, name) < 0) { ! 191: remove(tempfile); ! 192: return(-1); ! 193: } ! 194: remove(tempfile); ! 195: return(0); ! 196: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.