2 * $Id: hotplug.c,v 1.3 2004/05/01 09:03:48 ghostrider Exp $
4 * (C) 2002, 2003 by Andreas Monzner <ghostrider@tuxbox.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <sys/socket.h>
26 #include <sys/types.h>
30 int send_to_sock(unsigned char *buf
, size_t len
) {
32 struct sockaddr_un servaddr
;
36 memset(&servaddr
, 0, sizeof(struct sockaddr_un
));
37 servaddr
.sun_family
= AF_UNIX
;
38 strcpy(servaddr
.sun_path
, "/tmp/hotplug.socket");
39 clilen
= sizeof(servaddr
.sun_family
) + strlen(servaddr
.sun_path
);
40 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
41 connect(sock
, (struct sockaddr
*) &servaddr
, clilen
);
43 if (write(sock
, buf
, len
) == len
)
49 int main(int argc
, char **argv
)
51 unsigned char lenstr
[11]="LENGTH = 0";
53 char *action
=0, *devpath
=0, *product
=0, *type
=0, *interface
=0, *devfs
=0, *device
=0;
56 if (argc
< 2 || strcmp(argv
[1],"usb") )
58 fprintf(stderr
, "this programm is called by the kernel...\nread /linux/Documentation/usb/hotplug.txt\n");
63 // read environment variables
64 if ( (action
= getenv("ACTION")) )
66 if ( (devpath
= getenv("DEVPATH")) )
68 if ( (product
= getenv("PRODUCT")) )
70 if ( (type
= getenv("TYPE")) )
72 if ( (interface
= getenv("INTERFACE")) )
74 if ( (devfs
= getenv("DEVFS")) )
76 if ( (device
= getenv("DEVICE")) )
81 // first send the count of following messagstrings
82 if ( send_to_sock(lenstr
, 11) != EXIT_SUCCESS
)
87 char tmp
[6+1+strlen(action
)+1];
88 strcpy(tmp
,"ACTION=");
89 strcpy(tmp
+7, action
);
90 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)
96 char tmp
[7+1+strlen(devpath
)+1];
97 strcpy(tmp
,"DEVPATH=");
98 strcpy(tmp
+8, devpath
);
99 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)
105 char tmp
[7+1+strlen(product
)+1];
106 strcpy(tmp
,"PRODUCT=");
107 strcpy(tmp
+8, product
);
108 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)
114 char tmp
[4+1+strlen(type
)+1];
117 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)
123 char tmp
[9+1+strlen(interface
)+1];
124 strcpy(tmp
,"INTERFACE=");
125 strcpy(tmp
+10, interface
);
126 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)
132 char tmp
[5+1+strlen(devfs
)+1];
133 strcpy(tmp
,"DEVFS=");
134 strcpy(tmp
+6, devfs
);
135 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)
141 char tmp
[6+1+strlen(device
)+1];
142 strcpy(tmp
,"DEVICE=");
143 strcpy(tmp
+7, device
);
144 if ( send_to_sock(tmp
, strlen(tmp
)) != EXIT_SUCCESS
)