123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #include "libbb.h"
- int lsof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int lsof_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
- {
- procps_status_t *proc = NULL;
- while ((proc = procps_scan(proc, PSSCAN_PID|PSSCAN_EXE)) != NULL) {
- char name[sizeof("/proc/%u/fd/0123456789") + sizeof(int)*3];
- unsigned baseofs;
- DIR *d_fd;
- char *fdlink;
- struct dirent *entry;
- if (getpid() == proc->pid)
- continue;
- baseofs = sprintf(name, "/proc/%u/fd/", proc->pid);
- d_fd = opendir(name);
- if (d_fd) {
- while ((entry = readdir(d_fd)) != NULL) {
-
- if (entry->d_name[0] == '.')
- continue;
- safe_strncpy(name + baseofs, entry->d_name, 10);
- if ((fdlink = xmalloc_readlink(name)) != NULL) {
- printf("%d\t%s\t%s\n", proc->pid, proc->exe, fdlink);
- free(fdlink);
- }
- }
- closedir(d_fd);
- }
- }
- return EXIT_SUCCESS;
- }
|