summaryrefslogtreecommitdiff
path: root/pci-arbiter/options.c
blob: 2023fd9e2bd9be4566e66a38a0bcb4977c553376 (plain)
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
/*
   Copyright (C) 2017 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.org>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Fsysopts and command line option parsing */

#include "options.h"

#include <stdlib.h>
#include <argp.h>
#include <argz.h>
#include <error.h>
#include <regex.h>

#include "pcifs.h"

#define PCI_SLOT_REGEX "^(([0-9a-fA-F]{4}):)?([0-9a-fA-F]{2}):([0-9a-fA-F]{2})\\.([0-7])$"
#define PCI_SLOT_REGEX_GROUPS 6	// 2: Domain, 3: Bus, 4: Dev, 5: Func

/* Fsysopts and command line option parsing */

/* Adds an empty interface slot to H, and sets H's current interface to it, or
   returns an error. */
static error_t
parse_hook_add_set (struct parse_hook *h)
{
  struct pcifs_perm *new = realloc (h->permsets,
				    (h->num_permsets +
				     1) * sizeof (struct pcifs_perm));
  if (!new)
    return ENOMEM;

  h->permsets = new;
  h->num_permsets++;
  h->curset = new + h->num_permsets - 1;
  h->curset->domain = -1;
  h->curset->bus = -1;
  h->curset->dev = -1;
  h->curset->func = -1;
  h->curset->d_class = -1;
  h->curset->d_subclass = -1;
  h->curset->uid = -1;
  h->curset->gid = -1;

  return 0;
}

/*
 * Some options depend on other options to be valid. Check whether all
 * dependences are met.
 */
static error_t
check_options_validity (struct parse_hook *h)
{
  int i;
  struct pcifs_perm *p;

  for (p = h->permsets, i = 0; i < h->num_permsets; i++, p++)
    {
      if ((p->func >= 0 && p->dev < 0)
	  || (p->dev >= 0 && p->bus < 0)
	  || (p->bus >= 0 && p->domain < 0)
	  || (p->d_subclass >= 0 && p->d_class < 0)
	  || ((p->uid >= 0 || p->gid >= 0)
	      && (p->d_class < 0 && p->domain < 0)) || ((p->d_class >= 0
							 || p->domain >= 0)
							&& !(p->uid >= 0
							     || p->gid >= 0)))
	return EINVAL;
    }

  return 0;
}

/* Option parser */
static error_t
parse_opt (int opt, char *arg, struct argp_state *state)
{
  error_t err = 0;
  struct parse_hook *h = state->hook;
  regex_t slot_regex;
  regmatch_t slot_regex_groups[PCI_SLOT_REGEX_GROUPS];
  char regex_group_val[5];

  /* Return _ERR from this routine */
#define RETURN(_err)                          \
  do { return _err; } while (0)

  /* Print a parsing error message and (if exiting is turned off) return the
     error code ERR.  */
#define PERR(err, fmt, args...)               \
  do { argp_error (state, fmt , ##args); RETURN (err); } while (0)

  /* Like PERR but for non-parsing errors.  */
#define FAIL(rerr, status, perr, fmt, args...)  \
  do{ argp_failure (state, status, perr, fmt , ##args); RETURN (rerr); } while(0)

  if (!arg && state->next < state->argc && (*state->argv[state->next] != '-'))
    {
      arg = state->argv[state->next];
      state->next++;
    }

  /* Compile regular expression to check --slot option */
  err = regcomp (&slot_regex, PCI_SLOT_REGEX, REG_EXTENDED);
  if (err)
    FAIL (err, 1, err, "option parsing");

  switch (opt)
    {
    case 'C':
      h->curset->d_class = strtol (arg, 0, 16);
      break;
    case 'c':
      h->curset->d_subclass = strtol (arg, 0, 16);
      break;
    case 'd':
      h->curset->domain = strtol (arg, 0, 16);
      break;
    case 'b':
      if (h->curset->domain < 0)
	h->curset->domain = 0;

      h->curset->bus = strtol (arg, 0, 16);
      break;
    case 's':
      h->curset->dev = strtol (arg, 0, 16);
      break;
    case 'f':
      h->curset->func = strtol (arg, 0, 16);
      break;
    case 'D':
      err =
	regexec (&slot_regex, arg, PCI_SLOT_REGEX_GROUPS, slot_regex_groups,
		 0);
      if (!err)
	{
	  // Domain, 0000 by default
	  if (slot_regex_groups[2].rm_so >= 0)
	    {
	      strncpy (regex_group_val, arg + slot_regex_groups[2].rm_so, 4);
	      regex_group_val[4] = 0;
	    }
	  else
	    {
	      strncpy (regex_group_val, "0000", 5);
	    }

	  h->curset->domain = strtol (regex_group_val, 0, 16);

	  // Bus
	  strncpy (regex_group_val, arg + slot_regex_groups[3].rm_so, 2);
	  regex_group_val[2] = 0;

	  h->curset->bus = strtol (regex_group_val, 0, 16);

	  // Dev
	  strncpy (regex_group_val, arg + slot_regex_groups[4].rm_so, 2);
	  regex_group_val[2] = 0;

	  h->curset->dev = strtol (regex_group_val, 0, 16);

	  // Func
	  regex_group_val[0] = arg[slot_regex_groups[5].rm_so];
	  regex_group_val[1] = 0;

	  h->curset->func = strtol (regex_group_val, 0, 16);
	}
      else
	{
	  PERR (err, "Wrong PCI slot. Format: [<domain>:]<bus>:<dev>.<func>");
	}
      break;
    case 'U':
      if (h->curset->uid >= 0)
	parse_hook_add_set (h);

      h->curset->uid = atoi (arg);
      break;
    case 'G':
      if (h->curset->gid >= 0)
	parse_hook_add_set (h);

      h->curset->gid = atoi (arg);
      break;
    case 'n':
      h->ncache_len = atoi (arg);
      break;
    case ARGP_KEY_INIT:
      /* Initialize our parsing state.  */
      h = malloc (sizeof (struct parse_hook));
      if (!h)
	FAIL (ENOMEM, 1, ENOMEM, "option parsing");

      h->permsets = 0;
      h->num_permsets = 0;
      h->ncache_len = NODE_CACHE_MAX;
      err = parse_hook_add_set (h);
      if (err)
	FAIL (err, 1, err, "option parsing");

      state->hook = h;
      break;

    case ARGP_KEY_SUCCESS:
      /* Check option dependencies */
      err = check_options_validity (h);
      if (err)
	{
	  if (fs->root)
	    {
	      /*
	       * If there's an option dependence error but the server is yet
	       * running, print the error and do nothing.
	       */
	      free (h->permsets);
	      free (h);
	      PERR (err, "Invalid options, no changes were applied");
	    }
	  else
	    /* Invalid options on a non-started server, exit() */
	    PERR (err, "Option dependence error");
	}

      /* Set permissions to FS */
      if (fs->params.perms)
	free (fs->params.perms);
      fs->params.perms = h->permsets;
      fs->params.num_perms = h->num_permsets;

      /* Set cache len */
      fs->params.node_cache_max = h->ncache_len;

      if (fs->root)
	{
	  /*
	   * FS is already initialized, that means we've been called by fsysopts.
	   * Update permissions.
	   */

	  /* Don't accept new RPCs during this process */
	  err = ports_inhibit_all_rpcs ();
	  if (err)
	    return err;

	  err = fs_set_permissions (fs);

	  /* Accept RPCs again */
	  ports_resume_all_rpcs ();
	}

      /* Free the hook */
      free (h);

      break;

    case ARGP_KEY_ERROR:
      /* Parsing error occurred, free the permissions. */
      free (h->permsets);
      free (h);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  /* Free allocated regular expression for the --slot option */
  regfree (&slot_regex);

  return err;
}

/*
 * Print current permissions. Called by fsysopts.
 */
error_t
netfs_append_args (char **argz, size_t * argz_len)
{
  error_t err = 0;
  struct pcifs_perm *p;
  int i;

#define ADD_OPT(fmt, args...)           \
  do { char buf[100];                   \
       if (! err) {                     \
         snprintf (buf, sizeof buf, fmt , ##args);      \
         err = argz_add (argz, argz_len, buf); } } while (0)

  for (i = 0, p = fs->params.perms; i < fs->params.num_perms; i++, p++)
    {
      if (p->d_class >= 0)
	ADD_OPT ("--class=0x%02x", p->d_class);
      if (p->d_subclass >= 0)
	ADD_OPT ("--subclass=0x%02x", p->d_subclass);
      if (p->domain >= 0)
	ADD_OPT ("--domain=0x%04x", p->domain);
      if (p->bus >= 0)
	ADD_OPT ("--bus=0x%02x", p->bus);
      if (p->dev >= 0)
	ADD_OPT ("--slot=0x%02x", p->dev);
      if (p->func >= 0)
	ADD_OPT ("--func=%01u", p->func);
      if (p->uid >= 0)
	ADD_OPT ("--uid=%u", p->uid);
      if (p->gid >= 0)
	ADD_OPT ("--gid=%u", p->gid);
    }

  if (fs->params.node_cache_max != NODE_CACHE_MAX)
    ADD_OPT ("--ncache=%u", fs->params.node_cache_max);

#undef ADD_OPT
  return err;
}

struct argp pci_argp = { options, parse_opt, 0, doc };

struct argp *netfs_runtime_argp = &pci_argp;