int cron_parent_main(int argc, char *argv[]) { int pid, p2; extern int errno; #ifdef ERRFD dup2(2, ERRFD); errf = fdopen(ERRFD, "w"); #endif if (argc != 4) exit(1); /* Enough args? */ av = malloc(sizeof(char *) * 101); /* Allocate an argv array */ if (av == NULL) { print("Malloc failed: errno = %d", errno); _exit(1); } setsid(); /* Create our own little group */ changeuser(atoi(argv[2]), argv[3]); /* Give up privs */ setenviron(); /* Sanitise environ and extract useful information */ parseargs(argv[1]); /* Process the jobs command line */ setupfds(); /* Set up file descriptors */ pid = vfork(); /* Create the child process */ if (pid == 0) { close(PLISTENFD); /* Close parent only FDs */ close(PSENDFD); execve(path, av, environ); /* Run the job */ print("Exec failed: errno = %d", errno); _exit(1); } if (pid < 0) exit(1); /* Parent processing */ close(0); close(1); close(2); p2 = handlestdout(); /* Pass stdout to mail */ sendstdin(); /* Send stdin to process if required */ waitpid(pid, NULL, 0); /* Wait for children to exit */ if (p2 != 0) waitpid(p2, NULL, 0); return 0; }
extern int putenv(const char* s) { return setenviron(s) ? 0 : -1; }
extern void unsetenv(const char *name) { if (!strchr(name, '=')) setenviron(name); }