]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/hwinfo/hwscanqueue.c
Signierten GPG-Schluessel importiert.
[people/pmueller/ipfire-2.x.git] / src / hwinfo / hwscanqueue.c
1
2 /* hwscan front end
3 Copyright 2004 by SUSE (<adrian@suse.de>) */
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/ipc.h>
8 #include <sys/msg.h>
9 #include <signal.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 #include "init_message.h"
17
18 int main( int argc, char **argv )
19 {
20 int ret;
21 unsigned short i;
22 key_t key = KEY;
23 int msgid;
24 message m;
25 char *device = argv[2];
26
27 if ( argc < 2 ){
28 fprintf( stderr, "help: hwscanqueue hwscan-commands\n" );
29 fprintf( stderr, "help: commands:\n" );
30 for ( i=0; i<NR_COMMANDS; i++ ){
31 fprintf( stderr, " --%s", command_args[i] );
32 if ( command_with_device[i] )
33 fprintf( stderr, " device" );
34 fprintf( stderr, "\n");
35 }
36 fprintf( stderr, " --avail=yes/no id\n" );
37 fprintf( stderr, " --scan=device\n" );
38 fprintf( stderr, " --stop=device\n" );
39 exit(1);
40 }
41
42 if ( !strncmp("--cfg=", argv[1], 6) && argc>2 )
43 snprintf( m.mtext, MESSAGE_BUFFER, "C/sbin/hwscan %s %s", argv[1], argv[2] );
44 else if ( !strncmp("--avail=", argv[1], 8) && argc>2 )
45 snprintf( m.mtext, MESSAGE_BUFFER, "C/sbin/hwscan %s %s", argv[1], argv[2] );
46 else if ( !strncmp("--scan=", argv[1], 7) )
47 snprintf( m.mtext, MESSAGE_BUFFER, "A%s", argv[1]+7 );
48 else if ( !strncmp("--stop=", argv[1], 7) )
49 snprintf( m.mtext, MESSAGE_BUFFER, "R%s", argv[1]+7 );
50 else if ( !strncmp("--", argv[1], 2) ){
51 for ( i=0; i<NR_COMMANDS; i++ ){
52 if ( !strcmp(argv[1]+2,command_args[i]) ){
53 #if DEBUG
54 printf("COMMAND %s\n", command_args[i] );
55 #endif
56 snprintf( m.mtext, MESSAGE_BUFFER, "S%d", i );
57 if (command_with_device[i]){
58 if ( !device ){
59 fprintf(stderr, "need a device for this command\n");
60 exit(1);
61 }
62 strncat( m.mtext, device, MESSAGE_BUFFER-3 );
63 }
64 break;
65 }
66 }
67 if ( i>=NR_COMMANDS ){
68 fprintf(stderr, "unknown command\n");
69 exit(1);
70 }
71 }else
72 exit(1);
73
74 if ( (msgid = msgget(key, IPC_CREAT | 0600)) < 0 ){
75 perror("unable to init.");
76 exit(1);
77 }
78 m.mtype = 1;
79 ret = msgsnd( msgid, &m, MESSAGE_BUFFER, IPC_NOWAIT);
80 #if DEBUG
81 printf("SEND %s, return %d\n", m.mtext, ret );
82 #endif
83
84 if ( ret < 0 )
85 perror("message send failed");
86 else{
87 // success ... start hwscand, if it is not yet running
88 ssize_t r;
89 char buffer[1024];
90 char link[1024];
91 int fd = open( PID_FILE, O_RDONLY );
92 if ( fd >= 0 && (r=read(fd,buffer,1023)) > 0 ){
93 close(fd);
94 buffer[r]='\0';
95 snprintf(link, 1023, "/proc/%s/exe", buffer);
96 if ( (r=readlink( link, buffer, 1023 )) > 0 ){
97 buffer[r]='\0';
98 if ( r<8 )
99 fd=-1;
100 else if ( strcmp("/hwscand", buffer+strlen(buffer)-8) )
101 fd=-1;
102 }else
103 fd=-1;
104 }else
105 fd=-1;
106
107 if ( fd < 0 ){
108 pid_t pid;
109 signal(SIGCHLD,SIG_IGN);
110 pid=fork();
111 if (pid==0){
112 /* Change directory to allow clean shut-down */
113 chdir("/");
114 /* Close std fds */
115 close(0);
116 close(1);
117 close(2);
118 /* Start hwscand */
119 execve("/sbin/hwscand", 0, 0);
120 }
121 }
122 }
123
124 exit(ret);
125 }
126