blob: 8bedab5c27a7855b40bf20dd0b20026b9a0fbcd7 [file] [log] [blame]
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001/* Authors: Gregory P. Smith & Jeffrey Yasskin */
2#include "Python.h"
Victor Stinner5572ba72011-05-26 14:10:08 +02003#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
4# define _GNU_SOURCE
Gregory P. Smith51ee2702010-12-13 07:59:39 +00005#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00006#include <unistd.h>
Gregory P. Smith51ee2702010-12-13 07:59:39 +00007#include <fcntl.h>
Gregory P. Smith8facece2012-01-21 14:01:08 -08008#ifdef HAVE_SYS_TYPES_H
9#include <sys/types.h>
10#endif
Gregory P. Smith4842efc2012-01-21 21:01:24 -080011#if defined(HAVE_SYS_STAT_H) && defined(__FreeBSD__)
12#include <sys/stat.h>
13#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080014#ifdef HAVE_SYS_SYSCALL_H
15#include <sys/syscall.h>
16#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -070017#if defined(HAVE_SYS_RESOURCE_H)
18#include <sys/resource.h>
19#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080020#ifdef HAVE_DIRENT_H
21#include <dirent.h>
22#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000023
Gregory P. Smithefeb9da2014-04-14 13:31:21 -070024#if defined(__ANDROID__) && !defined(SYS_getdents64)
25/* Android doesn't expose syscalls, add the definition manually. */
26# include <sys/linux-syscalls.h>
27# define SYS_getdents64 __NR_getdents64
28#endif
29
Gregory P. Smithe3f78482012-01-21 15:16:17 -080030#if defined(sun)
31/* readdir64 is used to work around Solaris 9 bug 6395699. */
32# define readdir readdir64
33# define dirent dirent64
34# if !defined(HAVE_DIRFD)
Gregory P. Smith8facece2012-01-21 14:01:08 -080035/* Some versions of Solaris lack dirfd(). */
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -080036# define dirfd(dirp) ((dirp)->dd_fd)
Gregory P. Smithe3f78482012-01-21 15:16:17 -080037# define HAVE_DIRFD
Gregory P. Smithe3f78482012-01-21 15:16:17 -080038# endif
Gregory P. Smith8facece2012-01-21 14:01:08 -080039#endif
40
Gregory P. Smith4842efc2012-01-21 21:01:24 -080041#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
42# define FD_DIR "/dev/fd"
43#else
44# define FD_DIR "/proc/self/fd"
45#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000046
Victor Stinnerdaf45552013-08-28 00:53:59 +020047#define POSIX_CALL(call) do { if ((call) == -1) goto error; } while (0)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000048
49
Martin Panterafdd5132015-11-30 02:21:41 +000050/* If gc was disabled, call gc.enable(). Return 0 on success. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050051static int
Martin Panterafdd5132015-11-30 02:21:41 +000052_enable_gc(int need_to_reenable_gc, PyObject *gc_module)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000053{
54 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020055 _Py_IDENTIFIER(enable);
Martin Panterafdd5132015-11-30 02:21:41 +000056 PyObject *exctype, *val, *tb;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020057
Martin Panterafdd5132015-11-30 02:21:41 +000058 if (need_to_reenable_gc) {
59 PyErr_Fetch(&exctype, &val, &tb);
60 result = _PyObject_CallMethodId(gc_module, &PyId_enable, NULL);
61 if (exctype != NULL) {
62 PyErr_Restore(exctype, val, tb);
63 }
64 if (result == NULL) {
65 return 1;
66 }
67 Py_DECREF(result);
68 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +000069 return 0;
70}
71
72
Gregory P. Smith8facece2012-01-21 14:01:08 -080073/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
Benjamin Peterson91eef982012-01-22 20:04:46 -050074static int
75_pos_int_from_ascii(char *name)
Gregory P. Smith8facece2012-01-21 14:01:08 -080076{
77 int num = 0;
78 while (*name >= '0' && *name <= '9') {
79 num = num * 10 + (*name - '0');
80 ++name;
81 }
82 if (*name)
83 return -1; /* Non digit found, not a number. */
84 return num;
85}
86
87
Gregory P. Smith4842efc2012-01-21 21:01:24 -080088#if defined(__FreeBSD__)
89/* When /dev/fd isn't mounted it is often a static directory populated
90 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
91 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
92 * mounted) and do not have fdescfs for /dev/fd. MacOS X has a devfs
93 * that properly supports /dev/fd.
94 */
Benjamin Peterson91eef982012-01-22 20:04:46 -050095static int
Ross Lagerwall7f4fdb22012-03-07 20:06:33 +020096_is_fdescfs_mounted_on_dev_fd(void)
Gregory P. Smith4842efc2012-01-21 21:01:24 -080097{
98 struct stat dev_stat;
99 struct stat dev_fd_stat;
100 if (stat("/dev", &dev_stat) != 0)
101 return 0;
102 if (stat(FD_DIR, &dev_fd_stat) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +0200103 return 0;
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800104 if (dev_stat.st_dev == dev_fd_stat.st_dev)
105 return 0; /* / == /dev == /dev/fd means it is static. #fail */
106 return 1;
107}
108#endif
109
110
Gregory P. Smith8facece2012-01-21 14:01:08 -0800111/* Returns 1 if there is a problem with fd_sequence, 0 otherwise. */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500112static int
113_sanity_check_python_fd_sequence(PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800114{
115 Py_ssize_t seq_idx, seq_len = PySequence_Length(fd_sequence);
116 long prev_fd = -1;
117 for (seq_idx = 0; seq_idx < seq_len; ++seq_idx) {
118 PyObject* py_fd = PySequence_Fast_GET_ITEM(fd_sequence, seq_idx);
119 long iter_fd = PyLong_AsLong(py_fd);
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800120 if (iter_fd < 0 || iter_fd <= prev_fd || iter_fd > INT_MAX) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800121 /* Negative, overflow, not a Long, unsorted, too big for a fd. */
122 return 1;
123 }
Gregory P. Smithd0a5b1c2015-11-15 21:15:26 -0800124 prev_fd = iter_fd;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800125 }
126 return 0;
127}
128
129
130/* Is fd found in the sorted Python Sequence? */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500131static int
132_is_fd_in_sorted_fd_sequence(int fd, PyObject *fd_sequence)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800133{
134 /* Binary search. */
135 Py_ssize_t search_min = 0;
136 Py_ssize_t search_max = PySequence_Length(fd_sequence) - 1;
137 if (search_max < 0)
138 return 0;
139 do {
140 long middle = (search_min + search_max) / 2;
141 long middle_fd = PyLong_AsLong(
142 PySequence_Fast_GET_ITEM(fd_sequence, middle));
143 if (fd == middle_fd)
144 return 1;
145 if (fd > middle_fd)
146 search_min = middle + 1;
147 else
148 search_max = middle - 1;
149 } while (search_min <= search_max);
150 return 0;
151}
152
Victor Stinnerdaf45552013-08-28 00:53:59 +0200153static int
154make_inheritable(PyObject *py_fds_to_keep, int errpipe_write)
155{
156 Py_ssize_t i, len;
157
158 len = PySequence_Length(py_fds_to_keep);
159 for (i = 0; i < len; ++i) {
160 PyObject* fdobj = PySequence_Fast_GET_ITEM(py_fds_to_keep, i);
161 long fd = PyLong_AsLong(fdobj);
162 assert(!PyErr_Occurred());
163 assert(0 <= fd && fd <= INT_MAX);
164 if (fd == errpipe_write) {
165 /* errpipe_write is part of py_fds_to_keep. It must be closed at
166 exec(), but kept open in the child process until exec() is
167 called. */
168 continue;
169 }
170 if (_Py_set_inheritable((int)fd, 1, NULL) < 0)
171 return -1;
172 }
173 return 0;
174}
175
Gregory P. Smith8facece2012-01-21 14:01:08 -0800176
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700177/* Get the maximum file descriptor that could be opened by this process.
178 * This function is async signal safe for use between fork() and exec().
179 */
180static long
181safe_get_max_fd(void)
182{
183 long local_max_fd;
184#if defined(__NetBSD__)
185 local_max_fd = fcntl(0, F_MAXFD);
186 if (local_max_fd >= 0)
187 return local_max_fd;
188#endif
Gregory P. Smithf9681772015-04-25 23:43:34 -0700189#if defined(HAVE_SYS_RESOURCE_H) && defined(__OpenBSD__)
190 struct rlimit rl;
191 /* Not on the POSIX async signal safe functions list but likely
192 * safe. TODO - Someone should audit OpenBSD to make sure. */
193 if (getrlimit(RLIMIT_NOFILE, &rl) >= 0)
194 return (long) rl.rlim_max;
195#endif
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700196#ifdef _SC_OPEN_MAX
197 local_max_fd = sysconf(_SC_OPEN_MAX);
198 if (local_max_fd == -1)
199#endif
200 local_max_fd = 256; /* Matches legacy Lib/subprocess.py behavior. */
201 return local_max_fd;
202}
203
204
205/* Close all file descriptors in the range from start_fd and higher
206 * except for those in py_fds_to_keep. If the range defined by
207 * [start_fd, safe_get_max_fd()) is large this will take a long
208 * time as it calls close() on EVERY possible fd.
209 *
210 * It isn't possible to know for sure what the max fd to go up to
211 * is for processes with the capability of raising their maximum.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800212 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500213static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700214_close_fds_by_brute_force(long start_fd, PyObject *py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800215{
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700216 long end_fd = safe_get_max_fd();
Gregory P. Smith8facece2012-01-21 14:01:08 -0800217 Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep);
218 Py_ssize_t keep_seq_idx;
219 int fd_num;
220 /* As py_fds_to_keep is sorted we can loop through the list closing
221 * fds inbetween any in the keep list falling within our range. */
222 for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
223 PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep,
224 keep_seq_idx);
225 int keep_fd = PyLong_AsLong(py_keep_fd);
226 if (keep_fd < start_fd)
227 continue;
228 for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200229 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800230 }
231 start_fd = keep_fd + 1;
232 }
233 if (start_fd <= end_fd) {
234 for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200235 close(fd_num);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800236 }
237 }
238}
239
240
241#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
242/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
243 * only to read a directory of short file descriptor number names. The kernel
244 * will return an error if we didn't give it enough space. Highly Unlikely.
245 * This structure is very old and stable: It will not change unless the kernel
246 * chooses to break compatibility with all existing binaries. Highly Unlikely.
247 */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800248struct linux_dirent64 {
Gregory P. Smith58f07a92012-06-05 13:26:39 -0700249 unsigned long long d_ino;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800250 long long d_off;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800251 unsigned short d_reclen; /* Length of this linux_dirent */
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800252 unsigned char d_type;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800253 char d_name[256]; /* Filename (null-terminated) */
254};
255
Gregory P. Smitha26987a2014-06-01 13:46:36 -0700256/* Close all open file descriptors in the range from start_fd and higher
257 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800258 *
259 * This version is async signal safe as it does not make any unsafe C library
260 * calls, malloc calls or handle any locks. It is _unfortunate_ to be forced
261 * to resort to making a kernel system call directly but this is the ONLY api
262 * available that does no harm. opendir/readdir/closedir perform memory
263 * allocation and locking so while they usually work they are not guaranteed
264 * to (especially if you have replaced your malloc implementation). A version
265 * of this function that uses those can be found in the _maybe_unsafe variant.
266 *
267 * This is Linux specific because that is all I am ready to test it on. It
268 * should be easy to add OS specific dirent or dirent64 structures and modify
269 * it with some cpp #define magic to work on other OSes as well if you want.
270 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500271static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700272_close_open_fds_safe(int start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800273{
274 int fd_dir_fd;
Victor Stinnerdaf45552013-08-28 00:53:59 +0200275
Victor Stinner160e8192015-03-30 02:18:31 +0200276 fd_dir_fd = _Py_open_noraise(FD_DIR, O_RDONLY);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800277 if (fd_dir_fd == -1) {
278 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700279 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800280 return;
281 } else {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800282 char buffer[sizeof(struct linux_dirent64)];
Gregory P. Smith8facece2012-01-21 14:01:08 -0800283 int bytes;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800284 while ((bytes = syscall(SYS_getdents64, fd_dir_fd,
285 (struct linux_dirent64 *)buffer,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800286 sizeof(buffer))) > 0) {
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800287 struct linux_dirent64 *entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800288 int offset;
289 for (offset = 0; offset < bytes; offset += entry->d_reclen) {
290 int fd;
Gregory P. Smith255bf5b2013-03-03 10:45:05 -0800291 entry = (struct linux_dirent64 *)(buffer + offset);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800292 if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
293 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700294 if (fd != fd_dir_fd && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800295 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200296 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800297 }
298 }
299 }
Victor Stinnere7c74922015-04-02 16:24:46 +0200300 close(fd_dir_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800301 }
302}
303
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700304#define _close_open_fds _close_open_fds_safe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800305
306#else /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
307
308
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700309/* Close all open file descriptors from start_fd and higher.
310 * Do not close any in the sorted py_fds_to_keep list.
Gregory P. Smith8facece2012-01-21 14:01:08 -0800311 *
312 * This function violates the strict use of async signal safe functions. :(
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800313 * It calls opendir(), readdir() and closedir(). Of these, the one most
Gregory P. Smith8facece2012-01-21 14:01:08 -0800314 * likely to ever cause a problem is opendir() as it performs an internal
315 * malloc(). Practically this should not be a problem. The Java VM makes the
316 * same calls between fork and exec in its own UNIXProcess_md.c implementation.
317 *
318 * readdir_r() is not used because it provides no benefit. It is typically
319 * implemented as readdir() followed by memcpy(). See also:
320 * http://womble.decadent.org.uk/readdir_r-advisory.html
321 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500322static void
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700323_close_open_fds_maybe_unsafe(long start_fd, PyObject* py_fds_to_keep)
Gregory P. Smith8facece2012-01-21 14:01:08 -0800324{
325 DIR *proc_fd_dir;
326#ifndef HAVE_DIRFD
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700327 while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep)) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800328 ++start_fd;
329 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800330 /* Close our lowest fd before we call opendir so that it is likely to
331 * reuse that fd otherwise we might close opendir's file descriptor in
332 * our loop. This trick assumes that fd's are allocated on a lowest
333 * available basis. */
Victor Stinnere7c74922015-04-02 16:24:46 +0200334 close(start_fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800335 ++start_fd;
336#endif
Gregory P. Smith8facece2012-01-21 14:01:08 -0800337
Gregory P. Smith4842efc2012-01-21 21:01:24 -0800338#if defined(__FreeBSD__)
339 if (!_is_fdescfs_mounted_on_dev_fd())
340 proc_fd_dir = NULL;
341 else
342#endif
343 proc_fd_dir = opendir(FD_DIR);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800344 if (!proc_fd_dir) {
345 /* No way to get a list of open fds. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700346 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800347 } else {
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800348 struct dirent *dir_entry;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800349#ifdef HAVE_DIRFD
Gregory P. Smithe9b7cab2012-01-21 15:19:11 -0800350 int fd_used_by_opendir = dirfd(proc_fd_dir);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800351#else
352 int fd_used_by_opendir = start_fd - 1;
353#endif
354 errno = 0;
Gregory P. Smithe3f78482012-01-21 15:16:17 -0800355 while ((dir_entry = readdir(proc_fd_dir))) {
Gregory P. Smith8facece2012-01-21 14:01:08 -0800356 int fd;
357 if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
358 continue; /* Not a number. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700359 if (fd != fd_used_by_opendir && fd >= start_fd &&
Gregory P. Smith8facece2012-01-21 14:01:08 -0800360 !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep)) {
Victor Stinnere7c74922015-04-02 16:24:46 +0200361 close(fd);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800362 }
363 errno = 0;
364 }
365 if (errno) {
366 /* readdir error, revert behavior. Highly Unlikely. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700367 _close_fds_by_brute_force(start_fd, py_fds_to_keep);
Gregory P. Smith8facece2012-01-21 14:01:08 -0800368 }
369 closedir(proc_fd_dir);
370 }
371}
372
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700373#define _close_open_fds _close_open_fds_maybe_unsafe
Gregory P. Smith8facece2012-01-21 14:01:08 -0800374
375#endif /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
376
377
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000378/*
379 * This function is code executed in the child process immediately after fork
380 * to set things up and call exec().
381 *
382 * All of the code in this function must only use async-signal-safe functions,
383 * listed at `man 7 signal` or
384 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
385 *
386 * This restriction is documented at
387 * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
388 */
Benjamin Peterson91eef982012-01-22 20:04:46 -0500389static void
390child_exec(char *const exec_array[],
391 char *const argv[],
392 char *const envp[],
393 const char *cwd,
394 int p2cread, int p2cwrite,
395 int c2pread, int c2pwrite,
396 int errread, int errwrite,
397 int errpipe_read, int errpipe_write,
398 int close_fds, int restore_signals,
399 int call_setsid,
400 PyObject *py_fds_to_keep,
401 PyObject *preexec_fn,
402 PyObject *preexec_fn_args_tuple)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000403{
Victor Stinner185fd332015-04-01 18:35:53 +0200404 int i, saved_errno, reached_preexec = 0;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000405 PyObject *result;
Gregory P. Smith14affb82010-12-22 05:22:17 +0000406 const char* err_msg = "";
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000407 /* Buffer large enough to hold a hex integer. We can't malloc. */
408 char hex_errno[sizeof(saved_errno)*2+1];
409
Victor Stinnerdaf45552013-08-28 00:53:59 +0200410 if (make_inheritable(py_fds_to_keep, errpipe_write) < 0)
411 goto error;
412
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000413 /* Close parent's pipe ends. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200414 if (p2cwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000415 POSIX_CALL(close(p2cwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200416 if (c2pread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000417 POSIX_CALL(close(c2pread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200418 if (errread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000419 POSIX_CALL(close(errread));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000420 POSIX_CALL(close(errpipe_read));
421
Ross Lagerwalld98646e2011-07-27 07:16:31 +0200422 /* When duping fds, if there arises a situation where one of the fds is
423 either 0, 1 or 2, it is possible that it is overwritten (#12607). */
424 if (c2pwrite == 0)
425 POSIX_CALL(c2pwrite = dup(c2pwrite));
426 if (errwrite == 0 || errwrite == 1)
427 POSIX_CALL(errwrite = dup(errwrite));
428
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000429 /* Dup fds for child.
430 dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
431 would be a no-op (issue #10806). */
432 if (p2cread == 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200433 if (_Py_set_inheritable(p2cread, 1, NULL) < 0)
434 goto error;
435 }
436 else if (p2cread != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000437 POSIX_CALL(dup2(p2cread, 0)); /* stdin */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200438
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000439 if (c2pwrite == 1) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200440 if (_Py_set_inheritable(c2pwrite, 1, NULL) < 0)
441 goto error;
442 }
443 else if (c2pwrite != -1)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000444 POSIX_CALL(dup2(c2pwrite, 1)); /* stdout */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200445
Antoine Pitrouc9c83ba2011-01-03 18:23:55 +0000446 if (errwrite == 2) {
Victor Stinnerdaf45552013-08-28 00:53:59 +0200447 if (_Py_set_inheritable(errwrite, 1, NULL) < 0)
448 goto error;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000449 }
Victor Stinnerdaf45552013-08-28 00:53:59 +0200450 else if (errwrite != -1)
451 POSIX_CALL(dup2(errwrite, 2)); /* stderr */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000452
453 /* Close pipe fds. Make sure we don't close the same fd more than */
454 /* once, or standard fds. */
Victor Stinnerdaf45552013-08-28 00:53:59 +0200455 if (p2cread > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000456 POSIX_CALL(close(p2cread));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200457 if (c2pwrite > 2 && c2pwrite != p2cread)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000458 POSIX_CALL(close(c2pwrite));
Victor Stinnerdaf45552013-08-28 00:53:59 +0200459 if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2)
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000460 POSIX_CALL(close(errwrite));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000461
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000462 if (cwd)
463 POSIX_CALL(chdir(cwd));
464
465 if (restore_signals)
466 _Py_RestoreSignals();
467
468#ifdef HAVE_SETSID
469 if (call_setsid)
470 POSIX_CALL(setsid());
471#endif
472
Gregory P. Smith5591b022012-10-10 03:34:47 -0700473 reached_preexec = 1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000474 if (preexec_fn != Py_None && preexec_fn_args_tuple) {
475 /* This is where the user has asked us to deadlock their program. */
476 result = PyObject_Call(preexec_fn, preexec_fn_args_tuple, NULL);
477 if (result == NULL) {
478 /* Stringifying the exception or traceback would involve
479 * memory allocation and thus potential for deadlock.
480 * We've already faced potential deadlock by calling back
481 * into Python in the first place, so it probably doesn't
482 * matter but we avoid it to minimize the possibility. */
483 err_msg = "Exception occurred in preexec_fn.";
484 errno = 0; /* We don't want to report an OSError. */
485 goto error;
486 }
487 /* Py_DECREF(result); - We're about to exec so why bother? */
488 }
489
Charles-François Natali249cdc32013-08-25 18:24:45 +0200490 /* close FDs after executing preexec_fn, which might open FDs */
491 if (close_fds) {
Charles-François Natali249cdc32013-08-25 18:24:45 +0200492 /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
Gregory P. Smithd4dcb702014-06-01 13:18:28 -0700493 _close_open_fds(3, py_fds_to_keep);
Charles-François Natali249cdc32013-08-25 18:24:45 +0200494 }
495
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000496 /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
497 /* given the executable_list generated by Lib/subprocess.py. */
498 saved_errno = 0;
499 for (i = 0; exec_array[i] != NULL; ++i) {
500 const char *executable = exec_array[i];
501 if (envp) {
502 execve(executable, argv, envp);
503 } else {
504 execv(executable, argv);
505 }
506 if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
507 saved_errno = errno;
508 }
509 }
510 /* Report the first exec error, not the last. */
511 if (saved_errno)
512 errno = saved_errno;
513
514error:
515 saved_errno = errno;
516 /* Report the posix error to our parent process. */
Gregory P. Smith12fdca52012-01-21 12:31:25 -0800517 /* We ignore all write() return values as the total size of our writes is
Victor Stinner185fd332015-04-01 18:35:53 +0200518 less than PIPEBUF and we cannot do anything about an error anyways.
519 Use _Py_write_noraise() to retry write() if it is interrupted by a
520 signal (fails with EINTR). */
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000521 if (saved_errno) {
522 char *cur;
Victor Stinner185fd332015-04-01 18:35:53 +0200523 _Py_write_noraise(errpipe_write, "OSError:", 8);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000524 cur = hex_errno + sizeof(hex_errno);
525 while (saved_errno != 0 && cur > hex_errno) {
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000526 *--cur = Py_hexdigits[saved_errno % 16];
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000527 saved_errno /= 16;
528 }
Victor Stinner185fd332015-04-01 18:35:53 +0200529 _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
530 _Py_write_noraise(errpipe_write, ":", 1);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700531 if (!reached_preexec) {
532 /* Indicate to the parent that the error happened before exec(). */
Victor Stinner185fd332015-04-01 18:35:53 +0200533 _Py_write_noraise(errpipe_write, "noexec", 6);
Gregory P. Smith5591b022012-10-10 03:34:47 -0700534 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000535 /* We can't call strerror(saved_errno). It is not async signal safe.
536 * The parent process will look the error message up. */
537 } else {
Victor Stinner185fd332015-04-01 18:35:53 +0200538 _Py_write_noraise(errpipe_write, "SubprocessError:0:", 18);
539 _Py_write_noraise(errpipe_write, err_msg, strlen(err_msg));
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000540 }
541}
542
543
544static PyObject *
545subprocess_fork_exec(PyObject* self, PyObject *args)
546{
547 PyObject *gc_module = NULL;
Antoine Pitrou721738f2012-08-15 23:20:39 +0200548 PyObject *executable_list, *py_fds_to_keep;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000549 PyObject *env_list, *preexec_fn;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000550 PyObject *process_args, *converted_args = NULL, *fast_args = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000551 PyObject *preexec_fn_args_tuple = NULL;
552 int p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite;
553 int errpipe_read, errpipe_write, close_fds, restore_signals;
554 int call_setsid;
Victor Stinner0e59cc32010-04-16 23:49:32 +0000555 PyObject *cwd_obj, *cwd_obj2;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000556 const char *cwd;
557 pid_t pid;
558 int need_to_reenable_gc = 0;
559 char *const *exec_array, *const *argv = NULL, *const *envp = NULL;
Gregory P. Smith8facece2012-01-21 14:01:08 -0800560 Py_ssize_t arg_num;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200561#ifdef WITH_THREAD
Victor Stinner8f437aa2014-10-05 17:25:19 +0200562 int import_lock_held = 0;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200563#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000564
565 if (!PyArg_ParseTuple(
Antoine Pitrou721738f2012-08-15 23:20:39 +0200566 args, "OOpOOOiiiiiiiiiiO:fork_exec",
567 &process_args, &executable_list, &close_fds, &py_fds_to_keep,
Victor Stinner0e59cc32010-04-16 23:49:32 +0000568 &cwd_obj, &env_list,
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000569 &p2cread, &p2cwrite, &c2pread, &c2pwrite,
570 &errread, &errwrite, &errpipe_read, &errpipe_write,
571 &restore_signals, &call_setsid, &preexec_fn))
572 return NULL;
573
Gregory P. Smith361e30c2013-12-01 00:12:24 -0800574 if (close_fds && errpipe_write < 3) { /* precondition */
575 PyErr_SetString(PyExc_ValueError, "errpipe_write must be >= 3");
576 return NULL;
577 }
Gregory P. Smith8facece2012-01-21 14:01:08 -0800578 if (PySequence_Length(py_fds_to_keep) < 0) {
579 PyErr_SetString(PyExc_ValueError, "cannot get length of fds_to_keep");
580 return NULL;
581 }
582 if (_sanity_check_python_fd_sequence(py_fds_to_keep)) {
583 PyErr_SetString(PyExc_ValueError, "bad value(s) in fds_to_keep");
Gregory P. Smithd4cc7bf2010-12-04 11:22:11 +0000584 return NULL;
585 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000586
587 /* We need to call gc.disable() when we'll be calling preexec_fn */
588 if (preexec_fn != Py_None) {
589 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200590 _Py_IDENTIFIER(isenabled);
591 _Py_IDENTIFIER(disable);
Victor Stinnerdaf45552013-08-28 00:53:59 +0200592
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000593 gc_module = PyImport_ImportModule("gc");
594 if (gc_module == NULL)
595 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200596 result = _PyObject_CallMethodId(gc_module, &PyId_isenabled, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000597 if (result == NULL) {
598 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000599 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000600 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000601 need_to_reenable_gc = PyObject_IsTrue(result);
602 Py_DECREF(result);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000603 if (need_to_reenable_gc == -1) {
604 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000605 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000606 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200607 result = _PyObject_CallMethodId(gc_module, &PyId_disable, NULL);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000608 if (result == NULL) {
609 Py_DECREF(gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000610 return NULL;
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000611 }
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000612 Py_DECREF(result);
613 }
614
615 exec_array = _PySequence_BytesToCharpArray(executable_list);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200616 if (!exec_array)
617 goto cleanup;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000618
619 /* Convert args and env into appropriate arguments for exec() */
620 /* These conversions are done in the parent process to avoid allocating
621 or freeing memory in the child process. */
622 if (process_args != Py_None) {
Gregory P. Smith68f52172010-03-15 06:07:42 +0000623 Py_ssize_t num_args;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000624 /* Equivalent to: */
625 /* tuple(PyUnicode_FSConverter(arg) for arg in process_args) */
Gregory P. Smith68f52172010-03-15 06:07:42 +0000626 fast_args = PySequence_Fast(process_args, "argv must be a tuple");
Stefan Krahdb579d72012-08-20 14:36:47 +0200627 if (fast_args == NULL)
628 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000629 num_args = PySequence_Fast_GET_SIZE(fast_args);
630 converted_args = PyTuple_New(num_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000631 if (converted_args == NULL)
632 goto cleanup;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000633 for (arg_num = 0; arg_num < num_args; ++arg_num) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000634 PyObject *borrowed_arg, *converted_arg;
Gregory P. Smith68f52172010-03-15 06:07:42 +0000635 borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000636 if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
637 goto cleanup;
638 PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
639 }
640
641 argv = _PySequence_BytesToCharpArray(converted_args);
642 Py_CLEAR(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000643 Py_CLEAR(fast_args);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000644 if (!argv)
645 goto cleanup;
646 }
647
648 if (env_list != Py_None) {
649 envp = _PySequence_BytesToCharpArray(env_list);
650 if (!envp)
651 goto cleanup;
652 }
653
654 if (preexec_fn != Py_None) {
655 preexec_fn_args_tuple = PyTuple_New(0);
656 if (!preexec_fn_args_tuple)
657 goto cleanup;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200658#ifdef WITH_THREAD
Victor Stinner0e59cc32010-04-16 23:49:32 +0000659 _PyImport_AcquireLock();
Victor Stinner8f437aa2014-10-05 17:25:19 +0200660 import_lock_held = 1;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200661#endif
Victor Stinner0e59cc32010-04-16 23:49:32 +0000662 }
663
664 if (cwd_obj != Py_None) {
665 if (PyUnicode_FSConverter(cwd_obj, &cwd_obj2) == 0)
666 goto cleanup;
Victor Stinnerdcb24032010-04-22 12:08:36 +0000667 cwd = PyBytes_AsString(cwd_obj2);
Victor Stinner0e59cc32010-04-16 23:49:32 +0000668 } else {
669 cwd = NULL;
670 cwd_obj2 = NULL;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000671 }
672
673 pid = fork();
674 if (pid == 0) {
675 /* Child process */
Victor Stinner0e59cc32010-04-16 23:49:32 +0000676 /*
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000677 * Code from here to _exit() must only use async-signal-safe functions,
678 * listed at `man 7 signal` or
679 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
680 */
681
682 if (preexec_fn != Py_None) {
683 /* We'll be calling back into Python later so we need to do this.
684 * This call may not be async-signal-safe but neither is calling
685 * back into Python. The user asked us to use hope as a strategy
686 * to avoid deadlock... */
687 PyOS_AfterFork();
688 }
689
690 child_exec(exec_array, argv, envp, cwd,
691 p2cread, p2cwrite, c2pread, c2pwrite,
692 errread, errwrite, errpipe_read, errpipe_write,
693 close_fds, restore_signals, call_setsid,
Gregory P. Smith8facece2012-01-21 14:01:08 -0800694 py_fds_to_keep, preexec_fn, preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000695 _exit(255);
696 return NULL; /* Dead code to avoid a potential compiler warning. */
697 }
Victor Stinner0e59cc32010-04-16 23:49:32 +0000698 Py_XDECREF(cwd_obj2);
699
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000700 if (pid == -1) {
701 /* Capture the errno exception before errno can be clobbered. */
702 PyErr_SetFromErrno(PyExc_OSError);
703 }
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200704#ifdef WITH_THREAD
705 if (preexec_fn != Py_None
706 && _PyImport_ReleaseLock() < 0 && !PyErr_Occurred()) {
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000707 PyErr_SetString(PyExc_RuntimeError,
708 "not holding the import lock");
Martin Panterafdd5132015-11-30 02:21:41 +0000709 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000710 }
Victor Stinner8f437aa2014-10-05 17:25:19 +0200711 import_lock_held = 0;
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200712#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000713
714 /* Parent process */
715 if (envp)
716 _Py_FreeCharPArray(envp);
717 if (argv)
718 _Py_FreeCharPArray(argv);
719 _Py_FreeCharPArray(exec_array);
720
721 /* Reenable gc in the parent process (or if fork failed). */
Martin Panterafdd5132015-11-30 02:21:41 +0000722 if (_enable_gc(need_to_reenable_gc, gc_module)) {
723 pid = -1;
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000724 }
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000725 Py_XDECREF(preexec_fn_args_tuple);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000726 Py_XDECREF(gc_module);
727
728 if (pid == -1)
729 return NULL; /* fork() failed. Exception set earlier. */
730
731 return PyLong_FromPid(pid);
732
733cleanup:
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200734#ifdef WITH_THREAD
Victor Stinner8f437aa2014-10-05 17:25:19 +0200735 if (import_lock_held)
736 _PyImport_ReleaseLock();
Victor Stinnerbc5b80b2015-10-11 09:54:42 +0200737#endif
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000738 if (envp)
739 _Py_FreeCharPArray(envp);
740 if (argv)
741 _Py_FreeCharPArray(argv);
Victor Stinner8f437aa2014-10-05 17:25:19 +0200742 if (exec_array)
743 _Py_FreeCharPArray(exec_array);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000744 Py_XDECREF(converted_args);
Gregory P. Smith68f52172010-03-15 06:07:42 +0000745 Py_XDECREF(fast_args);
Gregory P. Smith32ec9da2010-03-19 16:53:08 +0000746 Py_XDECREF(preexec_fn_args_tuple);
Martin Panterafdd5132015-11-30 02:21:41 +0000747 _enable_gc(need_to_reenable_gc, gc_module);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000748 Py_XDECREF(gc_module);
749 return NULL;
750}
751
752
753PyDoc_STRVAR(subprocess_fork_exec_doc,
754"fork_exec(args, executable_list, close_fds, cwd, env,\n\
755 p2cread, p2cwrite, c2pread, c2pwrite,\n\
756 errread, errwrite, errpipe_read, errpipe_write,\n\
757 restore_signals, call_setsid, preexec_fn)\n\
758\n\
759Forks a child process, closes parent file descriptors as appropriate in the\n\
760child and dups the few that are needed before calling exec() in the child\n\
761process.\n\
762\n\
763The preexec_fn, if supplied, will be called immediately before exec.\n\
764WARNING: preexec_fn is NOT SAFE if your application uses threads.\n\
765 It may trigger infrequent, difficult to debug deadlocks.\n\
766\n\
767If an error occurs in the child process before the exec, it is\n\
768serialized and written to the errpipe_write fd per subprocess.py.\n\
769\n\
770Returns: the child process's PID.\n\
771\n\
772Raises: Only on an error in the parent process.\n\
773");
774
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000775/* module level code ********************************************************/
776
777PyDoc_STRVAR(module_doc,
778"A POSIX helper for the subprocess module.");
779
780
781static PyMethodDef module_methods[] = {
782 {"fork_exec", subprocess_fork_exec, METH_VARARGS, subprocess_fork_exec_doc},
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000783 {NULL, NULL} /* sentinel */
784};
785
786
787static struct PyModuleDef _posixsubprocessmodule = {
788 PyModuleDef_HEAD_INIT,
789 "_posixsubprocess",
790 module_doc,
791 -1, /* No memory is needed. */
792 module_methods,
793};
794
795PyMODINIT_FUNC
796PyInit__posixsubprocess(void)
797{
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +0000798 return PyModule_Create(&_posixsubprocessmodule);
799}