chiark / gitweb /
volume_id: run only once into a timeout for unreadable devices
[elogind.git] / extras / volume_id / lib / volume_id.c
1 /*
2  * volume_id - reads volume label and uuid
3  *
4  * Copyright (C) 2005-2007 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "libvolume_id.h"
29 #include "util.h"
30
31 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
32
33 struct prober {
34         volume_id_probe_fn_t prober;
35         const char *name[4];
36 };
37
38 static const struct prober prober_raid[] = {
39         { volume_id_probe_linux_raid, { "linux_raid", } },
40         { volume_id_probe_ddf_raid, { "ddf_raid", } },
41         { volume_id_probe_intel_software_raid, { "isw_raid", } },
42         { volume_id_probe_lsi_mega_raid, { "lsi_mega_raid", } },
43         { volume_id_probe_via_raid, { "via_raid", } },
44         { volume_id_probe_silicon_medley_raid, { "silicon_medley_raid", } },
45         { volume_id_probe_nvidia_raid, { "nvidia_raid", } },
46         { volume_id_probe_promise_fasttrack_raid, { "promise_fasttrack_raid", } },
47         { volume_id_probe_highpoint_45x_raid, { "highpoint_raid", } },
48         { volume_id_probe_adaptec_raid, { "adaptec_raid", } },
49         { volume_id_probe_jmicron_raid, { "jmicron_raid", } },
50         { volume_id_probe_lvm1, { "lvm1", } },
51         { volume_id_probe_lvm2, { "lvm2", } },
52         { volume_id_probe_highpoint_37x_raid, { "highpoint_raid", } },
53 };
54
55 static const struct prober prober_filesystem[] = {
56         { volume_id_probe_vfat, { "vfat", } },
57         { volume_id_probe_linux_swap, { "swap", } },
58         { volume_id_probe_luks, { "luks", } },
59         { volume_id_probe_xfs, { "xfs", } },
60         { volume_id_probe_ext, { "ext2", "ext3", "jbd", } },
61         { volume_id_probe_reiserfs, { "reiserfs", "reiser4", } },
62         { volume_id_probe_jfs, { "jfs", } },
63         { volume_id_probe_udf, { "udf", } },
64         { volume_id_probe_iso9660, { "iso9660", } },
65         { volume_id_probe_hfs_hfsplus, { "hfs", "hfsplus", } },
66         { volume_id_probe_ufs, { "ufs", } },
67         { volume_id_probe_ntfs, { "ntfs", } },
68         { volume_id_probe_cramfs, { "cramfs", } },
69         { volume_id_probe_romfs, { "romfs", } },
70         { volume_id_probe_hpfs, { "hpfs", } },
71         { volume_id_probe_sysv, { "sysv", "xenix", } },
72         { volume_id_probe_minix, { "minix",  } },
73         { volume_id_probe_ocfs1, { "ocfs1", } },
74         { volume_id_probe_ocfs2, { "ocfs2", } },
75         { volume_id_probe_vxfs, { "vxfs", } },
76         { volume_id_probe_squashfs, { "squashfs", } },
77         { volume_id_probe_netware, { "netware", } },
78 };
79
80 /* the user can overwrite this log function */
81 static void default_log(int priority, const char *file, int line, const char *format, ...)
82 {
83         return;
84 }
85
86 volume_id_log_fn_t volume_id_log_fn = default_log;
87
88 /**
89  * volume_id_get_label:
90  * @type: Type string.
91  *
92  * Lookup the probing function for a specific type.
93  *
94  * Returns: The probing function for the given type, #NULL otherwise.
95  **/
96 const volume_id_probe_fn_t *volume_id_get_prober_by_type(const char *type)
97 {
98         unsigned int p, n;
99
100         if (type == NULL)
101                 return NULL;
102
103         for (p = 0; p < ARRAY_SIZE(prober_raid); p++)
104                 for (n = 0; prober_raid[p].name[n] !=  NULL; n++)
105                         if (strcmp(type, prober_raid[p].name[n]) == 0)
106                                 return &prober_raid[p].prober;
107         for (p = 0; p < ARRAY_SIZE(prober_filesystem); p++)
108                 for (n = 0; prober_filesystem[p].name[n] !=  NULL; n++)
109                         if (strcmp(type, prober_filesystem[p].name[n]) == 0)
110                                 return &prober_filesystem[p].prober;
111         return NULL;
112 }
113
114 /**
115  * volume_id_get_label:
116  * @id: Probing context.
117  * @label: Label string. Must not be freed by the caller.
118  *
119  * Get the label string after a successful probe. Unicode
120  * is translated to UTF-8.
121  *
122  * Returns: 1 if the value was set, 0 otherwise.
123  **/
124 int volume_id_get_label(struct volume_id *id, const char **label)
125 {
126         if (id == NULL)
127                 return 0;
128         if (label == NULL)
129                 return 0;
130         if (id->usage_id == VOLUME_ID_UNUSED)
131                 return 0;
132
133         *label = id->label;
134         return 1;
135 }
136
137 /**
138  * volume_id_get_label_raw:
139  * @id: Probing context.
140  * @label: Label byte array. Must not be freed by the caller.
141  * @len: Length of raw label byte array.
142  *
143  * Get the raw label byte array after a successful probe. It may
144  * contain undecoded multibyte character streams.
145  *
146  * Returns: 1 if the value was set, 0 otherwise.
147  **/
148 int volume_id_get_label_raw(struct volume_id *id, const uint8_t **label, size_t *len)
149 {
150         if (id == NULL)
151                 return 0;
152         if (label == NULL)
153                 return 0;
154         if (len == NULL)
155                 return 0;
156         if (id->usage_id == VOLUME_ID_UNUSED)
157                 return 0;
158
159         *label = id->label_raw;
160         *len = id->label_raw_len;
161         return 1;
162 }
163
164 /**
165  * volume_id_get_uuid:
166  * @id: Probing context.
167  * @uuid: UUID string. Must not be freed by the caller.
168  *
169  * Get the raw UUID string after a successful probe.
170  *
171  * Returns: 1 if the value was set, 0 otherwise.
172  **/
173 int volume_id_get_uuid(struct volume_id *id, const char **uuid)
174 {
175         if (id == NULL)
176                 return 0;
177         if (uuid == NULL)
178                 return 0;
179         if (id->usage_id == VOLUME_ID_UNUSED)
180                 return 0;
181
182         *uuid = id->uuid;
183         return 1;
184 }
185
186 /**
187  * volume_id_get_uuid_raw:
188  * @id: Probing context.
189  * @uuid: UUID byte array. Must not be freed by the caller.
190  * @len: Length of raw UUID byte array.
191  *
192  * Get the raw UUID byte array after a successful probe. It may
193  * contain unconverted endianes values.
194  *
195  * Returns: 1 if the value was set, 0 otherwise.
196  **/
197 int volume_id_get_uuid_raw(struct volume_id *id, const uint8_t **uuid, size_t *len)
198 {
199         if (id == NULL)
200                 return 0;
201         if (uuid == NULL)
202                 return 0;
203         if (len == NULL)
204                 return 0;
205         if (id->usage_id == VOLUME_ID_UNUSED)
206                 return 0;
207
208         *uuid = id->uuid_raw;
209         *len = id->uuid_raw_len;
210         return 1;
211 }
212
213 /**
214  * volume_id_get_usage:
215  * @id: Probing context.
216  * @usage: Usage string. Must not be freed by the caller.
217  *
218  * Get the usage string after a successful probe.
219  *
220  * Returns: 1 if the value was set, 0 otherwise.
221  **/
222 int volume_id_get_usage(struct volume_id *id, const char **usage)
223 {
224         if (id == NULL)
225                 return 0;
226         if (usage == NULL)
227                 return 0;
228         if (id->usage_id == VOLUME_ID_UNUSED)
229                 return 0;
230
231         *usage = id->usage;
232         return 1;
233 }
234
235 /**
236  * volume_id_get_type:
237  * @id: Probing context
238  * @type: Type string. Must not be freed by the caller.
239  *
240  * Get the type string after a successful probe.
241  *
242  * Returns: 1 if the value was set, 0 otherwise.
243  **/
244 int volume_id_get_type(struct volume_id *id, const char **type)
245 {
246         if (id == NULL)
247                 return 0;
248         if (type == NULL)
249                 return 0;
250         if (id->usage_id == VOLUME_ID_UNUSED)
251                 return 0;
252
253         *type = id->type;
254         return 1;
255 }
256
257 /**
258  * volume_id_get_type_version:
259  * @id: Probing context.
260  * @type_version: Type version string. Must not be freed by the caller.
261  *
262  * Get the Type version string after a successful probe.
263  *
264  * Returns: 1 if the value was set, 0 otherwise.
265  **/
266 int volume_id_get_type_version(struct volume_id *id, const char **type_version)
267 {
268         if (id == NULL)
269                 return 0;
270         if (type_version == NULL)
271                 return 0;
272         if (id->usage_id == VOLUME_ID_UNUSED)
273                 return 0;
274
275         *type_version = id->type_version;
276         return 1;
277 }
278
279 static int needs_encoding(const char c)
280 {
281         if ((c >= '0' && c <= '9') ||
282             (c >= 'A' && c <= 'Z') ||
283             (c >= 'a' && c <= 'z') ||
284             strchr(ALLOWED_CHARS, c))
285                 return 0;
286         return 1;
287 }
288
289 /**
290  * volume_id_encode_string:
291  * @str: Input string to be encoded.
292  * @str_enc: Target string to store the encoded input.
293  * @len: Location to store the encoded string. The target string,
294  * which may be four times as long as the input string.
295  *
296  * Encode all potentially unsafe characters of a string to the
297  * corresponding hex value prefixed by '\x'.
298  *
299  * Returns: 1 if the entire string was copied, 0 otherwise.
300  **/
301 int volume_id_encode_string(const char *str, char *str_enc, size_t len)
302 {
303         size_t i, j;
304
305         if (str == NULL || str_enc == NULL || len == 0)
306                 return 0;
307
308         str_enc[0] = '\0';
309         for (i = 0, j = 0; str[i] != '\0'; i++) {
310                 int seqlen;
311
312                 seqlen = volume_id_utf8_encoded_valid_unichar(&str[i]);
313                 if (seqlen > 1) {
314                         memcpy(&str_enc[j], &str[i], seqlen);
315                         j += seqlen;
316                         i += (seqlen-1);
317                 } else if (str[i] == '\\' || needs_encoding(str[i])) {
318                         sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
319                         j += 4;
320                 } else {
321                         str_enc[j] = str[i];
322                         j++;
323                 }
324                 if (j+3 >= len)
325                         goto err;
326         }
327         str_enc[j] = '\0';
328         return 1;
329 err:
330         return 0;
331 }
332
333 /**
334  * volume_id_probe_raid:
335  * @id: Probing context.
336  * @off: Probing offset relative to the start of the device.
337  * @size: Total size of the device.
338  *
339  * Probe device for all known raid signatures.
340  *
341  * Returns: 0 on successful probe, otherwise negative value.
342  **/
343 int volume_id_probe_raid(struct volume_id *id, uint64_t off, uint64_t size)
344 {
345         unsigned int i;
346
347         if (id == NULL)
348                 return -EINVAL;
349
350         /* run only once into a timeout for unreadable devices */
351         if (volume_id_get_buffer(id, 0x00, 0x200) == NULL)
352                 return -1;
353
354         info("probing at offset 0x%llx, size 0x%llx",
355             (unsigned long long) off, (unsigned long long) size);
356
357         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
358                 if (prober_raid[i].prober(id, off, size) == 0)
359                         goto found;
360         return -1;
361
362 found:
363         /* If recognized, we free the allocated buffers */
364         volume_id_free_buffer(id);
365         return 0;
366 }
367
368 /**
369  * volume_id_probe_filesystem:
370  * @id: Probing context.
371  * @off: Probing offset relative to the start of the device.
372  * @size: Total size of the device.
373  *
374  * Probe device for all known filesystem signatures.
375  *
376  * Returns: 0 on successful probe, otherwise negative value.
377  **/
378 int volume_id_probe_filesystem(struct volume_id *id, uint64_t off, uint64_t size)
379 {
380         unsigned int i;
381
382         if (id == NULL)
383                 return -EINVAL;
384
385         /* run only once into a timeout for unreadable devices */
386         if (volume_id_get_buffer(id, 0x00, 0x200) == NULL)
387                 return -1;
388
389         info("probing at offset 0x%llx, size 0x%llx",
390             (unsigned long long) off, (unsigned long long) size);
391
392         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
393                 if (prober_filesystem[i].prober(id, off, size) == 0)
394                         goto found;
395         return -1;
396
397 found:
398         /* If recognized, we free the allocated buffers */
399         volume_id_free_buffer(id);
400         return 0;
401 }
402
403 /**
404  * volume_id_probe_all:
405  * @id: Probing context.
406  * @off: Probing offset relative to the start of the device.
407  * @size: Total size of the device.
408  *
409  * Probe device for all known raid and filesystem signatures.
410  *
411  * Returns: 0 on successful probe, otherwise negative value.
412  **/
413 int volume_id_probe_all(struct volume_id *id, uint64_t off, uint64_t size)
414 {
415         if (id == NULL)
416                 return -EINVAL;
417
418         /* probe for raid first, because fs probes may be successful on raid members */
419         if (volume_id_probe_raid(id, off, size) == 0)
420                 return 0;
421
422         if (volume_id_probe_filesystem(id, off, size) == 0)
423                 return 0;
424
425         return -1;
426 }
427
428 /**
429  * volume_id_probe_raid:
430  * @all_probers_fn: prober function to called for all known probing routines.
431  * @id: Context passed to prober function.
432  * @off: Offset value passed to prober function.
433  * @size: Size value passed to prober function.
434  * @data: Arbitrary data passed to the prober function.
435  *
436  * Run a custom function for all known probing routines.
437  **/
438 void volume_id_all_probers(all_probers_fn_t all_probers_fn,
439                            struct volume_id *id, uint64_t off, uint64_t size,
440                            void *data)
441 {
442         unsigned int i;
443
444         if (all_probers_fn == NULL)
445                 return;
446
447         for (i = 0; i < ARRAY_SIZE(prober_raid); i++)
448                 if (all_probers_fn(prober_raid[i].prober, id, off, size, data) != 0)
449                         goto out;
450         for (i = 0; i < ARRAY_SIZE(prober_filesystem); i++)
451                 if (all_probers_fn(prober_filesystem[i].prober, id, off, size, data) != 0)
452                         goto out;
453 out:
454         return;
455 }
456
457 /**
458  * volume_id_open_fd:
459  * @id: Probing context.
460  * @fd: Open file descriptor of device to read from.
461  *
462  * Create the context for probing.
463  *
464  * Returns: Probing context, or #NULL on failure.
465  **/
466 struct volume_id *volume_id_open_fd(int fd)
467 {
468         struct volume_id *id;
469
470         id = malloc(sizeof(struct volume_id));
471         if (id == NULL)
472                 return NULL;
473         memset(id, 0x00, sizeof(struct volume_id));
474
475         id->fd = fd;
476
477         return id;
478 }
479
480 struct volume_id *volume_id_open_node(const char *path)
481 {
482         struct volume_id *id;
483         int fd;
484
485         fd = open(path, O_RDONLY);
486         if (fd < 0) {
487                 dbg("unable to open '%s'", path);
488                 return NULL;
489         }
490
491         id = volume_id_open_fd(fd);
492         if (id == NULL)
493                 return NULL;
494
495         /* close fd on device close */
496         id->fd_close = 1;
497
498         return id;
499 }
500
501 /**
502  * volume_id_close:
503  * @id: Probing context.
504  *
505  * Release probing context and free all associated data.
506  */
507 void volume_id_close(struct volume_id *id)
508 {
509         if (id == NULL)
510                 return;
511
512         if (id->fd_close != 0)
513                 close(id->fd);
514
515         volume_id_free_buffer(id);
516
517         free(id);
518 }