diff options
author | Mike Lockwood <lockwood@android.com> | 2011-02-17 11:39:12 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-02-17 11:39:12 -0800 |
commit | 53df0b6393979e7f35ab271683429aa1ea1230cd (patch) | |
tree | ecd39aabb88612f62c704f4b6a60c140f78f2e6b /toolbox | |
parent | 0569692b730734e73b2f2a13b224c8fb72d3f7c5 (diff) | |
parent | 794cc3fdd32e713145a2aa55c7a34c91d2a8fa5f (diff) | |
download | system_core-53df0b6393979e7f35ab271683429aa1ea1230cd.zip system_core-53df0b6393979e7f35ab271683429aa1ea1230cd.tar.gz system_core-53df0b6393979e7f35ab271683429aa1ea1230cd.tar.bz2 |
Merge "lsof: Add support for printing open files for a single process"
Diffstat (limited to 'toolbox')
-rw-r--r-- | toolbox/lsof.c | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/toolbox/lsof.c b/toolbox/lsof.c index 99891db..c55384b 100644 --- a/toolbox/lsof.c +++ b/toolbox/lsof.c @@ -196,28 +196,37 @@ void lsof_dumpinfo(pid_t pid) int lsof_main(int argc, char *argv[]) { - DIR *dir = opendir("/proc"); - if (dir == NULL) { - fprintf(stderr, "Couldn't open /proc\n"); - return -1; + long int pid = 0; + char* endptr; + if (argc == 2) { + pid = strtol(argv[1], &endptr, 10); } print_header(); - struct dirent* de; - while ((de = readdir(dir))) { - if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) - continue; - - // Only inspect directories that are PID numbers - char* endptr; - long int pid = strtol(de->d_name, &endptr, 10); - if (*endptr != '\0') - continue; - + if (pid) { lsof_dumpinfo(pid); + } else { + DIR *dir = opendir("/proc"); + if (dir == NULL) { + fprintf(stderr, "Couldn't open /proc\n"); + return -1; + } + + struct dirent* de; + while ((de = readdir(dir))) { + if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) + continue; + + // Only inspect directories that are PID numbers + pid = strtol(de->d_name, &endptr, 10); + if (*endptr != '\0') + continue; + + lsof_dumpinfo(pid); + } + closedir(dir); } - closedir(dir); return 0; } |