chiark / gitweb /
import: prefer usec_t over time_t
[elogind.git] / src / machine / image.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/statfs.h>
23
24 #include "strv.h"
25 #include "utf8.h"
26 #include "btrfs-util.h"
27 #include "path-util.h"
28 #include "image.h"
29
30 static const char image_search_path[] =
31         "/var/lib/container\0"
32         "/var/lib/machine\0";
33
34 Image *image_unref(Image *i) {
35         if (!i)
36                 return NULL;
37
38         free(i->name);
39         free(i->path);
40         free(i);
41         return NULL;
42 }
43
44 static int image_new(
45                 ImageType t,
46                 const char *name,
47                 const char *path,
48                 bool read_only,
49                 usec_t mtime,
50                 usec_t btime,
51                 Image **ret) {
52
53         _cleanup_(image_unrefp) Image *i = NULL;
54
55         assert(t >= 0);
56         assert(t < _IMAGE_TYPE_MAX);
57         assert(name);
58         assert(ret);
59
60         i = new0(Image, 1);
61         if (!i)
62                 return -ENOMEM;
63
64         i->type = t;
65         i->read_only = read_only;
66         i->mtime = mtime;
67         i->btime = btime;
68
69         i->name = strdup(name);
70         if (!i->name)
71                 return -ENOMEM;
72
73         if (path) {
74                 i->path = strjoin(path, "/", name, NULL);
75                 if (!i->path)
76                         return -ENOMEM;
77
78                 path_kill_slashes(i->path);
79         }
80
81         *ret = i;
82         i = NULL;
83
84         return 0;
85 }
86
87 static int image_make(int dfd, const char *name, const char *path, Image **ret) {
88         struct stat st;
89         int r;
90
91         assert(dfd >= 0);
92         assert(name);
93
94         /* We explicitly *do* follow symlinks here, since we want to
95          * allow symlinking trees into /var/lib/container/, and treat
96          * them normally. */
97
98         if (fstatat(dfd, name, &st, 0) < 0)
99                 return -errno;
100
101         if (S_ISDIR(st.st_mode)) {
102
103                 if (!ret)
104                         return 1;
105
106                 /* btrfs subvolumes have inode 256 */
107                 if (st.st_ino == 256) {
108                         _cleanup_close_ int fd = -1;
109                         struct statfs sfs;
110
111                         fd = openat(dfd, name, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
112                         if (fd < 0)
113                                 return -errno;
114
115                         if (fstatfs(fd, &sfs) < 0)
116                                 return -errno;
117
118                         if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
119                                 usec_t btime = 0;
120                                 int ro;
121
122                                 /* It's a btrfs subvolume */
123
124                                 ro = btrfs_subvol_is_read_only_fd(fd);
125                                 if (ro < 0)
126                                         return ro;
127
128                                 /* r = btrfs_subvol_get_btime(fd, &btime); */
129                                 /* if (r < 0) */
130                                 /*         return r; */
131
132                                 r = image_new(IMAGE_SUBVOLUME,
133                                               name,
134                                               path,
135                                               ro,
136                                               0,
137                                               btime,
138                                               ret);
139                                 if (r < 0)
140                                         return r;
141
142                                 return 1;
143                         }
144                 }
145
146                 /* It's just a normal directory. */
147
148                 r = image_new(IMAGE_DIRECTORY,
149                               name,
150                               path,
151                               false,
152                               0,
153                               0,
154                               ret);
155                 if (r < 0)
156                         return r;
157
158                 return 1;
159
160         } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
161
162                 /* It's a GPT block device */
163
164                 if (!ret)
165                         return 1;
166
167                 r = image_new(IMAGE_GPT,
168                               name,
169                               path,
170                               !!(st.st_mode & 0222),
171                               timespec_load(&st.st_mtim),
172                               0,
173                               ret);
174                 if (r < 0)
175                         return r;
176
177                 return 1;
178         }
179
180         return 0;
181 }
182
183 int image_find(const char *name, Image **ret) {
184         const char *path;
185         int r;
186
187         assert(name);
188
189         /* There are no images with invalid names */
190         if (!image_name_is_valid(name))
191                 return 0;
192
193         NULSTR_FOREACH(path, image_search_path) {
194                 _cleanup_closedir_ DIR *d = NULL;
195
196                 d = opendir(path);
197                 if (!d) {
198                         if (errno == ENOENT)
199                                 continue;
200
201                         return -errno;
202                 }
203
204                 r = image_make(dirfd(d), name, path, ret);
205                 if (r == 0 || r == -ENOENT)
206                         continue;
207                 if (r < 0)
208                         return r;
209
210                 return 1;
211         }
212
213         return 0;
214 };
215
216 int image_discover(Hashmap *h) {
217         const char *path;
218         int r;
219
220         assert(h);
221
222         NULSTR_FOREACH(path, image_search_path) {
223                 _cleanup_closedir_ DIR *d = NULL;
224                 struct dirent *de;
225
226                 d = opendir(path);
227                 if (!d) {
228                         if (errno == ENOENT)
229                                 return 0;
230
231                         return -errno;
232                 }
233
234                 FOREACH_DIRENT_ALL(de, d, return -errno) {
235                         _cleanup_(image_unrefp) Image *image = NULL;
236
237                         if (!image_name_is_valid(de->d_name))
238                                 continue;
239
240                         if (hashmap_contains(h, de->d_name))
241                                 continue;
242
243                         r = image_make(dirfd(d), de->d_name, path, &image);
244                         if (r == 0 || r == -ENOENT)
245                                 continue;
246                         if (r < 0)
247                                 return r;
248
249                         r = hashmap_put(h, image->name, image);
250                         if (r < 0)
251                                 return r;
252
253                         image = NULL;
254                 }
255         }
256
257         return 0;
258 }
259
260 void image_hashmap_free(Hashmap *map) {
261         Image *i;
262
263         while ((i = hashmap_steal_first(map)))
264                 image_unref(i);
265
266         hashmap_free(map);
267 }
268
269 static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
270         [IMAGE_DIRECTORY] = "directory",
271         [IMAGE_SUBVOLUME] = "subvolume",
272         [IMAGE_GPT] = "gpt",
273 };
274
275 DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);