]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/fd.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / fd.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2012 Inktank
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "include/compat.h"
16 #include "fd.h"
17
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <dirent.h>
21 #include <errno.h>
22
23 #include "debug.h"
24 #include "errno.h"
25
26 void dump_open_fds(CephContext *cct)
27 {
28 const char *fn = PROCPREFIX "/proc/self/fd";
29 DIR *d = opendir(fn);
30 if (!d) {
31 lderr(cct) << "dump_open_fds unable to open " << fn << dendl;
32 return;
33 }
34 struct dirent *de = nullptr;
35
36 int n = 0;
37 while ((de = ::readdir(d))) {
38 if (de->d_name[0] == '.')
39 continue;
40 char path[PATH_MAX];
41 snprintf(path, sizeof(path), "%s/%s", fn, de->d_name);
42 char target[PATH_MAX];
43 ssize_t r = readlink(path, target, sizeof(target) - 1);
44 if (r < 0) {
45 r = -errno;
46 lderr(cct) << "dump_open_fds unable to readlink " << path << ": " << cpp_strerror(r) << dendl;
47 continue;
48 }
49 target[r] = 0;
50 lderr(cct) << "dump_open_fds " << de->d_name << " -> " << target << dendl;
51 n++;
52 }
53 lderr(cct) << "dump_open_fds dumped " << n << " open files" << dendl;
54
55 closedir(d);
56 }