mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-28 08:26:16 +02:00
pid file
git-svn-id: svn://elaine/murooma/trunk@329 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
parent
c40bfda64c
commit
a51c000a2d
6 changed files with 120 additions and 53 deletions
|
@ -18,7 +18,7 @@ client: $(OBJ)
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BIN) $(OBJ) *~
|
rm -rf $(BIN) $(OBJ) *~
|
||||||
|
|
||||||
install:
|
install: uninstall
|
||||||
@cp ./snapclient /usr/sbin/snapclient
|
@cp ./snapclient /usr/sbin/snapclient
|
||||||
@echo "Please enter the IP address or host name of the snapserver: [localhost]"; \
|
@echo "Please enter the IP address or host name of the snapserver: [localhost]"; \
|
||||||
read server_ip; \
|
read server_ip; \
|
||||||
|
@ -28,3 +28,13 @@ install:
|
||||||
echo "Server: "$$server_ip; \
|
echo "Server: "$$server_ip; \
|
||||||
cp ../init.d/snapclient /etc/init.d/snapclient; \
|
cp ../init.d/snapclient /etc/init.d/snapclient; \
|
||||||
sed -i s/_SERVER_HOST_/$${server_ip}/g /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; \
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
|
|
||||||
#include "common/utils.h"
|
#include "common/daemon.h"
|
||||||
#include "common/log.h"
|
#include "common/log.h"
|
||||||
#include "common/signalHandler.h"
|
#include "common/signalHandler.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
@ -95,7 +95,7 @@ int main (int argc, char *argv[])
|
||||||
std::clog.rdbuf(new Log("snapclient", LOG_DAEMON));
|
std::clog.rdbuf(new Log("snapclient", LOG_DAEMON));
|
||||||
if (runAsDaemon)
|
if (runAsDaemon)
|
||||||
{
|
{
|
||||||
daemonize();
|
daemonize("/var/run/snapclient.pid");
|
||||||
std::clog << kLogNotice << "daemon started" << std::endl;
|
std::clog << kLogNotice << "daemon started" << std::endl;
|
||||||
}
|
}
|
||||||
logS(kLogNotice) << "daemon started" << std::endl;
|
logS(kLogNotice) << "daemon started" << std::endl;
|
||||||
|
@ -119,6 +119,7 @@ int main (int argc, char *argv[])
|
||||||
usleep(100*1000);
|
usleep(100*1000);
|
||||||
|
|
||||||
controller.stop();
|
controller.stop();
|
||||||
|
daemonShutdown();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
91
common/daemon.h
Normal file
91
common/daemon.h
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
#ifndef DAEMONIZE_H
|
||||||
|
#define DAEMONIZE_H
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <syslog.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
|
@ -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
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,16 @@ server: $(OBJ)
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BIN) $(OBJ) *~
|
rm -rf $(BIN) $(OBJ) *~
|
||||||
|
|
||||||
install:
|
install: uninstall
|
||||||
@cp ./snapserver /usr/sbin/snapserver; \
|
@cp ./snapserver /usr/sbin/snapserver; \
|
||||||
cp ../init.d/snapserver /etc/init.d/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; \
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "common/timeDefs.h"
|
#include "common/timeDefs.h"
|
||||||
#include "common/signalHandler.h"
|
#include "common/signalHandler.h"
|
||||||
#include "common/utils.h"
|
#include "common/daemon.h"
|
||||||
#include "message/sampleFormat.h"
|
#include "message/sampleFormat.h"
|
||||||
#include "message/message.h"
|
#include "message/message.h"
|
||||||
#include "pcmEncoder.h"
|
#include "pcmEncoder.h"
|
||||||
|
@ -56,11 +56,11 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
if (runAsDaemon)
|
if (runAsDaemon)
|
||||||
{
|
{
|
||||||
daemonize();
|
daemonize("/var/run/snapserver.pid");
|
||||||
syslog (LOG_NOTICE, "First daemon started.");
|
syslog (LOG_NOTICE, "First daemon started.");
|
||||||
}
|
}
|
||||||
|
|
||||||
openlog ("firstdaemon", LOG_PID, LOG_DAEMON);
|
openlog("firstdaemon", LOG_PID, LOG_DAEMON);
|
||||||
|
|
||||||
using namespace std; // For atoi.
|
using namespace std; // For atoi.
|
||||||
|
|
||||||
|
@ -159,8 +159,8 @@ int main(int argc, char* argv[])
|
||||||
std::cerr << "Exception: " << e.what() << std::endl;
|
std::cerr << "Exception: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
syslog (LOG_NOTICE, "First daemon terminated.");
|
syslog(LOG_NOTICE, "First daemon terminated.");
|
||||||
cout << "Terminated\n";
|
daemonShutdown();
|
||||||
closelog();
|
closelog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue