Annotation of 3BSD/cmd/uucp/ulockf.c, revision 1.1

1.1     ! root        1:        /*  @(#)ulockf  2.3  5/18/79  11:52:11  */
        !             2: #include "uucp.h"
        !             3: #include <sys/types.h>
        !             4: #include <sys/stat.h>
        !             5: 
        !             6: char Sulockf[] = "@(#)ulockf   2.3";
        !             7: 
        !             8: 
        !             9: /*******
        !            10:  *     ulockf(file, atime)
        !            11:  *     char *file;
        !            12:  *     time_t atime;
        !            13:  *
        !            14:  *     ulockf  -  this routine will create a lock file (file).
        !            15:  *     If one already exists, the create time is checked for
        !            16:  *     older than the age time (atime).
        !            17:  *     If it is older, an attempt will be made to unlink it
        !            18:  *     and create a new one.
        !            19:  *
        !            20:  *     return codes:  0  |  FAIL
        !            21:  */
        !            22: 
        !            23: ulockf(file, atime)
        !            24: char *file;
        !            25: time_t atime;
        !            26: {
        !            27:        struct stat stbuf;
        !            28:        time_t ptime;
        !            29:        int ret;
        !            30:        static int pid = -1;
        !            31:        static char tempfile[NAMESIZE];
        !            32: 
        !            33:        if (pid < 0) {
        !            34:                pid = getpid();
        !            35:                sprintf(tempfile, "LTMP.%d", pid);
        !            36:        }
        !            37:        if (onelock(pid, tempfile, file) == -1) {
        !            38:                /* lock file exists */
        !            39:                /* get status to check age of the lock file */
        !            40:                ret = stat(file, &stbuf);
        !            41:                if (ret != -1) {
        !            42:                        time(&ptime);
        !            43:                        if ((ptime - stbuf.st_ctime) < atime) {
        !            44:                                /* file not old enough to delete */
        !            45:                                return(FAIL);
        !            46:                        }
        !            47:                }
        !            48:                ret = unlink(file);
        !            49:                ret = onelock(pid, tempfile, file);
        !            50:                if (ret != 0)
        !            51:                        return(FAIL);
        !            52:        }
        !            53:        stlock(file);
        !            54:        return(0);
        !            55: }
        !            56: 
        !            57: 
        !            58: #define MAXLOCKS 10    /* maximum number of lock files */
        !            59: char *Lockfile[MAXLOCKS];
        !            60: int Nlocks = 0;
        !            61: 
        !            62: /***
        !            63:  *     stlock(name)    put name in list of lock files
        !            64:  *     char *name;
        !            65:  *
        !            66:  *     return codes:  none
        !            67:  */
        !            68: 
        !            69: stlock(name)
        !            70: char *name;
        !            71: {
        !            72:        char *p;
        !            73:        extern char *calloc();
        !            74:        int i;
        !            75: 
        !            76:        for (i = 0; i < Nlocks; i++) {
        !            77:                if (Lockfile[i] == NULL)
        !            78:                        break;
        !            79:        }
        !            80:        ASSERT(i < MAXLOCKS, "TOO MANY LOCKS %d", i);
        !            81:        if (i >= Nlocks)
        !            82:                i = Nlocks++;
        !            83:        p = calloc(strlen(name) + 1, sizeof (char));
        !            84:        ASSERT(p != NULL, "CAN NOT ALLOCATE FOR %s", name);
        !            85:        strcpy(p, name);
        !            86:        Lockfile[i] = p;
        !            87:        return;
        !            88: }
        !            89: 
        !            90: 
        !            91: /***
        !            92:  *     rmlock(name)    remove all lock files in list
        !            93:  *     char *name;     or name
        !            94:  *
        !            95:  *     return codes: none
        !            96:  */
        !            97: 
        !            98: rmlock(name)
        !            99: char *name;
        !           100: {
        !           101:        int i;
        !           102: 
        !           103:        for (i = 0; i < Nlocks; i++) {
        !           104:                if (Lockfile[i] == NULL)
        !           105:                        continue;
        !           106:                if (name == NULL
        !           107:                || strcmp(name, Lockfile[i]) == SAME) {
        !           108:                        unlink(Lockfile[i]);
        !           109:                        free(Lockfile[i]);
        !           110:                        Lockfile[i] = NULL;
        !           111:                }
        !           112:        }
        !           113:        return;
        !           114: }
        !           115: 
        !           116: 
        !           117: /*  this stuff from pjw  */
        !           118: /*  /usr/pjw/bin/recover - check pids to remove unnecessary locks */
        !           119: /*     isalock(name) returns 0 if the name is a lock */
        !           120: /*     unlock(name)  unlocks name if it is a lock*/
        !           121: /*     onelock(pid,tempfile,name) makes lock a name
        !           122:        on behalf of pid.  Tempfile must be in the same
        !           123:        file system as name. */
        !           124: /*     lock(pid,tempfile,names) either locks all the
        !           125:        names or none of them */
        !           126: isalock(name) char *name;
        !           127: {
        !           128:        struct stat xstat;
        !           129:        if(stat(name,&xstat)<0) return(0);
        !           130:        if(xstat.st_size!=sizeof(int)) return(0);
        !           131:        return(1);
        !           132: }
        !           133: unlock(name) char *name;
        !           134: {
        !           135:        if(isalock(name)) return(unlink(name));
        !           136:        else return(-1);
        !           137: }
        !           138: onelock(pid,tempfile,name) char *tempfile,*name;
        !           139: {      int fd;
        !           140:        fd=creat(tempfile,0444);
        !           141:        if(fd<0) return(-1);
        !           142:        write(fd,&pid,sizeof(int));
        !           143:        close(fd);
        !           144:        if(link(tempfile,name)<0)
        !           145:        {       unlink(tempfile);
        !           146:                return(-1);
        !           147:        }
        !           148:        unlink(tempfile);
        !           149:        return(0);
        !           150: }
        !           151: lock(pid,tempfile,names) char *tempfile,**names;
        !           152: {      int i,j;
        !           153:        for(i=0;names[i]!=0;i++)
        !           154:        {       if(onelock(pid,tempfile,names[i])==0) continue;
        !           155:                for(j=0;j<i;j++) unlink(names[j]);
        !           156:                return(-1);
        !           157:        }
        !           158:        return(0);
        !           159: }
        !           160: 
        !           161: #define LOCKPRE "LCK."
        !           162: 
        !           163: /***
        !           164:  *     delock(s)       remove a lock file
        !           165:  *     char *s;
        !           166:  *
        !           167:  *     return codes:  0  |  FAIL
        !           168:  */
        !           169: 
        !           170: delock(s)
        !           171: char *s;
        !           172: {
        !           173:        char ln[30];
        !           174: 
        !           175:        sprintf(ln, "%s.%s", LOCKPRE, s);
        !           176:        rmlock(ln);
        !           177: }
        !           178: 
        !           179: 
        !           180: /***
        !           181:  *     mlock(sys)      create system lock
        !           182:  *     char *sys;
        !           183:  *
        !           184:  *     return codes:  0  |  FAIL
        !           185:  */
        !           186: 
        !           187: mlock(sys)
        !           188: char *sys;
        !           189: {
        !           190:        char lname[30];
        !           191:        sprintf(lname, "%s.%s", LOCKPRE, sys);
        !           192:        return(ulockf(lname, (time_t) 5400 ) < 0 ? FAIL : 0);
        !           193: }
        !           194: 
        !           195: 
        !           196: 
        !           197: /***
        !           198:  *     ultouch()       update access and modify times for lock files
        !           199:  *
        !           200:  *     return code - none
        !           201:  */
        !           202: 
        !           203: ultouch()
        !           204: {
        !           205:        time_t time(), ptime;
        !           206:        int i;
        !           207:        struct ut {
        !           208:                time_t actime;
        !           209:                time_t modtime;
        !           210:        } ut;
        !           211: 
        !           212:        ut.actime = time(&ut.modtime);
        !           213:        for (i = 0; i < Nlocks; i++) {
        !           214:                if (Lockfile[i] == NULL)
        !           215:                        continue;
        !           216:                utime(Lockfile[i], &ut);
        !           217:        }
        !           218:        return;
        !           219: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.