summaryrefslogtreecommitdiffstats
path: root/server/util
diff options
context:
space:
mode:
authorSimo Sorce <idra@samba.org>2008-11-20 08:16:52 -0500
committerSimo Sorce <idra@samba.org>2008-11-20 08:21:05 -0500
commitedd05ae9d671c9209d630c68a1aff5c5cca8ee32 (patch)
treedf9e496fc18e35f3216cb4380ce252f14676a01e /server/util
parentef946eebf0520607c1c7c72c80b51de63d9d941e (diff)
downloadsssd-edd05ae9d671c9209d630c68a1aff5c5cca8ee32.tar.gz
sssd-edd05ae9d671c9209d630c68a1aff5c5cca8ee32.tar.xz
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')
-rw-r--r--server/util/become_daemon.c65
-rw-r--r--server/util/debug.c1
-rw-r--r--server/util/util.h6
3 files changed, 69 insertions, 3 deletions
diff --git a/server/util/become_daemon.c b/server/util/become_daemon.c
index c753d08c0..4a940eaa1 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;
+}
+
diff --git a/server/util/debug.c b/server/util/debug.c
index 403ec8d2d..6fd6ccc3d 100644
--- a/server/util/debug.c
+++ b/server/util/debug.c
@@ -3,6 +3,7 @@
#include <stdarg.h>
#include <stdlib.h>
+const char *debug_prg_name = "sssd";
int debug_level = 3;
void debug_fn(const char *format, ...)
diff --git a/server/util/util.h b/server/util/util.h
index bb5526e48..897e35419 100644
--- a/server/util/util.h
+++ b/server/util/util.h
@@ -6,16 +6,17 @@
#include "replace.h"
#include "talloc.h"
+extern const char *debug_prg_name;
extern int debug_level;
void debug_fn(const char *format, ...);
#define DEBUG(level, body) do { \
if (level <= debug_level) { \
- debug_fn("%s[%s]: ", __location__, __FUNCTION__); \
+ debug_fn("[%s] [%s] (%d): ", \
+ debug_prg_name, __FUNCTION__, level); \
debug_fn body; \
} \
} while(0);
-#define DEBUGADD(level, body)
#ifndef discard_const
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
@@ -35,6 +36,7 @@ void debug_fn(const char *format, ...);
/* from become_daemon.c */
void become_daemon(bool Fork);
+int pidfile(const char *path, const char *name);
/* from signal.c */
#include <signal.h>