source: rtems-docs/shell/configuration_and_init.rst @ 633a24f

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

Simplify SPDX-License-Identifier comment

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