Exemple #1
0
/* Find the original arg buffer, return nonzero if found.
 *
 * 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.
 *
 * The function overcomes two 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.
 */
static int
get_argc_argv(int *argc, char ***argv)
{
    int rv = false;

#ifdef IS_PY3K
    if (!(rv = find_argv_from_env(argc, argv))) {
        spt_debug("get_argc_argv failed");
    }
#else
    Py_GetArgcArgv(argc, argv);
    *argv = fix_argv(*argc, *argv);
    rv = true;
#endif

    return rv;
}
Exemple #2
0
void
initsetproctitle(void)
{
    PyObject *m, *d;

    /* Create the module and add the functions */
    m = Py_InitModule3("setproctitle", spt_methods,
        setproctitle_module_documentation);

    /* Add version string to the module*/
    d = PyModule_GetDict(m);
    spt_version = PyString_FromString(xstr(SPT_VERSION));
    PyDict_SetItemString(d, "__version__", spt_version);

    /* Initialize the process title */
#ifndef WIN32
    int argc;
    char **argv;
    Py_GetArgcArgv(&argc, &argv);
    argv = fix_argv(argc, argv);
    save_ps_display_args(argc, argv);

    /* Set up the first title to fully initialize the code */
    char *init_title = join_argv(argc, argv);
    init_ps_display(init_title);
    free(init_title);
#else
    /* On Windows save_ps_display_args is a no-op
     * This is a good news, because Py_GetArgcArgv seems not usable.
     */
    LPTSTR init_title = GetCommandLine();
    init_ps_display(init_title);
#endif


    /* Check for errors */
    if (PyErr_Occurred())
        Py_FatalError("can't initialize module setproctitle");
}
/* 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;
}