From 11c12539d9ee64b816ad2e734b499d7ce1e83ac9 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Wed, 13 Feb 2013 20:43:09 +0000 Subject: [PATCH] initial commit --- Makefile | 7 ++++++ chdir.c | 19 ++++++++++++++++ chids.c | 26 +++++++++++++++++++++ daemon.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ exec.c | 26 +++++++++++++++++++++ monitor.c | 48 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 194 insertions(+) create mode 100644 Makefile create mode 100644 chdir.c create mode 100644 chids.c create mode 100644 daemon.c create mode 100644 exec.c create mode 100644 monitor.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f57d26b --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +PRG=daemon chdir chids +CFLAGS=-Wall -Wno-parentheses + +all: $(PRG) + +clean: + rm -f *.o $(PRG) diff --git a/chdir.c b/chdir.c new file mode 100644 index 0000000..32c0e82 --- /dev/null +++ b/chdir.c @@ -0,0 +1,19 @@ +/* + * daemonize and exec + */ + +#include +#include + +#include "exec.c" + +int main(int ac, char *av[]) { + if (chdir(av[1]) < 0) { + exit(EXIT_FAILURE); + } + + do_exec(2, ac, av); + + /* shouldn't get here... */ + return EXIT_FAILURE; +} diff --git a/chids.c b/chids.c new file mode 100644 index 0000000..61e7226 --- /dev/null +++ b/chids.c @@ -0,0 +1,26 @@ +/* + * daemonize and exec + */ + +#include +#include +#include +#include + +#include "exec.c" + +int main(int ac, char *av[]) { + struct passwd *pw; + + pw = getpwnam(av[1]); + if (!pw) { + exit(EXIT_FAILURE); + } + + seteuid(pw->pw_uid); + + do_exec(2, ac, av); + + /* shouldn't get here... */ + return EXIT_FAILURE; +} diff --git a/daemon.c b/daemon.c new file mode 100644 index 0000000..cdafbf6 --- /dev/null +++ b/daemon.c @@ -0,0 +1,68 @@ +/* + * daemonize and exec + */ + +#include +#include + +#include +#include +#include + +#include + +#include "exec.c" + +int main(int ac, char *av[]) { + pid_t pid, sid; + + pid = fork(); + if (pid < 0) { + fprintf(stderr, "fork fail!\n"); + exit(EXIT_FAILURE); + } + + if (pid > 0) { + exit(EXIT_SUCCESS); + } + + umask(0); + + sid = setsid(); + if (sid < 0) { + exit(EXIT_SUCCESS); + } + + pid = fork(); + if (pid < 0) { + fprintf(stderr, "fork fail!\n"); + exit(EXIT_FAILURE); + } + if (pid > 0) { + /* fprintf(stderr, "pid = %d\n", pid); */ + exit(EXIT_SUCCESS); + } + + /* reset the file descriptors */ + if (isatty(0)) { + close(0); + open("/dev/null", O_RDONLY); + } + if (isatty(1)) { + close(0); + open("/dev/null", O_WRONLY); + } + if (isatty(2)) { + close(0); + open("/dev/null", O_WRONLY); + } + + if (chdir("/") < 0) { + exit(EXIT_FAILURE); + } + + do_exec(1, ac, av); + + /* shouldn't get here... */ + return EXIT_FAILURE; +} diff --git a/exec.c b/exec.c new file mode 100644 index 0000000..94f319f --- /dev/null +++ b/exec.c @@ -0,0 +1,26 @@ +/* + * exec + */ + +#include +#include + +#include +#include + +#include + +#include + +void do_exec(int skip, int ac, char *av[]) { + int i; + + for (i=0;i +#include + +#include +#include + +#include + +#include "exec.c" + +int main(int ac, char *av[]) { + pid_t child = 0; + + /* create the child */ + child = fork(); + if (child < 0) { + fprintf(stderr, "fork fail!\n"); + exit(EXIT_FAILURE); + } + + /* parent, actually does the monitoring */ + if (child > 0) { + exit(EXIT_SUCCESS); + } + + pid = fork(); + if (pid < 0) { + fprintf(stderr, "fork fail!\n"); + exit(EXIT_FAILURE); + } + if (pid > 0) { + /* fprintf(stderr, "pid = %d\n", pid); */ + exit(EXIT_SUCCESS); + } + + if (chdir("/") < 0) { + exit(EXIT_FAILURE); + } + + do_exec(1, ac, av); + + /* shouldn't get here... */ + return EXIT_FAILURE; +} -- 2.40.0