xref: /dflybsd-src/lib/libutil/flopen.c (revision d316f7c95d4b8b07a5557eb0a39cfa39b7114297)
13997c939SPeter Avalos /*-
23997c939SPeter Avalos  * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
33997c939SPeter Avalos  * All rights reserved.
43997c939SPeter Avalos  *
53997c939SPeter Avalos  * Redistribution and use in source and binary forms, with or without
63997c939SPeter Avalos  * modification, are permitted provided that the following conditions
73997c939SPeter Avalos  * are met:
83997c939SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
93997c939SPeter Avalos  *    notice, this list of conditions and the following disclaimer
103997c939SPeter Avalos  *    in this position and unchanged.
113997c939SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
123997c939SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
133997c939SPeter Avalos  *    documentation and/or other materials provided with the distribution.
143997c939SPeter Avalos  *
153997c939SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163997c939SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173997c939SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183997c939SPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193997c939SPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203997c939SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213997c939SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223997c939SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233997c939SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243997c939SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253997c939SPeter Avalos  * SUCH DAMAGE.
263997c939SPeter Avalos  *
27*d316f7c9SJohn Marino  * $FreeBSD: head/lib/libutil/flopen.c 193591 2009-06-06 18:47:03Z des $
283997c939SPeter Avalos  */
293997c939SPeter Avalos 
303997c939SPeter Avalos #include <sys/stat.h>
313997c939SPeter Avalos 
323997c939SPeter Avalos #include <errno.h>
333997c939SPeter Avalos #include <stdarg.h>
343997c939SPeter Avalos #include <unistd.h>
35*d316f7c9SJohn Marino #include <fcntl.h>
363997c939SPeter Avalos #include <libutil.h>
373997c939SPeter Avalos 
383997c939SPeter Avalos int
flopen(const char * path,int flags,...)393997c939SPeter Avalos flopen(const char *path, int flags, ...)
403997c939SPeter Avalos {
413997c939SPeter Avalos 	int fd, operation, serrno, trunc;
423997c939SPeter Avalos 	struct stat sb, fsb;
433997c939SPeter Avalos 	mode_t mode;
443997c939SPeter Avalos 
453997c939SPeter Avalos #ifdef O_EXLOCK
463997c939SPeter Avalos 	flags &= ~O_EXLOCK;
473997c939SPeter Avalos #endif
483997c939SPeter Avalos 
493997c939SPeter Avalos 	mode = 0;
503997c939SPeter Avalos 	if (flags & O_CREAT) {
513997c939SPeter Avalos 		va_list ap;
523997c939SPeter Avalos 
533997c939SPeter Avalos 		va_start(ap, flags);
543997c939SPeter Avalos 		mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
553997c939SPeter Avalos 		va_end(ap);
563997c939SPeter Avalos 	}
573997c939SPeter Avalos 
58*d316f7c9SJohn Marino         operation = LOCK_EX;
59*d316f7c9SJohn Marino         if (flags & O_NONBLOCK)
60*d316f7c9SJohn Marino                 operation |= LOCK_NB;
613997c939SPeter Avalos 
623997c939SPeter Avalos 	trunc = (flags & O_TRUNC);
633997c939SPeter Avalos 	flags &= ~O_TRUNC;
643997c939SPeter Avalos 
653997c939SPeter Avalos 	for (;;) {
663997c939SPeter Avalos 		if ((fd = open(path, flags, mode)) == -1)
673997c939SPeter Avalos 			/* non-existent or no access */
683997c939SPeter Avalos 			return (-1);
69*d316f7c9SJohn Marino 		if (flock(fd, operation) == -1) {
703997c939SPeter Avalos 			/* unsupported or interrupted */
713997c939SPeter Avalos 			serrno = errno;
72*d316f7c9SJohn Marino 			(void)close(fd);
733997c939SPeter Avalos 			errno = serrno;
743997c939SPeter Avalos 			return (-1);
753997c939SPeter Avalos 		}
763997c939SPeter Avalos 		if (stat(path, &sb) == -1) {
773997c939SPeter Avalos 			/* disappeared from under our feet */
78*d316f7c9SJohn Marino 			(void)close(fd);
793997c939SPeter Avalos 			continue;
803997c939SPeter Avalos 		}
813997c939SPeter Avalos 		if (fstat(fd, &fsb) == -1) {
823997c939SPeter Avalos 			/* can't happen [tm] */
833997c939SPeter Avalos 			serrno = errno;
84*d316f7c9SJohn Marino 			(void)close(fd);
853997c939SPeter Avalos 			errno = serrno;
863997c939SPeter Avalos 			return (-1);
873997c939SPeter Avalos 		}
883997c939SPeter Avalos 		if (sb.st_dev != fsb.st_dev ||
893997c939SPeter Avalos 		    sb.st_ino != fsb.st_ino) {
903997c939SPeter Avalos 			/* changed under our feet */
91*d316f7c9SJohn Marino 			(void)close(fd);
923997c939SPeter Avalos 			continue;
933997c939SPeter Avalos 		}
943997c939SPeter Avalos 		if (trunc && ftruncate(fd, 0) != 0) {
953997c939SPeter Avalos 			/* can't happen [tm] */
963997c939SPeter Avalos 			serrno = errno;
97*d316f7c9SJohn Marino 			(void)close(fd);
983997c939SPeter Avalos 			errno = serrno;
993997c939SPeter Avalos 			return (-1);
1003997c939SPeter Avalos 		}
1013997c939SPeter Avalos 		return (fd);
1023997c939SPeter Avalos 	}
1033997c939SPeter Avalos }
104