Esempio n. 1
0
int archdep_spawn(const char *name, char **argv, char **pstdout_redir, const char *stderr_redir)
{
    pid_t child_pid;
    int child_status;
    char *stdout_redir = NULL;

    if (pstdout_redir != NULL) {
        if (*pstdout_redir == NULL) {
            *pstdout_redir = archdep_tmpnam();
        }
        stdout_redir = *pstdout_redir;
    }

#ifdef WORDS_BIGENDIAN
    child_pid = -1;
#else
    child_pid = vfork();
#endif

    if (child_pid < 0) {
        log_error(LOG_DEFAULT, "vfork() failed: %s.", strerror(errno));
        return -1;
    } else {
        if (child_pid == 0) {
            if (stdout_redir && freopen(stdout_redir, "w", stdout) == NULL) {
                log_error(LOG_DEFAULT, "freopen(\"%s\") failed: %s.",
                          stdout_redir, strerror(errno));
                _exit(-1);
            }
            if (stderr_redir && freopen(stderr_redir, "w", stderr) == NULL) {
                log_error(LOG_DEFAULT, "freopen(\"%s\") failed: %s.",
                          stderr_redir, strerror(errno));
                _exit(-1);
            }
            execvp(name, argv);
            _exit(-1);
        }
    }

    if (waitpid(child_pid, &child_status, 0) != child_pid) {
        log_error(LOG_DEFAULT, "waitpid() failed: %s", strerror(errno));
        return -1;
    }

    if (WIFEXITED(child_status)) {
        return WEXITSTATUS(child_status);
    } else {
        return -1;
    }
}
char *platform_get_sco_runtime_os(void)
{
    int retval = 0;
    struct utsname name;
    FILE *infile;
    int amount = 0;
    int i = 0;
    char *tempfile = NULL;
    char *tempsystem = NULL;

    if (!got_os) {
        buffer[0] = 0;
        retval = uname(&name);
        if (!retval) {
            sprintf(buffer, "%s", name.version);
        } else {
            tempfile = archdep_tmpnam();
            tempsystem = util_concat("uname -v >", tempfile, NULL);
            system(tempsystem);
            infile = fopen(tempfile, "rb");
            if (infile) {
                amount = fread(buffer, 1, 80, infile);
                if (amount) {
                    while (buffer[i] != '\n') {
                        i++;
                    }
                    buffer[i] = 0;
                }
                fclose(infile);
            }
            unlink(tempfile);
            lib_free(tempfile);
            lib_free(tempsystem);
        }
        if (buffer[0] == '2') {
            sprintf(platform_os, "SCO Unix v4.x");
        } else if (buffer[0] == '5' || buffer[0] == '6') {
            sprintf(platform_os, "SCO Openserver %s", buffer);
        } else if (buffer[0] == '7') {
            sprintf(platform_os, "SCO Unixware %s", buffer);
        } else {
            sprintf(platform_os, "SCO Unix");
        }
        got_os = 1;
    }
    return platform_os;
}
char *platform_get_sco_runtime_cpu(void)
{
    struct utsname name;
    int retval = 0;
    int i = 0;
    int amount = 0;
    FILE *infile;
    char *tempfile = NULL;
    char *tempsystem = NULL;

    if (!got_cpu) {
        retval = uname(&name);
        if (!retval) {
            sprintf(platform_cpu, "%s", name.machine);
        } else {
            tempfile = archdep_tmpnam();
            tempsystem = util_concat("uname -m >", tempfile, NULL);
            system(tempsystem);
            infile = fopen(tempfile, "rb");
            if (infile) {
                amount = fread(platform_cpu, 1, 20, infile);
                if (amount) {
                    while (platform_cpu[i] != '\n') {
                        i++;
                    }
                    platform_cpu[i] = 0;
                }
                fclose(infile);
            }
            unlink(tempfile);
            lib_free(tempfile);
            lib_free(tempsystem);
        }
        got_cpu = 1;
    }
    return platform_cpu;
}
char *platform_get_linux_runtime_cpu(void)
{
    FILE *cpuinfo = NULL;
    char *buffer = NULL;
    char *loc1 = NULL;
    char *loc2 = NULL;
    char *loc3 = NULL;
    char *tempfile = NULL;
    char *tempsystem = NULL;
    size_t size1 = 0;
    size_t size2 = 0;
    struct utsname name;

    if (!got_linux_cpu) {
        sprintf(linux_cpu, "Unknown CPU");
        cpuinfo = fopen("/proc/cpuinfo", "rb");
        if (cpuinfo) {
            fclose(cpuinfo);
            cpuinfo = NULL;
            tempfile = archdep_tmpnam();
            tempsystem = util_concat("cat /proc/cpuinfo >", tempfile, NULL);
            if (system(tempsystem) < 0) {
                log_warning(LOG_ERR, "`%s' failed.", tempsystem);
            }
            cpuinfo = fopen(tempfile, "rb");
        }
        if (cpuinfo) {
            fseek(cpuinfo, 0L, SEEK_END);
            size1 = ftell(cpuinfo);
            fseek(cpuinfo, 0L, SEEK_SET);
            buffer = (char *)malloc(size1);
            size2 = fread(buffer, 1, size1, cpuinfo);
            if (size1 == size2) {
                loc1 = strstr(buffer, "model name");
                if (!loc1) {
                    loc1 = strstr(buffer, "cpu type");
                }
                if (!loc1) {
                    loc1 = strstr(buffer, "cpu model");
                }
                if (!loc1) {
                    loc1 = strstr(buffer, "Processor");
                }
                if (!loc1) {
                    loc1 = strstr(buffer, "cpu");
                    if (loc1 && loc1 != buffer) {
                        loc1--;
                        if (*loc1 != '\n' && isspace(*loc1)) {
                            loc1 = NULL;
                        }
                    }
                }
                if (!loc1) {
                    loc1 = strstr(buffer, "CPU");
                }
                if (!loc1) {
                    loc1 = strstr(buffer, "vendor_id");
                }
                if (loc1) {
                    loc2 = strstr(loc1, ":");
                    if (loc2) {
                        loc2 += 2;
                        while (isspace(*loc2)) {
                            loc2++;
                        }
                        loc3 = strstr(loc2, "\n");
                        if (loc3) {
                            *loc3 = 0;
                            sprintf(linux_cpu, "%s", loc2);
                            got_linux_cpu = 1;
                        }
                    }
                }
            }
            fclose(cpuinfo);
            unlink(tempfile);
            lib_free(tempfile);
            lib_free(tempsystem);
            if (buffer) {
                free(buffer);
            }
        }
#ifndef PLATFORM_NO_X86_ASM
        if (!got_linux_cpu) {
            sprintf(linux_cpu, "%s", platform_get_x86_runtime_cpu());
            got_linux_cpu = 1;
        }
#endif
        if (!got_linux_cpu) {
            uname(&name);
            sprintf(linux_cpu, "%s", name.machine);
            got_linux_cpu = 1;
        }
    }
    return linux_cpu;
}
char *platform_get_netbsd_runtime_cpu(void)
{
    FILE *cpuinfo = NULL;
    char *buffer = NULL;
    char *loc1 = NULL;
    char *loc2 = NULL;
    char *loc3 = NULL;
    char *tempfile = NULL;
    char tempsystem[512];
    size_t size1 = 0;
    size_t size2 = 0;
    struct utsname name;
    char *cpu = NULL;
    char *machine = NULL;
    char *model = NULL;
    size_t len = 0;

    if (!got_netbsd_cpu) {
        sprintf(netbsd_cpu, "Unknown CPU");
#if !defined(__i386__) && !defined(__amd64__)
        if (sysctlbyname("hw.machine_arch", NULL, &len, NULL, 0) == 0) {
            cpu = lib_malloc(len);
            sysctlbyname("hw.machine_arch", cpu, &len, NULL, 0);

            sysctlbyname("hw.model", NULL, &len, NULL, 0);
            model = lib_malloc(len);
            sysctlbyname("hw.model", model, &len, NULL, 0);

            sysctlbyname("hw.machine", NULL, &len, NULL, 0);
            machine = lib_malloc(len);
            sysctlbyname("hw.machine", machine, &len, NULL, 0);

            sprintf(netbsd_cpu, "%s", cpu);

            /* Get specific CPU/MMU/FPU for m68k */
            if (!strcasecmp(cpu, "m68k")) {
                loc1 = strstr(model, "(");
                if (loc1) {
                    loc1++;
                    loc2 = strstr(loc1, ")");
                    if (loc2) {
                        loc2[0] = 0;
                        sprintf(netbsd_cpu, "%s", loc1);
                    }
                }
            }

            /* Get specific CPU for hpcmips */
            if (!strcasecmp(cpu, "mipsel")) {
                sprintf(netbsd_cpu, "%s", cpu);
                if (!strcasecmp(machine, "hpcmips")) {
                    loc1 = strstr(model, "(");
                    if (loc1) {
                        loc1++;
                        loc2 = strstr(loc1, ")");
                        if (loc2) {
                            loc2[0] = 0;
                            sprintf(netbsd_cpu, "%s (%s)", cpu, loc1);
                        }
                    }
                }
            }

            /* Get specific CPU for sparc or sparc64 */
            if (!strcasecmp(cpu, "sparc") || !strcasecmp(cpu, "sparc64")) {
                loc1 = strstr(model, "(");
                if (loc1) {
                    loc1++;
                    loc2 = strstr(loc1, ")");
                    if (loc2) {
                        loc2[0] = 0;
                        sprintf(netbsd_cpu, "%s (%s)", cpu, loc1);
                    }
                }
            }

            if (cpu) {
                lib_free(cpu);
            }
            if (machine) {
                lib_free(machine);
            }
            if (model) {
                lib_free(model);
            }
            got_netbsd_cpu = 1;
        } else
#endif
        {
            cpuinfo = fopen("/proc/cpuinfo", "rb");
            if (cpuinfo) {
                fclose(cpuinfo);
                cpuinfo = NULL;
                tempfile = archdep_tmpnam();
                sprintf(tempsystem, "cat /proc/cpuinfo >%s", tempfile);
                if (system(tempsystem) < 0) {
                    log_warning(LOG_ERR, "`%s' failed.", tempsystem);
                }
                cpuinfo = fopen(tempfile, "rb");
            }
            if (cpuinfo) {
                fseek(cpuinfo, 0L, SEEK_END);
                size1 = ftell(cpuinfo);
                fseek(cpuinfo, 0L, SEEK_SET);
                buffer = (char *)malloc(size1);
                size2 = fread(buffer, 1, size1, cpuinfo);
                if (size1 == size2) {
                    loc1 = strstr(buffer, "model name");
                    if (loc1) {
                        loc2 = strstr(loc1, ":");
                        if (loc2) {
                            loc2 += 2;
                            while (isspace(*loc2)) {
                                loc2++;
                            }
                            loc3 = strstr(loc2, "\n");
                            if (loc3) {
                                *loc3 = 0;
                                sprintf(netbsd_cpu, "%s", loc2);
                                got_netbsd_cpu = 1;
                            }
                        }
                    }
                }
                fclose(cpuinfo);
                unlink(tempfile);
                lib_free(tempfile);
                if (buffer) {
                    free(buffer);
                }
            }
        }
#ifndef PLATFORM_NO_X86_ASM
        if (!got_netbsd_cpu) {
            sprintf(netbsd_cpu, "%s", platform_get_x86_runtime_cpu());
            got_netbsd_cpu = 1;
        }
#endif
        if (!got_netbsd_cpu) {
            uname(&name);
            sprintf(netbsd_cpu, "%s", name.machine);
            got_netbsd_cpu = 1;
        }
    }
    return netbsd_cpu;
}
Esempio n. 6
0
/* Launch program `name' (searched via the PATH environment variable)
   passing `argv' as the parameters, wait for it to exit and return its
   exit status. If `stdout_redir' or `stderr_redir' are != NULL,
   redirect stdout or stderr to the corresponding file.  */
int archdep_spawn(const char *name, char **argv, char **pstdout_redir, const char *stderr_redir)
{
    // how to redirect stdout & stderr??
    typedef struct _CHILDINFO {  /* Define a structure for the queue data */
        USHORT usSessionID;
        USHORT usReturn;
    } CHILDINFO;

    UCHAR fqName[256] = ""; /* Result of PATH search             */
    HQUEUE hqQueue;         /* Queue handle                      */
    REQUESTDATA rdRequest;  /* Request data for the queue        */
    ULONG ulSzData;         /* Size of the queue data            */
    BYTE bPriority;         /* For the queue                     */
    PVOID pvData;           /* Pointer to the queue data         */
    STARTDATA sd;           /* Start Data for DosStartSession    */
    PID pid;                /* PID for the started child session */
    ULONG ulSession;        /* Session ID for the child session  */
    APIRET rc;              /* Return code from API's            */
    char *cmdline;
    char *stdout_redir = NULL;

    if (pstdout_redir != NULL) {
        if (*pstdout_redir == NULL) {
            *pstdout_redir = archdep_tmpnam();
        }
        stdout_redir = *pstdout_redir;
    }

    if (archdep_search_path(name, fqName, sizeof(fqName))) {
        return -1;
    }

    // Make the needed command string
    cmdline = archdep_cmdline(fqName, argv, stdout_redir, stderr_redir);

    memset(&sd, 0, sizeof(STARTDATA));
    sd.Length = sizeof(STARTDATA);
    sd.FgBg = SSF_FGBG_BACK;      /* Start session in background */
    sd.Related = SSF_RELATED_CHILD;  /* Start a child session       */
    sd.PgmName = "cmd.exe";
    sd.PgmInputs = cmdline;
    sd.PgmControl = SSF_CONTROL_INVISIBLE;
    sd.TermQ = "\\QUEUES\\VICE2\\CHILD.QUE";
    sd.InheritOpt = SSF_INHERTOPT_SHELL;

    /* Start a child process and return it's session ID.
     Wait for the session to end and get it's session ID
     from the termination queue */

    // this prevents you from closing Vice while a child is running
    if (DosRequestMutexSem(hmtxSpawn, SEM_INDEFINITE_WAIT)) {
        return 0;
    }

    if (!(rc = DosCreateQueue(&hqQueue, QUE_FIFO|QUE_CONVERT_ADDRESS, sd.TermQ))) {
        if (!(rc = DosStartSession(&sd, &ulSession, &pid)))  {
            if (!(rc = DosReadQueue(hqQueue, &rdRequest, &ulSzData, &pvData, 0, DCWW_WAIT, &bPriority, 0))) {
                rc = ((CHILDINFO*)pvData)->usReturn;

                DosFreeMem(pvData); /* Free the memory of the queue data element read */
            }
        }
        DosCloseQueue(hqQueue);
    }

    DosReleaseMutexSem(hmtxSpawn);

    lib_free(cmdline);
    return rc;
}
Esempio n. 7
0
int archdep_spawn(const char *name, char **argv, char **pstdout_redir, const char *stderr_redir)
{
#ifndef WATCOM_COMPILE
    int new_stdout, new_stderr;
    int old_stdout_mode, old_stderr_mode;
    int old_stdout, old_stderr;
    int retval;
    char *stdout_redir = NULL;

    if (pstdout_redir != NULL) {
        if (*pstdout_redir == NULL) {
            *pstdout_redir = archdep_tmpnam();
        }
        stdout_redir = *pstdout_redir;
    }

    new_stdout = new_stderr = old_stdout = old_stderr = -1;

    /* Make sure we are in binary mode.  */
    old_stdout_mode = _setmode(STDOUT_FILENO, _O_BINARY);
    old_stderr_mode = _setmode(STDERR_FILENO, _O_BINARY);

    /* Redirect stdout and stderr as requested, saving the old
       descriptors.  */
    if (stdout_redir != NULL) {
        old_stdout = _dup(STDOUT_FILENO);
        new_stdout = _open(stdout_redir, _O_WRONLY | _O_TRUNC | _O_CREAT, _S_IWRITE | _S_IREAD);
        if (new_stdout == -1) {
            log_error(LOG_DEFAULT, "open(\"%s\") failed: %s.", stdout_redir, strerror(errno));
            retval = -1;
            goto cleanup;
        }
        _dup2(new_stdout, STDOUT_FILENO);
    }
    if (stderr_redir != NULL) {
        old_stderr = _dup(STDERR_FILENO);
        new_stderr = _open(stderr_redir, _O_WRONLY | _O_TRUNC | _O_CREAT, _S_IWRITE | _S_IREAD);
        if (new_stderr == -1) {
            log_error(LOG_DEFAULT, "open(\"%s\") failed: %s.", stderr_redir, strerror(errno));
            retval = -1;
            goto cleanup;
        }
        _dup2(new_stderr, STDERR_FILENO);
    }

    /* Spawn the child process.  */
    retval = (int)_spawnvp(_P_WAIT, name, (const char **)argv);

cleanup:
    if (old_stdout >= 0) {
        _dup2(old_stdout, STDOUT_FILENO);
        _close(old_stdout);
    }
    if (old_stderr >= 0) {
        _dup2(old_stderr, STDERR_FILENO);
        _close(old_stderr);
    }
    if (old_stdout_mode >= 0) {
        _setmode(STDOUT_FILENO, old_stdout_mode);
    }
    if (old_stderr_mode >= 0) {
        _setmode(STDERR_FILENO, old_stderr_mode);
    }
    if (new_stdout >= 0) {
        _close(new_stdout);
    }
    if (new_stderr >= 0) {
        _close(new_stderr);
    }

    return retval;
#else
    return -1;
#endif
}
Esempio n. 8
0
/* If the file looks like a lynx image, try to extract it using c1541. We have
   to figure this out by reading the contsnts of the file */
static char *try_uncompress_lynx(const char *name, int write_mode)
{
    char *tmp_name;
    size_t i;
    int count;
    FILE *fd;
    char tmp[256];
    char *argv[20];
    int exit_status;

    /* can we read this file? */
    fd = fopen(name, MODE_READ);
    if (fd == NULL) {
        return NULL;
    }
    /* is this lynx -image? */
    i = fread(tmp, 1, 2, fd);
    if (i != 2 || tmp[0] != 1 || tmp[1] != 8) {
        fclose(fd);
        return NULL;
    }
    count = 0;
    while (1) {
        i = fread(tmp, 1, 1, fd);
        if (i != 1) {
            fclose(fd);
            return NULL;
        }
        if (tmp[0]) {
            count = 0;
        } else {
            count++;
        }
        if (count == 3) {
            break;
        }
    }
    i = fread(tmp, 1, 1, fd);
    if (i != 1 || tmp[0] != 13) {
        fclose(fd);
        return NULL;
    }
    count = 0;
    while (1) {
        i = fread(&tmp[count], 1, 1, fd);
        if (i != 1 || count == 254) {
            fclose(fd);
            return NULL;
        }
        if (tmp[count++] == 13) {
            break;
        }
    }
    tmp[count] = 0;
    if (!atoi(tmp)) {
        fclose(fd);
        return NULL;
    }
    /* XXX: this is not a full check, but perhaps enough? */

    fclose(fd);

    /* it is a lynx image. We cannot support write_mode */
    if (write_mode) {
        return "";
    }

    tmp_name = archdep_tmpnam();

    /* now create the image */
    argv[0] = lib_stralloc("c1541");
    argv[1] = lib_stralloc("-format");
    argv[2] = lib_stralloc("lynximage,00");
    argv[3] = lib_stralloc("x64");
    argv[4] = lib_stralloc(tmp_name);
    argv[5] = lib_stralloc("-unlynx");
    argv[6] = archdep_filename_parameter(name);
    argv[7] = NULL;

    exit_status = archdep_spawn("c1541", argv, NULL, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);
    lib_free(argv[3]);
    lib_free(argv[4]);
    lib_free(argv[5]);
    lib_free(argv[6]);

    if (exit_status) {
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }
    /* everything ok */
    return tmp_name;
}
Esempio n. 9
0
/* If this file looks like a zipcode, try to extract is using c1541. We have
   to figure this out by reading the contents of the file */
static char *try_uncompress_zipcode(const char *name, int write_mode)
{
    char *tmp_name = NULL;
    int i, count, sector, sectors = 0;
    FILE *fd;
    char tmp[256];
    char *argv[5];
    int exit_status;

    /* The 2nd char has to be '!'?  */
    util_fname_split(name, NULL, &tmp_name);
    if (tmp_name == NULL) {
        return NULL;
    }
    if (strlen(tmp_name) < 3 || tmp_name[1] != '!') {
        lib_free(tmp_name);
        return NULL;
    }
    lib_free(tmp_name);

    /* Can we read this file?  */
    fd = fopen(name, MODE_READ);
    if (fd == NULL) {
        return NULL;
    }
    /* Read first track to see if this is zipcode.  */
    fseek(fd, 4, SEEK_SET);
    for (count = 1; count < 21; count++) {
        i = zipcode_read_sector(fd, 1, &sector, tmp);
        if (i || sector < 0 || sector > 20 || (sectors & (1 << sector))) {
            fclose(fd);
            return NULL;
        }
        sectors |= 1 << sector;
    }
    fclose(fd);

    /* it is a zipcode. We cannot support write_mode */
    if (write_mode) {
        return "";
    }

    /* format image first */
    tmp_name = archdep_tmpnam();

    /* ok, now extract the zipcode */
    argv[0] = lib_stralloc(C1541_NAME);
    argv[1] = lib_stralloc("-zcreate");
    argv[2] = lib_stralloc(tmp_name);
    argv[3] = archdep_filename_parameter(name);
    argv[4] = NULL;

    exit_status = archdep_spawn(C1541_NAME, argv, NULL, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);
    lib_free(argv[3]);

    if (exit_status) {
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }
    /* everything ok */
    return tmp_name;
}
Esempio n. 10
0
static void network_server_connect_trap(WORD addr, void *data)
{
    FILE *f;
    BYTE *buf;
    size_t buf_size;
    BYTE send_size4[4];
    long i;
    event_list_state_t settings_list;

    vsync_suspend_speed_eval();

    /* Create snapshot and send it */
    snapshotfilename = archdep_tmpnam();
    if (machine_write_snapshot(snapshotfilename, 1, 1, 0) == 0) {
        f = fopen(snapshotfilename, MODE_READ);
        if (f == NULL) {
#ifdef HAS_TRANSLATION
            ui_error(translate_text(IDGS_CANNOT_LOAD_SNAPSHOT_TRANSFER));
#else
            ui_error(_("Cannot load snapshot file for transfer"));
#endif
            lib_free(snapshotfilename);
            return;
        }
        buf_size = util_file_length(f);
        buf = lib_malloc(buf_size);
        fread(buf, 1, buf_size, f);
        fclose(f);

#ifdef HAS_TRANSLATION
        ui_display_statustext(translate_text(IDGS_SENDING_SNAPSHOT_TO_CLIENT), 0);
#else
        ui_display_statustext(_("Sending snapshot to client..."), 0);
#endif
        util_int_to_le_buf4(send_size4, (int)buf_size);
        network_send_buffer(network_socket, send_size4, 4);
        i = network_send_buffer(network_socket, buf, (int)buf_size);
        lib_free(buf);
        if (i < 0) {
#ifdef HAS_TRANSLATION
            ui_error(translate_text(IDGS_CANNOT_SEND_SNAPSHOT_TO_CLIENT));
#else
            ui_error(_("Cannot send snapshot to client"));
#endif
            ui_display_statustext("", 0);
            lib_free(snapshotfilename);
            return;
        }

        network_mode = NETWORK_SERVER_CONNECTED;

        /* Send settings that need to be the same */
        event_register_event_list(&settings_list);
        resources_get_event_safe_list(&settings_list);

        buf_size = (size_t)network_create_event_buffer(&buf, &(settings_list));
        util_int_to_le_buf4(send_size4, (int)buf_size);

        network_send_buffer(network_socket, send_size4, 4);
        network_send_buffer(network_socket, buf, (int)buf_size);

        event_clear_list(&settings_list);
        lib_free(buf);

        current_send_frame = 0;
        last_received_frame = 0;

        network_test_delay();
    } else {
#ifdef HAS_TRANSLATION
        ui_error(translate_text(IDGS_CANNOT_CREATE_SNAPSHOT_FILE_S), snapshotfilename);
#else
        ui_error(_("Cannot create snapshot file %s"), snapshotfilename);
#endif
    }
    lib_free(snapshotfilename);
}