platforms.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.platforms
  4. ~~~~~~~~~~~~~~~~
  5. Utilities dealing with platform specifics: signals, daemonization,
  6. users, groups, and so on.
  7. """
  8. from __future__ import absolute_import, print_function
  9. import atexit
  10. import errno
  11. import math
  12. import os
  13. import platform as _platform
  14. import signal as _signal
  15. import sys
  16. from billiard import current_process
  17. from kombu.utils.compat import get_errno
  18. from kombu.utils.encoding import safe_str
  19. from contextlib import contextmanager
  20. from .local import try_import
  21. from .five import items, range, reraise, string_t, int_types
  22. _setproctitle = try_import('setproctitle')
  23. resource = try_import('resource')
  24. pwd = try_import('pwd')
  25. grp = try_import('grp')
  26. # exitcodes
  27. EX_OK = getattr(os, 'EX_OK', 0)
  28. EX_FAILURE = 1
  29. EX_UNAVAILABLE = getattr(os, 'EX_UNAVAILABLE', 69)
  30. EX_USAGE = getattr(os, 'EX_USAGE', 64)
  31. SYSTEM = _platform.system()
  32. IS_OSX = SYSTEM == 'Darwin'
  33. IS_WINDOWS = SYSTEM == 'Windows'
  34. DAEMON_UMASK = 0
  35. DAEMON_WORKDIR = '/'
  36. PIDFILE_FLAGS = os.O_CREAT | os.O_EXCL | os.O_WRONLY
  37. PIDFILE_MODE = ((os.R_OK | os.W_OK) << 6) | ((os.R_OK) << 3) | ((os.R_OK))
  38. PIDLOCKED = """ERROR: Pidfile ({0}) already exists.
  39. Seems we're already running? (pid: {1})"""
  40. try:
  41. from io import UnsupportedOperation
  42. FILENO_ERRORS = (AttributeError, UnsupportedOperation)
  43. except ImportError: # pragma: no cover
  44. # Py2
  45. FILENO_ERRORS = (AttributeError, ) # noqa
  46. try:
  47. from io import UnsupportedOperation
  48. FILENO_ERRORS = (AttributeError, UnsupportedOperation)
  49. except ImportError: # pragma: no cover
  50. # Py2
  51. FILENO_ERRORS = (AttributeError, ) # noqa
  52. def pyimplementation():
  53. """Returns string identifying the current Python implementation."""
  54. if hasattr(_platform, 'python_implementation'):
  55. return _platform.python_implementation()
  56. elif sys.platform.startswith('java'):
  57. return 'Jython ' + sys.platform
  58. elif hasattr(sys, 'pypy_version_info'):
  59. v = '.'.join(str(p) for p in sys.pypy_version_info[:3])
  60. if sys.pypy_version_info[3:]:
  61. v += '-' + ''.join(str(p) for p in sys.pypy_version_info[3:])
  62. return 'PyPy ' + v
  63. else:
  64. return 'CPython'
  65. def _find_option_with_arg(argv, short_opts=None, long_opts=None):
  66. """Search argv for option specifying its short and longopt
  67. alternatives.
  68. Returns the value of the option if found.
  69. """
  70. for i, arg in enumerate(argv):
  71. if arg.startswith('-'):
  72. if long_opts and arg.startswith('--'):
  73. name, _, val = arg.partition('=')
  74. if name in long_opts:
  75. return val
  76. if short_opts and arg in short_opts:
  77. return argv[i + 1]
  78. raise KeyError('|'.join(short_opts or [] + long_opts or []))
  79. def maybe_patch_concurrency(argv, short_opts=None, long_opts=None):
  80. """With short and long opt alternatives that specify the command line
  81. option to set the pool, this makes sure that anything that needs
  82. to be patched is completed as early as possible.
  83. (e.g. eventlet/gevent monkey patches)."""
  84. try:
  85. pool = _find_option_with_arg(argv, short_opts, long_opts)
  86. except KeyError:
  87. pass
  88. else:
  89. # set up eventlet/gevent environments ASAP.
  90. from celery import concurrency
  91. concurrency.get_implementation(pool)
  92. class LockFailed(Exception):
  93. """Raised if a pidlock can't be acquired."""
  94. def get_fdmax(default=None):
  95. """Returns the maximum number of open file descriptors
  96. on this system.
  97. :keyword default: Value returned if there's no file
  98. descriptor limit.
  99. """
  100. fdmax = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  101. if fdmax == resource.RLIM_INFINITY:
  102. return default
  103. return fdmax
  104. class Pidfile(object):
  105. """Pidfile
  106. This is the type returned by :func:`create_pidlock`.
  107. TIP: Use the :func:`create_pidlock` function instead,
  108. which is more convenient and also removes stale pidfiles (when
  109. the process holding the lock is no longer running).
  110. """
  111. #: Path to the pid lock file.
  112. path = None
  113. def __init__(self, path):
  114. self.path = os.path.abspath(path)
  115. def acquire(self):
  116. """Acquire lock."""
  117. try:
  118. self.write_pid()
  119. except OSError as exc:
  120. reraise(LockFailed, LockFailed(str(exc)), sys.exc_info()[2])
  121. return self
  122. __enter__ = acquire
  123. def is_locked(self):
  124. """Returns true if the pid lock exists."""
  125. return os.path.exists(self.path)
  126. def release(self, *args):
  127. """Release lock."""
  128. self.remove()
  129. __exit__ = release
  130. def read_pid(self):
  131. """Reads and returns the current pid."""
  132. with ignore_errno('ENOENT'):
  133. with open(self.path, 'r') as fh:
  134. line = fh.readline()
  135. if line.strip() == line: # must contain '\n'
  136. raise ValueError(
  137. 'Partial or invalid pidfile {0.path}'.format(self))
  138. try:
  139. return int(line.strip())
  140. except ValueError:
  141. raise ValueError(
  142. 'pidfile {0.path} contents invalid.'.format(self))
  143. def remove(self):
  144. """Removes the lock."""
  145. with ignore_errno(errno.ENOENT, errno.EACCES):
  146. os.unlink(self.path)
  147. def remove_if_stale(self):
  148. """Removes the lock if the process is not running.
  149. (does not respond to signals)."""
  150. try:
  151. pid = self.read_pid()
  152. except ValueError as exc:
  153. print('Broken pidfile found. Removing it.', file=sys.stderr)
  154. self.remove()
  155. return True
  156. if not pid:
  157. self.remove()
  158. return True
  159. try:
  160. os.kill(pid, 0)
  161. except os.error as exc:
  162. if exc.errno == errno.ESRCH:
  163. print('Stale pidfile exists. Removing it.', file=sys.stderr)
  164. self.remove()
  165. return True
  166. return False
  167. def write_pid(self):
  168. pid = os.getpid()
  169. content = '{0}\n'.format(pid)
  170. pidfile_fd = os.open(self.path, PIDFILE_FLAGS, PIDFILE_MODE)
  171. pidfile = os.fdopen(pidfile_fd, 'w')
  172. try:
  173. pidfile.write(content)
  174. # flush and sync so that the re-read below works.
  175. pidfile.flush()
  176. try:
  177. os.fsync(pidfile_fd)
  178. except AttributeError: # pragma: no cover
  179. pass
  180. finally:
  181. pidfile.close()
  182. rfh = open(self.path)
  183. try:
  184. if rfh.read() != content:
  185. raise LockFailed(
  186. "Inconsistency: Pidfile content doesn't match at re-read")
  187. finally:
  188. rfh.close()
  189. PIDFile = Pidfile # compat alias
  190. def create_pidlock(pidfile):
  191. """Create and verify pidfile.
  192. If the pidfile already exists the program exits with an error message,
  193. however if the process it refers to is not running anymore, the pidfile
  194. is deleted and the program continues.
  195. This function will automatically install an :mod:`atexit` handler
  196. to release the lock at exit, you can skip this by calling
  197. :func:`_create_pidlock` instead.
  198. :returns: :class:`Pidfile`.
  199. **Example**:
  200. .. code-block:: python
  201. pidlock = create_pidlock('/var/run/app.pid')
  202. """
  203. pidlock = _create_pidlock(pidfile)
  204. atexit.register(pidlock.release)
  205. return pidlock
  206. def _create_pidlock(pidfile):
  207. pidlock = Pidfile(pidfile)
  208. if pidlock.is_locked() and not pidlock.remove_if_stale():
  209. raise SystemExit(PIDLOCKED.format(pidfile, pidlock.read_pid()))
  210. pidlock.acquire()
  211. return pidlock
  212. def fileno(f):
  213. if isinstance(f, int_types):
  214. return f
  215. return f.fileno()
  216. def maybe_fileno(f):
  217. """Get object fileno, or :const:`None` if not defined."""
  218. try:
  219. return fileno(f)
  220. except FILENO_ERRORS:
  221. pass
  222. def close_open_fds(keep=None):
  223. keep = [maybe_fileno(f) for f in keep if maybe_fileno(f)] if keep else []
  224. for fd in reversed(range(get_fdmax(default=2048))):
  225. if fd not in keep:
  226. with ignore_errno(errno.EBADF):
  227. os.close(fd)
  228. class DaemonContext(object):
  229. _is_open = False
  230. def __init__(self, pidfile=None, workdir=None, umask=None,
  231. fake=False, after_chdir=None, **kwargs):
  232. self.workdir = workdir or DAEMON_WORKDIR
  233. self.umask = DAEMON_UMASK if umask is None else umask
  234. self.fake = fake
  235. self.after_chdir = after_chdir
  236. self.stdfds = (sys.stdin, sys.stdout, sys.stderr)
  237. def redirect_to_null(self, fd):
  238. if fd is not None:
  239. dest = os.open(os.devnull, os.O_RDWR)
  240. os.dup2(dest, fd)
  241. def open(self):
  242. if not self._is_open:
  243. if not self.fake:
  244. self._detach()
  245. os.chdir(self.workdir)
  246. os.umask(self.umask)
  247. if self.after_chdir:
  248. self.after_chdir()
  249. close_open_fds(self.stdfds)
  250. for fd in self.stdfds:
  251. self.redirect_to_null(maybe_fileno(fd))
  252. self._is_open = True
  253. __enter__ = open
  254. def close(self, *args):
  255. if self._is_open:
  256. self._is_open = False
  257. __exit__ = close
  258. def _detach(self):
  259. if os.fork() == 0: # first child
  260. os.setsid() # create new session
  261. if os.fork() > 0: # second child
  262. os._exit(0)
  263. else:
  264. os._exit(0)
  265. return self
  266. def detached(logfile=None, pidfile=None, uid=None, gid=None, umask=0,
  267. workdir=None, fake=False, **opts):
  268. """Detach the current process in the background (daemonize).
  269. :keyword logfile: Optional log file. The ability to write to this file
  270. will be verified before the process is detached.
  271. :keyword pidfile: Optional pidfile. The pidfile will not be created,
  272. as this is the responsibility of the child. But the process will
  273. exit if the pid lock exists and the pid written is still running.
  274. :keyword uid: Optional user id or user name to change
  275. effective privileges to.
  276. :keyword gid: Optional group id or group name to change effective
  277. privileges to.
  278. :keyword umask: Optional umask that will be effective in the child process.
  279. :keyword workdir: Optional new working directory.
  280. :keyword fake: Don't actually detach, intented for debugging purposes.
  281. :keyword \*\*opts: Ignored.
  282. **Example**:
  283. .. code-block:: python
  284. from celery.platforms import detached, create_pidlock
  285. with detached(logfile='/var/log/app.log', pidfile='/var/run/app.pid',
  286. uid='nobody'):
  287. # Now in detached child process with effective user set to nobody,
  288. # and we know that our logfile can be written to, and that
  289. # the pidfile is not locked.
  290. pidlock = create_pidlock('/var/run/app.pid')
  291. # Run the program
  292. program.run(logfile='/var/log/app.log')
  293. """
  294. if not resource:
  295. raise RuntimeError('This platform does not support detach.')
  296. workdir = os.getcwd() if workdir is None else workdir
  297. signals.reset('SIGCLD') # Make sure SIGCLD is using the default handler.
  298. if not os.geteuid():
  299. # no point trying to setuid unless we're root.
  300. maybe_drop_privileges(uid=uid, gid=gid)
  301. def after_chdir_do():
  302. # Since without stderr any errors will be silently suppressed,
  303. # we need to know that we have access to the logfile.
  304. logfile and open(logfile, 'a').close()
  305. # Doesn't actually create the pidfile, but makes sure it's not stale.
  306. if pidfile:
  307. _create_pidlock(pidfile).release()
  308. return DaemonContext(
  309. umask=umask, workdir=workdir, fake=fake, after_chdir=after_chdir_do,
  310. )
  311. def parse_uid(uid):
  312. """Parse user id.
  313. uid can be an integer (uid) or a string (user name), if a user name
  314. the uid is taken from the system user registry.
  315. """
  316. try:
  317. return int(uid)
  318. except ValueError:
  319. try:
  320. return pwd.getpwnam(uid).pw_uid
  321. except (AttributeError, KeyError):
  322. raise KeyError('User does not exist: {0}'.format(uid))
  323. def parse_gid(gid):
  324. """Parse group id.
  325. gid can be an integer (gid) or a string (group name), if a group name
  326. the gid is taken from the system group registry.
  327. """
  328. try:
  329. return int(gid)
  330. except ValueError:
  331. try:
  332. return grp.getgrnam(gid).gr_gid
  333. except (AttributeError, KeyError):
  334. raise KeyError('Group does not exist: {0}'.format(gid))
  335. def _setgroups_hack(groups):
  336. """:fun:`setgroups` may have a platform-dependent limit,
  337. and it is not always possible to know in advance what this limit
  338. is, so we use this ugly hack stolen from glibc."""
  339. groups = groups[:]
  340. while 1:
  341. try:
  342. return os.setgroups(groups)
  343. except ValueError: # error from Python's check.
  344. if len(groups) <= 1:
  345. raise
  346. groups[:] = groups[:-1]
  347. except OSError as exc: # error from the OS.
  348. if exc.errno != errno.EINVAL or len(groups) <= 1:
  349. raise
  350. groups[:] = groups[:-1]
  351. def setgroups(groups):
  352. """Set active groups from a list of group ids."""
  353. max_groups = None
  354. try:
  355. max_groups = os.sysconf('SC_NGROUPS_MAX')
  356. except Exception:
  357. pass
  358. try:
  359. return _setgroups_hack(groups[:max_groups])
  360. except OSError as exc:
  361. if exc.errno != errno.EPERM:
  362. raise
  363. if any(group not in groups for group in os.getgroups()):
  364. # we shouldn't be allowed to change to this group.
  365. raise
  366. def initgroups(uid, gid):
  367. """Compat version of :func:`os.initgroups` which was first
  368. added to Python 2.7."""
  369. if not pwd: # pragma: no cover
  370. return
  371. username = pwd.getpwuid(uid)[0]
  372. if hasattr(os, 'initgroups'): # Python 2.7+
  373. return os.initgroups(username, gid)
  374. groups = [gr.gr_gid for gr in grp.getgrall()
  375. if username in gr.gr_mem]
  376. setgroups(groups)
  377. def setgid(gid):
  378. """Version of :func:`os.setgid` supporting group names."""
  379. os.setgid(parse_gid(gid))
  380. def setuid(uid):
  381. """Version of :func:`os.setuid` supporting usernames."""
  382. os.setuid(parse_uid(uid))
  383. def maybe_drop_privileges(uid=None, gid=None):
  384. """Change process privileges to new user/group.
  385. If UID and GID is specified, the real user/group is changed.
  386. If only UID is specified, the real user is changed, and the group is
  387. changed to the users primary group.
  388. If only GID is specified, only the group is changed.
  389. """
  390. uid = uid and parse_uid(uid)
  391. gid = gid and parse_gid(gid)
  392. if uid:
  393. # If GID isn't defined, get the primary GID of the user.
  394. if not gid and pwd:
  395. gid = pwd.getpwuid(uid).pw_gid
  396. # Must set the GID before initgroups(), as setgid()
  397. # is known to zap the group list on some platforms.
  398. # setgid must happen before setuid (otherwise the setgid operation
  399. # may fail because of insufficient privileges and possibly stay
  400. # in a privileged group).
  401. setgid(gid)
  402. initgroups(uid, gid)
  403. # at last:
  404. setuid(uid)
  405. # ... and make sure privileges cannot be restored:
  406. try:
  407. setuid(0)
  408. except OSError as exc:
  409. if get_errno(exc) != errno.EPERM:
  410. raise
  411. pass # Good: cannot restore privileges.
  412. else:
  413. raise RuntimeError(
  414. 'non-root user able to restore privileges after setuid.')
  415. else:
  416. gid and setgid(gid)
  417. class Signals(object):
  418. """Convenience interface to :mod:`signals`.
  419. If the requested signal is not supported on the current platform,
  420. the operation will be ignored.
  421. **Examples**:
  422. .. code-block:: python
  423. >>> from celery.platforms import signals
  424. >>> signals['INT'] = my_handler
  425. >>> signals['INT']
  426. my_handler
  427. >>> signals.supported('INT')
  428. True
  429. >>> signals.signum('INT')
  430. 2
  431. >>> signals.ignore('USR1')
  432. >>> signals['USR1'] == signals.ignored
  433. True
  434. >>> signals.reset('USR1')
  435. >>> signals['USR1'] == signals.default
  436. True
  437. >>> signals.update(INT=exit_handler,
  438. ... TERM=exit_handler,
  439. ... HUP=hup_handler)
  440. """
  441. ignored = _signal.SIG_IGN
  442. default = _signal.SIG_DFL
  443. if hasattr(_signal, 'setitimer'):
  444. def arm_alarm(self, seconds):
  445. _signal.setitimer(_signal.ITIMER_REAL, seconds)
  446. else: # pragma: no cover
  447. try:
  448. from itimer import alarm as _itimer_alarm # noqa
  449. except ImportError:
  450. def arm_alarm(self, seconds): # noqa
  451. _signal.alarm(math.ceil(seconds))
  452. else: # pragma: no cover
  453. def arm_alarm(self, seconds): # noqa
  454. return _itimer_alarm(seconds) # noqa
  455. def reset_alarm(self):
  456. return _signal.alarm(0)
  457. def supported(self, signal_name):
  458. """Returns true value if ``signal_name`` exists on this platform."""
  459. try:
  460. return self.signum(signal_name)
  461. except AttributeError:
  462. pass
  463. def signum(self, signal_name):
  464. """Get signal number from signal name."""
  465. if isinstance(signal_name, int):
  466. return signal_name
  467. if not isinstance(signal_name, string_t) \
  468. or not signal_name.isupper():
  469. raise TypeError('signal name must be uppercase string.')
  470. if not signal_name.startswith('SIG'):
  471. signal_name = 'SIG' + signal_name
  472. return getattr(_signal, signal_name)
  473. def reset(self, *signal_names):
  474. """Reset signals to the default signal handler.
  475. Does nothing if the platform doesn't support signals,
  476. or the specified signal in particular.
  477. """
  478. self.update((sig, self.default) for sig in signal_names)
  479. def ignore(self, *signal_names):
  480. """Ignore signal using :const:`SIG_IGN`.
  481. Does nothing if the platform doesn't support signals,
  482. or the specified signal in particular.
  483. """
  484. self.update((sig, self.ignored) for sig in signal_names)
  485. def __getitem__(self, signal_name):
  486. return _signal.getsignal(self.signum(signal_name))
  487. def __setitem__(self, signal_name, handler):
  488. """Install signal handler.
  489. Does nothing if the current platform doesn't support signals,
  490. or the specified signal in particular.
  491. """
  492. try:
  493. _signal.signal(self.signum(signal_name), handler)
  494. except (AttributeError, ValueError):
  495. pass
  496. def update(self, _d_=None, **sigmap):
  497. """Set signal handlers from a mapping."""
  498. for signal_name, handler in items(dict(_d_ or {}, **sigmap)):
  499. self[signal_name] = handler
  500. signals = Signals()
  501. get_signal = signals.signum # compat
  502. install_signal_handler = signals.__setitem__ # compat
  503. reset_signal = signals.reset # compat
  504. ignore_signal = signals.ignore # compat
  505. def strargv(argv):
  506. arg_start = 2 if 'manage' in argv[0] else 1
  507. if len(argv) > arg_start:
  508. return ' '.join(argv[arg_start:])
  509. return ''
  510. def set_process_title(progname, info=None):
  511. """Set the ps name for the currently running process.
  512. Only works if :mod:`setproctitle` is installed.
  513. """
  514. proctitle = '[{0}]'.format(progname)
  515. proctitle = '{0} {1}'.format(proctitle, info) if info else proctitle
  516. if _setproctitle:
  517. _setproctitle.setproctitle(safe_str(proctitle))
  518. return proctitle
  519. if os.environ.get('NOSETPS'): # pragma: no cover
  520. def set_mp_process_title(*a, **k):
  521. pass
  522. else:
  523. def set_mp_process_title(progname, info=None, hostname=None): # noqa
  524. """Set the ps name using the multiprocessing process name.
  525. Only works if :mod:`setproctitle` is installed.
  526. """
  527. if hostname:
  528. progname = '{0}: {1}'.format(progname, hostname)
  529. return set_process_title(
  530. '{0}:{1}'.format(progname, current_process().name), info=info)
  531. def get_errno_name(n):
  532. """Get errno for string, e.g. ``ENOENT``."""
  533. if isinstance(n, string_t):
  534. return getattr(errno, n)
  535. return n
  536. @contextmanager
  537. def ignore_errno(*errnos, **kwargs):
  538. """Context manager to ignore specific POSIX error codes.
  539. Takes a list of error codes to ignore, which can be either
  540. the name of the code, or the code integer itself::
  541. >>> with ignore_errno('ENOENT'):
  542. ... with open('foo', 'r'):
  543. ... return r.read()
  544. >>> with ignore_errno(errno.ENOENT, errno.EPERM):
  545. ... pass
  546. :keyword types: A tuple of exceptions to ignore (when the errno matches),
  547. defaults to :exc:`Exception`.
  548. """
  549. types = kwargs.get('types') or (Exception, )
  550. errnos = [get_errno_name(errno) for errno in errnos]
  551. try:
  552. yield
  553. except types as exc:
  554. if not hasattr(exc, 'errno'):
  555. raise
  556. if exc.errno not in errnos:
  557. raise