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