summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/util.c b/util.c
index 21c1eb9..052b382 100644
--- a/util.c
+++ b/util.c
@@ -8,6 +8,7 @@
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
+#include <sys/wait.h>
@@ -296,6 +297,7 @@ int open_tcp_client (const char *host, unsigned port)
if (fd < 0) return -1;
+
sin.sin_port = htons (port);
if (connect (fd, (struct sockaddr *)&sin, sizeof (sin))) {
@@ -339,3 +341,67 @@ int open_tcp_server (unsigned port)
+int daemonish (int nochdir, int noclose)
+{
+ pid_t pid;
+ int status;
+
+ if (!nochdir && chdir ("/"))
+ return -1;
+
+ if (!noclose) {
+ int fd, failed = 0;
+
+ if ((fd = open ("/dev/null", O_RDWR)) < 0) return -1;
+
+ if (dup2 (fd, 0) < 0 || dup2 (fd, 1) < 0 || dup2 (fd, 2) < 0)
+ failed++;
+
+ if (fd > 2) close (fd);
+
+ if (failed) return -1;
+ }
+
+ switch ((pid = fork())) {
+ case 0:
+ break;
+
+ case -1:
+ return -1;
+
+ default:
+ waitpid (pid, &status, 0);
+ return 1;;
+ }
+
+ if (setsid() < 0) return -1;
+
+ switch (fork()) {
+ case 0:
+ break;
+
+ case -1:
+ return -1;
+
+ default:
+ _exit (0);
+ }
+
+ return 0;
+}
+
+
+int local_isalnum (char c)
+{
+ if ((c >= '0') && (c <= '9')) return 1;
+
+ if ((c >= 'A') && (c <= 'Z')) return 1;
+
+ if ((c >= 'a') && (c <= 'z')) return 1;
+
+ return 0;
+}
+
+
+
+