Example #1
0
/// gets a user-inputted string until return is entered
/// will use getChar to process characters one by one so that individual
/// key handlers can be created
std::string getInputString(teasafe::TeaSafe &theBfs, std::string const &workingPath)
{
    std::string toReturn("");

    // disable echo and prevent need to press enter for getchar flush
    setupTerminal();

    int cursorPos(0);
    while(1) {
        char c = getchar();
        if((int)c == 10) { // enter
            std::cout<<std::endl;
            break;
        }
        if((int)c == 127 || (int)c == 8) { // delete / backspace

            handleBackspace(cursorPos, toReturn);

        } else if((int)c == 9) { // tab

            handleTabKey(theBfs, workingPath, cursorPos, toReturn);

        } else { // print out char to screen and push into string vector
            std::cout<<c;
            ++cursorPos;
            toReturn.push_back(c);
        }
    }
    return toReturn;
}
Example #2
0
int get_username_and_password(char ** username, int * username_length, char ** password, int * password_length) {

        fputs("login: "******"passwd: ",stdout);
	setupTerminal();
        unsigned int password_position = 0;
        unsigned int saw_EOL = 0;
        unsigned int read_password = 0;
        while (!read_password) {
            char hidden_character;
            hidden_character = getchar();
            if (hidden_character == '\b' || (int) hidden_character == 127) {
                if (password_position > 0) {
                    puts("\b \b");
                    password_position--;
                }
            }
            else if (hidden_character == '\n' || hidden_character == '\r') {
                saw_EOL = 1;
                puts("\n");
                read_password = 1;
            } else if (hidden_character !=  0) {
               (*password)[password_position] = hidden_character;
               password_position++;
            }

            if (*password_length <= password_position) {
                read_password = 1;
            }
        }
        if (!saw_EOL) {
            exit(1);
        }
        (*password)[password_position] = '\0';
	resetTerminal();
	
        return 0;
}
Example #3
0
static void spawnProc(void *stack)
{
	kprintf("%$\x02" "Done%#\n");

	initInterp();

	kprintf("Allocating memory for bootstrap... ");
	FrameList *fl = palloc(2);
	AddSegment(getCurrentThread()->pm, 1, fl, PROT_READ | PROT_WRITE | PROT_EXEC);
	pdownref(fl);
	SetProcessMemory(getCurrentThread()->pm);
	kprintf("%$\x02" "Done%#\n");

	kprintf("Setting up the terminal... ");
	setupTerminal(getCurrentThread()->ftab);
	kprintf("%$\x02" "Done%#\n");

	kprintf("Loading /initrd/usbs... ");
	int err;
	File *file = vfsOpen("/initrd/usbs", VFS_CHECK_ACCESS, &err);
	if (file == NULL)
	{
		kprintf("%$\x04" "Failed%#\n");
		panic("failed to open /initrd/usbs");
	};
	ssize_t count = vfsRead(file, (void*) 0x1000, 0x1000);
	if (count < 1)
	{
		kprintf("%$\x04" "Failed%#\n");
		panic("read() /initrd/usbs: %d\n", count);
	};
	vfsClose(file);
	kprintf("%$\x02" "%d bytes%#\n", count);

	kprintf("Control will be transferred to usbs now.\n");
	_jmp_usbs(stack);
};
Example #4
0
int main(int argc, char **argv) {
    pid_t installpid, childpid;
    int waitStatus;
    int fd = -1;
    int doShutdown =0;
    reboot_action shutdown_method = HALT;
    int isSerial = 0;
    int isDevelMode = 0;
    char * console = NULL;
    int doKill = 1;
    char * argvc[15];
    char buf[1024];
    char ** argvp = argvc;
    char twelve = 12;
    struct serial_struct si;
    int i, disable_keys;
    int ret;

    if (!strncmp(basename(argv[0]), "poweroff", 8)) {
        printf("Running poweroff...\n");
        fd = getInitPid();
        if (fd > 0)
            kill(fd, SIGUSR2);
        doExit(0);
    } else if (!strncmp(basename(argv[0]), "halt", 4)) {
        printf("Running halt...\n");
        fd = getInitPid();
        if (fd > 0)
            kill(fd, SIGUSR1);
        doExit(0);
    } else if (!strncmp(basename(argv[0]), "reboot", 6)) {
        printf("Running reboot...\n");
        fd = getInitPid();
        if (fd > 0)
            kill(fd, SIGINT);
        doExit(0);
    }

    /* turn off screen blanking */
    printstr("\033[9;0]");
    printstr("\033[8]");

    umask(022);

    /* set up signal handler */
    setupBacktrace();

    printstr("\nGreetings.\n");

    printf("anaconda installer init version %s starting\n", VERSION);

    printf("mounting /proc filesystem... "); 
    fflush(stdout);
    if (mount("/proc", "/proc", "proc", 0, NULL))
        fatal_error(1);
    printf("done\n");

    /* check for development mode early */
    int fdn;
    if ((fdn = open("/proc/cmdline", O_RDONLY, 0)) != -1) {

        /* get cmdline info */
        int len = read(fdn, buf, sizeof(buf) - 1);
        char *develstart;
        close(fdn);

        /* check the arguments */
        if (len > 0) {
            develstart = buf;
            while (develstart && (*develstart) != '\0') {
                
                /* strip spaces */
		while(*develstart == ' ') develstart++;
		if(*develstart == '\0') break;
                
                /* not the word we are looking for */
                if (strncmp(develstart, "devel", 5)) {
                    develstart = strchr(develstart, ' ');
                    continue;
		}
                
                /* is it isolated? */
                if(((*(develstart+5)) == ' ' || (*(develstart+5)) == '\0')) {
                    printf("Enabling development mode - cores will be dumped\n");
                    isDevelMode++;
                    break;
                }
                
                /* Find next argument */
                develstart = strchr(develstart, ' ');
            }
        }
    }

    /* these args are only for testing from commandline */
    for (i = 1; i < argc; i++) {
        if (!strcmp (argv[i], "serial")) {
            isSerial = 1;
            break;
        }
    }

    printf("creating /dev filesystem... "); 
    fflush(stdout);
    if (mount("/dev", "/dev", "tmpfs", 0, NULL))
        fatal_error(1);
    createDevices();
    printf("done\n");

    printf("starting udev...");
    fflush(stdout);
    if ((childpid = fork()) == 0) {
        execl("/sbin/udevd", "/sbin/udevd", "--daemon", NULL);
        fprintf(stderr, " exec of /sbin/udevd failed.");
        exit(1);
    }

    /* wait at least until the udevd process that we forked exits */
    do {
        pid_t retpid;
        int waitstatus;

        retpid = wait(&waitstatus);
        if (retpid == -1) {
            if (errno == EINTR)
                continue;
            /* if the child exited before we called waitpid, we can get
             * ECHILD without anything really being wrong; we just lost
             * the race.*/
            if (errno == ECHILD)
                break;
            printf("init: error waiting on udevd: %m\n");
            exit(1);
        } else if ((retpid == childpid) && WIFEXITED(waitstatus)) {
            break;
        }
    } while (1);

    if (fork() == 0) {
        execl("/sbin/udevadm", "udevadm", "control", "--env=ANACONDA=1", NULL);
        fprintf(stderr, " exec of /sbin/udevadm failed.");
        exit(1);
    }
    printf("done\n");

    printf("mounting /dev/pts (unix98 pty) filesystem... "); 
    fflush(stdout);
    if (mount("/dev/pts", "/dev/pts", "devpts", 0, NULL))
        fatal_error(1);
    printf("done\n");

    printf("mounting /sys filesystem... "); 
    fflush(stdout);
    if (mount("/sys", "/sys", "sysfs", 0, NULL))
        fatal_error(1);
    printf("done\n");

    /* if anaconda dies suddenly we are doomed, so at least make a coredump */
    struct rlimit corelimit = { RLIM_INFINITY,  RLIM_INFINITY};
    ret = setrlimit(RLIMIT_CORE, &corelimit);
    if (ret) {
        perror("setrlimit failed - no coredumps will be available");
    }

    doKill = getKillPolicy();

#if !defined(__s390__) && !defined(__s390x__)
    static struct termios orig_cmode;
    static int            orig_flags;
    struct termios cmode, mode;
    int cfd;
    
    cfd =  open("/dev/console", O_RDONLY);
    tcgetattr(cfd,&orig_cmode);
    orig_flags = fcntl(cfd, F_GETFL);
    close(cfd);

    cmode = orig_cmode;
    cmode.c_lflag &= (~ECHO);

    cfd = open("/dev/console", O_WRONLY);
    tcsetattr(cfd,TCSANOW,&cmode);
    close(cfd);

    /* handle weird consoles */
#if defined(__powerpc__)
    char * consoles[] = { "/dev/hvc0", /* hvc for JS20 */

                          "/dev/hvsi0", "/dev/hvsi1",
                          "/dev/hvsi2", /* hvsi for POWER5 */
                          NULL };
#elif defined (__ia64__)
    char * consoles[] = { "/dev/ttySG0", "/dev/xvc0", "/dev/hvc0", NULL };
#elif defined (__i386__) || defined (__x86_64__)
    char * consoles[] = { "/dev/xvc0", "/dev/hvc0", NULL };
#else
    char * consoles[] = { NULL };
#endif
    for (i = 0; consoles[i] != NULL; i++) {
        if ((fd = open(consoles[i], O_RDWR)) >= 0 && !tcgetattr(fd, &mode) && !termcmp(&cmode, &mode)) {
            printf("anaconda installer init version %s using %s as console\n",
                   VERSION, consoles[i]);
            isSerial = 3;
            console = strdup(consoles[i]);
            break;
        }
        close(fd);
    }

    cfd = open("/dev/console", O_WRONLY);
    tcsetattr(cfd,TCSANOW,&orig_cmode);
    close(cfd); 

    if ((fd < 0) && (ioctl (0, TIOCLINUX, &twelve) < 0)) {
        isSerial = 2;

        if (ioctl(0, TIOCGSERIAL, &si) == -1) {
            isSerial = 0;
        }
    }

    if (isSerial && (isSerial != 3)) {
        char *device = "/dev/ttyS0";

        printf("anaconda installer init version %s using a serial console\n", 
               VERSION);

        if (isSerial == 2)
            device = "/dev/console";
        fd = open(device, O_RDWR, 0);
        if (fd < 0)
            device = "/dev/tts/0";

        if (fd < 0) {
            printf("failed to open %s\n", device);
            fatal_error(1);
        }

        setupTerminal(fd);
    } else if (isSerial == 3) {
        setupTerminal(fd);
    } else if (fd < 0)  {
        fd = open("/dev/tty1", O_RDWR, 0);
        if (fd < 0)
            fd = open("/dev/vc/1", O_RDWR, 0);

        if (fd < 0) {
            printf("failed to open /dev/tty1 and /dev/vc/1");
            fatal_error(1);
        }
    }

    setsid();
    if (ioctl(0, TIOCSCTTY, NULL)) {
        printf("could not set new controlling tty\n");
    }

    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    if (fd > 2)
        close(fd);
#else
    dup2(0, 1);
    dup2(0, 2);
#endif

    /* disable Ctrl+Z, Ctrl+C, etc ... but not in rescue mode */
    disable_keys = 1;
    if (argc > 1)
        if (strstr(argv[1], "rescue"))
            disable_keys = 0;

    if (disable_keys) {
        tcgetattr(0, &ts);
        ts.c_iflag &= ~BRKINT;
        ts.c_iflag |= IGNBRK;
        ts.c_iflag &= ~ISIG;
        tcsetattr(0, TCSANOW, &ts);
    }

    ret = sethostname("localhost.localdomain", 21);
    /* the default domainname (as of 2.0.35) is "(none)", which confuses 
     glibc */
    ret = setdomainname("", 0);

    printf("trying to remount root filesystem read write... ");
    fflush(stdout);
    if (mount("/", "/", "ext2", MS_REMOUNT | MS_MGC_VAL, NULL)) {
        fatal_error(1);
    }
    printf("done\n");

    /* we want our /tmp to be tmpfs, but we also want to let people hack
     * their initrds to add things like a ks.cfg, so this has to be a little
     * tricky */
    rename("/tmp", "/oldtmp");
    mkdir("/tmp", 0755);

    printf("mounting /tmp as tmpfs... ");
    fflush(stdout);
    if (mount("none", "/tmp", "tmpfs", 0, "size=250m"))
        fatal_error(1);
    printf("done\n");

    copyDirectory("/oldtmp", "/tmp", copyErrorFn, copyErrorFn);
    unlink("/oldtmp");

    /* Now we have some /tmp space set up, and /etc and /dev point to
       it. We should be in pretty good shape. */
    startSyslog();

    /* write out a pid file */
    if ((fd = open("/var/run/init.pid", O_WRONLY|O_CREAT, 0644)) > 0) {
        char * buf = malloc(10);
        int ret;

        snprintf(buf, 9, "%d", getpid());
        ret = write(fd, buf, strlen(buf));
        close(fd);
        free(buf);
    } else {
        printf("unable to write init.pid (%d): %m\n", errno);
        sleep(2);
    }

    /* D-Bus */
    if (fork() == 0) {
        execl("/sbin/dbus-uuidgen", "/sbin/dbus-uuidgen", "--ensure", NULL);
        fprintf(stderr, "exec of /sbin/dbus-uuidgen failed.");
        doExit(1);
    }

    if (fork() == 0) {
        execl("/sbin/dbus-daemon", "/sbin/dbus-daemon", "--system", NULL);
        fprintf(stderr, "exec of /sbin/dbus-daemon failed.");
        doExit(1);
    }

    sleep(2);

    /* Go into normal init mode - keep going, and then do a orderly shutdown
       when:

       1) /bin/install exits
       2) we receive a SIGHUP 
    */

    printf("running install...\n"); 

    if (!(installpid = fork())) {
        /* child */
        *argvp++ = "/sbin/loader";

        if (isSerial == 3) {
            *argvp++ = "--virtpconsole";
            *argvp++ = console;
        }

        if (isDevelMode) {
            *argvp++ = "--devel";
        }

        *argvp++ = NULL;

        printf("running %s\n", argvc[0]);
        execve(argvc[0], argvc, env);

        shutDown(1, HALT);
    }

    /* signal handlers for halt/poweroff */
    signal(SIGUSR1, sigUsr1Handler);
    signal(SIGUSR2, sigUsr2Handler);

    /* set up the ctrl+alt+delete handler to kill our pid, not pid 1 */
    signal(SIGINT, sigintHandler);
    if ((fd = open("/proc/sys/kernel/cad_pid", O_WRONLY)) != -1) {
        char buf[7];
        size_t count;
        sprintf(buf, "%d", getpid());
        count = write(fd, buf, strlen(buf));
        close(fd);
        /* if we succeeded in writing our pid, turn off the hard reboot
           ctrl-alt-del handler */
        if (count == strlen(buf) &&
            (fd = open("/proc/sys/kernel/ctrl-alt-del", O_WRONLY)) != -1) {
            int ret;

            ret = write(fd, "0", 1);
            close(fd);
        }
    }
    
    while (!doShutdown) {
        pid_t childpid;
        childpid = wait(&waitStatus);

        if (childpid == installpid) {
            doShutdown = 1;
            ioctl(0, VT_ACTIVATE, 1);
        }
    }

#ifdef  ROCKS
    /*
     * ignore child processes that throw error stati when
     * they terminate
     */
    shutdown_method = REBOOT;
#else
    if (!WIFEXITED(waitStatus) ||
        (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus))) {

        /* Restore terminal */
        cfd =  open("/dev/console", O_RDONLY);
        tcsetattr(cfd, TCSANOW, &orig_cmode);
        fcntl(cfd, F_SETFL, orig_flags);
        close(cfd);

        shutdown_method = DELAYED_REBOOT;
        printf("install exited abnormally [%d/%d] ", WIFEXITED(waitStatus),
                                                     WEXITSTATUS(waitStatus));
        if (WIFSIGNALED(waitStatus)) {
            printf("-- received signal %d", WTERMSIG(waitStatus));
        }
        printf("\n");

        /* If debug mode was requested, spawn shell */
        if(isDevelMode) {
            pid_t shellpid;

            printf("Development mode requested spawning shell...\n");

            if ((shellpid = fork()) == 0) {
                execl("/sbin/bash", "/sbin/bash", NULL);
            }
            else if (shellpid > 0) {
                waitpid(shellpid, NULL, 0);
            }
            else {
                perror("Execution of debug shell failed.");
            }

        }

    } else {
        shutdown_method = REBOOT;
    }
#endif

#ifdef  ROCKS
    while(dontReboot()) {
            sleep(10);
    }
#endif

    shutDown(doKill, shutdown_method);

    return 0;
}
int main(int argc, char **argv) {
    pid_t installpid, childpid;
    int waitStatus;
    int fd = -1;
    int doReboot = 0;
    int doShutdown =0;
    int isSerial = 0;
    char * console = NULL;
    int noKill = 0;
    char * argvc[15];
    char ** argvp = argvc;
    char twelve = 12;
    struct serial_struct si;
    int i, disable_keys;

    if (!strncmp(basename(argv[0]), "poweroff", 8)) {
        printf("Running poweroff...\n");
        fd = getInitPid();
        if (fd > 0)
            kill(fd, SIGUSR2);
        exit(0);
    } else if (!strncmp(basename(argv[0]), "halt", 4)) {
        printf("Running halt...\n");
        fd = getInitPid();
        if (fd > 0)
            kill(fd, SIGUSR1);
        exit(0);
    } else if (!strncmp(basename(argv[0]), "reboot", 6)) {
        printf("Running reboot...\n");
        fd = getInitPid();
        if (fd > 0)
            kill(fd, SIGINT);
        exit(0);
    }

#if !defined(__s390__) && !defined(__s390x__)
    testing = (getppid() != 0) && (getppid() != 1);
#endif

    if (!testing) {
        /* turn off screen blanking */
        printstr("\033[9;0]");
        printstr("\033[8]");
    } else {
        printstr("(running in test mode).\n");
    }

    umask(022);

    printstr("\nGreetings.\n");

    printf("anaconda installer init version %s starting\n", VERSION);

    printf("mounting /proc filesystem... ");
    if (!testing) {
        if (mount("/proc", "/proc", "proc", 0, NULL))
            fatal_error(1);
    }
    printf("done\n");

    printf("creating /dev filesystem... ");
    if (!testing) {
        if (mount("/dev", "/dev", "tmpfs", 0, NULL))
            fatal_error(1);
        createDevices();
    }
    printf("done\n");

    printf("mounting /dev/pts (unix98 pty) filesystem... ");
    if (!testing) {
        if (mount("/dev/pts", "/dev/pts", "devpts", 0, NULL))
            fatal_error(1);
    }
    printf("done\n");

    printf("mounting /sys filesystem... ");
    if (!testing) {
        if (mount("/sys", "/sys", "sysfs", 0, NULL))
            fatal_error(1);
    }
    printf("done\n");

    /* these args are only for testing from commandline */
    for (i = 1; i < argc; i++) {
        if (!strcmp (argv[i], "serial")) {
            isSerial = 1;
            break;
        }
    }

    noKill = getNoKill();

#if !defined(__s390__) && !defined(__s390x__)
    static struct termios orig_cmode;
    struct termios cmode, mode;
    int cfd;

    cfd =  open("/dev/console", O_RDONLY);
    tcgetattr(cfd,&orig_cmode);
    close(cfd);

    cmode = orig_cmode;
    cmode.c_lflag &= (~ECHO);

    cfd = open("/dev/console", O_WRONLY);
    tcsetattr(cfd,TCSANOW,&cmode);
    close(cfd);

    /* handle weird consoles */
#if defined(__powerpc__)
    char * consoles[] = { "/dev/hvc0", /* hvc for JS20 */

                          "/dev/hvsi0", "/dev/hvsi1",
                          "/dev/hvsi2", /* hvsi for POWER5 */
                          NULL
                        };
#elif defined (__ia64__)
    char * consoles[] = { "/dev/ttySG0", "/dev/xvc0", NULL };
#elif defined (__i386__) || defined (__x86_64__)
    char * consoles[] = { "/dev/xvc0", NULL };
#else
    char * consoles[] = { NULL };
#endif
    for (i = 0; consoles[i] != NULL; i++) {
        if ((fd = open(consoles[i], O_RDWR)) >= 0 && !tcgetattr(fd, &mode) && !termcmp(&cmode, &mode)) {
            printf("anaconda installer init version %s using %s as console\n",
                   VERSION, consoles[i]);
            isSerial = 3;
            console = strdup(consoles[i]);
            break;
        }
        close(fd);
    }

    cfd = open("/dev/console", O_WRONLY);
    tcsetattr(cfd,TCSANOW,&orig_cmode);
    close(cfd);

    if ((fd < 0) && (ioctl (0, TIOCLINUX, &twelve) < 0)) {
        isSerial = 2;
        if (ioctl(0, TIOCGSERIAL, &si) == -1) {
            isSerial = 0;
        }
    }

    if (isSerial && (isSerial != 3)) {
        char *device = "/dev/ttyS0";

        printf("anaconda installer init version %s using a serial console\n",
               VERSION);

        if (isSerial == 2)
            device = "/dev/console";
        fd = open(device, O_RDWR, 0);
        if (fd < 0)
            device = "/dev/tts/0";

        if (fd < 0) {
            printf("failed to open %s\n", device);
            fatal_error(1);
        }

        setupTerminal(fd);
    } else if (isSerial == 3) {
        setupTerminal(fd);
    } else if (fd < 0)  {
        fd = open("/dev/tty1", O_RDWR, 0);
        if (fd < 0)
            fd = open("/dev/vc/1", O_RDWR, 0);

        if (fd < 0) {
            printf("failed to open /dev/tty1 and /dev/vc/1");
            fatal_error(1);
        }
    }

    if (testing)
        exit(0);

    setsid();
    if (ioctl(0, TIOCSCTTY, NULL)) {
        printf("could not set new controlling tty\n");
    }

    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    if (fd > 2)
        close(fd);
#else
    dup2(0, 1);
    dup2(0, 2);
#endif

    /* disable Ctrl+Z, Ctrl+C, etc ... but not in rescue mode */
#ifdef SNAKES_ON_A_PLANE
    disable_keys = 0;
#else
    disable_keys = 1;
    if (argc > 1)
        if (mystrstr(argv[1], "rescue"))
            disable_keys = 0;
#endif

    if (disable_keys) {
        tcgetattr(0, &ts);
        ts.c_iflag &= ~BRKINT;
        ts.c_iflag |= IGNBRK;
        ts.c_iflag &= ~ISIG;
        tcsetattr(0, TCSANOW, &ts);
    }

    if (!testing) {
        int ret;
        ret = sethostname("localhost.localdomain", 21);
        /* the default domainname (as of 2.0.35) is "(none)", which confuses
         glibc */
        ret = setdomainname("", 0);
    }

    printf("trying to remount root filesystem read write... ");
    if (mount("/", "/", "ext2", MS_REMOUNT | MS_MGC_VAL, NULL)) {
        fatal_error(1);
    }
    printf("done\n");

    /* we want our /tmp to be ramfs, but we also want to let people hack
     * their initrds to add things like a ks.cfg, so this has to be a little
     * tricky */
    if (!testing) {
        rename("/tmp", "/oldtmp");
        mkdir("/tmp", 0755);

        printf("mounting /tmp as ramfs... ");
        if (mount("none", "/tmp", "ramfs", 0, NULL))
            fatal_error(1);
        printf("done\n");

        copyDirectory("/oldtmp", "/tmp");
        unlink("/oldtmp");
    }

    /* Now we have some /tmp space set up, and /etc and /dev point to
       it. We should be in pretty good shape. */

    if (!testing)
        doklog("/dev/tty4");

    /* write out a pid file */
    if ((fd = open("/var/run/init.pid", O_WRONLY|O_CREAT, 0644)) > 0) {
        char * buf = malloc(10);
        int ret;

        snprintf(buf, 9, "%d", getpid());
        ret = write(fd, buf, strlen(buf));
        close(fd);
        free(buf);
    } else {
        printf("unable to write init.pid (%d): %s\n", errno, strerror(errno));
        sleep(2);
    }

    /* Go into normal init mode - keep going, and then do a orderly shutdown
       when:

       1) /bin/install exits
       2) we receive a SIGHUP
    */

    printf("running install...\n");

    setsid();

#ifdef SNAKES_ON_A_PLANE
    printf("> Snakes on a Plane <\n");

    /* hack to load core modules for debugging mode */
    char * modvc[15];
    char ** modvp = modvc;
    *modvp++ = "/bin/modprobe";
    *modvp++ = "ehci-hcd";
    *modvp++ = "uhci-hcd";
    *modvp++ = "ohci-hcd";
    *modvp++ = NULL;
    pid_t blah = fork();
    int qux;
    if (blah == 0) {
        printf("loading core debugging modules...\n");
        execve(modvc[0], modvc, env);
    } else {
        waitpid(blah, &qux, WNOHANG);
    }
#endif

    if (!(installpid = fork())) {
        /* child */
#ifdef SNAKES_ON_A_PLANE
        *argvp++ = "/bin/strace";
#endif
        *argvp++ = "/sbin/loader";

        if (isSerial == 3) {
            *argvp++ = "--virtpconsole";
            *argvp++ = console;
        }

        *argvp++ = NULL;

        printf("running %s\n", argvc[0]);
        execve(argvc[0], argvc, env);

        shutDown(1, 0, 0);
    }

    /* signal handlers for halt/poweroff */
    signal(SIGUSR1, sigUsr1Handler);
    signal(SIGUSR2, sigUsr2Handler);

    /* set up the ctrl+alt+delete handler to kill our pid, not pid 1 */
    signal(SIGINT, sigintHandler);
    if ((fd = open("/proc/sys/kernel/cad_pid", O_WRONLY)) != -1) {
        char buf[7];
        size_t count;
        sprintf(buf, "%d", getpid());
        count = write(fd, buf, strlen(buf));
        close(fd);
        /* if we succeeded in writing our pid, turn off the hard reboot
           ctrl-alt-del handler */
        if (count == strlen(buf) &&
                (fd = open("/proc/sys/kernel/ctrl-alt-del", O_WRONLY)) != -1) {
            int ret;

            ret = write(fd, "0", 1);
            close(fd);
        }
    }

    while (!doShutdown) {
        childpid = waitpid(-1, &waitStatus, 0);

        if (childpid == installpid)
            doShutdown = 1;
    }

    if (!WIFEXITED(waitStatus) ||
            (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus))) {
        printf("install exited abnormally [%d/%d] ", WIFEXITED(waitStatus),
               WEXITSTATUS(waitStatus));
        if (WIFSIGNALED(waitStatus)) {
            printf("-- received signal %d", WTERMSIG(waitStatus));
        }
        printf("\n");
    } else {
        doReboot = 1;
    }

    if (testing)
        exit(0);

    shutDown(noKill, doReboot, 0);

    return 0;
}
Example #6
0
int main(int argc, char ** argv) {
    if(argc != 4) {
        std::cerr << "usage: " << argv[0] << " server_name port user_name" << std::endl;
        exit(-1);
    }
    char * hostName = argv[1];
    int portNum = atoi(argv[2]);
    char * portNumStr = argv[2];
    char * userName = argv[3];

    if(portNum < 0 || portNum > 65535) {
        std::cerr << "error: port number must be between 0 and 65535" << std::endl;
        exit(-2);
    } 

    if(strnlen(userName, USERNAME_MAX+1) > USERNAME_MAX) {
        std::cerr << "error: username can be no longer than " << USERNAME_MAX << " characters" << std::endl;
        exit(-3);
    }

    struct addrinfo hints, *servinfo, *p;
    int status;
    int numbytes;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    status = getaddrinfo(hostName, portNumStr, &hints, &servinfo);

    if(status != 0) {
        std::cerr << "error: unable to resolve address: " << gai_strerror(status) << std::endl;
        exit(-4);
    }

    for(p = servinfo; p != NULL; p = p->ai_next) {
        sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
        if(sock == -1) {
            std::cerr << "warning: could not connect to socket" << std::endl;
            continue;
        }
        break;
    }

    if(p == NULL) {
        std::cerr << "error: all sockets failed to connect" << std::endl;
        exit(-5);
    }
	
	memset(curChannel, 0, CHANNEL_MAX);
	strncpy(curChannel, "Common", CHANNEL_MAX);
	channelsJoined.insert(curChannel);
	
	buf = (text *) malloc(MAX_BUFFER_SIZE);

    fcntl(sock, F_SETFL, O_NONBLOCK);

    sendLoginPacket(sock, p, userName);
    sendJoinPacket(sock, p, "Common");

    atexit(restoreTerminal);
    setupTerminal();

    line = new char[MAXLINE+1];
	memset(line, '\0', MAXLINE+1);

	clearInput();
    refreshAll();

	signal(SIGALRM, timerExpired);
	alarm(KEEP_ALIVE_FREQ);

    while(handleInput(sock, p)) {
    }

    sendLogoutPacket(sock, p);

    delete [] line; 
	free(buf);

    return 0;
}
Example #7
0
int main(int argCount, char **argString)
{
	char keyPressed = FALSE;
	int keyboardLock = FALSE;
	double keyboardLockTime = ojGetTimeSec() + KEYBOARD_LOCK_TIMEOUT_SEC;
	time_t timeStamp;

	//Get and Format Time String
	time(&timeStamp);
	strftime(timeString, DEFAULT_STRING_LENGTH-1, "%m-%d-%Y %X", localtime(&timeStamp));

	system(CLEAR);

	//cDebug(1, "main: Starting Up %s Node Software\n", simulatorGetName());
//	if(simulatorStartup())
//	{
//		printf("main: %s Node Startup failed\n", simulatorGetName());
//		//cDebug(1, "main: Exiting %s Node Software\n", simulatorGetName());
//#if defined(WIN32)
//		system("pause");
//#else
//		printf("Press ENTER to exit\n");
//		getch();
//#endif
//		return 0;
//	}

	vehicleSimStartup();
	pd = pdCreate();
	gpos = gposCreate();
	vss = vssCreate();
	wd = wdCreate();

	setupTerminal();

	mainRunning = TRUE;

	while(mainRunning)
	{
		keyPressed = getUserInput();

		if(keyPressed)
		{
			keyboardLockTime = ojGetTimeSec() + KEYBOARD_LOCK_TIMEOUT_SEC;
		}
		else if(ojGetTimeSec() > keyboardLockTime)
		{
				keyboardLock = TRUE;
		}

		//if(verbose)
		//{
		//	choice = getc(stdin);
		//}
		//else // Not in verbose mode
		//{
		//	choice = getch(); // Get the key that the user has selected
		//	updateScreen(keyboardLock, choice);
		//}

		ojSleepMsec(100);
	}

	cleanupConsole();

	//cDebug(1, "main: Shutting Down %s Node Software\n", simulatorGetName());
	wdDestroy(wd);
	pdDestroy(pd);
	gposDestroy(gpos);
	vssDestroy(vss);
	vehicleSimShutdown();

	if(logFile != NULL)
	{
		fclose(logFile);
	}

	return 0;
}