source: rtems-docs/shell/configuration_and_init.rst @ 4886d60

5
Last change on this file since 4886d60 was 4886d60, checked in by Sebastian Huber <sebastian.huber@…>, on 01/09/19 at 15:14:04

Use standard format for copyright lines

  • Property mode set to 100644
File size: 12.4 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 1988, 2008 On-Line Applications Research Corporation (OAR)
4.. COMMENT: All rights reserved.
5
6Configuration and Initialization
7********************************
8
9Introduction
10============
11
12This chapter provides information on how the application configures and
13initializes the RTEMS shell.
14
15Configuration
16=============
17
18The command set available to the application is user configurable.  It is
19configured using a mechanism similar to the ``confdefs.h`` mechanism used to
20specify application configuration.
21
22In the simplest case, if the user wishes to configure a command set with all
23commands available that are neither filesystem management (e.g. mounting,
24formating, etc.) or network related, then the following is all that is
25required:
26
27.. code-block:: c
28
29    #define CONFIGURE_SHELL_COMMANDS_INIT
30    #define CONFIGURE_SHELL_COMMANDS_ALL
31    #include <rtems/shellconfig.h>
32
33In a slightly more complex example, if the user wishes to include all
34networking commands as well as support for mounting MS-DOS and NFS filesystems,
35then the following is all that is required:
36
37.. code-block:: c
38
39    #define CONFIGURE_SHELL_COMMANDS_INIT
40    #define CONFIGURE_SHELL_COMMANDS_ALL
41    #define CONFIGURE_SHELL_MOUNT_MSDOS
42    #define CONFIGURE_SHELL_MOUNT_NFS
43    #include <rtems/shellconfig.h>
44
45Customizing the Command Set
46---------------------------
47
48The user can configure specific command sets by either building up the set from
49individual commands or starting with a complete set and disabling individual
50commands.  Each command has two configuration macros associated with it.
51
52*CONFIGURE_SHELL_COMMAND_XXX*
53    Each command has a constant of this form which is defined when
54    building a command set by individually enabling specific
55    commands.
56
57*CONFIGURE_SHELL_NO_COMMAND_XXX*
58    In contrast, each command has a similar command which is
59    defined when the application is configuring a command set
60    by disabling specific commands in the set.
61
62Adding Custom Commands
63----------------------
64
65One of the design goals of the RTEMS Shell was to make it easy for a user to
66add custom commands specific to their application.  We believe this design goal
67was accomplished.  In order to add a custom command, the user is required to do
68the following:
69
70- Provide a *main-style* function which implements the command.  If that
71  command function uses a ``getopt`` related function to parse arguments, it
72  *MUST* use the reentrant form.
73
74- Provide a command definition structure of type ``rtems_shell_cmd_t``.
75
76- Configure that command using the ``CONFIGURE_SHELL_USER_COMMANDS`` macro.
77
78Custom aliases are configured similarly but the user only provides an alias
79definition structure of type ``rtems_shell_alias_t`` and configures the alias
80via the ``CONFIGURE_SHELL_USER_ALIASES`` macro.
81
82In the following example, we have implemented a custom command named
83``usercmd`` which simply prints the arguments it was passed. We have also
84provided an alias for ``usercmd`` named ``userecho``.
85
86.. code-block:: c
87
88    #include <rtems/shell.h>
89    int main_usercmd(int argc, char **argv)
90    {
91        int i;
92        printf( "UserCommand: argc=%d\n", argc );
93        for (i=0 ; i<argc ; i++ )
94            printf( "argv[%d]= %s\n", i, argv[i] );
95        return 0;
96    }
97    rtems_shell_cmd_t Shell_USERCMD_Command = {
98        "usercmd",                   /* name */
99        "usercmd n1 \[n2 \[n3...]]", /* usage */
100        "user",                      /* topic */
101        main_usercmd,                /* command */
102        NULL,                        /* alias */
103        NULL                         /* next */
104    };
105    rtems_shell_alias_t Shell_USERECHO_Alias = {
106        "usercmd",                   /* command */
107        "userecho"                   /* alias */
108    };
109    #define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command
110    #define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
111    #define CONFIGURE_SHELL_COMMANDS_INIT
112    #define CONFIGURE_SHELL_COMMANDS_ALL
113    #define CONFIGURE_SHELL_MOUNT_MSDOS
114    #include <rtems/shellconfig.h>
115
116Notice in the above example, that the user wrote the *main* for their command
117(e.g. ``main_usercmd``) which looks much like any other ``main()``.  They then
118defined a ``rtems_shell_cmd_t`` structure named ``Shell_USERCMD_Command`` which
119describes that command.  This command definition structure is registered into
120the static command set by defining ``CONFIGURE_SHELL_USER_COMMANDS``
121to ``&Shell_USERCMD_Command``.
122
123Similarly, to add the ``userecho`` alias, the user provides the alias
124definition structure named ``Shell_USERECHO_Alias`` and defines
125``CONFIGURE_SHELL_USER_ALIASES`` to configure the alias.
126
127The user can configure any number of commands and aliases in this manner.
128
129Initialization
130==============
131
132The shell may be easily attached to a serial port or to the ``telnetd`` server.
133This section describes how that is accomplished.
134
135Attached to a Serial Port
136-------------------------
137
138Starting the shell attached to the console or a serial port is very simple. The
139user invokes ``rtems_shell_init`` with parameters to indicate the
140characteristics of the task that will be executing the shell including name,
141stack size, and priority.  The user also specifies the device that the shell is
142to be attached to.
143
144This example is taken from the ``fileio`` sample test.  This shell portion of
145this test can be run on any target which provides a console with input and
146output capabilities.  It does not include any commands which cannot be
147supported on all BSPs.  The source code for this test is in
148``testsuites/samples/fileio`` with the shell configuration in the ``init.c``
149file.
150
151.. code-block:: c
152
153    #include <rtems/shell.h>
154    void start_shell(void)
155    {
156        printf(" =========================\n");
157        printf(" starting shell\n");
158        printf(" =========================\n");
159        rtems_shell_init(
160            "SHLL",                       /* task name */
161            RTEMS_MINIMUM_STACK_SIZE * 4, /* task stack size */
162            100,                          /* task priority */
163            "/dev/console",               /* device name */
164            false,                        /* run forever */
165            true,                         /* wait for shell to terminate */
166            rtems_shell_login_check       /* login check function,
167            use NULL to disable a login check */
168        );
169    }
170
171In the above example, the call to ``rtems_shell_init`` spawns a task to run the
172RTEMS Shell attached to ``/dev/console`` and executing at priority 100.  The
173caller suspends itself and lets the shell take over the console device.  When
174the shell is exited by the user, then control returns to the caller.
175
176Attached to a Socket
177--------------------
178
179TBD
180
181Access Control
182==============
183
184Login Checks
185------------
186
187Login checks are optional for the RTEMS shell and can be configured via a login
188check handler passed to ``rtems_shell_init()``.  One login check handler
189is ``rtems_shell_login_check()``.
190
191Configuration Files
192-------------------
193
194The following files are used by the login check handler
195``rtems_shell_login_check()`` to validate a passphrase for a user and to set up
196the user environment for the shell command execution.
197
198:file:`/etc/passwd`
199    The format for each line is
200
201    .. code:: c
202
203        user_name:password:UID:GID:GECOS:directory:shell
204
205    with colon separated fields.  For more information refer to the Linux
206    PASSWD(5) man page.  Use a ``password`` of ``*`` to disable the login of the
207    user.  An empty password allows login without a password for this user.  In
208    contrast to standard UNIX systems, this file is only readable and writeable
209    for the user with an UID of zero by default.  The ``directory`` is used to
210    perform a filesystem change root operation in ``rtems_shell_login_check()``
211    in contrast to a normal usage as the HOME directory of the user.
212    The *default* content is:
213
214    .. code:: c
215
216        root::0:0::::
217
218    so there is *no password required* for the ``root`` user.
219
220:file:`/etc/group`
221    The format for each line is:
222
223    .. code:: c
224
225        group_name:password:GID:user_list
226
227    with colon separated fields.  The ``user_list`` is comma separated.  For
228    more information refer to the Linux GROUP(5) man page.  In contrast to
229    standard UNIX systems, this file is only readable and writeable for the
230    user with an UID of zero by default.  The default content is
231
232    .. code:: c
233
234        root::0:
235
236Command Visibility and Execution Permission
237-------------------------------------------
238
239Each command has:
240
241- an owner,
242
243- a group, and
244
245- a read permission flag for the owner, the group and all other users, and
246
247- an execution permission flag for the owner, the group and all other
248  users.
249
250The read and write permission flags are stored in the command mode.  The read
251permission flags determine the visibility of the command for the current user.
252The execution permission flags determine the ability to execute a command for
253the current user.  These command properties can be displayed and changed with
254the:
255
256- ``cmdls``,
257
258- ``cmdchown``, and
259
260- ``cmdchmod``
261
262commands.  The access is determined by the effective UID, the effective GID and
263the supplementary group IDs of the current user and follows the standard
264filesystem access procedure.
265
266Add CRYPT(3) Formats
267--------------------
268
269By default the ``crypt_r()`` function used by ``rtems_shell_login_check()``
270supports only plain text passphrases.  Use ``crypt_add_format()`` to add more
271formats.  The following formats are available out of the box:
272
273- ``crypt_md5_format``,
274
275- ``crypt_sha256_format``, and
276
277- ``crypt_sha512_format``.
278
279An example follows:
280
281.. index:: crypt_add_format
282
283.. code:: c
284
285    #include <crypt.h>
286    void add_formats( void )
287    {
288        crypt_add_format( &crypt_md5_format );
289        crypt_add_format( &crypt_sha512_format );
290    }
291
292Functions
293=========
294
295This section describes the Shell related C functions which are publicly
296available related to initialization and configuration.
297
298.. raw:: latex
299
300   \clearpage
301
302rtems_shell_init - Initialize the shell
303---------------------------------------
304.. index:: initialization
305.. index:: rtems_shell_init
306
307CALLING SEQUENCE:
308    .. code-block:: c
309
310        rtems_status_code rtems_shell_init(
311            const char          *task_name,
312            size_t               task_stacksize,
313            rtems_task_priority  task_priority,
314            const char          *devname,
315            bool                 forever,
316            bool                 wait,
317            rtems_login_check    login_check
318        );
319
320DIRECTIVE STATUS CODES:
321    ``RTEMS_SUCCESSFUL`` - Shell task spawned successfully
322    *others* - to indicate a failure condition
323
324DESCRIPTION:
325    This service creates a task with the specified characteristics to run the RTEMS
326    Shell attached to the specified ``devname``.
327
328NOTES:
329    This method invokes the ``rtems_task_create`` and ``rtems_task_start``
330    directives and as such may return any status code that those directives may
331    return.
332
333    There is one POSIX key necessary for all shell instances together and one
334    POSIX key value pair per instance. You should make sure that your RTEMS
335    configuration accounts for these resources.
336
337.. raw:: latex
338
339   \clearpage
340
341rtems_shell_login_check - Default login check handler
342-----------------------------------------------------
343.. index:: initialization
344.. index:: rtems_shell_login_check
345
346CALLING SEQUENCE:
347    .. code:: c
348
349        bool rtems_shell_login_check(
350          const char *user,
351          const char *passphrase
352        );
353
354DIRECTIVE STATUS CODES:
355    ``true`` - login is allowed, and
356    ``false`` - otherwise.
357
358DESCRIPTION:
359    This function checks if the specified passphrase is valid for the specified
360    user.
361
362NOTES:
363    As a side-effect if the specified passphrase is valid for the specified
364    user, this function:
365
366    - performs a filesystem change root operation to the directory of the
367      specified user if the directory path is non-empty,
368
369    - changes the owner of the current shell device to the UID of the specified
370      user,
371
372    - sets the real and effective UID of the current user environment to the
373      UID of the specified user,
374
375    - sets the real and effective GID of the current user environment to the
376      GID of the specified user, and
377
378    - sets the supplementary group IDs of the current user environment to the
379      supplementary group IDs of the specified user.
380
381    In case the filesystem change root operation fails, then the environment
382    setup is aborted and ``false`` is returned.
Note: See TracBrowser for help on using the repository browser.