/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1998 John Birrell <[email protected]>.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the author nor the names of any co-contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* POSIX stdio FILE locking functions. These assume that the locking33* is only required at FILE structure level, not at file descriptor34* level too.35*36*/3738#include "namespace.h"39#include <stdio.h>40#include <stdlib.h>41#include <string.h>42#include <pthread.h>43#include "un-namespace.h"4445#include "local.h"464748/*49* Weak symbols for externally visible functions in this file:50*/51__weak_reference(_flockfile, flockfile);52__weak_reference(_flockfile_debug_stub, _flockfile_debug);53__weak_reference(_ftrylockfile, ftrylockfile);54__weak_reference(_funlockfile, funlockfile);5556void _flockfile_debug_stub(FILE *, char *, int);57int _ftrylockfile(FILE *);5859void60_flockfile(FILE *fp)61{62pthread_t curthread = _pthread_self();6364if (fp->_fl_owner == curthread)65fp->_fl_count++;66else {67/*68* Make sure this mutex is treated as a private69* internal mutex:70*/71_pthread_mutex_lock(&fp->_fl_mutex);72fp->_fl_owner = curthread;73fp->_fl_count = 1;74}75}7677/*78* This can be overriden by the threads library if it is linked in.79*/80void81_flockfile_debug_stub(FILE *fp, char *fname, int lineno)82{83_flockfile(fp);84}8586int87_ftrylockfile(FILE *fp)88{89pthread_t curthread = _pthread_self();90int ret = 0;9192if (fp->_fl_owner == curthread)93fp->_fl_count++;94/*95* Make sure this mutex is treated as a private96* internal mutex:97*/98else if (_pthread_mutex_trylock(&fp->_fl_mutex) == 0) {99fp->_fl_owner = curthread;100fp->_fl_count = 1;101}102else103ret = -1;104return (ret);105}106107void108_funlockfile(FILE *fp)109{110pthread_t curthread = _pthread_self();111112/*113* Check if this file is owned by the current thread:114*/115if (fp->_fl_owner == curthread) {116/*117* Check if this thread has locked the FILE118* more than once:119*/120if (fp->_fl_count > 1)121/*122* Decrement the count of the number of123* times the running thread has locked this124* file:125*/126fp->_fl_count--;127else {128/*129* The running thread will release the130* lock now:131*/132fp->_fl_count = 0;133fp->_fl_owner = NULL;134_pthread_mutex_unlock(&fp->_fl_mutex);135}136}137}138139140