]> pd.if.org Git - startuptools/commitdiff
initial commit
authorNathan Wagner <nw@hydaspes.if.org>
Wed, 13 Feb 2013 20:43:09 +0000 (20:43 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Wed, 13 Feb 2013 20:43:09 +0000 (20:43 +0000)
Makefile [new file with mode: 0644]
chdir.c [new file with mode: 0644]
chids.c [new file with mode: 0644]
daemon.c [new file with mode: 0644]
exec.c [new file with mode: 0644]
monitor.c [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
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 (file)
index 0000000..32c0e82
--- /dev/null
+++ b/chdir.c
@@ -0,0 +1,19 @@
+/*
+ * daemonize and exec
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..61e7226
--- /dev/null
+++ b/chids.c
@@ -0,0 +1,26 @@
+/*
+ * daemonize and exec
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+
+#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 (file)
index 0000000..cdafbf6
--- /dev/null
+++ b/daemon.c
@@ -0,0 +1,68 @@
+/*
+ * daemonize and exec
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <unistd.h>
+
+#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 (file)
index 0000000..94f319f
--- /dev/null
+++ b/exec.c
@@ -0,0 +1,26 @@
+/*
+ * exec
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <string.h>
+
+#include <unistd.h>
+
+void do_exec(int skip, int ac, char *av[]) {
+       int i;
+
+       for (i=0;i<ac-skip;i++) {
+               av[i] = av[i+skip];
+       }
+       av[i] = 0;
+
+       if (i = execvp(av[0], av) < 0) {
+               fprintf(stderr, "exec returned %d: %s\n", i, strerror(i));
+       }
+}
diff --git a/monitor.c b/monitor.c
new file mode 100644 (file)
index 0000000..fcceeb6
--- /dev/null
+++ b/monitor.c
@@ -0,0 +1,48 @@
+/*
+ * daemonize and exec
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <unistd.h>
+
+#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;
+}