xref: /freebsd/lib/libc/stdio/_flock_stub.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * POSIX stdio FILE locking functions. These assume that the locking
34  * is only required at FILE structure level, not at file descriptor
35  * level too.
36  *
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "namespace.h"
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <pthread.h>
47 #include "un-namespace.h"
48 
49 #include "local.h"
50 
51 
52 /*
53  * Weak symbols for externally visible functions in this file:
54  */
55 __weak_reference(_flockfile, flockfile);
56 __weak_reference(_flockfile_debug_stub, _flockfile_debug);
57 __weak_reference(_ftrylockfile, ftrylockfile);
58 __weak_reference(_funlockfile, funlockfile);
59 
60 void _flockfile_debug_stub(FILE *, char *, int);
61 int _ftrylockfile(FILE *);
62 
63 void
64 _flockfile(FILE *fp)
65 {
66 	pthread_t curthread = _pthread_self();
67 
68 	if (fp->_fl_owner == curthread)
69 		fp->_fl_count++;
70 	else {
71 		/*
72 		 * Make sure this mutex is treated as a private
73 		 * internal mutex:
74 		 */
75 		_pthread_mutex_lock(&fp->_fl_mutex);
76 		fp->_fl_owner = curthread;
77 		fp->_fl_count = 1;
78 	}
79 }
80 
81 /*
82  * This can be overriden by the threads library if it is linked in.
83  */
84 void
85 _flockfile_debug_stub(FILE *fp, char *fname, int lineno)
86 {
87 	_flockfile(fp);
88 }
89 
90 int
91 _ftrylockfile(FILE *fp)
92 {
93 	pthread_t curthread = _pthread_self();
94 	int	ret = 0;
95 
96 	if (fp->_fl_owner == curthread)
97 		fp->_fl_count++;
98 	/*
99 	 * Make sure this mutex is treated as a private
100 	 * internal mutex:
101 	 */
102 	else if (_pthread_mutex_trylock(&fp->_fl_mutex) == 0) {
103 		fp->_fl_owner = curthread;
104 		fp->_fl_count = 1;
105 	}
106 	else
107 		ret = -1;
108 	return (ret);
109 }
110 
111 void
112 _funlockfile(FILE *fp)
113 {
114 	pthread_t	curthread = _pthread_self();
115 
116 	/*
117 	 * Check if this file is owned by the current thread:
118 	 */
119 	if (fp->_fl_owner == curthread) {
120 		/*
121 		 * Check if this thread has locked the FILE
122 		 * more than once:
123 		 */
124 		if (fp->_fl_count > 1)
125 			/*
126 			 * Decrement the count of the number of
127 			 * times the running thread has locked this
128 			 * file:
129 			 */
130 			fp->_fl_count--;
131 		else {
132 			/*
133 			 * The running thread will release the
134 			 * lock now:
135 			 */
136 			fp->_fl_count = 0;
137 			fp->_fl_owner = NULL;
138 			_pthread_mutex_unlock(&fp->_fl_mutex);
139 		}
140 	}
141 }
142