Back to index

natlog  0.91.0
childprocess.cc
Go to the documentation of this file.
00001 #include "natfork.ih"
00002 
00003 void NatFork::childProcess()
00004 {
00005     ShowSeconds::setFormat();
00006 
00007     if (d_options.daemon())
00008         prepareDaemon();
00009 
00010     OFdStream out(d_pipe.writeFd());        // the message to the parent
00011 
00012     try
00013     {
00014         if (d_mode == CONNTRACK)
00015         {
00016             Conntrack conntrack(d_stdMsg);
00017             conntrack.run(out);
00018         }
00019         else 
00020         {
00021             Devices devices(d_stdMsg);
00022             devices.run(out);
00023         }
00024     }
00025     catch (Errno const &err)            // any errors at Conntrack or Devices
00026     {                                   // are thrown as Errno exceptions
00027         if (not d_options.daemon())
00028             throw;                      // rethrow the exception
00029 
00030         d_stdMsg << err.why() << endl;
00031         out << 1 << endl;               // The daemon can't start:
00032                                         // inform via the pipe
00033     }
00034 
00035         // when the child process ends it throws away its own pid file:
00036     ifstream pidFile(d_options.pidFile());
00037     pid_t pid;
00038     if (pidFile >> pid && pid == getpid())
00039     {
00040         pidFile.close();
00041         unlink(d_options.pidFile().c_str());
00042     }
00043 
00044     throw Options::OK;              // ends the program or the child process
00045 }
00046 
00047 
00048 
00049 
00050 
00051 
00052