~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise-security

« back to all changes in this revision

Viewing changes to drivers/staging/usbip/userspace/src/utils.c

  • Committer: Package Import Robot
  • Author(s): Paolo Pisati, Paolo Pisati
  • Date: 2011-12-06 15:56:07 UTC
  • Revision ID: package-import@ubuntu.com-20111206155607-pcf44kv5fmhk564f
Tags: 3.2.0-1401.1
[ Paolo Pisati ]

* Rebased on top of Ubuntu-3.2.0-3.8
* Tilt-tracking @ ef2487af4bb15bdd0689631774b5a5e3a59f74e2
* Delete debian.ti-omap4/control, it shoudln't be tracked
* Fix architecture spelling (s/armel/armhf/)
* [Config] Update configs following 3.2 import
* [Config] Fix compilation: disable CODA and ARCH_OMAP3
* [Config] Fix compilation: disable Ethernet Faraday
* Update series to precise

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *
3
 
 * Copyright (C) 2005-2007 Takahiro Hirofuchi
 
2
 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
 
3
 *               2005-2007 Takahiro Hirofuchi
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation, either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4
17
 */
5
18
 
 
19
#include <sysfs/libsysfs.h>
 
20
 
 
21
#include <errno.h>
 
22
#include <stdio.h>
 
23
#include <string.h>
 
24
 
 
25
#include "usbip_common.h"
6
26
#include "utils.h"
7
27
 
8
 
int read_integer(char *path)
 
28
int modify_match_busid(char *busid, int add)
9
29
{
10
 
        char buff[100];
11
 
        int fd;
12
 
        int ret = 0;
13
 
 
14
 
        bzero(buff, sizeof(buff));
15
 
 
16
 
        fd = open(path, O_RDONLY);
17
 
        if (fd < 0)
18
 
                return -1;
19
 
 
20
 
        ret = read(fd, buff, sizeof(buff));
21
 
        if (ret < 0) {
22
 
                close(fd);
23
 
                return -1;
24
 
        }
25
 
 
26
 
        sscanf(buff, "%d", &ret);
27
 
 
28
 
        close(fd);
 
30
        char bus_type[] = "usb";
 
31
        char attr_name[] = "match_busid";
 
32
        char buff[SYSFS_BUS_ID_SIZE + 4];
 
33
        char sysfs_mntpath[SYSFS_PATH_MAX];
 
34
        char match_busid_attr_path[SYSFS_PATH_MAX];
 
35
        struct sysfs_attribute *match_busid_attr;
 
36
        int rc, ret = 0;
 
37
 
 
38
        if (strnlen(busid, SYSFS_BUS_ID_SIZE) > SYSFS_BUS_ID_SIZE - 1) {
 
39
                dbg("busid is too long");
 
40
                return -1;
 
41
        }
 
42
 
 
43
        rc = sysfs_get_mnt_path(sysfs_mntpath, SYSFS_PATH_MAX);
 
44
        if (rc < 0) {
 
45
                err("sysfs must be mounted: %s", strerror(errno));
 
46
                return -1;
 
47
        }
 
48
 
 
49
        snprintf(match_busid_attr_path, sizeof(match_busid_attr_path),
 
50
                 "%s/%s/%s/%s/%s/%s", sysfs_mntpath, SYSFS_BUS_NAME, bus_type,
 
51
                 SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME, attr_name);
 
52
 
 
53
        match_busid_attr = sysfs_open_attribute(match_busid_attr_path);
 
54
        if (!match_busid_attr) {
 
55
                dbg("problem getting match_busid attribute: %s",
 
56
                    strerror(errno));
 
57
                return -1;
 
58
        }
 
59
 
 
60
        if (add)
 
61
                snprintf(buff, SYSFS_BUS_ID_SIZE + 4, "add %s", busid);
 
62
        else
 
63
                snprintf(buff, SYSFS_BUS_ID_SIZE + 4, "del %s", busid);
 
64
 
 
65
        dbg("write \"%s\" to %s", buff, match_busid_attr->path);
 
66
 
 
67
        rc = sysfs_write_attribute(match_busid_attr, buff, sizeof(buff));
 
68
        if (rc < 0) {
 
69
                dbg("failed to write match_busid: %s", strerror(errno));
 
70
                ret = -1;
 
71
        }
 
72
 
 
73
        sysfs_close_attribute(match_busid_attr);
29
74
 
30
75
        return ret;
31
76
}
32
 
 
33
 
int read_string(char *path, char *string, size_t len)
34
 
{
35
 
        int fd;
36
 
        int ret = 0;
37
 
        char  *p;
38
 
 
39
 
        bzero(string, len);
40
 
 
41
 
        fd = open(path, O_RDONLY);
42
 
        if (fd < 0) {
43
 
                string = NULL;
44
 
                return -1;
45
 
        }
46
 
 
47
 
        ret = read(fd, string, len-1);
48
 
        if (ret < 0) {
49
 
                string = NULL;
50
 
                close(fd);
51
 
                return -1;
52
 
        }
53
 
 
54
 
        p = strchr(string, '\n');
55
 
        *p = '\0';
56
 
 
57
 
        close(fd);
58
 
 
59
 
        return 0;
60
 
}
61
 
 
62
 
int write_integer(char *path, int value)
63
 
{
64
 
        int fd;
65
 
        int ret;
66
 
        char buff[100];
67
 
 
68
 
        snprintf(buff, sizeof(buff), "%d", value);
69
 
 
70
 
        fd = open(path, O_WRONLY);
71
 
        if (fd < 0)
72
 
                return -1;
73
 
 
74
 
        ret = write(fd, buff, strlen(buff));
75
 
        if (ret < 0) {
76
 
                close(fd);
77
 
                return -1;
78
 
        }
79
 
 
80
 
        close(fd);
81
 
 
82
 
        return 0;
83
 
}
84
 
 
85
 
int read_bConfigurationValue(char *busid)
86
 
{
87
 
        char path[PATH_MAX];
88
 
 
89
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
90
 
 
91
 
        return read_integer(path);
92
 
}
93
 
 
94
 
int write_bConfigurationValue(char *busid, int config)
95
 
{
96
 
        char path[PATH_MAX];
97
 
 
98
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
99
 
 
100
 
        return write_integer(path, config);
101
 
}
102
 
 
103
 
int read_bNumInterfaces(char *busid)
104
 
{
105
 
        char path[PATH_MAX];
106
 
 
107
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bNumInterfaces", busid);
108
 
 
109
 
        return read_integer(path);
110
 
}
111
 
 
112
 
int read_bDeviceClass(char *busid)
113
 
{
114
 
        char path[PATH_MAX];
115
 
 
116
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bDeviceClass", busid);
117
 
 
118
 
        return read_integer(path);
119
 
}
120
 
 
121
 
int getdriver(char *busid, int conf, int infnum, char *driver, size_t len)
122
 
{
123
 
        char path[PATH_MAX];
124
 
        char linkto[PATH_MAX];
125
 
        int ret;
126
 
 
127
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s:%d.%d/driver", busid, conf, infnum);
128
 
 
129
 
        /* readlink does not add NULL */
130
 
        bzero(linkto, sizeof(linkto));
131
 
        ret = readlink(path, linkto, sizeof(linkto)-1);
132
 
        if (ret < 0) {
133
 
                strncpy(driver, "none", len);
134
 
                return -1;
135
 
        } else {
136
 
                strncpy(driver, basename(linkto), len);
137
 
                return 0;
138
 
        }
139
 
}
140
 
 
141
 
int getdevicename(char *busid, char *name, size_t len)
142
 
{
143
 
        char path[PATH_MAX];
144
 
        char idProduct[10], idVendor[10];
145
 
 
146
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idVendor", busid);
147
 
        read_string(path, idVendor, sizeof(idVendor));
148
 
 
149
 
        snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idProduct", busid);
150
 
        read_string(path, idProduct, sizeof(idProduct));
151
 
 
152
 
        if (!idVendor[0] || !idProduct[0])
153
 
                return -1;
154
 
 
155
 
        snprintf(name, len, "%s:%s", idVendor, idProduct);
156
 
 
157
 
        return 0;
158
 
}
159
 
 
160
 
#define MAXLINE 100
161
 
 
162
 
/* if this cannot read a whole line, return -1 */
163
 
int readline(int sockfd, char *buff, int bufflen)
164
 
{
165
 
        int ret;
166
 
        char c;
167
 
        int index = 0;
168
 
 
169
 
 
170
 
        while (index < bufflen) {
171
 
                ret = read(sockfd, &c, sizeof(c));
172
 
                if (ret < 0 && errno == EINTR)
173
 
                        continue;
174
 
                if (ret <= 0) {
175
 
                        return -1;
176
 
                }
177
 
 
178
 
                buff[index] = c;
179
 
 
180
 
                if ( index > 0 && buff[index-1] == '\r'  && buff[index] == '\n') {
181
 
                        /* end of line */
182
 
                        buff[index-1] = '\0';   /* get rid of delimitor */
183
 
                        return index;
184
 
                } else
185
 
                        index++;
186
 
        }
187
 
 
188
 
        return -1;
189
 
}
190
 
 
191
 
#if 0
192
 
int writeline(int sockfd, char *str, int strlen)
193
 
{
194
 
        int ret;
195
 
        int index = 0;
196
 
        int len;
197
 
        char buff[MAXLINE];
198
 
 
199
 
        if (strlen + 3 > MAXLINE)
200
 
                return -1;
201
 
 
202
 
        strncpy(buff, str, strlen);
203
 
        buff[strlen+1] = '\r';
204
 
        buff[strlen+2] = '\n';
205
 
        buff[strlen+3] = '\0';
206
 
 
207
 
        len = strlen + 3;
208
 
 
209
 
        while (len > 0) {
210
 
                ret = write(sockfd, buff+index, len);
211
 
                if (ret <= 0) {
212
 
                        return -1;
213
 
                }
214
 
 
215
 
                len -= ret;
216
 
                index += ret;
217
 
        }
218
 
 
219
 
        return index;
220
 
}
221
 
#endif
222
 
 
223
 
int writeline(int sockfd, char *str, int strlen)
224
 
{
225
 
        int ret;
226
 
        int index = 0;
227
 
        int len;
228
 
        char buff[MAXLINE];
229
 
 
230
 
        len = strnlen(str, strlen);
231
 
 
232
 
        if (strlen + 2 > MAXLINE)
233
 
                return -1;
234
 
 
235
 
        memcpy(buff, str, strlen);
236
 
        buff[strlen] = '\r';
237
 
        buff[strlen+1] = '\n';          /* strlen+1 <= MAXLINE-1 */
238
 
 
239
 
        len = strlen + 2;
240
 
 
241
 
        while (len > 0) {
242
 
                ret = write(sockfd, buff+index, len);
243
 
                if (ret < 0 && errno == EINTR)
244
 
                        continue;
245
 
                if (ret <= 0) {
246
 
                        return -1;
247
 
                }
248
 
 
249
 
                len -= ret;
250
 
                index += ret;
251
 
        }
252
 
 
253
 
        return index;
254
 
}
255