mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-02 19:16:15 +00:00
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:
34
main/utils.c
34
main/utils.c
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user