1 /* trinity ioctl() support routines */
6 #include "trinity.h" // ARRAY_SIZE
11 #define IOCTL_GROUPS_MAX 48
13 static const struct ioctl_group
*grps
[IOCTL_GROUPS_MAX
];
16 void register_ioctl_group(const struct ioctl_group
*grp
)
18 /* group could be empty e.g. if everything is ifdeffed out */
19 if (grp
->ioctls_cnt
== 0)
22 if (grps_cnt
== ARRAY_SIZE(grps
)) {
23 fprintf(stderr
, "WARNING: please grow IOCTL_GROUPS_MAX.\n");
32 const struct ioctl_group
*find_ioctl_group(int fd
)
39 if (fstat(fd
, &stbuf
) < 0)
42 if (stbuf
.st_rdev
== 0)
45 devname
= map_dev(stbuf
.st_rdev
, stbuf
.st_mode
);
49 for (i
=0; i
< grps_cnt
; ++i
) {
50 if (grps
[i
]->fd_test
) {
51 if (grps
[i
]->fd_test(fd
, &stbuf
) == 0)
57 switch (grps
[i
]->devtype
) {
59 /* fall through. misc devices are char devices. */
61 if (!S_ISCHR(stbuf
.st_mode
))
65 if (!S_ISBLK(stbuf
.st_mode
))
71 for (j
=0; j
< grps
[i
]->devs_cnt
; ++j
)
72 if (strcmp(devname
, grps
[i
]->devs
[j
]) == 0)
79 const struct ioctl_group
*get_random_ioctl_group(void)
84 return grps
[rand() % grps_cnt
];
87 void pick_random_ioctl(const struct ioctl_group
*grp
, int childno
)
91 ioctlnr
= rand() % grp
->ioctls_cnt
;
93 shm
->a2
[childno
] = grp
->ioctls
[ioctlnr
].request
;
96 void dump_ioctls(void)
101 for (i
=0; i
< grps_cnt
; ++i
) {
103 printf("- %s:\n", grps
[i
]->name
);
104 else if (grps
[i
]->devtype
) {
105 if (grps
[i
]->devtype
== DEV_MISC
)
106 printf("- misc devices");
107 else if (grps
[i
]->devtype
== DEV_CHAR
)
108 printf("- char devices");
109 else if (grps
[i
]->devtype
== DEV_BLOCK
)
110 printf("- block devices");
111 for (j
=0; j
< grps
[i
]->devs_cnt
; ++j
)
117 printf("- <unknown>:\n");
119 for (j
=0; j
< grps
[i
]->ioctls_cnt
; ++j
) {
120 printf(" - 0x%08x : %s\n",
121 grps
[i
]->ioctls
[j
].request
,
122 grps
[i
]->ioctls
[j
].name
? : "");