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)
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
27 #include <sys/types.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
45 filp
= fopen("/proc/cpuinfo", "r");
47 fprintf(stderr
, "%s: Can not open /proc/cpuinfo: %s \n"
48 , name
, strerror(errno
));
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
));
57 buffer
[bytes_read
] = '\0';
58 contains_vm
= strstr(buffer
, "version = FF");
59 if (contains_vm
== NULL
) {
62 printf("Detected System running in LPAR mode\n");
66 printf("Detected System running in z/VM mode\n");
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
,
82 sprintf(dst
, "%.*s", count
, src
+ start
);
87 * check whether we are started as root.
88 * Return 0 if user is root, non-zero otherwise.
90 int check_for_root(void)
99 * check if a given string is composed of hex numbers soley.
100 * return 0 if true and -1 otherwise
108 for (i
= 0; i
<= strlen(cp
); i
++) {
118 /* convert a string to lower case */
128 * write a string to a particular file
130 int strwrt(char *string
, char *file
)
136 strncpy(path
, file
, sizeof(path
));
137 if (access(path
, W_OK
) == 0) {
138 filp
= fopen(path
, "w");
140 fprintf(stderr
, "%s: Can not open %s :", name
, path
);
141 fprintf(stderr
, "%s\n", strerror(errno
));
144 rc
= fputs(string
, filp
);
147 fprintf(stderr
, "%s: Failed to write %s "
148 "into %s:", name
, string
, path
);
149 fprintf(stderr
, "%s\n", strerror(errno
));
154 fprintf(stderr
, "%s: Can not open %s :", name
, path
);
155 fprintf(stderr
, "%s\n", strerror(errno
));
161 * read a string from a particular file
163 int strrd(char *string
, char *file
)
169 strncpy(path
, file
, sizeof(path
));
170 if (access(path
, R_OK
) == 0) {
171 filp
= fopen(path
, "rb");
173 fprintf(stderr
, "%s: Can not open %s: ", name
, file
);
174 fprintf(stderr
, "%s\n", strerror(errno
));
177 rc
= fread(string
, 1, 4096, 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) {
191 fprintf(stderr
, "%s: Failed to read from %s:", name
,
193 fprintf(stderr
, "%s\n", strerror(errno
));
197 if (string
[strlen(string
) - 1] == '\n')
198 string
[strlen(string
) - 1] = 0;
202 fprintf(stderr
, "%s: Can not open %s: ", name
, file
);
203 fprintf(stderr
, "%s\n", strerror(errno
));
209 * read an integer from a particular file
211 int intrd(int *val
, char *file
)
217 strncpy(path
, file
, sizeof(path
));
218 if (access(path
, R_OK
) == 0) {
219 filp
= fopen(path
, "r");
221 fprintf(stderr
, "%s: Can not open %s: ", name
, file
);
222 fprintf(stderr
, "%s\n", strerror(errno
));
225 rc
= fscanf(filp
, "%d", val
);
229 * supress error messages when called via lsreipl
232 if (strncmp(file
, "/sys/firmware/reipl/ccw/loadparm",
235 fprintf(stderr
, "%s: Failed to read from %s: ", name
,
237 fprintf(stderr
, "%s\n", strerror(errno
));
242 fprintf(stderr
, "%s: Can not open %s: ", name
, file
);
243 fprintf(stderr
, "%s\n", strerror(errno
));
249 * write an integer to a particular file
251 int intwrt(int val
, char *file
)
257 strncpy(path
, file
, sizeof(path
));
258 if (access(path
, W_OK
) == 0) {
259 filp
= fopen(path
, "w");
261 fprintf(stderr
, "%s: Can not open %s :", name
, path
);
262 fprintf(stderr
, "%s\n", strerror(errno
));
265 rc
= fprintf(filp
, "%d\n", val
);
268 fprintf(stderr
, "%s: Failed to write %d into %s: ",
270 fprintf(stderr
, "%s\n", strerror(errno
));
275 fprintf(stderr
, "%s: Can not open %s :", name
, path
);
276 fprintf(stderr
, "%s\n", strerror(errno
));
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
)
290 strncpy(path
, file
, sizeof(path
));
291 if (access(path
, W_OK
) == 0) {
292 fd
= open(file
, O_WRONLY
);
294 fprintf(stderr
, "%s: Can not open %s : ", name
, path
);
295 fprintf(stderr
, "%s\n", strerror(errno
));
298 rc
= write(fd
, string
, strlen(string
));
301 fprintf(stderr
, "%s: Failed to write %s into %s: ",
303 fprintf(stderr
, "%s\n", strerror(errno
));
308 fprintf(stderr
, "%s: Can not open %s : ", name
, path
);
309 fprintf(stderr
, "%s\n", strerror(errno
));