return safe_getcwd(ret);
r = readlink_malloc(FORMAT_PROC_FD_PATH(fd), ret);
- if (r == -ENOENT) {
- /* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's make
- * things debuggable and distinguish the two. */
-
- if (proc_mounted() == 0)
- return -ENOSYS; /* /proc is not available or not set up properly, we're most likely in some chroot
- * environment. */
- return -EBADF; /* The directory exists, hence it's the fd that doesn't. */
- }
-
+ if (r == -ENOENT)
+ return proc_fd_enoent_errno();
return r;
}
}
int fd_reopen(int fd, int flags) {
- int r;
-
assert(fd >= 0 || fd == AT_FDCWD);
assert(!FLAGS_SET(flags, O_CREAT));
if (errno != ENOENT)
return -errno;
- r = proc_mounted();
- if (r == 0)
- return -ENOSYS; /* if we have no /proc/, the concept is not implementable */
-
- return r > 0 ? -EBADF : -ENOENT; /* If /proc/ is definitely around then this means the fd is
- * not valid, otherwise let's propagate the original
- * error */
+ return proc_fd_enoent_errno();
}
return new_fd;
assert_se(snprintf_ok(buf, PROC_PID_FD_PATH_MAX, "/proc/" PID_FMT "/fd/%i", pid == 0 ? getpid_cached() : pid, fd));
return buf;
}
+
+int proc_fd_enoent_errno(void) {
+ int r;
+
+ /* When ENOENT is returned during the use of FORMAT_PROC_FD_PATH, it can mean two things:
+ * that the fd does not exist or that /proc/ is not mounted.
+ * Let's make things debuggable and figure out the most appropriate errno. */
+
+ r = proc_mounted();
+ if (r == 0)
+ return -ENOSYS; /* /proc/ is not available or not set up properly, we're most likely
+ in some chroot environment. */
+ return r > 0 ? -EBADF : -ENOENT; /* If /proc/ is definitely around then this means the fd is
+ not valid, otherwise let's propagate the original ENOENT. */
+}
#define FORMAT_PROC_PID_FD_PATH(pid, fd) \
format_proc_pid_fd_path((char[PROC_PID_FD_PATH_MAX]) {}, (pid), (fd))
+int proc_fd_enoent_errno(void);
+
const char *accmode_to_string(int flags);
/* Like ASSERT_PTR, but for fds */
if (errno != ENOENT)
return -errno;
- if (proc_mounted() == 0)
- return -ENOSYS; /* if we have no /proc/, the concept is not implementable */
-
- return -ENOENT;
+ return proc_fd_enoent_errno();
}
return 0;
if (errno != ENOENT)
return -errno;
- if (proc_mounted() == 0)
- return -ENOSYS;
-
- return -ENOENT;
+ return proc_fd_enoent_errno();
}
return 0;
if (errno != ENOENT)
return -errno;
- /* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's
- * make things debuggable and distinguish the two. */
-
- if (proc_mounted() == 0)
- return -ENOSYS; /* /proc is not available or not set up properly, we're most likely in some chroot
- * environment. */
-
- return -EBADF; /* The directory exists, hence it's the fd that doesn't. */
+ return proc_fd_enoent_errno();
}
return 0;
}
int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
- int wd, r;
+ int wd;
+
+ assert(fd >= 0);
+ assert(what >= 0);
/* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */
wd = inotify_add_watch(fd, FORMAT_PROC_FD_PATH(what), mask);
if (errno != ENOENT)
return -errno;
- /* Didn't work with ENOENT? If so, then either /proc/ isn't mounted, or the fd is bad */
- r = proc_mounted();
- if (r == 0)
- return -ENOSYS;
- if (r > 0)
- return -EBADF;
-
- return -ENOENT; /* OK, no clue, let's propagate the original error */
+ return proc_fd_enoent_errno();
}
return wd;