summaryrefslogtreecommitdiff
path: root/server/src/daemon.cc
diff options
context:
space:
mode:
authordeva <deva>2010-07-05 09:49:42 +0000
committerdeva <deva>2010-07-05 09:49:42 +0000
commitdc6abe86d2730b16cb155a02b3ec9d79fcaa72f7 (patch)
tree0fa222007bd1827da42e01dedbafaa005faedf31 /server/src/daemon.cc
parent04168eca33079dd27c60c894a54d76d4bc627c55 (diff)
Make server write its pid file. Daemon class ported from pentominos.
Diffstat (limited to 'server/src/daemon.cc')
-rw-r--r--server/src/daemon.cc67
1 files changed, 31 insertions, 36 deletions
diff --git a/server/src/daemon.cc b/server/src/daemon.cc
index 6aed66e..9de3807 100644
--- a/server/src/daemon.cc
+++ b/server/src/daemon.cc
@@ -3,7 +3,7 @@
* daemon.cc
*
* Thu Jun 9 10:27:59 CEST 2005
- * Copyright 2005 Bent Bisballe
+ * Copyright 2005 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -41,13 +41,13 @@
// For strcmp
#include <string.h>
-/* Resolve a group's name or id into a numeric gid */
+// Resolve a group's name or id into a numeric gid
static gid_t get_gid(const char *grp)
{
char *eptr;
gid_t xid = strtoul(grp, &eptr, 10);
struct group *gr;
- errno = 0; /* Per getgrnam(3) and getgrgid(3) manual page */
+ errno = 0; // Per getgrnam(3) and getgrgid(3) manual page
if(!*eptr)
gr = getgrgid(xid);
else
@@ -55,13 +55,13 @@ static gid_t get_gid(const char *grp)
return !gr ? 0 : gr->gr_gid;
}
-/* Resolve a user's name or id into a numeric uid with associated gid */
+// Resolve a user's name or id into a numeric uid with associated gid
static uid_t get_uid(const char *usr, gid_t *gid)
{
char *eptr;
uid_t xid = strtoul(usr, &eptr, 10);
struct passwd *pw;
- errno = 0; /* Per getpwnam(3) and getpwuid(3) manual page */
+ errno = 0; // Per getpwnam(3) and getpwuid(3) manual page
if(!*eptr)
pw = getpwuid(xid);
else
@@ -79,11 +79,9 @@ Daemon::Daemon()
Daemon::~Daemon()
{}
-int Daemon::run(const char *user, const char* group, bool detach)
+int Daemon::run(const char *user, const char* group, bool detach,
+ std::string pidfile)
{
- // int f;
- int fd;
-
// Fetch user and group IDs
gid_t gid = 0;
uid_t uid = 0;
@@ -103,30 +101,32 @@ int Daemon::run(const char *user, const char* group, bool detach)
}
}
- if(chdir("/")) {
- fprintf(stderr, "Could not chdir to / : %s\n", strerror(errno));
- }
umask(0);
if(detach) {
- switch(fork()) {
- case -1: // Fork error
- perror("Fork in daemon.cc");
- return 1;
-
- case 0: // Forked child
- break; // Break out of switch
-
- default: // Parent
- // exit(0);
- return 0;
+ if( daemon(0, 0) == -1) {
+ perror("");
+ return -1;
+ }
+ }
+
+ if(pidfile != "" ) {
+ pid_t pid = getpid();
+ FILE *fp = fopen(pidfile.c_str(), "w");
+ if(errno) {
+ fprintf(stderr, "Could not write pid file \"%s\"", pidfile.c_str());
+ perror("");
+ } else {
+ fprintf(fp, "%lu", (unsigned long int)pid);
+ fclose(fp);
}
}
if(gid) {
// Switch to given group
if(setgid(gid) != 0) {
- fprintf(stderr, "Failed to change to group \"%s\" (gid: %d), quitting.\n", group, gid);
+ fprintf(stderr, "Failed to change to group \"%s\" (gid: %d).\n",
+ group, gid);
perror("");
fprintf(stderr, "Runnning daemon as current group\n");
}
@@ -135,26 +135,21 @@ int Daemon::run(const char *user, const char* group, bool detach)
if(uid) {
// Switch to given user
if(setuid(uid) != 0) {
- fprintf(stderr, "Failed to change to user \"%s\" (uid: %d), quitting.\n", user, uid);
+ fprintf(stderr, "Failed to change to user \"%s\" (uid: %d).\n",
+ user, uid);
perror("");
fprintf(stderr, "Runnning daemon as current user\n");
}
}
-
- // Redirect stdin, stdout and stderr to /dev/null
- fd = open("/dev/null", O_NOCTTY | O_RDWR, 0666);
-
- dup2(0, fd);
- dup2(1, fd);
- dup2(2, fd);
-
- close(fd);
setsid();
signal (SIGTERM, SIG_IGN);
- // signal (SIGHUP, SIG_IGN);
- if(detach) signal (SIGINT, SIG_IGN); // Don't disable Ctrl+c when running in foreground.
+
+ //signal (SIGHUP, SIG_IGN);
+
+ // Don't disable Ctrl+c when running in foreground.
+ //if(detach) signal (SIGINT, SIG_IGN);
return daemon_main();
}