static void lsof_dumpinfo(pid_t pid) { int fd; struct pid_info_t info; struct stat pidstat; struct passwd *pw; info.pid = pid; snprintf(info.path, sizeof(info.path), "/proc/%d/", pid); info.parent_length = strlen(info.path); // Get the UID by calling stat on the proc/pid directory. if (!stat(info.path, &pidstat)) { pw = getpwuid(pidstat.st_uid); if (pw) { strlcpy(info.user, pw->pw_name, sizeof(info.user)); } else { snprintf(info.user, USER_DISPLAY_MAX, "%d", (int)pidstat.st_uid); } } else { strcpy(info.user, "???"); } // Read the command line information; each argument is terminated with NULL. strlcat(info.path, "cmdline", sizeof(info.path)); fd = open(info.path, O_RDONLY); if (fd < 0) { fprintf(stderr, "Couldn't read %s\n", info.path); return; } char cmdline[PATH_MAX]; int numRead = read(fd, cmdline, sizeof(cmdline) - 1); close(fd); if (numRead < 0) { fprintf(stderr, "Error reading cmdline: %s: %s\n", info.path, strerror(errno)); return; } cmdline[numRead] = '\0'; // We only want the basename of the cmdline strlcpy(info.cmdline, basename(cmdline), sizeof(info.cmdline)); // Read each of these symlinks print_type("cwd", &info); print_type("exe", &info); print_type("root", &info); print_fds(&info); print_maps(&info); }
static void lsof_dumpinfo(pid_t pid) { memset(&info, 0, sizeof(info)); info.pid = pid; snprintf(info.path, sizeof(info.path), "/proc/%d/", pid); info.parent_length = strlen(info.path); /* read from stat file instead of cmdline, since reading from cmdline requires scheduling and cannot function in panic */ get_cmd(pid, info.cmdline, CMD_DISPLAY_MAX+2); // Read each of these symlinks print_type("cwd", &info); print_type("exe", &info); print_type("root", &info); print_fds(&info); print_maps(&info); }
void process_requests (tmfd_t* asock, size_t *alen, fd_set* rset, struct server_ctx* srv) { size_t nmax = *alen, i = 0, nserved = 0; int rc = 0, served = 0; char param[ 128 ] = "\0"; time_t now = time (NULL); /* uncomment to DEBUG */ TRACE( print_fds (g_flog, "pre-process sockets", asock, nmax) ); for (; i < nmax; ++i, served = 0) { assert (asock[i].fd >= 0); assert (asock[i].atime > 0); do { /* not selected - yet try to time it out */ if (!FD_ISSET(asock[i].fd, rset)) { if ((asock[i].atime + g_uopt.ssel_tmout) < now) { TRACE( (void)tmfprintf (g_flog, "%s: accepted socket [%ld] timed out\n", __func__, (long)asock[i].fd) ); ++served; /* timed out - must close */ } break; } /* selected */ TRACE( (void)tmfprintf (g_flog, "acting on accepted socket " "[%d] (%d/%d)\n", asock[i].fd, i+1, nmax) ); ++served; /* selected - must close regardless */ rc = read_command( asock[i].fd, srv->cmd, sizeof(srv->cmd), param, sizeof(param) ); if( 0 != rc ) break; rc = process_command (asock[i].fd, srv, param, sizeof(param) ); } while (0); if (0 != rc) { TRACE( (void)tmfprintf (g_flog, "error [%d] processing " "client socket [%d]\n", rc, asock[i])); } TRACE( (void)tmfprintf (g_flog, "%s: %s accepted " "socket [%d]\n", __func__, (served ? "closing" : "skipping"), asock[i].fd) ); if (served) { (void) close (asock[i].fd); asock[i].fd = -1; ++nserved; } } /* for */ TRACE( (void)tmfprintf (g_flog, "Processed [%ld/%ld] accepted sockets\n", (long)nserved, (long)nmax) ); TRACE( print_fds (g_flog, "newly-accepted sockets", asock, nmax) ); if (nserved >= nmax) { *alen = 0; TRACE( (void)tmfputs ("All accepted sockets processed\n", g_flog) ); } else { shrink_asock (asock, alen, nserved); } return; }
int main(int argc, char **argv) { const char *pattern = NULL; dev_t device = -1; ino_t node = -1; int32 id = -1; info_mode mode = kList; bool brief = false; // parse arguments if (argc == 2) { // filter output if (isdigit(argv[1][0])) id = atol(argv[1]); else if (argv[1][0] == '-') usage(!strcmp(argv[1], "--help")); else pattern = argv[1]; } else if (argc > 2) { if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "-D")) { // filter by device usage device = dev_for_path(argv[2]); if (device < 0) { fprintf(stderr, "%s: could not find device: %s\n", __progname, strerror(errno)); return 1; } mode = kFilterDevice; if (argv[1][1] == 'D') brief = true; } else if (!strcmp(argv[1], "-f") || !strcmp(argv[1], "-F")) { // filter by file usage struct stat stat; if (::stat(argv[2], &stat) < 0) { fprintf(stderr, "%s: could not open file: %s\n", __progname, strerror(errno)); return 1; } device = stat.st_dev; node = stat.st_ino; mode = kFilterFile; if (argv[1][1] == 'F') brief = true; } else usage(true); } // do the job! team_info info; int32 cookie = 0; while (get_next_team_info(&cookie, &info) == B_OK) { switch (mode) { case kList: if ((id != -1 && id != info.team) || (pattern != NULL && !strstr(info.args, pattern))) continue; print_fds(info); break; case kFilterDevice: filter_device(info, device, brief); break; case kFilterFile: filter_file(info, device, node, brief); break; } } return 0; }