| 16 | class WorkerTmp: |
| 17 | |
| 18 | def __init__(self, cfg): |
| 19 | old_umask = os.umask(cfg.umask) |
| 20 | fdir = cfg.worker_tmp_dir |
| 21 | if fdir and not os.path.isdir(fdir): |
| 22 | raise RuntimeError("%s doesn't exist. Can't create workertmp." % fdir) |
| 23 | fd, name = tempfile.mkstemp(prefix="wgunicorn-", dir=fdir) |
| 24 | os.umask(old_umask) |
| 25 | |
| 26 | # change the owner and group of the file if the worker will run as |
| 27 | # a different user or group, so that the worker can modify the file |
| 28 | if cfg.uid != os.geteuid() or cfg.gid != os.getegid(): |
| 29 | util.chown(name, cfg.uid, cfg.gid) |
| 30 | |
| 31 | # unlink the file so we don't leak temporary files |
| 32 | try: |
| 33 | if not IS_CYGWIN: |
| 34 | util.unlink(name) |
| 35 | # In Python 3.8, open() emits RuntimeWarning if buffering=1 for binary mode. |
| 36 | # Because we never write to this file, pass 0 to switch buffering off. |
| 37 | self._tmp = os.fdopen(fd, 'w+b', 0) |
| 38 | except Exception: |
| 39 | os.close(fd) |
| 40 | raise |
| 41 | |
| 42 | def notify(self): |
| 43 | new_time = time.monotonic() |