1#include <stdio.h>
2#include <fcntl.h>
3#include <unistd.h>
4#include <sys/ioctl.h>
5#include <linux/dm-ioctl.h>
6#include <stdlib.h>
7
8#define DM_CRYPT_BUF_SIZE 4096
9
10static void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
11{
12    memset(io, 0, dataSize);
13    io->data_size = dataSize;
14    io->data_start = sizeof(struct dm_ioctl);
15    io->version[0] = 4;
16    io->version[1] = 0;
17    io->version[2] = 0;
18    io->flags = flags;
19    if (name) {
20        strncpy(io->name, name, sizeof(io->name));
21    }
22}
23
24int main(int argc, char *argv[])
25{
26    char buffer[DM_CRYPT_BUF_SIZE];
27    struct dm_ioctl *io;
28    struct dm_target_versions *v;
29    int i;
30    int fd;
31
32    fd = open("/dev/device-mapper", O_RDWR);
33    if (fd < 0) {
34        fprintf(stderr, "Cannot open /dev/device-mapper\n");
35        exit(1);
36    }
37
38    io = (struct dm_ioctl *) buffer;
39
40    ioctl_init(io, DM_CRYPT_BUF_SIZE, NULL, 0);
41
42    if (ioctl(fd, DM_LIST_VERSIONS, io)) {
43        fprintf(stderr, "ioctl(DM_LIST_VERSIONS) returned an error\n");
44        exit(1);
45    }
46
47    /* Iterate over the returned versions, and print each subsystem's version */
48    v = (struct dm_target_versions *) &buffer[sizeof(struct dm_ioctl)];
49    while (v->next) {
50        printf("%s: %d.%d.%d\n", v->name, v->version[0], v->version[1], v->version[2]);
51        v = (struct dm_target_versions *)(((char *)v) + v->next);
52    }
53
54    close(fd);
55    exit(0);
56}
57
58