"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/lock.c" (12 Oct 2016, 6663 Bytes) of package /linux/misc/tin-2.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lock.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : lock.c
    4  *  Author    : Urs Janssen <urs@tin.org>
    5  *  Created   : 1998-07-27
    6  *  Updated   : 2013-11-17
    7  *  Notes     :
    8  *
    9  * Copyright (c) 1998-2017 Urs Janssen <urs@tin.org>
   10  * All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. The name of the author may not be used to endorse or promote
   21  *    products derived from this software without specific prior written
   22  *    permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS
   25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   28  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   30  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35  */
   36 
   37 #ifndef TIN_H
   38 #   include "tin.h"
   39 #endif /* !TIN_H */
   40 
   41 #if !defined(USE_FLOCK) && !defined(USE_LOCKF) && !defined(USE_FCNTL)
   42 #   ifdef HAVE_FCNTL
   43 #       define USE_FCNTL 1
   44 #   else
   45 #       ifdef HAVE_LOCKF
   46 #           define USE_LOCKF 1
   47 #       else
   48 #           ifdef HAVE_FLOCK
   49 #               define USE_FLOCK 1
   50 #           endif /* HAVE_FLOCK */
   51 #       endif /* HAVE_LOCKF */
   52 #   endif /* HAVE_FCNTL */
   53 #endif /* !USE_FLOCK && !USE_LOCKF && !USE_FCNTL */
   54 
   55 /*
   56  * TODO: add support for $LOCKEXT
   57  */
   58 #define LOCK_SUFFIX ".lock"
   59 
   60 /*
   61  * fd_lock(fd, block)
   62  *
   63  * try to lock a file descriptor with fcntl(), flock() or lockf()
   64  *
   65  * return codes:
   66  *  0 = file locked successfully
   67  * -1 = some error occurred
   68  */
   69 int
   70 fd_lock(
   71     int fd,
   72     t_bool block)
   73 {
   74     int rval = -1; /* assume an error */
   75 
   76 #ifdef USE_FCNTL
   77     struct flock flk;
   78 
   79     flk.l_type = F_WRLCK;
   80     flk.l_whence = SEEK_SET;
   81     flk.l_start = 0;
   82     flk.l_len = 0;
   83     if ((rval = fcntl(fd, block ? F_SETLKW : F_SETLK, &flk)))
   84         return rval; /* fcntl locking failed */
   85 #else
   86 #   ifdef USE_LOCKF
   87     if ((rval = lockf(fd, block ? F_LOCK : F_TLOCK, 0L)))
   88         return rval; /* lockf locking failed */
   89 #   else
   90 #       ifdef USE_FLOCK
   91     if ((rval = flock(fd, block ? LOCK_EX : (LOCK_EX | LOCK_NB))))
   92         return rval; /* flock locking failed */
   93 #       endif /* USE_FLOCK */
   94 #   endif /* USE_LOCKF */
   95 #endif /* USE_FCNTL */
   96 
   97     return rval;    /* available lock successfully applied or no locking available */
   98 }
   99 
  100 
  101 #if 0 /* unused */
  102 /*
  103  * test_fd_lock(fd)
  104  *
  105  * check for an existing lock on file descriptor with fcntl(), lockf()
  106  * or flock()
  107  *
  108  * return codes:
  109  *  0 = file is not locked
  110  *  1 = file is locked
  111  * -1 = some error occurred
  112  */
  113 int
  114 test_fd_lock(
  115     int fd)
  116 {
  117     int rval = -1; /* assume an error */
  118 
  119     errno = 0;
  120 #ifdef USE_FCNTL
  121     {
  122         struct flock flk;
  123 
  124         flk.l_type = F_WRLCK;
  125         flk.l_whence = SEEK_SET;
  126         flk.l_start = 0;
  127         flk.l_len = 0;
  128         if (fcntl(fd, F_GETLK, &flk) < 0)
  129             return -1; /* some error occurred */
  130         else {
  131             if (flk.l_type != F_UNLCK)
  132                 return 1;   /* file is locked */
  133             else
  134                 rval = 0; /* file is not fcntl locked */
  135         }
  136     }
  137 #else
  138 #   ifdef USE_LOCKF
  139     if (lockf(fd, F_TEST, 0L)) {
  140         if (errno == EACCES)
  141             return 1;   /* file is locked */
  142         else
  143             return -1;  /* some error occurred */
  144     } else
  145         rval = 0;   /* file is not lockf locked */
  146 #   else
  147 #       ifdef USE_FLOCK
  148     if (flock(fd, (LOCK_EX|LOCK_NB))) {
  149         if (errno == EWOULDBLOCK)
  150             return 1;   /* file is locked */
  151         else
  152             return -1;  /* some error occurred */
  153     } else
  154         rval = 0; /* file is not flock locked */
  155 
  156 #       endif /* USE_FLOCK */
  157 #   endif /* USE_LOCKF */
  158 #endif /* USE_FCNTL */
  159 
  160     return rval;    /* file wasn't locked or no locking available */
  161 }
  162 #endif /* 0 */
  163 
  164 
  165 /*
  166  * fd_unlock(fd)
  167  *
  168  * try to unlock a file descriptor with fcntl(), lockf() or flock()
  169  *
  170  * return codes:
  171  *  0 = file unlocked successfully
  172  * -1 = some error occurred
  173  */
  174 int
  175 fd_unlock(
  176     int fd)
  177 {
  178     int rval = -1; /* assume an error */
  179 
  180 #ifdef USE_FCNTL
  181     {
  182         struct flock flk;
  183 
  184         flk.l_type = F_UNLCK;
  185         flk.l_whence = SEEK_SET;
  186         flk.l_start = 0;
  187         flk.l_len = 0;
  188         if ((rval = fcntl(fd, F_SETLK, &flk)))
  189             return rval; /* couldn't release fcntl lock */
  190     }
  191 #else
  192 #   ifdef USE_LOCKF
  193     if ((rval = lockf(fd, F_ULOCK, 0L)))
  194         return rval; /* couldn't release lockf lock */
  195 #   else
  196 #       ifdef USE_FLOCK
  197     if ((rval = flock(fd, LOCK_UN)))
  198         return rval; /* couldn't release flock lock */
  199 #       endif /* USE_FLOCK */
  200 #   endif /* USE_LOCKF */
  201 #endif /* USE_FCNTL */
  202 
  203     return rval;    /* file successfully unlocked or no locking available */
  204 }
  205 
  206 
  207 /*
  208  * dot_lock(filename)
  209  *
  210  * try to lock filename via dotfile locking
  211  *
  212  * return codes:
  213  *  TRUE  = file locked successfully
  214  *  FALSE = some error occurred
  215  */
  216 t_bool
  217 dot_lock(
  218     const char *filename)
  219 {
  220     char tempfile[PATH_LEN];
  221     char lockfile[PATH_LEN];
  222     char base_dir[PATH_LEN];
  223     int dot_fd;
  224     struct stat statbuf;
  225     t_bool rval = FALSE;
  226 
  227     dir_name(filename, base_dir);
  228     if (!strcmp(filename, base_dir)) /* no filename portion */
  229         return rval;
  230     if ((dot_fd = my_tmpfile(tempfile, sizeof(tempfile) - 1, base_dir)) == -1)
  231         return rval;
  232     snprintf(lockfile, sizeof(lockfile), "%s%s", filename, LOCK_SUFFIX);
  233 
  234 #ifdef HAVE_LINK
  235     if (stat(lockfile, &statbuf)) { /* lockfile doesn't exist */
  236         if (!link(tempfile, lockfile)) {            /* link successful */
  237             if (!stat(tempfile, &statbuf)) {    /* tempfile exist */
  238                 if (statbuf.st_nlink == 2)          /* link count ok */
  239                     rval = TRUE;
  240             }
  241         }
  242     }
  243 #endif /* HAVE_LINK */
  244 
  245     close(dot_fd);
  246     (void) unlink(tempfile);
  247 
  248     if (!stat(lockfile, &statbuf)) {            /* lockfile still here */
  249         if (statbuf.st_nlink != 1)                  /* link count wrong? */
  250             rval = FALSE;                               /* shouldn't happen */
  251     }
  252     return rval;
  253 }
  254 
  255 
  256 /*
  257  * try to remove a dotlock for filename
  258  *
  259  * return codes:
  260  *  TRUE  = file unlocked successfully
  261  *  FALSE = some error occurred
  262  */
  263 t_bool
  264 dot_unlock(
  265     const char *filename)
  266 {
  267     char *lockfile;
  268     t_bool rval = FALSE;
  269 
  270     lockfile = my_malloc(strlen(filename) + strlen(LOCK_SUFFIX) + 2);
  271     strcpy(lockfile, filename);
  272     strcat(lockfile, LOCK_SUFFIX);
  273     if (!unlink(lockfile))
  274         rval = TRUE;
  275     free(lockfile);
  276     return rval;
  277 }