Skip to content

Commit 2a2806b

Browse files
cfriedthenrikbrixandersen
authored andcommitted
os: fdtable: restore errno if optional locking ioctl fails
The `zvfs_finalize_typed_fd()` function notifies some backends via `ioctl()` with `ZFD_IOCTL_SET_LOCK`. However, support for this method and functionality is optional. In backends that do not support locking, this benign failure can set `errno` to 95 (`EOPNOTSUPP`) in many circumstances where a change in `errno` (indicating some kind of failure) is not appropriate. Prevent errno poisoning by backing-up and restoring `errno`. Signed-off-by: Chris Friedt <[email protected]>
1 parent ba8025f commit 2a2806b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

lib/os/fdtable.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,14 @@ void zvfs_finalize_typed_fd(int fd, void *obj, const struct fd_op_vtable *vtable
280280
* variables to avoid keeping the lock for a long period of time.
281281
*/
282282
if (vtable && vtable->ioctl) {
283+
int prev_errno = errno;
284+
283285
(void)zvfs_fdtable_call_ioctl(vtable, obj, ZFD_IOCTL_SET_LOCK,
284286
&fdtable[fd].lock);
287+
if ((prev_errno != EOPNOTSUPP) && (errno == EOPNOTSUPP)) {
288+
/* restore backed-up errno value if the backend does not support locking */
289+
errno = prev_errno;
290+
}
285291
}
286292
}
287293

0 commit comments

Comments
 (0)