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