summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Larson <slioch@eng-140.vyatta.com>2008-01-30 17:19:15 -0800
committerMichael Larson <slioch@eng-140.vyatta.com>2008-01-30 17:19:15 -0800
commit07e2dbf1d5a98860333aefa989370305467c2254 (patch)
tree99efbe8822ec249b1c8ded9054f4821ec2ca6f37 /src
parentad4a7086e81292e0aa76b98739356a02ff9873ed (diff)
downloadvyatta-wanloadbalance-07e2dbf1d5a98860333aefa989370305467c2254.tar.gz
vyatta-wanloadbalance-07e2dbf1d5a98860333aefa989370305467c2254.zip
numerous changes: added help, added init script, modified source to support daemon and pid creation, fixed conf file creation when
using cli. postinst. etc.
Diffstat (limited to 'src')
-rw-r--r--src/main.cc60
1 files changed, 57 insertions, 3 deletions
diff --git a/src/main.cc b/src/main.cc
index e8d93e0..098b664 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -25,6 +25,8 @@
* **** End License ****
*
*/
+#include <sys/types.h>
+#include <sys/stat.h>
#include <signal.h>
#include <syslog.h>
#include <stdio.h>
@@ -32,13 +34,16 @@
#include "loadbalance.hh"
LoadBalance *g_lb = NULL;
+pid_t pid_output (const char *path);
static void usage()
{
- cout << "lb -fh" << endl;
+ cout << "lb -ftidh" << endl;
cout << "-f [file] configuration file" << endl;
cout << "-t load configuration file only and exit" << endl;
+ cout << "-i specify location of pid directory" << endl;
+ cout << "-d run as daemon" << endl;
cout << "-h help" << endl;
}
@@ -65,11 +70,12 @@ static void sig_user(int signo)
int main(int argc, char* argv[])
{
int ch;
- bool config_debug_mode = false;
+ bool config_debug_mode = false, daemon = false;
+ string pid_path;
string c_file;
//grab inputs
- while ((ch = getopt(argc, argv, "f:ht")) != -1) {
+ while ((ch = getopt(argc, argv, "f:hti:d")) != -1) {
switch (ch) {
case 'f':
c_file = optarg;
@@ -80,18 +86,35 @@ int main(int argc, char* argv[])
case 't':
config_debug_mode = true;
break;
+ case 'd':
+ daemon = true;
+ break;
+ case 'i':
+ pid_path = optarg;
+ break;
default:
usage();
exit(0);
}
}
+
//parse conf file
if (c_file.empty()) {
cout << "Configuration file is empty" << endl;
exit(0);
}
+ if (daemon) {
+ if (fork() != 0) {
+ exit(0);
+ }
+ }
+
+ if (pid_path.empty() == false) {
+ pid_output(pid_path.c_str());
+ }
+
g_lb = new LoadBalance();
bool success = g_lb->set_conf(c_file);
@@ -136,3 +159,34 @@ int main(int argc, char* argv[])
} while (g_lb->start_cycle());
exit(0);
}
+
+/**
+ *
+ *below borrowed from quagga library.
+ **/
+#define PIDFILE_MASK 0644
+pid_t
+pid_output (const char *path)
+{
+ FILE *fp;
+ pid_t pid;
+ mode_t oldumask;
+
+ pid = getpid();
+
+ oldumask = umask(0777 & ~PIDFILE_MASK);
+ fp = fopen (path, "w");
+ if (fp != NULL)
+ {
+ fprintf (fp, "%d\n", (int) pid);
+ fclose (fp);
+ umask(oldumask);
+ return pid;
+ }
+ /* XXX Why do we continue instead of exiting? This seems incompatible
+ with the behavior of the fcntl version below. */
+ syslog(LOG_ERR,"Can't fopen pid lock file %s, continuing",
+ path);
+ umask(oldumask);
+ return -1;
+}