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