]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3/lib: add proc fds infrastructure
authorRalph Boehme <slow@samba.org>
Wed, 30 Sep 2020 12:45:34 +0000 (14:45 +0200)
committerRalph Boehme <slow@samba.org>
Wed, 16 Dec 2020 09:08:30 +0000 (09:08 +0000)
Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/include/proto.h
source3/lib/system.c

index 9f74287f9671d31b67b15bde534eb4b1a8b14310..2ce632c54000583ccc298f7cee507c6ce49785c6 100644 (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,
index cf4c4d46c36c6382e837a2cb19d5efbd0c5707d3..8ea2af9f93bbe305b66eb238a7849e4d3e434c02 100644 (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;
+}