--- projects/libstatgrab/src/libstatgrab/tools.c 2003/11/10 22:46:32 1.13 +++ projects/libstatgrab/src/libstatgrab/tools.c 2003/11/13 17:02:46 1.14 @@ -37,6 +37,195 @@ #include "tools.h" +#ifdef SOLARIS +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#ifdef SOLARIS +struct map{ + char *bsd; + char *svr; + + struct map *next; +}; +typedef struct map mapping_t; + +static mapping_t *mapping = NULL; + +char *get_svr_from_bsd(char *bsd){ + mapping_t *map_ptr; + for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next) + if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr; + + return bsd; +} + +void add_mapping(char *bsd, char *svr){ + mapping_t *map_ptr; + mapping_t *map_end_ptr; + + bsd = strdup(bsd); + svr = strdup(svr); + + if (mapping == NULL){ + printf("New malloc\n"); + mapping = malloc(sizeof(mapping_t)); + if (mapping == NULL) return; + map_ptr = mapping; + }else{ + /* See if its already been added */ + for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){ + if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){ + printf("%s matches %s\n", map_ptr->bsd, bsd); + return; + } + map_end_ptr = map_ptr; + } + + /* We've reached end of list and not found the entry.. So we need to malloc + * new mapping_t + */ + map_end_ptr->next = malloc(sizeof(mapping_t)); + printf("Second malloc\n"); + if (map_end_ptr->next == NULL) return; + map_ptr = map_end_ptr->next; + } + + printf("Adding %s\n", bsd); + map_ptr->next = NULL; + map_ptr->bsd = bsd; + map_ptr->svr = svr; + + return; +} + +char *read_dir(char *disk_path){ + DIR *dirp; + struct dirent *dp; + struct stat stbuf; + char *svr_name; + char current_dir[MAXPATHLEN]; + char file_name[MAXPATHLEN]; + char temp_name[MAXPATHLEN]; + char dir_dname[MAXPATHLEN]; + char *dsk_dir; + int x; + + dsk_dir = "/dev/osa/dev/dsk"; + strncpy(current_dir, dsk_dir, sizeof current_dir); + if ((dirp = opendir(current_dir)) == NULL){ + dsk_dir = "/dev/dsk"; + snprintf(current_dir, sizeof current_dir, "%s", dsk_dir); + if ((dirp = opendir(current_dir)) == NULL){ + return NULL; + } + } + + while ((dp = readdir(dirp)) != NULL){ + snprintf(temp_name, sizeof temp_name, "../..%s", disk_path); + snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name); + stat(dir_dname,&stbuf); + + if (S_ISBLK(stbuf.st_mode)){ + x = readlink(dir_dname, file_name, sizeof(file_name)); + file_name[x] = '\0'; + if (strcmp(file_name, temp_name) == 0) { + svr_name = strdup(dp->d_name); + closedir(dirp); + return svr_name; + } + } + closedir(dirp); + } + return NULL; +} + + + +int get_alias(char *alias){ + char file[MAXPATHLEN]; + di_node_t root_node; + di_node_t node; + di_minor_t minor = DI_MINOR_NIL; + char tmpnode[MAXPATHLEN]; + char *phys_path; + char *minor_name; + char *value; + int instance; + if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { + fprintf(stderr, "di_init() failed\n"); + exit(1); + } + node = di_drv_first_node(alias, root_node); + while (node != DI_NODE_NIL) { + if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) { + instance = di_instance(node); + phys_path = di_devfs_path(node); + minor_name = di_minor_name(minor); + strcpy(tmpnode, alias); + sprintf(tmpnode, "%s%d", tmpnode, instance); + strcpy(file, "/devices"); + strcat(file, phys_path); + strcat(file, ":"); + strcat(file, minor_name); + value = read_dir(file); + if (value != NULL){ + add_mapping(tmpnode, value); + } + di_devfs_path_free(phys_path); + node = di_drv_next_node(node); + }else{ + node = di_drv_next_node(node); + } + } /* End of the while loop */ + di_fini(root_node); + return (-1); +} + +void build_mapping(){ + char device_name[512]; + int x; + kstat_ctl_t *kc; + kstat_t *ksp; + kstat_io_t kios; + + if ((kc = kstat_open()) == NULL) { + return NULL; + } + + for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { + if (!strcmp(ksp->ks_class, "disk")) { + + if(ksp->ks_type != KSTAT_TYPE_IO) continue; + /* We dont want metadevices appearins as num_diskio */ + if(strcmp(ksp->ks_module, "md")==0) continue; + if((kstat_read(kc, ksp, &kios))==-1) continue; + strncpy(device_name, ksp->ks_name, sizeof device_name); + for(x=0;x<(sizeof device_name);x++){ + if( isdigit((int)device_name[x]) ) break; + } + if(x == sizeof device_name) x--; + device_name[x] = '\0'; + get_alias(device_name); + } + } + + return; +} + +#endif + + + char *f_read_line(FILE *f, const char *string){ /* Max line length. 8k should be more than enough */ static char line[8192]; @@ -60,6 +249,8 @@ char *get_string_match(char *line, regmatch_t *match){ return match_string; } + + #ifndef HAVE_ATOLL static long long atoll(const char *s) { long long value = 0; @@ -144,6 +335,9 @@ int statgrab_init(){ struct uvmexp *uvm = get_uvmexp(); if (uvm == NULL) return 1; } +#endif +#ifdef SOLARIS + build_mapping(); #endif return 0; }