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 
11 static 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 
25 int 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 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