Skip to content

Commit

Permalink
Fix: pass env to child process explicitly (bsc#1205925)
Browse files Browse the repository at this point in the history
A bug in python stardard library, python/cpython#100516, make
environment variables, `COLUMNS` and `ROWS` to be added at fork. These
environment variables may make some of the commands called by crmsh to
truncate its output even if those commands are not writing to the
terminal.

Passing argument `env` explicitly is a workaround for the python bug.
  • Loading branch information
nicholasyang2022 committed Mar 13, 2024
1 parent ad10dad commit 5b35eac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
10 changes: 8 additions & 2 deletions crmsh/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,9 @@ def _check_control_persist():
print((".EXT", cmd))
cmd = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
(out, err) = cmd.communicate()
return "Bad configuration option" not in err

Expand Down Expand Up @@ -1138,7 +1140,11 @@ def _cleanup_local(workdir):
if workdir and os.path.isdir(workdir):
cleanscript = os.path.join(workdir, 'crm_clean.py')
if os.path.isfile(cleanscript):
if subprocess.call([cleanscript, workdir], shell=False) != 0:
if subprocess.call(
[cleanscript, workdir],
shell=False,
env=os.environ, # bsc#1205925
) != 0:
shutil.rmtree(workdir)
else:
shutil.rmtree(workdir)
Expand Down
11 changes: 7 additions & 4 deletions crmsh/ui_node.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2008-2011 Dejan Muhamedagic <[email protected]>
# Copyright (C) 2013 Kristoffer Gronlund <[email protected]>
# See COPYING for license information.

import os
import re
import copy
import subprocess
Expand Down Expand Up @@ -550,10 +550,13 @@ def do_delete(self, context, node):
'usage: delete <node>'
logger.warning('`crm node delete` is deprecated and will very likely be dropped in the near future. It is auto-replaced as `crm cluster remove -c {}`.'.format(node))
if config.core.force:
rc = subprocess.call(['crm', 'cluster', 'remove', '-F', '-c', node])
args = ['crm', 'cluster', 'remove', '-F', '-c', node]
else:
rc = subprocess.call(['crm', 'cluster', 'remove', '-c', node])
return rc == 0
args = ['crm', 'cluster', 'remove', '-c', node]
return 0 == subprocess.call(
args,
env=os.environ, # bsc#1205925
)

@command.wait
@command.completers(compl.nodes, compl.choice(['set', 'delete', 'show']), _find_attr)
Expand Down
31 changes: 25 additions & 6 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,12 @@ def pipe_string(cmd, s):
logger.debug("piping string to %s", cmd)
if options.regression_tests:
print(".EXT", cmd)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
p = subprocess.Popen(
cmd,
shell=True,
stdin=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
try:
# communicate() expects encoded bytes
if isinstance(s, str):
Expand Down Expand Up @@ -552,7 +557,9 @@ def filter_string(cmd, s, stderr_on=True, shell=True):
shell=shell,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=stderr)
stderr=stderr,
env=os.environ, # bsc#1205925
)
try:
# bytes expected here
if isinstance(s, str):
Expand Down Expand Up @@ -770,7 +777,9 @@ def show_dot_graph(dotfile, keep_file=False, desc="transition graph"):
if options.regression_tests:
print(".EXT", cmd)
subprocess.Popen(cmd, shell=True, bufsize=0,
stdin=None, stdout=None, stderr=None, close_fds=True)
stdin=None, stdout=None, stderr=None, close_fds=True,
env=os.environ, # bsc#1205925
)
logger.info("starting %s to show %s", config.core.dotty, desc)


Expand All @@ -779,13 +788,21 @@ def ext_cmd(cmd, shell=True):
if options.regression_tests:
print(".EXT", cmd)
logger.debug("invoke: %s", cmd)
return subprocess.call(cmd, shell=shell)
return subprocess.call(
cmd,
shell=shell,
env=os.environ, # bsc#1205925
)


def ext_cmd_nosudo(cmd, shell=True):
if options.regression_tests:
print(".EXT", cmd)
return subprocess.call(cmd, shell=shell)
return subprocess.call(
cmd,
shell=shell,
env=os.environ, # bsc#1205925
)


def rmdir_r(d):
Expand Down Expand Up @@ -918,7 +935,9 @@ def pipe_cmd_nosudo(cmd):
proc = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
(outp, err_outp) = proc.communicate()
proc.wait()
rc = proc.returncode
Expand Down
6 changes: 5 additions & 1 deletion crmsh/xmlutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ def sudocall(cmd):
cmd = add_sudo(cmd)
if options.regression_tests:
print(".EXT", cmd)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = subprocess.Popen(
cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
try:
outp, errp = p.communicate()
p.wait()
Expand Down

0 comments on commit 5b35eac

Please sign in to comment.