util: implement fd_nonbloc()/fd_cloexec()
This commit is contained in:
parent
7418040c3a
commit
3a0ecb08f4
39
util.c
39
util.c
|
@ -32,6 +32,7 @@
|
||||||
#include <linux/sched.h>
|
#include <linux/sched.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -1084,6 +1085,44 @@ bool ignore_file(const char *filename) {
|
||||||
endswith(filename, ".swp");
|
endswith(filename, ".swp");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int fd_nonblock(int fd, bool nonblock) {
|
||||||
|
int flags;
|
||||||
|
|
||||||
|
assert(fd >= 0);
|
||||||
|
|
||||||
|
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
if (nonblock)
|
||||||
|
flags |= O_NONBLOCK;
|
||||||
|
else
|
||||||
|
flags &= ~O_NONBLOCK;
|
||||||
|
|
||||||
|
if (fcntl(fd, F_SETFL, flags) < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd_cloexec(int fd, bool cloexec) {
|
||||||
|
int flags;
|
||||||
|
|
||||||
|
assert(fd >= 0);
|
||||||
|
|
||||||
|
if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
if (cloexec)
|
||||||
|
flags |= FD_CLOEXEC;
|
||||||
|
else
|
||||||
|
flags &= ~FD_CLOEXEC;
|
||||||
|
|
||||||
|
if (fcntl(fd, F_SETFD, flags) < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *const ioprio_class_table[] = {
|
static const char *const ioprio_class_table[] = {
|
||||||
[IOPRIO_CLASS_NONE] = "none",
|
[IOPRIO_CLASS_NONE] = "none",
|
||||||
[IOPRIO_CLASS_RT] = "realtime",
|
[IOPRIO_CLASS_RT] = "realtime",
|
||||||
|
|
3
util.h
3
util.h
|
@ -172,6 +172,9 @@ bool ignore_file(const char *filename);
|
||||||
struct __useless_struct_to_allow_trailing_semicolon__
|
struct __useless_struct_to_allow_trailing_semicolon__
|
||||||
|
|
||||||
|
|
||||||
|
int fd_nonblock(int fd, bool nonblock);
|
||||||
|
int fd_cloexec(int fd, bool cloexec);
|
||||||
|
|
||||||
const char *ioprio_class_to_string(int i);
|
const char *ioprio_class_to_string(int i);
|
||||||
int ioprio_class_from_string(const char *s);
|
int ioprio_class_from_string(const char *s);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue