utils: Add convenience function for setting fd flags

There are many places in the code base where we ignore the return value
of fcntl() when getting/setting file descriptior flags. This patch
introduces a convenience function that allows setting or clearing file
descriptor flags and will also log an error on failure for later
analysis.

Change-Id: I8b81901e1b1bd537ca632567cdb408931c6eded7
This commit is contained in:
Sean Bright
2017-12-07 10:52:39 -05:00
parent b0b28446c1
commit 2ffe52a116
17 changed files with 100 additions and 78 deletions

View File

@@ -2749,3 +2749,37 @@ int ast_compare_versions(const char *version1, const char *version2)
}
return extra[0] - extra[1];
}
int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op,
const char *file, int lineno, const char *function)
{
int f;
f = fcntl(fd, F_GETFL);
if (f == -1) {
ast_log(__LOG_ERROR, file, lineno, function,
"Failed to get fcntl() flags for file descriptor: %s\n", strerror(errno));
return -1;
}
switch (op) {
case AST_FD_FLAG_SET:
f |= flags;
break;
case AST_FD_FLAG_CLEAR:
f &= ~flags;
break;
default:
ast_assert(0);
break;
}
f = fcntl(fd, F_SETFL, f);
if (f == -1) {
ast_log(__LOG_ERROR, file, lineno, function,
"Failed to set fcntl() flags for file descriptor: %s\n", strerror(errno));
return -1;
}
return 0;
}