[Python-modules-commits] [python-setproctitle] 01/13: Import python-setproctitle_1.0.orig.tar.gz
Orestis Ioannou
oorestisime-guest at moszumanska.debian.org
Sun Sep 18 12:02:44 UTC 2016
This is an automated email from the git hooks/post-receive script.
oorestisime-guest pushed a commit to branch master
in repository python-setproctitle.
commit b58a275344264d026623abeb31d209fc87cbd21e
Author: Orestis Ioannou <orestis at oioannou.com>
Date: Wed Sep 14 00:42:49 2016 +0200
Import python-setproctitle_1.0.orig.tar.gz
---
COPYRIGHT | 25 +++
HISTORY | 40 +++++
PKG-INFO | 151 ++++++++++++++++
README | 86 ++++++++++
setup.py | 96 +++++++++++
src/c.h | 38 +++++
src/setproctitle.c | 156 +++++++++++++++++
src/spt_config.h | 18 ++
src/spt_status.c | 416 +++++++++++++++++++++++++++++++++++++++++++++
src/spt_status.h | 26 +++
src/strlcpy.c | 71 ++++++++
tests/setproctitle_test.py | 162 ++++++++++++++++++
12 files changed, 1285 insertions(+)
diff --git a/COPYRIGHT b/COPYRIGHT
new file mode 100644
index 0000000..9cb3f80
--- /dev/null
+++ b/COPYRIGHT
@@ -0,0 +1,25 @@
+Copyright (c) 2009, Daniele Varrazzo <daniele.varrazzo at gmail.com>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+* The name of Daniele Varrazzo may not be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/HISTORY b/HISTORY
new file mode 100644
index 0000000..e0e8cdb
--- /dev/null
+++ b/HISTORY
@@ -0,0 +1,40 @@
+Releases history
+----------------
+
+Version 1.0
+~~~~~~~~~~~
+
+No major change since the previous version. The module has been heavily used
+in production environment without any problem reported, so it's time to declare
+it stable.
+
+
+Version 0.4
+~~~~~~~~~~~
+
+- Module works on BSD (tested on FreeBSD 7.2).
+
+- Module works on Windows. Many thanks to `Develer`_ for providing a neat `GCC
+ package for Windows with Python integration`__ that made the Windows porting
+ painless.
+
+ .. _Develer: http://www.develer.com/
+ .. __: http://www.develer.com/oss/GccWinBinaries
+
+
+Version 0.3
+~~~~~~~~~~~
+
+- Module works on Mac OS X 10.2. Reported working on OS X 10.6 too.
+
+
+Version 0.2
+~~~~~~~~~~~
+
+- Added ``prctl()`` call on Linux >= 2.6.9 to update ``/proc/self/status``.
+
+
+Version 0.1
+~~~~~~~~~~~
+
+- Initial public release.
diff --git a/PKG-INFO b/PKG-INFO
new file mode 100644
index 0000000..76b9be0
--- /dev/null
+++ b/PKG-INFO
@@ -0,0 +1,151 @@
+Metadata-Version: 1.0
+Name: setproctitle
+Version: 1.0
+Summary: Allow customization of the process title.
+Home-page: http://code.google.com/p/py-setproctitle/
+Author: Daniele Varrazzo
+Author-email: daniele.varrazzo at gmail.com
+License: BSD
+Download-URL: http://pypi.python.org/pypi/setproctitle/
+Description: A ``setproctitle`` implementation for Python
+ ============================================
+
+ :author: Daniele Varrazzo
+
+ The library allows a process to change its title (as displayed by system tools
+ such as ``ps`` and ``top``).
+
+ Changing the title is mostly useful in multi-process systems, for example
+ when a master process is forked: changing the children's title allows to
+ identify the task each process is busy with. The technique is used by
+ PostgreSQL_ and the `OpenSSH Server`_ for example.
+
+ The procedure is hardly portable across different systems. PostgreSQL provides
+ a good `multi-platform implementation`__: this module is a Python wrapper
+ around PostgreSQL code.
+
+ .. _PostgreSQL: http://www.postgresql.org
+ .. _OpenSSH Server: http://www.openssh.com/
+ .. __: http://doxygen.postgresql.org/ps__status_8c-source.html
+
+
+ Installation
+ ------------
+
+ You can use ``easy_install`` to install the module: to perform a system-wide
+ installation use::
+
+ sudo easy_install setproctitle
+
+ If you are an unprivileged user or you want to limit installation to a local
+ environment, you can use the command::
+
+ easy_install -d /target/path setproctitle
+
+ Notice that ``easy_install`` requires ``/target/path`` to be in your
+ ``PYTHONPATH``.
+
+
+ Module content
+ --------------
+
+ The module exports the following functions:
+
+ ``setproctitle(title)``
+ Set *title* as the title for the current process.
+
+
+ ``getproctitle()``
+ Return the current process title.
+
+
+ Module status
+ -------------
+
+ The module can be currently compiled and effectively used on the following
+ platforms:
+
+ - GNU/Linux
+ - BSD
+ - MacOS X
+ - Windows
+
+ Notice that on Windows there is no way to change the process string:
+ what the module does is to create a Named Object whose value can be read
+ in tools such as Process Explorer.
+
+ The module can probably work on HP-UX, but I haven't found any to test with.
+ It is unlikely that it can work on Solaris instead.
+
+
+ Other known implementations and discussions
+ -------------------------------------------
+
+ - `procname`_: a module exposing the same functionality, but less portable
+ and not well packaged.
+ - `Issue 5672`_: where the introduction of such functionality into the stdlib
+ is being discussed.
+
+ .. _procname: http://code.google.com/p/procname/
+ .. _Issue 5672: http://bugs.python.org/issue5672
+
+
+ ..
+ vim: set filetype=rst:
+
+
+ Releases history
+ ----------------
+
+ Version 1.0
+ ~~~~~~~~~~~
+
+ No major change since the previous version. The module has been heavily used
+ in production environment without any problem reported, so it's time to declare
+ it stable.
+
+
+ Version 0.4
+ ~~~~~~~~~~~
+
+ - Module works on BSD (tested on FreeBSD 7.2).
+
+ - Module works on Windows. Many thanks to `Develer`_ for providing a neat `GCC
+ package for Windows with Python integration`__ that made the Windows porting
+ painless.
+
+ .. _Develer: http://www.develer.com/
+ .. __: http://www.develer.com/oss/GccWinBinaries
+
+
+ Version 0.3
+ ~~~~~~~~~~~
+
+ - Module works on Mac OS X 10.2. Reported working on OS X 10.6 too.
+
+
+ Version 0.2
+ ~~~~~~~~~~~
+
+ - Added ``prctl()`` call on Linux >= 2.6.9 to update ``/proc/self/status``.
+
+
+ Version 0.1
+ ~~~~~~~~~~~
+
+ - Initial public release.
+
+Platform: GNU/Linux
+Platform: BSD
+Platform: MacOS X
+Platform: Windows
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: BSD License
+Classifier: Programming Language :: C
+Classifier: Programming Language :: Python
+Classifier: Operating System :: POSIX :: Linux
+Classifier: Operating System :: POSIX :: BSD
+Classifier: Operating System :: MacOS :: MacOS X
+Classifier: Operating System :: Microsoft :: Windows
+Classifier: Topic :: Software Development
diff --git a/README b/README
new file mode 100644
index 0000000..9fe63ca
--- /dev/null
+++ b/README
@@ -0,0 +1,86 @@
+A ``setproctitle`` implementation for Python
+============================================
+
+:author: Daniele Varrazzo
+
+The library allows a process to change its title (as displayed by system tools
+such as ``ps`` and ``top``).
+
+Changing the title is mostly useful in multi-process systems, for example
+when a master process is forked: changing the children's title allows to
+identify the task each process is busy with. The technique is used by
+PostgreSQL_ and the `OpenSSH Server`_ for example.
+
+The procedure is hardly portable across different systems. PostgreSQL provides
+a good `multi-platform implementation`__: this module is a Python wrapper
+around PostgreSQL code.
+
+.. _PostgreSQL: http://www.postgresql.org
+.. _OpenSSH Server: http://www.openssh.com/
+.. __: http://doxygen.postgresql.org/ps__status_8c-source.html
+
+
+Installation
+------------
+
+You can use ``easy_install`` to install the module: to perform a system-wide
+installation use::
+
+ sudo easy_install setproctitle
+
+If you are an unprivileged user or you want to limit installation to a local
+environment, you can use the command::
+
+ easy_install -d /target/path setproctitle
+
+Notice that ``easy_install`` requires ``/target/path`` to be in your
+``PYTHONPATH``.
+
+
+Module content
+--------------
+
+The module exports the following functions:
+
+``setproctitle(title)``
+ Set *title* as the title for the current process.
+
+
+``getproctitle()``
+ Return the current process title.
+
+
+Module status
+-------------
+
+The module can be currently compiled and effectively used on the following
+platforms:
+
+- GNU/Linux
+- BSD
+- MacOS X
+- Windows
+
+Notice that on Windows there is no way to change the process string:
+what the module does is to create a Named Object whose value can be read
+in tools such as Process Explorer.
+
+The module can probably work on HP-UX, but I haven't found any to test with.
+It is unlikely that it can work on Solaris instead.
+
+
+Other known implementations and discussions
+-------------------------------------------
+
+- `procname`_: a module exposing the same functionality, but less portable
+ and not well packaged.
+- `Issue 5672`_: where the introduction of such functionality into the stdlib
+ is being discussed.
+
+.. _procname: http://code.google.com/p/procname/
+.. _Issue 5672: http://bugs.python.org/issue5672
+
+
+..
+ vim: set filetype=rst:
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..0e418d6
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,96 @@
+#!/usr/bin/env python
+"""
+setproctitle setup script.
+
+Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+"""
+
+VERSION = '1.0'
+
+import os
+import re
+import sys
+from distutils.core import setup, Extension
+
+define_macros={}
+
+define_macros['SPT_VERSION'] = VERSION
+
+if sys.platform == 'linux2':
+ try:
+ linux_version = map(int,
+ re.search("[.0-9]+", os.popen("uname -r").read())
+ .group().split(".")[:3])
+ except:
+ pass
+ else:
+ if linux_version >= [2, 6, 9]:
+ define_macros['HAVE_SYS_PRCTL_H'] = 1
+
+elif sys.platform == 'darwin':
+ # __darwin__ symbol is not defined; __APPLE__ is instead.
+ define_macros['__darwin__'] = 1
+
+elif 'bsd' in sys.platform: # OMG, how many of them are?
+ # Old BSD versions don't have setproctitle
+ # TODO: not tested on an "old BSD"
+ if 0 == os.spawnlp(os.P_WAIT, 'grep',
+ 'grep', '-q', 'setproctitle', '/usr/include/unistd.h'):
+ define_macros['HAVE_SETPROCTITLE'] = 1
+ else:
+ define_macros['HAVE_PS_STRING'] = 1
+
+# NOTE: the library may work on HP-UX using pstat
+# thus setting define_macros['HAVE_SYS_PSTAT_H']
+# see http://www.noc.utoronto.ca/~mikep/unix/HPTRICKS
+# But I have none handy to test with.
+
+mod_spt = Extension('setproctitle',
+ define_macros=define_macros.items(),
+ sources = [
+ 'src/setproctitle.c',
+ 'src/spt_status.c',
+ 'src/strlcpy.c', # TODO: not needed on some platform
+ ])
+
+# patch distutils if it can't cope with the "classifiers" or
+# "download_url" keywords
+if sys.version < '2.2.3':
+ from distutils.dist import DistributionMetadata
+ DistributionMetadata.classifiers = None
+ DistributionMetadata.download_url = None
+
+# Try to include the long description in the setup
+kwargs = {}
+try:
+ kwargs['long_description'] = (
+ open('README').read()
+ + '\n'
+ +open('HISTORY').read())
+except:
+ pass
+
+setup(
+ name = 'setproctitle',
+ description = 'Allow customization of the process title.',
+ version = VERSION,
+ author = 'Daniele Varrazzo',
+ author_email = 'daniele.varrazzo at gmail.com',
+ url = 'http://code.google.com/p/py-setproctitle/',
+ download_url = 'http://pypi.python.org/pypi/setproctitle/',
+ license = 'BSD',
+ platforms = ['GNU/Linux', 'BSD', 'MacOS X', 'Windows'],
+ classifiers = filter(None, map(str.strip, """
+ Development Status :: 5 - Production/Stable
+ Intended Audience :: Developers
+ License :: OSI Approved :: BSD License
+ Programming Language :: C
+ Programming Language :: Python
+ Operating System :: POSIX :: Linux
+ Operating System :: POSIX :: BSD
+ Operating System :: MacOS :: MacOS X
+ Operating System :: Microsoft :: Windows
+ Topic :: Software Development
+ """.splitlines())),
+ ext_modules = [mod_spt],
+ **kwargs)
diff --git a/src/c.h b/src/c.h
new file mode 100644
index 0000000..f40cdab
--- /dev/null
+++ b/src/c.h
@@ -0,0 +1,38 @@
+/*-------------------------------------------------------------------------
+ *
+ * c.h
+ * A few fundamental C definitions.
+ *
+ * Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef C_H
+#define C_H
+
+#ifndef __cplusplus
+
+#ifndef bool
+typedef char bool;
+#endif
+
+#ifndef true
+#define true ((bool) 1)
+#endif
+
+#ifndef false
+#define false ((bool) 0)
+#endif
+#endif /* not C++ */
+
+#include <stddef.h>
+
+#if !HAVE_DECL_STRLCPY
+extern size_t strlcpy(char *dst, const char *src, size_t siz);
+#endif
+
+#ifdef WIN32
+#include <Windows.h>
+#endif
+
+#endif /* C_H */
diff --git a/src/setproctitle.c b/src/setproctitle.c
new file mode 100644
index 0000000..6d580a0
--- /dev/null
+++ b/src/setproctitle.c
@@ -0,0 +1,156 @@
+/*-------------------------------------------------------------------------
+ *
+ * setproctitle.c
+ * Python extension module to update and read the process title.
+ *
+ * Copyright (c) 2009 Daniele Varrazzo <daniele.varrazzo at gmail.com>
+ *
+ * The module allows Python code to access the functions get_ps_display()
+ * and set_ps_display(). The process title initialization (functions
+ * save_ps_display_args() and init_ps_display()) are called at module
+ * initialization.
+ *-------------------------------------------------------------------------
+ */
+
+#include "Python.h"
+#include "spt_status.h"
+
+#ifndef SPT_VERSION
+#define SPT_VERSION unknown
+#endif
+
+/* defined in Modules/main.c but not publically declared */
+void Py_GetArgcArgv(int *argc, char ***argv);
+
+/* macro trick to stringify a macro expansion */
+#define xstr(s) str(s)
+#define str(s) #s
+
+/* ----------------------------------------------------- */
+
+static PyObject *spt_version;
+
+static char spt_setproctitle__doc__[] =
+"Change the process title."
+;
+
+static PyObject *
+spt_setproctitle(PyObject *self /* Not used */, PyObject *args)
+{
+ const char *title;
+
+ if (!PyArg_ParseTuple(args, "s", &title))
+ return NULL;
+
+ set_ps_display(title, true);
+
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static char spt_getproctitle__doc__[] =
+"Get the current process title."
+;
+
+static PyObject *
+spt_getproctitle(PyObject *self /* Not used */, PyObject *args)
+{
+
+ if (!PyArg_ParseTuple(args, ""))
+ return NULL;
+
+ int tlen;
+ const char *title;
+ title = get_ps_display(&tlen);
+
+ return Py_BuildValue("s#", title, tlen);
+}
+
+/* List of methods defined in the module */
+
+static struct PyMethodDef spt_methods[] = {
+ {"setproctitle", (PyCFunction)spt_setproctitle, METH_VARARGS, spt_setproctitle__doc__},
+ {"getproctitle", (PyCFunction)spt_getproctitle, METH_VARARGS, spt_getproctitle__doc__},
+
+ {NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
+};
+
+
+/* return a concatenated version of a strings vector
+ *
+ * Return newly allocated heap space: clean it up with free()
+ */
+static char *
+join_argv(int argc, char **argv)
+{
+ /* Calculate the final string length */
+ int i;
+ size_t len = 0;
+ for (i = 0; i < argc; i++) {
+ len += strlen(argv[i]) + 1;
+ }
+
+ char *buf = (char *)malloc(len);
+
+ /* Copy the strings in the buffer joining with spaces */
+ char *dest = buf;
+ char *src;
+ for (i = 0; i < argc; i++) {
+ src = argv[i];
+ while (*src) {
+ *dest++ = *src++;
+ }
+ *dest++ = ' ';
+ }
+ *--dest = '\x00';
+
+ return buf;
+}
+
+
+/* Initialization function for the module (*must* be called initsetproctitle) */
+
+static char setproctitle_module_documentation[] =
+"Allow customization of the process title."
+;
+
+void
+initsetproctitle(void)
+{
+ PyObject *m, *d;
+
+ /* Create the module and add the functions */
+ m = Py_InitModule4("setproctitle", spt_methods,
+ setproctitle_module_documentation,
+ (PyObject*)NULL,PYTHON_API_VERSION);
+
+ /* 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);
+ 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");
+}
+
diff --git a/src/spt_config.h b/src/spt_config.h
new file mode 100644
index 0000000..ca71b72
--- /dev/null
+++ b/src/spt_config.h
@@ -0,0 +1,18 @@
+/* Stub file: should be created in configuration phase */
+/* This configuration was taken from an Ubuntu i386 installation. */
+
+/* Define to 1 if you have the `setproctitle' function. */
+/* #undef HAVE_SETPROCTITLE */
+
+/* Define to 1 if the PS_STRINGS thing exists. */
+/* #undef HAVE_PS_STRINGS */
+
+/* Define to 1 if you have the <sys/pstat.h> header file. */
+/* #undef HAVE_SYS_PSTAT_H */
+
+/* Define to 1 if you have the <sys/prctl.h> header file. */
+/* #undef HAVE_SYS_PRCTL_H */
+
+/* Define to 1 if you have the declaration of `strlcpy', and to 0 if you
+ don't. */
+#define HAVE_DECL_STRLCPY 0
diff --git a/src/spt_status.c b/src/spt_status.c
new file mode 100644
index 0000000..ec7d1c9
--- /dev/null
+++ b/src/spt_status.c
@@ -0,0 +1,416 @@
+/*--------------------------------------------------------------------
+ * spt_status.c
+ *
+ * Routines to support changing the ps display of a process.
+ * Mechanism differs wildly across platforms.
+ *
+ * Copyright (c) 2000-2009, PostgreSQL Global Development Group
+ * various details abducted from various places
+ *
+ * This file was taken from PostgreSQL. The PostgreSQL copyright terms follow.
+ *--------------------------------------------------------------------
+ */
+
+/*
+ * PostgreSQL Database Management System
+ * (formerly known as Postgres, then as Postgres95)
+ *
+ * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
+ *
+ * Portions Copyright (c) 1994, The Regents of the University of California
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without a written agreement
+ * is hereby granted, provided that the above copyright notice and this
+ * paragraph and the following two paragraphs appear in all copies.
+ *
+ * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
+ * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
+ * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
+ * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
+ * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
+ * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+ */
+
+#include "spt_config.h"
+
+#include <unistd.h>
+#ifdef HAVE_SYS_PSTAT_H
+#include <sys/pstat.h> /* for HP-UX */
+#endif
+#ifdef HAVE_PS_STRINGS
+#include <machine/vmparam.h> /* for old BSD */
+#include <sys/exec.h>
+#endif
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h> /* for Linux >= 2.6.9 */
+#include <linux/prctl.h>
+#endif
+#if defined(__darwin__)
+#include <crt_externs.h>
+#endif
+
+#include "spt_status.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Darwin doesn't export environ */
+#if defined(__darwin__)
+#define environ (*_NSGetEnviron())
+#else
+extern char **environ;
+#endif
+
+bool update_process_title = true;
+
+/*
+ * Alternative ways of updating ps display:
+ *
+ * PS_USE_SETPROCTITLE
+ * use the function setproctitle(const char *, ...)
+ * (newer BSD systems)
+ * PS_USE_PSTAT
+ * use the pstat(PSTAT_SETCMD, )
+ * (HPUX)
+ * PS_USE_PS_STRINGS
+ * assign PS_STRINGS->ps_argvstr = "string"
+ * (some BSD systems)
+ * PS_USE_CHANGE_ARGV
+ * assign argv[0] = "string"
+ * (some other BSD systems)
+ * PS_USE_PRCTL
+ * use prctl(PR_SET_NAME, )
+ * (Linux >= 2.6.9)
+ * PS_USE_CLOBBER_ARGV
+ * write over the argv and environment area
+ * (most SysV-like systems)
+ * PS_USE_WIN32
+ * push the string out as the name of a Windows event
+ * PS_USE_NONE
+ * don't update ps display
+ * (This is the default, as it is safest.)
+ */
+#if defined(HAVE_SETPROCTITLE)
+#define PS_USE_SETPROCTITLE
+#elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD)
+#define PS_USE_PSTAT
+#elif defined(HAVE_PS_STRINGS)
+#define PS_USE_PS_STRINGS
+#elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__)
+#define PS_USE_CHANGE_ARGV
+#elif defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__)
+#define PS_USE_CLOBBER_ARGV
+#elif defined(WIN32)
+#define PS_USE_WIN32
+#else
+#define PS_USE_NONE
+#endif
+
+/* we use this strategy together with another one (probably PS_USE_CLOBBER_ARGV) */
+#if defined(HAVE_SYS_PRCTL_H) && !defined(PS_USE_NONE)
+#define PS_USE_PRCTL
+#endif
+
+/* Different systems want the buffer padded differently */
+#if defined(_AIX) || defined(__linux__) || defined(__svr4__) || defined(__darwin__)
+#define PS_PADDING '\0'
+#else
+#define PS_PADDING ' '
+#endif
+
+
+#ifndef PS_USE_CLOBBER_ARGV
+/* all but one options need a buffer to write their ps line in */
+#define PS_BUFFER_SIZE 256
+static char ps_buffer[PS_BUFFER_SIZE];
+static const size_t ps_buffer_size = PS_BUFFER_SIZE;
+#else /* PS_USE_CLOBBER_ARGV */
+static char *ps_buffer; /* will point to argv area */
+static size_t ps_buffer_size; /* space determined at run time */
+static size_t last_status_len; /* use to minimize length of clobber */
+#endif /* PS_USE_CLOBBER_ARGV */
+
+static size_t ps_buffer_fixed_size; /* size of the constant prefix */
+
+/* save the original argv[] location here */
+static int save_argc;
+static char **save_argv;
+
+
+/*
+ * Call this early in startup to save the original argc/argv values.
+ * If needed, we make a copy of the original argv[] array to preserve it
+ * from being clobbered by subsequent ps_display actions.
+ *
+ * (The original argv[] will not be overwritten by this routine, but may be
+ * overwritten during init_ps_display. Also, the physical location of the
+ * environment strings may be moved, so this should be called before any code
+ * that might try to hang onto a getenv() result.)
+ */
+char **
+save_ps_display_args(int argc, char **argv)
+{
+ save_argc = argc;
+ save_argv = argv;
+
+#if defined(PS_USE_CLOBBER_ARGV)
+
+ /*
+ * If we're going to overwrite the argv area, count the available space.
+ * Also move the environment to make additional room.
+ */
+ {
+ char *end_of_area = NULL;
+ char **new_environ;
+ int i;
+
+ /*
+ * check for contiguous argv strings
+ */
+ for (i = 0; i < argc; i++)
+ {
+ if (i == 0 || end_of_area + 1 == argv[i])
+ end_of_area = argv[i] + strlen(argv[i]);
+ }
+
+ if (end_of_area == NULL) /* probably can't happen? */
+ {
+ ps_buffer = NULL;
+ ps_buffer_size = 0;
+ return argv;
+ }
+
+ /*
+ * check for contiguous environ strings following argv
+ */
+ for (i = 0; environ[i] != NULL; i++)
+ {
+ if (end_of_area + 1 == environ[i])
+ end_of_area = environ[i] + strlen(environ[i]);
+ }
+
+ ps_buffer = argv[0];
+ last_status_len = ps_buffer_size = end_of_area - argv[0];
+
+ /*
+ * move the environment out of the way
+ */
+ new_environ = (char **) malloc((i + 1) * sizeof(char *));
+ for (i = 0; environ[i] != NULL; i++)
+ new_environ[i] = strdup(environ[i]);
+ new_environ[i] = NULL;
+ environ = new_environ;
+ }
+#endif /* PS_USE_CLOBBER_ARGV */
+
+#if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV)
+
+ /*
+ * If we're going to change the original argv[] then make a copy for
+ * argument parsing purposes.
+ *
+ * (NB: do NOT think to remove the copying of argv[], even though
+ * postmaster.c finishes looking at argv[] long before we ever consider
+ * changing the ps display. On some platforms, getopt() keeps pointers
+ * into the argv array, and will get horribly confused when it is
+ * re-called to analyze a subprocess' argument string if the argv storage
+ * has been clobbered meanwhile. Other platforms have other dependencies
+ * on argv[].
+ */
+ {
+ char **new_argv;
+ int i;
+
+ new_argv = (char **) malloc((argc + 1) * sizeof(char *));
+ for (i = 0; i < argc; i++)
+ new_argv[i] = strdup(argv[i]);
+ new_argv[argc] = NULL;
+
+#if defined(__darwin__)
+
+ /*
+ * Darwin (and perhaps other NeXT-derived platforms?) has a static
+ * copy of the argv pointer, which we may fix like so:
+ */
+ *_NSGetArgv() = new_argv;
+#endif
+
+ argv = new_argv;
+ }
+#endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */
+
+ return argv;
+}
+
+/*
+ * Call this once during subprocess startup to set the identification
+ * values. At this point, the original argv[] array may be overwritten.
+ */
+void
+init_ps_display(const char *initial_str)
+{
+
+#ifndef PS_USE_NONE
+ /* no ps display if you didn't call save_ps_display_args() */
+ if (!save_argv)
+ return;
+#ifdef PS_USE_CLOBBER_ARGV
+ /* If ps_buffer is a pointer, it might still be null */
+ if (!ps_buffer)
+ return;
+#endif
+
+ /*
+ * Overwrite argv[] to point at appropriate space, if needed
+ */
+
+#ifdef PS_USE_CHANGE_ARGV
+ save_argv[0] = ps_buffer;
+ save_argv[1] = NULL;
+#endif /* PS_USE_CHANGE_ARGV */
+
+#ifdef PS_USE_CLOBBER_ARGV
+ {
+ int i;
+
+ /* make extra argv slots point at end_of_area (a NUL) */
+ for (i = 1; i < save_argc; i++)
+ save_argv[i] = ps_buffer + ps_buffer_size;
+ }
+#endif /* PS_USE_CLOBBER_ARGV */
+
+ /*
+ * Make fixed prefix of ps display.
+ */
+
+ ps_buffer[0] = '\0';
+
+ ps_buffer_fixed_size = strlen(ps_buffer);
+
+ set_ps_display(initial_str, true);
+#endif /* not PS_USE_NONE */
+}
+
+
+
+/*
+ * Call this to update the ps status display to a fixed prefix plus an
+ * indication of what you're currently doing passed in the argument.
+ */
+void
+set_ps_display(const char *activity, bool force)
+{
+
+ if (!force && !update_process_title)
+ return;
+
+#ifndef PS_USE_NONE
+
+#ifdef PS_USE_CLOBBER_ARGV
... 377 lines suppressed ...
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-setproctitle.git
More information about the Python-modules-commits
mailing list