Commit Graph

207 Commits

Author SHA1 Message Date
Michal Privoznik
d3b2d5158a lib: Substitute some STREQLEN with STRPREFIX
There are few cases where STREQLEN() is called like this:

  STREQLEN(var, string, strlen(string))

which is the same as STRPREFIX(var, string). Use STRPREFIX()
because it is more obvious what the check is doing.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
2021-02-10 11:51:59 +01:00
Laine Stump
6b1595317c tools: replace VIR_FREE with g_free in all vir*Free() functions
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-02-05 00:20:45 -05:00
Michal Privoznik
aad2262b9e vsh: Allow double quotes imbalance for auto completion in vshCommandStringGetArg()
If user is trying to auto complete a value that contains a space,
they have two options: use backslash to escape space or use
quotes, like this:

  virsh # start --domain "domain with space<TAB>

However, in this case our tokenizer sees imbalance in (double)
quotes: there is a starting one that's missing its companion.
Well, that's obvious - user is still in process of writing the
command. What we need to do in this case is to ignore the
imbalance and return success (from the tokenizer) - readline will
handle closing the quote properly.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
22904b5702 vsh: Rework how option to complete is found
The way that auto completion works currently is that user's input
is parsed, and then we try to find the first --option (in the
parsed structure) that has the same value as user's input around
where <TAB> was pressed. For instance, for the following input:

  virsh # command --arg1 hello --arg2 world<TAB>

we will see "world" as text that user is trying to autocomplete
(this is affected by rl_basic_word_break_characters which
readline uses internally to break user's input into individual
words) and find that it is --arg2 that user is trying to
autocomplete. So far so good, for this naive approach. But
consider the following example:

  virsh # command --arg1 world --arg2 world<TAB>

Here, both arguments have the same value and because we see
"world" as text that user is trying to autocomplete we would
think that it is --arg1 that user wants to autocomplete. This is
obviously wrong.

Fortunately, readline stores the current position of cursor (into
rl_point) and we can use that when parsing user's input: whenever
we reach a position that matches the cursor then we know that
that is the place where <TAB> was pressed and hence that is the
--option that user wants to autocomplete. Readline stores the
cursor position as offset (numbered from 1) from the beginning of
user's input. We store this input into @parser->pos initially,
but then advance it as we tokenize it. Therefore, what we need is
to store the original position too.

Thanks to Martin who helped me with this.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
f61a4e91ef vshReadlineParse: Escape list of candidates earlier
The way our completer callbacks work is that they return all
possible candidates and then vshCompleterFilter() is called to
prune the list of all candidates removing those which don't match
user's input. This allows us to have simpler completer callbacks
as their only job is to fetch all possible candidates.

Anyway, if the completion candidate we're returning contains a
space, it has to be escaped (shell like escaping), unless there
is already a quote character (single quote or double quote).

But ordering is critical. Completer callback returns string
without any escaping, but the filter function sees the user input
escaped. For instance, if user's input is "domain with
space<TAB>" then the filtering function gets "domain\ with\
space" as user's input but completer returns "domain with space".
Since these two strings don't match the filtering function
removes this candidate from the list. What we need to do is to
escape strings before calling the filtering function. This way,
the filtering function will see two same strings.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
c31e80c653 vshReadlineParse: Rename @buf to @line
In next commit the block that does escaping of returned string
will be brought into this block. But both contain variable @buf
and use it in different contexts. Rename @buf from @state == 0
block to @line which reflects its purpose better.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
100052135f vshReadlineParse: Use g_auto*()
Instead of freeing @partial and @buf explicitly, we can use
g_auto*() to do that automatically.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
e19ecee54c vshReadlineParse: Bring some variables into !state block
On readline completion vshReadlineCompletion() is called which
does nothing more than calling rl_completion_matches() with
vshReadlineParse() as a callback. This means, that
vshReadlineParse() is called repeatedly, each time returning next
completion candidate, until it returns NULL which is interpreted
as the end of the list of candidates.

The function takes two parameters: @text which is a portion of
input line around cursor when TAB was pressed, and @state. The
@state is an integer that is zero on the very first call and
non-zero on each subsequent call (in fact, readline does @state++
on each call).

Anyway, the idea is that the callback gets the whole list of
candidates on @state == 0 and returns one candidate at each call.
And this is what vshReadlineParse() is doing but some variables
(@partial, @cmd and @opt) are really used only in the @state == 0
case but declared for whole function. We can limit their scope by
declaring them inside the @state == 0 body which also means that
they don't have to be static anymore.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
df9f1efc53 vsh: Don't break word on backslash
A backslash is the way we escape characters in virsh. For
instance:

  virsh # start domain\ with\ long\ name

For readline completion, we do not want to get four separate
words ("domain", "with", "long", "name").  This means, that we
can't use virBufferEscapeShell() because it doesn't escape spaces
the way we want.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Michal Privoznik
9b9542586c vshCommandStringGetArg: Drop @sz
This variable is unused since introduction of the function in
v0.8.5~150.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2021-01-26 16:46:41 +01:00
Peter Krempa
a1ed7f37ce tools: vshCmddefOptParse: Remove return value
The function can't fail so there's no need to return a value or check it
in the callers.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:30 +01:00
Peter Krempa
fde5b4cf9d tools: vshCmddefHelp: Don't call vshCmddefOptParse
The help formatter called vshCmddefOptParse just for validation
purposes. Since vshCmddefOptParse no longer validates the command itself
and we don't need the bitmaps returned by it we can drop the call
entirely.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:30 +01:00
Peter Krempa
cd0602290a tools: vshCmddefOptParse: Remove 'optional' command validation
Since vshCmddefCheckInternals now has this check we no longer need it in
vshCmddefOptParse.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:29 +01:00
Peter Krempa
0d00e95298 tools: cmdSelfTest: Drop misleading comment
We no longer print help for every command to validate the args.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:29 +01:00
Peter Krempa
fa7265e127 tools: vshCmddefCheckInternals: Port mandatory options check from vshCmddefOptParse
'vshCmddefCheckInternals' is the go-to place for all checks related to
the definition of parameters for commands, but the check that all
mandatory parameters must be ordered before optional parameters was
still only in vshCmddefOptParse.

Adding a non-compliant option would not be caught by our test suite as
'virsh self-test' doesn't call vshCmddefOptParse.

Re-implement the check in vshCmddefCheckInternals.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:29 +01:00
Peter Krempa
b070332261 tools: vshCmddefCheckInternals: Add parameter name to error message
If a parameter definition is invalid we can include the name of the
parameter for simpler debugging.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:29 +01:00
Peter Krempa
f2a602f5a3 tools: vsh: Unexport vshCmddefHelp
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-11-13 11:12:29 +01:00
Ján Tomko
1647e376e8 virsh: delete vsh[CM]alloc
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2020-10-06 09:01:46 +02:00
Ján Tomko
504913bf23 virsh: use g_new0 instead of vsh[CM]alloc
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2020-10-06 09:01:46 +02:00
Ján Tomko
157b17f706 tools: use g_new0 instead of VIR_ALLOC*
With the exception of vsh*alloc.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-10-01 12:34:13 +02:00
Lin Ma
d9a320bf97 vshReadlineParse: Ignore vshReadlineOptionsGenerator for VSH_OT_INT options
Commit c7151b0 added the completion for VSH_OT_INT options, say '--cellno'
and '--pagesize', So we need to ignore VSH_OT_INT otherwise we get the
incorrect completion.

before:
 # virsh freepages --pagesize <TAB><TAB>
--all     --cellno  1GiB      2MiB      4KiB

after:
 # virsh freepages --pagesize <TAB><TAB>
1GiB  2MiB  4KiB

Signed-off-by: Lin Ma <lma@suse.de>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-09-14 15:52:40 +02:00
Tim Wiederhake
b668054209 tools: Use glib memory functions in vshCompleterFilter
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
2020-09-11 18:19:59 +02:00
Daniel P. Berrangé
7bd8621291 tools: avoid unused parameter warning when readline is disabled
The vshReadlineHistoryAdd stub method does not use its parameter.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-09-04 10:17:30 +01:00
Michal Privoznik
4bb1a0e488 vsh: Define HAVE_STDARG_H before including readline
As it turned out my previous commits which switched from HAVE_ to
WITH_ and dropped stdarg.h detection were a bit too aggressive.
Because of reasons described in 9ea3424a17 we need to define
HAVE_STDARG_H before including readline otherwise macos build
fails. Honestly, I still don't fully understand the problem so I
am not going to bother you with "explanation".

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-09-03 16:25:43 +02:00
Michal Privoznik
f123412b53 vsh: Introduce wrapper for add_history()
This allows us to drop include of readline header files from
virsh.c and virt-admin.c because they needed it only because of
the add_history() function.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-09-03 16:25:43 +02:00
Boris Fiuczynski
e2bd2af6e4 tools: avoid potential null pointer dereference by GCC 10
GCC 10 complains about "arg" possibly being a NULL dereference.
Even though it might be a false positive, we can easily avoid it.

Avoiding
 ../tools/vsh.c: In function ‘vshCommandOptStringReq’:
 ../tools/vsh.c:1034:19: error: potential null pointer dereference [-Werror=null-dereference]
  1034 |     else if (!*arg->data && !(arg->def->flags & VSH_OFLAG_EMPTY_OK))
       |                ~~~^~~~~~

Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2020-08-17 08:25:28 +02:00
Ján Tomko
8003fe0361 util: recommend g_strfreev instead of virStringListFree
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
2020-08-03 15:37:36 +02:00
Ján Tomko
ee247e1d3f Use g_strfeev instead of virStringFreeList
Both accept a NULL value gracefully and virStringFreeList
does not zero the pointer afterwards, so a straight replace
is safe.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
2020-08-03 15:37:36 +02:00
Ján Tomko
24b2f96a41 tools: remove unnecessary includes
After the split of virsh to multiple files, and the subsequent
split to vsh/virt-admin, there are quite a few leftovers.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
2020-08-03 15:30:40 +02:00
Pavel Hrdina
943fe685a2 meson: drop default_editor configure argument
Most likely rarely changed with configure option and it is used only
as fallback if there is no VISUAL or EDITOR environment variable.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Neal Gompa <ngompa13@gmail.com>
2020-08-03 09:26:28 +02:00
Daniel P. Berrangé
ec55001810 tools: be more paranoid about possibly NULL description
GCC 10 complains about "desc" possibly being a NULL dereference. Even
though it is a false positive, we can easily avoid it.

Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-07-23 14:58:24 +01:00
Laine Stump
ecc4ee2c42 tools: use g_auto() for all virBuffers
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2020-07-08 16:34:13 -04:00
Ján Tomko
3757302243 tools: vsh.c: remove virstrerror.h include
This was only used to pull in virStrerror.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-03-13 17:26:55 +01:00
Ján Tomko
b0eea635b3 Use g_strerror instead of virStrerror
Remove lots of stack-allocated buffers.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-03-13 17:26:55 +01:00
Ján Tomko
7e0d11be5b virsh: include virutil.h where used
Include virutil.h in all files that use it,
instead of relying on it being pulled in somehow.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2020-02-24 23:15:50 +01:00
Daniel P. Berrangé
c7aac54bc1 tools: conditionalize use of O_SYNC flag
The O_SYNC flag is not defined on Windows platforms.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-02-04 14:00:45 +00:00
Daniel P. Berrangé
cc46e137eb src: convert code to use virPipe APIs
This addreses portability to Windows and standardizes
error reporting. This fixes a number of places which
failed to set O_CLOEXEC or failed to report errors.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-02-04 14:00:44 +00:00
Ján Tomko
0a125c7144 virBufferTrim: do not accept len
Always trim the full specified suffix.

All of the callers outside of tests were passing either
strlen or the actual length of the string.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
2020-02-03 19:44:38 +01:00
Ján Tomko
4838e8b3fc Use virBufferTrimLen when applicable
Replace all the cases that only supply the length
and do not care about matching a suffix, as well
as that one test case that does.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
2020-02-03 19:44:38 +01:00
Daniel P. Berrangé
ff627b4726 rpc: conditionalize signal handling
The Windows platform does not have the signal handling
support we need, so it must be disabled in several parts
of the codebase.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-01-29 14:51:40 +00:00
Daniel P. Berrangé
3caa28dc50 src: replace gmtime_r/localtime_r/strftime with GDateTime
gmtime_r/localtime_r are mostly used in combination with
strftime to format timestamps in libvirt. This can all
be replaced with GDateTime resulting in simpler code
that is also more portable.

There is some boundary condition problem in parsing POSIX
timezone offsets in GLib which tickles our test suite.
The test suite is hacked to avoid the problem. The upsteam
GLib bug report is

  https://gitlab.gnome.org/GNOME/glib/issues/1999

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-01-17 10:02:01 +00:00
Daniel P. Berrangé
7c828af858 src: conditionally exclude cfmakeraw/termios.h on WIN32
The GNULIB termios module ensures termios.h exists (but
is none the less empty) when building for Windows. We
already exclude usage of the functions that would exist
in a real termios.h, so having an empty termios.h is
not especially useful.

It is simpler to just put all use of termios.h related
functions behind a "#ifndef WIN32" conditional.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-01-17 10:02:01 +00:00
Daniel P. Berrangé
0b4598b183 src: replace getcwd() with g_get_current_dir()
commandhelper.c is not converted since this is a standalone
program only run on UNIX, so can rely on getcwd().

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-01-03 15:42:13 +00:00
Daniel P. Berrangé
f5e9bdb87f src: replace clock_gettime()/gettimeofday() with g_get_real_time()
g_get_real_time() returns the time since epoch in microseconds.
It uses gettimeofday() internally while libvirt used clock_gettime
because it is declared async signal safe. In practice gettimeofday
is also async signal safe *provided* the timezone parameter is
NULL. This is indeed the case in g_get_real_time().

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2020-01-03 15:42:13 +00:00
Fabiano Fidêncio
bee366a9ca tools: Don't check the output of virGetUserCacheDirectory()
virGetUserCacheDirectory() *never* *ever* returns NULL, making the
checks for it completely unnecessary.

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-12-20 09:38:43 +01:00
Fabiano Fidêncio
dd478cc538 tools: Use g_autofree in cmdCd()
Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-12-20 09:38:43 +01:00
Ján Tomko
7318bff551 Remove VIR_STRNDUP usage with checked pointers
Remove the usage where sanity of the length argument is verified
by other conditions not matching the previous patches.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-12-12 14:24:34 +01:00
Ján Tomko
997340588b vsh: remove vshErrorOOM
We abort on allocation errors now so there is no need to
have a function for it.

Replace the only use by return -1, chosen by fair dice roll.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-12-12 14:24:34 +01:00
Pavel Hrdina
cc0e7541ad use g_ascii_tolower instead of c_tolower from gnulib
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Cole Robinson <crobinso@redhat.com>
2019-12-10 13:49:24 +01:00
Pavel Hrdina
c275ea93db use g_ascii_isalnum instead of c_isalnum from gnulib
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Cole Robinson <crobinso@redhat.com>
2019-12-10 13:49:23 +01:00
Ján Tomko
af5aa266ed g_mkstemp_full: pass O_RDWR
This flag is not implied by g_mkstemp_full, only by g_mkstemp.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reported-by: Bjoern Walk <bwalk@linux.ibm.com>
Fixes: 4ac4773040
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
2019-11-18 18:49:02 +01:00
Pavel Hrdina
43b01ef2d6 replace use of gnulib snprintf by g_snprintf
Glib implementation follows the ISO C99 standard so it's safe to replace
the gnulib implementation.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
2019-11-15 15:07:40 +01:00
Ján Tomko
4ac4773040 Use g_mkstemp_full instead of mkostemp(s)
With g_mkstemp_full, there is no need to distinguish between
mkostemp and mkostemps (no suffix vs. a suffix of a fixed length),
because the GLib function looks for the XXXXXX pattern everywhere
in the string.

Use S_IRUSR | S_IWUSR for the permissions and do not pass O_RDWR
in flags since it's implied.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
2019-11-14 19:02:31 +01:00
Daniel Henrique Barboza
10d6290f1c tools: remove unneeded cleanup labels
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-11-12 17:54:01 +01:00
Michal Privoznik
8eaa708991 Use g_strdup_vprintf() instead of virVasprintf() everywhere
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-11-12 16:15:59 +01:00
Michal Privoznik
26a137093b tools: Use g_strdup_printf() instead of virAsprintf()
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-11-12 16:15:59 +01:00
Peter Krempa
0967708b81 util: buffer: Remove virBufferCheckError
The function now does not return an error so we can drop it fully.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-10-24 19:35:34 +02:00
Peter Krempa
205d6a2af7 util: buffer: Remove virBufferError
The function now does not return an error so we can drop it fully.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-10-24 19:35:34 +02:00
Peter Krempa
b2814b6a6f virsh: Reimplement _vshCalloc using g_malloc0_n
Drop the dead code by using glib's allocator.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
2019-10-24 19:35:34 +02:00
Michal Privoznik
3b4df5d350 Drop needless ret variable
In few places we have the following code pattern:

  int ret;
  ... /* @ret is not accessed here */
  ret = f(...);
  return ret;

This pattern can be written less verbose:

  ...
  return f(...);

This patch was generated with following coccinelle spatch:

  @@
  type T;
  constant C;
  expression f;
  identifier ret;
  @@
  -T ret = C;
   ... when != ret
  -ret = f;
  -return ret;
  +return f;

Afterwards I needed to fix a few places, e.g. comment in
virDomainNetIPParseXML() was removed too because coccinelle
thinks it refers to @ret while in fact it doesn't. Also in few
places it replaced @ret declaration with a few spaces instead of
removing the line. But nothing terribly wrong.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-10-24 08:10:37 +02:00
Ján Tomko
506d313fa1 tools: use g_strdup instead of VIR_STRDUP
Replace all occurrences of
  if (VIR_STRDUP(a, b) < 0)
     /* effectively dead code */
with:
  a = g_strdup(b);

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-10-21 12:51:59 +02:00
Ján Tomko
fbc5a15372 vsh: mark ctl as unused in vshReadline
My commit removed the last use in the version for platforms
without readline.

Fixes: c937c1d23d

Signed-off-by: Ján Tomko <jtomko@redhat.com>
2019-10-21 11:11:04 +02:00
Ján Tomko
f31bdc7ced tools: delete vshStrdup
Now that we use g_strdup everywhere, delete vshStrdup.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-10-20 14:37:17 +02:00
Ján Tomko
c937c1d23d tools: prefer g_strdup to vshStrdup
Remove all the uses of vshStrdup in favor of GLib's g_strdup.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-10-20 14:37:16 +02:00
Ján Tomko
7863f1547a tools: vshCommandArgvGetArg: prefer g_strdup
Remove the use of vshStrdup.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-10-20 14:37:16 +02:00
Ján Tomko
df329e94cb tools: vshCommandArgvGetArg: one parameter per line
Split the parameters to make changes more readable.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-10-20 14:37:16 +02:00
Ján Tomko
4f7c65da27 tools: use g_steal_pointer instead of VIR_STEAL_PTR
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-10-16 15:59:42 +02:00
Ján Tomko
123196aa05 tools: use G_GNUC_UNUSED
Use G_GNUC_UNUSED from GLib instead of ATTRIBUTE_UNUSED.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-10-15 11:25:25 +02:00
Daniel P. Berrangé
d5d6dbcfb5 build: remove all gnulib bit manipulation modules
We're using gnulib to get ffs, ffsl, rotl32, count_one_bits,
and count_leading_zeros. Except for rotl32 they can all be
replaced with gcc/clangs builtins. rotl32 is a one-line
trivial function.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-10-07 13:39:26 +01:00
Daniel P. Berrangé
2b0d597670 util: get rid of virGetEnv{Allow,Block}SUID functions
Now that 100% of libvirt code is forbidden in a SUID environment,
we no longer need to worry about whether env variables are
trustworthy or not. The virt-login-shell setuid program, which
does not link to any libvirt code, will purge all environment
variables, except $TERM, before invoking the virt-login-shell-helper
program which uses libvirt.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2019-08-07 16:54:02 +01:00
Ján Tomko
eaad9e92a9 virsh: clean up includes
We don't need domain_conf or libvirt-{qemu,lxc} in these generic files.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Acked-by: Peter Krempa <pkrempa@redhat.com>
2019-07-25 15:06:15 +02:00
Andrea Bolognani
49a4a292fb tools: vsh: Drop obsolete readline compatibility code
This code is needed to use readline older than 4.1, but all
our target platforms ship with at least 6.0 these days so we
can safely get rid of it.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
2019-04-12 16:22:12 +02:00
Eric Blake
2efb42e9ac virsh: Add 'echo --err' option
Since test:///default resets state on every connection, writing a test
that covers a sequence of commands must be done from a single
session. But if the test wants to exercise particular failure modes as
well as successes, it can be nice to leave witnesses in the stderr
stream immediately before and after the spot where the expected error
should be, to ensure the rest of the script is not causing errors.

Do this by adding an --err option.

Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
2019-03-25 09:02:02 -05:00
Eric Blake
4e650259f9 virsh: Treat any command name starting with # as comment
As the previous commit mentioned, argv mode (such as when you feed
virsh via stdin with <<\EOF instead of via a single shell argument)
didn't permit comments. Do this by treating any command name token
that starts with # as a comment which silently eats all remaining
arguments to the next newline or semicolon.

Note that batch mode recognizes unquoted # at the start of any word as
a command as part of the tokenizer, while this patch only treats # at
the start of the command word as a comment (any other # remaining by
the time vshCommandParse() is processing things was already quoted
during the tokenzier, and as such was probably intended as the actual
argument to the command word earlier in the line).

Now I can do something like:

$ virsh -c test:///default <<EOF
  # setup
  snapshot-create-as test s1
  snapshot-create-as test s2
  # check
  snapshot-list test --name
EOF

Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
2019-03-25 09:01:53 -05:00
Eric Blake
834f64ca47 virsh: Parse # comments in batch mode
Continuing from what I did in commit 4817dec0, now I want to write a
sequence that is self-documenting.  So I need comments :)

Now I can do something like:

$ virsh -c test:///default '
  # setup
  snapshot-create-as test s1
  snapshot-create-as test s2
  # check
  snapshot-list test --name
'

Note that this does NOT accept comments in argv mode, another patch
will tackle that.

(If I'm not careful, I might turn virsh into a full-fledged 'sh'
replacement? Here's hoping I don't go that far...)

Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
2019-03-25 09:01:42 -05:00
Peter Krempa
eae99ca97e tools: vsh: Don't use assert()
It's meant for testing, not for production builds. Also we have a helper
for reporting OOM errors. Introduced by 23e0bf1c4e

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
2019-03-12 14:17:02 +01:00
Eric Blake
23e0bf1c4e virsh: Make self-test failures noisy
In local testing, I accidentally introduced a self-test failure,
and spent way too much time debugging it. Make sure the testsuite
log includes some hint as to why command option validation failed.
Lone exception: allocation failure is unlikely during self-test,
and if it happens, we are better off asserting (vsh.c can do this,
even if libvirt.so cannot).

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
Acked-by: Peter Krempa <pkrempa@redhat.com>
2019-03-12 06:37:48 -05:00
Eric Blake
5817dec014 virsh: Elide backslash-newline in batch mode
The previous patch made it possible to split multiple commands by
adding newline, but not to split a long single command. The sequence
backslash-newline was being used as if it were a quoted newline
character, rather than completely elided the way the shell does.

Again, add more tests, although this time it seems more like I am
suffering from a leaning-toothpick syndrome with all the \.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2019-02-26 15:42:21 -06:00
Eric Blake
fe1b683fd0 virsh: Treat \n like ; in batch mode
I wanted to do a demonstration with virsh batch mode, which
takes multiple commands all packed into a single argument:

$ virsh -c test:///default 'echo a; echo b;'
a
b

but that produced a really long line, so I tried to make it
more legible:

$ virsh -c test:///default '
   echo a;
   echo b;
'
error: unknown command: '
'

Let's be more like the shell, and treat unquoted newline as a
command separator just as we do for semicolon.  In fact, with
that, I can even now mix styles:

$ virsh -c test:///default '
   echo a; echo b
   echo c
'
a
b
c

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2019-02-26 14:16:04 -06:00
Michal Privoznik
c99e954973 Remove even more Author(s): lines from source files
In 600462834f we've tried to remove Author(s): lines
from comments at the beginning of our source files. Well, in some
files while we removed the "Author" line we did not remove the
actual list of authors.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2019-01-03 13:24:18 +01:00
Eric Blake
35966308b5 virsh: Fix regression with duplicated error messages
Commit 4f4c3b13 (v3.3) fixed an issue where performing cleanup of
libvirt objects could sometimes lose error messages, by adding code
to copy the libvirt error into last_error prior to cleanup paths.
However, it caused a regression: on other paths, some errors are now
printed twice, if libvirt still remembers in its thread-local
storage that an error was set even after virsh cleared last_error.
For example:

$ virsh -c test:///default snapshot-delete test blah
error: Domain snapshot not found: no domain snapshot with matching name 'blah'
error: Domain snapshot not found: no domain snapshot with matching name 'blah'

Fix things by telling libvirt to discard any thread-local errors at
the same time virsh prints an error message (whether or not the libvirt
error is the same as what is stored in last_error).

Update the virsh-undefine testsuite (partially reverting portions of
commit b620bdee, by removing -q, to more easily pinpoint which commands
are causing which messages), now that there is only one error message
instead of two.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2018-10-12 09:30:56 -05:00
Erik Skultety
5165ff0971 src: More cleanup of some system headers already contained in internal.h
All of the ones being removed are pulled in by internal.h. The only
exception is sanlock which expects the application to include <stdint.h>
before sanlock's headers, because sanlock prototypes use fixed width
int, but they don't include stdint.h themselves, so we have to leave
that one in place.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
2018-09-20 10:16:39 +02:00
Erik Skultety
9403b63102 internal: Move <stdio.h> include to internal.h
It doesn't really make sense for us to have stdlib.h and string.h but
not stdio.h in the internal.h header.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
2018-09-20 10:16:38 +02:00
ramyelkest
2b6667abbf all: Replace virGetLastError with virGetLastErrorCode where we can
Replace instances where we previously called virGetLastError just to
either get the code or to check if an error exists with
virGetLastErrorCode to avoid a validity pre-check.

Signed-off-by: Ramy Elkest <ramyelkest@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2018-06-05 18:44:05 +02:00
Lin Ma
f33d02c11f virsh: Don't skip the first entry when the typed command share same prefix
Signed-off-by: Lin Ma <lma@suse.com>
2018-05-11 13:13:22 +02:00
Lin Ma
0d1c1a74b7 vshReadlineParse: Ignore vshReadlineOptionsGenerator for VSH_OT_ARGV options
Currently the VSH_OT_ARGV options don't support complete, But some of
VSH_OT_ARGV options are gonna support complete in upcoming patches.

Once applied the upcoming completion patches for VSH_OT_ARGV options, If
we don't ignore VSH_OT_ARGV here, The vshReadlineOptionsGenerator will
be called, Hence complete output will consist of the result by command
completer + the result by option completer, It's confusing.
e.g.
    $ virsh domstats --domain <TAB><TAB>
    --backing     --interface      --list-paused      --perf      --vcpu
    --balloon     leap42.3         --list-persistent  --raw       win10
    --block       --list-active    --list-running     sles12sp3
    --cpu-total   --list-inactive  --list-shutoff     sles15
    --enforce     --list-other     --list-transient   --state

After this patch and the upcoming completion patches:
    $ virsh domstats --domain <TAB><TAB>
    leap42.3    sles12sp3    sles15    win10

Signed-off-by: Lin Ma <lma@suse.com>
2018-05-10 10:01:15 +02:00
Lin Ma
ee6113aaea vshReadlineOptionsGenerator: Add already provided VSH_OT_ARGV options to list
It's helpful for users while they type certain kind of VSH_OT_ARGV options.
e.g.

$ virsh domstats --domain sles12sp3 --d<TAB>

Signed-off-by: Lin Ma <lma@suse.com>
2018-05-10 10:01:15 +02:00
Lin Ma
55948988e8 virsh: Conditionally Ignore the first entry in list of completions
The first entry in the returned array is the substitution for TEXT. It
causes unnecessary output if other commands or options share the same
prefix, e.g.

$ virsh des<TAB><TAB>
des      desc     destroy

or

$ virsh domblklist --d<TAB><TAB>
--d        --details  --domain

This patch fixes the above issue.

Signed-off-by: Lin Ma <lma@suse.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-05-10 09:05:46 +02:00
Michal Privoznik
846d3b5874 vshCommandOpt: Do more checking if skipChecks is set
Currently if cmd->skipChecks is set (done only from completers)
some basic checks are skipped because we're working over
partially parsed command. See a26ff63ae4 for more detailed
explanation. Anyway, the referenced commit was too aggressive in
disabling checks and effectively returned success even in clear
case of failure. For instance:

  # domif-getlink --interface <TAB><TAB>

causes virshDomainInterfaceCompleter() to be called, which calls
virshDomainGetXML() which eventually calls
vshCommandOptStringReq(.., name = "domain"); The --domain
argument is required for the command and if not present -1 should
be returned to tell the caller the argument was not found. Well,
zero is returned meaning the argument was not found but it's not
required either.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-02-28 10:36:24 +01:00
Erik Skultety
23594ccda9 vsh: Cmd aliases lookups should return results for the aliased command
Unfortunately, we have a number of aliases in virsh and even though
these are not visible any more, we have to support them. The problem is
that when trying to print help for the alias, we get SIGSEGV because
there isn't any @def structure anymore and we need to query the command
being aliased instead.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1538570

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-25 16:20:27 +01:00
Erik Skultety
da60878c4c vsh: Drop redundant definition searches from vshCmd{def,Grp}Help
These helpers are called from a single place only - cmdHelp wrapper and
just before the wrapper invokes the helpers, it performs the search,
either for command group or for the command itself, except the result is
discarded and the helper therefore needs to do it again. Drop this
inefficient handling and pass the @def structure rather than a name,
thus preventing the helper from needing to perform the search again.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
2018-01-25 16:20:27 +01:00
Michal Privoznik
66aa7e02c6 vsh: Provide cmdComplete stub for readline disabled builds
When building without readline, this function does nothing but
return false. Without touching any of its arguments which
triggers a build error. Therefore, provide a stub that has
arguments marked as unused.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2018-01-15 15:49:07 +01:00
Michal Privoznik
98bc781b42 vshReadlineOptionsGenerator: Don't add already specified options to the list
The current state of art is as follows:

 1) vshReadlineOptionsGenerator() generate all possible --options
 for given command, and then
 2) vshReadlineOptionsPrune() clears out already provided ones
 from the list.

Not only this brings needless memory complexity it is also not
trivial to get right. We can switch to easier approach: just
don't add already specified --options in the first step.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
2018-01-15 15:45:14 +01:00
Michal Privoznik
a0e1ada63c vsh: Introduce complete command
This command is going to be called from bash completion script in
the following form:

  virsh complete -- start --domain

Its only purpose is to return list of possible strings for
completion. Note that this is a 'hidden', unlisted command and
therefore there's no documentation to it.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00
Michal Privoznik
cc005fe5a8 vsh: Filter --options
Similarly to the previous commit, once we've presented an
--option for a command to the user it makes no sense to offer it
again. Therefore, we can prune all already specified options. For
instance, after this patch:

  virsh # migrate --verbose <TAB><TAB>

will no longer offer --verbose option.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00
Michal Privoznik
d4e63aff5d vsh: Prune string list returned by completer
Instead of having completers prune returned string list based on
user's input we can do that right after the callback is called.
Only strings matching the prefix will be presented to the user
then.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00
Michal Privoznik
f1da85d44e vsh: Call vshCmdOptDef completer
Now that we have everything prepared we can call options'
completer again. At the same time, pass partially parsed input to
the completer callback - it will help the callbacks to narrow
down the list of returned options based on user's input. For
instance, if the completer is supposed to return list of
interfaces depending on user input it may return just those
interfaces defined for already specified domain. Of course,
completers might ignore this parameter.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00
Michal Privoznik
4d3c65c2e8 vsh: Fix vshCompleter signature
The first argument passed to this function is vshControl *.
There's no need to use void pointer.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00
Michal Privoznik
a26ff63ae4 vshCommandOpt: Allow caller avoiding assert()
In the future, completer callbacks will receive partially parsed
command (and thus possibly incomplete). However, we still want
them to use command options fetching APIs we already have (e.g.
vshCommandOpt*()) and at the same time don't report any errors
(nor call any asserts).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
2018-01-11 18:53:04 +01:00
Michal Privoznik
69ebc64518 vshReadlineParse: Use string list
It's better to fetch list of either commands or options just once
and then iterate over it. Moreover, it makes future completers
way simpler as they will return string lists too.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2018-01-11 18:53:04 +01:00