platforms.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.platforms
  4. ~~~~~~~~~~~~~~~~
  5. Utilities dealing with platform specifics: signals, daemonization,
  6. users, groups, and so on.
  7. :copyright: (c) 2009 - 2012 by Ask Solem.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. from __future__ import absolute_import
  11. from __future__ import with_statement
  12. import atexit
  13. import errno
  14. import os
  15. import platform as _platform
  16. import shlex
  17. import signal as _signal
  18. import sys
  19. from contextlib import contextmanager
  20. from .local import try_import
  21. from billiard import current_process
  22. from kombu.utils.limits import TokenBucket
  23. _setproctitle = try_import("setproctitle")
  24. resource = try_import("resource")
  25. pwd = try_import("pwd")
  26. grp = try_import("grp")
  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. DAEMON_REDIRECT_TO = getattr(os, "devnull", "/dev/null")
  37. PIDFILE_FLAGS = os.O_CREAT | os.O_EXCL | os.O_WRONLY
  38. PIDFILE_MODE = ((os.R_OK | os.W_OK) << 6) | ((os.R_OK) << 3) | ((os.R_OK))
  39. _setps_bucket = TokenBucket(0.5) # 30/m, every 2 seconds
  40. PIDLOCKED = """ERROR: Pidfile (%s) already exists.
  41. Seems we're already running? (PID: %s)"""
  42. def pyimplementation():
  43. if hasattr(_platform, "python_implementation"):
  44. return _platform.python_implementation()
  45. elif sys.platform.startswith("java"):
  46. return "Jython " + sys.platform
  47. elif hasattr(sys, "pypy_version_info"):
  48. v = ".".join(map(str, sys.pypy_version_info[:3]))
  49. if sys.pypy_version_info[3:]:
  50. v += "-" + "".join(map(str, sys.pypy_version_info[3:]))
  51. return "PyPy " + v
  52. else:
  53. return "CPython"
  54. class LockFailed(Exception):
  55. """Raised if a pidlock can't be acquired."""
  56. pass
  57. def get_fdmax(default=None):
  58. """Returns the maximum number of open file descriptors
  59. on this system.
  60. :keyword default: Value returned if there's no file
  61. descriptor limit.
  62. """
  63. fdmax = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
  64. if fdmax == resource.RLIM_INFINITY:
  65. return default
  66. return fdmax
  67. class PIDFile(object):
  68. """PID lock file.
  69. This is the type returned by :func:`create_pidlock`.
  70. **Should not be used directly, use the :func:`create_pidlock`
  71. context instead**
  72. """
  73. #: Path to the pid lock file.
  74. path = None
  75. def __init__(self, path):
  76. self.path = os.path.abspath(path)
  77. def acquire(self):
  78. """Acquire lock."""
  79. try:
  80. self.write_pid()
  81. except OSError, exc:
  82. raise LockFailed, LockFailed(str(exc)), sys.exc_info()[2]
  83. return self
  84. __enter__ = acquire
  85. def is_locked(self):
  86. """Returns true if the pid lock exists."""
  87. return os.path.exists(self.path)
  88. def release(self, *args):
  89. """Release lock."""
  90. self.remove()
  91. __exit__ = release
  92. def read_pid(self):
  93. """Reads and returns the current pid."""
  94. try:
  95. fh = open(self.path, "r")
  96. except IOError, exc:
  97. if exc.errno == errno.ENOENT:
  98. return
  99. raise
  100. try:
  101. line = fh.readline()
  102. if line.strip() == line: # must contain '\n'
  103. raise ValueError(
  104. "Partially written or invalid pidfile %r" % (self.path))
  105. finally:
  106. fh.close()
  107. try:
  108. return int(line.strip())
  109. except ValueError:
  110. raise ValueError("PID file %r contents invalid." % self.path)
  111. def remove(self):
  112. """Removes the lock."""
  113. try:
  114. os.unlink(self.path)
  115. except OSError, exc:
  116. if exc.errno in (errno.ENOENT, errno.EACCES):
  117. return
  118. raise
  119. def remove_if_stale(self):
  120. """Removes the lock if the process is not running.
  121. (does not respond to signals)."""
  122. try:
  123. pid = self.read_pid()
  124. except ValueError, exc:
  125. sys.stderr.write("Broken pidfile found. Removing it.\n")
  126. self.remove()
  127. return True
  128. if not pid:
  129. self.remove()
  130. return True
  131. try:
  132. os.kill(pid, 0)
  133. except os.error, exc:
  134. if exc.errno == errno.ESRCH:
  135. sys.stderr.write("Stale pidfile exists. Removing it.\n")
  136. self.remove()
  137. return True
  138. return False
  139. def write_pid(self):
  140. pid = os.getpid()
  141. content = "%d\n" % (pid, )
  142. pidfile_fd = os.open(self.path, PIDFILE_FLAGS, PIDFILE_MODE)
  143. pidfile = os.fdopen(pidfile_fd, "w")
  144. try:
  145. pidfile.write(content)
  146. # flush and sync so that the re-read below works.
  147. pidfile.flush()
  148. try:
  149. os.fsync(pidfile_fd)
  150. except AttributeError: # pragma: no cover
  151. pass
  152. finally:
  153. pidfile.close()
  154. rfh = open(self.path)
  155. try:
  156. if rfh.read() != content:
  157. raise LockFailed(
  158. "Inconsistency: Pidfile content doesn't match at re-read")
  159. finally:
  160. rfh.close()
  161. def create_pidlock(pidfile):
  162. """Create and verify pid file.
  163. If the pid file already exists the program exits with an error message,
  164. however if the process it refers to is not running anymore, the pid file
  165. is deleted and the program continues.
  166. The caller is responsible for releasing the lock before the program
  167. exits.
  168. :returns: :class:`PIDFile`.
  169. **Example**:
  170. .. code-block:: python
  171. pidlock = create_pidlock("/var/run/app.pid")
  172. """
  173. pidlock = PIDFile(pidfile)
  174. if pidlock.is_locked() and not pidlock.remove_if_stale():
  175. raise SystemExit(PIDLOCKED % (pidfile, pidlock.read_pid()))
  176. pidlock.acquire()
  177. atexit.register(pidlock.release)
  178. return pidlock
  179. class DaemonContext(object):
  180. _is_open = False
  181. workdir = DAEMON_WORKDIR
  182. umask = DAEMON_UMASK
  183. def __init__(self, pidfile=None, workdir=None, umask=None,
  184. fake=False, **kwargs):
  185. self.workdir = workdir or self.workdir
  186. self.umask = self.umask if umask is None else umask
  187. self.fake = fake
  188. def open(self):
  189. if not self._is_open:
  190. if not self.fake:
  191. self._detach()
  192. os.chdir(self.workdir)
  193. os.umask(self.umask)
  194. for fd in reversed(range(get_fdmax(default=2048))):
  195. with ignore_EBADF():
  196. os.close(fd)
  197. os.open(DAEMON_REDIRECT_TO, os.O_RDWR)
  198. os.dup2(0, 1)
  199. os.dup2(0, 2)
  200. self._is_open = True
  201. __enter__ = open
  202. def close(self, *args):
  203. if self._is_open:
  204. self._is_open = False
  205. __exit__ = close
  206. def _detach(self):
  207. if os.fork() == 0: # first child
  208. os.setsid() # create new session
  209. if os.fork() > 0: # second child
  210. os._exit(0)
  211. else:
  212. os._exit(0)
  213. return self
  214. def detached(logfile=None, pidfile=None, uid=None, gid=None, umask=0,
  215. workdir=None, fake=False, **opts):
  216. """Detach the current process in the background (daemonize).
  217. :keyword logfile: Optional log file. The ability to write to this file
  218. will be verified before the process is detached.
  219. :keyword pidfile: Optional pid file. The pid file will not be created,
  220. as this is the responsibility of the child. But the process will
  221. exit if the pid lock exists and the pid written is still running.
  222. :keyword uid: Optional user id or user name to change
  223. effective privileges to.
  224. :keyword gid: Optional group id or group name to change effective
  225. privileges to.
  226. :keyword umask: Optional umask that will be effective in the child process.
  227. :keyword workdir: Optional new working directory.
  228. :keyword fake: Don't actually detach, intented for debugging purposes.
  229. :keyword \*\*opts: Ignored.
  230. **Example**:
  231. .. code-block:: python
  232. import atexit
  233. from celery.platforms import detached, create_pidlock
  234. with detached(logfile="/var/log/app.log", pidfile="/var/run/app.pid",
  235. uid="nobody"):
  236. # Now in detached child process with effective user set to nobody,
  237. # and we know that our logfile can be written to, and that
  238. # the pidfile is not locked.
  239. pidlock = create_pidlock("/var/run/app.pid").acquire()
  240. atexit.register(pidlock.release)
  241. # Run the program
  242. program.run(logfile="/var/log/app.log")
  243. """
  244. if not resource:
  245. raise RuntimeError("This platform does not support detach.")
  246. workdir = os.getcwd() if workdir is None else workdir
  247. signals.reset("SIGCLD") # Make sure SIGCLD is using the default handler.
  248. if not os.geteuid():
  249. # no point trying to setuid unless we're root.
  250. maybe_drop_privileges(uid=uid, gid=gid)
  251. # Since without stderr any errors will be silently suppressed,
  252. # we need to know that we have access to the logfile.
  253. logfile and open(logfile, "a").close()
  254. # Doesn't actually create the pidfile, but makes sure it's not stale.
  255. pidfile and create_pidlock(pidfile)
  256. return DaemonContext(umask=umask, workdir=workdir, fake=fake)
  257. def parse_uid(uid):
  258. """Parse user id.
  259. uid can be an integer (uid) or a string (user name), if a user name
  260. the uid is taken from the password file.
  261. """
  262. try:
  263. return int(uid)
  264. except ValueError:
  265. try:
  266. return pwd.getpwnam(uid).pw_uid
  267. except (AttributeError, KeyError):
  268. raise KeyError("User does not exist: %r" % (uid, ))
  269. def parse_gid(gid):
  270. """Parse group id.
  271. gid can be an integer (gid) or a string (group name), if a group name
  272. the gid is taken from the password file.
  273. """
  274. try:
  275. return int(gid)
  276. except ValueError:
  277. try:
  278. return grp.getgrnam(gid).gr_gid
  279. except (AttributeError, KeyError):
  280. raise KeyError("Group does not exist: %r" % (gid, ))
  281. def _setgroups_hack(groups):
  282. """:fun:`setgroups` may have a platform-dependent limit,
  283. and it is not always possible to know in advance what this limit
  284. is, so we use this ugly hack stolen from glibc."""
  285. groups = groups[:]
  286. while 1:
  287. try:
  288. return os.setgroups(groups)
  289. except ValueError: # error from Python's check.
  290. if len(groups) <= 1:
  291. raise
  292. groups[:] = groups[:-1]
  293. except OSError, exc: # error from the OS.
  294. if exc.errno != errno.EINVAL or len(groups) <= 1:
  295. raise
  296. groups[:] = groups[:-1]
  297. def setgroups(groups):
  298. max_groups = None
  299. try:
  300. max_groups = os.sysconf("SC_NGROUPS_MAX")
  301. except Exception:
  302. pass
  303. try:
  304. return _setgroups_hack(groups[:max_groups])
  305. except OSError, exc:
  306. if exc.errno != errno.EPERM:
  307. raise
  308. if any(group not in groups for group in os.getgroups()):
  309. # we shouldn't be allowed to change to this group.
  310. raise
  311. def initgroups(uid, gid):
  312. if not pwd: # pragma: no cover
  313. return
  314. username = pwd.getpwuid(uid)[0]
  315. if hasattr(os, "initgroups"): # Python 2.7+
  316. return os.initgroups(username, gid)
  317. groups = [gr.gr_gid for gr in grp.getgrall()
  318. if username in gr.gr_mem]
  319. setgroups(groups)
  320. def setegid(gid):
  321. """Set effective group id."""
  322. gid = parse_gid(gid)
  323. if gid != os.getegid():
  324. os.setegid(gid)
  325. def seteuid(uid):
  326. """Set effective user id."""
  327. uid = parse_uid(uid)
  328. if uid != os.geteuid():
  329. os.seteuid(uid)
  330. def setgid(gid):
  331. os.setgid(parse_gid(gid))
  332. def setuid(uid):
  333. os.setuid(parse_uid(uid))
  334. def maybe_drop_privileges(uid=None, gid=None):
  335. """Change process privileges to new user/group.
  336. If UID and GID is specified, the real user/group is changed.
  337. If only UID is specified, the real user is changed, and the group is
  338. changed to the users primary group.
  339. If only GID is specified, only the group is changed.
  340. """
  341. uid = uid and parse_uid(uid)
  342. gid = gid and parse_gid(gid)
  343. if uid:
  344. # If GID isn't defined, get the primary GID of the user.
  345. if not gid and pwd:
  346. gid = pwd.getpwuid(uid).pw_gid
  347. # Must set the GID before initgroups(), as setgid()
  348. # is known to zap the group list on some platforms.
  349. setgid(gid)
  350. initgroups(uid, gid)
  351. # at last:
  352. setuid(uid)
  353. else:
  354. gid and setgid(gid)
  355. class Signals(object):
  356. """Convenience interface to :mod:`signals`.
  357. If the requested signal is not supported on the current platform,
  358. the operation will be ignored.
  359. **Examples**:
  360. .. code-block:: python
  361. >>> from celery.platforms import signals
  362. >>> signals["INT"] = my_handler
  363. >>> signals["INT"]
  364. my_handler
  365. >>> signals.supported("INT")
  366. True
  367. >>> signals.signum("INT")
  368. 2
  369. >>> signals.ignore("USR1")
  370. >>> signals["USR1"] == signals.ignored
  371. True
  372. >>> signals.reset("USR1")
  373. >>> signals["USR1"] == signals.default
  374. True
  375. >>> signals.update(INT=exit_handler,
  376. ... TERM=exit_handler,
  377. ... HUP=hup_handler)
  378. """
  379. ignored = _signal.SIG_IGN
  380. default = _signal.SIG_DFL
  381. def supported(self, signal_name):
  382. """Returns true value if ``signal_name`` exists on this platform."""
  383. try:
  384. return self.signum(signal_name)
  385. except AttributeError:
  386. pass
  387. def signum(self, signal_name):
  388. """Get signal number from signal name."""
  389. if isinstance(signal_name, int):
  390. return signal_name
  391. if not isinstance(signal_name, basestring) \
  392. or not signal_name.isupper():
  393. raise TypeError("signal name must be uppercase string.")
  394. if not signal_name.startswith("SIG"):
  395. signal_name = "SIG" + signal_name
  396. return getattr(_signal, signal_name)
  397. def reset(self, *signal_names):
  398. """Reset signals to the default signal handler.
  399. Does nothing if the platform doesn't support signals,
  400. or the specified signal in particular.
  401. """
  402. self.update((sig, self.default) for sig in signal_names)
  403. def ignore(self, *signal_names):
  404. """Ignore signal using :const:`SIG_IGN`.
  405. Does nothing if the platform doesn't support signals,
  406. or the specified signal in particular.
  407. """
  408. self.update((sig, self.ignored) for sig in signal_names)
  409. def __getitem__(self, signal_name):
  410. return _signal.getsignal(self.signum(signal_name))
  411. def __setitem__(self, signal_name, handler):
  412. """Install signal handler.
  413. Does nothing if the current platform doesn't support signals,
  414. or the specified signal in particular.
  415. """
  416. try:
  417. _signal.signal(self.signum(signal_name), handler)
  418. except (AttributeError, ValueError):
  419. pass
  420. def update(self, _d_=None, **sigmap):
  421. """Set signal handlers from a mapping."""
  422. for signal_name, handler in dict(_d_ or {}, **sigmap).iteritems():
  423. self[signal_name] = handler
  424. signals = Signals()
  425. get_signal = signals.signum # compat
  426. install_signal_handler = signals.__setitem__ # compat
  427. reset_signal = signals.reset # compat
  428. ignore_signal = signals.ignore # compat
  429. def strargv(argv):
  430. arg_start = 2 if "manage" in argv[0] else 1
  431. if len(argv) > arg_start:
  432. return " ".join(argv[arg_start:])
  433. return ""
  434. def set_process_title(progname, info=None):
  435. """Set the ps name for the currently running process.
  436. Only works if :mod:`setproctitle` is installed.
  437. """
  438. proctitle = "[%s]" % progname
  439. proctitle = "%s %s" % (proctitle, info) if info else proctitle
  440. if _setproctitle:
  441. _setproctitle.setproctitle(proctitle)
  442. return proctitle
  443. if os.environ.get("NOSETPS"): # pragma: no cover
  444. def set_mp_process_title(*a, **k):
  445. pass
  446. else:
  447. def set_mp_process_title(progname, info=None, hostname=None, # noqa
  448. rate_limit=False):
  449. """Set the ps name using the multiprocessing process name.
  450. Only works if :mod:`setproctitle` is installed.
  451. """
  452. if not rate_limit or _setps_bucket.can_consume(1):
  453. if hostname:
  454. progname = "%s@%s" % (progname, hostname.split(".")[0])
  455. return set_process_title(
  456. "%s:%s" % (progname, current_process().name), info=info)
  457. def shellsplit(s, posix=True):
  458. # posix= option to shlex.split first available in Python 2.6+
  459. lexer = shlex.shlex(s, posix=not IS_WINDOWS)
  460. lexer.whitespace_split = True
  461. lexer.commenters = ''
  462. return list(lexer)
  463. @contextmanager
  464. def ignore_EBADF():
  465. try:
  466. yield
  467. except OSError, exc:
  468. if exc.errno != errno.EBADF:
  469. raise