summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2010-12-22 16:37:36 -0500
committerMike Lockwood <lockwood@android.com>2010-12-22 16:39:12 -0500
commit794cc3fdd32e713145a2aa55c7a34c91d2a8fa5f (patch)
tree03cf883c2547bb101fa16ae391c13b4cee33da44
parent752923c168009d03e9e00e590155fbd0a2880ccb (diff)
downloadsystem_core-794cc3fdd32e713145a2aa55c7a34c91d2a8fa5f.tar.gz
system_core-794cc3fdd32e713145a2aa55c7a34c91d2a8fa5f.tar.bz2
system_core-794cc3fdd32e713145a2aa55c7a34c91d2a8fa5f.zip
lsof: Add support for printing open files for a single process
Change-Id: If2afa0937064dffca6df2a8642ca75009dc6e70e Signed-off-by: Mike Lockwood <lockwood@android.com>
-rw-r--r--toolbox/lsof.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/toolbox/lsof.c b/toolbox/lsof.c
index 99891dbf4..c55384bb3 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;
}