Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug: descriptors list netbsd. #2018

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion librz/debug/p/debug_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ static RzList *rz_debug_desc_native_list(int pid) {
return xnu_desc_list(pid);
#elif __WINDOWS__
return w32_desc_list(pid);
#elif __KFBSD__
#elif __KFBSD__ || __NetBSD__
return bsd_desc_list(pid);
#elif __linux__
return linux_desc_list(pid);
Expand Down
68 changes: 67 additions & 1 deletion librz/debug/p/native/bsd/bsd_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,74 @@ RzList *bsd_desc_list(int pid) {

free(buf);
return ret;
#elif __NetBSD__
RzList *ret = NULL;
char path[512], file[512], buf[512];
struct dirent *de;
RzDebugDesc *desc;
int type, perm;
int len, len2;
struct stat st;
DIR *dd = NULL;

rz_strf(path, "/proc/%i/fd/", pid);
if (!(dd = opendir(path))) {
rz_sys_perror("opendir /proc/x/fd");
return NULL;
}
ret = rz_list_newf((RzListFree)rz_debug_desc_free);
if (!ret) {
closedir(dd);
return NULL;
}
while ((de = (struct dirent *)readdir(dd))) {
if (de->d_name[0] == '.') {
continue;
}
len = strlen(path);
len2 = strlen(de->d_name);
if (len + len2 + 1 >= sizeof(file)) {
RZ_LOG_ERROR("Filename is too long.\n");
goto fail;
}
memcpy(file, path, len);
memcpy(file + len, de->d_name, len2 + 1);
buf[0] = 0;
if (readlink(file, buf, sizeof(buf) - 1) == -1) {
RZ_LOG_ERROR("readlink %s failed.\n", file);
goto fail;
}
buf[sizeof(buf) - 1] = 0;
type = perm = 0;
if (stat(file, &st) != -1) {
type = st.st_mode & S_IFIFO ? 'P' : st.st_mode & S_IFSOCK ? 'S'
: st.st_mode & S_IFCHR ? 'C'
: '-';
}
if (lstat(path, &st) != -1) {
if (st.st_mode & S_IRUSR) {
perm |= RZ_PERM_R;
}
if (st.st_mode & S_IWUSR) {
perm |= RZ_PERM_W;
}
}
//TODO: Offset
desc = rz_debug_desc_new(atoi(de->d_name), buf, perm, type, 0);
if (!desc) {
break;
}
rz_list_append(ret, desc);
}
closedir(dd);
return ret;

fail:
rz_list_free(ret);
closedir(dd);
return NULL;
#else
return false;
return NULL;
#endif
}

Expand Down
19 changes: 11 additions & 8 deletions librz/debug/p/native/linux/linux_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,34 +1301,32 @@ RzList *linux_desc_list(int pid) {
struct stat st;
DIR *dd = NULL;

snprintf(path, sizeof(path), "/proc/%i/fd/", pid);
rz_strf(path, "/proc/%i/fd/", pid);
if (!(dd = opendir(path))) {
rz_sys_perror("opendir /proc/x/fd");
return NULL;
}
ret = rz_list_new();
ret = rz_list_newf((RzListFree)rz_debug_desc_free);
if (!ret) {
closedir(dd);
return NULL;
}
ret->free = (RzListFree)rz_debug_desc_free;
while ((de = (struct dirent *)readdir(dd))) {
if (de->d_name[0] == '.') {
continue;
}
len = strlen(path);
len2 = strlen(de->d_name);
if (len + len2 + 1 >= sizeof(file)) {
rz_list_free(ret);
closedir(dd);
eprintf("Filename is too long");
return NULL;
RZ_LOG_ERROR("Filename is too long.\n");
goto fail;
}
memcpy(file, path, len);
memcpy(file + len, de->d_name, len2 + 1);
buf[0] = 0;
if (readlink(file, buf, sizeof(buf) - 1) == -1) {
return NULL;
RZ_LOG_ERROR("readlink %s failed.\n", file);
goto fail;
}
buf[sizeof(buf) - 1] = 0;
type = perm = 0;
Expand Down Expand Up @@ -1358,6 +1356,11 @@ RzList *linux_desc_list(int pid) {
}
closedir(dd);
return ret;

fail:
rz_list_free(ret);
closedir(dd);
return NULL;
}

#endif