chiark / gitweb /
3fdfa96018e9af4e31c33a81adfaed29853192b9
[elogind.git] / extras / chassis_id / chassis_id.c
1 /* 
2  * chassis_id.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 v 2.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 <stdlib.h>
26 #include <syslog.h>
27
28 #include "chassis_id.h"
29
30 //#define DEBUG              1
31
32 int main(int argc, char **argv, char ** envp)
33 {
34         int chassis_num, slot_num, retval;
35         char disk_snum[255], devpath[255];
36         char *ptr;
37         int disk_index;
38
39         syslog( LOG_PID| LOG_DAEMON| LOG_ERR, "\n%s", "starting chassis_id" );
40
41         ptr = getenv("DEVPATH");
42         if (ptr == NULL)
43                 return -ERROR_NO_DEVPATH;
44
45         sscanf(ptr, "%s", &devpath[0]);
46         #ifdef DEBUG
47         syslog(LOG_PID|LOG_DAEMON| LOG_ERR, "Devpath %s", devpath);
48         #endif
49
50         retval = table_init();
51         if (retval < 0)
52         return -ERROR_BAD_TABLE;
53
54         getserial_number(devpath, disk_snum);
55
56         /* Now we open the provisioning table t find actual entry for the serial number*/
57         disk_index =  table_find_disk(disk_snum, &chassis_num, &slot_num);
58         if ( disk_index == -1 ) {
59                 // typical provisioning error
60                 return -ERROR_NO_DISK;
61         } else {
62                 table_select_disk( disk_index );
63         }
64         return 0;
65 }
66
67
68 /* Run SCSI id to find serial number of the device */
69 int getserial_number( char * devpath, char * snumber )
70 {
71         FILE *fp;
72         char vendor[255], model[255], cmd[255];
73         int retval;
74
75         sprintf(cmd, "/sbin/scsi_id -s %s -p 0x80", devpath);
76
77         fp = popen(cmd, "r");
78
79         if (fp == NULL)
80                 return -ERROR_BAD_SNUMBER;
81
82         fscanf(fp, "%s %s %s", vendor, model, snumber);
83         #ifdef DEBUG
84         syslog(LOG_PID| LOG_DAEMON| LOG_ERR, "\n%s", snumber );
85         #endif
86
87         retval = pclose(fp);
88         if (retval == -1)
89                 return -ERROR_BAD_SNUMBER;
90         else
91                 return NO_ERROR;
92 }
93