summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/daemon.cc67
-rw-r--r--server/src/daemon.h8
-rw-r--r--server/src/pracrod.cc10
3 files changed, 45 insertions, 40 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();
}
diff --git a/server/src/daemon.h b/server/src/daemon.h
index 89c3f1c..ef6eecc 100644
--- a/server/src/daemon.h
+++ b/server/src/daemon.h
@@ -3,7 +3,7 @@
* daemon.h
*
* Thu Jun 9 10:27:59 CEST 2005
- * Copyright 2005 Bent Bisballe
+ * Copyright 2005 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -28,6 +28,8 @@
#include <sys/types.h>
+#include <string>
+
class Daemon {
public:
Daemon();
@@ -36,10 +38,12 @@ public:
/**
* Use NOBODY_GROUP and NOBODY_USER if no privileges are needed to run.
*/
- int run(const char* user, const char* group, bool detach = true);
+ int run(const char* user, const char* group, bool detach = true,
+ std::string pidfile = "");
private:
virtual int daemon_main() = 0;
+
};
#endif/*__DAEMON_H__*/
diff --git a/server/src/pracrod.cc b/server/src/pracrod.cc
index 64fb538..1488909 100644
--- a/server/src/pracrod.cc
+++ b/server/src/pracrod.cc
@@ -184,6 +184,7 @@ int main(int argc, char *argv[])
char *xml_basedir = NULL;
char *debugstr = NULL;
std::string database;
+ std::string pidfile;
int option_index = 0;
while(1) {
@@ -199,11 +200,12 @@ int main(int argc, char *argv[])
{"debug", required_argument, 0, 'D'},
{"database", required_argument, 0, 'd'},
{"ssl", required_argument, 0, 's'},
+ {"pidfile", required_argument, 0, 'P'},
{"logfile", required_argument, 0, 'L'},
{0, 0, 0, 0}
};
- c = getopt_long (argc, argv, "D:hvfc:u:g:x:d:s:L:",
+ c = getopt_long (argc, argv, "D:hvfc:u:g:x:d:s:L:P:",
long_options, &option_index);
if (c == -1)
@@ -234,6 +236,10 @@ int main(int argc, char *argv[])
xml_basedir = strdup(optarg);
break;
+ case 'P':
+ pidfile = optarg;
+ break;
+
case 'L':
logfile = optarg;
break;
@@ -313,7 +319,7 @@ int main(int argc, char *argv[])
signal(SIGINT, ctrl_c);
PracroDaemon daemon;
- daemon.run(user, group, !foreground);
+ daemon.run(user, group, !foreground, pidfile);
// Clean up
if(configfile) free(configfile);