CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FileDescriptorHandler.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <sstream>
5 
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <dirent.h>
9 
10 #include <stdlib.h>
11 #include <unistd.h>
12 
13 #include <vector>
14 
15 namespace evf{
16 
18 
19  //find all socket file descriptors inherited from parent process and close them
20 
21  pid_t pid = ::getpid();
22  std::ostringstream ost;
23  ost << "/proc/" << pid << "/fd/";
24  DIR *dir = opendir(ost.str().c_str());
25  dirent *de;
26  struct stat buf;
27  std::vector<int> oldfds;
28  std::vector<int> newfds;
29 
30  while((de = readdir(dir))!=0){
31  char *name = de->d_name;
32  std::string path = ost.str()+name;
33  stat(path.c_str(),&buf);
34  if(S_ISSOCK(buf.st_mode)){
35  int fd = atoi(name);
36  oldfds.push_back(fd);
37  int newfd = dup(fd);
38  if(newfd>0) newfds.push_back(newfd);
39  else std::cout <<"couldn't duplicate old fd " << fd << std::endl;
40  }
41  }
42  closedir(dir);
43  for(unsigned int i = 0; i < oldfds.size(); i++){
44  close(oldfds[i]);
45  int newfd = dup2(newfds[i],oldfds[i]);
46  if(newfd!=oldfds[i]) std::cout <<"couldn't duplicate new fd to old "
47  << oldfds[i] << std::endl;
48  close(newfds[i]);
49  }
50 }
51 
52 }
int i
Definition: DBlmapReader.cc:9
list path
Definition: scaleCards.py:51
tuple cout
Definition: gather_cfg.py:121
dbl *** dir
Definition: mlp_gen.cc:35