/*************************************************************************** * Copyright (C) 03/2005 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include "hlswmaster.h" /** * daemonize() * macht den Prozess zu einen Daemon * und fuehrt ihn unterem user aus * * @param char *pw_name * * TODO: hier schon logging benutzen? stderr? wann exit/return? */ void daemonize(char *pw_name) { int fail = 0; pid_t pid; struct passwd *pwl; if ((pid = fork()) < 0) { log_print("daemonize(): fork()"); exit(-1); } else if (pid != 0) { /* Elternprozess beenden */ exit(0); } else { /* Kindprozess weiterlaufen lassen */ if (setsid() == -1) { log_print("daemonize: setsid()"); exit(-1); } umask(0) ; if (pw_name != NULL) { if((pwl = getpwnam(pw_name)) == NULL) { log_print("mkdaemon(): getpwnam(\"%s\")", pw_name); exit(-1); } if (setgid(pwl->pw_gid) != 0) { log_print("mkdaemon(): setgid()"); fail = 1; } if (setuid(pwl->pw_uid) != 0) { log_print("mkdaemon(): setuid()"); fail = 1; } if (fail == 0) { log_print("user changed to: %s, pid: %d", pw_name, getpid()); } } } }