Annotation of researchv10no/lbin/Mail/olock.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <sys/types.h>
        !             3: #include <dir.h>
        !             4: #include <sys/stat.h>          /* mjm <stat.h> --> <sys/stat.h> */
        !             5: #include "string.h"
        !             6: 
        !             7: /* imports */
        !             8: extern char *mktemp();
        !             9: extern long time();
        !            10: extern unsigned int sleep();
        !            11: 
        !            12: #define LOCKPREFIX "/tmp/L."
        !            13: static char    lockname[DIRSIZ+sizeof(LOCKPREFIX)] = { 0 };
        !            14: 
        !            15: /* Lock the given file.  The parameter "file" must contain at least one '/'. */
        !            16: extern void
        !            17: lock(file)
        !            18: char *file;
        !            19: {
        !            20: #      define TMPLNAME "/tmp/mlXXXXX"
        !            21: #      define LOCKPREFIX "/tmp/L."
        !            22:        char tmplname[sizeof(TMPLNAME)*2];
        !            23:        struct stat stbuf;
        !            24:        int fd;
        !            25: 
        !            26:        /* return if we are already in the middle of a lock */
        !            27:        if (*lockname != '\0')
        !            28:                return;
        !            29: 
        !            30:        /* create a temporary file */
        !            31:        (void)strcpy(tmplname, TMPLNAME);
        !            32:        (void)mktemp(tmplname);
        !            33:        if ((fd=creat(tmplname, 0444))<0)
        !            34:                return;
        !            35:        close(fd);
        !            36: 
        !            37:        /* Make a link to it with the lock file name.  This will fail only
        !            38:         * if it already exists.
        !            39:         */
        !            40:        (void)strcpy(lockname, LOCKPREFIX);
        !            41:        (void)strcat(lockname, strrchr(file, '/')+1);
        !            42:        lockname[DIRSIZ+sizeof("/tmp/")-1] = '\0';
        !            43:        while (link(tmplname, lockname) < 0) {
        !            44:                long now;
        !            45: 
        !            46:                /* File is already locked.  Break it if the lock is old. */
        !            47:                sleep(2);
        !            48:                now = time((long *)0);
        !            49:                if (stat(lockname, &stbuf)==0 && stbuf.st_ctime+60 < now) {
        !            50:                        if (stat(file, &stbuf)==0 && stbuf.st_mtime+60 >= now)
        !            51:                                continue;
        !            52:                        fprintf(stderr, "mail: breaking lock\n");
        !            53:                        unlink(lockname);
        !            54:                }
        !            55:        }
        !            56:        unlink(tmplname);
        !            57:        return;
        !            58: }
        !            59: 
        !            60: extern void
        !            61: unlock()
        !            62: {
        !            63:        if (*lockname != '\0') {
        !            64:                unlink(lockname);
        !            65:                *lockname = '\0';
        !            66:        }
        !            67: }
        !            68: 

unix.superglobalmegacorp.com

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