File: python-startup.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (438 lines) | stat: -rw-r--r-- 13,909 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (c) 2015 Balabit
 * Copyright (c) 2015 Balazs Scheidler <balazs.scheidler@balabit.com>
 * Copyright (c) 2023 Balazs Scheidler <balazs.scheidler@axoflow.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "python-startup.h"
#include "python-dest.h"
#include "python-source.h"
#include "python-fetcher.h"
#include "python-logparser.h"
#include "python-tf.h"
#include "python-logmsg.h"
#include "python-logtemplate.h"
#include "python-integerpointer.h"
#include "python-logger.h"
#include "python-persist.h"
#include "python-bookmark.h"
#include "python-ack-tracker.h"
#include "python-debugger.h"
#include "python-confgen.h"
#include "python-global-code-loader.h"
#include "python-types.h"
#include "python-reloc.h"

#include "reloc.h"

static gboolean interpreter_initialized = FALSE;

static gchar *
_format_python_path(void)
{
  const gchar *current_python_path = getenv("PYTHONPATH");
  GString *python_path = g_string_new("");

  g_string_printf(python_path, "%s:%s",
                  get_installation_path_for(SYSLOG_NG_PYTHON_SYSCONF_MODULE_DIR),
                  get_installation_path_for(SYSLOG_NG_PYTHON_MODULE_DIR));

  if (current_python_path)
    g_string_append_printf(python_path, ":%s", current_python_path);
  return g_string_free(python_path, FALSE);
}

static gboolean
_py_is_virtualenv_valid(const gchar *path)
{
  gchar *python_venv_binary = g_strdup_printf("%s/bin/python", path);
  gboolean result = FALSE;

  /* check if the virtualenv is populated */
  if (g_file_test(path, G_FILE_TEST_IS_DIR) &&
      g_file_test(python_venv_binary, G_FILE_TEST_IS_EXECUTABLE))
    {
      result = TRUE;
    }

  g_free(python_venv_binary);
  return result;
}

static gboolean
_py_requirements_up_to_date(const gchar *python_venv_path)
{
  const gchar *my_requirements_file = get_installation_path_for(SYSLOG_NG_PYTHON_MODULE_DIR "/requirements.txt");
  gchar *installed_requirements_file = g_strdup_printf("%s/requirements.txt", python_venv_path);
  gchar *installed_requirements = NULL;
  gsize installed_requirements_len;
  gchar *my_requirements = NULL;
  gsize my_requirements_len;
  gboolean result = FALSE;

  if (!g_file_get_contents(installed_requirements_file, &installed_requirements, &installed_requirements_len, NULL))
    goto mismatch;

  if (!g_file_get_contents(my_requirements_file, &my_requirements, &my_requirements_len, NULL))
    goto mismatch;

  if (my_requirements_len != installed_requirements_len ||
      strcmp(my_requirements, installed_requirements) != 0)
    goto mismatch;

  result = TRUE;
mismatch:
  g_free(installed_requirements);
  g_free(my_requirements);
  g_free(installed_requirements_file);
  return result;
}

static const gchar *
_get_venv_path(void)
{
  const gchar *env_virtual_env = getenv("VIRTUAL_ENV");
  if (env_virtual_env)
    {
      if (!_py_is_virtualenv_valid(env_virtual_env))
        {
          msg_error("python: environment variable VIRTUAL_ENV is set, but does not point to a valid virtualenv, Python executable not found",
                    evt_tag_str("path", env_virtual_env));
          return NULL;
        }

      msg_debug("python: using virtualenv pointed to by $VIRTUAL_ENV", evt_tag_str("path", env_virtual_env));
      return env_virtual_env;
    }

  const gchar *private_venv_path = get_installation_path_for(SYSLOG_NG_PYTHON_VENV_DIR);
  if (!_py_is_virtualenv_valid(private_venv_path))
    {
      msg_debug("python: private virtualenv is not initialized, use the `syslog-ng-update-virtualenv' "
                "script to initialize it or make sure all required Python dependencies are available in "
                "the system Python installation",
                evt_tag_str("path", private_venv_path));
      return NULL;
    }

  if (!_py_requirements_up_to_date(private_venv_path))
    {
      msg_warning("python: the current set of requirements installed in our virtualenv seems to be out of date, "
                  "use the `syslog-ng-update-virtualenv' script to upgrade Python dependencies",
                  evt_tag_str("path", private_venv_path));
      return NULL;
    }

  msg_debug("python: the virtualenv validation successful");
  return private_venv_path;
}

#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 8 || PY_MAJOR_VERSION > 3

static gboolean
_py_set_python_path(PyConfig *config)
{
  gchar *python_path = _format_python_path();
  PyStatus status = PyConfig_SetBytesString(config, &config->pythonpath_env, python_path);
  g_free(python_path);

  if (PyStatus_Exception(status))
    {
      msg_error("Error initializing Python, setting PYTHONPATH failed",
                evt_tag_str("func", status.func),
                evt_tag_str("error", status.err_msg),
                evt_tag_int("exitcode", status.exitcode));
      return FALSE;
    }

  return TRUE;
}

static gboolean
_py_set_python_home(PyConfig *config)
{
#ifdef SYSLOG_NG_PYTHON3_HOME_DIR
  if (strlen(SYSLOG_NG_PYTHON3_HOME_DIR) > 0)
    {
      const gchar *resolved_python_home = get_installation_path_for(SYSLOG_NG_PYTHON3_HOME_DIR);
      PyStatus status = PyConfig_SetBytesString(config, &config->home, resolved_python_home);
      if (PyStatus_Exception(status))
        {
          msg_error("Error initializing Python, setting PYTHONHOME failed",
                    evt_tag_str("func", status.func),
                    evt_tag_str("error", status.err_msg),
                    evt_tag_int("exitcode", status.exitcode));
          return FALSE;
        }
    }
#endif
  return TRUE;
}

static gboolean
_py_set_argv(PyConfig *config, const gchar *binary)
{
  char *argv[] = { (char *) binary };
  PyStatus status = PyConfig_SetBytesArgv(config, 1, argv);

  if (PyStatus_Exception(status))
    {
      msg_error("Error initializing Python, PyConfig_SetBytesArgv() failed",
                evt_tag_str("func", status.func),
                evt_tag_str("error", status.err_msg),
                evt_tag_int("exitcode", status.exitcode));
      return FALSE;
    }
  return TRUE;
}

static gboolean
_py_activate_venv(PyConfig *config)
{
  const gchar *python_venv_path = _get_venv_path();

  gboolean success;
  if (python_venv_path)
    {
      /* Python will detect the virtual env based on its sys.executable value,
       * which we are setting through PyConfig_SetBytesArgv().  The algorithm is
       * described in site.py (and various PEPs and documentation).
       *
       * We basically behave as if the user executed
       * ${python_venvdir}/bin/python when it started syslog-ng.
       */
      gchar *python_venv_binary = g_strdup_printf("%s/bin/python", python_venv_path);

      msg_debug("python: activating virtualenv",
                evt_tag_str("path", python_venv_path),
                evt_tag_str("executable", python_venv_binary));
      success = _py_set_argv(config, python_venv_binary);
      g_free(python_venv_binary);
    }
  else
    {
      success = _py_set_argv(config, "syslog-ng");
    }

  return success;
}

/*
 * NOTE: we can have the following embedded Python deployment:
 *
 *   1) private Python installation: we have a complete Python installation
 *      (from source) usually compiled in the same ${prefix} as we are.  In
 *      this case, PYTHONHOME needs to point to the Python prefix.
 *      syslog-ng supports this case via the PYTHON3_HOME_DIR configure
 *      option which you need to set in this case.
 *
 *      In this case we are not using any Python binaries (libpython.so) nor
 *      Python packages from the system. Everything is private.
 *
 *       - Python binaries are private, using a custom prefix
 *       - Python packages are in ${PYTHONHOME}/lib/pythonX.Y
 *       - syslog-ng Python code is in ${moduledir}/python
 *
 *   2) system Python + system installed Python packages: we are relying on
 *      the system Python installation both for binaries and packages.  This
 *      roughly match the distribution model: all Python packages are
 *      installed via system packages (to /usr/lib/pythonX.Y) OR by using pip
 *      to augment the system installation (/usr/local/lib/pythonX.Y)
 *
 *       - Python binaries are the system ones, usually /usr/bin/python and
 *         /usr/lib/libpython.so (or their versioned equivalents)
 *       - Python packages are in /usr/lib/pythonX.Y/
 *         and their /usr/local counterparts (e.g.  the standard paths for
 *         system Python)
 *       - syslog-ng Python code is in ${moduledir}/python
 *       - all imports performed by syslog-ng Python modules will be
 *         satisfied through system paths, which you can populate either by
 *         install python packages or via the installation of pip packages
 *         into /usr/local
 *
 *   3) system Python + virtualenv: in this mode, we have our own private
 *      virtualenv where you can deploy packages privately, without making
 *      them visible globally on the system.
 *
 *       - Python binaries are the system ones, usually /usr/bin/python and
 *         /usr/lib/libpython.so (or their versioned equivalents)
 *       - Python packages are in our virtualenv folder, system Python
 *         packages are either accessible or isolated (depending on the
 *         virtualenv's --system-site-packages option)
 *       - syslog-ng Python code is in ${moduledir}/python
 *
 * PYTHONPATH is popuolated as follows
 *   1. /etc/syslog-ng/python      (${sysconfdir}/python)
 *   2. /usr/lib/syslog-ng/python  (${exec_prefix}/lib/syslog-ng/python)
 *   3. $PYTHONPATH as passed by the user in the environment
 *   4. automatic Python path set by the interpreter (venv or system)
 */
static gboolean
_py_configure_virtualenv_python(PyConfig *config)
{
  if (!_py_set_python_path(config) ||
      !_py_set_python_home(config) ||
      !_py_activate_venv(config))
    return FALSE;
  return TRUE;
}

static gboolean
_py_configure_system_python(PyConfig *config)
{
  if (!_py_set_python_path(config) ||
      !_py_set_python_home(config) ||
      !_py_set_argv(config, "syslog-ng"))
    return FALSE;
  return TRUE;
}

static gboolean
_py_configure_interpreter(gboolean use_virtualenv)
{
  PyConfig config;
  PyConfig_InitPythonConfig(&config);

  /* NOTE: to pick up PYTHONPATH, work around https://github.com/python/cpython/issues/101471 */
  config.configure_c_stdio = 0;
  config.install_signal_handlers = 0;
  config.parse_argv = 0;
  config.pathconfig_warnings = 0;
  config.user_site_directory = 0;
  config.use_environment = 1;
  gboolean success = use_virtualenv ? _py_configure_virtualenv_python(&config)
                     : _py_configure_system_python(&config);
  if (success)
    {
      Py_InitializeFromConfig(&config);
      PyConfig_Clear(&config);
      return TRUE;
    }
  return FALSE;
}

#else

/* Python older than v3.8 lacks the PyConfig based API */

static void
_py_setup_python_home(void)
{
#ifdef SYSLOG_NG_PYTHON3_HOME_DIR
  if (strlen(SYSLOG_NG_PYTHON3_HOME_DIR) > 0)
    {
      const gchar *resolved_python_home = get_installation_path_for(SYSLOG_NG_PYTHON3_HOME_DIR);
      Py_SetPythonHome(Py_DecodeLocale(resolved_python_home, NULL));
    }
#endif
}

static void
_py_set_python_path(void)
{
  gchar *python_path = _format_python_path();
  setenv("PYTHONPATH", python_path, 1);
  g_free(python_path);
}

static void
_py_init_argv(void)
{
  static wchar_t *argv[] = {L"syslog-ng"};
  PySys_SetArgvEx(1, argv, 0);
}

static gboolean
_py_activate_venv(void)
{
  const gchar *python_venv_path = _get_venv_path();

  if (!python_venv_path)
    return FALSE;

  gchar *python_venv_binary = g_build_path(G_DIR_SEPARATOR_S, python_venv_path, "bin", "python", NULL);
  wchar_t *python_programname = Py_DecodeLocale(python_venv_binary, NULL);

  msg_debug("python: activating virtualenv",
            evt_tag_str("path", python_venv_path),
            evt_tag_str("executable", python_venv_binary));
  g_free(python_venv_binary);

  Py_SetProgramName(python_programname);
  return TRUE;
}

static gboolean
_py_configure_interpreter(gboolean use_virtualenv)
{
  _py_setup_python_home();
  _py_set_python_path();

  if (use_virtualenv)
    _py_activate_venv();

  Py_Initialize();
  _py_init_argv();
  return TRUE;
}

#endif

static void
_py_initialize_builtin_modules(void)
{
  py_init_threads();
  py_init_types();
  py_init_confgen();

  py_log_message_global_init();
  py_log_template_global_init();
  py_integer_pointer_global_init();
  py_log_destination_global_init();
  py_log_parser_global_init();
  py_log_source_global_init();
  py_log_fetcher_global_init();
  py_persist_global_init();
  py_bookmark_global_init();
  py_ack_tracker_global_init();
  py_reloc_global_init();
  py_global_code_loader_global_init();
  py_logger_global_init();
}

gboolean
_py_init_interpreter(gboolean use_virtualenv)
{
  if (!interpreter_initialized)
    {
      python_debugger_append_inittab();

      if (!_py_configure_interpreter(use_virtualenv))
        return FALSE;

      _py_initialize_builtin_modules();

      PyEval_SaveThread();

      interpreter_initialized = TRUE;
    }
  return TRUE;
}