chiark / gitweb /
f4689ae24361980af0c7f30d2db4b3939026d8c0
[elogind.git] / extras / chassis_id / table.c
1 /* 
2  * table.c
3  *
4  * Copyright (C) 2004 Intel Corporation.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public
8  * License v2.0 as published by the Free Software Foundation; 
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 021110-1307, USA.
19  *
20  * Authors: Atul Sabharwal
21  *          
22  */
23
24 #include <stdio.h>
25
26 #define TABLE_SIZE 100
27 #define PROVISION_DB  "/usr/local/bin/provision.tbl"
28
29 struct provision_record
30 {
31         int  id;
32         int  num_disks;
33         int  chassis_num;
34         int  slot_num;
35         char serial_num[32];
36         char name[32];
37 } ptable[TABLE_SIZE];
38
39 int ptable_size;
40
41 /* Initialize the provisioning table by reading the data from special file provision.tbl *
42    Return error if something does not work appropriately.                                */
43 int table_init()
44 {
45         FILE *fp;
46         char ptr[255];
47         int i;
48
49         fp = fopen( PROVISION_DB, "r");
50
51         if ((fp== NULL) || feof(fp))
52                  return -1;
53
54         // skip the first line of text which contains descriptive details.
55         fgets(ptr, 80, fp);     
56         i = 0;
57         while (!feof(fp)) {
58                 fgets(ptr, 80, fp);     
59                 sscanf(ptr, "%d %d %d %d %d %s %s", &ptable[i].id,
60                         &ptable[i].num_disks, 
61                         &ptable[i].chassis_num, &ptable[i].slot_num,
62                         ptable[i].serial_num, ptable[i].name);
63                 i++;       
64         }  
65
66         ptable_size = i;
67         fclose(fp);
68         return 0;
69 }
70
71
72 /* return -1 when no disk found. Otherwise return index of disk */
73 int table_find_disk( char * serialnumber , int * host_num, int * chassis_num, int *slot_num)
74 {
75         int i;
76
77         for (i = 0; i < ptable_size; i++) {
78                 if (strcmp(ptable[i].serial_num, serialnumber) == 0) {
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
92  * so that it can create descriptive GDN for it. So, for that we need to output
93  * this data to stdout.
94  */
95 int table_select_disk( int diskindex )
96 {
97         printf("%d ", ptable[diskindex].chassis_num);
98         printf("%d ", ptable[diskindex].slot_num);
99         printf("%s ", ptable[diskindex].name);
100 }
101