Skip to content

Commit

Permalink
fix popen hang on invoking awk in cross builds
Browse files Browse the repository at this point in the history
This mirrors the sed -> sedparse change, except that I was not able
to readily find a Python awk parser. If we can locate one, we can
revisit this and make it a little smarter.
  • Loading branch information
abathur committed Mar 28, 2024
1 parent 7cdd1a4 commit 245ff54
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.10.2 (Mar 22 2024)
- Fix some cases where resholve could fail to find a node's first span_id.
- Ongoing cross oriented fixes exposed by xdg-utils getting resholved:
- Swap out use of `awk` executable for dumb string searches and convert from an error to a warning.

## v0.10.1 (Mar 22 2024)
- Refactor to minimize cross changes. (Trying to land this in nixpkgs made me a little more confident about the best approach.)

Expand Down
72 changes: 30 additions & 42 deletions resholve
Original file line number Diff line number Diff line change
Expand Up @@ -1271,10 +1271,18 @@ class NixSetuidWrapper(Detain):
issue = 29


class AwkSubCommand(Detain):
problem = "I don't know how to handle awk sub commands yet--sorry :("
class AwkSubCommand(Census):
"""
Caution: note that this was formerly an error, but I'm backing it off
to a simple warning for now since I'm having to convert to a method
that we can't trust to accurately identify the problem cases.
"""

problem = (
"I don't know how to find and replace sub commands in awk scripts yet--sorry :("
)
ideas = ("- See the feedback issue for a workaround",)
status = 13
# status = 13
issue = 31


Expand Down Expand Up @@ -3764,6 +3772,11 @@ class RecordCommandlike(object):

def _find_awk_sub_cmd(self, expr):
"""
Note: the first draft of this behavior leveraged awk's --sandbox flag
to figure out when this behavior was present. This is good, but for
reasons I haven't yet understood the calls out were hanging during
cross builds (instead of producing binary format errors...):
$ awk --sandbox 'BEGIN { system("date"); close("date")}'
awk: cmd. line:1: fatal: 'system' function not allowed in sandbox mode
Expand All @@ -3775,50 +3788,25 @@ class RecordCommandlike(object):
close(cmd);
}'
awk: cmd. line:3: fatal: redirection not allowed in sandbox mode
Since we're having cross trouble and I haven't had much luck finding
a Python parser for the Awk language, we'll do the dumbest-plausible
thing here but then just warn about this for now.
"""
awk = lookup("awk")

if not awk:
raise Exception(
"Somehow I ended up trying to look for an awk sub command in %r when awk isn't even present. Oops! Please report this @ https://github.com/abathur/resholve",
expr,
if "system(" in expr:
logger.warning(
"awk expression may contain system command (exec): '%s'", expr
)
return True

# TODO: abstract this pattern
# close dupe in sed
p = Popen(
[awk, "--sandbox", expr],
shell=False,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
close_fds=True,
)

try:
timer = Timer(2, p.kill)
timer.start()
stdout, stderr = p.communicate(input="something cute")
finally:
if timer.is_alive():
timer.cancel()
else:
logger.warning(
"timeout after 1s waiting for awk expression test: \n%s\ncontinuing but I'm not certain it doesn't contain external commands.",
expr,
)
if " | " in expr:
logger.warning("awk expression may contain pipe (exec): '%s'", expr)
return True

if p.returncode == 2:
if "fatal: 'system' function not allowed in sandbox mode" in stderr:
return True
# test for | since normal > redirects also trigger
elif (
"fatal: redirection not allowed in sandbox mode" in stderr
and "|" in expr
):
return True
logger.debug("unhandled awk error: %r", stderr)
# TODO: maybe later we can do something smarter
if " |& " in expr:
logger.warning("awk expression may contain pipe (exec): '%s'", expr)
return True

return False

Expand Down
1 change: 1 addition & 0 deletions tests/parse_awk.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# 2 cases for issue 82
awk -F'\t' -vcategory="$1" '{ split($9, cs, "|");for (i in cs) if (cs[i] == category) { print; break; }; }'
awk -F'\t' -v category="$1" '{ split($9, cs, "|");for (i in cs) if (cs[i] == category) { print; break; }; }'
awk 'BEGIN { system("date"); close("date")}'

0 comments on commit 245ff54

Please sign in to comment.