/
setproctitle.c
145 lines (109 loc) · 3.06 KB
/
setproctitle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*-------------------------------------------------------------------------
*
* setproctitle.c
* Python extension module to update and read the process title.
*
* Copyright (c) 2009-2016 Daniele Varrazzo <daniele.varrazzo@gmail.com>
*
* The module allows Python code to access the functions get_ps_display()
* and set_ps_display().
*
*-------------------------------------------------------------------------
*/
#include "spt.h"
#include "spt_setup.h"
#include "spt_status.h"
#ifndef SPT_VERSION
#define SPT_VERSION unknown
#endif
/* 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__[] =
"setproctitle(title) -- Change the process title."
;
static PyObject *
spt_setproctitle(PyObject *self, PyObject *args, PyObject *kwargs)
{
const char *title = NULL;
static char *kwlist[] = {"title", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &title))
return NULL;
set_ps_display(title, true);
Py_INCREF(Py_None);
return Py_None;
}
static char spt_getproctitle__doc__[] =
"getproctitle() -- Get the current process title."
;
static PyObject *
spt_getproctitle(PyObject *self, PyObject *args)
{
size_t tlen;
const char *title;
title = get_ps_display(&tlen);
return Py_BuildValue("s#", title, (int)tlen);
}
/* List of methods defined in the module */
static struct PyMethodDef spt_methods[] = {
{"setproctitle",
(PyCFunction)spt_setproctitle,
METH_VARARGS|METH_KEYWORDS,
spt_setproctitle__doc__},
{"getproctitle",
(PyCFunction)spt_getproctitle,
METH_NOARGS,
spt_getproctitle__doc__},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initsetproctitle) */
static char setproctitle_module_documentation[] =
"Allow customization of the process title."
;
#ifdef IS_PY3K
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"setproctitle",
setproctitle_module_documentation,
-1,
spt_methods,
NULL,
NULL,
NULL,
NULL
};
#endif
PyMODINIT_FUNC
INIT_MODULE(setproctitle)(void)
{
PyObject *m, *d;
spt_debug("module init");
/* Create the module and add the functions */
#ifdef IS_PY3K
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("setproctitle", spt_methods,
setproctitle_module_documentation);
#endif
if (m == NULL) { goto exit; }
/* Add version string to the module*/
d = PyModule_GetDict(m);
spt_version = Py_BuildValue("s", xstr(SPT_VERSION));
PyDict_SetItemString(d, "__version__", spt_version);
/* Initialize the process title */
if (0 > spt_setup()) {
spt_debug("failed to initialize module setproctitle");
/* Check for errors */
if (PyErr_Occurred()) {
spt_debug("an exception is set: import will fail");
}
}
exit:
#ifdef IS_PY3K
return m;
#else
return;
#endif
}