10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55891Sraf * Common Development and Distribution License (the "License").
65891Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
215891Sraf
220Sstevel@tonic-gate /*
235891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "mtlib.h"
340Sstevel@tonic-gate #include "file64.h"
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <thread.h>
370Sstevel@tonic-gate #include <synch.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <errno.h>
400Sstevel@tonic-gate #include <stdio_ext.h>
410Sstevel@tonic-gate #include "stdiom.h"
420Sstevel@tonic-gate
43*6812Sraf #define _iob __iob
44*6812Sraf
450Sstevel@tonic-gate /*
465891Sraf * _flockget and _flockrel are only called by the
475891Sraf * FLOCKFILE/FUNLOCKFILE macros in mtlib.h.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * compute the lock's position, acquire it and return its pointer
520Sstevel@tonic-gate */
530Sstevel@tonic-gate
540Sstevel@tonic-gate rmutex_t *
_flockget(FILE * iop)550Sstevel@tonic-gate _flockget(FILE *iop)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate rmutex_t *rl = IOB_LCK(iop);
580Sstevel@tonic-gate
590Sstevel@tonic-gate if (rl != NULL)
605891Sraf cancel_safe_mutex_lock(rl);
610Sstevel@tonic-gate return (rl);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate int
ftrylockfile(FILE * iop)650Sstevel@tonic-gate ftrylockfile(FILE *iop)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate rmutex_t *rl = IOB_LCK(iop);
680Sstevel@tonic-gate
690Sstevel@tonic-gate if (rl != NULL)
706515Sraf return (mutex_trylock(rl));
710Sstevel@tonic-gate return (0); /* can't happen? */
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate void
flockfile(FILE * iop)750Sstevel@tonic-gate flockfile(FILE *iop)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate rmutex_t *rl = IOB_LCK(iop);
780Sstevel@tonic-gate
790Sstevel@tonic-gate if (rl != NULL)
80*6812Sraf (void) mutex_lock(rl);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate void
funlockfile(FILE * iop)840Sstevel@tonic-gate funlockfile(FILE *iop)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate rmutex_t *rl = IOB_LCK(iop);
870Sstevel@tonic-gate
880Sstevel@tonic-gate if (rl != NULL)
89*6812Sraf (void) mutex_unlock(rl);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate int
__fsetlocking(FILE * iop,int type)930Sstevel@tonic-gate __fsetlocking(FILE *iop, int type)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate int ret = 0;
960Sstevel@tonic-gate
970Sstevel@tonic-gate ret = GET_IONOLOCK(iop) ? FSETLOCKING_BYCALLER : FSETLOCKING_INTERNAL;
980Sstevel@tonic-gate
990Sstevel@tonic-gate switch (type) {
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate case FSETLOCKING_QUERY:
1020Sstevel@tonic-gate break;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate case FSETLOCKING_INTERNAL:
1050Sstevel@tonic-gate CLEAR_IONOLOCK(iop);
1060Sstevel@tonic-gate break;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate case FSETLOCKING_BYCALLER:
1090Sstevel@tonic-gate SET_IONOLOCK(iop);
1100Sstevel@tonic-gate break;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate default:
1130Sstevel@tonic-gate errno = EINVAL;
1140Sstevel@tonic-gate return (-1);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate return (ret);
1180Sstevel@tonic-gate }
119