From ec92cd45ea0f244f00f8acb68fa188d5a32ad6ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Wed, 11 Jul 2018 15:06:55 +0000 Subject: [PATCH] First commit --- .gitignore | 3 +++ Makefile | 31 +++++++++++++++++++++++++++++++ UNLICENSE | 24 ++++++++++++++++++++++++ pidfilewrapper.1 | 27 +++++++++++++++++++++++++++ pidfilewrapper.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 UNLICENSE create mode 100644 pidfilewrapper.1 create mode 100644 pidfilewrapper.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f5acaa --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +*.o +pidfilewrapper diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a926566 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +DESTDIR ?= +PREFIX ?= /usr/local +EXEC_PREFIX ?= $(PREFIX) +BINDIR ?= $(DESTDIR)$(EXEC_PREFIX)/bin +DATAROOTDIR ?= $(PREFIX)/share +MANDIR ?= $(DATAROOTDIR)/man + +CFLAGS += -std=c11 -Os -g -Wall -Wextra -pedantic +CPPFLAGS += +LDFLAGS += + +all: pidfilewrapper + +pidfilewrapper: pidfilewrapper.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $< + +.PHONY: all install uninstall clean distclean + +install: all + mkdir -p $(BINDIR) + install pidfilewrapper $(BINDIR) + mkdir -p $(DESTDIR)$(MANDIR)/man1 + cp pidfilewrapper.1 $(DESTDIR)$(MANDIR)/man1/pidfilewrapper.1 + +uninstall: + rm -f $(BINDIR)/pidfilewrapper $(DESTDIR)$(MANDIR)/man1/pidfilewrapper.1 + +clean: + rm -f pidfilewrapper + +distclean: clean diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..69843e4 --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to [http://unlicense.org] diff --git a/pidfilewrapper.1 b/pidfilewrapper.1 new file mode 100644 index 0000000..4a99798 --- /dev/null +++ b/pidfilewrapper.1 @@ -0,0 +1,27 @@ +.Dd Jul 11, 2018 +.Dt pidfilewrapper 1 +.Os +.Sh NAME +.Nm pidfilewrapper +.Nd create PID files for a simple daemon +.Sh SYNOPSIS +.Nm +.Ar pidfile +.Ar command +.Op Ar arguments +.Sh DESCRIPTION +.Nm +creates a PID file with specified filename and then executes the given command. +It is intended for simple daemons that do not +.Xr fork 2 +and then exit the initial process. +.Pp +.Nm +executes the given command as the same process. +.Sh EXIT STATUS +.Nm +will exit with status 1 if it fails to create a PID file or execute the given +command. If it is succesful, exit status will be that of the given command. +.Sh AUTHORS +.Nm +has been written by nortti. diff --git a/pidfilewrapper.c b/pidfilewrapper.c new file mode 100644 index 0000000..9bbaa41 --- /dev/null +++ b/pidfilewrapper.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + if(argc < 3) { + fprintf(stderr, "Usage: %s pidfile command [arguments]\n", argv[0]); + return 1; + } + + const char *pidfilename = argv[1]; + FILE *pidfile = fopen(pidfilename, "w"); + if(pidfile == NULL) { + perror("fopen (opening pidfile)"); + return 1; + } + + pid_t own_pid = getpid(); + + if(fprintf(pidfile, "%ji\n", (intmax_t) own_pid) < 0) { + perror("fprintf (printing pid)"); + fclose(pidfile); + return 1; + } + + if(fclose(pidfile) != 0) { + perror("fclose (pidfile)"); + return 1; + } + + char **daemon_argv = &argv[2]; + const char *daemon_command = daemon_argv[0]; + + execvp(daemon_command, daemon_argv); + + perror("execvp"); + return 1; +}