| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "config.h" |
| 15 |
#include "compat/include/bylabel.h" |
| 16 |
|
| 17 |
#include <sys/types.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <string.h> |
| 20 |
#include <ctype.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <endian.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#ifdef HAVE_EXT2FS_EXT2_FS_H |
| 26 |
#include <ext2fs/ext2_fs.h> |
| 27 |
#else |
| 28 |
#include <linux/ext2_fs.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "common/shared_globals.h" |
| 32 |
|
| 33 |
#ifndef HAVE_BLKID |
| 34 |
|
| 35 |
#define PROC_PARTITIONS "/proc/partitions" |
| 36 |
#define DEVLABELDIR "/dev" |
| 37 |
|
| 38 |
#define EXT2_SUPER_OFFSET 1024 |
| 39 |
#define EXT2_SUPER_SIZE sizeof(struct ext2_super_block) |
| 40 |
#define EXT2_SUPER_MAGIC 0xEF53 |
| 41 |
|
| 42 |
#if BYTE_ORDER == BIG_ENDIAN |
| 43 |
#define ext2magic(s) ext2fs_swab16(s.s_magic) |
| 44 |
#else |
| 45 |
#define ext2magic(s) (s.s_magic) |
| 46 |
#endif |
| 47 |
|
| 48 |
static struct uuidCache_s { |
| 49 |
struct uuidCache_s *next; |
| 50 |
char uuid[16]; |
| 51 |
char *label; |
| 52 |
char *device; |
| 53 |
} *uuidCache = NULL; |
| 54 |
|
| 55 |
|
| 56 |
static int |
| 57 |
get_label_uuid(const char *device, char **label, char *uuid) { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
int fd; |
| 62 |
struct ext2_super_block e2sb; |
| 63 |
|
| 64 |
fd = open(device, O_RDONLY); |
| 65 |
if (fd < 0) |
| 66 |
return 1; |
| 67 |
|
| 68 |
if (lseek(fd, EXT2_SUPER_OFFSET, SEEK_SET) != EXT2_SUPER_OFFSET || |
| 69 |
read(fd, (char *) &e2sb, EXT2_SUPER_SIZE) != EXT2_SUPER_SIZE || |
| 70 |
ext2magic(e2sb) != EXT2_SUPER_MAGIC) { |
| 71 |
close(fd); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
close(fd); |
| 76 |
|
| 77 |
|
| 78 |
memcpy(uuid, e2sb.s_uuid, sizeof(e2sb.s_uuid)); |
| 79 |
*label = malloc(EXT2_LABEL_LEN + 1); |
| 80 |
strncpy(*label, (char*)e2sb.s_volume_name, EXT2_LABEL_LEN); |
| 81 |
(*label)[EXT2_LABEL_LEN] = 0; |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
uuidcache_addentry(char *device, char *label, char *uuid) { |
| 88 |
struct uuidCache_s *last; |
| 89 |
|
| 90 |
if (!uuidCache) { |
| 91 |
last = uuidCache = (struct uuidCache_s *)malloc(sizeof(*uuidCache)); |
| 92 |
} else { |
| 93 |
for (last = uuidCache; last->next; last = last->next) ; |
| 94 |
last->next = (struct uuidCache_s *)malloc(sizeof(*uuidCache)); |
| 95 |
last = last->next; |
| 96 |
} |
| 97 |
last->next = NULL; |
| 98 |
last->device = device; |
| 99 |
last->label = label; |
| 100 |
memcpy(last->uuid, uuid, sizeof(last->uuid)); |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
uuidcache_init(void) { |
| 105 |
char line[100]; |
| 106 |
char *s; |
| 107 |
int ma, mi, sz; |
| 108 |
static char ptname[100]; |
| 109 |
FILE *procpt; |
| 110 |
char uuid[16], *label; |
| 111 |
char device[110]; |
| 112 |
int firstPass; |
| 113 |
int handleOnFirst; |
| 114 |
|
| 115 |
if (uuidCache) |
| 116 |
return; |
| 117 |
|
| 118 |
procpt = fopen(PROC_PARTITIONS, "r"); |
| 119 |
if (!procpt) |
| 120 |
return; |
| 121 |
|
| 122 |
for (firstPass = 1; firstPass >= 0; firstPass--) { |
| 123 |
fseek(procpt, 0, SEEK_SET); |
| 124 |
|
| 125 |
while (fgets(line, sizeof(line), procpt)) { |
| 126 |
if (sscanf (line, " %d %d %d %[^\n ]", |
| 127 |
&ma, &mi, &sz, ptname) != 4) |
| 128 |
continue; |
| 129 |
|
| 130 |
|
| 131 |
if (sz == 1) |
| 132 |
continue; |
| 133 |
|
| 134 |
|
| 135 |
handleOnFirst = !strncmp(ptname, "md", 2); |
| 136 |
if (firstPass != handleOnFirst) |
| 137 |
continue; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
for(s = ptname; *s; s++); |
| 144 |
if (isdigit(s[-1])) { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
sprintf(device, "%s/%s", DEVLABELDIR, ptname); |
| 155 |
if (!get_label_uuid(device, &label, uuid)) |
| 156 |
uuidcache_addentry(strdup(device), label, uuid); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
fclose(procpt); |
| 162 |
} |
| 163 |
|
| 164 |
#define UUID 1 |
| 165 |
#define VOL 2 |
| 166 |
|
| 167 |
static char * |
| 168 |
get_spec_by_x(int n, const char *t) { |
| 169 |
struct uuidCache_s *uc; |
| 170 |
|
| 171 |
uuidcache_init(); |
| 172 |
uc = uuidCache; |
| 173 |
|
| 174 |
while(uc) { |
| 175 |
switch (n) { |
| 176 |
case UUID: |
| 177 |
if (!memcmp(t, uc->uuid, sizeof(uc->uuid))) |
| 178 |
return strdup(uc->device); |
| 179 |
break; |
| 180 |
case VOL: |
| 181 |
if (!strcmp(t, uc->label)) |
| 182 |
return strdup(uc->device); |
| 183 |
break; |
| 184 |
} |
| 185 |
uc = uc->next; |
| 186 |
} |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
static u_char |
| 191 |
fromhex(char c) { |
| 192 |
if (isdigit(c)) |
| 193 |
return (c - '0'); |
| 194 |
else if (islower(c)) |
| 195 |
return (c - 'a' + 10); |
| 196 |
else |
| 197 |
return (c - 'A' + 10); |
| 198 |
} |
| 199 |
|
| 200 |
static char * |
| 201 |
get_spec_by_uuid(const char *s) { |
| 202 |
char uuid[16]; |
| 203 |
int i; |
| 204 |
|
| 205 |
if (strlen(s) != 36 || |
| 206 |
s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') |
| 207 |
goto bad_uuid; |
| 208 |
for (i=0; i<16; i++) { |
| 209 |
if (*s == '-') s++; |
| 210 |
if (!isxdigit(s[0]) || !isxdigit(s[1])) |
| 211 |
goto bad_uuid; |
| 212 |
uuid[i] = ((fromhex(s[0])<<4) | fromhex(s[1])); |
| 213 |
s += 2; |
| 214 |
} |
| 215 |
return get_spec_by_x(UUID, uuid); |
| 216 |
|
| 217 |
bad_uuid: |
| 218 |
msg("mount: bad UUID\n"); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
|
| 222 |
static char * |
| 223 |
get_spec_by_volume_label(const char *s) { |
| 224 |
return get_spec_by_x(VOL, s); |
| 225 |
} |
| 226 |
|
| 227 |
const char * |
| 228 |
get_device_name(const char * item) { |
| 229 |
const char * rc; |
| 230 |
|
| 231 |
if (!strncmp(item, "UUID=", 5)) { |
| 232 |
rc = get_spec_by_uuid(item+5); |
| 233 |
} |
| 234 |
else |
| 235 |
if (!strncmp(item, "LABEL=", 6)) { |
| 236 |
rc = get_spec_by_volume_label(item+6); |
| 237 |
} |
| 238 |
else { |
| 239 |
rc = strdup(item); |
| 240 |
} |
| 241 |
|
| 242 |
return rc; |
| 243 |
} |
| 244 |
|
| 245 |
const char * |
| 246 |
get_device_label(const char * spec) { |
| 247 |
struct uuidCache_s *uc; |
| 248 |
|
| 249 |
uuidcache_init(); |
| 250 |
uc = uuidCache; |
| 251 |
|
| 252 |
while(uc) { |
| 253 |
if (!strcmp(spec, uc->device)) |
| 254 |
return uc->label[0] == '\0' ? NULL : strdup(uc->label); |
| 255 |
uc = uc->next; |
| 256 |
} |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
#endif |