tree-wide: use -EBADF for fd initialization

-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-12-19 15:00:57 +01:00
parent cbff793ffb
commit 254d1313ae
284 changed files with 792 additions and 793 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
#include "user-util.h"
static int chown_cgroup_path(const char *path, uid_t uid_shift) {
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY);
if (fd < 0)
+1 -1
View File
@@ -160,7 +160,7 @@ int expose_port_execute(sd_netlink *rtnl, FirewallContext **fw_ctx, ExposePort *
}
int expose_port_send_rtnl(int send_fd) {
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
int r;
assert(send_fd >= 0);
+2 -2
View File
@@ -28,7 +28,7 @@ static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
assert(ret);
if (name) {
_cleanup_close_ int child_fd = -1;
_cleanup_close_ int child_fd = -EBADF;
child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
if (child_fd < 0)
@@ -53,7 +53,7 @@ static int set_acl(int fd, const char *name, acl_type_t type, acl_t acl) {
assert(acl);
if (name) {
_cleanup_close_ int child_fd = -1;
_cleanup_close_ int child_fd = -EBADF;
child_fd = openat(fd, name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
if (child_fd < 0)
+1 -1
View File
@@ -93,7 +93,7 @@ int change_uid_gid(const char *user, bool chown_stdio, char **ret_home) {
_cleanup_free_ gid_t *gids = NULL;
_cleanup_free_ char *home = NULL, *line = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
unsigned n_gids = 0;
uid_t uid;
gid_t gid;
+8 -8
View File
@@ -2459,7 +2459,7 @@ static int setup_credentials(const char *root) {
for (size_t i = 0; i < arg_n_credentials; i++) {
_cleanup_free_ char *j = NULL;
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
j = path_join(q, arg_credentials[i].id);
if (!j)
@@ -2500,7 +2500,7 @@ static int setup_credentials(const char *root) {
static int setup_kmsg(int fd_inner_socket) {
_cleanup_(unlink_and_freep) char *from = NULL;
_cleanup_free_ char *fifo = NULL;
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
int r;
assert(fd_inner_socket >= 0);
@@ -3590,7 +3590,7 @@ static int inner_child(
}
static int setup_notify_child(void) {
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
static const union sockaddr_union sa = {
.un.sun_family = AF_UNIX,
.un.sun_path = NSPAWN_NOTIFY_SOCKET_PATH,
@@ -3631,7 +3631,7 @@ static int outer_child(
_cleanup_(bind_user_context_freep) BindUserContext *bind_user_context = NULL;
_cleanup_strv_free_ char **os_release_pairs = NULL;
_cleanup_close_ int fd = -1, mntns_fd = -EBADF;
_cleanup_close_ int fd = -EBADF, mntns_fd = -EBADF;
bool idmap = false;
const char *p;
pid_t pid;
@@ -4760,7 +4760,7 @@ static int run_container(
int ifi = 0, r;
ssize_t l;
sigset_t mask_chld;
_cleanup_close_ int child_netns_fd = -1;
_cleanup_close_ int child_netns_fd = -EBADF;
assert_se(sigemptyset(&mask_chld) == 0);
assert_se(sigaddset(&mask_chld, SIGCHLD) == 0);
@@ -5168,7 +5168,7 @@ static int run_container(
}
if (arg_console_mode != CONSOLE_PIPE) {
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
PTYForwardFlags flags = 0;
/* Retrieve the master pty allocated by inner child */
@@ -5236,7 +5236,7 @@ static int run_container(
return r;
if (r == 0) {
_cleanup_close_ int parent_netns_fd = -1;
_cleanup_close_ int parent_netns_fd = -EBADF;
r = namespace_open(getpid(), NULL, NULL, &parent_netns_fd, NULL, NULL);
if (r < 0) {
@@ -5373,7 +5373,7 @@ static int initialize_rlimits(void) {
}
static int cant_be_in_netns(void) {
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -EBADF;
struct ucred ucred;
int r;