]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/hwinfo/hwscand.c
3 Copyright 2004 by SUSE (<adrian@suse.de>) */
17 #include "init_message.h"
20 #define LONG_TIMEOUT 0
23 int main( int argc
, char **argv
)
31 int block
, usb
, firewire
, pci
;
32 int dev_last_state
[BUFFERS
];
33 int dev_counter
[BUFFERS
];
34 char * command_device
[NR_COMMANDS
][BUFFERS
];
35 time_t command_device_last
[NR_COMMANDS
][BUFFERS
];
42 // are we running already, maybe ?
48 int fd
= open( PID_FILE
, O_RDONLY
);
49 if ( fd
>= 0 && (r
=read(fd
,b
,1023)) > 0 ){
52 snprintf(link
, 1023, "/proc/%s/exe", b
);
53 if ( (r
=readlink( link
, b
, 1023 )) > 0 ){
57 else if ( strcmp("/hwscand", b
+strlen(b
)-8) )
65 } while ( 0 > (ret
= open( PID_FILE
, O_WRONLY
|O_CREAT
|O_EXCL
, S_IRUSR
|S_IWUSR
) ) );
66 sprintf(buffer
, "%d", getpid());
67 if ( ret
< 0 || write(ret
,buffer
,strlen(buffer
)) <= 0 ){
68 perror("hwscand: unable to write pid file "PID_FILE
);
75 for ( i
=0; i
<NR_COMMANDS
; i
++ ){
76 command_device
[i
][0] = 0;
77 command_device_last
[i
][0] = 1;
80 last
=block
=usb
=firewire
=pci
=0;
81 commands
= (char**) malloc( BUFFERS
* sizeof(char*) );
82 devices
= (char**) malloc( BUFFERS
* sizeof(char*) );
84 msgid
= msgget(key
, IPC_CREAT
| 0600);
96 if( msgrcv(msgid
, &m
, MESSAGE_BUFFER
, 1, mode
) >= 0 ){
100 fprintf( stderr
, "hwscand: error, zero sized message\n" );
102 if ( p
[0] == 'S' && strlen(p
) > 1 ){
109 if ( c
< NR_COMMANDS
){
110 if ( ! command_with_device
[c
] ){
112 if ( LONG_TIMEOUT
+command_device_last
[c
][0] < time(0L) )
113 command_device_last
[c
][0] = 0;
115 for ( i
=0; i
<BUFFERS
; i
++ ){
116 if ( !command_device
[c
][i
] ){
118 command_device
[c
][i
] = strdup(p
+2);
119 command_device
[c
][i
+1] = 0;
120 command_device_last
[c
][i
] = 0;
122 }else if ( !strcmp(command_device
[c
][i
], p
+2) ){
124 if ( LONG_TIMEOUT
+command_device_last
[c
][i
] < time(0L) )
125 command_device_last
[c
][i
] = 0;
131 if ( p
[0] == 'C' && lines
< BUFFERS
){
134 commands
[lines
] = strdup(p
+1);
137 if ( p
[0] == 'A' && dev_nr
< BUFFERS
){
139 devices
[dev_nr
] = strdup(p
+1);
140 dev_last_state
[dev_nr
] = 0;
141 dev_counter
[dev_nr
] = 0;
144 if ( p
[0] == 'R' && dev_nr
< BUFFERS
){
145 for ( i
=0; i
<dev_nr
; i
++ ){
146 if ( !strcmp(p
+1, devices
[i
]) ){
149 for ( j
=i
; j
+1<dev_nr
; j
++ ){
150 devices
[j
] = devices
[j
+1];
151 dev_last_state
[j
] = dev_last_state
[j
+1];
152 dev_counter
[j
] = dev_counter
[j
+1];
160 printf("CALL RECEIVED %s\n", p
);
163 // we do this only in scanning mode ...
166 for ( i
=0; i
<dev_nr
; i
++ ){
167 if (dev_counter
[i
]<0) continue;
169 if ( dev_counter
[i
] > 5 ){
171 char buf
[MESSAGE_BUFFER
];
173 fd
= open( devices
[i
], O_RDONLY
);
174 strcpy( buf
, "/sbin/hwscan --fast --partition --only=");
175 strcat( buf
, devices
[i
] );
177 if ( dev_last_state
[i
] )
179 dev_last_state
[i
] = 0;
181 if ( dev_last_state
[i
] == 0)
183 dev_last_state
[i
] = 1;
190 if ( last
&& (last
+TIMEOUT
<= time(0L)) ){
191 char buf
[MESSAGE_BUFFER
* NR_COMMANDS
];
195 strcpy( buf
, "/sbin/hwscan --fast --boot --silent" );
196 for ( i
=0; i
<NR_COMMANDS
; i
++ ){
197 if ( command_with_device
[i
] == 0 &&
198 command_device_last
[i
][0] == 0 ){
199 command_device_last
[i
][0] = time(0L);
201 strcat( buf
, command_args
[i
] );
205 int commappended
= 0;
207 for ( j
=0; j
<BUFFERS
; j
++ ){
208 if ( !command_device
[i
][j
] )
210 if ( command_device_last
[i
][j
] == 0 ){
213 strcat( buf
, command_args
[i
] );
216 strcat( buf
, " --only=" );
217 strcat( buf
, command_device
[i
][j
] );
218 command_device_last
[i
][j
] = time(0L);
220 if (strlen(buf
) > sizeof(buf
) - MESSAGE_BUFFER
)
225 if (strlen(buf
) > sizeof(buf
) - MESSAGE_BUFFER
) {
226 last
= time(0L); /* call me again */
233 printf("RUN %s\n", buf
);
237 printf("RUN quit %s\n", buf
);
241 for (i
=0; i
<lines
; i
++){
243 printf("CALL DIRECT %s\n", commands
[i
]);
247 printf("CALL quit %s\n", commands
[i
]);