propagator/probing.c
2013-02-14 17:08:46 +04:00

307 lines
7.1 KiB
C

/*
* Guillaume Cottenceau (gc@mandrakesoft.com)
*
* Copyright 2000 MandrakeSoft
*
* This software may be freely redistributed under the terms of the GNU
* public license.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* Portions from Erik Troan (ewt@redhat.com)
*
* Copyright 1996 Red Hat Software
*
*/
/*
* This contains stuff related to probing:
* (1) any (actually only SCSI, NET, CPQ, USB Controllers) devices (autoprobe for PCI and USB)
* (2) IDE media
* (3) SCSI media
* (4) ETH devices
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <dirent.h>
#include "stage1.h"
#include "log.h"
#include "frontend.h"
#include "modules.h"
#include "probing.h"
struct media_info {
char * name;
char * model;
enum media_type type;
};
#ifndef DISABLE_NETWORK
struct net_description_elem
{
char * intf_name;
char * intf_description;
};
static struct net_description_elem net_descriptions[50];
static int net_descr_number = 0;
static char * intf_descr_for_discover = NULL;
static char * net_intf_too_early_name[50]; /* for modules providing more than one net intf */
static int net_intf_too_early_number = 0;
static int net_intf_too_early_ptr = 0;
void net_discovered_interface(char * intf_name)
{
if (!intf_descr_for_discover) {
net_intf_too_early_name[net_intf_too_early_number++] = strdup(intf_name);
return;
}
if (!intf_name) {
if (net_intf_too_early_ptr >= net_intf_too_early_number) {
log_message("NET: was expecting another network interface (broken net module?)");
return;
}
net_descriptions[net_descr_number].intf_name = net_intf_too_early_name[net_intf_too_early_ptr++];
}
else
net_descriptions[net_descr_number].intf_name = strdup(intf_name);
net_descriptions[net_descr_number].intf_description = strdup(intf_descr_for_discover);
intf_descr_for_discover = NULL;
net_descr_number++;
}
char * get_net_intf_description(char * intf_name)
{
char dev[SYSFS_PATH_MAX];
char drv[SYSFS_PATH_MAX];
ssize_t i;
snprintf(dev, SYSFS_PATH_MAX, "/sys/class/net/%s/device/driver", intf_name);
if ((i = readlink(dev, drv, SYSFS_PATH_MAX)) > 0) {
drv[i] = '\0';
return strdup(strrchr(drv, '/') + 1);
} else {
return strdup("unknown");
}
}
#endif
#ifndef DISABLE_MEDIAS
static struct media_info * medias = NULL;
static int usb_not_there(void)
{
DIR *dir = opendir("/sys/module/usb_storage");
if (dir) closedir(dir);
return !dir;
}
static void find_media(void)
{
FILE *f;
DIR *dir = NULL;
struct dirent *dirent = NULL;
struct stat st;
struct media_info tmp[50];
char path[SYSFS_PATH_MAX];
char buf[512];
char *s;
int i = 0, count = 0;
if (medias)
free(medias); /* that does not free the strings, by the way */
log_message("checking for /sys/module/usb_storage directory");
if (usb_not_there()) {
wait_message("Waiting for USB storage devices to show up...");
my_insmod("usb_storage", NULL);
while (usb_not_there() && (i < 10)) {
sleep(1);
i++;
}
remove_wait_message();
}
if ((dir = opendir("/sys/block")) == NULL) {
log_message("failed to open /sys/block directory");
return;
}
while ((dirent = readdir(dir)) != NULL) {
if (!strcmp(dirent->d_name, ".") ||
!strcmp(dirent->d_name, "..")) continue;
memset(path, 0, SYSFS_PATH_MAX);
strcpy(path, "/sys/block/");
strcat(path, dirent->d_name);
s = path + strlen(path);
/* probe for scsi type */
strcat(path, "/device/type");
if (lstat(path, &st) == 0 && S_ISREG(st.st_mode)) {
tmp[count].name = strdup(dirent->d_name);
tmp[count].type = UNKNOWN_MEDIA;
if ((f = fopen(path, "r")) != NULL) {
int type;
if (fgets(buf, sizeof(buf), f) && sscanf(buf, "%d", &type)) {
if (type == SCSI_TYPE_DISK) tmp[count].type = DISK;
else if (type == SCSI_TYPE_ROM) tmp[count].type = CDROM;
else if (type == SCSI_TYPE_TAPE) tmp[count].type = TAPE;
}
fclose(f);
}
*s = 0;
strcat(path, "/device/model");
if ((f = fopen(path, "r")) != NULL) {
if (fgets(buf, sizeof(buf), f)) {
if(buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = 0;
tmp[count].model = strdup(buf);
}
fclose(f);
} else {
tmp[count].model = strdup("(unknown)");
}
log_message("SCSI/%d: %s is a %s", tmp[count].type, tmp[count].name, tmp[count].model);
count++;
continue;
}
/* assume ide */
*s = 0;
strcat(s, "/device/media");
if (lstat(path, &st) == 0 && S_ISREG(st.st_mode)) {
tmp[count].name = strdup(dirent->d_name);
tmp[count].type = UNKNOWN_MEDIA;
if ((f = fopen(path, "r")) != NULL) {
if (fgets(buf, sizeof(buf), f)) {
if (!strncmp("disk", buf, 4))
tmp[count].type = DISK;
else if (!strncmp("cdrom", buf, 5))
tmp[count].type = CDROM;
else if (!strncmp("tape", buf, 4))
tmp[count].type = TAPE;
else if (!strncmp("floppy", buf, 6))
tmp[count].type = FLOPPY;
}
fclose(f);
}
/* grab model */
strcpy(path, "/proc/ide/");
strcat(path, dirent->d_name);
strcat(path, "/model");
if ((f = fopen(path, "r")) != NULL) {
if (fgets(buf, sizeof(buf), f)) {
if(buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = 0;
tmp[count].model = strdup(buf);
}
fclose(f);
} else {
tmp[count].model = strdup("(none)");
}
log_message("IDE/%d: %s is a %s", tmp[count].type, tmp[count].name, tmp[count].model);
count++;
}
}
closedir(dir);
tmp[count].name = NULL;
count++;
medias = memdup(tmp, sizeof(struct media_info) * count);
}
/* Finds by media */
void get_medias(enum media_type media, char *** names, char *** models)
{
struct media_info * m;
char * tmp_names[50];
char * tmp_models[50];
int count;
find_media();
m = medias;
count = 0;
while (m && m->name) {
/* detect hybrid iso images (those dumped to a USB stick) */
if ((m->type == media) || ((media == CDROM) && (m->type == DISK))) {
tmp_names[count] = strdup(m->name);
tmp_models[count++] = strdup(m->model);
}
m++;
}
tmp_names[count] = NULL;
tmp_models[count++] = NULL;
*names = memdup(tmp_names, sizeof(char *) * count);
*models = memdup(tmp_models, sizeof(char *) * count);
}
#endif /* DISABLE_MEDIAS */
#ifndef DISABLE_NETWORK
static int net_device_available(char * device)
{
struct ifreq req;
int s;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
log_perror(device);
return 0;
}
strcpy(req.ifr_name, device);
if (ioctl(s, SIOCGIFFLAGS, &req)) {
/* if we can't get the flags, the networking device isn't available */
close(s);
return 0;
}
close(s);
return 1;
}
char ** get_net_devices(void)
{
DIR * sys_net;
char * tmp[50];
struct dirent * ent;
int i = 0;
sys_net = opendir("/sys/class/net");
if (sys_net == NULL) return(strdup("\0"));
while ( ent = readdir(sys_net)){
if( !strcmp("lo", ent->d_name))continue;
if (net_device_available(ent->d_name)){
tmp[i++] = strdup(ent->d_name);
}
}
tmp[i++] = NULL;
return memdup(tmp, sizeof(char *) * i);
}
#endif /* DISABLE_NETWORK */