check if uid changes across syscalls.
[trinity.git] / ioctls / ioctls.c
blob76cf5501df34f6660801b701287a0aa33585a4a2
1 /* trinity ioctl() support routines */
3 #include <string.h>
4 #include <stdio.h>
6 #include "trinity.h" // ARRAY_SIZE
7 #include "files.h"
8 #include "shm.h"
9 #include "ioctls.h"
11 #define IOCTL_GROUPS_MAX 48
13 static const struct ioctl_group *grps[IOCTL_GROUPS_MAX];
14 static int grps_cnt;
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)
20 return;
22 if (grps_cnt == ARRAY_SIZE(grps)) {
23 fprintf(stderr, "WARNING: please grow IOCTL_GROUPS_MAX.\n");
24 return;
27 grps[grps_cnt] = grp;
29 ++grps_cnt;
32 const struct ioctl_group *find_ioctl_group(int fd)
34 const char *devname;
35 struct stat stbuf;
36 int i;
37 size_t j;
39 if (fstat(fd, &stbuf) < 0)
40 return NULL;
42 if (stbuf.st_rdev == 0)
43 return NULL;
45 devname = map_dev(stbuf.st_rdev, stbuf.st_mode);
46 if (!devname)
47 return NULL;
49 for (i=0; i < grps_cnt; ++i) {
50 if (grps[i]->fd_test) {
51 if (grps[i]->fd_test(fd, &stbuf) == 0)
52 return grps[i];
53 else
54 continue;
57 switch (grps[i]->devtype) {
58 case DEV_MISC:
59 /* fall through. misc devices are char devices. */
60 case DEV_CHAR:
61 if (!S_ISCHR(stbuf.st_mode))
62 continue;
63 break;
64 case DEV_BLOCK:
65 if (!S_ISBLK(stbuf.st_mode))
66 continue;
67 break;
68 default: break;
71 for (j=0; j < grps[i]->devs_cnt; ++j)
72 if (strcmp(devname, grps[i]->devs[j]) == 0)
73 return grps[i];
76 return NULL;
79 const struct ioctl_group *get_random_ioctl_group(void)
81 if (grps_cnt == 0)
82 return NULL;
84 return grps[rand() % grps_cnt];
87 void pick_random_ioctl(const struct ioctl_group *grp, int childno)
89 int ioctlnr;
91 ioctlnr = rand() % grp->ioctls_cnt;
93 shm->a2[childno] = grp->ioctls[ioctlnr].request;
96 void dump_ioctls(void)
98 int i;
99 size_t j;
101 for (i=0; i < grps_cnt; ++i) {
102 if (grps[i]->name)
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)
112 printf("%s '%s'",
113 j == 0 ? "" : ",",
114 grps[i]->devs[j]);
115 printf(":\n");
116 } else
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 ? : "");