xref: /titanic_50/usr/src/lib/libc/port/stdio/flockf.c (revision df14233e629298598736976c5bfcf4a31873745f)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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  * _flockget and _flockrel are only called by the
52  * FLOCKFILE/FUNLOCKFILE macros in mtlib.h.
53  */
54 
55 /*
56  * compute the lock's position, acquire it and return its pointer
57  */
58 
59 rmutex_t *
60 _flockget(FILE *iop)
61 {
62 	rmutex_t *rl = IOB_LCK(iop);
63 
64 	if (rl != NULL)
65 		cancel_safe_mutex_lock(rl);
66 	return (rl);
67 }
68 
69 int
70 ftrylockfile(FILE *iop)
71 {
72 	rmutex_t *rl = IOB_LCK(iop);
73 
74 	if (rl != NULL)
75 		return (_private_mutex_trylock(rl));
76 	return (0);	/* can't happen? */
77 }
78 
79 void
80 flockfile(FILE *iop)
81 {
82 	rmutex_t *rl = IOB_LCK(iop);
83 
84 	if (rl != NULL)
85 		_private_mutex_lock(rl);
86 }
87 
88 void
89 funlockfile(FILE *iop)
90 {
91 	rmutex_t *rl = IOB_LCK(iop);
92 
93 	if (rl != NULL)
94 		_private_mutex_unlock(rl);
95 }
96 
97 int
98 __fsetlocking(FILE *iop, int type)
99 {
100 	int	ret = 0;
101 
102 	ret = GET_IONOLOCK(iop) ? FSETLOCKING_BYCALLER : FSETLOCKING_INTERNAL;
103 
104 	switch (type) {
105 
106 	case FSETLOCKING_QUERY:
107 		break;
108 
109 	case FSETLOCKING_INTERNAL:
110 		CLEAR_IONOLOCK(iop);
111 		break;
112 
113 	case FSETLOCKING_BYCALLER:
114 		SET_IONOLOCK(iop);
115 		break;
116 
117 	default:
118 		errno = EINVAL;
119 		return (-1);
120 	}
121 
122 	return (ret);
123 }
124