summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2007-07-06 22:42:57 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2007-07-06 22:42:57 (GMT)
commitcaba1e82891eb4b419b1ac9fef6e774104acafe2 (patch)
tree0cafc2965f5571e2a289da89ed1ec75798568d05
downloadlib-caba1e82891eb4b419b1ac9fef6e774104acafe2.zip
lib-caba1e82891eb4b419b1ac9fef6e774104acafe2.tar.gz
lib-caba1e82891eb4b419b1ac9fef6e774104acafe2.tar.bz2
lib-caba1e82891eb4b419b1ac9fef6e774104acafe2.tar.xz
Initial commit: collection of useful functions
-rw-r--r--bsdsignal.c28
-rw-r--r--daemon.c37
-rw-r--r--dup2.c25
-rw-r--r--xmalloc.c20
-rw-r--r--xpread.c29
-rw-r--r--xpwrite.c29
-rw-r--r--xread.c28
-rw-r--r--xstrdup.c20
-rw-r--r--xwrite.c28
9 files changed, 244 insertions, 0 deletions
diff --git a/bsdsignal.c b/bsdsignal.c
new file mode 100644
index 0000000..92de999
--- /dev/null
+++ b/bsdsignal.c
@@ -0,0 +1,28 @@
+/*
+ * bsdsignal.c
+ *
+ * Use sigaction() to simulate BSD signal()
+ */
+
+#include "config.h"
+
+void (*bsd_signal(int signum, void (*handler)(int)))(int)
+{
+ struct sigaction action, oldaction;
+
+ memset(&action, 0, sizeof action);
+ action.sa_handler = handler;
+ sigemptyset(&action.sa_mask);
+ sigaddset(&action.sa_mask, signum);
+ action.sa_flags = SA_RESTART;
+
+ if (sigaction(signum, &action, &oldaction) == -1) {
+#ifdef SIG_ERR
+ return SIG_ERR;
+#else
+ return NULL;
+#endif
+ }
+
+ return oldaction.sa_handler;
+}
diff --git a/daemon.c b/daemon.c
new file mode 100644
index 0000000..c3106b5
--- /dev/null
+++ b/daemon.c
@@ -0,0 +1,37 @@
+/*
+ * daemon.c - "daemonize" a process
+ */
+
+#include "config.h"
+
+int daemon(int nochdir, int noclose)
+{
+ int nullfd;
+ pid_t f;
+
+ if (!nochdir) {
+ if (chdir("/"))
+ return -1;
+ }
+
+ if (!noclose) {
+ if ((nullfd = open("/dev/null", O_RDWR)) < 0 ||
+ dup2(nullfd, 0) < 0 ||
+ dup2(nullfd, 1) < 0 ||
+ dup2(nullfd, 2) < 0)
+ return -1;
+ close(nullfd);
+ }
+
+ f = fork();
+ if (f < 0)
+ return -1;
+ else if (f > 0)
+ _exit(0);
+
+#ifdef HAVE_SETSID
+ return setsid();
+#else
+ return 0;
+#endif
+}
diff --git a/dup2.c b/dup2.c
new file mode 100644
index 0000000..bdf3325
--- /dev/null
+++ b/dup2.c
@@ -0,0 +1,25 @@
+/*
+ * dup2.c
+ *
+ * Ersatz dup2() for really ancient systems
+ */
+
+#include "config.h"
+
+int dup2(int oldfd, int newfd)
+{
+ int rv, nfd;
+
+ close(newfd);
+
+ nfd = rv = dup(oldfd);
+
+ if (rv >= 0 && rv != newfd) {
+ rv = dup2(oldfd, newfd);
+ close(nfd);
+ }
+
+ return rv;
+}
+
+
diff --git a/xmalloc.c b/xmalloc.c
new file mode 100644
index 0000000..f234a46
--- /dev/null
+++ b/xmalloc.c
@@ -0,0 +1,20 @@
+/*
+ * xmalloc.c
+ *
+ * Simple error-checking version of malloc()
+ *
+ */
+
+#include "config.h"
+
+void *xmalloc(size_t size)
+{
+ void *p = malloc(size);
+
+ if ( !p ) {
+ fprintf(stderr, "Out of memory!\n");
+ exit(128);
+ }
+
+ return p;
+}
diff --git a/xpread.c b/xpread.c
new file mode 100644
index 0000000..8cea02e
--- /dev/null
+++ b/xpread.c
@@ -0,0 +1,29 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+ssize_t xpread(int fd, void *buf, size_t count, off_t offset)
+{
+ char *p = buf;
+ ssize_t out = 0;
+ ssize_t rv;
+
+ while (count) {
+ rv = pread(fd, p, count, offset);
+ if (rv == -1) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return out ? out : -1; /* Error */
+ } else if (rv == 0) {
+ return out; /* EOF */
+ }
+
+ p += rv;
+ out += rv;
+ count -= rv;
+ offset += rv;
+ }
+
+ return out;
+}
+
diff --git a/xpwrite.c b/xpwrite.c
new file mode 100644
index 0000000..46012b3
--- /dev/null
+++ b/xpwrite.c
@@ -0,0 +1,29 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
+{
+ const char *p = buf;
+ ssize_t out = 0;
+ ssize_t rv;
+
+ while (count) {
+ rv = pwrite(fd, p, count, offset);
+ if (rv == -1) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return out ? out : -1; /* Error */
+ } else if (rv == 0) {
+ return out; /* EOF */
+ }
+
+ p += rv;
+ out += rv;
+ count -= rv;
+ offset += rv;
+ }
+
+ return out;
+}
+
diff --git a/xread.c b/xread.c
new file mode 100644
index 0000000..bd163a3
--- /dev/null
+++ b/xread.c
@@ -0,0 +1,28 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+ssize_t xread(int fd, void *buf, size_t count)
+{
+ char *p = buf;
+ ssize_t out = 0;
+ ssize_t rv;
+
+ while (count) {
+ rv = read(fd, p, count);
+ if (rv == -1) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return out ? out : -1; /* Error */
+ } else if (rv == 0) {
+ return out; /* EOF */
+ }
+
+ p += rv;
+ out += rv;
+ count -= rv;
+ }
+
+ return out;
+}
+
diff --git a/xstrdup.c b/xstrdup.c
new file mode 100644
index 0000000..036b3b2
--- /dev/null
+++ b/xstrdup.c
@@ -0,0 +1,20 @@
+/*
+ * xstrdup.c
+ *
+ * Simple error-checking version of strdup()
+ *
+ */
+
+#include "config.h"
+
+char *xstrdup(const char *s)
+{
+ char *p = strdup(s);
+
+ if ( !p ) {
+ fprintf(stderr, "Out of memory!\n");
+ exit(128);
+ }
+
+ return p;
+}
diff --git a/xwrite.c b/xwrite.c
new file mode 100644
index 0000000..80a754a
--- /dev/null
+++ b/xwrite.c
@@ -0,0 +1,28 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+ssize_t xwrite(int fd, const void *buf, size_t count)
+{
+ const char *p = buf;
+ ssize_t out = 0;
+ ssize_t rv;
+
+ while (count) {
+ rv = write(fd, p, count);
+ if (rv == -1) {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ return out ? out : -1; /* Error */
+ } else if (rv == 0) {
+ return out; /* EOF */
+ }
+
+ p += rv;
+ out += rv;
+ count -= rv;
+ }
+
+ return out;
+}
+