static void open_console_window (void) { AllocConsole (); getconsole (); consoleopen = -1; reopen_console (); }
FILE *log_open (const TCHAR *name, int append, int bootlog) { FILE *f = NULL; if (!cs_init) InitializeCriticalSection (&cs); cs_init = 1; if (name != NULL) { if (bootlog >= 0) f = _tfopen (name, append ? L"a, ccs=UTF-8" : L"wt, ccs=UTF-8"); bootlogmode = bootlog; } else if (1) { TCHAR *c = GetCommandLine (); if (_tcsstr (c, L" -console")) { if (GetStdHandle (STD_INPUT_HANDLE) && GetStdHandle (STD_OUTPUT_HANDLE)) { consoleopen = -1; realconsole = 1; getconsole (); _setmode(_fileno (stdout), _O_U16TEXT); _setmode(_fileno (stdin), _O_U16TEXT); } } } return f; }
FILE *log_open (const TCHAR *name, int append, int bootlog, TCHAR *outpath) { FILE *f = NULL; if (!cs_init) InitializeCriticalSection (&cs); cs_init = 1; if (name != NULL) { if (bootlog >= 0) { _tcscpy (outpath, name); f = _tfopen (name, append ? _T("a, ccs=UTF-8") : _T("wt, ccs=UTF-8")); if (!f && bootlog) { TCHAR tmp[MAX_DPATH]; tmp[0] = 0; if (GetTempPath (MAX_DPATH, tmp) > 0) { _tcscat (tmp, _T("winuaetemplog.txt")); _tcscpy (outpath, tmp); f = _tfopen (tmp, append ? _T("a, ccs=UTF-8") : _T("wt, ccs=UTF-8")); } } } } else if (1) { TCHAR *c = GetCommandLine (); if (_tcsstr (c, _T(" -console"))) { if (GetStdHandle (STD_INPUT_HANDLE) && GetStdHandle (STD_OUTPUT_HANDLE)) { consoleopen = -1; realconsole = 1; getconsole (); _setmode(_fileno (stdout), _O_U16TEXT); _setmode(_fileno (stdin), _O_U16TEXT); } } } bootlogmode = bootlog; return f; }
int main(int argc, char *argv[]) { int index; struct sigaction sa; int c; char *p = strrchr(argv[0], '/'); if (p == NULL) p = argv[0]; else p++; pname = p; (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); if (getuid() != 0) die(gettext("must be root to run this program\n")); /* * Handle normal termination signals that may be received. */ sa.sa_handler = SIG_IGN; sa.sa_flags = 0; (void) sigemptyset(&sa.sa_mask); (void) sigaction(SIGHUP, &sa, NULL); (void) sigaction(SIGINT, &sa, NULL); (void) sigaction(SIGQUIT, &sa, NULL); (void) sigaction(SIGTERM, &sa, NULL); /* * To make sure persistent state gets removed. */ sa.sa_handler = cleanup_on_exit; sa.sa_flags = 0; (void) sigemptyset(&sa.sa_mask); (void) sigaction(SIGSEGV, &sa, NULL); (void) sigaction(SIGILL, &sa, NULL); (void) sigaction(SIGABRT, &sa, NULL); (void) sigaction(SIGBUS, &sa, NULL); if (strcmp(pname, CONSDAEMON) == 0) { fallbackdaemon(); return (E_SUCCESS); } if (argc == 1) display++; else { while ((c = getopt(argc, argv, "adp")) != EOF) { switch (c) { case 'a': addflag++; break; case 'd': deleteflag++; break; case 'p': persist++; break; default: (void) fprintf(stderr, gettext(usage)); exit(E_USAGE); /*NOTREACHED*/ } } } if (display) { getconsole(); return (E_SUCCESS); } if (addflag && deleteflag) { (void) fprintf(stderr, gettext(usage)); return (E_ERROR); } if (addflag) { if (optind == argc) { (void) fprintf(stderr, gettext(usage)); return (E_ERROR); } /* separately check every device path specified */ for (index = optind; index < argc; index++) { if (verifyarg(argv[index], addflag)) return (E_ERROR); } for (index = optind; index < argc; index++) { setaux(argv[index]); if (persist) addtolist(argv[index]); } /* * start/restart daemon based on the auxilary * consoles at this time. */ setfallback(argv); return (E_SUCCESS); } else if (deleteflag) { if (optind == argc) { (void) fprintf(stderr, gettext(usage)); return (E_ERROR); } /* separately check every device path specified */ for (index = optind; index < argc; index++) { if (verifyarg(argv[index], 0)) return (E_ERROR); } for (index = optind; index < argc; index++) { unsetaux(argv[index]); if (persist && deleteflag) removefromlist(argv[index]); } /* * kill off daemon and restart with * new list of auxiliary consoles */ setfallback(argv); return (E_SUCCESS); } else if (persist) { if (optind < argc) { (void) fprintf(stderr, gettext(usage)); return (E_ERROR); } persistlist(); return (E_SUCCESS); } else { (void) fprintf(stderr, gettext(usage)); return (E_ERROR); } } /* main */
int main(int argc, char **argv) { char *gidmap = NULL, *inside = NULL, *outside = NULL, *uidmap = NULL; char *bind = NULL; int hostnet = 0, master, option, stdio = 0; pid_t child, parent; while ((option = getopt(argc, argv, "+:b:cg:i:no:u:")) > 0) switch (option) { case 'b': bind = optarg; break; case 'c': stdio++; break; case 'g': gidmap = optarg; break; case 'i': inside = optarg; break; case 'n': hostnet++; break; case 'o': outside = optarg; break; case 'u': uidmap = optarg; break; default: usage(argv[0]); } if (argc <= optind) usage(argv[0]); parent = getpid(); switch (child = fork()) { case -1: error(1, errno, "fork"); case 0: raise(SIGSTOP); // if (geteuid() != 0) // denysetgroups(parent); writemap(parent, GID, gidmap); writemap(parent, UID, uidmap); if (outside) { if (setgid(getgid()) < 0 || setuid(getuid()) < 0) error(1, 0, "Failed to drop privileges"); execlp(SHELL, SHELL, "-c", outside, NULL); error(1, errno, "exec %s", outside); } exit(EXIT_SUCCESS); } if (setgid(getgid()) < 0 || setuid(getuid()) < 0) error(1, 0, "Failed to drop privileges"); if (unshare(CLONE_NEWIPC | CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWUTS) < 0) error(1, 0, "Failed to unshare namespaces"); if (!hostnet && unshare(CLONE_NEWNET) < 0) error(1, 0, "Failed to unshare network namespace"); waitforstop(child); kill(child, SIGCONT); waitforexit(child); setgid(0); setgroups(0, NULL); setuid(0); master = stdio ? -1 : getconsole(); createroot(argv[optind], master, inside, bind); unshare(CLONE_NEWPID); switch (child = fork()) { case -1: error(1, errno, "fork"); case 0: mountproc(); if (!hostnet) mountsys(); enterroot(); if (master >= 0) { close(master); setconsole("/dev/console"); } clearenv(); putenv("container=contain"); if (argv[optind + 1]) execv(argv[optind + 1], argv + optind + 1); else execl(SHELL, SHELL, NULL); error(1, errno, "exec"); } return supervise(child, master); }