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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <time.h>
#include <utmpx.h>
#include <assert.h>
static char *cmdname; /* command name for messages */
static char verbose; /* non-zero if the -v option has been given */
static char all_flag; /* non-zero if the -a option has been given */
static char force; /* non-zero if the -F option has been given */
static char log_open; /* non-zero if openlog() has been called */
static struct utmpx ut; /* structure for logging to /etc/wtmpx. */
static char *basename(char *);
static void
usage(void)
{
(void) fprintf(stderr,
"usage: \n\t%s [-F] -f|-n|-i|-s [-v] processor_id ...\n"
"\t%s -a -f|-n|-i [-v]\n", cmdname, cmdname);
}
/*
* Find base name of filename.
*/
static char *
basename(char *cp)
{
char *sp;
if ((sp = strrchr(cp, '/')) != NULL)
return (sp + 1);
return (cp);
}
typedef struct _psr_action {
int p_op;
char *p_state;
char *p_action;
char *p_wtmp;
} psr_action_t;
static psr_action_t psr_action[] = {
{ P_ONLINE, "on-line", "brought", "on" },
{ P_OFFLINE, "off-line", "taken", "off" },
{ P_NOINTR, "no-intr", "set to", "ni" },
{ P_SPARE, "spare", "marked", "spr" },
{ P_FAULTED, "faulted", "marked", "flt" },
};
static int psr_actions = sizeof (psr_action) / sizeof (psr_action_t);
static psr_action_t *
psr_action_lookup(int action)
{
int i;
for (i = 0; i < psr_actions; ++i) {
if (psr_action[i].p_op == action) {
return (&psr_action[i]);
}
}
return (NULL);
}
/*
* Set processor state.
* Return non-zero if a processor was found.
* Print messages and update wtmp and the system log.
* If mustexist is set, it is an error if a processor isn't there.
*/
static int
psr_set_state(processorid_t cpu, int action, psr_action_t *pac, int mustexist)
{
int old_state;
int err;
time_t now;
char buf[80];
old_state = p_online(cpu, P_STATUS);
if (old_state < 0) {
if (errno == EINVAL && !mustexist)
return (0); /* no such processor */
err = errno; /* in case sprintf smashes errno */
(void) snprintf(buf, sizeof (buf), "%s: processor %d",
cmdname, cpu);
errno = err;
perror(buf);
return (-1);
}
if (old_state == P_FAULTED && action != P_FAULTED && !force) {
(void) printf("%s: processor %d in faulted state; "
"add -F option to force change\n", cmdname, cpu);
return (-1);
}
old_state = p_online(cpu, force ? action | P_FORCED : action);
if (old_state < 0) {
if (errno == EINVAL && !mustexist)
return (0); /* no such processor */
err = errno;
(void) snprintf(buf, sizeof (buf), "%s: processor %d",
cmdname, cpu);
errno = err;
perror(buf);
return (-1);
}
if (old_state == action) {
if (verbose)
(void) printf("processor %d already %s.\n", cpu,
pac->p_state);
return (1); /* no change */
}
(void) snprintf(buf, sizeof (buf), "processor %d %s %s.",
cpu, pac->p_action, pac->p_state);
if (verbose)
(void) printf("%s\n", buf);
/*
* Log the change.
*/
if (!log_open) {
log_open = 1;
openlog(cmdname, LOG_CONS, LOG_USER); /* open syslog */
(void) setlogmask(LOG_UPTO(LOG_INFO));
ut.ut_pid = getpid();
ut.ut_type = USER_PROCESS;
(void) strncpy(ut.ut_user, "psradm", sizeof (ut.ut_user) - 1);
}
syslog(LOG_INFO, "%s", buf);
/*
* Update wtmp.
*/
(void) snprintf(ut.ut_line, sizeof (ut.ut_line), PSRADM_MSG,
cpu, pac->p_wtmp);
(void) time(&now);
ut.ut_xtime = now;
updwtmpx(WTMPX_FILE, &ut);
return (1); /* the processor exists and no errors occurred */
}
static int
do_range(processorid_t first, processorid_t last, int action,
psr_action_t *pac)
{
processorid_t cpu;
int error = 0;
int rv;
int found_one = 0;
for (cpu = first; cpu <= last; cpu++) {
if ((rv = psr_set_state(cpu, action, pac, 0)) > 0)
found_one = 1;
else if (rv < 0)
error = 1;
}
if (!found_one && error == 0) {
(void) fprintf(stderr, "%s: no processors in range %d-%d\n",
cmdname, first, last);
error = 1;
}
return (error);
}
int
main(int argc, char *argv[])
{
int c;
int action = 0;
processorid_t cpu;
processorid_t cpuid_max;
char *errptr;
int errors;
psr_action_t *pac;
cmdname = basename(argv[0]);
while ((c = getopt(argc, argv, "afFinsv")) != EOF) {
switch (c) {
case 'a': /* applies to all possible CPUs */
all_flag = 1;
break;
case 'F':
force = 1;
break;
case 'f':
case 'i':
case 'n':
case 's':
if (action != 0 && action != c) {
(void) fprintf(stderr,
"%s: options -f, -n, -i, and -s are "
"mutually exclusive.\n", cmdname);
usage();
return (2);
}
action = c;
break;
case 'v':
verbose = 1;
break;
default:
usage();
return (2);
}
}
switch (action) {
case 'f':
action = P_OFFLINE;
break;
case 'i':
action = P_NOINTR;
break;
case 'n':
action = P_ONLINE;
break;
case 's':
action = P_SPARE;
break;
default:
if (force != 0) {
/*
* The -F option without other transition options
* puts processor(s) into faulted state.
*/
action = P_FAULTED;
break;
}
(void) fprintf(stderr,
"%s: option -f, -n, -s or -i must "
"be specified.\n", cmdname);
usage();
return (2);
}
pac = psr_action_lookup(action);
assert(pac != NULL);
errors = 0;
if (all_flag) {
if (argc != optind) {
usage();
return (2);
}
cpuid_max = (processorid_t)sysconf(_SC_CPUID_MAX);
for (cpu = 0; cpu <= cpuid_max; cpu++) {
if (psr_set_state(cpu, action, pac, 0) < 0)
errors = 1;
}
} else {
argc -= optind;
if (argc <= 0) {
usage(); /* not enough arguments */
return (2);
}
for (argv += optind; argc > 0; argv++, argc--) {
if (strchr(*argv, '-') == NULL) {
/* individual processor id */
cpu = (processorid_t)
strtol(*argv, &errptr, 10);
if (errptr != NULL && *errptr != '\0') {
(void) fprintf(stderr,
"%s: invalid processor"
" ID %s\n", cmdname, *argv);
errors = 2;
continue;
}
if (psr_set_state(cpu, action, pac, 1) < 0)
errors = 1;
} else {
/* range of processors */
processorid_t first, last;
first = (processorid_t)
strtol(*argv, &errptr, 10);
if (*errptr++ != '-') {
(void) fprintf(stderr,
"%s: invalid processor"
" range %s\n", cmdname, *argv);
errors = 2;
continue;
}
last = (processorid_t)
strtol(errptr, &errptr, 10);
if ((errptr != NULL && *errptr != '\0') ||
last < first || first < 0) {
(void) fprintf(stderr,
"%s: invalid processor"
" range %s\n", cmdname, *argv);
errors = 2;
continue;
}
if (do_range(first, last, action, pac))
errors = 1;
}
}
}
if (log_open) {
closelog();
}
return (errors);
}
|