int main(int argc, char *argv[]) { char const * dir; int root_fd; int this_fd; char * umount_cmd[] = { UMOUNT_PROG, "-l", "-n", ".", 0 }; if (argc<2) { WRITE_MSG(2, "Try '"); WRITE_STR(2, argv[0]); WRITE_MSG(2, " --help' for more information.\n"); return EXIT_FAILURE; } if (strcmp(argv[1], "--help")==0) showHelp(1, argv[0], 0); if (strcmp(argv[1], "--version")==0) showVersion(); dir = argv[1]; if (strcmp(dir, "--")==0 && argc>=3) dir = argv[2]; root_fd = Eopen("/", O_RDONLY, 0); Echroot("."); Echdir(dir); this_fd = Eopen(".", O_RDONLY, 0); Efchdir(root_fd); Echroot("."); Efchdir(this_fd); Eclose(root_fd); Eclose(this_fd); Eexecv(umount_cmd[0], umount_cmd); }
inline static pid_t initializeDaemon(/*@in@*/struct ConfigInfo const *cfg) { assert(cfg!=0); if (cfg->do_fork) Esetsid(); Eclose(1); if (cfg->chroot_path[0]!='\0') { Echdir (cfg->chroot_path); Echroot(cfg->chroot_path); } Esetgroups(1, &cfg->gid); Esetgid(cfg->gid); Esetuid(cfg->uid); limitResources(&cfg->ulimits); if (cfg->do_fork) return 0; else return getpid(); }
static void initMtab(struct Configuration const *cfg) { ENSC_PI_DECLARE(mtab_subpath, "apps/init/mtab"); PathInfo mtab_path = cfg->cfgdir; char mtab_buf[ENSC_PI_APPSZ(mtab_path, mtab_subpath)]; PathInfo_append(&mtab_path, &mtab_subpath, mtab_buf); char const * mtab = findMtab(mtab_path.d); pid_t pid; int p[2]; Epipe(p); pid = Efork(); if (pid==0) { Undo_detach(); Eclose(p[1]); Echdir(cfg->vdir); Echroot("."); int fd = Eopen("/etc/mtab", O_WRONLY|O_CREAT, 0644); for (;;) { char buf[4096]; ssize_t len = TEMP_FAILURE_RETRY(read(p[0], buf, sizeof buf)); if (len==0) break; if (len==-1) { perror("vserver-start: initMtab/read():"); _exit(1); } Ewrite(fd, buf, len); } Eclose(fd); Eclose(p[0]); _exit(0); } else { Eclose(p[0]); if (mtab!=0) { int fd = Eopen(mtab, O_RDONLY, 0644); for (;;) { char buf[4096]; ssize_t len = TEMP_FAILURE_RETRY(read(fd, buf, sizeof buf)); if (len==0) break; if (len==-1) { perror("vserver-start: initMtab/read():"); _exit(1); } Ewrite(p[1], buf, len); } Eclose(fd); } Eclose(p[1]); int status; TEMP_FAILURE_RETRY(wait4(pid, &status, 0,0)); if (!WIFEXITED(status) || WEXITSTATUS(status)!=0) { exit(1); } } }