it works! :)
[moctel.git] / show_ioctl.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <linux/ioctl.h>
5 #include <stdint.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8
9 struct fetch_fops_args {
10   int fd;
11   uint64_t retp;
12 };
13
14 #define MOCTEL_FETCH_FOPS _IOR('m', 1, struct fetch_fops_args)
15
16 struct file_operations {
17         void *owner;
18         void *llseek;
19         void *read;
20         void *write;
21         void *aio_read;
22         void *aio_write;
23         void *readdir;
24         void *poll;
25         void *unlocked_ioctl;
26         void *compat_ioctl;
27         void *mmap;
28         void *open;
29         void *flush;
30         void *release;
31         void *fsync;
32         void *aio_fsync;
33         void *fasync;
34         void *lock;
35         void *sendpage;
36         void *get_unmapped_area;
37         void *check_flags;
38         void *flock;
39         void *splice_write;
40         void *splice_read;
41         void *setlease;
42         void *fallocate;
43 };
44
45
46
47 int main(int argc, char **argv) {
48   if (argc != 2) fputs("Usage: show_ioctl <device>\n", stderr), exit(1);
49
50   char *devname = argv[1];
51   int devfd = open(devname, O_RDONLY);
52   if (devfd == -1) fprintf(stderr, "Can't open %s: %m\n", devname), exit(1);
53
54   int ioctlfd = open("/dev/ioctl_info", O_RDONLY);
55   if (ioctlfd == -1) fprintf(stderr, "Can't open /dev/ioctl_info: %m\n"), exit(1);
56
57   struct file_operations fops;
58   struct fetch_fops_args ioctl_args = {
59     .fd = devfd,
60     .retp = (uint64_t)&fops
61   };
62   int ret = ioctl(ioctlfd, MOCTEL_FETCH_FOPS, &ioctl_args);
63   if (ret) fprintf(stderr, "can't perform MOCTEL_FETCH_FOPS: %m\n"), exit(1);
64
65   close(ioctlfd);
66   close(devfd);
67
68   printf("unlocked_ioctl: %llx\n", (unsigned long long)fops.unlocked_ioctl);
69
70   return 0;
71 }