OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [usb/] [slave/] [current/] [host/] [usbchmod.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//=================================================================
2
//
3
//        usb_chmod.c
4
//
5
//        A utility to manipulate /proc/bus/usb access rights
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// The Linux kernel allows raw access to USB devices via /proc/bus/usb.
43
// However, such access requires root privileges: this makes perfect
44
// sense for typical USB devices, but gets in the way of eCos USB
45
// testing. This utility runs suid and can be used to change the access
46
// rights to a specific and validated USB device.
47
//
48
// Author(s):     bartv
49
// Date:          2001-07-18
50
//####DESCRIPTIONEND####
51
//==========================================================================
52
 
53
#include <stdio.h>
54
#include <stdlib.h>
55
#include <limits.h>
56
#include <sys/types.h>
57
#include <sys/stat.h>
58
#include <errno.h>
59
#include <string.h>
60
 
61
// Note: this code is duplicated in usbhost.c. Any changes here
62
// should be propagated. For now the routine is too small to warrant
63
// a separate source file.
64
#define USB_ROOT        "/proc/bus/usb/"
65
#define PRODUCT_STRING  "Red Hat eCos USB test"
66
 
67
static int
68
usb_scan_devices(int* bus, int* dev)
69
{
70
    FILE*       devs_file       = fopen(USB_ROOT "devices", "r");
71
    int         current_bus     = -1;
72
    int         current_dev     = -1;
73
    int         ch;
74
 
75
    if (NULL == devs_file) {
76
        fprintf(stderr, "Error: unable to access " USB_ROOT "devices\n");
77
        return 0;
78
    }
79
    ch = getc(devs_file);
80
    while (EOF != ch) {
81
        if ('T' == ch) {
82
            if (2 !=fscanf(devs_file, ":  Bus=%d %*[^D\n]Dev#=%d", &current_bus, &current_dev)) {
83
                current_bus = -1;
84
                current_dev = -1;
85
            }
86
        } else if ('S' == ch) {
87
            int start = 0, end = 0;
88
            if (EOF != fscanf(devs_file, ":  Product=%n" PRODUCT_STRING "%n", &start, &end)) {
89
                if (start < end) {
90
                    *bus = current_bus;
91
                    *dev = current_dev;
92
                    break;
93
                }
94
            }
95
        }
96
        // Move to the end of the current line.
97
        while ((EOF != ch) && ('\n' != ch)) {
98
            ch = getc(devs_file);
99
        }
100
        if (EOF != ch) {
101
            ch = getc(devs_file);
102
        }
103
    }
104
 
105
    fclose(devs_file);
106
    if ((-1 != *bus) && (-1 != *dev)) {
107
        return 1;
108
    }
109
    fprintf(stderr, "Error: failed to find a USB device \"" PRODUCT_STRING "\"\n");
110
    return 0;
111
}
112
 
113
int
114
main(int argc, char** argv)
115
{
116
    int         bus, dev;
117
    int         actual_bus  = 0;
118
    int         actual_dev  = 0;
119
    char        devname[_POSIX_PATH_MAX];
120
    long        strtol_tmp1;
121
    char*       strtol_tmp2;
122
 
123
    if (3 != argc) {
124
        fprintf(stderr, "usb_chmod: wrong number of arguments\n");
125
        fprintf(stderr, "         : usage, usb_chmod <bus> <dev>\n");
126
        exit(EXIT_FAILURE);
127
    }
128
    if (('\0' == argv[1][0]) || ('\0' == argv[2][0])) {
129
        fprintf(stderr, "usb_chmod: invalid arguments\n");
130
        exit(EXIT_FAILURE);
131
    }
132
 
133
    strtol_tmp1 = strtol(argv[1], &strtol_tmp2, 10);
134
    if ('\0' != *strtol_tmp2) {
135
        fprintf(stderr, "usbchmod: invalid first argument, not a number\n");
136
        exit(EXIT_FAILURE);
137
    }
138
    if (strtol_tmp1 > INT_MAX) {
139
        fprintf(stderr, "usbchmod: invalid first argument, number too large\n");
140
        exit(EXIT_FAILURE);
141
    }
142
    bus = (int) strtol_tmp1;
143
 
144
    strtol_tmp1 = strtol(argv[2], &strtol_tmp2, 10);
145
    if ('\0' != *strtol_tmp2) {
146
        fprintf(stderr, "usbchmod: invalid second argument, not a number\n");
147
        exit(EXIT_FAILURE);
148
    }
149
    if (strtol_tmp1 > INT_MAX) {
150
        fprintf(stderr, "usbchmod: invalid second argument, number too large\n");
151
        exit(EXIT_FAILURE);
152
    }
153
    dev = (int) strtol_tmp1;
154
 
155
    if (!usb_scan_devices(&actual_bus, &actual_dev)) {
156
        fprintf(stderr, "usb_chmod: failed to find eCos USB test application\n");
157
        exit(EXIT_FAILURE);
158
    }
159
    if ((bus != actual_bus) || (dev != actual_dev)) {
160
        fprintf(stderr, "usbchmod: mismatch between specified and actual USB identifiers.\n");
161
        fprintf(stderr, "         : eCos test application is at %03d/%03d, not %03d/%03d\n",
162
                actual_bus, actual_dev, bus, dev);
163
        exit(EXIT_FAILURE);
164
    }
165
 
166
    if (_POSIX_PATH_MAX == snprintf(devname, _POSIX_PATH_MAX, "/proc/bus/usb/" "%03d/%03d", actual_bus, actual_dev)) {
167
        fprintf(stderr, "usbchmod: internal error, buffer overflow\n");
168
        exit(EXIT_FAILURE);
169
    }
170
 
171
    if (0 != chmod(devname, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) {
172
        int old_errno = errno;
173
        fprintf(stderr, "usbchmod: failed to modify access rights on %s\n", devname);
174
        fprintf(stderr, "         : %s\n", strerror(old_errno));
175
        exit(EXIT_FAILURE);
176
    }
177
 
178
    return EXIT_SUCCESS;
179
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.