Update FSF address.
[libguestfs.git] / daemon / 9p.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <fcntl.h>
31
32 #include "daemon.h"
33 #include "actions.h"
34
35 #define BUS_PATH "/sys/bus/virtio/drivers/9pnet_virtio"
36
37 static char *read_whole_file (const char *filename);
38
39 /* https://bugzilla.redhat.com/show_bug.cgi?id=714981#c1 */
40 char **
41 do_list_9p (void)
42 {
43   char **r = NULL;
44   int size = 0, alloc = 0;
45
46   DIR *dir;
47   int err = 0;
48
49   dir = opendir (BUS_PATH);
50   if (!dir) {
51     perror ("opendir: " BUS_PATH);
52     if (errno != ENOENT)
53       return NULL;
54
55     /* If this directory doesn't exist, it probably means that
56      * the virtio driver isn't loaded.  Don't return an error
57      * in this case, but return an empty list.
58      */
59     if (add_string (&r, &size, &alloc, NULL) == -1)
60       return NULL;
61
62     return r;
63   }
64
65   while (1) {
66     errno = 0;
67     struct dirent *d = readdir (dir);
68     if (d == NULL) break;
69
70     if (STRPREFIX (d->d_name, "virtio")) {
71       char mount_tag_path[256];
72       snprintf (mount_tag_path, sizeof mount_tag_path,
73                 BUS_PATH "/%s/mount_tag", d->d_name);
74
75       /* A bit unclear, but it looks like the virtio transport allows
76        * the mount tag length to be unlimited (or up to 65536 bytes).
77        * See: linux/include/linux/virtio_9p.h
78        */
79       char *mount_tag = read_whole_file (mount_tag_path);
80       if (mount_tag == 0)
81         continue;
82
83       if (add_string (&r, &size, &alloc, mount_tag) == -1) {
84         free (mount_tag);
85         closedir (dir);
86         return NULL;
87       }
88
89       free (mount_tag);
90     }
91   }
92
93   /* Check readdir didn't fail */
94   if (errno != 0) {
95     reply_with_perror ("readdir: /sys/block");
96     free_stringslen (r, size);
97     closedir (dir);
98     return NULL;
99   }
100
101   /* Close the directory handle */
102   if (closedir (dir) == -1) {
103     reply_with_perror ("closedir: /sys/block");
104     free_stringslen (r, size);
105     return NULL;
106   }
107
108   /* Sort the tags.  Note that r might be NULL if there are no tags. */
109   if (r != NULL)
110     sort_strings (r, size);
111
112   /* NULL terminate the list */
113   if (add_string (&r, &size, &alloc, NULL) == -1)
114     return NULL;
115
116   return r;
117 }
118
119 /* Read whole file into dynamically allocated array.  If there is an
120  * error, DON'T call reply_with_perror, just return NULL.  Returns a
121  * \0-terminated string.
122  */
123 static char *
124 read_whole_file (const char *filename)
125 {
126   char *r = NULL;
127   size_t alloc = 0, size = 0;
128   int fd;
129
130   fd = open (filename, O_RDONLY);
131   if (fd == -1) {
132     perror (filename);
133     return NULL;
134   }
135
136   while (1) {
137     alloc += 256;
138     char *r2 = realloc (r, alloc);
139     if (r2 == NULL) {
140       perror ("realloc");
141       free (r);
142       return NULL;
143     }
144     r = r2;
145
146     /* The '- 1' in the size calculation ensures there is space below
147      * to add \0 to the end of the input.
148      */
149     ssize_t n = read (fd, r + size, alloc - size - 1);
150     if (n == -1) {
151       perror (filename);
152       free (r);
153       return NULL;
154     }
155     if (n == 0)
156       break;
157     size += n;
158   }
159
160   if (close (fd) == -1) {
161     perror (filename);
162     free (r);
163     return NULL;
164   }
165
166   r[size] = '\0';
167
168   return r;
169 }
170
171 /* Takes optional arguments, consult optargs_bitmask. */
172 int
173 do_mount_9p (const char *mount_tag, const char *mountpoint, const char *options)
174 {
175   char *mp = NULL, *opts = NULL, *err = NULL;
176   struct stat statbuf;
177   int r = -1;
178
179   ABS_PATH (mountpoint, , return -1);
180
181   mp = sysroot_path (mountpoint);
182   if (!mp) {
183     reply_with_perror ("malloc");
184     goto out;
185   }
186
187   /* Check the mountpoint exists and is a directory. */
188   if (stat (mp, &statbuf) == -1) {
189     reply_with_perror ("%s", mountpoint);
190     goto out;
191   }
192   if (!S_ISDIR (statbuf.st_mode)) {
193     reply_with_perror ("%s: mount point is not a directory", mountpoint);
194     goto out;
195   }
196
197   /* Add trans=virtio to the options. */
198   if ((optargs_bitmask & GUESTFS_MOUNT_9P_OPTIONS_BITMASK) &&
199       STRNEQ (options, "")) {
200     if (asprintf (&opts, "trans=virtio,%s", options) == -1) {
201       reply_with_perror ("asprintf");
202       goto out;
203     }
204   }
205   else {
206     opts = strdup ("trans=virtio");
207     if (opts == NULL) {
208       reply_with_perror ("strdup");
209       goto out;
210     }
211   }
212
213   r = command (NULL, &err,
214                "mount", "-o", opts, "-t", "9p", mount_tag, mp, NULL);
215   if (r == -1) {
216     reply_with_error ("%s on %s: %s", mount_tag, mountpoint, err);
217     goto out;
218   }
219
220   r = 0;
221  out:
222   free (err);
223   free (opts);
224   free (mp);
225   return r;
226 }