summaryrefslogtreecommitdiff
path: root/extras/chassis_id/chassis_id.c
blob: 857707eac7efa5e3b2a5b4fd7e595b893595fad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*-c-*-: 
 **
 ** (C) 2003 Intel Corporation
 **          Atul Sabharwal <atul.sabharwal@intel.com>
 **
 ** Distributed under the terms of the GNU Public License, v2.0 or
 ** later.
 **
 ** Many parts heavily based on test-skeleton.c, by Ulrich Drepper;
 ** with his permission, they have been re-licensed GPL, and his
 ** copyright still applies on them
 **
 */

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

#include "chassis_id.h"

//#define DEBUG              1

int main(int argc, char **argv, char ** envp)
{
	int chassis_num, slot_num, retval, host_num;
	char disk_snum[255], devpath[255];
	char *ptr;
	int disk_index;

	syslog( LOG_PID| LOG_DAEMON| LOG_ERR, "\n%s", "starting chassis_id" );

#if 0
	ptr = getenv("CHASSIS");
	if (ptr == NULL)
		return -ERROR_NO_CHASSIS;

	sscanf(ptr, "%d", &chassis_num);
	#ifdef DEBUG
	syslog(LOG_PID| LOG_DAEMON| LOG_ERR, "Chassis %d", chassis_num);
	#endif


	ptr = getenv("SLOT");
	if (ptr== NULL)
		return -ERROR_NO_SLOT;

	sscanf(ptr, "%d", &slot_num);
	#ifdef DEBUG
	syslog( LOG_PID|LOG_DAEMON| LOG_ERR, "Slot %d", slot_num);
	#endif
#endif
	ptr = getenv("DEVPATH");
	if (ptr == NULL)
		return -ERROR_NO_DEVPATH;

	sscanf(ptr, "%s", &devpath[0]);
	#ifdef DEBUG
	syslog(LOG_PID|LOG_DAEMON| LOG_ERR, "Devpath %s", devpath);
	#endif

	retval = table_init();
	if (retval < 0)
	return -ERROR_BAD_TABLE;

	getserial_number(devpath, disk_snum);

	/* Now we open the provisioning table t find actual entry for the serial number*/
	disk_index =  table_find_disk(disk_snum, &host_num, &chassis_num, &slot_num);
	if ( disk_index == -1 ) {
		// typical provisioning error
		return -ERROR_NO_DISK;
	} else {
		table_select_disk( disk_index );
	}
	return 0;
}


/* Run SCSI id to find serial number of the device */
int getserial_number( char * devpath, char * snumber )
{
	FILE *fp;
	char vendor[255], model[255], cmd[255];
	int retval;

	sprintf(cmd, "/sbin/scsi_id -s %s -p 0x80", devpath);

	fp = popen(cmd, "r");

	if (fp == NULL)
		return -ERROR_BAD_SNUMBER;

	fscanf(fp, "%s %s %s", vendor, model, snumber);
	#ifdef DEBUG
	syslog(LOG_PID| LOG_DAEMON| LOG_ERR, "\n%s", snumber );
	#endif

	retval = pclose(fp);
	if (retval == -1)
		return -ERROR_BAD_SNUMBER;
	else
		return NO_ERROR;
}