chiark / gitweb /
418497edbe852141cbf7353464ffdadcf53d86ae
[elogind.git] / extras / chassis_id / table.c
1   /* -*-c-*-: 
2    **
3    ** (C) 2003 Intel Corporation
4    **          Atul Sabharwal <atul.sabharwal@intel.com>
5    **
6    ** $Id: table.c,v 1.4 2004/03/18 21:56:24 atul Exp $
7    **
8    ** Distributed under the terms of the GNU Public License, v2.0 or
9    ** later.
10    **
11    ** Many parts heavily based on test-skeleton.c, by Ulrich Drepper;
12    ** with his permission, they have been re-licensed GPL, and his
13    ** copyright still applies on them. 
14    **
15    */
16
17 #include <stdio.h>
18 #define TABLE_SIZE 100
19 #define PROVISION_DB  "/usr/local/bin/provision.tbl"
20
21 struct provision_record
22 {
23     int  id;
24     int  host_num;          //port # or adaptor number
25     int  num_disks;
26     int  chassis_num;
27     int  slot_num;
28     char serial_num[32];
29     char name[32];
30
31 } ptable[TABLE_SIZE];
32
33 int ptable_size;
34
35 /* Initialize the provisioning table by reading the data from special file provision.tbl *
36    Return error if something does not work appropriately.                                */
37 int table_init()
38 {
39    FILE *fp;
40    char ptr[255];
41    int i;
42    
43    fp=fopen( PROVISION_DB, "r");
44
45    if ((fp== NULL) || feof(fp))
46         return -1;
47
48    //skip the first line of text which contains descriptive details.
49    fgets(ptr, 80, fp);  
50    i = 0;
51    while (!feof(fp))
52    {
53        fgets(ptr, 80, fp);      
54        sscanf( ptr, "%d %d %d %d %d %s %s", &ptable[i].id, &ptable[i].host_num, 
55             &ptable[i].num_disks, &ptable[i].chassis_num, &ptable[i].slot_num, 
56               ptable[i].serial_num, ptable[i].name );
57        i++;       
58    }  
59
60    ptable_size = i;
61    fclose(fp);
62    return 0;
63 }
64
65
66 /*  return -1 when no disk found. Otherwise return index of disk */
67 int table_find_disk( char * serialnumber , int * host_num, int * chassis_num, int *slot_num)
68 {
69     
70    int i;
71  
72    for(i = 0; i < ptable_size; i++)
73    {
74
75       if(strcmp(ptable[i].serial_num, serialnumber) == 0)
76       {
77
78             *host_num =  ptable[i].host_num;
79             *chassis_num =  ptable[i].chassis_num;
80             *slot_num = ptable[i].slot_num;
81             break;
82       }
83    }
84
85    if(i == ptable_size)
86        return -1;
87    else
88        return i;
89 }
90
91 /* This function is primarily there for passing the selected disk entry to udev so that *
92  * it can create descriptive GDN for it. So, for that we need to output this data to    *
93  * stdout.                                                                              */
94 int table_select_disk( int diskindex )
95 {
96    printf("%d ", ptable[diskindex].chassis_num);
97    printf("%d ", ptable[diskindex].slot_num);
98    printf("%d ", ptable[diskindex].host_num);
99    printf("%s ", ptable[diskindex].name);
100
101 }
102