From 42f978a143b3bf2b2f8696ef70ccf712ed147350 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 18 Mar 2008 09:11:45 +0000 Subject: A lot of changes to the daemon code. --- server/src/pracrod.cc | 73 ++++++++++++--------------------------------------- 1 file changed, 17 insertions(+), 56 deletions(-) (limited to 'server/src/pracrod.cc') diff --git a/server/src/pracrod.cc b/server/src/pracrod.cc index cd5bfe7..134b212 100644 --- a/server/src/pracrod.cc +++ b/server/src/pracrod.cc @@ -27,6 +27,8 @@ // For ETC #include +#include "daemon.h" + // For setuid and setgid #include #include @@ -93,6 +95,19 @@ void reload(int fisk) } } +class PracroDaemon : public Daemon { +private: + int daemon_main(); +}; + +int PracroDaemon::daemon_main() +{ + // Activate the server main loop. + server(); + + return 0; +} + int main(int argc, char *argv[]) { int c; @@ -152,9 +167,6 @@ int main(int argc, char *argv[]) } } - //std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator] - //std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator] - char *cfname = NULL; if(!configfile) cfname = ETC"/pracrod.conf"; else cfname = configfile; @@ -170,24 +182,6 @@ int main(int argc, char *argv[]) } } - if(user) { - // Fetch user id - int uid = -1; - struct passwd *p = getpwent(); - while(p) { - if(strcmp(p->pw_name, user) == 0) uid = p->pw_uid; - p = getpwent(); - } - if(uid == -1) { - fprintf(stderr, "Could not find user \"%s\" in /etc/passwd file.\n", user); - return 1; - } - - if(setuid(uid) == -1) { - fprintf(stderr, "Could not switch to user %s: %s\n", user, strerror(errno)); - return 1; - } - } if(!group) { std::string groupstr; @@ -199,41 +193,8 @@ int main(int argc, char *argv[]) } - if(group) { - // Fetch group id - int gid = -1; - struct group *g = getgrent(); - while(g) { - if(strcmp(g->gr_name, group) == 0) gid = g->gr_gid; - g = getgrent(); - } - if(gid == -1) { - fprintf(stderr, "Could not find group \"%s\" in /etc/group file.\n", group); - return 1; - } - - if(setgid(gid) == -1) { - fprintf(stderr, "Could not switch to group %s: %s\n", group, strerror(errno)); - return 1; - } - } - - if(!foreground) { - daemon(0,0); - signal (SIGTERM, SIG_IGN); - signal (SIGINT, SIG_IGN); - } else { - printf("Running in foreground mode.\n"); - } - - // Create callback to reload conf files. - signal (SIGHUP, reload); - - signal(SIGCLD, SIG_IGN); // Ved SIGCHILD til IGNORE maa wait/waitpid ikke kaldes - // (ellers kommer der kernel-brok) - - // Activate the server main loop. - server(); + PracroDaemon daemon; + daemon.run(user, group, !foreground); // Clean up if(configfile) free(configfile); -- cgit v1.2.3