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); }
static bool checkFile(char const *fname) { int fd = Eopen(fname, O_RDONLY, 0); off_t l = Elseek(fd, 0, SEEK_END); char const * data = 0; bool res = true; if (l>100*1024*1024) { WRITE_MSG(2, "WARNING: '"); WRITE_STR(2, fname); WRITE_STR(2, "' is too large for a vserver configuration file\n"); res = false; } else if (l>0) { data = mmap(0, l, PROT_READ, MAP_PRIVATE, fd, 0); if (data==MAP_FAILED) { perror("mmap()"); exit(wrapper_exit_code); } if (data[l-1]!='\n') { WRITE_MSG(2, "WARNING: '"); WRITE_STR(2, fname); WRITE_MSG(2, "' does not end on newline\n"); res = false; } munmap(const_cast(char *)(data), l); }
/*@-superuser@*/ int initializeSystem(int argc, char *argv[], struct InterfaceInfoList * ifs, struct ServerInfoList * servers, struct FdInfoList * fds) { struct ConfigInfo cfg; pid_t pid, pidfile_pid; int pidfile_fd; cfg.conffile_name = CFG_FILENAME; cfg.do_fork = true; parseCommandline(argc, argv, &cfg); /*@-boundswrite@*/ getConfig(cfg.conffile_name, &cfg); initFDs(fds, &cfg); /*@=boundswrite@*/ pidfile_fd = Eopen(cfg.pidfile_name, O_WRONLY|O_CREAT, 0444); openMsgfile(cfg.logfile_name); *ifs = cfg.interfaces; *servers = cfg.servers; Eclose(0); if (cfg.do_fork) pid = fork(); else pid = 0; pidfile_pid = 0; switch (pid) { case 0 : pidfile_pid = initializeDaemon(&cfg); break; case -1 : perror("fork()"); break; default : pidfile_pid = pid; break; } if (pidfile_pid!=0) { writeUInt(pidfile_fd, pidfile_pid); (void)write(pidfile_fd, "\n", 1); } freeLimitList(&cfg.ulimits); /* It is too late to handle an error here. So just ignore it... */ (void)close(pidfile_fd); return pid; }
int main(int argc, char *argv[]) { bool quiet = false; char const * vserver; VserverTag tag; while (1) { int c = getopt_long(argc, argv, "ql", CMDLINE_OPTIONS, 0); if (c==-1) break; switch (c) { case 'h' : showHelp(1, argv[0], 0); case 'v' : showVersion(); case 'l' : showTags(); case 'q' : quiet = true; break; default : WRITE_MSG(2, "Try '"); WRITE_STR(2, argv[0]); WRITE_MSG(2, " --help' for more information.\n"); exit(1); break; } } if (optind+2>argc) { execQuery("-", tgSYSINFO, 0, 0); WRITE_MSG(2, "\nAssumed 'SYSINFO' as no other option given; try '--help' for more information.\n"); exit(0); } vserver = argv[optind]; tag = stringToTag(argv[optind+1]); if (tag==tgNONE) { WRITE_MSG(2, "Unknown tag; use '-l' to get list of valid tags\n"); exit(1); } if (quiet) { int fd = Eopen("/dev/null", O_WRONLY, 0644); Edup2(fd, 1); Eclose(fd); } return execQuery(vserver, tag, argc-(optind+2), argv+optind+2); }
void openMsgfile(/*@in@*//*@null@*/char const *filename) /*@globals msg_fd@*/ /*@modifies msg_fd@*/ { if (filename==0 || filename[0]=='\0') { msg_fd = 2; } else if (strcmp(filename, "/dev/null")==0) { msg_fd = -1; } else { int new_fd; new_fd = Eopen(filename, O_CREAT|O_WRONLY|O_APPEND, 0400); msg_fd = Edup2(new_fd, 2); assert(msg_fd==2); Eclose(new_fd); } }
void suppl_log_log_header(void) { char *fnam; nlstime t; assert(suppl_Stack); if(!suppl_log_lock()) return; /* Open the logfile if not done so already */ if(!suppl_l_logfile) { /* If to lock the debug package fails, _item() and _trailer() will silently ignore any calls */ chkHeap if((fnam = appNameEx()) == 0) Esuppl_noMem(); fnam = EStrConcat(2, fnam, DBG_EXTENSION); suppl_l_logfile = Eopen(fnam, suppl_l_openmode); chkHeap free(fnam); }
void openlog(const char * const ident, int option, int facility) { static FLAG8 recursive = 0; /* to prevent recursive calls to openlog() when using syslogo() */ DBG_ENTER("openlog", Suppl_syslog) /* "++semaphore" is considered an atomic operation by many programs; it's native C's best approximation for the P(semaphore) operation */ if(++recursive == 1 /* not recursively called */ && ++syslog_opencount == 1) { /* not opened til now */ /* When these statements are executed, no logfile has been opened so far. That means that they can be logged onto STDERR only. */ chkHeap syslog_options = option; syslog_facility = facility; syslog_ident = Estrdup(ident); if((syslog_fnam = appNameEx()) == 0) Esuppl_noMem(); syslog_fnam = SYSLOG_FNAM(syslog_fnam); syslog_logfile = Eopen(syslog_fnam, syslog_openmode); chkHeap }
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); } } }