New API: list-9p lists 9p filesystem mount tags (RHBZ#714981).
[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., 675 Mass Ave, Cambridge, MA 02139, 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         free_stringslen (r, size);
86         closedir (dir);
87         return NULL;
88       }
89
90       free (mount_tag);
91     }
92   }
93
94   /* Check readdir didn't fail */
95   if (errno != 0) {
96     reply_with_perror ("readdir: /sys/block");
97     free_stringslen (r, size);
98     closedir (dir);
99     return NULL;
100   }
101
102   /* Close the directory handle */
103   if (closedir (dir) == -1) {
104     reply_with_perror ("closedir: /sys/block");
105     free_stringslen (r, size);
106     return NULL;
107   }
108
109   /* Sort the tags.  Note that r might be NULL if there are no tags. */
110   if (r != NULL)
111     sort_strings (r, size);
112
113   /* NULL terminate the list */
114   if (add_string (&r, &size, &alloc, NULL) == -1)
115     return NULL;
116
117   return r;
118 }
119
120 /* Read whole file into dynamically allocated array.  If there is an
121  * error, DON'T call reply_with_perror, just return NULL.  Returns a
122  * \0-terminated string.
123  */
124 static char *
125 read_whole_file (const char *filename)
126 {
127   char *r = NULL;
128   size_t alloc = 0, size = 0;
129   int fd;
130
131   fd = open (filename, O_RDONLY);
132   if (fd == -1) {
133     perror (filename);
134     return NULL;
135   }
136
137   while (1) {
138     alloc += 256;
139     char *r2 = realloc (r, alloc);
140     if (r2 == NULL) {
141       perror ("realloc");
142       free (r);
143       return NULL;
144     }
145     r = r2;
146
147     /* The '- 1' in the size calculation ensures there is space below
148      * to add \0 to the end of the input.
149      */
150     ssize_t n = read (fd, r + size, alloc - size - 1);
151     if (n == -1) {
152       perror (filename);
153       free (r);
154       return NULL;
155     }
156     if (n == 0)
157       break;
158     size += n;
159   }
160
161   if (close (fd) == -1) {
162     perror (filename);
163     free (r);
164     return NULL;
165   }
166
167   r[size] = '\0';
168
169   return r;
170 }