| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 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> | 
| Elliott Hughes | a744b05 | 2015-01-28 11:37:57 -0800 | [diff] [blame] | 38 | #include <string.h> | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 39 | #include <unistd.h> | 
|  | 40 |  | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 41 | #include <pwd.h> | 
|  | 42 | #include <sys/stat.h> | 
|  | 43 |  | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 44 | #define BUF_MAX 1024 | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 45 | #define CMD_DISPLAY_MAX (9 + 1) | 
|  | 46 | #define USER_DISPLAY_MAX (10 + 1) | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 47 |  | 
|  | 48 | struct pid_info_t { | 
|  | 49 | pid_t pid; | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 50 | char user[USER_DISPLAY_MAX]; | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | char cmdline[CMD_DISPLAY_MAX]; | 
|  | 53 |  | 
|  | 54 | char path[PATH_MAX]; | 
|  | 55 | ssize_t parent_length; | 
|  | 56 | }; | 
|  | 57 |  | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 58 | static void print_header() | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 59 | { | 
|  | 60 | printf("%-9s %5s %10s %4s %9s %18s %9s %10s %s\n", | 
|  | 61 | "COMMAND", | 
|  | 62 | "PID", | 
|  | 63 | "USER", | 
|  | 64 | "FD", | 
|  | 65 | "TYPE", | 
|  | 66 | "DEVICE", | 
|  | 67 | "SIZE/OFF", | 
|  | 68 | "NODE", | 
|  | 69 | "NAME"); | 
|  | 70 | } | 
|  | 71 |  | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 72 | static void print_type(char *type, struct pid_info_t* info) | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 73 | { | 
|  | 74 | static ssize_t link_dest_size; | 
|  | 75 | static char link_dest[PATH_MAX]; | 
|  | 76 |  | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 77 | strlcat(info->path, type, sizeof(info->path)); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 78 | if ((link_dest_size = readlink(info->path, link_dest, sizeof(link_dest)-1)) < 0) { | 
|  | 79 | if (errno == ENOENT) | 
|  | 80 | goto out; | 
|  | 81 |  | 
|  | 82 | snprintf(link_dest, sizeof(link_dest), "%s (readlink: %s)", info->path, strerror(errno)); | 
|  | 83 | } else { | 
|  | 84 | link_dest[link_dest_size] = '\0'; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | // Things that are just the root filesystem are uninteresting (we already know) | 
|  | 88 | if (!strcmp(link_dest, "/")) | 
|  | 89 | goto out; | 
|  | 90 |  | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 91 | printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n", | 
|  | 92 | info->cmdline, info->pid, info->user, type, | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 93 | "???", "???", "???", "???", link_dest); | 
|  | 94 |  | 
|  | 95 | out: | 
|  | 96 | info->path[info->parent_length] = '\0'; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | // Prints out all file that have been memory mapped | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 100 | static void print_maps(struct pid_info_t* info) | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 101 | { | 
|  | 102 | FILE *maps; | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | size_t offset; | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 105 | char device[10]; | 
|  | 106 | long int inode; | 
| Elliott Hughes | 008efb7 | 2016-04-19 14:57:09 -0700 | [diff] [blame] | 107 | char file[1024]; | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 108 |  | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 109 | strlcat(info->path, "maps", sizeof(info->path)); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | maps = fopen(info->path, "r"); | 
|  | 112 | if (!maps) | 
|  | 113 | goto out; | 
|  | 114 |  | 
| Elliott Hughes | 008efb7 | 2016-04-19 14:57:09 -0700 | [diff] [blame] | 115 | while (fscanf(maps, "%*x-%*x %*s %zx %5s %ld %1023s\n", | 
|  | 116 | &offset, device, &inode, file) == 4) { | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 117 | // We don't care about non-file maps | 
|  | 118 | if (inode == 0 || !strcmp(device, "00:00")) | 
|  | 119 | continue; | 
|  | 120 |  | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 121 | printf("%-9s %5d %10s %4s %9s %18s %9zd %10ld %s\n", | 
|  | 122 | info->cmdline, info->pid, info->user, "mem", | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 123 | "???", device, offset, inode, file); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | fclose(maps); | 
|  | 127 |  | 
|  | 128 | out: | 
|  | 129 | info->path[info->parent_length] = '\0'; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | // Prints out all open file descriptors | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 133 | static void print_fds(struct pid_info_t* info) | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 134 | { | 
|  | 135 | static char* fd_path = "fd/"; | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 136 | strlcat(info->path, fd_path, sizeof(info->path)); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 137 |  | 
|  | 138 | int previous_length = info->parent_length; | 
|  | 139 | info->parent_length += strlen(fd_path); | 
|  | 140 |  | 
|  | 141 | DIR *dir = opendir(info->path); | 
|  | 142 | if (dir == NULL) { | 
|  | 143 | char msg[BUF_MAX]; | 
|  | 144 | snprintf(msg, sizeof(msg), "%s (opendir: %s)", info->path, strerror(errno)); | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 145 | printf("%-9s %5d %10s %4s %9s %18s %9s %10s %s\n", | 
|  | 146 | info->cmdline, info->pid, info->user, "FDS", | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 147 | "", "", "", "", msg); | 
|  | 148 | goto out; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | struct dirent* de; | 
|  | 152 | while ((de = readdir(dir))) { | 
|  | 153 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) | 
|  | 154 | continue; | 
|  | 155 |  | 
|  | 156 | print_type(de->d_name, info); | 
|  | 157 | } | 
|  | 158 | closedir(dir); | 
|  | 159 |  | 
|  | 160 | out: | 
|  | 161 | info->parent_length = previous_length; | 
|  | 162 | info->path[info->parent_length] = '\0'; | 
|  | 163 | } | 
|  | 164 |  | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 165 | static void lsof_dumpinfo(pid_t pid) | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 166 | { | 
|  | 167 | int fd; | 
|  | 168 | struct pid_info_t info; | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 169 | struct stat pidstat; | 
|  | 170 | struct passwd *pw; | 
|  | 171 |  | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 172 | info.pid = pid; | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 173 | snprintf(info.path, sizeof(info.path), "/proc/%d/", pid); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 174 | info.parent_length = strlen(info.path); | 
|  | 175 |  | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 176 | // Get the UID by calling stat on the proc/pid directory. | 
|  | 177 | if (!stat(info.path, &pidstat)) { | 
|  | 178 | pw = getpwuid(pidstat.st_uid); | 
|  | 179 | if (pw) { | 
| Kenny Root | b953fc2 | 2011-12-01 11:38:53 -0800 | [diff] [blame] | 180 | strlcpy(info.user, pw->pw_name, sizeof(info.user)); | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 181 | } else { | 
|  | 182 | snprintf(info.user, USER_DISPLAY_MAX, "%d", (int)pidstat.st_uid); | 
|  | 183 | } | 
|  | 184 | } else { | 
|  | 185 | strcpy(info.user, "???"); | 
|  | 186 | } | 
|  | 187 |  | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 188 | // Read the command line information; each argument is terminated with NULL. | 
| Nick Kralevich | 960ac43 | 2013-06-03 12:10:30 -0700 | [diff] [blame] | 189 | strlcat(info.path, "cmdline", sizeof(info.path)); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 190 | fd = open(info.path, O_RDONLY); | 
|  | 191 | if (fd < 0) { | 
|  | 192 | fprintf(stderr, "Couldn't read %s\n", info.path); | 
|  | 193 | return; | 
|  | 194 | } | 
| Kenny Root | b953fc2 | 2011-12-01 11:38:53 -0800 | [diff] [blame] | 195 |  | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 196 | char cmdline[PATH_MAX]; | 
| Kenny Root | b953fc2 | 2011-12-01 11:38:53 -0800 | [diff] [blame] | 197 | int numRead = read(fd, cmdline, sizeof(cmdline) - 1); | 
|  | 198 | close(fd); | 
|  | 199 |  | 
|  | 200 | if (numRead < 0) { | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 201 | fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno)); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 202 | return; | 
|  | 203 | } | 
| Kenny Root | b953fc2 | 2011-12-01 11:38:53 -0800 | [diff] [blame] | 204 |  | 
|  | 205 | cmdline[numRead] = '\0'; | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 206 |  | 
|  | 207 | // We only want the basename of the cmdline | 
| Kenny Root | b953fc2 | 2011-12-01 11:38:53 -0800 | [diff] [blame] | 208 | strlcpy(info.cmdline, basename(cmdline), sizeof(info.cmdline)); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 209 |  | 
|  | 210 | // Read each of these symlinks | 
|  | 211 | print_type("cwd", &info); | 
|  | 212 | print_type("exe", &info); | 
|  | 213 | print_type("root", &info); | 
|  | 214 |  | 
|  | 215 | print_fds(&info); | 
|  | 216 | print_maps(&info); | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | int lsof_main(int argc, char *argv[]) | 
|  | 220 | { | 
| Mike Lockwood | 794cc3f | 2010-12-22 16:37:36 -0500 | [diff] [blame] | 221 | long int pid = 0; | 
|  | 222 | char* endptr; | 
|  | 223 | if (argc == 2) { | 
|  | 224 | pid = strtol(argv[1], &endptr, 10); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | print_header(); | 
|  | 228 |  | 
| Mike Lockwood | 794cc3f | 2010-12-22 16:37:36 -0500 | [diff] [blame] | 229 | if (pid) { | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 230 | lsof_dumpinfo(pid); | 
| Mike Lockwood | 794cc3f | 2010-12-22 16:37:36 -0500 | [diff] [blame] | 231 | } else { | 
|  | 232 | DIR *dir = opendir("/proc"); | 
|  | 233 | if (dir == NULL) { | 
|  | 234 | fprintf(stderr, "Couldn't open /proc\n"); | 
|  | 235 | return -1; | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | struct dirent* de; | 
|  | 239 | while ((de = readdir(dir))) { | 
|  | 240 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) | 
|  | 241 | continue; | 
|  | 242 |  | 
|  | 243 | // Only inspect directories that are PID numbers | 
| Jeff Brown | f96993e | 2011-06-29 15:00:39 -0700 | [diff] [blame] | 244 | pid = strtol(de->d_name, &endptr, 10); | 
| Mike Lockwood | 794cc3f | 2010-12-22 16:37:36 -0500 | [diff] [blame] | 245 | if (*endptr != '\0') | 
|  | 246 | continue; | 
|  | 247 |  | 
|  | 248 | lsof_dumpinfo(pid); | 
|  | 249 | } | 
|  | 250 | closedir(dir); | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 251 | } | 
| Kenny Root | 8b9b105 | 2010-07-27 09:20:02 -0700 | [diff] [blame] | 252 |  | 
|  | 253 | return 0; | 
|  | 254 | } |