File: probe.c

package info (click to toggle)
libexplain 1.4.D001-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 42,148 kB
  • sloc: ansic: 156,027; makefile: 47,893; sh: 16,303; yacc: 1,898; awk: 245
file content (178 lines) | stat: -rw-r--r-- 4,984 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * libexplain - Explain errno values returned by libc functions
 * Copyright (C) 2009, 2010 Peter Miller
 * Written by Peter Miller <pmiller@opensource.org.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <libexplain/ac/errno.h>
#include <libexplain/ac/fcntl.h>
#include <libexplain/ac/stdio.h>
#include <libexplain/ac/sys/ioctl.h> /* for ioctl() except Solaris */
#include <libexplain/ac/unistd.h> /* for ioctl() on Solaris */

#include <libexplain/iocontrol/generic.h>
#include <libexplain/open.h>
#include <libexplain/close.h>

#include <ioctl-scan/probe.h>


static void
report(int fildes, int request, void *data)
{
    const explain_iocontrol_t *p;
    char            name[50];

    p = explain_iocontrol_find_by_number(fildes, request, data);
    explain_iocontrol_fake_syscall_name(name, sizeof(name), p, request);
    printf("%s\n", name);
}


static inline int
size_incr(int size)
{
    if (size < 0)
        return 0;
    if (size == 1)
        return 2;
    if (size == 2 || size == 3)
        return 4;
    return ((size + 4) & -4);
}


#ifndef _IOC_TYPEMASK
#define _IOC_TYPEMASK 255
#endif

#ifndef _IOC_NRMASK
#define _IOC_NRMASK 255
#endif

#ifndef _IOC_DIRMASK
# ifdef IOC_DIRMASK
#  define _IOC_DIRMASK (int)(IOC_DIRMASK)
# else
#  define _IOC_DIRMASK 3
# endif
#endif

#ifndef _IOC_SIZEMASK
# ifdef IOCPARM_MASK
#  define _IOC_SIZEMASK IOCPARM_MASK
# else
#  define _IOC_SIZEMASK 0x1FFF
# endif
#endif

#ifndef _IOC
#define _IOC(dir, type, nr, size) \
    (((dir)*0) + ((type) << 8) + (nr) + ((size)*0))
#endif

#define minbit(x) ((x) & -(x))


void
probe(const char *pathname)
{
    int             fildes;
    void            *data;
    int             type;
    int             bol;

    data = (void *)0x12345;
    fildes = explain_open_or_die(pathname, O_RDONLY | O_NONBLOCK, 0);
    bol = 1;
    for (type = 0; type <= _IOC_TYPEMASK; ++type)
    {
        int             nr;

        fprintf(stderr, "\r%3d of %d ", type, _IOC_TYPEMASK + 1);
        fflush(stderr);
        bol = 0;
        for (nr = 0; nr <= _IOC_NRMASK; ++nr)
        {
            int             dir;

            for (dir = 0; dir <= _IOC_DIRMASK; dir += minbit(_IOC_DIRMASK))
            {
                int             size;

                for (size = 0; size <= _IOC_SIZEMASK; size = size_incr(size))
                {
                    int             request;

                    request = _IOC(dir, type, nr, size);
                    if (ioctl(fildes, request, data) >= 0)
                    {
                        /* This one worked.  Hope it didn't make a mess */
                        if (!bol)
                        {
                            fprintf(stderr, "\r\33[K");
                            bol = 1;
                        }
                        report(fildes, request, data);
                    }
                    else
                    {
                        switch (errno)
                        {
                        case ENOSYS:
#if defined(EOPNOTSUPP) && (ENOSYS != EOPNOTSUPP)
                        case EOPNOTSUPP:
#endif
                        case ENOTTY:
#ifdef ENOIOCTLCMD
                        case ENOIOCTLCMD:
#endif
#ifdef ENOIOCTL
                        case ENOIOCTL:
#endif
                            /* definitely does not exist */
                            break;

                        case EINVAL:
                            /*
                             * Some (old) broken device drivers report
                             * these instead of ENOTTY like they are
                             * supposed to.  Assume it means "no".
                             */
                            break;

                        case EFAULT:
                        case EPERM:
                        case EACCES:
                        default:
                            /* definitely exists */
                            if (!bol)
                            {
                                fprintf(stderr, "\r\33[K");
                                bol = 1;
                            }
                            report(fildes, request, data);
                            break;
                        }
                    }
                }
            }
        }
    }
    explain_close_or_die(fildes);
    if (!bol)
        fprintf(stderr, "\r\33[K");
}