fast method_name->source_file lookups, heuristics for finding ioctl names
[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 #define KSYM_NAME_LEN 128
14
15 struct fetch_fops_args {
16   int fd;
17   uint64_t retp;
18 };
19
20 #define MOCTEL_FETCH_FOPS _IOR('m', 1, struct fetch_fops_args)
21
22 int main(int argc, char **argv) {
23   if (argc != 2) fputs("Usage: show_ioctl <device>\n", stderr), exit(1);
24
25   char *devname = argv[1];
26   int devfd = open(devname, O_RDONLY);
27   if (devfd == -1) fprintf(stderr, "Can't open %s: %m\n", devname), exit(1);
28
29   int ioctlfd = open("/dev/ioctl_info", O_RDONLY);
30   if (ioctlfd == -1) fprintf(stderr, "Can't open /dev/ioctl_info: %m\n"), exit(1);
31
32   char resbuf[KSYM_NAME_LEN];
33   struct fetch_fops_args ioctl_args = {
34     .fd = devfd,
35     .retp = (uint64_t)resbuf
36   };
37   int ret = ioctl(ioctlfd, MOCTEL_FETCH_FOPS, &ioctl_args);
38   if (ret) fprintf(stderr, "can't perform MOCTEL_FETCH_FOPS: %m\n"), exit(1);
39
40   close(ioctlfd);
41   close(devfd);
42
43   printf("%s\n", resbuf);
44
45   return 0;
46 }