+/*
+ * 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;
+}