filelock-os2.cpp revision 4eb60291a3736df8e5c706848a0c5dfa75a5814f
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync/* $Id$ */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - File Locking, OS/2.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2008 Oracle Corporation
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync *
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * available from http://www.virtualbox.org. This file is free software;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * General Public License (GPL) as published by the Free Software
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync *
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * The contents of this file may alternatively be used under the terms
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * of the Common Development and Distribution License Version 1.0
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * CDDL are applicable instead of those of the GPL.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync *
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * You may elect to license modified versions of this file under the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * terms and conditions of either the GPL or the CDDL or both.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync/*******************************************************************************
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync* Header Files *
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync*******************************************************************************/
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#define LOG_GROUP RTLOGGROUP_FILE
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <errno.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <sys/types.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <sys/ioctl.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <sys/fcntl.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <fcntl.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <unistd.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <sys/time.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <iprt/file.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <iprt/assert.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <iprt/string.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <iprt/err.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include <iprt/log.h>
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include "internal/file.h"
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync#include "internal/fs.h"
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsyncRTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync{
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync Assert(offLock >= 0);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Check arguments. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (fLock & ~RTFILE_LOCK_MASK)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync {
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_INVALID_PARAMETER;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync }
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /*
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * Validate offset.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if ( sizeof(off_t) < sizeof(cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync && ( (offLock >> 32) != 0
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync || (cbLock >> 32) != 0
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync || ((offLock + cbLock) >> 32) != 0))
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync {
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_NOT_SUPPORTED;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync }
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Prepare flock structure. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync struct flock fl;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync Assert(RTFILE_LOCK_WRITE);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_whence = SEEK_SET;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_start = (off_t)offLock;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_len = (off_t)cbLock;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_pid = 0;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync Assert(RTFILE_LOCK_WAIT);
4eb60291a3736df8e5c706848a0c5dfa75a5814fvboxsync if (fcntl(RTFileToNative(File), (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VINF_SUCCESS;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync int iErr = errno;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if ( iErr == EAGAIN
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync || iErr == EACCES)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_FILE_LOCK_VIOLATION;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return RTErrConvertFromErrno(iErr);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync}
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsyncRTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync{
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /** @todo copied from ../win/fileio-win.cpp for now but a proper solution
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * would probably be to modify kLIBC so that __fcntl_locking() first
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * assumes a change lock request is made (e.g. the same region was
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * previously F_RDLCK'ed and now needs to be F_WRLCK'ed or vice versa) and
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * tries to use atomic locking, and only if it fails, it does the regular
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * lock procedure. The alternative is to use DosSetFileLocks directly here
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * which basically means copy-pasting the __fcntl_locking() source
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * code :) Note that the first attempt to call RTFileLock() below assumes
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * that kLIBC is patched as described above one day and gives it a chance;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * on failure, we fall back to the Win-like unlock-then-lock approach. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync int rc = RTFileLock(File, fLock, offLock, cbLock);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (RT_FAILURE(rc) && rc != VERR_FILE_LOCK_VIOLATION)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return rc;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Check arguments. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (fLock & ~RTFILE_LOCK_MASK)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync {
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_INVALID_PARAMETER;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync }
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Remove old lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync rc = RTFileUnlock(File, offLock, cbLock);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (RT_FAILURE(rc))
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return rc;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Set new lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync rc = RTFileLock(File, fLock, offLock, cbLock);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (RT_SUCCESS(rc))
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return rc;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Try to restore old lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync rc = RTFileLock(File, fLockOld, offLock, cbLock);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (RT_SUCCESS(rc))
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_FILE_LOCK_VIOLATION;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync else
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_FILE_LOCK_LOST;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync}
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsyncRTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync{
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync Assert(offLock >= 0);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /*
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * Validate offset.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if ( sizeof(off_t) < sizeof(cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync && ( (offLock >> 32) != 0
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync || (cbLock >> 32) != 0
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync || ((offLock + cbLock) >> 32) != 0))
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync {
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_NOT_SUPPORTED;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync }
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Prepare flock structure. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync struct flock fl;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_type = F_UNLCK;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_whence = SEEK_SET;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_start = (off_t)offLock;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_len = (off_t)cbLock;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_pid = 0;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
4eb60291a3736df8e5c706848a0c5dfa75a5814fvboxsync if (fcntl(RTFileToNative(File), F_SETLK, &fl) >= 0)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VINF_SUCCESS;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /** @todo check error codes for non existing lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync int iErr = errno;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if ( iErr == EAGAIN
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync || iErr == EACCES)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return VERR_FILE_LOCK_VIOLATION;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync return RTErrConvertFromErrno(iErr);
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync}
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync