mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-01-09 11:17:37 +01:00
Add flopenat() function from FreeBSD
This commit is contained in:
parent
30b4d50754
commit
993828d84e
2
COPYING
2
COPYING
@ -367,7 +367,7 @@ License: BSD-2-clause
|
|||||||
Files:
|
Files:
|
||||||
src/flopen.c
|
src/flopen.c
|
||||||
Copyright:
|
Copyright:
|
||||||
Copyright © 2007 Dag-Erling Coïdan Smørgrav
|
Copyright © 2007-2009 Dag-Erling Coïdan Smørgrav
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
License: BSD-2-clause-verbatim
|
License: BSD-2-clause-verbatim
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -62,6 +62,7 @@ int humanize_number(char *buf, size_t len, int64_t bytes,
|
|||||||
int expand_number(const char *_buf, uint64_t *_num);
|
int expand_number(const char *_buf, uint64_t *_num);
|
||||||
|
|
||||||
int flopen(const char *_path, int _flags, ...);
|
int flopen(const char *_path, int _flags, ...);
|
||||||
|
int flopenat(int dirfd, const char *path, int flags, ...);
|
||||||
|
|
||||||
struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
|
struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
|
||||||
int pidfile_write(struct pidfh *pfh);
|
int pidfile_write(struct pidfh *pfh);
|
||||||
|
@ -25,12 +25,13 @@
|
|||||||
.\"
|
.\"
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd June 6, 2009
|
.Dd July 28, 2017
|
||||||
.Dt FLOPEN 3bsd
|
.Dt FLOPEN 3bsd
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
.Nm flopen
|
.Nm flopen ,
|
||||||
.Nd reliably open and lock a file
|
.Nm flopenat
|
||||||
|
.Nd "Reliably open and lock a file"
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
.Lb libbsd
|
.Lb libbsd
|
||||||
@ -44,6 +45,10 @@ for include usage.)
|
|||||||
.Fn flopen "const char *path" "int flags"
|
.Fn flopen "const char *path" "int flags"
|
||||||
.Ft int
|
.Ft int
|
||||||
.Fn flopen "const char *path" "int flags" "mode_t mode"
|
.Fn flopen "const char *path" "int flags" "mode_t mode"
|
||||||
|
.Ft int
|
||||||
|
.Fn flopenat "int fd" "const char *path" "int flags"
|
||||||
|
.Ft int
|
||||||
|
.Fn flopenat "int fd" "const char *path" "int flags" "mode_t mode"
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
The
|
The
|
||||||
.Fn flopen
|
.Fn flopen
|
||||||
@ -53,7 +58,7 @@ It is essentially equivalent with calling
|
|||||||
with the same parameters followed by
|
with the same parameters followed by
|
||||||
.Fn flock
|
.Fn flock
|
||||||
with an
|
with an
|
||||||
.Va operation
|
.Fa operation
|
||||||
argument of
|
argument of
|
||||||
.Dv LOCK_EX ,
|
.Dv LOCK_EX ,
|
||||||
except that
|
except that
|
||||||
@ -65,7 +70,7 @@ files, mailboxes and other kinds of files which are used for
|
|||||||
synchronization between processes.
|
synchronization between processes.
|
||||||
.Pp
|
.Pp
|
||||||
If
|
If
|
||||||
.Va flags
|
.Fa flags
|
||||||
includes
|
includes
|
||||||
.Dv O_NONBLOCK
|
.Dv O_NONBLOCK
|
||||||
and the file is already locked,
|
and the file is already locked,
|
||||||
@ -78,11 +83,32 @@ to
|
|||||||
As with
|
As with
|
||||||
.Fn open ,
|
.Fn open ,
|
||||||
the additional
|
the additional
|
||||||
.Va mode
|
.Fa mode
|
||||||
argument is required if
|
argument is required if
|
||||||
.Va flags
|
.Fa flags
|
||||||
includes
|
includes
|
||||||
.Dv O_CREAT .
|
.Dv O_CREAT .
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn flopenat
|
||||||
|
function is equivalent to the
|
||||||
|
.Fn flopen
|
||||||
|
function except in the case where the
|
||||||
|
.Fa path
|
||||||
|
specifies a relative path.
|
||||||
|
In this case the file to be opened is determined relative to the directory
|
||||||
|
associated with the file descriptor
|
||||||
|
.Fa fd
|
||||||
|
instead of the current working directory.
|
||||||
|
If
|
||||||
|
.Fn flopenat
|
||||||
|
is passed the special value
|
||||||
|
.Dv AT_FDCWD
|
||||||
|
in the
|
||||||
|
.Fa fd
|
||||||
|
parameter, the current working directory is used
|
||||||
|
and the behavior is identical to a call to
|
||||||
|
.Fn flopen .
|
||||||
.Sh RETURN VALUES
|
.Sh RETURN VALUES
|
||||||
If successful,
|
If successful,
|
||||||
.Fn flopen
|
.Fn flopen
|
||||||
@ -102,4 +128,4 @@ and
|
|||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
function and this manual page were written by
|
function and this manual page were written by
|
||||||
.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
|
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
|
||||||
|
62
src/flopen.c
62
src/flopen.c
@ -1,5 +1,7 @@
|
|||||||
/*-
|
/*-
|
||||||
* Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
||||||
|
*
|
||||||
|
* Copyright (c) 2007-2009 Dag-Erling Coïdan Smørgrav
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -30,13 +32,21 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <libutil.h>
|
#include <libutil.h>
|
||||||
|
|
||||||
int
|
/*
|
||||||
flopen(const char *path, int flags, ...)
|
* Reliably open and lock a file.
|
||||||
|
*
|
||||||
|
* Please do not modify this code without first reading the revision history
|
||||||
|
* and discussing your changes with <des@freebsd.org>. Don't be fooled by the
|
||||||
|
* code's apparent simplicity; there would be no need for this function if it
|
||||||
|
* was easy to get right.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
vflopenat(int dirfd, const char *path, int flags, va_list ap)
|
||||||
{
|
{
|
||||||
int fd, operation, serrno, trunc;
|
int fd, operation, serrno, trunc;
|
||||||
struct stat sb, fsb;
|
struct stat sb, fsb;
|
||||||
@ -48,11 +58,7 @@ flopen(const char *path, int flags, ...)
|
|||||||
|
|
||||||
mode = 0;
|
mode = 0;
|
||||||
if (flags & O_CREAT) {
|
if (flags & O_CREAT) {
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, flags);
|
|
||||||
mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
|
mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
|
||||||
va_end(ap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
operation = LOCK_EX;
|
operation = LOCK_EX;
|
||||||
@ -63,7 +69,7 @@ flopen(const char *path, int flags, ...)
|
|||||||
flags &= ~O_TRUNC;
|
flags &= ~O_TRUNC;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if ((fd = open(path, flags, mode)) == -1)
|
if ((fd = openat(dirfd, path, flags, mode)) == -1)
|
||||||
/* non-existent or no access */
|
/* non-existent or no access */
|
||||||
return (-1);
|
return (-1);
|
||||||
if (flock(fd, operation) == -1) {
|
if (flock(fd, operation) == -1) {
|
||||||
@ -73,7 +79,7 @@ flopen(const char *path, int flags, ...)
|
|||||||
errno = serrno;
|
errno = serrno;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
if (stat(path, &sb) == -1) {
|
if (fstatat(dirfd, path, &sb, 0) == -1) {
|
||||||
/* disappeared from under our feet */
|
/* disappeared from under our feet */
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
continue;
|
continue;
|
||||||
@ -98,6 +104,42 @@ flopen(const char *path, int flags, ...)
|
|||||||
errno = serrno;
|
errno = serrno;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* The following change is provided as a specific example to
|
||||||
|
* avoid.
|
||||||
|
*/
|
||||||
|
#if 0
|
||||||
|
if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
|
||||||
|
serrno = errno;
|
||||||
|
(void)close(fd);
|
||||||
|
errno = serrno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return (fd);
|
return (fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
flopen(const char *path, int flags, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
va_start(ap, flags);
|
||||||
|
ret = vflopenat(AT_FDCWD, path, flags, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
flopenat(int dirfd, const char *path, int flags, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
va_start(ap, flags);
|
||||||
|
ret = vflopenat(dirfd, path, flags, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
@ -139,3 +139,7 @@ LIBBSD_0.7 {
|
|||||||
LIBBSD_0.8 {
|
LIBBSD_0.8 {
|
||||||
explicit_bzero;
|
explicit_bzero;
|
||||||
} LIBBSD_0.7;
|
} LIBBSD_0.7;
|
||||||
|
|
||||||
|
LIBBSD_0.9 {
|
||||||
|
flopenat;
|
||||||
|
} LIBBSD_0.8;
|
||||||
|
Loading…
Reference in New Issue
Block a user