xref: /titanic_41/usr/src/lib/libc/port/stdio/flockf.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #pragma weak flockfile = _flockfile
33 #pragma weak ftrylockfile = _ftrylockfile
34 #pragma weak funlockfile = _funlockfile
35 
36 #include "synonyms.h"
37 #include "mtlib.h"
38 
39 #define	_iob	__iob
40 
41 #include "file64.h"
42 #include <stdio.h>
43 #include <thread.h>
44 #include <synch.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <stdio_ext.h>
48 #include "stdiom.h"
49 
50 /*
51  * The rmutex_lock/unlock routines are only called (for stdio FILE
52  * locking in libc) by _flockget, _flockrel, flockfile, ftrylockfile,
53  * and funlockfile.  _flockget and _flockrel are only called by the
54  * FLOCKFILE/FUNLOCKFILE macros in mtlib.h.  rmutex_trylock(), as a
55  * special case, is called from GETIOP()/getiop() in _findiop().
56  */
57 
58 /*
59  * compute the lock's position, acquire it and return its pointer
60  */
61 
62 rmutex_t *
63 _flockget(FILE *iop)
64 {
65 	rmutex_t *rl = IOB_LCK(iop);
66 
67 	if (rl != NULL)
68 		rmutex_lock(rl);
69 	return (rl);
70 }
71 
72 int
73 ftrylockfile(FILE *iop)
74 {
75 	rmutex_t *rl = IOB_LCK(iop);
76 
77 	if (rl != NULL)
78 		return (rmutex_trylock(rl));
79 	return (0);	/* can't happen? */
80 }
81 
82 void
83 flockfile(FILE *iop)
84 {
85 	rmutex_t *rl = IOB_LCK(iop);
86 
87 	if (rl != NULL)
88 		rmutex_lock(rl);
89 }
90 
91 void
92 funlockfile(FILE *iop)
93 {
94 	rmutex_t *rl = IOB_LCK(iop);
95 
96 	if (rl != NULL)
97 		rmutex_unlock(rl);
98 }
99 
100 int
101 __fsetlocking(FILE *iop, int type)
102 {
103 	int	ret = 0;
104 
105 	ret = GET_IONOLOCK(iop) ? FSETLOCKING_BYCALLER : FSETLOCKING_INTERNAL;
106 
107 	switch (type) {
108 
109 	case FSETLOCKING_QUERY:
110 		break;
111 
112 	case FSETLOCKING_INTERNAL:
113 		CLEAR_IONOLOCK(iop);
114 		break;
115 
116 	case FSETLOCKING_BYCALLER:
117 		SET_IONOLOCK(iop);
118 		break;
119 
120 	default:
121 		errno = EINVAL;
122 		return (-1);
123 	}
124 
125 	return (ret);
126 }
127