[go: up one dir, main page]

File: fcp.c

package info (click to toggle)
s390-tools 1.8.3-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,584 kB
  • ctags: 5,973
  • sloc: ansic: 34,053; cpp: 8,153; sh: 7,784; asm: 5,573; perl: 2,519; makefile: 799
file content (213 lines) | stat: -rw-r--r-- 4,560 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright IBM Corp 2008
 * Author: Hans-Joachim Picht <hans@linux.vnet.ibm.com>
 *
 *  Linux for System z shutdown actions
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
#include <syslog.h>
#include <pthread.h>
#include "chreipl.h"


/*
 * return the current reipl type from /sys/firmware/reipl/reipl_type
 * 0 = fcp, 1 = ccw, -1, error
 */
int get_reipl_type(void)
{
	FILE *filp;
	char reipltype[4];
	char path[4096];
	int rc;

	strcpy(path, "/sys/firmware/reipl/reipl_type");
	if (access(path, R_OK) == 0) {
		filp = fopen(path, "r");
		if (!filp) {
			fprintf(stderr,	"%s: Can not open /sys/firmware/"
				"reipl/reipl_type: ", name);
			fprintf(stderr, "%s\n", strerror(errno));
			return -1;
		}
		rc = fscanf(filp, "%s", reipltype);
		fclose(filp);
		if (rc < 0) {
			fprintf(stderr, "%s: Failed to read "
				"/sys/firmware/reipl/reipl_type:", name);
			fprintf(stderr, "%s\n", strerror(errno));
			return -1;
		}
		if (strncmp(reipltype, "fcp", strlen("fcp")) == 0)
			return T_FCP;
		else if (strncmp(reipltype, "ccw", strlen("ccw")) == 0)
			return T_CCW;
		/* TODO: add NSS support */
	} else {
		fprintf(stderr, "%s: Can not open /sys/firmware/reipl/"
			"reipl_type:", name);
		fprintf(stderr, " %s\n", strerror(errno));
	}
	return -1;
}

/*
 * check if the specified device number is a valid device number
 * which can be found in the /sys/bus/ccw/drivers/zfcp/
 * structure
 */
int isfcpdev(char *devno)
{
	char path[4096];

	sprintf(path, "/sys/bus/ccw/drivers/zfcp/%s", devno);
	if (chdir(path) != 0)
		return -1;
	return 0;
}

/*
 * check if a device, e.g. /dev/sda1 has a valid entry in /proc/partitions
 * return 0 is true, -1 otherwise
 */
int ispartition(char *devno)
{
	FILE *filp;
	size_t bytes_read;
	char buffer[4096];
	char *match;

	filp = fopen("/proc/partitions", "r");
	if (!filp) {
		fprintf(stderr, "%s: Can not open /proc/partitions: %s\n",
			name, strerror(errno));
		return -1;
	}
	while ((bytes_read = fread(buffer, 1, sizeof(buffer), filp)) != 0) {
		buffer[bytes_read] = '\0';
		match = strstr(buffer, devno);
		if (match != NULL)
			return 0;
	}
	fclose(filp);
	return -1;
}

/*
 * return the wwpn of a device
 */
int get_wwpn(char *device, char *wwpn)
{
	FILE *filp;
	char path[4096];
	char buf[4096];
	int rc;

	sprintf(path, "/sys/block/%s/device/wwpn", device);
	filp = fopen(path, "r");
	if (!filp) {
	       fprintf(stderr, "%s: Can not open %s: %s\n",
		       name, path, strerror(errno));
	       return -1;
	}
	rc = fscanf(filp, "%s", buf);
	if (rc <= 0)
	       return -1;
	strncpy(wwpn, buf, 20);
	fclose(filp);
	return 0;
}


/*
 * return the lun of a device
 */
int get_lun(char *device, char *lun)
{
	FILE *filp;
	char buf[4096];
	char path[4096];
	int rc;

	sprintf(path, "/sys/block/%s/device/fcp_lun", device);
	filp = fopen(path, "r");
	if (!filp) {
		fprintf(stderr, "%s: Can not open %s: %s\n",
			name, path, strerror(errno));
		return -1;
	}
	rc = fscanf(filp, "%s", buf);
	if (rc <= 0)
		return -1;
	strncpy(lun, buf, 20);
	fclose(filp);
	return 0;
}

/*
 * return the device number of a device
 */
int get_fcp_devno(char *device, char *devno)
{
	FILE *filp;
	char buf[4096];
	char path[4096];
	int rc;

	sprintf(path, "/sys/block/%s/device/hba_id", device);
	filp = fopen(path, "r");
	if (!filp) {
		fprintf(stderr, "%s: Can not open %s: %s\n",
			name, path, strerror(errno));
		return -1;
	}
	rc = fscanf(filp, "%s", buf);
	if (rc <= 0)
		return -1;
	strcpy(devno, buf);
	fclose(filp);
	return 0;
}

/*
 * get the device from the partition
 */
int get_fcp_dev(char *partition, char *device)
{
	char *start, *stop;
	size_t len;

	/*
	 * find the text between the two slashes
	 * in /dev/sda1, e.g. sda
	 */
	start = strchr(partition, '/');
	stop  = strrchr(partition, '/');
	len = stop-start;
	if (start == NULL || stop == NULL || len <= 0)
		return -1;
	strncpy(device, stop+1, len-1);
	device[len] = '\0';
	return 0;
}