Skip to content

Commit

Permalink
Use KERN_PROC_CWD if supported, from Tiwei Bie.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicm committed Nov 6, 2014
1 parent 6ca8c58 commit 218b181
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions osdep-freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ osdep_get_name(int fd, char *tty)
return (NULL);
}

char *
osdep_get_cwd(int fd)
static char *
osdep_get_cwd_fallback(int fd)
{
static char wd[PATH_MAX];
struct kinfo_file *info = NULL;
Expand All @@ -158,6 +158,38 @@ osdep_get_cwd(int fd)
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) {
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 */

struct event_base *
osdep_event_init(void)
{
Expand Down

0 comments on commit 218b181

Please sign in to comment.