--- /dev/null
+PRG=daemon chdir chids
+CFLAGS=-Wall -Wno-parentheses
+
+all: $(PRG)
+
+clean:
+ rm -f *.o $(PRG)
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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));
+ }
+}
--- /dev/null
+/*
+ * 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;
+}