summaryrefslogtreecommitdiff
path: root/server/util
diff options
context:
space:
mode:
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 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;
+}
+
diff --git a/server/util/debug.c b/server/util/debug.c
index 403ec8d2..6fd6ccc3 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 bb5526e4..897e3541 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>