Commit Graph

113 Commits

Author SHA1 Message Date
Pau Espin 079149e451 vty: command.c: Add assert
a program being developed right now crashed at this point, without
providing any meaningful information on where did it crash.

Change-Id: Ia14f43142e7409f72eb9efd5c9131bea5eed6e82
2022-06-17 20:28:01 +00:00
Neels Hofmeyr 34907fe6e1 revisit some calls of strtol(), stroul(), strtoull()
Replace some with atoi(), where the VTY has already validated correct
range of the argument.

Replace others with the new osmo_str_to_int() or osmo_str_to_int64()
functions, possibly covering more detection of invalid number strings.

Leave those strtol() callers that depend on endptr to provide the next
string token.

Related: SYS#5542
Change-Id: I0ebb06e751c28f7d1cdf328de29cd227a2449391
2021-10-04 11:24:59 +00:00
Alexander Couzens 06929166de vty: add vty_out_uptime() print the uptime to the vty
vty_out_uptime() calculates the time difference to a given timespec
and print it in a human readable format (days, hours,
minutes, seconds) to the vty.

Related: OS#5028
Change-Id: I264a3f49096b96646e0a1f5366623ac20d860793
2021-09-23 13:12:34 +02:00
Oliver Smith 95a03e54ef vty: show uptime: use timespecsub
Related: OS#4609
Change-Id: If94795b3cd74040c49bdcbed8c410bf6e950afff
2021-07-19 09:01:15 +02:00
Oliver Smith 8a482fd0a8 vty: add "shutdown"
Kill programs with SIGTERM with the new vty command, as it would happen
with "systemctl stop".

I've considered running the select shutdown logic too in order to ensure
that pending OSMO_FD_WRITE requests are serviced. But as noted in
review, it's better to ensure that the regular SIGTERM code path works
as expected and call that instead of introducing an alternative code
path.

Closes: OS#4609
Change-Id: I3596a35b0f4a24e49484ec3f24a98e4d4f583e1e
2021-07-15 12:51:44 +02:00
Oliver Smith 0c78bc628e vty: add "show pid"
Related: OS#4609
Change-Id: Ib636050afc1eed134f450f2f1cdd3b51153b0160
2021-07-15 12:51:44 +02:00
Oliver Smith d243c2ad73 vty: add "show uptime"
Related: OS#4609
Change-Id: Ic7d9d5d3c8a9abd06a7e626aed7679f68406040c
2021-07-15 12:50:24 +02:00
Philipp Maier eabc6fd4b1 vty: make function cmd_range_match() public
Certain control interface commands also may require to verfy a range in
their verify function. cmd_range_match() from the VTY does exactly that
and the range can be specified as string, the same way as we would
specify it in the VTY.

Change-Id: I53fc207677f52b1dc748b01d58424839cdba807c
related: SYS#5369
2021-06-18 12:31:01 +00:00
Pau Espin ebb6c1fc4d vty: Implement missing public API host_config_file()
The API was defined in the header file but not implemented.

Change-Id: I69de9864fa7cd421557b9fc630beefea6d90b4cc
2021-05-17 18:54:23 +02:00
Harald Welte c296e2921a vty: Fix left shifting out of range on signed variable
Fixes following ASan runtime errors while running vty tests:
command.c:730:27: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'

Somehow we didn't catch this one in Ie11ff18d6fd9f6e1e91a51b6156fb6b0b7d3a9a8

Change-Id: I601caf7daa947f3cf391316f1011007ef9188c90
2020-12-21 15:45:45 +01:00
Vadim Yanitskiy 7031ac1dd2 vty/command: add 'hidden only' VTY reference generation mode
Change-Id: I511ce26350cd04bb0f66d130b5286cab84f16df2
Related: SYS#4910
2020-11-17 16:23:11 +07:00
Vadim Yanitskiy 0a2d9bdfae vty/command: fix: restrict the expert mode to the current session
Having the expert mode flag stored in the global 'host' structure
was a bad idea, because this way it applies globally.  In other
words, if user Bob activates the expert mode in his dedicated
session (e.g. a telnet connection), then not only him, but all
other users would see the hidden commands in their VTYs.

Moreover, if somebody deactivates the expert mode, it would also
affect the Bob's VTY session.  And finally, terminating a VTY
session would not deactivate the expert mode.

Let's move that flag from the global 'struct host' to 'struct vty'
representing an individual VTY session, so then the expert mode
would only affect the session where it was activated.

In functions related to the XML VTY reference generation we don't
have access to 'struct vty' (there may be no VTY session at all).
Add two additional arguments to vty_dump_nodes(), indicating the
global flag mask and a matching mode.  This would allow to match
the VTY commands in many different ways, e.g. one can dump hidden
commands only, or all commands except the library specific ones.

Change-Id: Iba13f0949061e3dadf9cf92829d15e97074fe4ad
Related: SYS#4910
2020-10-25 16:52:42 +07:00
Vadim Yanitskiy 6760845da6 vty/command: add CMD_ATTR_HIDDEN to CMD_ATTR_PUBLIC_MASK
Otherwise in the expert mode, hidden commands do not get the
associated attribute printed to the XML VTY reference.

Change-Id: I8ded973031beb6d8c7ad55f06641c9651b1d9e71
Related: OS#4910
2020-10-23 20:37:37 +07:00
Vadim Yanitskiy 4165d048f3 vty/command: introduce vty_dump_xml_ref_mode()
This change introduces an enumerated type 'vty_ref_gen_mode' that
(as the name suggests) defines the VTY reference generation mode:

  - DEFAULT - all commands except deprecated and hidden,
  - EXPERT - all commands including hidden, excluding deprecated;

and a new function vty_dump_xml_ref_mode(), that allows to specify
that mode.  The old vty_dump_xml_ref() is now deprecated.

Change-Id: Ie2022a7f9e167e5ceacf15350c037dd43768ff40
Related: SYS#4910
2020-10-23 20:20:23 +07:00
Vadim Yanitskiy efe1342b62 vty/command: make some 'struct cmd_element' pointers const
Change-Id: I4a8b3bada44538a5e5711bbc4dccf8b2cf3e2e08
2020-10-21 20:36:21 +07:00
Vadim Yanitskiy 72b90883f9 vty: introduce the expert mode and a command to enable it
Some VTY commands are intentionally hidden, e.g. because they might
by relatively dangerous if used in production operation.  We equip
such commands with a special attribute - CMD_ATTR_HIDDEN.

The problem is that neiter they appear in the XML VTY reference,
nor in the online VTY help, so it's a bit tricky to invoke them.
This change introduces so-called 'expert' mode, in which hidden
(but not deprecated) commands are getting visible.

In the (telnet) VTY session, this mode can be activated by passing
an additional argument to well-known 'enable' command:

  OsmoApp> enable ?
    [expert-mode]  Enable the expert mode (show hidden commands)
  OsmoApp> enable expert-mode
  OsmoApp#

so then hidden commands will appear together with all the other
commands.  They will be marked with a special '^' flag:

  OsmoApp# list with-flags
    ^ ...  foo-hidden [expert-mode]
    . ...  foo-regular-one
    ! ...  foo-immediate
    ^ u..  app-hidden-unbelievable

For the XML reference generation, additional API needs to be
introduced.  This will be implemented in subsequent patches.

Change-Id: Ie69c2a19b22fb31d7bd7f6412f0aeac86ea5048f
Related: SYS#4910
2020-10-21 06:27:48 +07:00
Pau Espin 715a612aba vty: Fix left shifting out of range on signed variable
Fixes following ASan runtime errors while running vty tests:
src/vty/command.c:3088:27: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'
src/vty/command.c:3136:23: runtime error: left shift of 1 by 31 places cannot be represented in type 'int'

Change-Id: Ie11ff18d6fd9f6e1e91a51b6156fb6b0b7d3a9a8
2020-10-12 15:09:37 +00:00
Philipp Maier a5218ea9b3 command: add library command attribute for libosmo-abis
Change-Id: I0efc57f2cb54798ba207ae6fef9af4771d96bfa9
Related: SYS#4937, OS#1601
2020-10-08 19:08:17 +02:00
Vadim Yanitskiy f1fc9d3af5 vty: fix vty_dump_element(): do not print empty <attributes>
Some attributes like CMD_ATTR_LIB_COMMAND are not being printed
to the XML VTY reference (despite being set), so we should not
print empty "<attributes scope='global'></attributes>".

Change-Id: Ie7e53b080c10564bfef6f0e8ddeb470e46fad387
Related: SYS#4937
2020-10-07 14:17:02 +07:00
Vadim Yanitskiy ef4c597b4a vty/command: restrict the use of '.', '!', and '@' as flags
Change-Id: Icb4acbab0a15de2b0ed7b88fb0e227675317146a
Related: SYS#4937
2020-10-07 14:17:02 +07:00
Vadim Yanitskiy 7e1a78fb03 vty/command: assign flags to CMD_ATTR_{IMMEDIATE,NODE_EXIT}
Change-Id: I77c1ef7ca4c667c769cc53c7ac65c3be5c7e1c86
Related: SYS#4937
2020-10-07 14:17:02 +07:00
Vadim Yanitskiy 75fec3a419 vty/command: print attribute flags in the output of 'list'
Here is an example:

  OsmoAPP(config-foo)# list with-flags
    ...  help
    ...  list
    ...  show running-config
    ...  exit
    ..F  lib-command foo (one|two|three)
    ZB.  lib-command bar [zoo]
    .bf  app-command foo-bar
    z..  app-command zoo .TEXT
    ...  app-command nope

A dot indicates that the associated attribute is not set.

Note that there is no strict relation between rows and index values
of the attributes.  In the example above there could be one or more
hidden flag rows corresponding to attributes that are not assigned
to any of the commands within 'config-foo' node.

If neither of the commands belonging to the current node (where
'list' command is executed) has attributes, the output would
not contain empty dot-rows.

Global attributes (such as CMD_ATTR_IMMEDIATE) are not yet displayed
because we still have not agreed on what kind of symbols to assign
them.  This will be implemented later.

Change-Id: I71cef3ec0fab44c7e11fc353b8bc42268a4ee8f0
Related: SYS#4937
2020-10-07 12:18:10 +07:00
Vadim Yanitskiy 44c4d17d40 vty/command: introduce a command to list attributes
Here is an example of listing all attributes:

  OsmoBSC# show vty-attributes
    Global attributes:
      .  This command is deprecated
      .  This command is hidden
      .  This command applies immediately
      .  This command applies on VTY node exit
    Library specific attributes:
      (no attributes)
    Application specific attributes:
      o  This command applies on A-bis OML link (re)establishment
      r  This command applies on A-bis RSL link (re)establishment

or only a specific kind of attributes:

  OsmoBSC# show vty-attributes application
    Application specific attributes:
      o  This command applies on A-bis OML link (re)establishment
      r  This command applies on A-bis RSL link (re)establishment

Change-Id: I561114d7416e30cc06b7d49c0bc1217a76039c99
Related: SYS#4937
2020-10-07 04:01:48 +07:00
Philipp Maier 608b1a40ff command: add library command attribute for libosmo-sccp
Change-Id: I4439a414af05700cb1ccff7e7e5927ffc194d171
Related: SYS#4937, OS#1601
2020-10-06 16:42:58 +02:00
Vadim Yanitskiy c0745ebc28 vty/command: introduce API for the library specific attributes
See https://lists.osmocom.org/pipermail/openbsc/2020-October/013278.html.

Change-Id: I15184569635b3ef7dfe9eeddcc19bf16cc358f66
Related: SYS#4937
2020-10-06 00:22:26 +07:00
Vadim Yanitskiy 8e7c49649e vty: use install_lib_element() and install_lib_element_ve()
See https://lists.osmocom.org/pipermail/openbsc/2020-October/013278.html.

Change-Id: Ic541126ffd4975daf87199abfafb465e2055e44f
Related: SYS#4937
2020-10-06 00:22:22 +07:00
Vadim Yanitskiy 99d5c2d721 vty/command: add CMD_ATTR_LIB_COMMAND and install() API wrappers
This new attribute would allow to distinguish commands provided
by libraries from commands registered by the application itself,
so vty_dump_element() would print proper description for the
library specific attributes.

All VTY commands defined by the libraries need to use the new API:

  - install_lib_element(), and
  - install_lib_element_ve,

instead of the old functions (respectively):

  - install_element(), and
  - install_element_ve().

See https://lists.osmocom.org/pipermail/openbsc/2020-October/013278.html.

Change-Id: I8baf31ace93c536421893c2aa4e3d9d298dcbcc6
Related: SYS#4937
2020-10-06 00:22:06 +07:00
Vadim Yanitskiy ceb3b39440 vty/command: add global command attribute CMD_ATTR_NODE_EXIT
Change-Id: I56306f4886a72a3525b60225721aa2fcf7c57213
Related: SYS#4937
2020-10-06 00:20:37 +07:00
Vadim Yanitskiy 7f6c87da2f vty/command: reflect global attributes in the XML reference
Given that commands with either/both of the following attributes:

  - CMD_ATTR_DEPRECATED,
  - CMD_ATTR_HIDDEN,

never end up in the XML reference, only CMD_ATTR_IMMEDIATE would
be reflected for commands taking effect immediately as follows:

  <command id='foo'>
    <!-- Global attributes -->
    <attributes scope='global'>
      <attribute doc='This command applies immediately' />
    </attributes>

    <!-- Application specific attributes -->
    <attributes scope='application'>
      <!-- ... -->
    </attributes>

    <params>
      <!-- ... -->
    </params>
  </command>

Change-Id: I8476c1163c23a9a52641987acf3df0b8c49d8f7b
Related: SYS#4937
2020-09-28 13:03:52 +07:00
Vadim Yanitskiy 34e94f3c3a vty: print program specific attributes in the XML reference
Change-Id: I1f35368ba9178e1454f2e3ddfcad9d96576143ef
Related: SYS#4937
2020-09-20 09:57:44 +00:00
Vadim Yanitskiy 478dd1b330 vty/command: cosmetic: swap i and j in vty_dump_element()
It's more convenient to use i in the outer loop, and j, k, etc.
in the inner loops.  Otherwise it looks a bit confusing.

Change-Id: I61ef48fcf977d6a872e288571a4ff2c3dfe3184b
2020-08-18 18:40:22 +07:00
Vadim Yanitskiy b0bbd72b2e vty/command: fix switch / case coding style in vty_go_parent()
Change-Id: I6f7f5af6879d91811be6f0edf3feb2ebae185ac7
2020-08-18 18:34:47 +07:00
Vadim Yanitskiy 1d7ae15625 vty/command: cosmetic: fix formatting of config_help_cmd
Change-Id: I8f784d0cf65fa7813088e3ff76d7213b99b975d0
2020-08-15 07:46:53 +00:00
Vadim Yanitskiy 6f50e21936 vty/command: cosmetic: simplify conditions in in config_list_cmd
Change-Id: Id1c082d102b7156dafb9c5d9197dcdc4d26bff63
2020-08-15 07:46:53 +00:00
Pau Espin a184701108 vty: Don't match negative values on purely positive ranges
Without this patch, for instance having a range 0-ULONG_MAX would match
if someones types value -3, which would be converted to unsigned but
that's clearly what is expected here from user point of view.

Change-Id: Ia95f6314a2dd3f94d21fc219cf69bb8d39b8e7f0
2020-07-30 21:02:03 +00:00
Pau Espin 9fdc871532 vty: Allow 64 bit values in numeric ranges if system supports it
This fixes commands not being matched due to providing a range with more
than 10 digits.

The last case (passing -4000 matching 0-ULONG_MAX) shows a different bug
which will be fixed in next commit.

Change-Id: I0afa0caabffe36083c36b92ba90696ded00bb7be
2020-07-30 21:02:03 +00:00
Neels Hofmeyr cf70aa0c40 enable vty xml dumping to stdout
Allow dumping the VTY XML reference (for generating manuals) to a normal FILE*
stream instead of a vty output buffer.

We currently generate the VTY reference by starting the client program,
connecting to the VTY telnet and dumping the reference. That is weirdly
convoluted, especially since there has to be a valid config file that
successfully starts up a minimal set of external links before the reference can
be generated. IMO we should have dumped the XML reference to stdout from the
start, and never to a VTY session.

With this patch, it is trivial to generate the XML VTY reference by a
commandline switch. The client program will set up the entire VTY, and
immediately after parsing the cmdline options but before parsing a config file,
just dumps the reference and doesn't even start establishing local ports. That
would allow generating the XML reference on the fly during the build process of
the manuals, without the need of a docker container or somesuch.

A first implementation of such a commandline switch is `osmo-bsc -X`, added in
I316efedb2c1652791434ecf14a1e261367cd2fb7

This patch jumps through a bit of a hoop to still allow dumping to a VTY buffer
without code dup, to still allow dumping the XML reference through telnet VTY,
until all our programs have implemented an -X switch (TM).

Change-Id: Ic74bbdb6dc5ea05f03c791cc70184861e39cd492
2020-05-18 13:55:08 +02:00
Neels Hofmeyr d31de23758 vty: track parent nodes also for telnet sessions
Keep track of parent nodes and go back hierarchically, not only for .cfg file
reading, but also for telnet VTY sessions.

A long time ago cfg file parsing was made strictly hierarchical: node exits go
back to parent nodes exactly as they were entered. However, live telnet VTY
sessions still lacked this and depended on the go_parent_cb().

From this commit on, implementing a go_parent_cb() is completely optional. The
go_parent_cb() no longer has the task to determine the correct parent node,
neither for cfg files (as already the case before this patch) nor for telnet
VTY sessions (added by this patch). Instead, a go_parent_cb() implementation
can merely take actions it requires on node exits, for example applying some
config when leaving a specific node.

The node value that is returned by the go_parent_cb() and the vty->node and
vty->index values that might be set are completely ignored; instead the
implicit parent node tracking determines the parent and node object.

As a side effect, the is_config_node() callback is no longer needed, since the
VTY now always implicitly knows when to exit back to the CONFIG_NODE.

For example, osmo_ss7_is_config_node() could now be dropped, and the
osmo_ss7_vty_go_parent() could be shortened by five switch cases, does no
longer need to set vty->node nor vty->index and could thus be shortened to:

int osmo_ss7_vty_go_parent(struct vty *vty)
{
        struct osmo_ss7_asp *asp;
        struct osmo_xua_server *oxs;

        switch (vty->node) {
        case L_CS7_ASP_NODE:
                asp = vty->index;
                /* If no local addr was set */
                if (!asp->cfg.local.host_cnt) {
                        asp->cfg.local.host[0] = NULL;
                        asp->cfg.local.host_cnt = 1;
                }
                osmo_ss7_asp_restart(asp);
                break;
        case L_CS7_XUA_NODE:
                oxs = vty->index;
                /* If no local addr was set, or erased after _create(): */
                if (!oxs->cfg.local.host_cnt)
                        osmo_ss7_xua_server_set_local_host(oxs, NULL);
                if (osmo_ss7_xua_server_bind(oxs) < 0)
                        vty_out(vty, "%% Unable to bind xUA server to IP(s)%s", VTY_NEWLINE);
                break;
        }
        return 0;
}

Before parent tracking, every program was required to write a go_parent_cb()
which has to return every node's parent node, basically a switch() statement
that manually traces the way back out of child nodes. If the go_parent_cb() has
errors, we may wildly jump around the node tree: a common error is to jump
right out to the top config node with one exit, even though we were N levels
deep. This kind of error has been eliminated for cfg files long ago, but still
exists for telnet VTY sessions, which this patch fixes.

This came up when I was adding multi-level config nodes to osmo-hlr to support
Distributed GSM / remote MS lookup: the config file worked fine, while vty node
tests failed to exit to the correct nodes.

Change-Id: I2b32b4fe20732728db6e9cdac7e484d96ab86dc5
2019-11-24 19:59:35 +01:00
Pau Espin a0c8195ad3 vty: Return error if cmd returns CMD_WARNING while reading cfg file
Otherwise bad configurations can easily sneak in and produce unexpected
behavior.

Change-Id: Ic9c1b566ec4a459f03e6319cf369691903cf9d00
2019-10-28 19:15:29 +00:00
Pau Espin 0fd0fe61fa vty: Fix go_parent_cb not called for indented nodes at end of cfg file
Without this patch, for instance in this cfg file below, go_parent_cb is
not called for nodes such as "listen" and "cs7":
"""
line vty
 no login
cs7 instance 0
 xua rkm routing-key-allocation dynamic-permitted
 listen m3ua 2905
  accept-asp-connections dynamic-permitted
  local-ip 127.0.0.1
"""

Related: OS#3608
Change-Id: Ia6d88c0e63d94ba99e950da6efbc4c1871070012
2019-10-11 14:13:02 +02:00
Pau Espin eda8b7b23d vty: Optionally Set/replace cfg file during cmd 'write file'
This way if the process is started with no file associated (eg. no -c
param and default cfg path doesn't exist), config can be later saved
into a file by passing the parameter. Otherwise, until now this message
was displayed:
Can't save to configuration file, using vtysh.

Related: OS#4024
Change-Id: I38edcf902a08b6bd0ebb9aa6fc1a7041421af525
2019-10-07 20:01:45 +00:00
Ruben Undheim 766f77c3d9 MAXPATHLEN set if not defined
Change-Id: I1dce8ace228814b5a7246a00b31309ab9461d266
2019-09-02 09:12:27 +00:00
Pau Espin e1e1ec31a3 vty: Simplify char escaping in asciidoc output
Change-Id: I7df6858bb98abffc1d5bf420f991ae5854b24638
2019-06-25 21:46:44 +00:00
Pau Espin 32e6710487 vty: command.c: Fix: single-choice optional args are no longer passed incomplete to vty func
For instance, take command "single0 [one]":
If user executes "single0 on", VTY func will receive argv[0]="one"
instead of argv[0]="on".

Related: OS#4045
Change-Id: I5f4e2d16c62a2d22717989c6acc77450957168cb
2019-06-14 17:44:21 +02:00
Pau Espin 7e1b03f763 vty: command.c: Fix: multi-choice args are no longer passed incomplete to vty func
For instance, take command "multi0 (one|two|three)":
If user executes "multi0 tw", VTY func will receive argv[0]="two"
instead of argv[0]="tw".

Fixes: OS#4045
Change-Id: I91b6621ac3d87fda5412a9b415e7bfb4736c8a9a
2019-06-14 17:44:16 +02:00
Pau Espin 8930ace072 vty: command.c: Get rid of big indentation block
This block will become bigger in forthcoming commits.

Change-Id: Ibc1494014b1e77ce10950f7268a44d2d2091a6f2
2019-06-14 12:42:22 +02:00
Pau Espin c17c6d6ea5 command.c: Improve return check condition in cmd_execut_command_real()
Check against MAX argc is changed to == since it cannot be incremented
twice without passing the check.

Change-Id: Ia330e475989fda863bedcc3cbf94deaf8dd83037
2019-06-14 12:38:44 +02:00
Pau Espin 274ac4dcc3 vty: command.c: Get rid of huge indentation block
Huge conditional block inside for loop is negated in this patch
together with a "continue" keyword, similar to what was already done
recently in 4742526645.

Change-Id: I803c4ed38e9ab09bf929528c75a60e6f65da3928
2019-06-12 14:25:11 +00:00
Pau Espin de89099f68 cosmetic: vty: command.c: Use upper case for enum match_type value names
Makes code easier to follow because enum values no longer look like
variables.

Change-Id: Ib6e9592c5962d047869a280c10f9b557fae6f435
2019-06-12 14:25:11 +00:00
Pau Espin 6df2e44404 vty: command.c: Fix is_cmd_ambiguous() returning always 0
inner block defined variable "enum match_type ret" was being masking
outter block variable "int ret = 0". The ret variable was being given
non zero values only inside the inner block, so that change was done on
the inner variable and not the outer one, which is returned.

Fixes: 5314c513f2
Change-Id: Iec87d7db49a096d07e38ff8a060b923a52bfd6ba
2019-06-11 21:50:17 +02:00