diff options
author | Simo Sorce <idra@samba.org> | 2008-11-20 08:16:52 -0500 |
---|---|---|
committer | Simo Sorce <idra@samba.org> | 2008-11-20 08:21:05 -0500 |
commit | edd05ae9d671c9209d630c68a1aff5c5cca8ee32 (patch) | |
tree | df9e496fc18e35f3216cb4380ce252f14676a01e /server/util/become_daemon.c | |
parent | ef946eebf0520607c1c7c72c80b51de63d9d941e (diff) | |
download | sssd-edd05ae9d671c9209d630c68a1aff5c5cca8ee32.tar.gz sssd-edd05ae9d671c9209d630c68a1aff5c5cca8ee32.tar.bz2 sssd-edd05ae9d671c9209d630c68a1aff5c5cca8ee32.zip |
Start conversion from a fork() and live to a fork()/exec() model.
To start the dameon now you need to pass the option -s monitor
Still have some problems communicating with children.
Diffstat (limited to 'server/util/become_daemon.c')
-rw-r--r-- | server/util/become_daemon.c | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/server/util/become_daemon.c b/server/util/become_daemon.c index c753d08c..4a940eaa 100644 --- a/server/util/become_daemon.c +++ b/server/util/become_daemon.c @@ -21,7 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ - +#define _GNU_SOURCE +#include <stdio.h> #include <stdbool.h> #include <unistd.h> #include <sys/types.h> @@ -96,3 +97,65 @@ void become_daemon(bool Fork) attach it to the logfile */ } +int pidfile(const char *path, const char *name) +{ + char pid_str[32]; + pid_t pid; + char *file; + int fd; + int ret; + + asprintf(&file, "%s/%s.pid", path, name); + + fd = open(file, O_RDONLY, 0644); + if (fd != -1) { + + pid_str[sizeof(pid_str) -1] = '\0'; + ret = read(fd, pid_str, sizeof(pid_str) -1); + if (ret > 0) { + /* let's check the pid */ + + pid = (pid_t)atoi(pid_str); + if (pid != 0) { + errno = 0; + ret = kill(pid, 0); + if (ret != 0 && errno != ESRCH) { + close(fd); + free(file); + return EEXIST; + } + } + } + + /* notihng in the file or no process */ + close(fd); + unlink(file); + + } else { + if (errno != ENOENT) { + free(file); + return EIO; + } + } + + fd = open(file, O_CREAT | O_WRONLY | O_EXCL, 0644); + if (fd == -1) { + free(file); + return EIO; + } + free(file); + + memset(pid_str, 0, sizeof(pid_str)); + snprintf(pid_str, sizeof(pid_str) -1, "%u\n", (unsigned int) getpid()); + + ret = write(fd, pid_str, strlen(pid_str)); + if (ret != strlen(pid_str)) { + close(fd); + return EIO; + } + + close(fd); + + return 0; +} + |