File: hwscand.c

package info (click to toggle)
hwinfo 21.67-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,836 kB
  • sloc: ansic: 45,096; perl: 3,325; makefile: 233; sh: 181
file content (257 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download | duplicates (8)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

/* hwscan front end
   Copyright 2004 by SUSE (<adrian@suse.de>) */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "init_message.h"

#define TIMEOUT 2
#define LONG_TIMEOUT 0
#define BUFFERS 1024

int main( int argc, char **argv )
{
        int ret, i;
	key_t key = KEY;
	int msgid;
	int mode = 0;
	int dev_nr = 0;
	int lines = 0;
	int block, usb, firewire, pci;
	int dev_last_state[BUFFERS];
	int dev_counter[BUFFERS];
	char * command_device[NR_COMMANDS][BUFFERS];
	time_t command_device_last[NR_COMMANDS][BUFFERS];
	time_t last;
	char **commands;
	char **devices;
	char buffer[32];
	message m;

	// are we running already, maybe ?
	{
		do {
			ssize_t r;
			char b[1024];
			char link[1024];
			int fd = open( PID_FILE, O_RDONLY );
			if ( fd >= 0 && (r=read(fd,b,1023)) > 0 ){
			        close(fd);
			        b[r]='\0';
			        snprintf(link, 1023, "/proc/%s/exe", b);
			        if ( (r=readlink( link, b, 1023 )) > 0 ){
			                b[r]='\0';
			                if ( r<8 )
			                        unlink(PID_FILE);
			                else if ( strcmp("/hwscand", b+strlen(b)-8) )
			                        unlink(PID_FILE);
					else
						exit(1);
			        }else
		                        unlink(PID_FILE);
		        }else if ( fd >= 0 )
	                        unlink(PID_FILE);
		} while ( 0 > (ret = open( PID_FILE, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR ) ) );
		sprintf(buffer, "%d", getpid());
		if ( ret < 0 || write(ret,buffer,strlen(buffer)) <= 0 ){
			perror("hwscand: unable to write pid file "PID_FILE);
			exit(1);
		}
		close(ret);
	}

	// initialize ...
	for ( i=0; i<NR_COMMANDS; i++ ){
		command_device[i][0] = 0;
		command_device_last[i][0] = 1;
	}

	last=block=usb=firewire=pci=0;
	commands = (char**) malloc( BUFFERS * sizeof(char*) );
	devices  = (char**) malloc( BUFFERS * sizeof(char*) );

	msgid = msgget(key, IPC_CREAT | 0600);
        if (msgid < 0) {
		perror("msgget");
		exit(1);
        }

	while (1) {
		if ( last || dev_nr )
			mode = IPC_NOWAIT;
		else
			mode = 0;

		if( msgrcv(msgid, &m, MESSAGE_BUFFER, 1, mode) >= 0 ){
			char *p = m.mtext;

			if ( p == 0 ){
				fprintf( stderr, "hwscand: error, zero sized message\n" );
			}else{
				if ( p[0] == 'S' && strlen(p) > 1 ){
					// scan calls
					char z[2];
					int c;
					z[0] = *(p+1);
					z[1] = '\0';
					c = atoi(z);
					if ( c < NR_COMMANDS ){
						if ( ! command_with_device[c] ){
							last = time(0L);
							if ( LONG_TIMEOUT+command_device_last[c][0] < time(0L) )
								command_device_last[c][0] = 0;
						}else
						for ( i=0; i<BUFFERS; i++ ){
							if ( !command_device[c][i] ){
								last = time(0L);
								command_device[c][i] = strdup(p+2);
								command_device[c][i+1] = 0;
								command_device_last[c][i] = 0;
								break;
							}else if ( !strcmp(command_device[c][i], p+2) ){
								last = time(0L);
								if ( LONG_TIMEOUT+command_device_last[c][i] < time(0L) )
									command_device_last[c][i] = 0;
								break;
							}
						}
					}
				}
				if ( p[0] == 'C' && lines < BUFFERS ){
					last = time(0L);
					// config calls
					commands[lines] = strdup(p+1);
					lines++;	
				}
				if ( p[0] == 'A' && dev_nr < BUFFERS ){	
					// add scan devices
					devices[dev_nr]        = strdup(p+1);
					dev_last_state[dev_nr] = 0;
					dev_counter[dev_nr]    = 0;
					dev_nr++;
				}
				if ( p[0] == 'R' && dev_nr < BUFFERS ){	
					for ( i=0; i<dev_nr; i++ ){
						if ( !strcmp(p+1, devices[i]) ){
							int j;
							free(devices[i]);
							for ( j=i; j+1<dev_nr; j++ ){
								devices[j]        = devices[j+1];
								dev_last_state[j] = dev_last_state[j+1];
								dev_counter[j]    = dev_counter[j+1];
							}
							dev_nr--;
						}
					}
				}
			}
#if DEBUG
				printf("CALL RECEIVED %s\n", p);
#endif
		}else{
			// we do this only in scanning mode ...

			sleep(1);
			for ( i=0; i<dev_nr; i++ ){
				if (dev_counter[i]<0) continue;
				dev_counter[i]++;
				if ( dev_counter[i] > 5 ){
					int fd;
					char buf[MESSAGE_BUFFER];
					dev_counter[i] = 0;
					fd = open( devices[i], O_RDONLY );
					strcpy( buf, "/sbin/hwscan --fast --partition --only=");
					strcat( buf, devices[i] );
					if ( fd < 0 ){
						if ( dev_last_state[i] )
							system(buf);
						dev_last_state[i] = 0;
					}else{
						if ( dev_last_state[i] == 0)
							system(buf);
						dev_last_state[i] = 1;
						close(fd);
					}
				}
			}
		}
		
		if ( last && (last+TIMEOUT <= time(0L)) ){
			char buf[MESSAGE_BUFFER * NR_COMMANDS];
			int run_really = 0;

			last=0;
			strcpy( buf, "/sbin/hwscan --fast --boot --silent" );
			for ( i=0; i<NR_COMMANDS; i++ ){
				if ( command_with_device[i] == 0 &&
				     command_device_last[i][0] == 0 ){
					command_device_last[i][0] = time(0L);
					strcat( buf, " --");
					strcat( buf, command_args[i] );
				 	run_really = 1;
				} else {
					int j;
					int commappended = 0;

					for ( j=0; j<BUFFERS; j++ ){
						if ( !command_device[i][j] )
							break;
						if ( command_device_last[i][j] == 0 ){
							if (!commappended) {
								strcat( buf, " --");
								strcat( buf, command_args[i] );
								commappended = 1;
						        }
							strcat( buf, " --only=" );
							strcat( buf, command_device[i][j] );
							command_device_last[i][j] = time(0L);
							run_really = 1;
							if (strlen(buf) > sizeof(buf) - MESSAGE_BUFFER)
								break;
						}
					}
				}
				if (strlen(buf) > sizeof(buf) - MESSAGE_BUFFER) {
					last = time(0L);	/* call me again */
					break;
				}
			}

			if ( run_really ){
#if DEBUG
				printf("RUN %s\n", buf);
#endif
				system(buf);
#if DEBUG				
				printf("RUN quit %s\n", buf);
#endif
			}
			if ( lines ){
				for (i=0; i<lines; i++){
#if DEBUG
				printf("CALL DIRECT %s\n", commands[i]);
#endif
					system(commands[i]);
#if DEBUG
				printf("CALL quit %s\n", commands[i]);
#endif
					free(commands[i]);
				}
				lines=0;
			}
		}
	}

	return 0;
}