Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't chdir when running as a daemon #1547

Merged
merged 3 commits into from Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions cherrypy/__main__.py
@@ -1,4 +1,5 @@
import cherrypy.daemon
from cherrypy.daemon import run


if __name__ == '__main__':
cherrypy.daemon.run()
run()
6 changes: 0 additions & 6 deletions cherrypy/cherryd

This file was deleted.

1 change: 0 additions & 1 deletion cherrypy/process/plugins.py
Expand Up @@ -409,7 +409,6 @@ def start(self):
sys.exit('%s: fork #2 failed: (%d) %s\n'
% (sys.argv[0], exc.errno, exc.strerror))

os.chdir('/')
os.umask(0)

si = open(self.stdin, 'r')
Expand Down
53 changes: 52 additions & 1 deletion cherrypy/process/wspbus.py
Expand Up @@ -447,6 +447,57 @@ def _get_true_argv():
argc = ctypes.c_int()

ctypes.pythonapi.Py_GetArgcArgv(ctypes.byref(argc), ctypes.byref(argv))

_argv = argv[:argc.value]

# The code below is trying to correctly handle special cases.
# `-c`'s argument interpreted by Python itself becomes `-c` as
# well. Same applies to `-m`. This snippet is trying to survive
# at least the case with `-m`
# Ref: https://github.com/cherrypy/cherrypy/issues/1545
# Ref: python/cpython@418baf9
argv_len, is_command, is_module = len(_argv), False, False

try:
m_ind = _argv.index('-m')
if m_ind < argv_len - 1 and _argv[m_ind + 1] in ('-c', '-m'):
"""
In some older Python versions `-m`'s argument may be
substituted with `-c`, not `-m`
"""
is_module = True
except (IndexError, ValueError):
m_ind = None

try:
c_ind = _argv.index('-c')
if m_ind < argv_len - 1 and _argv[c_ind + 1] == '-c':
is_command = True
except (IndexError, ValueError):
c_ind = None

if is_module:
"""It's containing `-m -m` sequence of arguments"""
if is_command and c_ind < m_ind:
"""There's `-c -c` before `-m`"""
raise RuntimeError(
"Cannot reconstruct command from '-c'. Ref: "
'https://github.com/cherrypy/cherrypy/issues/1545')
# Survive module argument here
original_module = sys.argv[0]
if not os.access(original_module, os.R_OK):
"""There's no such module exist"""
raise AttributeError(
"{} doesn't seem to be a module "
"accessible by current user".format(original_module))
del _argv[m_ind:m_ind + 2] # remove `-m -m`
# ... and substitute it with the original module path:
_argv.insert(m_ind, original_module)
elif is_command:
"""It's containing just `-c -c` sequence of arguments"""
raise RuntimeError(
"Cannot reconstruct command from '-c'. "
'Ref: https://github.com/cherrypy/cherrypy/issues/1545')
except AttributeError:
"""It looks Py_GetArgcArgv is completely absent in some environments

Expand All @@ -459,7 +510,7 @@ def _get_true_argv():
"""
raise NotImplementedError
else:
return argv[:argc.value]
return _argv

@staticmethod
def _extend_pythonpath(env):
Expand Down
2 changes: 1 addition & 1 deletion cherrypy/scaffold/__init__.py
Expand Up @@ -8,7 +8,7 @@
Even before any tweaking, this should serve a few demonstration pages.
Change to this directory and run:

../cherryd -c site.conf
cherryd -c site.conf

"""

Expand Down
21 changes: 20 additions & 1 deletion cherrypy/test/helper.py
Expand Up @@ -473,10 +473,29 @@ def start(self, imports=None):
cherrypy._cpserver.wait_for_free_port(self.host, self.port)

args = [
os.path.join(thisdir, '..', 'cherryd'),
'-m',
'cherrypy.__main__', # __main__ is needed for `-m` in Python 2.6
'-c', self.config_file,
'-p', self.pid_file,
]
"""
Command for running cherryd server with autoreload enabled

Using

```
['-c',
"__requires__ = 'CherryPy'; \
import pkg_resources, re, sys; \
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]); \
sys.exit(\
pkg_resources.load_entry_point(\
'CherryPy', 'console_scripts', 'cherryd')())"]
```

doesn't work as it's impossible to reconstruct the `-c`'s contents.
Ref: https://github.com/cherrypy/cherrypy/issues/1545
"""

if not isinstance(imports, (list, tuple)):
imports = [imports]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -107,7 +107,7 @@
url=url,
license=cp_license,
packages=packages,
scripts=scripts,
entry_points={"console_scripts": ["cherryd = cherrypy.__main__:run"]},
include_package_data=True,
install_requires=install_requires,
extras_require=extras_require,
Expand Down