1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-26 21:57:41 +03:00

s3/lib: add proc fds infrastructure

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Ralph Boehme 2020-09-30 14:45:34 +02:00
parent b56f554ff0
commit c39940d81b
2 changed files with 59 additions and 0 deletions

View File

@ -237,6 +237,9 @@ char *sys_realpath(const char *path);
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,

View File

@ -1030,3 +1030,59 @@ int sys_get_number_of_cores(void)
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;
}