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