source: rtems/testsuites/libtests/shell01/init.c @ 4d906d6a

4.11
Last change on this file since 4d906d6a was 4d906d6a, checked in by Chris Johns <chrisj@…>, on Feb 18, 2020 at 12:34:46 AM

libmisc/shell: Fix the handling of joel scripts in telnet

  • Fix the passing of std[in/out] to child threads
  • Fix deleting of managed memory in the key destructor
  • Only set the key in the main loop thread
  • Only allocate a shell env outside of the main loop
  • Fix memory leak if the task start fails
  • Remove error level from shell env, it cannot be returned this way. Add exit_code but the API is broken so it cannot be returned.

Closes #3877

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#ifdef HAVE_CONFIG_H
16  #include "config.h"
17#endif
18
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <errno.h>
22#include <grp.h>
23#include <pwd.h>
24#include <stdio.h>
25#include <unistd.h>
26
27#include <rtems/shell.h>
28#include <rtems/userenv.h>
29
30#include "tmacros.h"
31
32const char rtems_test_name[] = "SHELL 1";
33
34static void create_file(const char *name, const char *content)
35{
36  FILE *fp;
37  int rv;
38
39  fp = fopen(name, "wx");
40  rtems_test_assert(fp != NULL);
41
42  rv = fputs(content, fp);
43  rtems_test_assert(rv == 0);
44
45  rv = fclose(fp);
46  rtems_test_assert(rv == 0);
47}
48
49static const char joel_in[] =
50  "#! joel\n"
51  "jtst hello world\n"
52  "jtst 1 2 3 4 5\n";
53
54static const char joel_out_1[] =
55  " 3 'jtst hello world'\n"
56  " 6 'jtst 1 2 3 4 5'\n";
57
58static const char joel_out_2[] =
59  "\n"
60  "RTEMS Shell on (null). Use 'help' to list commands.\n"
61  " 3 'jtst hello world'\n"
62  " 6 'jtst 1 2 3 4 5'\n";
63
64static int joel_test_command(int argc, char** argv)
65{
66  int i;
67  fprintf(stdout, "%2d '", argc);
68  for (i = 0; i < argc; ++i) {
69    fprintf(stdout, argv[i]);
70    if (i < (argc - 1))
71      fprintf(stdout, " ");
72  }
73  fprintf(stdout, "'\n");
74  return 0;
75}
76
77static void test_joel(void)
78{
79  /*
80   * Running a joel script tests the shell main loop.
81   */
82  char buf[sizeof(joel_out_2) + 1];
83  rtems_shell_cmd_t* cmd;
84  FILE *in;
85  FILE *out;
86  FILE *current_stdout = stdout;
87  FILE *current_stdin = stdin;
88  size_t len;
89  rtems_status_code sc;
90
91  /*
92   * Use a private command due to the way the testsuite maps printk onto printf.
93   */
94  cmd = rtems_shell_add_cmd("jtst", "misc", "joel test", joel_test_command);
95  rtems_test_assert(cmd != NULL);
96
97  len = strlen(joel_in);
98
99  in = fopen("/jin", "w");
100  rtems_test_assert(in != NULL);
101  rtems_test_assert(fwrite(joel_in, sizeof(char), len, in) == len);
102  rtems_test_assert(fclose(in) == 0);
103
104  /*
105   * The shell opens the input and output files.
106   */
107  sc = rtems_shell_script(
108    "JTST",
109    8 * 1024,
110    1,
111    "/jin",
112    "/jout",
113    false,
114    true,
115    false);
116  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
117
118  out = fopen("/jout", "r");
119  rtems_test_assert(out != NULL);
120  rtems_test_assert(!feof(out));
121  memset(buf, 0, sizeof(buf));
122  len = fread(buf, sizeof(char), sizeof(buf), out);
123  rtems_test_assert(len > 0);
124  rtems_test_assert(strcmp(joel_out_1, buf) == 0);
125  rtems_test_assert(fclose(out) == 0);
126
127  /*
128   * The shell task inherits the parent stdin and stdout
129   */
130  in = fopen("/jin", "r");
131  rtems_test_assert(in != NULL);
132  out = fopen("/jout", "w");
133  rtems_test_assert(out != NULL);
134
135  stdin = in;
136  stdout = out;
137
138  sc = rtems_shell_script(
139    "JTST",
140    8 * 1024,
141    1,
142    "stdin",
143    "stdout",
144    false,
145    true,
146    false);
147  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
148
149  stdout = current_stdout;
150  stdin = current_stdin;
151
152  rtems_test_assert(fclose(in) == 0);
153  rtems_test_assert(fclose(out) == 0);
154
155  out = fopen("/jout", "r");
156  rtems_test_assert(out != NULL);
157  rtems_test_assert(!feof(out));
158  memset(buf, 0, sizeof(buf));
159  len = fread(buf, sizeof(char), sizeof(buf), out);
160  rtems_test_assert(len > 0);
161  rtems_test_assert(strcmp(joel_out_2, buf) == 0);
162  rtems_test_assert(fclose(out) == 0);
163}
164
165static void test(void)
166{
167  rtems_user_env_t *uenv;
168  rtems_status_code sc;
169  struct stat st_chroot;
170  struct stat st_workdir;
171  bool ok;
172  int rv;
173
174  rv = mkdir("/etc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
175  rtems_test_assert(rv == 0);
176
177  rv = mkdir("/chroot", S_IRWXU | S_IRWXG | S_IRWXO);
178  rtems_test_assert(rv == 0);
179
180  rv = lstat("/chroot", &st_chroot);
181  rtems_test_assert(rv == 0);
182
183  create_file(
184    "/etc/passwd",
185    "moop:foo:1:3:blob:a::c\n"
186    "no:*:2:4::::\n"
187    "zero::3:5::::\n"
188    "shadow:x:4:6::::\n"
189    "invchroot::5:7:::/inv:\n"
190    "chroot::6:8:::/chroot:\n"
191  );
192
193  create_file(
194    "/etc/group",
195    "A::1:moop,u,v,w,zero\n"
196    "B::2:moop\n"
197    "blub:bar:3:moop\n"
198    "C::4:l,m,n,moop\n"
199    "D::5:moop,moop\n"
200    "E::6:x\n"
201    "E::7:y,z\n"
202    "F::8:s,moop,t\n"
203  );
204
205  sc = rtems_libio_set_private_env();
206  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
207
208  uenv = rtems_current_user_env_get();
209
210  rv = setuid(0);
211  rtems_test_assert(rv == 0);
212
213  rv = seteuid(0);
214  rtems_test_assert(rv == 0);
215
216  ok = rtems_shell_login_check("inv", NULL);
217  rtems_test_assert(!ok);
218
219  ok = rtems_shell_login_check("no", NULL);
220  rtems_test_assert(!ok);
221
222  ok = rtems_shell_login_check("shadow", NULL);
223  rtems_test_assert(!ok);
224
225  ok = rtems_shell_login_check("moop", "false");
226  rtems_test_assert(!ok);
227
228  ok = rtems_shell_login_check("invchroot", NULL);
229  rtems_test_assert(!ok);
230
231  rtems_test_assert(getuid() == 0);
232  rtems_test_assert(geteuid() == 0);
233  rtems_test_assert(getgid() == 0);
234  rtems_test_assert(getegid() == 0);
235  rtems_test_assert(uenv->ngroups == 0);
236
237  ok = rtems_shell_login_check("zero", NULL);
238  rtems_test_assert(ok);
239  rtems_test_assert(getuid() == 3);
240  rtems_test_assert(geteuid() == 3);
241  rtems_test_assert(getgid() == 5);
242  rtems_test_assert(getegid() == 5);
243  rtems_test_assert(uenv->ngroups == 1);
244  rtems_test_assert(uenv->groups[0] == 1);
245
246  ok = rtems_shell_login_check("moop", "foo");
247  rtems_test_assert(ok);
248  rtems_test_assert(getuid() == 1);
249  rtems_test_assert(geteuid() == 1);
250  rtems_test_assert(getgid() == 3);
251  rtems_test_assert(getegid() == 3);
252  rtems_test_assert(uenv->ngroups == 5);
253  rtems_test_assert(uenv->groups[0] == 1);
254  rtems_test_assert(uenv->groups[1] == 2);
255  rtems_test_assert(uenv->groups[2] == 4);
256  rtems_test_assert(uenv->groups[3] == 5);
257  rtems_test_assert(uenv->groups[4] == 8);
258
259  rv = setuid(0);
260  rtems_test_assert(rv == 0);
261
262  rv = seteuid(0);
263  rtems_test_assert(rv == 0);
264
265  ok = rtems_shell_login_check("chroot", NULL);
266  rtems_test_assert(ok);
267  rtems_test_assert(getuid() == 6);
268  rtems_test_assert(geteuid() == 6);
269  rtems_test_assert(getgid() == 8);
270  rtems_test_assert(getegid() == 8);
271
272  rv = lstat(".", &st_workdir);
273  rtems_test_assert(rv == 0);
274  rtems_test_assert(memcmp(&st_chroot, &st_workdir, sizeof(st_chroot)) == 0);
275
276  rtems_libio_use_global_env();
277}
278
279static void Init(rtems_task_argument arg)
280{
281  TEST_BEGIN();
282
283  test();
284  test_joel();
285
286  TEST_END();
287  rtems_test_exit(0);
288}
289
290#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
291#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
292
293#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 5
294
295#define CONFIGURE_MAXIMUM_TASKS 3
296#define CONFIGURE_MAXIMUM_POSIX_KEYS 2
297#define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 2
298
299#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
300
301#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
302
303#define CONFIGURE_INIT
304
305#include <rtems/confdefs.h>
306
307#define CONFIGURE_SHELL_COMMANDS_INIT
308
309#include <rtems/shellconfig.h>
Note: See TracBrowser for help on using the repository browser.