First commit
This commit is contained in:
commit
ec92cd45ea
|
@ -0,0 +1,3 @@
|
|||
*.swp
|
||||
*.o
|
||||
pidfilewrapper
|
|
@ -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
|
|
@ -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]
|
|
@ -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.
|
|
@ -0,0 +1,40 @@
|
|||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue