CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/EventFilter/Processor/src/FileDescriptorHandler.cc

Go to the documentation of this file.
00001 #include "FileDescriptorHandler.h"
00002 
00003 #include <iostream>
00004 #include <sstream>
00005 
00006 #include <sys/types.h>
00007 #include <sys/stat.h>
00008 #include <dirent.h>
00009 
00010 #include <stdlib.h>
00011 #include <unistd.h>
00012 
00013 #include <vector>
00014 
00015 namespace evf{
00016 
00017 FileDescriptorHandler::FileDescriptorHandler(){
00018 
00019   //find all socket file descriptors inherited from parent process and close them
00020 
00021   pid_t pid = ::getpid();
00022   std::ostringstream ost;
00023   ost << "/proc/" << pid << "/fd/";
00024   DIR *dir = opendir(ost.str().c_str());
00025   dirent *de; 
00026   struct stat buf;
00027   std::vector<int> oldfds;
00028   std::vector<int> newfds;
00029 
00030   while((de = readdir(dir))!=0){
00031     char *name = de->d_name;    
00032     std::string path = ost.str()+name;
00033     stat(path.c_str(),&buf);
00034     if(S_ISSOCK(buf.st_mode)){
00035       int fd = atoi(name);
00036       oldfds.push_back(fd);
00037       int newfd = dup(fd);
00038       if(newfd>0) newfds.push_back(newfd);
00039       else std::cout <<"couldn't duplicate old fd " << fd << std::endl;
00040     }
00041   }
00042   closedir(dir);
00043   for(unsigned int i = 0; i < oldfds.size(); i++){
00044     close(oldfds[i]);
00045     int newfd = dup2(newfds[i],oldfds[i]);
00046     if(newfd!=oldfds[i]) std::cout <<"couldn't duplicate new fd to old " 
00047                                    << oldfds[i] << std::endl;
00048     close(newfds[i]);
00049   }
00050 }
00051 
00052 }