summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKylie McClain <kylie@somas.is>2020-08-28 00:23:30 -0400
committerKylie McClain <kylie@somas.is>2020-09-24 21:01:17 -0400
commit405cf24ccebeb6d6d5c98a9bc506d70d4ee21964 (patch)
treecde87ef4eb349153c7fc14dc5107c60f845a7a71
parent6c96cc2618fcc0ff8d7fbeeaa31e288db1894ad2 (diff)
downloadcatsit-405cf24ccebeb6d6d5c98a9bc506d70d4ee21964.tar.gz
catsit-405cf24ccebeb6d6d5c98a9bc506d70d4ee21964.tar.xz
catsit-405cf24ccebeb6d6d5c98a9bc506d70d4ee21964.zip
Port for Linux systems
-rw-r--r--.gitignore2
-rw-r--r--Makefile6
-rw-r--r--catsitd.86
-rw-r--r--compat_getprogname_linux.c26
-rw-r--r--compat_linux.h17
-rw-r--r--compat_setproctitle_linux.c117
-rw-r--r--compat_sys_signame_linux.sh22
-rw-r--r--compat_sys_time_linux.h80
-rwxr-xr-xconfigure17
-rw-r--r--daemon.c6
-rw-r--r--daemon.h2
-rw-r--r--service.c5
12 files changed, 296 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 3b0a7f4..3f7fe67 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,5 @@ catsit.conf
catsitd
config.mk
tags
+
+compat_sys_signame_linux.c
diff --git a/Makefile b/Makefile
index 0831a80..5bed65b 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,8 @@ ETCDIR ?= ${PREFIX}/etc
CFLAGS += -std=c99 -Wall -Wextra -Wpedantic
CFLAGS += -D'ETCDIR="${ETCDIR}"' -D'RUNDIR="${RUNDIR}"'
+dev: tags all
+
-include config.mk
BINS = catsit catsitd
@@ -15,8 +17,6 @@ MAN5 = catsit.conf.5
OBJS += daemon.o
OBJS += service.o
-dev: tags all
-
all: ${BINS}
catsitd: ${OBJS}
@@ -33,7 +33,7 @@ tags: *.c *.h
ctags -w *.c *.h
clean:
- rm -f ${BINS} ${OBJS} ${RC_SCRIPT} tags
+ rm -f ${BINS} ${OBJS} ${RC_SCRIPT} compat_sys_signame_linux.h tags
install: ${BINS} ${MAN5} ${MAN8}
install -d ${DESTDIR}${PREFIX}/sbin ${DESTDIR}${ETCDIR}/rc.d
diff --git a/catsitd.8 b/catsitd.8
index fa1e67a..9c5721e 100644
--- a/catsitd.8
+++ b/catsitd.8
@@ -24,9 +24,9 @@ The
.Nm
daemon
is a process supervisor for
-.Fx
-and
-.Ox .
+.Fx ,
+.Ox ,
+and Linux.
It is designed to supervise processes
with the following behavior:
.Bl -enum
diff --git a/compat_getprogname_linux.c b/compat_getprogname_linux.c
new file mode 100644
index 0000000..3fe9c2b
--- /dev/null
+++ b/compat_getprogname_linux.c
@@ -0,0 +1,26 @@
+/* ISC licensed, from LibreSSL-portable.
+ * https://github.com/libressl-portable/portable/blob/master/crypto/compat/getprogname_linux.c
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+
+const char *
+getprogname(void)
+{
+#if defined(__ANDROID_API__) && __ANDROID_API__ < 21
+ /*
+ * Android added getprogname with API 21, so we should not end up here
+ * with APIs newer than 21.
+ * https://github.com/aosp-mirror/platform_bionic/blob/1eb6d3/libc/include/stdlib.h#L160
+ *
+ * Since Android is using portions of OpenBSD libc, it should have
+ * a symbol called __progname.
+ * https://github.com/aosp-mirror/platform_bionic/commit/692207
+ */
+ extern const char *__progname;
+ return __progname;
+#else
+ return program_invocation_short_name;
+#endif
+}
diff --git a/compat_linux.h b/compat_linux.h
new file mode 100644
index 0000000..9665e2e
--- /dev/null
+++ b/compat_linux.h
@@ -0,0 +1,17 @@
+#ifdef NEED_COMPAT_LINUX
+#ifndef COMPAT_LINUX
+
+/* Necessary for ARG_MAX. */
+#include <linux/limits.h>
+
+#include "compat_sys_time_linux.h"
+
+const char * getprogname(void);
+void setproctitle(const char* fmt, ...);
+void setproctitle_init(char** main_argv);
+extern char *sys_signame[];
+
+#define COMPAT_LINUX
+
+#endif /* !COMPAT_LINUX */
+#endif /* NEED_COMPAT_LINUX */
diff --git a/compat_setproctitle_linux.c b/compat_setproctitle_linux.c
new file mode 100644
index 0000000..a435e42
--- /dev/null
+++ b/compat_setproctitle_linux.c
@@ -0,0 +1,117 @@
+/* BSD-3 licensed, from Chromium.
+ * https://chromium.googlesource.com/chromium/src/+/75e10302d5eb778d012a2c8b054bef8f1a67193a/base/setproctitle_linux.c
+ */
+
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file implements BSD-style setproctitle() for Linux.
+// It is written such that it can easily be compiled outside Chromium.
+//
+// The Linux kernel sets up two locations in memory to pass arguments and
+// environment variables to processes. First, there are two char* arrays stored
+// one after another: argv and environ. A pointer to argv is passed to main(),
+// while glibc sets the global variable |environ| to point at the latter. Both
+// of these arrays are terminated by a NULL pointer; the environment array is
+// also followed by some empty space to allow additional variables to be added.
+//
+// These arrays contain pointers to a second location in memory, where the
+// strings themselves are stored one after another: first all the arguments,
+// then the environment variables. The kernel will allocate a single page of
+// memory for this purpose, so the end of the page containing argv[0] is the
+// end of the storage potentially available to store the process title.
+//
+// When the kernel reads the command line arguments for a process, it looks at
+// the range of memory within this page that it initially used for the argument
+// list. If the terminating '\0' character is still where it expects, nothing
+// further is done. If it has been overwritten, the kernel will scan up to the
+// size of a page looking for another. (Note, however, that in general not that
+// much space is actually mapped, since argv[0] is rarely page-aligned and only
+// one page is mapped.)
+//
+// Thus to change the process title, we must move any environment variables out
+// of the way to make room for a potentially longer title, and then overwrite
+// the memory pointed to by argv[0] with a single replacement string, making
+// sure its size does not exceed the available space.
+//
+// It is perhaps worth noting that patches to add a system call to Linux for
+// this, like in BSD, have never made it in: this is the "official" way to do
+// this on Linux. Presumably it is not in glibc due to some disagreement over
+// this position within the glibc project, leaving applications caught in the
+// middle. (Also, only a very few applications need or want this anyway.)
+
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+extern char** environ;
+
+static char** g_main_argv = NULL;
+static char* g_orig_argv0 = NULL;
+
+void setproctitle(const char* fmt, ...) {
+ va_list ap;
+ size_t i, avail_size;
+ uintptr_t page_size, page, page_end;
+ // Sanity check before we try and set the process title.
+ // The BSD version allows fmt == NULL to restore the original title.
+ if (!g_main_argv || !environ || !fmt)
+ return;
+ if (!g_orig_argv0) {
+ // Save the original argv[0].
+ g_orig_argv0 = strdup(g_main_argv[0]);
+ if (!g_orig_argv0)
+ return;
+ }
+ page_size = sysconf(_SC_PAGESIZE);
+ // Get the page on which the argument list and environment live.
+ page = (uintptr_t) g_main_argv[0];
+ page -= page % page_size;
+ page_end = page + page_size;
+ // Move the environment out of the way. Note that we are moving the values,
+ // not the environment array itself (which may not be on the page we need
+ // to overwrite anyway).
+ for (i = 0; environ[i]; ++i) {
+ uintptr_t env_i = (uintptr_t) environ[i];
+ // Only move the value if it's actually in the way. This avoids
+ // leaking copies of the values if this function is called again.
+ if (page <= env_i && env_i < page_end) {
+ char* copy = strdup(environ[i]);
+ // Be paranoid. Check for allocation failure and bail out.
+ if (!copy)
+ return;
+ environ[i] = copy;
+ }
+ }
+ // Put the title in argv[0]. We have to zero out the space first since the
+ // kernel doesn't actually look for a null terminator unless we make the
+ // argument list longer than it started.
+ avail_size = page_end - (uintptr_t) g_main_argv[0];
+ memset(g_main_argv[0], 0, avail_size);
+ va_start(ap, fmt);
+ if (fmt[0] == '-') {
+ vsnprintf(g_main_argv[0], avail_size, &fmt[1], ap);
+ } else {
+ size_t size = snprintf(g_main_argv[0], avail_size, "%s ", g_orig_argv0);
+ if (size < avail_size)
+ vsnprintf(g_main_argv[0] + size, avail_size - size, fmt, ap);
+ }
+ va_end(ap);
+ g_main_argv[1] = NULL;
+}
+
+// A version of this built into glibc would not need this function, since
+// it could stash the argv pointer in __libc_start_main(). But we need it.
+void setproctitle_init(char** main_argv) {
+ if (g_main_argv)
+ return;
+
+ uintptr_t page_size = sysconf(_SC_PAGESIZE);
+ // Check that the argv array is in fact on the same page of memory
+ // as the environment array just as an added measure of protection.
+ if (((uintptr_t) environ) / page_size == ((uintptr_t) main_argv) / page_size)
+ g_main_argv = main_argv;
+}
diff --git a/compat_sys_signame_linux.sh b/compat_sys_signame_linux.sh
new file mode 100644
index 0000000..164f607
--- /dev/null
+++ b/compat_sys_signame_linux.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+set -e
+
+cat <<'EOF'
+#ifdef NEED_SYS_SIGNAME
+
+#ifndef SYS_SIGNAME_DEFINED
+char *sys_signame[] = {
+ "",
+EOF
+
+for i in $(kill -l); do
+ echo \"$i\",
+done
+
+cat <<'EOF'
+};
+#define SYS_SIGNAME_DEFINED
+#endif
+
+#endif /* NEED_SYS_SIGNAME */
+EOF
diff --git a/compat_sys_time_linux.h b/compat_sys_time_linux.h
new file mode 100644
index 0000000..202421b
--- /dev/null
+++ b/compat_sys_time_linux.h
@@ -0,0 +1,80 @@
+/* $OpenBSD: time.h,v 1.11 2000/10/10 13:36:48 itojun Exp $ */
+/* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */
+
+/*
+ * Copyright (c) 1982, 1986, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)time.h 8.2 (Berkeley) 7/10/94
+ */
+
+#ifndef _SYS_TIME_H_
+#define _SYS_TIME_H_
+
+#include <sys/types.h>
+
+/* Operations on timespecs. */
+#ifndef timespecclear
+#define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
+#endif
+
+#ifndef timespecisset
+#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
+#endif
+
+#ifndef timespeccmp
+#define timespeccmp(tsp, usp, cmp) \
+ (((tsp)->tv_sec == (usp)->tv_sec) ? \
+ ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
+ ((tsp)->tv_sec cmp (usp)->tv_sec))
+#endif
+
+#ifndef timespecadd
+#define timespecadd(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec >= 1000000000L) { \
+ (vsp)->tv_sec++; \
+ (vsp)->tv_nsec -= 1000000000L; \
+ } \
+ } while (0)
+#endif
+
+#ifndef timespecsub
+#define timespecsub(tsp, usp, vsp) \
+ do { \
+ (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
+ (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
+ if ((vsp)->tv_nsec < 0) { \
+ (vsp)->tv_sec--; \
+ (vsp)->tv_nsec += 1000000000L; \
+ } \
+ } while (0)
+#endif
+
+#endif /* !_SYS_TIME_H_ */
diff --git a/configure b/configure
index b8b5cdb..18e3b02 100755
--- a/configure
+++ b/configure
@@ -11,6 +11,10 @@ ldlibs() {
def() {
cflags "-D'$1=$2'"
}
+obj() {
+ echo "OBJS ${o:-}= $*"
+ o=+
+}
exec >config.mk
@@ -37,3 +41,16 @@ for opt; do
esac
done
+case "${uname}" in
+ (Linux)
+ cflags -Wno-pedantic -D_GNU_SOURCE -DNEED_COMPAT_LINUX -DNEED_SYS_SIGNAME
+ def SIGINFO SIGUSR2
+ obj compat_getprogname_linux.o compat_setproctitle_linux.o compat_sys_signame_linux.o
+ cat <<'EOF'
+compat_sys_signame_linux.c:
+ sh ./compat_sys_signame_linux.sh > compat_sys_signame_linux.c
+
+${OBJS}: compat_sys_signame_linux.c
+EOF
+ ;;
+esac
diff --git a/daemon.c b/daemon.c
index fe58065..3ec244b 100644
--- a/daemon.c
+++ b/daemon.c
@@ -14,6 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "daemon.h"
+
#include <err.h>
#include <errno.h>
#include <fcntl.h>
@@ -29,15 +31,15 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
+#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <syslog.h>
+#include <time.h>
#include <unistd.h>
-#include "daemon.h"
-
#ifndef RUNDIR
#define RUNDIR "/var/run"
#endif
diff --git a/daemon.h b/daemon.h
index e86e43a..eb26f48 100644
--- a/daemon.h
+++ b/daemon.h
@@ -14,6 +14,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "compat_linux.h"
+
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
diff --git a/service.c b/service.c
index 0607f80..fea23ef 100644
--- a/service.c
+++ b/service.c
@@ -14,10 +14,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "daemon.h"
+
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <grp.h>
#include <limits.h>
#include <paths.h>
#include <signal.h>
@@ -32,8 +35,6 @@
#include <time.h>
#include <unistd.h>
-#include "daemon.h"
-
enum {
ExitNotFound = 127,
ExitNoExec = 126,