chiark / gitweb /
8d14cd1b592a6bbbd270673887ee0b82ebb6d195
[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 #include <string.h>
26
27 #include "chassis_id.h"
28
29 #define TABLE_SIZE 100
30 #define PROVISION_DB  "/usr/local/bin/provision.tbl"
31
32 struct provision_record
33 {
34         int  id;
35         int  num_disks;
36         int  chassis_num;
37         int  slot_num;
38         char serial_num[32];
39         char name[32];
40 } ptable[TABLE_SIZE];
41
42 int ptable_size;
43
44 /* Initialize the provisioning table by reading the data from special file provision.tbl *
45    Return error if something does not work appropriately.                                */
46 int table_init(void)
47 {
48         FILE *fp;
49         char ptr[255];
50         int i;
51
52         fp = fopen( PROVISION_DB, "r");
53
54         if ((fp== NULL) || feof(fp))
55                  return -1;
56
57         // skip the first line of text which contains descriptive details.
58         fgets(ptr, 80, fp);     
59         i = 0;
60         while (!feof(fp)) {
61                 fgets(ptr, 80, fp);     
62                 sscanf(ptr, "%d %d %d %d %d %s %s", &ptable[i].id,
63                         &ptable[i].num_disks, 
64                         &ptable[i].chassis_num, &ptable[i].slot_num,
65                         ptable[i].serial_num, ptable[i].name);
66                 i++;       
67         }  
68
69         ptable_size = i;
70         fclose(fp);
71         return 0;
72 }
73
74
75 /* return -1 when no disk found. Otherwise return index of disk */
76 int table_find_disk(const char *serialnumber , int *chassis_num, int *slot_num)
77 {
78         int i;
79
80         for (i = 0; i < ptable_size; i++) {
81                 if (strcmp(ptable[i].serial_num, serialnumber) == 0) {
82                         *chassis_num =  ptable[i].chassis_num;
83                         *slot_num = ptable[i].slot_num;
84                         break;
85                 }
86         }
87
88         if (i == ptable_size)
89                 return -1;
90         else
91                 return i;
92 }
93
94 /* This function is primarily there for passing the selected disk entry to udev
95  * so that it can create descriptive GDN for it. So, for that we need to output
96  * this data to stdout.
97  */
98 int table_select_disk(int diskindex)
99 {
100         printf("%d ", ptable[diskindex].chassis_num);
101         printf("%d ", ptable[diskindex].slot_num);
102         printf("%s ", ptable[diskindex].name);
103
104         return 0;
105 }
106