summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny Root2010-07-27 14:58:23 -0500
committerAndroid (Google) Code Review2010-07-27 14:58:23 -0500
commit5a0adf95b1170d3d9ec91b0106662cd17514f0e4 (patch)
treebd7736b947c7734de74e82525201b43b57c60908
parent53308d4cd5c4414402e979a6771f7ef3b35f5c2f (diff)
parent8b9b105bc7bf6428591d55462b3e727ba7504b29 (diff)
downloadplatform-system-core-5a0adf95b1170d3d9ec91b0106662cd17514f0e4.tar.gz
platform-system-core-5a0adf95b1170d3d9ec91b0106662cd17514f0e4.tar.xz
platform-system-core-5a0adf95b1170d3d9ec91b0106662cd17514f0e4.zip
Merge "Add basic lsof command" into gingerbread
-rw-r--r--toolbox/Android.mk3
-rw-r--r--toolbox/lsof.c223
2 files changed, 225 insertions, 1 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 93492764b..588dac0b2 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -53,7 +53,8 @@ TOOLS := \
53 uptime \ 53 uptime \
54 vmstat \ 54 vmstat \
55 nandread \ 55 nandread \
56 ionice 56 ionice \
57 lsof
57 58
58LOCAL_SRC_FILES:= \ 59LOCAL_SRC_FILES:= \
59 toolbox.c \ 60 toolbox.c \
diff --git a/toolbox/lsof.c b/toolbox/lsof.c
new file mode 100644
index 000000000..99891dbf4
--- /dev/null
+++ b/toolbox/lsof.c
@@ -0,0 +1,223 @@
1/*
2 * Copyright (c) 2010, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <dirent.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <libgen.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <unistd.h>
39
40#define BUF_MAX 1024
41#define CMD_DISPLAY_MAX 10
42
43struct pid_info_t {
44 pid_t pid;
45
46 char cmdline[CMD_DISPLAY_MAX];
47
48 char path[PATH_MAX];
49 ssize_t parent_length;
50};
51
52void print_header()
53{
54 printf("%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
55 "COMMAND",
56 "PID",
57 "USER",
58 "FD",
59 "TYPE",
60 "DEVICE",
61 "SIZE/OFF",
62 "NODE",
63 "NAME");
64}
65
66void print_type(char *type, struct pid_info_t* info)
67{
68 static ssize_t link_dest_size;
69 static char link_dest[PATH_MAX];
70
71 strncat(info->path, type, sizeof(info->path));
72 if ((link_dest_size = readlink(info->path, link_dest, sizeof(link_dest)-1)) < 0) {
73 if (errno == ENOENT)
74 goto out;
75
76 snprintf(link_dest, sizeof(link_dest), "%s (readlink: %s)", info->path, strerror(errno));
77 } else {
78 link_dest[link_dest_size] = '\0';
79 }
80
81 // Things that are just the root filesystem are uninteresting (we already know)
82 if (!strcmp(link_dest, "/"))
83 goto out;
84
85 printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n", info->cmdline, info->pid, "???", type,
86 "???", "???", "???", "???", link_dest);
87
88out:
89 info->path[info->parent_length] = '\0';
90}
91
92// Prints out all file that have been memory mapped
93void print_maps(struct pid_info_t* info)
94{
95 FILE *maps;
96 char buffer[PATH_MAX + 100];
97
98 size_t offset;
99 int major, minor;
100 char device[10];
101 long int inode;
102 char file[PATH_MAX];
103
104 strncat(info->path, "maps", sizeof(info->path));
105
106 maps = fopen(info->path, "r");
107 if (!maps)
108 goto out;
109
110 while (fscanf(maps, "%*x-%*x %*s %zx %5s %ld %s\n", &offset, device, &inode,
111 file) == 4) {
112 // We don't care about non-file maps
113 if (inode == 0 || !strcmp(device, "00:00"))
114 continue;
115
116 printf("%-9s %5d %10s %4s %9s %18s %9zd %10ld %s\n", info->cmdline, info->pid, "???", "mem",
117 "???", device, offset, inode, file);
118 }
119
120 fclose(maps);
121
122out:
123 info->path[info->parent_length] = '\0';
124}
125
126// Prints out all open file descriptors
127void print_fds(struct pid_info_t* info)
128{
129 static char* fd_path = "fd/";
130 strncat(info->path, fd_path, sizeof(info->path));
131
132 int previous_length = info->parent_length;
133 info->parent_length += strlen(fd_path);
134
135 DIR *dir = opendir(info->path);
136 if (dir == NULL) {
137 char msg[BUF_MAX];
138 snprintf(msg, sizeof(msg), "%s (opendir: %s)", info->path, strerror(errno));
139 printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n", info->cmdline, info->pid, "???", "FDS",
140 "", "", "", "", msg);
141 goto out;
142 }
143
144 struct dirent* de;
145 while ((de = readdir(dir))) {
146 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
147 continue;
148
149 print_type(de->d_name, info);
150 }
151 closedir(dir);
152
153out:
154 info->parent_length = previous_length;
155 info->path[info->parent_length] = '\0';
156}
157
158void lsof_dumpinfo(pid_t pid)
159{
160 int fd;
161 struct pid_info_t info;
162 info.pid = pid;
163
164 snprintf(info.path, sizeof(info.path), "/proc/%d/", pid);
165
166 info.parent_length = strlen(info.path);
167
168 // Read the command line information; each argument is terminated with NULL.
169 strncat(info.path, "cmdline", sizeof(info.path));
170 fd = open(info.path, O_RDONLY);
171 if (fd < 0) {
172 fprintf(stderr, "Couldn't read %s\n", info.path);
173 return;
174 }
175 char cmdline[PATH_MAX];
176 if (read(fd, cmdline, sizeof(cmdline)) < 0) {
177 fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno));
178 close(fd);
179 return;
180 }
181 close(fd);
182 info.path[info.parent_length] = '\0';
183
184 // We only want the basename of the cmdline
185 strncpy(info.cmdline, basename(cmdline), sizeof(info.cmdline));
186 info.cmdline[sizeof(info.cmdline)-1] = '\0';
187
188 // Read each of these symlinks
189 print_type("cwd", &info);
190 print_type("exe", &info);
191 print_type("root", &info);
192
193 print_fds(&info);
194 print_maps(&info);
195}
196
197int lsof_main(int argc, char *argv[])
198{
199 DIR *dir = opendir("/proc");
200 if (dir == NULL) {
201 fprintf(stderr, "Couldn't open /proc\n");
202 return -1;
203 }
204
205 print_header();
206
207 struct dirent* de;
208 while ((de = readdir(dir))) {
209 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
210 continue;
211
212 // Only inspect directories that are PID numbers
213 char* endptr;
214 long int pid = strtol(de->d_name, &endptr, 10);
215 if (*endptr != '\0')
216 continue;
217
218 lsof_dumpinfo(pid);
219 }
220 closedir(dir);
221
222 return 0;
223}