@@ -61,10 +61,16 @@ module syslog
61
61
62
62
#include "clinic/syslogmodule.c.h"
63
63
64
- /* only one instance, only one syslog, so globals should be ok */
65
- static PyObject * S_ident_o = NULL ; /* identifier, held by openlog() */
64
+ /* only one instance, only one syslog, so globals should be ok,
65
+ * these fields are writable from the main interpreter only. */
66
+ static PyObject * S_ident_o = NULL ; // identifier, held by openlog()
66
67
static char S_log_open = 0 ;
67
68
69
+ static inline int
70
+ is_main_interpreter (void )
71
+ {
72
+ return (PyInterpreterState_Get () == PyInterpreterState_Main ());
73
+ }
68
74
69
75
static PyObject *
70
76
syslog_get_argv (void )
@@ -135,6 +141,13 @@ syslog_openlog_impl(PyObject *module, PyObject *ident, long logopt,
135
141
long facility )
136
142
/*[clinic end generated code: output=5476c12829b6eb75 input=8a987a96a586eee7]*/
137
143
{
144
+ // Since the sys.openlog changes the process level state of syslog library,
145
+ // this operation is only allowed for the main interpreter.
146
+ if (!is_main_interpreter ()) {
147
+ PyErr_SetString (PyExc_RuntimeError , "subinterpreter can't use syslog.openlog()" );
148
+ return NULL ;
149
+ }
150
+
138
151
const char * ident_str = NULL ;
139
152
140
153
if (ident ) {
@@ -195,6 +208,11 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
195
208
196
209
/* if log is not opened, open it now */
197
210
if (!S_log_open ) {
211
+ if (!is_main_interpreter ()) {
212
+ PyErr_SetString (PyExc_RuntimeError , "subinterpreter can't use syslog.syslog() "
213
+ "until the syslog is opened by the main interpreter" );
214
+ return NULL ;
215
+ }
198
216
PyObject * openlog_ret = syslog_openlog_impl (module , NULL , 0 , LOG_USER );
199
217
if (openlog_ret == NULL ) {
200
218
return NULL ;
@@ -229,6 +247,13 @@ static PyObject *
229
247
syslog_closelog_impl (PyObject * module )
230
248
/*[clinic end generated code: output=97890a80a24b1b84 input=fb77a54d447acf07]*/
231
249
{
250
+ // Since the sys.closelog changes the process level state of syslog library,
251
+ // this operation is only allowed for the main interpreter.
252
+ if (!is_main_interpreter ()) {
253
+ PyErr_SetString (PyExc_RuntimeError , "sunbinterpreter can't use syslog.closelog()" );
254
+ return NULL ;
255
+ }
256
+
232
257
if (PySys_Audit ("syslog.closelog" , NULL ) < 0 ) {
233
258
return NULL ;
234
259
}
0 commit comments