change sockaddr generation to pass around correct types.
[trinity.git] / ioctls / ioctls.c
blobde26851b59c7d01d15a1bc83d36b6872e0018af8
1 /* trinity ioctl() support routines */
3 #include <string.h>
4 #include <stdio.h>
6 #include "files.h"
7 #include "ioctls.h"
8 #include "log.h"
9 #include "shm.h"
10 #include "utils.h" // ARRAY_SIZE
12 #define IOCTL_GROUPS_MAX 48
14 static const struct ioctl_group *grps[IOCTL_GROUPS_MAX];
15 static int grps_cnt;
17 void register_ioctl_group(const struct ioctl_group *grp)
19 /* group could be empty e.g. if everything is ifdeffed out */
20 if (grp->ioctls_cnt == 0)
21 return;
23 if (grps_cnt == ARRAY_SIZE(grps)) {
24 outputerr("WARNING: please grow IOCTL_GROUPS_MAX.\n");
25 return;
28 grps[grps_cnt] = grp;
30 ++grps_cnt;
33 const struct ioctl_group *find_ioctl_group(int fd)
35 const char *devname;
36 struct stat stbuf;
37 int i;
38 size_t j;
40 if (fstat(fd, &stbuf) < 0)
41 return NULL;
43 if (stbuf.st_rdev == 0)
44 return NULL;
46 devname = map_dev(stbuf.st_rdev, stbuf.st_mode);
47 if (!devname)
48 return NULL;
50 for (i=0; i < grps_cnt; ++i) {
51 if (grps[i]->fd_test) {
52 if (grps[i]->fd_test(fd, &stbuf) == 0)
53 return grps[i];
54 else
55 continue;
58 switch (grps[i]->devtype) {
59 case DEV_MISC:
60 /* fall through. misc devices are char devices. */
61 case DEV_CHAR:
62 if (!S_ISCHR(stbuf.st_mode))
63 continue;
64 break;
65 case DEV_BLOCK:
66 if (!S_ISBLK(stbuf.st_mode))
67 continue;
68 break;
69 default: break;
72 for (j=0; j < grps[i]->devs_cnt; ++j)
73 if (strcmp(devname, grps[i]->devs[j]) == 0)
74 return grps[i];
77 return NULL;
80 const struct ioctl_group *get_random_ioctl_group(void)
82 if (grps_cnt == 0)
83 return NULL;
85 return grps[rand() % grps_cnt];
88 void pick_random_ioctl(const struct ioctl_group *grp, int childno)
90 int ioctlnr;
92 ioctlnr = rand() % grp->ioctls_cnt;
94 shm->a2[childno] = grp->ioctls[ioctlnr].request;
97 void dump_ioctls(void)
99 int i;
100 size_t j;
102 for (i=0; i < grps_cnt; ++i) {
103 if (grps[i]->name)
104 outputerr("- %s:\n", grps[i]->name);
105 else if (grps[i]->devtype) {
106 if (grps[i]->devtype == DEV_MISC)
107 outputerr("- misc devices");
108 else if (grps[i]->devtype == DEV_CHAR)
109 outputerr("- char devices");
110 else if (grps[i]->devtype == DEV_BLOCK)
111 outputerr("- block devices");
112 for (j=0; j < grps[i]->devs_cnt; ++j)
113 outputerr("%s '%s'",
114 j == 0 ? "" : ",",
115 grps[i]->devs[j]);
116 outputerr(":\n");
117 } else
118 outputerr("- <unknown>:\n");
120 for (j=0; j < grps[i]->ioctls_cnt; ++j) {
121 outputerr(" - 0x%08x : %s\n",
122 grps[i]->ioctls[j].request,
123 grps[i]->ioctls[j].name ? : "");