xref: /illumos-gate/usr/src/lib/libc/port/stdio/flockf.c (revision 0ccfe5834c3b867c1b6c273cae02ec8922bc0fd2)
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 #include "lint.h"
31 #include "mtlib.h"
32 #include "file64.h"
33 #include <stdio.h>
34 #include <thread.h>
35 #include <synch.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <stdio_ext.h>
39 #include "stdiom.h"
40 
41 #define	_iob	__iob
42 
43 /*
44  * _flockget and _flockrel are only called by the
45  * FLOCKFILE/FUNLOCKFILE macros in mtlib.h.
46  */
47 
48 /*
49  * compute the lock's position, acquire it and return its pointer
50  */
51 
52 rmutex_t *
53 _flockget(FILE *iop)
54 {
55 	rmutex_t *rl = IOB_LCK(iop);
56 
57 	if (rl != NULL)
58 		cancel_safe_mutex_lock(rl);
59 	return (rl);
60 }
61 
62 int
63 ftrylockfile(FILE *iop)
64 {
65 	rmutex_t *rl = IOB_LCK(iop);
66 
67 	if (rl != NULL)
68 		return (mutex_trylock(rl));
69 	return (0);	/* can't happen? */
70 }
71 
72 void
73 flockfile(FILE *iop)
74 {
75 	rmutex_t *rl = IOB_LCK(iop);
76 
77 	if (rl != NULL)
78 		(void) mutex_lock(rl);
79 }
80 
81 void
82 funlockfile(FILE *iop)
83 {
84 	rmutex_t *rl = IOB_LCK(iop);
85 
86 	if (rl != NULL)
87 		(void) mutex_unlock(rl);
88 }
89 
90 int
91 __fsetlocking(FILE *iop, int type)
92 {
93 	int	ret = 0;
94 
95 	ret = GET_IONOLOCK(iop) ? FSETLOCKING_BYCALLER : FSETLOCKING_INTERNAL;
96 
97 	switch (type) {
98 
99 	case FSETLOCKING_QUERY:
100 		break;
101 
102 	case FSETLOCKING_INTERNAL:
103 		CLEAR_IONOLOCK(iop);
104 		break;
105 
106 	case FSETLOCKING_BYCALLER:
107 		SET_IONOLOCK(iop);
108 		break;
109 
110 	default:
111 		errno = EINVAL;
112 		return (-1);
113 	}
114 
115 	return (ret);
116 }
117