/* Find the original arg buffer, return 0 if found, else -1.
 *
 * If found, set argc to the number of arguments, argv to an array
 * of pointers to the single arguments. The array is allocated via malloc.
 *
 * If the function fails in a way that shouldn't be ignored, also set
 * a Python exception.
 *
 * The function overcomes three Py_GetArgcArgv shortcomings:
 * - some python parameters mess up with the original argv, e.g. -m
 *   (see issue #8)
 * - with Python 3, argv is a decoded copy and doesn't point to
 *   the original area.
 * - If python is embedded, the function doesn't return anything.
 */
static int
get_argc_argv(int *argc_o, char ***argv_o)
{
    int argc = 0;
    argv_t **argv_py = NULL;
    char **argv = NULL;
    char *arg0 = NULL;
    int rv = -1;

#ifndef IS_PYPY
    spt_debug("reading argc/argv from Python main");
    Py_GetArgcArgv(&argc, &argv_py);
#endif

    if (argc > 0) {
        spt_debug("found %d arguments", argc);

#ifdef IS_PY3K
        if (!(arg0 = get_encoded_arg0(argv_py[0]))) {
            spt_debug("couldn't get a copy of argv[0]");
            goto exit;
        }
#else
        if (!(argv = fix_argv(argc, (char **)argv_py))) {
            spt_debug("failed to fix argv");
            goto exit;
        }
#endif
        /* we got argv: on py2 it points to the right place in memory; on py3
         * we only got a copy of argv[0]: we will use it to look from environ
         */
    }
    else {
        spt_debug("no good news from Py_GetArgcArgv");

        /* get a copy of argv[0] from /proc, so we get back in the same
         * situation of Py3 */
        if (0 > get_args_from_proc(&argc, &arg0)) {
            spt_debug("failed to get args from proc fs");
            goto exit;
        }
    }

    /* If we don't know argv but we know the content of argv[0], we can walk
     * backwards from environ and see if we get it. */
    if (arg0 && !argv) {
        if (!(argv = find_argv_from_env(argc, arg0))) {
            spt_debug("couldn't find argv from environ");
            goto exit;
        }
    }

    /* success */
    *argc_o = argc;
    *argv_o = argv;
    argv = NULL;
    rv = 0;

exit:
    if (arg0) { free(arg0); }
    if (argv) { free(argv); }

    return rv;
}
Beispiel #2
0
/* Find the original arg buffer starting from the env position.
 *
 * Return nonzero if found.
 *
 * Required on Python 3 as Py_GetArgcArgv doesn't return pointers to the
 * original area.
 */
static int
find_argv_from_env(int *argc_o, char ***argv_o)
{
    int rv = 0;
    int argc;
    wchar_t **argv;
    char **buf = NULL;
    char *arg0 = NULL;

    /* Find the number of parameters. */
    Py_GetArgcArgv(&argc, &argv);

    buf = (char **)malloc((argc + 1) * sizeof(char *));
    buf[argc] = NULL;

    /* Walk back from environ until you find argc-1 null-terminated strings.
     * Don't look for argv[0] as it's probably not preceded by 0. */
    int i;
    char *ptr = environ[0];
    char *limit = ptr - 8192;  /* TODO: empiric limit: should use MAX_ARG */
    --ptr;
    for (i = argc - 1; i >= 1; --i) {
        if (*ptr) {
            spt_debug("zero %d not found", i);
            goto error;
        }
        --ptr;
        while (*ptr && ptr > limit) { --ptr; }
        if (ptr <= limit) {
            spt_debug("failed to found arg %d start", i);
            goto error;
        }
        buf[i] = (ptr + 1);
    }

    /* The first arg has not a zero in front. But what we have is reliable
     * enough (modulo its encoding). Check if it is exactly what found.
     *
     * The check is known to fail on OS X with locale C if there are
     * non-ascii characters in the executable path. See Python issue #9167
     */
    arg0 = get_encoded_arg0(argv[0]);
    if (!arg0) { goto error; }
    ptr -= strlen(arg0);

    if (ptr <= limit) {
        spt_debug("failed to found argv[0] start");
        goto error;
    }
    if (strcmp(ptr, arg0)) {
        spt_debug("failed to recognize argv[0]");
        goto error;
    }

    /* We have all the pieces of the jigsaw. */
    buf[0] = ptr;
    *argc_o = argc;
    *argv_o = buf;
    rv = 1;

    goto exit;

error:
    if (buf) { free(buf); }

exit:
    if (arg0) { free(arg0); }

    return rv;
}