int sys_get_number_of_cores(void);
#endif
+struct sys_proc_fd_path_buf {
+ char buf[35]; /* "/proc/self/fd/" + strlen(2^64) + 0-terminator */
+};
bool sys_have_proc_fds(void);
-const char *sys_proc_fd_path(int fd, char *buf, size_t bufsize);
+char *sys_proc_fd_path(int fd, struct sys_proc_fd_path_buf *buf);
struct stat;
void init_stat_ex_from_stat (struct stat_ex *dst,
}
#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;
+ static bool checked = false;
+ static bool have_proc_fds = false;
struct stat sb;
int ret;
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;
- }
-
+ ret = stat("/proc/self/fd/0", &sb);
+ have_proc_fds = (ret == 0);
checked = true;
+
return have_proc_fds;
}
-const char *sys_proc_fd_path(int fd, char *buf, size_t bufsize)
+char *sys_proc_fd_path(int fd, struct sys_proc_fd_path_buf *buf)
{
- int written;
+ int written =
+ snprintf(buf->buf, sizeof(buf->buf), "/proc/self/fd/%d", fd);
- if (!sys_have_proc_fds()) {
- return NULL;
- }
-
-#if defined(__clang__)
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat-nonliteral"
-#endif
- written = snprintf(buf,
- bufsize,
- proc_fd_pattern,
- fd);
-#if defined(__clang__)
-#pragma clang diagnostic pop
-#endif
- if (written >= bufsize) {
- return NULL;
- }
+ SMB_ASSERT(sys_have_proc_fds() && (written >= 0));
- return buf;
+ return buf->buf;
}
struct files_struct *fsp,
uint16_t *_compression_fmt)
{
- char buf[PATH_MAX];
- const char *p = NULL;
+ struct sys_proc_fd_path_buf buf;
int ret;
long flags = 0;
int fsp_fd = fsp_get_pathref_fd(fsp);
return NT_STATUS_NOT_IMPLEMENTED;
}
- p = sys_proc_fd_path(fsp_fd, buf, sizeof(buf));
- if (p == NULL) {
- return NT_STATUS_NO_MEMORY;
- }
-
- fd = open(p, O_RDONLY);
+ fd = open(sys_proc_fd_path(fsp_fd, &buf), O_RDONLY);
if (fd == -1) {
- DBG_DEBUG("/proc open of %s failed: %s\n", p, strerror(errno));
+ DBG_DEBUG("/proc open of %s failed: %s\n",
+ buf.buf,
+ strerror(errno));
return map_nt_error_from_unix(errno);
}
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *p = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
+
+ result = chmod(sys_proc_fd_path(fd, &buf), mode);
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p != NULL) {
- result = chmod(p, mode);
- } else {
- result = -1;
- }
END_PROFILE(syscall_fchmod);
return result;
}
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *p = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
+
+ result = chown(sys_proc_fd_path(fd, &buf), uid, gid);
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p != NULL) {
- result = chown(p, uid, gid);
- } else {
- result = -1;
- }
END_PROFILE(syscall_fchown);
return result;
}
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *p = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p != NULL) {
- /*
- * The dirfd argument of utimensat is ignored when
- * pathname is an absolute path
- */
- result = utimensat(AT_FDCWD, p, times, 0);
- } else {
- result = -1;
- }
+ result = utimensat(AT_FDCWD,
+ sys_proc_fd_path(fd, &buf),
+ times,
+ 0);
goto out;
}
}
if (fsp->fsp_flags.have_proc_fds) {
- const char *p = NULL;
- char buf[PATH_MAX];
-
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
+ struct sys_proc_fd_path_buf buf;
- return chflags(p, flags);
+ return chflags(sys_proc_fd_path(fd, &buf), flags);
}
/*
}
if (fsp->fsp_flags.have_proc_fds) {
- const char *p = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
-
- return getxattr(p, name, value, size);
+ return getxattr(sys_proc_fd_path(fd, &buf), name, value, size);
}
/*
}
if (fsp->fsp_flags.have_proc_fds) {
- const char *p = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
-
- return listxattr(p, list, size);
+ return listxattr(sys_proc_fd_path(fd, &buf), list, size);
}
/*
}
if (fsp->fsp_flags.have_proc_fds) {
- const char *p = NULL;
- char buf[PATH_MAX];
-
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
+ struct sys_proc_fd_path_buf buf;
- return removexattr(p, name);
+ return removexattr(sys_proc_fd_path(fd, &buf), name);
}
/*
}
if (fsp->fsp_flags.have_proc_fds) {
- const char *p = NULL;
- char buf[PATH_MAX];
-
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
+ struct sys_proc_fd_path_buf buf;
- return setxattr(p, name, value, size, flags);
+ return setxattr(sys_proc_fd_path(fd, &buf),
+ name,
+ value,
+ size,
+ flags);
}
/*
{
struct gpfs_config_data *config;
int fd = fsp_get_pathref_fd(fsp);
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
const char *p = NULL;
struct gpfs_iattr64 iattr = { };
unsigned int litemask = 0;
if (fsp->fsp_flags.is_pathref && !config->pathref_ok.gpfs_fstat_x) {
if (fsp->fsp_flags.have_proc_fds) {
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return NT_STATUS_NO_MEMORY;
- }
+ p = sys_proc_fd_path(fd, &buf);
} else {
p = fsp->fsp_name->base_name;
}
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *p = NULL;
- char buf[PATH_MAX];
-
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return NT_STATUS_NO_MEMORY;
- }
+ struct sys_proc_fd_path_buf buf;
- ret = gpfswrap_set_winattrs_path(p,
+ ret = gpfswrap_set_winattrs_path(sys_proc_fd_path(fd, &buf),
GPFS_WINATTR_SET_ATTRS,
&attrs);
if (ret == -1) {
- DBG_WARNING("Setting winattrs failed for [%s][%s]: %s\n",
- p, fsp_str_dbg(fsp), strerror(errno));
+ DBG_WARNING("Setting winattrs failed for "
+ "[%s][%s]: %s\n",
+ buf.buf,
+ fsp_str_dbg(fsp),
+ strerror(errno));
return map_nt_error_from_unix(errno);
}
return NT_STATUS_OK;
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *p = NULL;
- char buf[PATH_MAX];
-
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
+ struct sys_proc_fd_path_buf buf;
- rc = gpfswrap_set_times_path(buf, flags, gpfs_times);
+ rc = gpfswrap_set_times_path(sys_proc_fd_path(fd, &buf),
+ flags,
+ gpfs_times);
if (rc != 0) {
DBG_WARNING("gpfs_set_times_path(%s,%s) failed: %s\n",
- fsp_str_dbg(fsp), p, strerror(errno));
+ fsp_str_dbg(fsp),
+ buf.buf,
+ strerror(errno));
}
return rc;
}
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *p = NULL;
- char buf[PATH_MAX];
-
- p = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (p == NULL) {
- return -1;
- }
+ struct sys_proc_fd_path_buf buf;
- ret = gpfswrap_set_winattrs_path(p,
- GPFS_WINATTR_SET_CREATION_TIME,
- &attrs);
+ ret = gpfswrap_set_winattrs_path(
+ sys_proc_fd_path(fd, &buf),
+ GPFS_WINATTR_SET_CREATION_TIME,
+ &attrs);
if (ret == -1 && errno != ENOSYS) {
DBG_WARNING("Set GPFS ntimes failed %d\n", ret);
return -1;
acl = acl_get_fd(fsp_get_io_fd(fsp));
} else if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *proc_fd_path = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
- proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (proc_fd_path == NULL) {
- return NULL;
- }
-
- acl = acl_get_file(proc_fd_path, acl_type);
+ acl = acl_get_file(sys_proc_fd_path(fd, &buf), acl_type);
} else {
/*
* This is no longer a handle based call.
if (!fsp->fsp_flags.is_pathref && type == SMB_ACL_TYPE_ACCESS) {
res = acl_set_fd(fd, acl);
} else if (fsp->fsp_flags.have_proc_fds) {
- const char *proc_fd_path = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
- proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (proc_fd_path == NULL) {
- acl_free(acl);
- return -1;
- }
- res = acl_set_file(proc_fd_path, acl_type, acl);
+ res = acl_set_file(sys_proc_fd_path(fd, &buf), acl_type, acl);
} else {
/*
* This is no longer a handle based call.
{
if (fsp->fsp_flags.have_proc_fds) {
int fd = fsp_get_pathref_fd(fsp);
- const char *proc_fd_path = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
- proc_fd_path = sys_proc_fd_path(fd, buf, sizeof(buf));
- if (proc_fd_path == NULL) {
- return -1;
- }
- return acl_delete_def_file(proc_fd_path);
+ return acl_delete_def_file(sys_proc_fd_path(fd, &buf));
}
/*
const struct vfs_open_how *how)
{
struct smb_filename proc_fname;
- const char *p = NULL;
- char buf[PATH_MAX];
+ struct sys_proc_fd_path_buf buf;
int old_fd;
int new_fd;
NTSTATUS status;
return NT_STATUS_INVALID_HANDLE;
}
- p = sys_proc_fd_path(old_fd, buf, sizeof(buf));
- if (p == NULL) {
- return NT_STATUS_NO_MEMORY;
- }
-
- proc_fname = (struct smb_filename) {
- .base_name = discard_const_p(char, p),
+ proc_fname = (struct smb_filename){
+ .base_name = sys_proc_fd_path(old_fd, &buf),
};
fsp->fsp_flags.is_pathref = false;