ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/tools.c (file contents):
Revision 1.12 by tdb, Mon Nov 10 21:07:04 2003 UTC vs.
Revision 1.16 by tdb, Thu Nov 20 12:13:12 2003 UTC

# Line 37 | Line 37
37  
38   #include "tools.h"
39  
40 + #ifdef SOLARIS
41 + #include <libdevinfo.h>
42 + #include <kstat.h>
43 + #include <unistd.h>
44 + #include <ctype.h>
45 + #include <sys/types.h>
46 + #include <sys/dkio.h>
47 + #include <sys/stat.h>
48 + #include <fcntl.h>
49 + #include <sys/fcntl.h>
50 + #include <dirent.h>
51 + #endif
52 +
53 + #ifdef SOLARIS
54 + struct map{
55 +        char *bsd;
56 +        char *svr;
57 +
58 +        struct map *next;
59 + };
60 + typedef struct map mapping_t;
61 +
62 + static mapping_t *mapping = NULL;
63 +
64 + char *get_svr_from_bsd(char *bsd){
65 +        mapping_t *map_ptr;
66 +        for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
67 +                if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
68 +
69 +        return bsd;
70 + }
71 +
72 + void add_mapping(char *bsd, char *svr){
73 +        mapping_t *map_ptr;
74 +        mapping_t *map_end_ptr;
75 +
76 +        bsd = strdup(bsd);
77 +        svr = strdup(svr);
78 +
79 +        if (mapping == NULL){
80 +                mapping = malloc(sizeof(mapping_t));
81 +                if (mapping == NULL) return;
82 +                map_ptr = mapping;
83 +        }else{
84 +                /* See if its already been added */
85 +                for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){
86 +                        if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){
87 +                                return;
88 +                        }
89 +                        map_end_ptr = map_ptr;
90 +                }
91 +
92 +                /* We've reached end of list and not found the entry.. So we need to malloc
93 +                 * new mapping_t
94 +                 */
95 +                map_end_ptr->next = malloc(sizeof(mapping_t));
96 +                if (map_end_ptr->next == NULL) return;
97 +                map_ptr = map_end_ptr->next;
98 +        }
99 +
100 +        map_ptr->next = NULL;
101 +        map_ptr->bsd = bsd;
102 +        map_ptr->svr = svr;
103 +
104 +        return;
105 + }
106 +
107 + char *read_dir(char *disk_path){
108 +        DIR *dirp;
109 +        struct dirent *dp;
110 +        struct stat stbuf;
111 +        char *svr_name;
112 +        char current_dir[MAXPATHLEN];
113 +        char file_name[MAXPATHLEN];
114 +        char temp_name[MAXPATHLEN];
115 +        char dir_dname[MAXPATHLEN];
116 +        char *dsk_dir;
117 +        int x;
118 +
119 +        dsk_dir = "/dev/osa/dev/dsk";
120 +        strncpy(current_dir, dsk_dir, sizeof current_dir);
121 +        if ((dirp = opendir(current_dir)) == NULL){
122 +                dsk_dir = "/dev/dsk";
123 +                snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
124 +                if ((dirp = opendir(current_dir)) == NULL){
125 +                        return NULL;
126 +                }
127 +        }
128 +
129 +        while ((dp = readdir(dirp)) != NULL){
130 +                snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
131 +                snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
132 +                stat(dir_dname,&stbuf);
133 +
134 +                if (S_ISBLK(stbuf.st_mode)){
135 +                        x = readlink(dir_dname, file_name, sizeof(file_name));
136 +                        file_name[x] = '\0';
137 +                        if (strcmp(file_name, temp_name) == 0) {
138 +                                svr_name = strdup(dp->d_name);
139 +                                closedir(dirp);
140 +                                return svr_name;
141 +                        }
142 +                }
143 +        }
144 +        closedir(dirp);
145 +        return NULL;
146 + }
147 +
148 +
149 +
150 + int get_alias(char *alias){
151 +        char file[MAXPATHLEN];
152 +        di_node_t root_node;
153 +        di_node_t node;
154 +        di_minor_t minor = DI_MINOR_NIL;
155 +        char tmpnode[MAXPATHLEN];
156 +        char *phys_path;
157 +        char *minor_name;
158 +        char *value;
159 +        int instance;
160 +        if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
161 +                fprintf(stderr, "di_init() failed\n");
162 +                exit(1);
163 +        }
164 +        node = di_drv_first_node(alias, root_node);
165 +        while (node != DI_NODE_NIL) {
166 +                if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
167 +                        instance = di_instance(node);
168 +                        phys_path = di_devfs_path(node);
169 +                        minor_name = di_minor_name(minor);
170 +                        strcpy(tmpnode, alias);
171 +                        sprintf(tmpnode, "%s%d", tmpnode, instance);
172 +                        strcpy(file, "/devices");
173 +                        strcat(file, phys_path);
174 +                        strcat(file, ":");
175 +                        strcat(file, minor_name);
176 +                        value = read_dir(file);
177 +                        if (value != NULL){
178 +                                add_mapping(tmpnode, value);
179 +                        }
180 +                        di_devfs_path_free(phys_path);
181 +                        node = di_drv_next_node(node);
182 +                }else{
183 +                        node = di_drv_next_node(node);
184 +                }
185 +        }
186 +        di_fini(root_node);
187 +        return (-1);
188 + }
189 +
190 + void build_mapping(){
191 +        char device_name[512];
192 +        int x;
193 +        kstat_ctl_t *kc;
194 +        kstat_t *ksp;
195 +        kstat_io_t kios;
196 +
197 +        if ((kc = kstat_open()) == NULL) {
198 +                return;
199 +        }
200 +
201 +        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
202 +                if (!strcmp(ksp->ks_class, "disk")) {
203 +                        if(ksp->ks_type != KSTAT_TYPE_IO) continue;
204 +                        /* We dont want metadevices appearing as num_diskio */
205 +                        if(strcmp(ksp->ks_module, "md")==0) continue;
206 +                        if((kstat_read(kc, ksp, &kios))==-1) continue;
207 +                        strncpy(device_name, ksp->ks_name, sizeof device_name);
208 +                        for(x=0;x<(sizeof device_name);x++){
209 +                                if( isdigit((int)device_name[x]) ) break;
210 +                        }
211 +                        if(x == sizeof device_name) x--;
212 +                        device_name[x] = '\0';
213 +                        get_alias(device_name);
214 +                }
215 +        }
216 +
217 +        return;
218 + }
219 +
220 + #endif
221 +
222 +
223 +
224   char *f_read_line(FILE *f, const char *string){
225          /* Max line length. 8k should be more than enough */
226          static char line[8192];
# Line 60 | Line 244 | char *get_string_match(char *line, regmatch_t *match){
244          return match_string;
245   }
246  
247 +
248 +
249 + #ifndef HAVE_ATOLL
250 + static long long atoll(const char *s) {
251 +        long long value = 0;
252 +        int isneg = 0;
253 +
254 +        while (*s == ' ' || *s == '\t') {
255 +                s++;
256 +        }
257 +        if (*s == '-') {
258 +                isneg = 1;
259 +                s++;
260 +        }
261 +        while (*s >= '0' && *s <= '9') {
262 +                value = (10 * value) + (*s - '0');
263 +                s++;
264 +        }
265 +        return (isneg ? -value : value);
266 + }
267 + #endif
268 +
269   long long get_ll_match(char *line, regmatch_t *match){
270          char *ptr;
271          long long num;
272  
273          ptr=line+match->rm_so;
68 #ifdef HAVE_ATOLL
274          num=atoll(ptr);
70 #else
71        /* Don't have atoll, so use this bodge instead */
72        {
73                long numl;
74                numl=atol(ptr);
75                num=numl;
76        }
77 #endif
275  
276          return num;
277   }
# Line 125 | Line 322 | int statgrab_init(){
322   #ifdef ALLBSD
323          {
324                  kvm_t *kvmd = get_kvm();
325 <                if (kvmd == NULL) return 1;
325 >                if (kvmd == NULL) return 1;
326          }
327   #endif
328   #ifdef NETBSD
# Line 133 | Line 330 | int statgrab_init(){
330                  struct uvmexp *uvm = get_uvmexp();
331                  if (uvm == NULL) return 1;
332          }
333 + #endif
334 + #ifdef SOLARIS
335 +        build_mapping();
336   #endif
337          return 0;
338   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines