int sys_get_number_of_cores(void);
#endif
+bool sys_have_proc_fds(void);
+const char *sys_proc_fd_path(int fd, char *buf, int bufsize);
+
struct stat;
void init_stat_ex_from_stat (struct stat_ex *dst,
const struct stat *src,
return ret;
}
#endif
+
+static struct proc_fd_pattern {
+ const char *pattern;
+ const char *test_path;
+} proc_fd_patterns[] = {
+ /* Linux */
+ { "/proc/self/fd/%d", "/proc/self/fd/0" },
+ { NULL, NULL },
+};
+
+static const char *proc_fd_pattern;
+
+bool sys_have_proc_fds(void)
+{
+ static bool checked;
+ static bool have_proc_fds;
+ struct proc_fd_pattern *p = NULL;
+ struct stat sb;
+ int ret;
+
+ if (checked) {
+ return have_proc_fds;
+ }
+
+ for (p = &proc_fd_patterns[0]; p->test_path != NULL; p++) {
+ ret = stat(p->test_path, &sb);
+ if (ret != 0) {
+ continue;
+ }
+ have_proc_fds = true;
+ proc_fd_pattern = p->pattern;
+ break;
+ }
+
+ checked = true;
+ return have_proc_fds;
+}
+
+const char *sys_proc_fd_path(int fd, char *buf, int bufsize)
+{
+ int written;
+
+ if (!sys_have_proc_fds()) {
+ return NULL;
+ }
+
+ written = snprintf(buf,
+ bufsize,
+ proc_fd_pattern,
+ fd);
+ if (written >= bufsize) {
+ return NULL;
+ }
+
+ return buf;
+}