blob: bee981dee4b263719ba989adf6cf38483da44d94 [file] [log] [blame]
Kenny Root8b9b1052010-07-27 09:20:02 -07001/*
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
Jeff Brownf96993e2011-06-29 15:00:39 -070040#include <pwd.h>
41#include <sys/stat.h>
42
Kenny Root8b9b1052010-07-27 09:20:02 -070043#define BUF_MAX 1024
Jeff Brownf96993e2011-06-29 15:00:39 -070044#define CMD_DISPLAY_MAX (9 + 1)
45#define USER_DISPLAY_MAX (10 + 1)
Kenny Root8b9b1052010-07-27 09:20:02 -070046
47struct pid_info_t {
48 pid_t pid;
Jeff Brownf96993e2011-06-29 15:00:39 -070049 char user[USER_DISPLAY_MAX];
Kenny Root8b9b1052010-07-27 09:20:02 -070050
51 char cmdline[CMD_DISPLAY_MAX];
52
53 char path[PATH_MAX];
54 ssize_t parent_length;
55};
56
Nick Kralevich960ac432013-06-03 12:10:30 -070057static void print_header()
Kenny Root8b9b1052010-07-27 09:20:02 -070058{
59 printf("%-9s %5s %10s %4s %9s %18s %9s %10s %s\n",
60 "COMMAND",
61 "PID",
62 "USER",
63 "FD",
64 "TYPE",
65 "DEVICE",
66 "SIZE/OFF",
67 "NODE",
68 "NAME");
69}
70
Nick Kralevich960ac432013-06-03 12:10:30 -070071static void print_type(char *type, struct pid_info_t* info)
Kenny Root8b9b1052010-07-27 09:20:02 -070072{
73 static ssize_t link_dest_size;
74 static char link_dest[PATH_MAX];
75
Nick Kralevich960ac432013-06-03 12:10:30 -070076 strlcat(info->path, type, sizeof(info->path));
Kenny Root8b9b1052010-07-27 09:20:02 -070077 if ((link_dest_size = readlink(info->path, link_dest, sizeof(link_dest)-1)) < 0) {
78 if (errno == ENOENT)
79 goto out;
80
81 snprintf(link_dest, sizeof(link_dest), "%s (readlink: %s)", info->path, strerror(errno));
82 } else {
83 link_dest[link_dest_size] = '\0';
84 }
85
86 // Things that are just the root filesystem are uninteresting (we already know)
87 if (!strcmp(link_dest, "/"))
88 goto out;
89
Jeff Brownf96993e2011-06-29 15:00:39 -070090 printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n",
91 info->cmdline, info->pid, info->user, type,
Kenny Root8b9b1052010-07-27 09:20:02 -070092 "???", "???", "???", "???", link_dest);
93
94out:
95 info->path[info->parent_length] = '\0';
96}
97
98// Prints out all file that have been memory mapped
Nick Kralevich960ac432013-06-03 12:10:30 -070099static void print_maps(struct pid_info_t* info)
Kenny Root8b9b1052010-07-27 09:20:02 -0700100{
101 FILE *maps;
Kenny Root8b9b1052010-07-27 09:20:02 -0700102 size_t offset;
Kenny Root8b9b1052010-07-27 09:20:02 -0700103 char device[10];
104 long int inode;
105 char file[PATH_MAX];
106
Nick Kralevich960ac432013-06-03 12:10:30 -0700107 strlcat(info->path, "maps", sizeof(info->path));
Kenny Root8b9b1052010-07-27 09:20:02 -0700108
109 maps = fopen(info->path, "r");
110 if (!maps)
111 goto out;
112
Dmitriy Ivanov7c1fc0e2014-03-04 11:17:57 -0800113 while (fscanf(maps, "%*x-%*x %*s %zx %s %ld %s\n", &offset, device, &inode,
Kenny Root8b9b1052010-07-27 09:20:02 -0700114 file) == 4) {
115 // We don't care about non-file maps
116 if (inode == 0 || !strcmp(device, "00:00"))
117 continue;
118
Jeff Brownf96993e2011-06-29 15:00:39 -0700119 printf("%-9s %5d %10s %4s %9s %18s %9zd %10ld %s\n",
120 info->cmdline, info->pid, info->user, "mem",
Kenny Root8b9b1052010-07-27 09:20:02 -0700121 "???", device, offset, inode, file);
122 }
123
124 fclose(maps);
125
126out:
127 info->path[info->parent_length] = '\0';
128}
129
130// Prints out all open file descriptors
Nick Kralevich960ac432013-06-03 12:10:30 -0700131static void print_fds(struct pid_info_t* info)
Kenny Root8b9b1052010-07-27 09:20:02 -0700132{
133 static char* fd_path = "fd/";
Nick Kralevich960ac432013-06-03 12:10:30 -0700134 strlcat(info->path, fd_path, sizeof(info->path));
Kenny Root8b9b1052010-07-27 09:20:02 -0700135
136 int previous_length = info->parent_length;
137 info->parent_length += strlen(fd_path);
138
139 DIR *dir = opendir(info->path);
140 if (dir == NULL) {
141 char msg[BUF_MAX];
142 snprintf(msg, sizeof(msg), "%s (opendir: %s)", info->path, strerror(errno));
Jeff Brownf96993e2011-06-29 15:00:39 -0700143 printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n",
144 info->cmdline, info->pid, info->user, "FDS",
Kenny Root8b9b1052010-07-27 09:20:02 -0700145 "", "", "", "", msg);
146 goto out;
147 }
148
149 struct dirent* de;
150 while ((de = readdir(dir))) {
151 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
152 continue;
153
154 print_type(de->d_name, info);
155 }
156 closedir(dir);
157
158out:
159 info->parent_length = previous_length;
160 info->path[info->parent_length] = '\0';
161}
162
Nick Kralevich960ac432013-06-03 12:10:30 -0700163static void lsof_dumpinfo(pid_t pid)
Kenny Root8b9b1052010-07-27 09:20:02 -0700164{
165 int fd;
166 struct pid_info_t info;
Jeff Brownf96993e2011-06-29 15:00:39 -0700167 struct stat pidstat;
168 struct passwd *pw;
169
Kenny Root8b9b1052010-07-27 09:20:02 -0700170 info.pid = pid;
Kenny Root8b9b1052010-07-27 09:20:02 -0700171 snprintf(info.path, sizeof(info.path), "/proc/%d/", pid);
Kenny Root8b9b1052010-07-27 09:20:02 -0700172 info.parent_length = strlen(info.path);
173
Jeff Brownf96993e2011-06-29 15:00:39 -0700174 // Get the UID by calling stat on the proc/pid directory.
175 if (!stat(info.path, &pidstat)) {
176 pw = getpwuid(pidstat.st_uid);
177 if (pw) {
Kenny Rootb953fc22011-12-01 11:38:53 -0800178 strlcpy(info.user, pw->pw_name, sizeof(info.user));
Jeff Brownf96993e2011-06-29 15:00:39 -0700179 } else {
180 snprintf(info.user, USER_DISPLAY_MAX, "%d", (int)pidstat.st_uid);
181 }
182 } else {
183 strcpy(info.user, "???");
184 }
185
Kenny Root8b9b1052010-07-27 09:20:02 -0700186 // Read the command line information; each argument is terminated with NULL.
Nick Kralevich960ac432013-06-03 12:10:30 -0700187 strlcat(info.path, "cmdline", sizeof(info.path));
Kenny Root8b9b1052010-07-27 09:20:02 -0700188 fd = open(info.path, O_RDONLY);
189 if (fd < 0) {
190 fprintf(stderr, "Couldn't read %s\n", info.path);
191 return;
192 }
Kenny Rootb953fc22011-12-01 11:38:53 -0800193
Kenny Root8b9b1052010-07-27 09:20:02 -0700194 char cmdline[PATH_MAX];
Kenny Rootb953fc22011-12-01 11:38:53 -0800195 int numRead = read(fd, cmdline, sizeof(cmdline) - 1);
196 close(fd);
197
198 if (numRead < 0) {
Kenny Root8b9b1052010-07-27 09:20:02 -0700199 fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno));
Kenny Root8b9b1052010-07-27 09:20:02 -0700200 return;
201 }
Kenny Rootb953fc22011-12-01 11:38:53 -0800202
203 cmdline[numRead] = '\0';
Kenny Root8b9b1052010-07-27 09:20:02 -0700204
205 // We only want the basename of the cmdline
Kenny Rootb953fc22011-12-01 11:38:53 -0800206 strlcpy(info.cmdline, basename(cmdline), sizeof(info.cmdline));
Kenny Root8b9b1052010-07-27 09:20:02 -0700207
208 // Read each of these symlinks
209 print_type("cwd", &info);
210 print_type("exe", &info);
211 print_type("root", &info);
212
213 print_fds(&info);
214 print_maps(&info);
215}
216
217int lsof_main(int argc, char *argv[])
218{
Mike Lockwood794cc3f2010-12-22 16:37:36 -0500219 long int pid = 0;
220 char* endptr;
221 if (argc == 2) {
222 pid = strtol(argv[1], &endptr, 10);
Kenny Root8b9b1052010-07-27 09:20:02 -0700223 }
224
225 print_header();
226
Mike Lockwood794cc3f2010-12-22 16:37:36 -0500227 if (pid) {
Kenny Root8b9b1052010-07-27 09:20:02 -0700228 lsof_dumpinfo(pid);
Mike Lockwood794cc3f2010-12-22 16:37:36 -0500229 } else {
230 DIR *dir = opendir("/proc");
231 if (dir == NULL) {
232 fprintf(stderr, "Couldn't open /proc\n");
233 return -1;
234 }
235
236 struct dirent* de;
237 while ((de = readdir(dir))) {
238 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
239 continue;
240
241 // Only inspect directories that are PID numbers
Jeff Brownf96993e2011-06-29 15:00:39 -0700242 pid = strtol(de->d_name, &endptr, 10);
Mike Lockwood794cc3f2010-12-22 16:37:36 -0500243 if (*endptr != '\0')
244 continue;
245
246 lsof_dumpinfo(pid);
247 }
248 closedir(dir);
Kenny Root8b9b1052010-07-27 09:20:02 -0700249 }
Kenny Root8b9b1052010-07-27 09:20:02 -0700250
251 return 0;
252}