diff --git a/client/Makefile b/client/Makefile index 43f25546..662d6761 100644 --- a/client/Makefile +++ b/client/Makefile @@ -18,7 +18,7 @@ client: $(OBJ) clean: rm -rf $(BIN) $(OBJ) *~ -install: +install: uninstall @cp ./snapclient /usr/sbin/snapclient @echo "Please enter the IP address or host name of the snapserver: [localhost]"; \ read server_ip; \ @@ -28,3 +28,13 @@ install: echo "Server: "$$server_ip; \ cp ../init.d/snapclient /etc/init.d/snapclient; \ sed -i s/_SERVER_HOST_/$${server_ip}/g /etc/init.d/snapclient; \ + update-rc.d snapclient defaults; \ + /etc/init.d/snapclient start; \ + +uninstall: + @/etc/init.d/snapclient stop; \ + killall -9 snapclient; \ + rm -f /usr/sbin/snapclient; \ + rm -f /etc/init.d/snapclient; \ + update-rc.d -f snapclient remove; \ + diff --git a/client/snapClient.cpp b/client/snapClient.cpp index ed97574d..3486f293 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -9,7 +9,7 @@ #include #include -#include "common/utils.h" +#include "common/daemon.h" #include "common/log.h" #include "common/signalHandler.h" #include "controller.h" @@ -95,7 +95,7 @@ int main (int argc, char *argv[]) std::clog.rdbuf(new Log("snapclient", LOG_DAEMON)); if (runAsDaemon) { - daemonize(); + daemonize("/var/run/snapclient.pid"); std::clog << kLogNotice << "daemon started" << std::endl; } logS(kLogNotice) << "daemon started" << std::endl; @@ -119,6 +119,7 @@ int main (int argc, char *argv[]) usleep(100*1000); controller.stop(); + daemonShutdown(); return 0; } diff --git a/common/daemon.h b/common/daemon.h new file mode 100644 index 00000000..081b3cca --- /dev/null +++ b/common/daemon.h @@ -0,0 +1,91 @@ +#ifndef DAEMONIZE_H +#define DAEMONIZE_H + +#include +#include +#include +#include + +int pidFilehandle; + +void daemonize(const char *pidfile) +{ + /* Our process ID and Session ID */ + pid_t pid, sid; + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) + exit(EXIT_FAILURE); + + /* If we got a good PID, then + we can exit the parent process. */ + if (pid > 0) + exit(EXIT_SUCCESS); + + /* Change the file mode mask */ + umask(0); + + /* Open any logs here */ + + /* Create a new SID for the child process */ + sid = setsid(); + if (sid < 0) + { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + /* Change the current working directory */ + if ((chdir("/")) < 0) + { + /* Log the failure */ + exit(EXIT_FAILURE); + } + + /* Ensure only one copy */ + pidFilehandle = open(pidfile, O_RDWR|O_CREAT, 0600); + + if (pidFilehandle == -1 ) + { + /* Couldn't open lock file */ + syslog(LOG_INFO, "Could not open PID lock file %s, exiting", pidfile); + exit(EXIT_FAILURE); + } + + /* Try to lock file */ + if (lockf(pidFilehandle,F_TLOCK,0) == -1) + { + /* Couldn't get lock on lock file */ + syslog(LOG_INFO, "Could not lock PID lock file %s, exiting", pidfile); + exit(EXIT_FAILURE); + } + + char str[10]; + /* Get and format PID */ + sprintf(str, "%d\n", getpid()); + + /* write pid to lockfile */ + if (write(pidFilehandle, str, strlen(str)) != (int)strlen(str)) + { + /* Couldn't get lock on lock file */ + syslog(LOG_INFO, "Could write PID to lock file %s, exiting", pidfile); + exit(EXIT_FAILURE); + } + + /* Close out the standard file descriptors */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); +} + + +void daemonShutdown() +{ + close(pidFilehandle); +} + + +#endif + + diff --git a/common/utils.h b/common/utils.h index d93606a8..16ba52bd 100644 --- a/common/utils.h +++ b/common/utils.h @@ -85,50 +85,6 @@ static std::string getMacAddress(int sock) */ -static void daemonize() -{ - /* Our process ID and Session ID */ - pid_t pid, sid; - - /* Fork off the parent process */ - pid = fork(); - if (pid < 0) - exit(EXIT_FAILURE); - - /* If we got a good PID, then - we can exit the parent process. */ - if (pid > 0) - exit(EXIT_SUCCESS); - - /* Change the file mode mask */ - umask(0); - - /* Open any logs here */ - - /* Create a new SID for the child process */ - sid = setsid(); - if (sid < 0) - { - /* Log the failure */ - exit(EXIT_FAILURE); - } - - /* Change the current working directory */ - if ((chdir("/")) < 0) - { - /* Log the failure */ - exit(EXIT_FAILURE); - } - - /* Close out the standard file descriptors */ - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(STDERR_FILENO); -} - - - - #endif diff --git a/server/Makefile b/server/Makefile index 0d99cf52..7b582d56 100644 --- a/server/Makefile +++ b/server/Makefile @@ -18,7 +18,16 @@ server: $(OBJ) clean: rm -rf $(BIN) $(OBJ) *~ -install: +install: uninstall @cp ./snapserver /usr/sbin/snapserver; \ cp ../init.d/snapserver /etc/init.d/snapserver; \ + update-rc.d snapserver defaults; \ + /etc/init.d/snapserver start; \ +uninstall: + @/etc/init.d/snapserver stop; \ + killall -9 snapserver; \ + rm -f /usr/sbin/snapserver; \ + rm -f /etc/init.d/snapserver; \ + update-rc.d -f snapserver remove; \ + diff --git a/server/snapServer.cpp b/server/snapServer.cpp index b60652f3..431d7a39 100644 --- a/server/snapServer.cpp +++ b/server/snapServer.cpp @@ -3,7 +3,7 @@ #include #include "common/timeDefs.h" #include "common/signalHandler.h" -#include "common/utils.h" +#include "common/daemon.h" #include "message/sampleFormat.h" #include "message/message.h" #include "pcmEncoder.h" @@ -56,11 +56,11 @@ int main(int argc, char* argv[]) if (runAsDaemon) { - daemonize(); + daemonize("/var/run/snapserver.pid"); syslog (LOG_NOTICE, "First daemon started."); } - openlog ("firstdaemon", LOG_PID, LOG_DAEMON); + openlog("firstdaemon", LOG_PID, LOG_DAEMON); using namespace std; // For atoi. @@ -159,8 +159,8 @@ int main(int argc, char* argv[]) std::cerr << "Exception: " << e.what() << std::endl; } - syslog (LOG_NOTICE, "First daemon terminated."); - cout << "Terminated\n"; + syslog(LOG_NOTICE, "First daemon terminated."); + daemonShutdown(); closelog(); }