update to 1.8.4
[s390-tools.git] / ipl_tools / system.c
blobca6c5affc9f0f8e1aa166ce1685f1f8910817188
1 /*
2 * Copyright IBM Corp 2008
3 * Author: Hans-Joachim Picht <hans@linux.vnet.ibm.com>
5 * Linux for System z shutdown actions
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include <ctype.h>
18 #include <getopt.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 #include <signal.h>
29 #include <syslog.h>
30 #include <pthread.h>
31 #include "chreipl.h"
33 /* check if we are running in an LPAR environment.
34 * this functions return 0 if we run inside an lpar and -1 otherwise
36 int islpar(void)
38 int rc;
39 FILE *filp;
40 size_t bytes_read;
41 char buffer[2048];
42 char *contains_vm;
44 rc = -1;
45 filp = fopen("/proc/cpuinfo", "r");
46 if (!filp) {
47 fprintf(stderr, "%s: Can not open /proc/cpuinfo: %s \n"
48 , name, strerror(errno));
49 return rc;
51 bytes_read = fread(buffer, 1, sizeof(buffer), filp);
52 if (bytes_read == 0) {
53 fprintf(stderr, "%s: Reading /proc/cpuinfo failed:", name);
54 fprintf(stderr, "%s\n", strerror(errno));
55 goto out;
57 buffer[bytes_read] = '\0';
58 contains_vm = strstr(buffer, "version = FF");
59 if (contains_vm == NULL) {
60 rc = 0;
61 if (verbose)
62 printf("Detected System running in LPAR mode\n");
63 goto out;
64 } else {
65 if (verbose)
66 printf("Detected System running in z/VM mode\n");
68 out:
69 fclose(filp);
70 return rc;
73 /* get a substring of a given source with fixed start, end and length */
74 char *substring(size_t start, size_t stop, const char *src, char *dst,
75 size_t size)
77 unsigned int count;
79 count = stop - start;
80 if (count >= --size)
81 count = size;
82 sprintf(dst, "%.*s", count, src + start);
83 return dst;
87 * check whether we are started as root.
88 * Return 0 if user is root, non-zero otherwise.
90 int check_for_root(void)
92 if (geteuid() != 0)
93 return -1;
94 else
95 return 0;
99 * check if a given string is composed of hex numbers soley.
100 * return 0 if true and -1 otherwise
102 int ishex(char *cp)
105 unsigned int i;
106 char c;
108 for (i = 0; i <= strlen(cp); i++) {
109 c = cp[i];
110 if (isxdigit(cp[i]))
111 continue;
112 else
113 return -1;
115 return 0;
118 /* convert a string to lower case */
119 void strlow(char *s)
121 while (*s) {
122 *s = tolower(*s);
123 s++;
128 * write a string to a particular file
130 int strwrt(char *string, char *file)
132 FILE *filp;
133 char path[4096];
134 int rc;
136 strncpy(path, file, sizeof(path));
137 if (access(path, W_OK) == 0) {
138 filp = fopen(path, "w");
139 if (!filp) {
140 fprintf(stderr, "%s: Can not open %s :", name, path);
141 fprintf(stderr, "%s\n", strerror(errno));
142 exit(1);
144 rc = fputs(string, filp);
145 fclose(filp);
146 if (rc < 0) {
147 fprintf(stderr, "%s: Failed to write %s "
148 "into %s:", name, string, path);
149 fprintf(stderr, "%s\n", strerror(errno));
150 return -1;
151 } else
152 return 0;
153 } else {
154 fprintf(stderr, "%s: Can not open %s :", name, path);
155 fprintf(stderr, "%s\n", strerror(errno));
157 return -1;
161 * read a string from a particular file
163 int strrd(char *string, char *file)
165 FILE *filp;
166 char path[4096];
167 int rc;
169 strncpy(path, file, sizeof(path));
170 if (access(path, R_OK) == 0) {
171 filp = fopen(path, "rb");
172 if (!filp) {
173 fprintf(stderr, "%s: Can not open %s: ", name, file);
174 fprintf(stderr, "%s\n", strerror(errno));
175 exit(1);
177 rc = fread(string, 1, 4096, filp);
178 fclose(filp);
180 * special handling is required for
181 * /sys/firmware/reipl/ccw/loadparm
182 * since this file can be empty
184 if (rc < 0 && strncmp(file,
185 "/sys/firmware/reipl/ccw/loadparm",
186 strlen(file)) == 0) {
187 string[0] = '\0';
188 return 0;
190 if (rc < 0) {
191 fprintf(stderr, "%s: Failed to read from %s:", name,
192 file);
193 fprintf(stderr, "%s\n", strerror(errno));
194 return -1;
195 } else {
196 string[rc] = 0;
197 if (string[strlen(string) - 1] == '\n')
198 string[strlen(string) - 1] = 0;
199 return 0;
201 } else {
202 fprintf(stderr, "%s: Can not open %s: ", name, file);
203 fprintf(stderr, "%s\n", strerror(errno));
205 return -1;
209 * read an integer from a particular file
211 int intrd(int *val, char *file)
213 FILE *filp;
214 char path[4096];
215 int rc;
217 strncpy(path, file, sizeof(path));
218 if (access(path, R_OK) == 0) {
219 filp = fopen(path, "r");
220 if (!filp) {
221 fprintf(stderr, "%s: Can not open %s: ", name, file);
222 fprintf(stderr, "%s\n", strerror(errno));
223 exit(1);
225 rc = fscanf(filp, "%d", val);
226 fclose(filp);
227 if (rc < 0) {
229 * supress error messages when called via lsreipl
232 if (strncmp(file, "/sys/firmware/reipl/ccw/loadparm",
233 strlen(file)) >= 0)
234 return -1;
235 fprintf(stderr, "%s: Failed to read from %s: ", name,
236 file);
237 fprintf(stderr, "%s\n", strerror(errno));
238 return -1;
239 } else
240 return 0;
241 } else {
242 fprintf(stderr, "%s: Can not open %s: ", name, file);
243 fprintf(stderr, "%s\n", strerror(errno));
245 return -1;
249 * write an integer to a particular file
251 int intwrt(int val, char *file)
253 FILE *filp;
254 char path[4096];
255 int rc;
257 strncpy(path, file, sizeof(path));
258 if (access(path, W_OK) == 0) {
259 filp = fopen(path, "w");
260 if (!filp) {
261 fprintf(stderr, "%s: Can not open %s :", name, path);
262 fprintf(stderr, "%s\n", strerror(errno));
263 exit(1);
265 rc = fprintf(filp, "%d\n", val);
266 fclose(filp);
267 if (rc < 0) {
268 fprintf(stderr, "%s: Failed to write %d into %s: ",
269 name, val, path);
270 fprintf(stderr, "%s\n", strerror(errno));
271 return -1;
272 } else
273 return 0;
274 } else {
275 fprintf(stderr, "%s: Can not open %s :", name, path);
276 fprintf(stderr, "%s\n", strerror(errno));
278 return -1;
282 * write an unformated string to a particular file. this is needed to specify
283 * multiple vmcmd commands
285 int ustrwrt(char *string, char *file)
287 char path[4096];
288 int rc, fd;
290 strncpy(path, file, sizeof(path));
291 if (access(path, W_OK) == 0) {
292 fd = open(file, O_WRONLY);
293 if (!fd) {
294 fprintf(stderr, "%s: Can not open %s : ", name, path);
295 fprintf(stderr, "%s\n", strerror(errno));
296 exit(1);
298 rc = write(fd, string, strlen(string));
299 close(fd);
300 if (rc < 0) {
301 fprintf(stderr, "%s: Failed to write %s into %s: ",
302 name, string, path);
303 fprintf(stderr, "%s\n", strerror(errno));
304 return -1;
305 } else
306 return 0;
307 } else {
308 fprintf(stderr, "%s: Can not open %s : ", name, path);
309 fprintf(stderr, "%s\n", strerror(errno));
311 return -1;