1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 2018-2020 Alex Richardson <arichardson@FreeBSD.org>
5 *
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory (Department of Computer Science and
8 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9 * DARPA SSITH research programme.
10 *
11 * This software was developed by SRI International and the University of
12 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
13 * ("CTSRD"), as part of the DARPA CRASH research programme.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * $FreeBSD$
37 */
38#pragma once
39
40
41/* <bits/fcntl.h> includes <bits/stat.h> which contains a member __unused */
42#include "__unused_workaround_start.h"
43#include_next <fcntl.h>
44#include "__unused_workaround_end.h"
45
46#ifdef __unused_undefd
47#undef __unused_undefd
48#define __unused __attribute__((unused))
49#endif
50
51#include <sys/file.h>
52
53#ifndef O_EXLOCK
54#define O_EXLOCK (1 << 30)
55#endif
56#ifndef O_SHLOCK
57#define O_SHLOCK (1 << 31)
58#endif
59
60#undef open
61#define open(path, flags, ...) ({ \
62    int __fd = (open)(path, flags, ##__VA_ARGS__); \
63    if (flags & O_EXLOCK) flock(__fd, LOCK_EX); \
64    if (flags & O_SHLOCK) flock(__fd, LOCK_SH); \
65    __fd; })
66