Hello,
I need to obtain Current Working Directory for terminal. Specifically tmux needs that in order to get pane_current_path command. Current code in tmux uses sysctl(3) with parameters : KERN_PROC and KERN_PROC_CWD. However, if you invoke this as non root user sysctl(3) returns EPERM error for given parameters. If this code is run as root it works as supposed.
This is code snippet from tmux source code (
My question is:
Since kfile_getinfo fails as well, it means that KERN_PROC_FILEDESC does not work either as non root user.
Is there any way to call sysctl with KERN_PROC_CWD or KERN_PROC_FILEDESC as non root user?
Is there any other way to obtain current working directory for process group other than sysctl?
many thanks
Mipko
I need to obtain Current Working Directory for terminal. Specifically tmux needs that in order to get pane_current_path command. Current code in tmux uses sysctl(3) with parameters : KERN_PROC and KERN_PROC_CWD. However, if you invoke this as non root user sysctl(3) returns EPERM error for given parameters. If this code is run as root it works as supposed.
This is code snippet from tmux source code (
Code:
[noparse]static char *
osdep_get_cwd_fallback(int fd)
{
static char wd[PATH_MAX];
struct kinfo_file *info = NULL;
pid_t pgrp;
int nrecords, i;
if ((pgrp = tcgetpgrp(fd)) == -1)
return (NULL);
if ((info = kinfo_getfile(pgrp, &nrecords)) == NULL) //fails with EPER for non root user
return (NULL);
for (i = 0; i < nrecords; i++) {
if (info[i].kf_fd == KF_FD_TYPE_CWD) {
strlcpy(wd, info[i].kf_path, sizeof wd);
free(info);
return (wd);
}
}
free(info);
return (NULL);
}
#ifdef KERN_PROC_CWD
char *
osdep_get_cwd(int fd)
{
static struct kinfo_file info;
static int fallback;
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_CWD, 0 };
size_t len = sizeof info;
if (fallback)
return (osdep_get_cwd_fallback(fd));
if ((name[3] = tcgetpgrp(fd)) == -1)
return (NULL);
if (sysctl(name, 4, &info, &len, NULL, 0) == -1) { //fails wiht EPERM for non root user
if (errno == ENOENT) {
fallback = 1;
return (osdep_get_cwd_fallback(fd));
}
return (NULL);
}
return (info.kf_path);
}
#else /* !KERN_PROC_CWD */
char *
osdep_get_cwd(int fd)
{
return (osdep_get_cwd_fallback(fd));
}
#endif /* KERN_PROC_CWD */[/noparse]
My question is:
Since kfile_getinfo fails as well, it means that KERN_PROC_FILEDESC does not work either as non root user.
Is there any way to call sysctl with KERN_PROC_CWD or KERN_PROC_FILEDESC as non root user?
Is there any other way to obtain current working directory for process group other than sysctl?
many thanks
Mipko