safelock.c
/* Create a new lock file and atomically move it into place.
 *
 * Returns 0 on success, otherwise errno.
 * If another thread creates the lock file first, return success */
Create a new lock file and atomically move it into place.
Returns 0 on success, otherwise errno.
If another thread creates the lock file first, return success
Create a new lock file and atomically move it into place . Returns 0 on success otherwise errno . If another thread creates the lock file first return success
static int safelock_create(const char *filename, mode_t mode)
{
    char *filename_tmp;
    int ret, err;
    filename_tmp = safelock_create_unique(filename, mode);
    if (!filename_tmp)
        return errno;
    ret = link(filename_tmp, filename);
    err = (ret < 0 && errno != EEXIST) ? errno : 0;
    unlink(filename_tmp);
    free(filename_tmp);
    return err;
}
static int safelock_create ( const char * filename , mode_t mode ) { char * filename_tmp ; int ret , err ; filename_tmp = safelock_create_unique ( filename , mode ) ; if ( ! filename_tmp ) return errno ; ret = link ( filename_tmp , filename ) ; err = ( ret < 0 && errno != EEXIST ) ? errno : 0 ; unlink ( filename_tmp ) ; free ( filename_tmp ) ; return err ; }
Create a new lock file and atomically move it into place.
Create a new lock file and atomically move it into place .
/* Ignore EEXIST, assume another process created the lock first */ /* Unlink shouldn't fail except during very exceptional race
     * conditions:
     * - Another process removed the file
     * - Another process changed the permissions
     * - The filesystem was remounted read-only
     * - ..
     *
     * Skip error checking, if the file couldn't be unlinked, some
     * littering may occur. */