summaryrefslogtreecommitdiffstats
path: root/daemon/available.c
blob: 08e72f302576f8c561af7146751d3d53777853f8 (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
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "c-ctype.h"

#include "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"
#include "optgroups.h"

int
do_available (char *const *groups)
{
  int av;
  size_t i, j;

  for (i = 0; groups[i] != NULL; ++i) {
    for (j = 0; optgroups[j].group != NULL; ++j) {
      if (STREQ (groups[i], optgroups[j].group)) {
        av = optgroups[j].available ();
        if (!av) {
          reply_with_error ("%s: group not available", optgroups[j].group);
          return -1;
        }
        break; /* out of for (j) loop */
      }
    }

    /* Unknown group? */
    if (optgroups[j].group == NULL) {
      reply_with_error ("%s: unknown group", groups[i]);
      return -1;
    }
  }

  return 0;
}

char **
do_available_all_groups (void)
{
  size_t i;
  DECLARE_STRINGSBUF (groups);

  for (i = 0; optgroups[i].group != NULL; ++i) {
    if (add_string (&groups, optgroups[i].group) == -1)
      return NULL;
  }

  if (end_stringsbuf (&groups) == -1)
    return NULL;

  return groups.argv;           /* caller frees */
}

/* Search for filesystem in /proc/filesystems, ignoring "nodev". */
static int
test_proc_filesystems (const char *filesystem)
{
  size_t len = strlen (filesystem) + 32;
  char regex[len];
  char *err;
  int r;

  snprintf (regex, len, "^[[:space:]]*%s$", filesystem);

  r = commandr (NULL, &err, "grep", regex, "/proc/filesystems", NULL);
  if (r == -1 || r >= 2) {
    fprintf (stderr, "grep /proc/filesystems: %s", err);
    free (err);
    return -1;
  }
  free (err);

  return r == 0;
}

/* Do modprobe, ignore any errors. */
static void
modprobe (const char *module)
{
  command (NULL, NULL, "modprobe", module, NULL);
}

/* Internal function for testing if a filesystem is available.  Note
 * this must not call reply_with_error functions.
 */
int
filesystem_available (const char *filesystem)
{
  int r;

  r = test_proc_filesystems (filesystem);
  if (r == -1 || r > 0)
    return r;

  /* Not found: try to modprobe the module, then test again. */
  if (optgroup_linuxmodules_available ()) {
    modprobe (filesystem);

    r = test_proc_filesystems (filesystem);
    if (r == -1)
      return -1;
  }

  return r;
}

int
do_filesystem_available (const char *filesystem)
{
  size_t i, len = strlen (filesystem);
  int r;

  for (i = 0; i < len; ++i) {
    if (!c_isalnum (filesystem[i]) && filesystem[i] != '_') {
      reply_with_error ("filesystem name contains non-alphanumeric characters");
      return -1;
    }
  }

  r = filesystem_available (filesystem);
  if (r == -1) {
    reply_with_error ("error testing for filesystem availability; "
                      "enable verbose mode and look at preceeding output");
    return -1;
  }

  return r;
}