summaryrefslogtreecommitdiffstats
path: root/isys/probe.c
blob: 800a14ff8d1402c7bde9250789c0d29efa3df727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <linux/hdreg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

#include "isys.h"
#include "probe.h"

static int dac960GetDevices(struct knownDevices * devices);
static int CompaqSmartArrayGetDevices(struct knownDevices * devices);
static int CompaqSmartArray5300GetDevices(struct knownDevices * devices);
/* Added support for I2O Block devices: Boji Kannanthanam 
	<boji.t.Kannanthanam@intel.com> */
static int I2OGetDevices(struct knownDevices * devices);

static int readFD (int fd, char **buf)
{
    char *p;
    size_t size = 4096;
    int s, filesize;

    *buf = malloc (size);
    if (*buf == 0)
      return -1;

    filesize = 0;
    do {
	p = &(*buf) [filesize];
	s = read (fd, p, 4096);
	if (s < 0)
	    break;
	filesize += s;
	if (s != 4096)
	    break;
	size += 4096;
	*buf = realloc (*buf, size);
    } while (1);

    if (filesize == 0 && s < 0) {
	free (*buf);     
	*buf = NULL;
	return -1;
    }

    return filesize;
}

static int sortDevices(const void * a, const void * b) {
    const struct kddevice * one = a;
    const struct kddevice * two = b;

    return strcmp(one->name, two->name);
}

static int deviceKnown(struct knownDevices * devices, char * dev) {
    int i;

    for (i = 0; i < devices->numKnown; i++)
    	if (!strcmp(devices->known[i].name, dev)) return 1;

    return 0;
}

static void addDevice(struct knownDevices * devices, struct kddevice dev) {
    if (devices->numKnown == devices->numKnownAlloced) {
    	devices->numKnownAlloced += 5;
    	devices->known = realloc(devices->known, 
		sizeof(*devices->known) * devices->numKnownAlloced);
    }

    devices->known[devices->numKnown++] = dev;
}

void kdAddDevice(struct knownDevices * devices, enum deviceClass devClass, 
		 char * devName, char * devModel) {
    struct kddevice new;

    new.class = devClass;
    new.name = devName;
    new.model = devModel;

    addDevice(devices, new);
}

void kdFree(struct knownDevices * devices) {
    if (devices->known) free(devices->known);
    devices->known = NULL;
    devices->numKnown = devices->numKnownAlloced = 0;
}

int kdFindNetList(struct knownDevices * devices, int code) {
    int fd;
    char *buf;
    char * start, * end;
    struct kddevice newDevice;
    int s;

    if ((fd = open("/proc/net/dev", O_RDONLY)) < 0) {
	fprintf(stderr, "failed to open /proc/net/dev!\n");
	return 1;
    }

    s = readFD(fd, &buf);
    close(fd);
    if (s < 0) {
	fprintf(stderr, "error reading /proc/net/dev!\n");
	return 1;
    }

    buf[s] = '\0';

    /* skip the first two lines */
    start = strchr(buf, '\n');
    if (!start) goto bye;
    start = strchr(start + 1, '\n');
    if (!start) goto bye;

    start++;
    while (start && *start) {
	while (isspace(*start)) start++;
	end = strchr(start, ':');
	if (!end) goto bye;
	*end = '\0';
	
    	if (strcmp(start, "lo")) {
	    if (deviceKnown(devices, start)) continue;

	    newDevice.name = strdup(start);
	    newDevice.model = NULL;
	    newDevice.class = CLASS_NETWORK;
	    newDevice.code = code;
	    addDevice(devices, newDevice);
	}

	start = strchr(end + 1, '\n');
	if (start) start++;
    }

    qsort(devices->known, devices->numKnown, sizeof(*devices->known),
	  sortDevices);

bye:
    free (buf);
    return 0;
}

int kdFindIdeList(struct knownDevices * devices, int code) {
    return kdFindFilteredIdeList(devices, code, NULL);
}

int kdFindFilteredIdeList(struct knownDevices * devices, int code, 
			  kdFilterType filter) {
    DIR * dir;
    char path[80];
    int fd, i;
    struct dirent * ent;
    struct kddevice device;

    if (access("/proc/ide", R_OK)) return 0;

    if (!(dir = opendir("/proc/ide"))) {
	return 1;
    }

    /* set errno to 0, so we can tell when readdir() fails */
    errno = 0;
    while ((ent = readdir(dir))) {
    	if (!deviceKnown(devices, ent->d_name)) {
	    sprintf(path, "/proc/ide/%s/media", ent->d_name);
	    if ((fd = open(path, O_RDONLY)) >= 0) {
		i = read(fd, path, 50);
		close(fd);
		path[i - 1] = '\0';		/* chop off trailing \n */

		device.code = code;

		device.class = CLASS_UNSPEC;
		if (!strcmp(path, "cdrom")) 
		    device.class = CLASS_CDROM;
		else if (!strcmp(path, "disk"))
		    device.class = CLASS_HD;
		else if (!strcmp(path, "floppy"))
		    device.class = CLASS_FLOPPY;

		if (device.class != CLASS_UNSPEC) {
		    device.name = strdup(ent->d_name);

		    sprintf(path, "/proc/ide/%s/model", ent->d_name);
		    if ((fd = open(path, O_RDONLY)) >= 0) {
			i = read(fd, path, 50);
			close(fd);
			path[i - 1] = '\0';	/* chop off trailing \n */
			device.model = strdup(path);
		    }

		    if (filter && !filter(&device)) {
			free(device.model);
			free(device.name);
		    } else {
			addDevice(devices, device);
		    }
		}
	    }
	}

        errno = 0;          
    }

    closedir(dir);

    qsort(devices->known, devices->numKnown, sizeof(*devices->known),
	  sortDevices);

    return 0;
}

#define SCSISCSI_TOP	0
#define SCSISCSI_HOST 	1
#define SCSISCSI_VENDOR 2
#define SCSISCSI_TYPE 	3

int kdFindScsiList(struct knownDevices * devices, int code) {
    int fd;
    char *buf;
    char linebuf[80];
    char typebuf[10];
    int i, state = SCSISCSI_TOP;
    char * start, * chptr, * next, *end;
    int driveNum = 0;
    char cdromNum = '0';
    char tapeNum = '0';
    struct kddevice device;
    int val = 0;

    if (access("/proc/scsi/scsi", R_OK)) {
	dac960GetDevices(devices);
	CompaqSmartArrayGetDevices(devices);
	CompaqSmartArray5300GetDevices(devices);
	I2OGetDevices(devices);
	return 0;
    }

    fd = open("/proc/scsi/scsi", O_RDONLY);
    if (fd < 0) return 1;
    
    i = readFD(fd, &buf);
    if (i < 1) {
        close(fd);
	return 1;
    }
    close(fd);
    buf[i] = '\0';

    if (!strncmp(buf, "Attached devices: none", 22)) {
	dac960GetDevices(devices);
	CompaqSmartArrayGetDevices(devices);
	CompaqSmartArray5300GetDevices(devices);
	I2OGetDevices(devices);
	goto bye;
    }

    start = buf;
    while (*start) {
	chptr = start;
 	while (*chptr != '\n') chptr++;
	*chptr = '\0';
	next = chptr + 1;

	switch (state) {
	  case SCSISCSI_TOP:
	    if (strcmp("Attached devices: ", start)) {
		val = -1;
		goto bye;
	    }
	    state = SCSISCSI_HOST;
	    break;

	  case SCSISCSI_HOST:
	    if (strncmp("Host: ", start, 6)) {
		val = -1;
		goto bye;
	    }

	    start = strstr(start, "Id: ");
	    if (!start) {
		val = -1;
		goto bye;
	    }
	    start += 4;

	    /*id = strtol(start, NULL, 10);*/

	    state = SCSISCSI_VENDOR;
	    break;

	  case SCSISCSI_VENDOR:
	    if (strncmp("  Vendor: ", start, 10)) {
		val = -1;
		goto bye;
	    }

	    start += 10;
	    end = chptr = strstr(start, "Model:");
	    if (!chptr) {
		val = -1;
		goto bye;
	    }

	    chptr--;
	    while (*chptr == ' ' && *chptr != ':' ) chptr--;
	    if (*chptr == ':') {
		    chptr++;
		    *(chptr + 1) = '\0';
		    strcpy(linebuf,"Unknown");
	    } else {
		    *(chptr + 1) = '\0';
		    strcpy(linebuf, start);
	    }
	    *linebuf = toupper(*linebuf);
	    chptr = linebuf + 1;
	    while (*chptr) {
		*chptr = tolower(*chptr);
		chptr++;
	    }

	    start = end;  /* beginning of "Model:" */
	    start += 7;
		
	    chptr = strstr(start, "Rev:");
	    if (!chptr) {
		val = -1;
		goto bye;
	    }
	   
	    chptr--;
	    while (*chptr == ' ') chptr--;
	    *(chptr + 1) = '\0';

	    strcat(linebuf, " ");
	    strcat(linebuf, start);

	    state = SCSISCSI_TYPE;

	    break;

	  case SCSISCSI_TYPE:
	    if (strncmp("  Type:", start, 7)) {
		val = -1;
		goto bye;
	    }
	    *typebuf = '\0';
	    if (strstr(start, "Direct-Access")) {
		if (driveNum < 26)
		    sprintf(typebuf, "sd%c", driveNum + 'a');
		else
		    sprintf(typebuf, "sd%c%c", (driveNum / 26 - 1) + 'a',
			    (driveNum % 26) + 'a');
		driveNum++;
		device.class = CLASS_HD;
	    } else if (strstr(start, "Sequential-Access")) {
		sprintf(typebuf, "st%c", tapeNum++);
		device.class = CLASS_TAPE;
	    } else if (strstr(start, "CD-ROM")) {
		sprintf(typebuf, "scd%c", cdromNum++);
		device.class = CLASS_CDROM;
	    }

	    if (*typebuf && !deviceKnown(devices, typebuf)) {
		device.name = strdup(typebuf);
		device.model = strdup(linebuf);
		device.code = code;

		/* Do we need this for anything?
		sdi[numMatches].bus = 0;
		sdi[numMatches].id = id;
		*/

		addDevice(devices, device);
	    }

	    state = SCSISCSI_HOST;
	}

	start = next;
    }

    dac960GetDevices(devices);
    CompaqSmartArrayGetDevices(devices);
    CompaqSmartArray5300GetDevices(devices);
    I2OGetDevices(devices);

    qsort(devices->known, devices->numKnown, sizeof(*devices->known),
	  sortDevices);

bye:
    free (buf);
    return val;
}

int kdFindDasdList(struct knownDevices * devices, int code) {
  /* patch for s390 by Oliver Paukstadt <oliver.paukstadt@millenux.com> */
  /* based upon code by Erik Tews <erik.tews@gmx.net> */

  FILE * fd;
  struct kddevice device;
  char line[200];
  char name[10];
  char status[10];
  char model[30];

  if (access("/proc/dasd/devices", R_OK)) return 0;
  /* a system without /proc/dasd/devices is nothing to worry about */

  fd = fopen ("/proc/dasd/devices", "r");
  if (fd == NULL) return 1;

  /* Every line in this file is a harddisk */
  while ((fgets(line, 190, fd)) != NULL) {

    sscanf(line, "%*X %*[(A-Z)] at (%*d:%*d) is %[a-z0-9]:%s ", name, status);
    /* Take every dasd, formated and unformated */

    if (!deviceKnown(devices, name)) {
      snprintf(model, sizeof(model), "IBM DASD (%s)", status);
      device.class = CLASS_HD;
      device.name = strdup(name);
      device.model = strdup(model);
      device.code = code;
      addDevice(devices, device);
    }
  }
  fclose (fd);

  qsort(devices->known, devices->numKnown, sizeof(*devices->known),
	sortDevices);
  return 0;

}

struct knownDevices kdInit(void) {
    struct knownDevices kd;

    memset(&kd, 0, sizeof(kd));

    return kd;
}

static int dac960GetDevices(struct knownDevices * devices) {
    struct kddevice newDevice;
    char ctl[50];
    int ctlNum = 0;
    char *buf = NULL;
    int fd;
    int i;
    char * start, * chptr;

    sprintf(ctl, "/proc/rd/c%d/current_status", ctlNum++);

    while ((fd = open(ctl, O_RDONLY)) >= 0) {
    	free (buf);
	i = readFD(fd, &buf);
	buf[i] = '\0';
	start = buf;
	while (start && (start = strstr(start, "/dev/rd/"))) {
	    start += 5;
	    chptr = strchr(start, ':');
	    if (!chptr)
		continue;

	    *chptr = '\0';
	    if (!deviceKnown(devices, start)) {
		newDevice.name = strdup(start);

		start = chptr + 2;
		chptr = strchr(start, '\n');
		*chptr = '\0';

		newDevice.model = strdup(start);
		newDevice.class = CLASS_HD;
		addDevice(devices, newDevice);

		*chptr = '\n';
	    } else {
		*chptr = '\0';
	    }

	    start = strchr(chptr, '\n');
	    if (start) start++;
	}

	sprintf(ctl, "/proc/rd/c%d/current_status", ctlNum++);
    }

    free (buf);
    return 0;
}

static int CompaqSmartArrayGetDevices(struct knownDevices * devices) {
    struct kddevice newDevice;
    FILE *f;
    char buf[256];
    char *ptr;
    int ctlNum = 0;
    char ctl[64];
    char *path;
	
    path = "/proc/driver/array";

    sprintf(ctl, "%s/ida%d", path, ctlNum++);
		
    f = fopen(ctl, "r");
    if (!f) {
	    path = "/proc/ida";
	    sprintf(ctl, "%s/ida%d", path, ctlNum++);
	    f = fopen(ctl, "r");
    }

    while (f) {
	while (fgets(buf, sizeof(buf) - 1, f)) {
	    if (!strncmp(buf, "ida/", 4)) {
		ptr = strchr(buf, ':');
		*ptr = '\0';

		if (!deviceKnown(devices, buf)) {
		    newDevice.name = strdup(buf);
		    newDevice.model = strdup("Compaq RAID logical disk");
		    newDevice.class = CLASS_HD;
		    addDevice(devices, newDevice);
		}
	    }
	}
	sprintf(ctl, "%s/ida%d", path, ctlNum++);
        fclose(f);
	f = fopen(ctl, "r");
    }

    
    return 0;
}

static int I2OGetDevices(struct knownDevices * devices) {
    struct kddevice newDevice;
    int fd, i;
    char *buf;
    char * start, *chptr, *next, *end;
    char ctl[40];

    /* Read from /proc/partitions */
    fd = open("/proc/partitions", O_RDONLY);
    if (fd < 0) 
	{
	    fprintf(stderr, "failed to open /proc/partitions!\n");
	    return 1;
	}

    i = readFD(fd, &buf);
    if (i < 1) {
        close(fd);
        free (buf);
	fprintf(stderr, "error reading /proc/partitions!\n");
        return 1;
    }
    close(fd);
    buf[i] = '\0';

    /* skip the first two lines */
    start = strchr(buf, '\n');
    if (!start) goto bye;

    start = strchr(start + 1, '\n');
    if (!start) goto bye;

    start++;
    end = start + strlen(start);
    while (*start && start < end) {
	/* parse till end of line and store the start of next line. */
	chptr = start;
	while (*chptr != '\n') chptr++;
	*chptr = '\0';
	next = chptr + 1;

	/*get rid of anything which is not alpha */
	while ( !(isalpha(*start)) ) start++;
		
	/* See if it is an I2O Block device */
	if( ! strncmp("i2o/", start, 4))
	    {
		i = 0;
		while( !(isspace(*start)) ) 
		    {
			ctl[i] = *start;
			i++;
			start++;
		    }
		ctl[i] = '\0';
    		if (i < 1) { 
		    free (buf);
		    return 1; 
		}
		/* We don't want partitions just the disks ! */
		if ( !isdigit(ctl[i-1]) ){
		    if (!deviceKnown(devices, ctl)) {
			newDevice.name = strdup(ctl);
			newDevice.model = strdup("I2O Block Device");
			newDevice.class = CLASS_HD;
			addDevice(devices, newDevice);
		    }
		}
	    } /* end of if it is an i2o device */
	start = next;
	end = start + strlen(start);
    } /* end of while */
 bye:
    free (buf);
    return 0;
}


static int CompaqSmartArray5300GetDevices(struct knownDevices * devices) {
    struct kddevice newDevice;
    FILE *f;
    char buf[256];
    char *ptr;
    int ctlNum = 0;
    char ctl[64];
    char *path;
	
    path = "/proc/driver/cciss";

    sprintf(ctl, "%s/cciss%d", path, ctlNum++);
		
    f = fopen(ctl, "r");
    if (!f) {
	    path = "/proc/cciss";
	    sprintf(ctl, "%s/cciss%d", path, ctlNum++);
	    f = fopen(ctl, "r");
    }

    while (f) {
	while (fgets(buf, sizeof(buf) - 1, f)) {
	    if (!strncmp(buf, "cciss/", 6)) {
		ptr = strchr(buf, ':');
		*ptr = '\0';

		if (!deviceKnown(devices, buf)) {
		    newDevice.name = strdup(buf);
		    newDevice.model = strdup("Compaq RAID logical disk");
		    newDevice.class = CLASS_HD;
		    addDevice(devices, newDevice);
		}
	    }
	}
	sprintf(ctl, "%s/cciss%d", path, ctlNum++);
	fclose(f);
	f = fopen (ctl, "r");
    }

    
    return 0;
}