trunk 20080912
[gitenigma.git] / hotplug / hotplug.c
blob71a9e5d21f9a7f73106f2519751d0ffb93620127
1 /*
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.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 #include <sys/un.h>
28 #include <unistd.h>
30 int send_to_sock(unsigned char *buf, size_t len) {
32 struct sockaddr_un servaddr;
33 int clilen;
34 int sock;
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)
44 return EXIT_SUCCESS;
46 return EXIT_FAILURE;
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;
55 fflush(stdout);
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");
59 return EXIT_FAILURE;
62 lenstr[9]=0;
63 // read environment variables
64 if ( (action = getenv("ACTION")) )
65 lenstr[9]++;
66 if ( (devpath = getenv("DEVPATH")) )
67 lenstr[9]++;
68 if ( (product = getenv("PRODUCT")) )
69 lenstr[9]++;
70 if ( (type = getenv("TYPE")) )
71 lenstr[9]++;
72 if ( (interface = getenv("INTERFACE")) )
73 lenstr[9]++;
74 if ( (devfs = getenv("DEVFS")) )
75 lenstr[9]++;
76 if ( (device = getenv("DEVICE")) )
77 lenstr[9]++;
78 if (!lenstr[9])
79 return EXIT_FAILURE;
81 // first send the count of following messagstrings
82 if ( send_to_sock(lenstr, 11) != EXIT_SUCCESS )
83 return EXIT_FAILURE;
85 if ( action )
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 )
91 return EXIT_FAILURE;
94 if ( devpath )
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 )
100 return EXIT_FAILURE;
103 if ( product )
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 )
109 return EXIT_FAILURE;
112 if ( type )
114 char tmp[4+1+strlen(type)+1];
115 strcpy(tmp,"TYPE=");
116 strcpy(tmp+5, type);
117 if ( send_to_sock(tmp, strlen(tmp)) != EXIT_SUCCESS )
118 return EXIT_FAILURE;
121 if ( interface )
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 )
127 return EXIT_FAILURE;
130 if ( devfs )
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 )
136 return EXIT_FAILURE;
139 if ( device )
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 )
145 return EXIT_FAILURE;
148 return EXIT_SUCCESS;