Skip to content

Commit

Permalink
Add --null flag to qubesd-query
Browse files Browse the repository at this point in the history
This allows avoiding a spurious call to printf(1).
  • Loading branch information
DemiMarie committed Dec 20, 2022
1 parent dd058ec commit f8835ea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
5 changes: 5 additions & 0 deletions doc/manpages/qubesd-query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ Options
is not passed, and 1024 otherwise. This option can be used to lower this
value, though not raise it.

.. option:: --null

bash's ``read -r -d ''`` expects that the string being read is terminated by
NUL byte. This option causes qubesd-query to add one to qubesd's response.

Description
-----------

Expand Down
13 changes: 5 additions & 8 deletions qubes-rpc/admin.vm.volume.Import
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ admin.vm.volume.Import) flags=(--empty);;
*) printf 'Invoked with unknown basename %q, cannot continue\n' "${0##*/}" >&2; exit 1;;
esac

{
qubesd-query "${flags[@]}" -c /var/run/qubesd.internal.sock \
"$QREXEC_REMOTE_DOMAIN" \
"internal.vm.volume.ImportBegin" \
"$QREXEC_REQUESTED_TARGET" \
"$1"
printf \\0
} >"$tmpfile"
qubesd-query --null "${flags[@]}" -c /var/run/qubesd.internal.sock \
"$QREXEC_REMOTE_DOMAIN" \
"internal.vm.volume.ImportBegin" \
"$QREXEC_REQUESTED_TARGET" \
"$1" >"$tmpfile"

{
# Read response from qubesd
Expand Down
8 changes: 8 additions & 0 deletions qubes/tests/rpc_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class TestRpcImport(qubes.tests.QubesTestCase):

QUBESD_QUERY = '''\
#!/bin/sh -e
unset null
null=false
if [ "$1" = --null ]; then
shift
null=true
fi
if [ "$1" = --single-line ] && [ "$2" = --max-bytes=21 ]; then
shift 2
Expand All @@ -54,6 +61,7 @@ class TestRpcImport(qubes.tests.QubesTestCase):
fi
echo "$@" > "command-$method"
cat -- "response-$method"
if [ "$null" = true ]; then printf '\\0'; fi
'''

RPC_FILE_PATH = os.path.abspath(os.path.join(
Expand Down
7 changes: 7 additions & 0 deletions qubes/tools/qubesd_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
action='store_true',
default=False,
help='Only read a single line of input')
parser.add_argument('--null',
dest='null',
action='store_true',
default=False,
help="Print an extra NUL byte; useful for shell scripts")

def sighandler(loop, signame, coro):
print('caught {}, exiting'.format(signame))
Expand Down Expand Up @@ -157,6 +162,8 @@ def main(args=None):

if args.fail:
return returncode
if args.null:
sys.stdout.buffer.write(b'\0')
return 0

if __name__ == '__main__':
Expand Down

0 comments on commit f8835ea

Please sign in to comment.