1aa772005SRobert Watson /*-
2*e738085bSDag-Erling Smørgrav * Copyright (c) 2007 Dag-Erling Smørgrav
3aa772005SRobert Watson * All rights reserved.
4aa772005SRobert Watson *
5aa772005SRobert Watson * Redistribution and use in source and binary forms, with or without
6aa772005SRobert Watson * modification, are permitted provided that the following conditions
7aa772005SRobert Watson * are met:
8aa772005SRobert Watson * 1. Redistributions of source code must retain the above copyright
9aa772005SRobert Watson * notice, this list of conditions and the following disclaimer
10aa772005SRobert Watson * in this position and unchanged.
11aa772005SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
12aa772005SRobert Watson * notice, this list of conditions and the following disclaimer in the
13aa772005SRobert Watson * documentation and/or other materials provided with the distribution.
14aa772005SRobert Watson *
15aa772005SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16aa772005SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17aa772005SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18aa772005SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19aa772005SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20aa772005SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21aa772005SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22aa772005SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23aa772005SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24aa772005SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25aa772005SRobert Watson * SUCH DAMAGE.
26aa772005SRobert Watson *
27aa772005SRobert Watson * Derived from FreeBSD head/lib/libutil/flopen.c r193591
28aa772005SRobert Watson */
29aa772005SRobert Watson
30aa772005SRobert Watson #include <sys/file.h>
31aa772005SRobert Watson #include <sys/stat.h>
32aa772005SRobert Watson
33aa772005SRobert Watson #include <errno.h>
34aa772005SRobert Watson #include <stdarg.h>
35aa772005SRobert Watson #include <unistd.h>
36aa772005SRobert Watson
37aa772005SRobert Watson static int
flopen(const char * path,int flags,...)38aa772005SRobert Watson flopen(const char *path, int flags, ...)
39aa772005SRobert Watson {
40aa772005SRobert Watson int fd, operation, serrno, trunc;
41aa772005SRobert Watson struct stat sb, fsb;
42aa772005SRobert Watson mode_t mode;
43aa772005SRobert Watson
44aa772005SRobert Watson #ifdef O_EXLOCK
45aa772005SRobert Watson flags &= ~O_EXLOCK;
46aa772005SRobert Watson #endif
47aa772005SRobert Watson
48aa772005SRobert Watson mode = 0;
49aa772005SRobert Watson if (flags & O_CREAT) {
50aa772005SRobert Watson va_list ap;
51aa772005SRobert Watson
52aa772005SRobert Watson va_start(ap, flags);
53aa772005SRobert Watson mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
54aa772005SRobert Watson va_end(ap);
55aa772005SRobert Watson }
56aa772005SRobert Watson
57aa772005SRobert Watson operation = LOCK_EX;
58aa772005SRobert Watson if (flags & O_NONBLOCK)
59aa772005SRobert Watson operation |= LOCK_NB;
60aa772005SRobert Watson
61aa772005SRobert Watson trunc = (flags & O_TRUNC);
62aa772005SRobert Watson flags &= ~O_TRUNC;
63aa772005SRobert Watson
64aa772005SRobert Watson for (;;) {
65aa772005SRobert Watson if ((fd = open(path, flags, mode)) == -1)
66aa772005SRobert Watson /* non-existent or no access */
67aa772005SRobert Watson return (-1);
68aa772005SRobert Watson if (flock(fd, operation) == -1) {
69aa772005SRobert Watson /* unsupported or interrupted */
70aa772005SRobert Watson serrno = errno;
71aa772005SRobert Watson (void)close(fd);
72aa772005SRobert Watson errno = serrno;
73aa772005SRobert Watson return (-1);
74aa772005SRobert Watson }
75aa772005SRobert Watson if (stat(path, &sb) == -1) {
76aa772005SRobert Watson /* disappeared from under our feet */
77aa772005SRobert Watson (void)close(fd);
78aa772005SRobert Watson continue;
79aa772005SRobert Watson }
80aa772005SRobert Watson if (fstat(fd, &fsb) == -1) {
81aa772005SRobert Watson /* can't happen [tm] */
82aa772005SRobert Watson serrno = errno;
83aa772005SRobert Watson (void)close(fd);
84aa772005SRobert Watson errno = serrno;
85aa772005SRobert Watson return (-1);
86aa772005SRobert Watson }
87aa772005SRobert Watson if (sb.st_dev != fsb.st_dev ||
88aa772005SRobert Watson sb.st_ino != fsb.st_ino) {
89aa772005SRobert Watson /* changed under our feet */
90aa772005SRobert Watson (void)close(fd);
91aa772005SRobert Watson continue;
92aa772005SRobert Watson }
93aa772005SRobert Watson if (trunc && ftruncate(fd, 0) != 0) {
94aa772005SRobert Watson /* can't happen [tm] */
95aa772005SRobert Watson serrno = errno;
96aa772005SRobert Watson (void)close(fd);
97aa772005SRobert Watson errno = serrno;
98aa772005SRobert Watson return (-1);
99aa772005SRobert Watson }
100aa772005SRobert Watson return (fd);
101aa772005SRobert Watson }
102aa772005SRobert Watson }
103