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