summaryrefslogtreecommitdiffstats
path: root/isys/devices.c
blob: e312a43e98635dab2e5d5c0ada959be76d45417f (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * devices.c - various hardware probing functionality
 *
 * Copyright (C) 2007  Red Hat, Inc.
 * All rights reserved.
 *
 * 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 2 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/>.
 *
 * Author(s): Bill Nottingham <notting@redhat.com>
 */

#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <limits.h>

#include "devices.h"

/* for 'disks', to filter out weird stuff */
#define MINIMUM_INTERESTING_SIZE	32*1024 	/* 32MB */

/* from genhd.h, kernel side */
#define GENHD_FL_REMOVABLE                      1
#define GENHD_FL_DRIVERFS                       2
#define GENHD_FL_MEDIA_CHANGE_NOTIFY            4
#define GENHD_FL_CD                             8
#define GENHD_FL_UP                             16
#define GENHD_FL_SUPPRESS_PARTITION_INFO        32
#define GENHD_FL_FAIL                           64


struct device **getDevices(enum deviceType type) {
    struct device **ret = NULL;
    struct device *new;
    int numdevices = 0;

    if (type & (DEVICE_DISK | DEVICE_CDROM)) {
        DIR *dir;
        struct dirent *ent;

        dir = opendir("/sys/block");

        if (!dir) goto storagedone;

        while ((ent = readdir(dir))) {
            char path[64];
            char buf[64];
            int fd, caps, devtype;

            snprintf(path, 64, "/sys/block/%s/capability", ent->d_name);
            fd = open(path, O_RDONLY);
            if (fd == -1)
                continue;
            if (read(fd, buf, 64) <= 0) {
                close(fd);
                continue;
            }

            close(fd);
            errno = 0;
            caps = strtol(buf, NULL, 16);

            if ((errno == ERANGE && (caps == LONG_MIN || caps == LONG_MAX)) ||
                (errno != 0 && caps == 0)) {
                return NULL;
            }

            if (caps & GENHD_FL_CD)
                devtype = DEVICE_CDROM;
            else
                devtype = DEVICE_DISK;
            if (!(devtype & type))
                continue;

            if (devtype == DEVICE_DISK && !(caps & GENHD_FL_REMOVABLE)) {
                int size;

                snprintf(path, 64, "/sys/block/%s/size", ent->d_name);
                fd = open(path, O_RDONLY);

                if (fd == -1)
                    continue;
                if (read(fd, buf, 64) <= 0) {
                    close(fd);
                    continue;
                }

                close(fd);
                errno = 0;
                size = strtol(buf, NULL, 10);

                if ((errno == ERANGE && (size == LONG_MIN ||
                                         size == LONG_MAX)) ||
                    (errno != 0 && size == 0)) {
                    return NULL;
                }

                if (size < MINIMUM_INTERESTING_SIZE)
                    continue;
            }

            new = calloc(1, sizeof(struct device));
            new->device = strdup(ent->d_name);
            /* FIXME */
            if (asprintf(&new->description, "Storage device %s",
                         new->device) == -1) {
                fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
                        strerror(errno));
                fflush(stderr);
                abort();
            }
            new->type = devtype;
            if (caps & GENHD_FL_REMOVABLE) {
                new->priv.removable = 1;
            }
            ret = realloc(ret, (numdevices+2) * sizeof(struct device));
            ret[numdevices] = new;
            ret[numdevices+1] = NULL;
            numdevices++;
        }
    }
storagedone:

    if (type & DEVICE_NETWORK) {
        DIR *dir;
        struct dirent *ent;

        dir = opendir("/sys/class/net");

        if (!dir) goto netdone;

        while ((ent = readdir(dir))) {
            char path[64];
            int fd, type;
            char buf[64];

            snprintf(path, 64, "/sys/class/net/%s/type", ent->d_name);
            fd = open(path, O_RDONLY);
            if (fd == -1)
                continue;
            if (read(fd, buf, 64) <= 0) {
                close(fd);
                continue;
            }

            close(fd);
            errno = 0;
            type = strtol(buf, NULL, 10);

            if ((errno == ERANGE && (type == LONG_MIN || type == LONG_MAX)) ||
                (errno != 0 && type == 0)) {
                return NULL;
            }

            if (type != 1)
                continue;

            new = calloc(1, sizeof(struct device));
            new->device = strdup(ent->d_name);
            /* FIXME */
            snprintf(path, 64, "/sys/class/net/%s/address", ent->d_name);
            fd = open(path, O_RDONLY);
            if (fd != -1) {
                if (read(fd, buf, 64) > 0) {
                    int i;
                    for (i = (strlen(buf)-1); isspace(buf[i]); i--)
                        buf[i] = '\0';
                    new->priv.hwaddr = strdup(buf);
                }
            }

            if (new->priv.hwaddr) {
                if (asprintf(&new->description, "Ethernet device %s - %s",
                             new->device, new->priv.hwaddr) == -1) {
                    fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
                            strerror(errno));
                    fflush(stderr);
                    abort();
                }
            } else {
                if (asprintf(&new->description, "Ethernet device %s",
                             new->device) == -1) {
                    fprintf(stderr, "%s: %d: %s\n", __func__, __LINE__,
                            strerror(errno));
                    fflush(stderr);
                    abort();
                }
            }

            ret = realloc(ret, (numdevices+2) * sizeof(struct device));
            ret[numdevices] = new;
            ret[numdevices+1] = NULL;
            numdevices++;
        }
    }
netdone:
    return ret;
}