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