summaryrefslogtreecommitdiff
path: root/query-uuid.c
blob: 6cba1b653bc2c5229ef055ed6b576741098b9014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/types.h>

#include <uuid/uuid.h>

struct fsuuid2 {
	__u8			len;
	__u8			uuid[16];
};
#define FS_IOC_GETFSUUID               _IOR(0x15, 0, struct fsuuid2)

void die(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
	fputc('\n', stderr);

	_exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
	const char *path = argv[1] ?: ".";
	int fd = open(path, O_RDONLY);
	if (fd < 0)
		die("Error opening %s: %m\n", path);

	struct fsuuid2 uuid;
	if (ioctl(fd, FS_IOC_GETFSUUID, (unsigned long) &uuid))
		die("FS_IOC_GETFSUUID error: %m\n");

	if (uuid.len == 16) {
		uuid_t u;
		memcpy(&u, uuid.uuid, 16);

		char str[37];
		uuid_unparse(u, str);
		puts(str);
	} else {
		for (unsigned i = 0; i < uuid.len; i++)
			printf("%x", uuid.uuid[i]);
	}
}