Add support for creating pidfile through '-p' option

This commit is contained in:
Daniel Adolfsson 2011-09-18 15:22:30 +02:00
parent 1a5c251046
commit d03e890269

View File

@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <iostream> #include <iostream>
#include <fstream>
#include <string> #include <string>
#include <getopt.h> #include <getopt.h>
@ -51,6 +52,7 @@ int daemonize()
int main(int argc, char *argv[], char *env[]) int main(int argc, char *argv[], char *env[])
{ {
std::string config_path("/etc/ndppd.conf"); std::string config_path("/etc/ndppd.conf");
std::string pidfile;
bool daemon = false; bool daemon = false;
while(1) while(1)
@ -64,7 +66,7 @@ int main(int argc, char *argv[], char *env[])
{ 0, 0, 0, 0} { 0, 0, 0, 0}
}; };
c = getopt_long(argc, argv, "c:d", long_options, &opt); c = getopt_long(argc, argv, "c:dp:", long_options, &opt);
if(c == -1) if(c == -1)
break; break;
@ -72,18 +74,16 @@ int main(int argc, char *argv[], char *env[])
switch(c) switch(c)
{ {
case 'c': case 'c':
if(!optarg)
{
ERR("Invalid arguments");
return -1;
}
config_path = optarg; config_path = optarg;
break; break;
case 'd': case 'd':
daemon = true; daemon = true;
break; break;
case 'p':
pidfile = optarg;
break;
} }
} }
@ -98,6 +98,14 @@ int main(int argc, char *argv[], char *env[])
} }
} }
if(!pidfile.empty())
{
std::ofstream pf;
pf.open(pidfile.c_str(), std::ios::out | std::ios::trunc);
pf << getpid() << std::endl;
pf.close();
}
NFO("ndppd (NDP Proxy Daemon) version " NDPPD_VERSION); NFO("ndppd (NDP Proxy Daemon) version " NDPPD_VERSION);
NFO("Using configuration file '%s'", config_path.c_str()); NFO("Using configuration file '%s'", config_path.c_str());