Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "debug.h"
17 #include "errno.h"
18
19 void dump_open_fds(CephContext *cct)
20 {
21   const char *fn = PROCPREFIX "/proc/self/fd";
22   DIR *d = opendir(fn);
23   if (!d) {
24     lderr(cct) << "dump_open_fds unable to open " << fn << dendl;
25     return;
26   }
27   struct dirent *de = nullptr;
28
29   int n = 0;
30   while ((de = ::readdir(d))) {
31     if (de->d_name[0] == '.')
32       continue;
33     char path[PATH_MAX];
34     snprintf(path, sizeof(path), "%s/%s", fn, de->d_name);
35     char target[PATH_MAX];
36     ssize_t r = readlink(path, target, sizeof(target) - 1);
37     if (r < 0) {
38       r = -errno;
39       lderr(cct) << "dump_open_fds unable to readlink " << path << ": " << cpp_strerror(r) << dendl;
40       continue;
41     }
42     target[r] = 0;
43     lderr(cct) << "dump_open_fds " << de->d_name << " -> " << target << dendl;
44     n++;
45   }
46   lderr(cct) << "dump_open_fds dumped " << n << " open files" << dendl;
47
48   closedir(d);
49 }