diff options
| author | Vladimir Dronnikov <dronnikov@gmail.com> | 2009-10-15 09:24:25 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-10-15 09:24:25 +0200 |
| commit | db67a20595be279e7db9f5f8e27bd94534efb8d4 (patch) | |
| tree | 0263d0ac18ff70781d9a3074f12f5abcd2e18dfc | |
| parent | 7eabffafa5b6faaaa87ff8ba0efbf637aaa364e8 (diff) | |
| download | busybox-w32-db67a20595be279e7db9f5f8e27bd94534efb8d4.tar.gz busybox-w32-db67a20595be279e7db9f5f8e27bd94534efb8d4.tar.bz2 busybox-w32-db67a20595be279e7db9f5f8e27bd94534efb8d4.zip | |
move generate_uuid from mkswap to libbb
Signed-off-by: Vladimir Dronnikov <dronnikov@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | include/libbb.h | 3 | ||||
| -rw-r--r-- | libbb/xfuncs.c | 63 | ||||
| -rw-r--r-- | util-linux/mkswap.c | 92 |
3 files changed, 79 insertions, 79 deletions
diff --git a/include/libbb.h b/include/libbb.h index be175d79f..6e629d187 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -701,6 +701,9 @@ const char *make_human_readable_str(unsigned long long size, | |||
| 701 | /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */ | 701 | /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */ |
| 702 | char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC; | 702 | char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC; |
| 703 | 703 | ||
| 704 | /* Generate a UUID */ | ||
| 705 | void generate_uuid(uint8_t *buf) FAST_FUNC; | ||
| 706 | |||
| 704 | /* Last element is marked by mult == 0 */ | 707 | /* Last element is marked by mult == 0 */ |
| 705 | struct suffix_mult { | 708 | struct suffix_mult { |
| 706 | char suffix[4]; | 709 | char suffix[4]; |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index aa0812914..e47b01dc1 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
| @@ -209,3 +209,66 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) | |||
| 209 | { | 209 | { |
| 210 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); | 210 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); |
| 211 | } | 211 | } |
| 212 | |||
| 213 | void FAST_FUNC generate_uuid(uint8_t *buf) | ||
| 214 | { | ||
| 215 | /* http://www.ietf.org/rfc/rfc4122.txt | ||
| 216 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
| 217 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 218 | * | time_low | | ||
| 219 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 220 | * | time_mid | time_hi_and_version | | ||
| 221 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 222 | * |clk_seq_and_variant | node (0-1) | | ||
| 223 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 224 | * | node (2-5) | | ||
| 225 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 226 | * IOW, uuid has this layout: | ||
| 227 | * uint32_t time_low (big endian) | ||
| 228 | * uint16_t time_mid (big endian) | ||
| 229 | * uint16_t time_hi_and_version (big endian) | ||
| 230 | * version is a 4-bit field: | ||
| 231 | * 1 Time-based | ||
| 232 | * 2 DCE Security, with embedded POSIX UIDs | ||
| 233 | * 3 Name-based (MD5) | ||
| 234 | * 4 Randomly generated | ||
| 235 | * 5 Name-based (SHA-1) | ||
| 236 | * uint16_t clk_seq_and_variant (big endian) | ||
| 237 | * variant is a 3-bit field: | ||
| 238 | * 0xx Reserved, NCS backward compatibility | ||
| 239 | * 10x The variant specified in rfc4122 | ||
| 240 | * 110 Reserved, Microsoft backward compatibility | ||
| 241 | * 111 Reserved for future definition | ||
| 242 | * uint8_t node[6] | ||
| 243 | * | ||
| 244 | * For version 4, these bits are set/cleared: | ||
| 245 | * time_hi_and_version & 0x0fff | 0x4000 | ||
| 246 | * clk_seq_and_variant & 0x3fff | 0x8000 | ||
| 247 | */ | ||
| 248 | pid_t pid; | ||
| 249 | int i; | ||
| 250 | |||
| 251 | i = open("/dev/urandom", O_RDONLY); | ||
| 252 | if (i >= 0) { | ||
| 253 | read(i, buf, 16); | ||
| 254 | close(i); | ||
| 255 | } | ||
| 256 | /* Paranoia. /dev/urandom may be missing. | ||
| 257 | * rand() is guaranteed to generate at least [0, 2^15) range, | ||
| 258 | * but lowest bits in some libc are not so "random". */ | ||
| 259 | srand(monotonic_us()); | ||
| 260 | pid = getpid(); | ||
| 261 | while (1) { | ||
| 262 | for (i = 0; i < 16; i++) | ||
| 263 | buf[i] ^= rand() >> 5; | ||
| 264 | if (pid == 0) | ||
| 265 | break; | ||
| 266 | srand(pid); | ||
| 267 | pid = 0; | ||
| 268 | } | ||
| 269 | |||
| 270 | /* version = 4 */ | ||
| 271 | buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; | ||
| 272 | /* variant = 10x */ | ||
| 273 | buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; | ||
| 274 | } | ||
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c index f71884340..226831bba 100644 --- a/util-linux/mkswap.c +++ b/util-linux/mkswap.c | |||
| @@ -50,84 +50,6 @@ static void mkswap_selinux_setcontext(int fd, const char *path) | |||
| 50 | # define mkswap_selinux_setcontext(fd, path) ((void)0) | 50 | # define mkswap_selinux_setcontext(fd, path) ((void)0) |
| 51 | #endif | 51 | #endif |
| 52 | 52 | ||
| 53 | #if ENABLE_FEATURE_MKSWAP_UUID | ||
| 54 | static void mkswap_generate_uuid(uint8_t *buf) | ||
| 55 | { | ||
| 56 | /* http://www.ietf.org/rfc/rfc4122.txt | ||
| 57 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
| 58 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 59 | * | time_low | | ||
| 60 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 61 | * | time_mid | time_hi_and_version | | ||
| 62 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 63 | * |clk_seq_and_variant | node (0-1) | | ||
| 64 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 65 | * | node (2-5) | | ||
| 66 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| 67 | * IOW, uuid has this layout: | ||
| 68 | * uint32_t time_low (big endian) | ||
| 69 | * uint16_t time_mid (big endian) | ||
| 70 | * uint16_t time_hi_and_version (big endian) | ||
| 71 | * version is a 4-bit field: | ||
| 72 | * 1 Time-based | ||
| 73 | * 2 DCE Security, with embedded POSIX UIDs | ||
| 74 | * 3 Name-based (MD5) | ||
| 75 | * 4 Randomly generated | ||
| 76 | * 5 Name-based (SHA-1) | ||
| 77 | * uint16_t clk_seq_and_variant (big endian) | ||
| 78 | * variant is a 3-bit field: | ||
| 79 | * 0xx Reserved, NCS backward compatibility | ||
| 80 | * 10x The variant specified in rfc4122 | ||
| 81 | * 110 Reserved, Microsoft backward compatibility | ||
| 82 | * 111 Reserved for future definition | ||
| 83 | * uint8_t node[6] | ||
| 84 | * | ||
| 85 | * For version 4, these bits are set/cleared: | ||
| 86 | * time_hi_and_version & 0x0fff | 0x4000 | ||
| 87 | * clk_seq_and_variant & 0x3fff | 0x8000 | ||
| 88 | */ | ||
| 89 | pid_t pid; | ||
| 90 | int i; | ||
| 91 | char uuid_string[32]; | ||
| 92 | |||
| 93 | i = open("/dev/urandom", O_RDONLY); | ||
| 94 | if (i >= 0) { | ||
| 95 | read(i, buf, 16); | ||
| 96 | close(i); | ||
| 97 | } | ||
| 98 | /* Paranoia. /dev/urandom may be missing. | ||
| 99 | * rand() is guaranteed to generate at least [0, 2^15) range, | ||
| 100 | * but lowest bits in some libc are not so "random". */ | ||
| 101 | srand(monotonic_us()); | ||
| 102 | pid = getpid(); | ||
| 103 | while (1) { | ||
| 104 | for (i = 0; i < 16; i++) | ||
| 105 | buf[i] ^= rand() >> 5; | ||
| 106 | if (pid == 0) | ||
| 107 | break; | ||
| 108 | srand(pid); | ||
| 109 | pid = 0; | ||
| 110 | } | ||
| 111 | |||
| 112 | /* version = 4 */ | ||
| 113 | buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; | ||
| 114 | /* variant = 10x */ | ||
| 115 | buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; | ||
| 116 | |||
| 117 | bin2hex(uuid_string, (void*) buf, 16); | ||
| 118 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ | ||
| 119 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", | ||
| 120 | uuid_string, | ||
| 121 | uuid_string+8, | ||
| 122 | uuid_string+8+4, | ||
| 123 | uuid_string+8+4+4, | ||
| 124 | uuid_string+8+4+4+4 | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | #else | ||
| 128 | # define mkswap_generate_uuid(buf) ((void)0) | ||
| 129 | #endif | ||
| 130 | |||
| 131 | #if 0 /* from Linux 2.6.23 */ | 53 | #if 0 /* from Linux 2.6.23 */ |
| 132 | /* | 54 | /* |
| 133 | * Magic header for a swap area. The first part of the union is | 55 | * Magic header for a swap area. The first part of the union is |
| @@ -190,7 +112,19 @@ int mkswap_main(int argc, char **argv) | |||
| 190 | // Make a header. hdr is zero-filled so far... | 112 | // Make a header. hdr is zero-filled so far... |
| 191 | hdr[0] = 1; | 113 | hdr[0] = 1; |
| 192 | hdr[1] = (len / pagesize) - 1; | 114 | hdr[1] = (len / pagesize) - 1; |
| 193 | mkswap_generate_uuid((void*) &hdr[3]); | 115 | #if ENABLE_FEATURE_MKSWAP_UUID |
| 116 | char uuid_string[32]; | ||
| 117 | generate_uuid((void*) &hdr[3]); | ||
| 118 | bin2hex(uuid_string, (void*) &hdr[3], 16); | ||
| 119 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ | ||
| 120 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", | ||
| 121 | uuid_string, | ||
| 122 | uuid_string+8, | ||
| 123 | uuid_string+8+4, | ||
| 124 | uuid_string+8+4+4, | ||
| 125 | uuid_string+8+4+4+4 | ||
| 126 | ); | ||
| 127 | #endif | ||
| 194 | 128 | ||
| 195 | // Write the header. Sync to disk because some kernel versions check | 129 | // Write the header. Sync to disk because some kernel versions check |
| 196 | // signature on disk (not in cache) during swapon. | 130 | // signature on disk (not in cache) during swapon. |
