title: Creating a C++ daemon and keeping the environment
body: <p>I am trying to create a c++ daemon that runs on a Red Hat 6.3 platform and am having trouble understanding the differences between the libc <code>daemon()</code> call, the <code>daemon</code> shell command, <code>startproc</code>, <code>start-stop-daemon</code> and about half a dozen other methods that google suggests for creating daemons.</p>
  
  <p>I have seen suggestions that two <code>forks</code> are needed, but calling <code>daemon</code> only does one.  Why is the second fork needed?  </p>
  
  <p>If I write the init.d script to call bash <code>daemon</code>, does the c code still need to call <code>daemon</code>?</p>
  
  <p>I implemented my application to call the c <code>daemon()</code> function since it seems the simplest solution, but I am running into the problem of my environment variables seem to get discarded.  How do I prevent this?</p>
  
  <p>I also need to run the daemon as a particular user, not as root.</p>
  
  <p>What is the simplest way to create a C++ daemon that keeps its environment variables, runs as a specific user, and is started on system boot?</p>