mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #54237 from dwoz/dup2
Do not dup2 things without fileno
This commit is contained in:
commit
268803b095
2 changed files with 44 additions and 6 deletions
|
@ -6,6 +6,7 @@ Functions for daemonizing and otherwise modifying running processes
|
|||
# Import python libs
|
||||
from __future__ import absolute_import, with_statement, print_function, unicode_literals
|
||||
import copy
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
@ -102,12 +103,32 @@ def daemonize(redirect_out=True):
|
|||
with salt.utils.files.fopen('/dev/null', 'r+') as dev_null:
|
||||
# Redirect python stdin/out/err
|
||||
# and the os stdin/out/err which can be different
|
||||
os.dup2(dev_null.fileno(), sys.stdin.fileno())
|
||||
os.dup2(dev_null.fileno(), sys.stdout.fileno())
|
||||
os.dup2(dev_null.fileno(), sys.stderr.fileno())
|
||||
os.dup2(dev_null.fileno(), 0)
|
||||
os.dup2(dev_null.fileno(), 1)
|
||||
os.dup2(dev_null.fileno(), 2)
|
||||
dup2(dev_null, sys.stdin)
|
||||
dup2(dev_null, sys.stdout)
|
||||
dup2(dev_null, sys.stderr)
|
||||
dup2(dev_null, 0)
|
||||
dup2(dev_null, 1)
|
||||
dup2(dev_null, 2)
|
||||
|
||||
|
||||
def dup2(file1, file2):
|
||||
if isinstance(file1, int):
|
||||
fno1 = file1
|
||||
else:
|
||||
try:
|
||||
fno1 = file1.fileno()
|
||||
except io.UnsupportedOperation:
|
||||
log.warn('Unsupported operation on file: %r', file1)
|
||||
return
|
||||
if isinstance(file2, int):
|
||||
fno2 = file2
|
||||
else:
|
||||
try:
|
||||
fno2 = file2.fileno()
|
||||
except io.UnsupportedOperation:
|
||||
log.warn('Unsupported operation on file: %r', file2)
|
||||
return
|
||||
os.dup2(fno1, fno2)
|
||||
|
||||
|
||||
def daemonize_if(opts):
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
# Import python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
@ -399,3 +400,19 @@ class TestSignalHandlingMultiprocessingProcess(TestCase):
|
|||
evt.set()
|
||||
proc2.join(30)
|
||||
proc.join(30)
|
||||
|
||||
|
||||
class TestDup2(TestCase):
|
||||
|
||||
def test_dup2_no_fileno(self):
|
||||
'The dup2 method does not fail on streams without fileno support'
|
||||
f1 = io.StringIO("some initial text data")
|
||||
f2 = io.StringIO("some initial other text data")
|
||||
with self.assertRaises(io.UnsupportedOperation):
|
||||
f1.fileno()
|
||||
with patch('os.dup2') as dup_mock:
|
||||
try:
|
||||
salt.utils.process.dup2(f1, f2)
|
||||
except io.UnsupportedOperation:
|
||||
assert False, 'io.UnsupportedOperation was raised'
|
||||
assert not dup_mock.called
|
||||
|
|
Loading…
Add table
Reference in a new issue