xref: /freebsd/lib/libc/stdio/_flock_stub.c (revision 1da7f3f6f72b2245e458fc7195733268ae4a1136)
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 "namespace.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <pthread.h>
44 #include "un-namespace.h"
45 
46 #include "local.h"
47 
48 
49 /*
50  * Weak symbols for externally visible functions in this file:
51  */
52 __weak_reference(_flockfile, flockfile);
53 __weak_reference(_flockfile_debug_stub, _flockfile_debug);
54 __weak_reference(_ftrylockfile, ftrylockfile);
55 __weak_reference(_funlockfile, funlockfile);
56 
57 void _flockfile_debug_stub(FILE *, char *, int);
58 int _ftrylockfile(FILE *);
59 
60 void
61 _flockfile(FILE *fp)
62 {
63 	pthread_t curthread = _pthread_self();
64 
65 	if (fp->_fl_owner == curthread)
66 		fp->_fl_count++;
67 	else {
68 		/*
69 		 * Make sure this mutex is treated as a private
70 		 * internal mutex:
71 		 */
72 		_pthread_mutex_lock(&fp->_fl_mutex);
73 		fp->_fl_owner = curthread;
74 		fp->_fl_count = 1;
75 	}
76 }
77 
78 /*
79  * This can be overriden by the threads library if it is linked in.
80  */
81 void
82 _flockfile_debug_stub(FILE *fp, char *fname, int lineno)
83 {
84 	_flockfile(fp);
85 }
86 
87 int
88 _ftrylockfile(FILE *fp)
89 {
90 	pthread_t curthread = _pthread_self();
91 	int	ret = 0;
92 
93 	if (fp->_fl_owner == curthread)
94 		fp->_fl_count++;
95 	/*
96 	 * Make sure this mutex is treated as a private
97 	 * internal mutex:
98 	 */
99 	else if (_pthread_mutex_trylock(&fp->_fl_mutex) == 0) {
100 		fp->_fl_owner = curthread;
101 		fp->_fl_count = 1;
102 	}
103 	else
104 		ret = -1;
105 	return (ret);
106 }
107 
108 void
109 _funlockfile(FILE *fp)
110 {
111 	pthread_t	curthread = _pthread_self();
112 
113 	/*
114 	 * Check if this file is owned by the current thread:
115 	 */
116 	if (fp->_fl_owner == curthread) {
117 		/*
118 		 * Check if this thread has locked the FILE
119 		 * more than once:
120 		 */
121 		if (fp->_fl_count > 1)
122 			/*
123 			 * Decrement the count of the number of
124 			 * times the running thread has locked this
125 			 * file:
126 			 */
127 			fp->_fl_count--;
128 		else {
129 			/*
130 			 * The running thread will release the
131 			 * lock now:
132 			 */
133 			fp->_fl_count = 0;
134 			fp->_fl_owner = NULL;
135 			_pthread_mutex_unlock(&fp->_fl_mutex);
136 		}
137 	}
138 }
139