chiark / gitweb /
e619fa9ef32ef9dd6cf81f8c419092f423262f84
[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
19 #define TABLE_SIZE 100
20 #define PROVISION_DB  "/usr/local/bin/provision.tbl"
21
22 struct provision_record
23 {
24         int  id;
25         int  host_num;          //port # or adaptor number
26         int  num_disks;
27         int  chassis_num;
28         int  slot_num;
29         char serial_num[32];
30         char name[32];
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                 fgets(ptr, 80, fp);     
53                 sscanf(ptr, "%d %d %d %d %d %s %s", &ptable[i].id,
54                         &ptable[i].host_num, &ptable[i].num_disks, 
55                         &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         int i;
70
71         for (i = 0; i < ptable_size; i++) {
72                 if (strcmp(ptable[i].serial_num, serialnumber) == 0) {
73                         *host_num =  ptable[i].host_num;
74                         *chassis_num =  ptable[i].chassis_num;
75                         *slot_num = ptable[i].slot_num;
76                         break;
77                 }
78         }
79
80         if (i == ptable_size)
81                 return -1;
82         else
83                 return i;
84 }
85
86 /* This function is primarily there for passing the selected disk entry to udev
87  * so that it can create descriptive GDN for it. So, for that we need to output
88  * this data to stdout.
89  */
90 int table_select_disk( int diskindex )
91 {
92         printf("%d ", ptable[diskindex].chassis_num);
93         printf("%d ", ptable[diskindex].slot_num);
94         printf("%d ", ptable[diskindex].host_num);
95         printf("%s ", ptable[diskindex].name);
96 }
97