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