title: Opening File and system function always fail?
body: <p>Thanks in advance.</p>
  <p>I'm writing a daemon program in c++, and it executes on centos7.
  In each loop, it first read in the config file, and then check if storage is mounted to specified path, if not mounts the storage. It runs properly for several loops, and then it starts to fail.</p>
  <p>The code is as follow.</p>
  <pre><code>bool ContentsCopyCopier::run()
  {
      while(1)
      {
          try{
              std::string status = db_.getCopierStatus(hostName_);
              if(status == QUIESCE)
              {
                  if(currentThreadNum_ &gt; 0){
                      SleepOrWaitForThread();
                      continue;
                  }else{
                      db_.updateCopierStatus(hostName_, DISABLE);
                      break;
                  }
              }
          }catch(MySQLException e){
              log_.write(LogType::WARNING, e.detail + &quot;(&quot; + e.query + &quot;)&quot;);
              log_.write(LogType::WARNING, e.msg);
          }
  
          /* Read config file */
          if(!setSystemParameter())
          {
              log_.write(LogType::WARNING, &quot;Failed to set system parameter.&quot;);
          }
  
          try{
              if(!checkStorageMount(param_.mount_dir))
              {
                  if(!mountStorage(param_.mount_dir))
                  {
                      log_.write(LogType::ERROR,&quot;Failed to mount storage to path &quot; + param_.mount_dir + &quot;.&quot;);
                      return false;
                  }
              }
          }catch(...){
              log_.write(LogType::WARNING, &quot;Failed to execute system function.&quot;);
              std::this_thread::sleep_for(std::chrono::seconds(1));
              continue;
          }
          ...
      }
  }
  </code></pre>
  <p>The functions used above are as follow.</p>
  <pre><code>bool ContentsCopyCopier::setSystemParameter()
  {
      std::map&lt;std::string,std::string&gt; configMap;
      try{
          configMap = getConfig(configFile_);
      }catch(...){
          log_.write(LogType::ERROR,&quot;Failed to read configuration file.&quot;);
          return false;
      }
      ...
  }
  
  std::map&lt;std::string, std::string&gt; getConfig(std::string filename){
      std::map&lt;std::string,std::string&gt; configMap;
      std::string line;
  
      std::ifstream configfile(filename);
      if(configfile.is_open())
      {
          while(std::getline(configfile,line))
          {
              std::string key, value;
              line = trim(line);
              std::replace(line.begin(),line.end(),'=',' ');
  
              if(!line.empty() &amp;&amp; line[0]!='[' &amp;&amp; line[0]!=';')
              {
                  if(line.find(&quot; &quot;)==std::string::npos)
                  {
                      configfile.close();
                      throw ConfigException();
                  }
                  std::stringstream ss(line);
                  ss &gt;&gt; key &gt;&gt; value;
  
                  configMap[key] = value;
              }
          }
          configfile.close();
      }else{
          throw ConfigException();
      }
  
      return configMap;
  }
  
  bool checkStorageMount(std::string path)
  {
      std::string cmd;
      cmd += &quot;/usr/bin/mount|/usr/bin/grep &quot; + path + &quot; &gt; /dev/null 2&gt;&amp;1&quot;;
      int res = system_retry(cmd);
      log_.write(LogType::DEBUG,cmd);
      log_.write(LogType::DEBUG,std::to_string(WIFEXITED(res)));
      log_.write(LogType::DEBUG,std::to_string(WEXITSTATUS(res)));
      if(res == -1 || (WIFEXITED(res) &amp;&amp; WEXITSTATUS(res) == 127)){
          throw SystemOperationException();
      }else if(res != 0)
      {
          return false;
      }
      return true;
  }
  
  bool mountStorage(std::string path)
  {
      std::string cmd;
      cmd += &quot;/usr/bin/mount &quot; + path + &quot; &gt; /dev/null 2&gt;&amp;1&quot;;
      //cmd += &quot;/usr/bin/mount -a &gt; /dev/null 2&gt;&amp;1&quot;;
      int res = system_retry(cmd);
      log_.write(LogType::DEBUG,cmd);
      log_.write(LogType::DEBUG,std::to_string(WIFEXITED(res)));
      log_.write(LogType::DEBUG,std::to_string(WEXITSTATUS(res)));
      if(res == -1 || (WIFEXITED(res) &amp;&amp; WEXITSTATUS(res) == 127)){
          throw SystemOperationException();
      }else if(res != 0 &amp;&amp; WEXITSTATUS(res) != 32)
      {
          return false;
      }
      return true;
  }
  
  int system_retry(std::string cmd)
  {
      int retry = retry_times;
      int res = 0;
      while(retry &gt; 0){
          res = system(cmd.c_str());
          if(res == -1 || (WIFEXITED(res) &amp;&amp; WEXITSTATUS(res) == 127)){
              std::this_thread::sleep_for(std::chrono::seconds(1));
              retry--;
              continue;
          }else{
              break;
          }
      }
  
      return res;
  }
  </code></pre>
  <p>When the program starts, it works fine for several loops, and then it starts to fail in every loop. The config file cant not be opened and executing system function returns status 127. When I restart the program, it is OK again.</p>
  <p>The logs are as follow.</p>
  <pre><code>2020-06-26 10:15:05 [DEBUG  ] /usr/bin/mount|/usr/bin/grep /mnt/stock &gt; /dev/nul
  l 2&gt;&amp;1
  2020-06-26 10:15:05 [DEBUG  ] 1
  2020-06-26 10:15:05 [DEBUG  ] 129
  2020-06-26 10:15:05 [DEBUG  ] /usr/bin/mount /mnt/stock &gt; /dev/null 2&gt;&amp;1
  2020-06-26 10:15:05 [DEBUG  ] 1
  2020-06-26 10:15:05 [DEBUG  ] 32
  2020-06-26 10:15:06 [ERROR  ] No disk related to 1JCOM-HD-NHKEX_tid_470067_m-618
  087.
  2020-06-26 10:15:06 [ERROR  ] Failed to read configuration file.
  2020-06-26 10:15:06 [WARNING] Failed to set system parameter.
  2020-06-26 10:15:09 [DEBUG  ] /usr/bin/mount|/usr/bin/grep /mnt/stock &gt; /dev/nul
  l 2&gt;&amp;1
  2020-06-26 10:15:09 [DEBUG  ] 1
  2020-06-26 10:15:09 [DEBUG  ] 127
  2020-06-26 10:15:09 [WARNING] Failed to execute system function.
  2020-06-26 10:15:10 [ERROR  ] Failed to read configuration file.
  2020-06-26 10:15:10 [WARNING] Failed to set system parameter.
  2020-06-26 10:15:13 [DEBUG  ] /usr/bin/mount|/usr/bin/grep /mnt/stock &gt; /dev/null 2&gt;&amp;1
  2020-06-26 10:15:13 [DEBUG  ] 1
  2020-06-26 10:15:13 [DEBUG  ] 127
  2020-06-26 10:15:13 [WARNING] Failed to execute system function.
  </code></pre>