summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/rtems/rtems-bsd-rc-conf.c
blob: 76f181d69d9973517381d1c47018a4b902ccc95f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Parse the /etc/rc.conf format file and execute the configuration options
 * we want to support.
 */


#include <ctype.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <regex.h>

#include <rtems.h>
#include <rtems/chain.h>

#include <machine/rtems-bsd-rc-conf.h>

/*
 * Max line length.
 */
#define MAX_LINE_SIZE (512)

/*
 * Directive handler chain.
 */
struct rtems_bsd_rc_conf_ {
  const char*  name;       /**< Name of the file. */
  const char*  data;       /**< Pre-processed rc.conf data. */
  size_t       line_count; /**< Number of lines with text. */
  const char** lines;      /**< The lines in the file's text. */
  size_t       line;       /**< The line being processed. */
  bool         verbose;    /**< Verbose processing. */
};

/*
 * Directive handler chain.
 */
typedef struct {
  rtems_chain_node            node;
  const char*                 directive;
  rtems_bsd_rc_conf_directive handler;
} directive;

/*
 * Control of argc and argv.
 */
typedef struct {
  char*        command;
  int          argc;
  const char** argv;
} argc_argv;

/*
 * The chain of directives.
 */
static RTEMS_CHAIN_DEFINE_EMPTY(directives);

static int
argc_argv_create(const char* line, argc_argv* aa)
{
  char* c;
  int   arg;
  char* brk;

  if (strnlen(line, MAX_LINE_SIZE) >= MAX_LINE_SIZE) {
    errno = EFBIG;
    return -1;
  }

  aa->command = strdup(line);
  if (aa->command == NULL) {
    errno = ENOMEM;
    return -1;
  }

  aa->argc = 0;
  aa->argv = NULL;

  /*
   * Yes, the man page says do not use strtok. This is a local oopy, and the
   * re-entrant version is being used.
   */
  c = aa->command;
  while (true) {
    c = strtok_r(c, " \t=\"", &brk);
    if (c == NULL)
      break;
    ++aa->argc;
    c = NULL;
  }

  /*
   * Plus 1 for the trailing NULL present in argv lists.
   */
  aa->argv = calloc(aa->argc + 1, sizeof(char*));
  if (aa->argv == NULL) {
    free(aa->command);
    errno = ENOMEM;
    return -1;
  }

  aa->argv[0] = aa->command;
  c = aa->command;

  for (arg = 1; arg < aa->argc; ++arg) {
    c += strlen(c) + 1;
    while (*c != '\0' && (*c == '"' || isspace(*c)))
      ++c;
    aa->argv[arg] = c;
  }

  return 0;
}

static void
argc_argv_destroy(argc_argv* aa)
{
  free(aa->argv);
  free(aa->command);
  aa->argv = NULL;
  aa->command = NULL;
}

static int
rc_conf_create(rtems_bsd_rc_conf* rc_conf,
               const char*        name,
               const char*        text,
               bool               verbose)
{
  size_t       length;
  char*        line;
  char*        end;
  char*        copy;
  char*        marker;
  const char** lines;
  size_t       line_count;

  memset(rc_conf, 0, sizeof(*rc_conf));

  /*
   * Range check, this makes the rest safer in respect to buffer overflows.
   */
  length = strnlen(text, RTEMS_BSD_RC_CONF_MAX_SIZE);
  if (length == RTEMS_BSD_RC_CONF_MAX_SIZE) {
    errno = E2BIG;
    return -1;
  }

  copy = strdup(text);
  if (copy == NULL) {
    errno = ENOMEM;
    return -1;
  }

  end = copy + length;

  /*
   * Break the text up into lines.
   */
  line_count = 0;
  for (line = copy; line != end; ++line) {
    if (*line == '\n') {
      line_count++;
      *line = '\0';
    }
  }

  lines = malloc(sizeof(char*) * line_count);
  if (lines == NULL) {
    free(copy);
    errno = ENOMEM;
    return -1;
  }

  memset(lines, 0, sizeof(char*) * line_count);

  /*
   * Extract all the lines.
   */
  line_count = 0;
  for (marker = line = copy; line != end; ++line) {
    if (*line == '\0' && line != end) {
      /*
       * Look for a comment.
       */
      char* comment = strchr(marker, '#');
      if (comment != NULL)
        *comment = '\0';
      /*
       * Remove leading whitespace.
       */
      length = strlen(marker);
      while (*marker != '\0' && isspace(*marker))
        memmove(marker, marker + 1, length--);
      /*
       * Remove trailing whitespace.
       */
      length = strlen(marker) - 1;
      while (length > 0 && isspace(marker[length]))
        marker[length--] = '\0';
      /*
       * Set the line.
       */
      lines[line_count] = marker;
      ++line_count;
      marker = line + 1;
    }
  }

  rc_conf->name = name;
  rc_conf->data = copy;
  rc_conf->line_count = line_count;
  rc_conf->lines = lines;
  rc_conf->line = 0;
  rc_conf->verbose = verbose;

  return 0;
}

static void
rc_conf_destroy(rtems_bsd_rc_conf* rc_conf)
{
  free((void*) rc_conf->data);
  rc_conf->data = NULL;
  rc_conf->name = NULL;
}


static int
parse_line(rtems_bsd_rc_conf* rc_conf)
{
  const char*             line;
  rtems_chain_node*       node = rtems_chain_first(&directives);
  const rtems_chain_node* tail = rtems_chain_tail(&directives);
  argc_argv               aa;
  int                     r;

  line = rc_conf->lines[rc_conf->line];

  if (*line == '\0')
    return 0;

  r = argc_argv_create(line, &aa);
  if (r < 0) {
    fprintf(stderr, "error: %s:%lu: creating argc/argv: %s\n",
            rc_conf->name, rc_conf->line + 1, strerror(errno));
    return r;
  }

  while (node != tail) {
    directive* dir = (directive*) node;
    regex_t    rege;
    #define    MAX_MATCHES 1
    regmatch_t matches[MAX_MATCHES];

    r = regcomp(&rege, dir->directive, REG_EXTENDED);
    if (r != 0) {
      char rerror[128];
      regerror(r, &rege, rerror, sizeof(rerror));
      fprintf(stderr, "error: %s:%lu: %s\n",
              rc_conf->name, rc_conf->line + 1, rerror);
      argc_argv_destroy(&aa);
      return -1;
    }

    r = regexec(&rege, aa.argv[0], MAX_MATCHES, matches, 0);
    if (r != 0 && r != REG_NOMATCH) {
      char rerror[128];
      regerror(r, &rege, rerror, sizeof(rerror));
      fprintf(stderr, "error: %s:%lu: %s\n",
              rc_conf->name, rc_conf->line + 1, rerror);
      regfree(&rege);
      argc_argv_destroy(&aa);
      return -1;
    }

    if (r == 0) {
      r = dir->handler(rc_conf, aa.argc, aa.argv);
      if (r < 0)
        fprintf(stderr, "error: %s:%lu: runtime error: %s: %s\n",
                rc_conf->name, rc_conf->line + 1, aa.argv[0], strerror(errno));
      regfree(&rege);
      argc_argv_destroy(&aa);
      return r;
    }

    regfree(&rege);

    node = rtems_chain_next(node);
  }

  errno = ENOSYS;

  fprintf(stderr, "error: %s:%lu: %s: configuration name is not supported\n",
          rc_conf->name, rc_conf->line + 1, aa.argv[0]);

  argc_argv_destroy(&aa);

  return -1;
}

int
rtems_bsd_rc_conf_directive_add(const char*                 dir_regex,
                                rtems_bsd_rc_conf_directive handler)
{
  directive* dir;

  dir = malloc(sizeof(*dir));
  if (dir == NULL) {
    errno = ENOMEM;
    return -1;
  }

  memset(dir, 0, sizeof(*dir));

  dir->directive = strdup(dir_regex);
  if (dir->directive == NULL) {
    free(dir);
    errno = ENOMEM;
    return -1;
  }

  dir->handler = handler;

  rtems_chain_append(&directives, &dir->node);

  return 0;
}

int
rtems_bsd_run_rc_conf_script(const char* name, const char* text, bool verbose)
{
  rtems_bsd_rc_conf rc_conf;
  int               r;

  if (verbose)
    printf("rc.conf: processing: %s size:%lu\n", name, strlen(text));

  r = rc_conf_create(&rc_conf, name, text, verbose);
  if (r < 0) {
    fprintf(stderr, "error: %s: parse error: %s\n",
            name, strerror(errno));
    return -1;
  }

  while (rc_conf.line < rc_conf.line_count) {
    r = parse_line(&rc_conf);
    if (r < 0)
      break;
    ++rc_conf.line;
  }

  rc_conf_destroy(&rc_conf);

  return r;
}

int
rtems_bsd_run_rc_conf(const char* name, bool verbose)
{
  struct stat sb;
  int         r;
  char*       rc_conf;
  FILE*       file;

  r = stat(name, &sb);
  if (r < 0)
    return r;

  rc_conf = malloc(sb.st_size);
  if (rc_conf == NULL) {
    errno = ENOMEM;
    return -1;
  }

  if (verbose)
    printf("rc.conf: loading: %s\n", name);

  file = fopen(name, "r");
  if (file == NULL) {
    free(rc_conf);
    return -1;
  }

  if (fread(rc_conf, 1, sb.st_size, file) != sb.st_size) {
    fclose(file);
    free(rc_conf);
    return -1;
  }

  fclose(file);

  r = rtems_bsd_run_rc_conf_script(name, rc_conf, verbose);

  free(rc_conf);

  return r;
}

int
rtems_bsd_run_etc_rc_conf(bool verbose)
{
  return rtems_bsd_run_rc_conf("/etc/rc.conf", verbose);
}

const char*
rtems_bsd_rc_conf_name(rtems_bsd_rc_conf* rc_conf)
{
  return rc_conf->name;
}

int
rtems_bsd_rc_conf_line(rtems_bsd_rc_conf* rc_conf)
{
  return rc_conf->line + 1;
}

bool
 rtems_bsd_rc_conf_verbose(rtems_bsd_rc_conf* rc_conf)
{
  return rc_conf->verbose;
}

void rtems_bsd_rc_conf_print_cmd(rtems_bsd_rc_conf* rc_conf,
                                 const char*        name,
                                 int                argc,
                                 const char**       argv)
{
  if (rc_conf->verbose) {
    int arg;
    printf("rc.conf: %s:%lu: %s:", rc_conf->name, rc_conf->line + 1, name);
    for (arg = 0; arg < argc; ++arg)
      printf(" %s", argv[arg]);
    printf("\n");
  }
}