[go: up one dir, main page]

File: main.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 (379 lines) | stat: -rw-r--r-- 9,889 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright IBM Corp 2008
 * Author: Hans-Joachim Picht <hans@linux.vnet.ibm.com>
 *
 * Linux for System z shutdown action
 *
 * 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 <setjmp.h>
#include "chreipl.h"

int use_ccw = 1;	/* default is a ccw device */
char loadparm[9];	/* the entry in the boot menu */
int loadparm_set;
int verbose;		/* default: don't be verbose */
int bootprog;		/* default bootprog value is 0 */
int bootprog_set;
char wwpn[20];		/* 18 character +0x" */
int wwpn_set;
char lun[20];		/* 18 character +0x" */
int lun_set;
char devno[9];		/* device number e.g. 0.0.4711 */
int devno_set;
char device[9];		/* the device itself, e.g. sda1 */
char partition[15];	/* partition, e.g. /dev/dasda1 */
int partition_set;
char devparent[9];	/* the device a partion belongs to. e.g. dasda */
char saction[8];	/* the shutdown action */
char name[256];		/* program name */
int action;		/* either CCW, FCP or NODE */

/*
 * "main" function for the lsreipl related stuff
 */
int lsreipl(int argc, char *argv[])
{
	int rc;
	char bootprog[1024], lba[1024], val[9];

	/* parse the command line options in getop.c */
	parse_lsreipl_options(argc, argv);

	rc = get_reipl_type();
	if (rc == 0) {
		printf("Re-IPL type: fcp\n");
		rc = strrd(wwpn, "/sys/firmware/reipl/fcp/wwpn");
		if (rc != 0)
			exit(1);
		rc = strrd(lun, "/sys/firmware/reipl/fcp/lun");
		if (rc != 0)
			exit(1);
		rc = strrd(devno, "/sys/firmware/reipl/fcp/device");
		if (rc != 0)
			exit(1);
		rc = strrd(bootprog, "/sys/firmware/reipl/fcp/bootprog");
		if (rc != 0)
			exit(1);
		rc = strrd(lba, "/sys/firmware/reipl/fcp/br_lba");
		if (rc != 0)
			exit(1);
		if (strlen(wwpn) > 0)
			printf("WWPN:        %s\n", wwpn);
		if (strlen(lun) > 0)
			printf("LUN:         %s\n", lun);
		if (strlen(devno) > 0)
			printf("Device:      %s\n", devno);
		if (strlen(bootprog) > 0)
			printf("bootprog:    %s\n", bootprog);
		if (strlen(lba) > 0)
			printf("br_lba:      %s\n", lba);
	}
	if (rc == 1) {
		printf("Re-IPL type:   ccw\n");
		rc = strrd(devno, "/sys/firmware/reipl/ccw/device");
		if (rc != 0)
			exit(1);
		if (strlen(devno) > 0)
			printf("Device:        %s\n", devno);
		/*
		 * check if we can read the load parameter (Loadparm)
		 */
		rc = strrd(val, "/sys/firmware/reipl/ccw/loadparm");
		if (rc != -1)
			printf("Loadparm:      %s\n", val);
		else
			printf("Loadparm:      \n");
	}
	return 0;
}

/*
 * "main" function for the reipl related stuff
 *
 */
int reipl(int argc, char *argv[])
{

	int rc, lpval;
	char path[4096];

	lpval = 0;
	/* parse the command line options in getop.c */
	parse_options(argc, argv);
	/*
	 * in case we want to reipl from a ccw device
	 */
	if (use_ccw == 1) {
		if (action == ACT_NODE) {
			rc = get_ccw_dev(partition, device);
			if (rc != 0) {
				fprintf(stderr, "%s: Cannot find device for "
					"partition: %s\n", name, partition);
				exit(1);
			}
			rc = get_ccw_devno(device, devno);
			if (rc != 0)  {
				fprintf(stderr, "%s: Unable to lookup device"
					" number for device %s\n", name,
					device);
				exit(1);
			}
		}
		if (isccwdev(devno) != 0) {
			fprintf(stderr, "%s: Unable to find a valid ccw"
				" device number\n", name);
			exit(1);
		}
		rc = strwrt(devno, "/sys/firmware/reipl/ccw/device");
		if (rc != 0)
			fprintf(stderr, "%s: Failed to set ccw device "
				"number\n", name);
		rc = strwrt("ccw", "/sys/firmware/reipl/reipl_type");
		if (rc != 0)
			fprintf(stderr, "%s: Failed to set reipl type "
				"to ccw\n", name);
		printf("Settings changed to: %s\n", devno);
		printf("Re-IPL type:         ccw\n");
		if (strlen(loadparm) != 0) {
			rc = strwrt(loadparm,
				"/sys/firmware/reipl/ccw/loadparm");
		} else {
			rc = ustrwrt("\n",
				"/sys/firmware/reipl/ccw/loadparm");
		}
		if (rc != 0)
			fprintf(stderr, "%s: Failed to set "
				"loadparm\n", name);
		printf("Loadparm:            %s\n", loadparm);
	}
	/*
	 * in case we reipl from a scsi device
	 */
	if (use_ccw == 0) {
		/*
		 * detect the necessary settings based on the device file:
		 * device number, lun and wwpn
		 */
		if (action == ACT_NODE) {
			/* get the device from the partition */
			rc = get_fcp_dev(partition, device);
			if (rc != 0) {
				fprintf(stderr, "%s Cannot find device for"
					" partition: %s\n", name, partition);
				exit(1);
			}
			if (ispartition(device) != 0) {
				fprintf(stderr, "%s: %s is not a valid "
					"partition\n", name, partition);
				exit(1);
			}
			if (verbose)
				printf("device: found %s\n", device);
			sprintf(path, "/sys/block/%s/device", device);
			if (verbose)
				printf("path is %s\n", path);
			if (chdir(path) != 0) {
				fprintf(stderr, "%s: Cannot find required"
					" data related to device %s\n",
					name, partition);
				exit(1);
			}
			rc = get_wwpn(device, wwpn);
			if (rc != 0) {
				fprintf(stderr, "%s: Failed to lookup "
					"WWPN\n", name);
				exit(1);
			}
			rc = get_lun(device, lun);
			if (rc != 0) {
				fprintf(stderr, "%s: Failed to lookup "
					"LUN\n", name);
				exit(1);
			}
			rc = get_fcp_devno(device, devno);
			if (rc != 0) {
				fprintf(stderr, "%s: Cannot find device for "
					"partition: %s\n", name, partition);
				exit(1);
			}
			if (verbose)
				printf("Device: %s\n", devno);
		}
		if (isfcpdev(devno) != 0) {
			fprintf(stderr, "%s: %s is not a valid fcp device"
				" number.\n", name, devno);
			exit(1);
		}
		rc = strwrt(devno, "/sys/firmware/reipl/fcp/device");
		if (rc != 0) {
			fprintf(stderr, "%s: Failed to set fcp device "
				"number\n", name);
			exit(1);
		}
		rc = strwrt(wwpn, "/sys/firmware/reipl/fcp/wwpn");
		if (rc != 0) {
			fprintf(stderr, "%s: Failed to set WWPN\n", name);
			exit(1);
		}
		rc = strwrt(lun, "/sys/firmware/reipl/fcp/lun");
		if (rc != 0) {
			fprintf(stderr, "%s: Failed to set fcp LUN\n", name);
			exit(1);
		}
		rc = strwrt("fcp", "/sys/firmware/reipl/reipl_type");
		if (rc != 0) {
			fprintf(stderr, "%s: Failed to set reipl type to"
				" fcp\n", name);
			exit(1);
		}
		/*
		 * set the boot record logical block address. Master boot
		 * record. It is always 0 for Linux
		 */
		rc = intwrt(0, "/sys/firmware/reipl/fcp/br_lba");
		if (rc != 0) {
			fprintf(stderr, "%s: Failed to set boot record "
				"logical address to 0\n", name);
			exit(1);
		}
		rc = intwrt(bootprog,  "/sys/firmware/reipl/fcp/bootprog");
		if (rc != 0) {
			fprintf(stderr, "%s: Failed to set "
				"bootprog\n", name);
			exit(1);
		}
		printf("Settings changed to: %s\n", devno);
		printf("WWPN:                %s\n", wwpn);
		printf("LUN:                 %s\n", lun);
		if (bootprog != 0)
			printf("Bootprog:            %d\n", bootprog);
		printf("Re-IPL type:         fcp\n");
	}
	return 0;
}
/* "main" function for list shutdown actions */
int lsshut(int argc, char *argv[])
{
	int rc;
	char tmp[1024];
	char cmd[1024];

	parse_lsshut_options(argc, argv);
	printf("Trigger          Action\n");
	printf("========================\n");
	if (access("/sys/firmware/shutdown_actions/on_halt", R_OK) == 0) {
		rc = get_sa(tmp, "on_halt");
		if (rc != -1) {
			if (strncmp(tmp, "vmcmd", strlen("vmcmd")) == 0) {
				rc = strrd(cmd, "/sys/firmware/vmcmd/on_halt");
				if (rc != 0)
					exit(1);
				printf("Halt             %s (\"%s\")\n", tmp,
						cmd);
			} else
				printf("Halt             %s\n", tmp);
		}

	}
	if (access("/sys/firmware/shutdown_actions/on_panic", R_OK) == 0) {
		rc = get_sa(tmp, "on_panic");
		if (rc != -1) {
			if (strncmp(tmp, "vmcmd", strlen("vmcmd")) == 0) {
				rc = strrd(cmd, "/sys/firmware/vmcmd/on_panic");
				if (rc != 0)
					exit(1);
				printf("Panic            %s (\"%s\")\n", tmp,
						cmd);
			} else
				printf("Panic            %s\n", tmp);
		}
	}
	if (access("/sys/firmware/shutdown_actions/on_poff", R_OK) == 0) {
		rc = get_sa(tmp, "on_poff");
		if (rc != -1) {
			if (strncmp(tmp, "vmcmd", strlen("vmcmd")) == 0) {
				rc = strrd(cmd, "/sys/firmware/vmcmd/on_poff");
				if (rc != 0)
					exit(1);
				printf("Power off        %s (\"%s\")\n", tmp,
						cmd);
			} else
				printf("Power off        %s\n", tmp);
		}
	}
	if (access("/sys/firmware/shutdown_actions/on_reboot", R_OK) == 0) {
		rc = get_sa(tmp, "on_reboot");
		if (rc != -1) {
			if (strncmp(tmp, "vmcmd", strlen("vmcmd")) == 0) {
				rc = strrd(cmd, "/sys/firmware/vmcmd/"
					"on_reboot");
				if (rc != 0)
					exit(1);
				printf("Reboot           %s (\"%s\")\n", tmp,
						cmd);
			} else
				printf("Reboot           %s\n", tmp);
		}
	}
	return 0;
}

/* "main" function for shutdown actions */
int chshut(int argc, char *argv[])
{
	parse_shutdown_options(argc, argv);
	return 0;
}

int main(int argc, char *argv[])
{

	strcpy(name, argv[0]);
	if (check_for_root() != 0) {
		fprintf(stderr, "%s: You must be root to perform this "
			"operation\n", name);
		exit(1);
	}
	/* lets see how we are called */
	if (strstr(argv[0], "chreipl") != NULL) {
		reipl(argc, argv);
		return 0;
	}
	if (strstr(argv[0], "chshut") != NULL) {
		chshut(argc, argv);
		return 0;
	}
	if (strstr(argv[0], "lsreipl") != NULL) {
		lsreipl(argc, argv);
		return 0;
	}
	if (strstr(argv[0], "lsshut") != NULL) {
		lsshut(argc, argv);
		return 0;
	}
	fprintf(stderr, "%s: Dont know how we are called.\n", name);
	return 1;
}