17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*34e48580Sdp * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/procset.h>
317c478bd9Sstevel@tonic-gate #include <sys/processor.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate #include <time.h>
397c478bd9Sstevel@tonic-gate #include <utmpx.h>
40*34e48580Sdp #include <assert.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate static char *cmdname; /* command name for messages */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate static char verbose; /* non-zero if the -v option has been given */
457c478bd9Sstevel@tonic-gate static char all_flag; /* non-zero if the -a option has been given */
467c478bd9Sstevel@tonic-gate static char force; /* non-zero if the -F option has been given */
477c478bd9Sstevel@tonic-gate static char log_open; /* non-zero if openlog() has been called */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static struct utmpx ut; /* structure for logging to /etc/wtmpx. */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static char *basename(char *);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate static void
usage(void)547c478bd9Sstevel@tonic-gate usage(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
577c478bd9Sstevel@tonic-gate "usage: \n\t%s [-F] -f|-n|-i|-s [-v] processor_id ...\n"
587c478bd9Sstevel@tonic-gate "\t%s -a -f|-n|-i [-v]\n", cmdname, cmdname);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * Find base name of filename.
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate static char *
basename(char * cp)657c478bd9Sstevel@tonic-gate basename(char *cp)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate char *sp;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate if ((sp = strrchr(cp, '/')) != NULL)
707c478bd9Sstevel@tonic-gate return (sp + 1);
717c478bd9Sstevel@tonic-gate return (cp);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate typedef struct _psr_action {
757c478bd9Sstevel@tonic-gate int p_op;
767c478bd9Sstevel@tonic-gate char *p_state;
777c478bd9Sstevel@tonic-gate char *p_action;
787c478bd9Sstevel@tonic-gate char *p_wtmp;
797c478bd9Sstevel@tonic-gate } psr_action_t;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static psr_action_t psr_action[] = {
827c478bd9Sstevel@tonic-gate { P_ONLINE, "on-line", "brought", "on" },
837c478bd9Sstevel@tonic-gate { P_OFFLINE, "off-line", "taken", "off" },
847c478bd9Sstevel@tonic-gate { P_NOINTR, "no-intr", "set to", "ni" },
857c478bd9Sstevel@tonic-gate { P_SPARE, "spare", "marked", "spr" },
867c478bd9Sstevel@tonic-gate { P_FAULTED, "faulted", "marked", "flt" },
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static int psr_actions = sizeof (psr_action) / sizeof (psr_action_t);
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate static psr_action_t *
psr_action_lookup(int action)927c478bd9Sstevel@tonic-gate psr_action_lookup(int action)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate int i;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate for (i = 0; i < psr_actions; ++i) {
977c478bd9Sstevel@tonic-gate if (psr_action[i].p_op == action) {
987c478bd9Sstevel@tonic-gate return (&psr_action[i]);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate }
101*34e48580Sdp return (NULL);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * Set processor state.
1067c478bd9Sstevel@tonic-gate * Return non-zero if a processor was found.
1077c478bd9Sstevel@tonic-gate * Print messages and update wtmp and the system log.
1087c478bd9Sstevel@tonic-gate * If mustexist is set, it is an error if a processor isn't there.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate static int
psr_set_state(processorid_t cpu,int action,psr_action_t * pac,int mustexist)1127c478bd9Sstevel@tonic-gate psr_set_state(processorid_t cpu, int action, psr_action_t *pac, int mustexist)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate int old_state;
1157c478bd9Sstevel@tonic-gate int err;
1167c478bd9Sstevel@tonic-gate time_t now;
1177c478bd9Sstevel@tonic-gate char buf[80];
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate old_state = p_online(cpu, P_STATUS);
1207c478bd9Sstevel@tonic-gate if (old_state < 0) {
1217c478bd9Sstevel@tonic-gate if (errno == EINVAL && !mustexist)
1227c478bd9Sstevel@tonic-gate return (0); /* no such processor */
1237c478bd9Sstevel@tonic-gate err = errno; /* in case sprintf smashes errno */
1247c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: processor %d",
1257c478bd9Sstevel@tonic-gate cmdname, cpu);
1267c478bd9Sstevel@tonic-gate errno = err;
1277c478bd9Sstevel@tonic-gate perror(buf);
1287c478bd9Sstevel@tonic-gate return (-1);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (old_state == P_FAULTED && action != P_FAULTED && !force) {
1327c478bd9Sstevel@tonic-gate (void) printf("%s: processor %d in faulted state; "
1337c478bd9Sstevel@tonic-gate "add -F option to force change\n", cmdname, cpu);
1347c478bd9Sstevel@tonic-gate return (-1);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate old_state = p_online(cpu, force ? action | P_FORCED : action);
1387c478bd9Sstevel@tonic-gate if (old_state < 0) {
1397c478bd9Sstevel@tonic-gate if (errno == EINVAL && !mustexist)
1407c478bd9Sstevel@tonic-gate return (0); /* no such processor */
1417c478bd9Sstevel@tonic-gate err = errno;
1427c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s: processor %d",
1437c478bd9Sstevel@tonic-gate cmdname, cpu);
1447c478bd9Sstevel@tonic-gate errno = err;
1457c478bd9Sstevel@tonic-gate perror(buf);
1467c478bd9Sstevel@tonic-gate return (-1);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate if (old_state == action) {
1497c478bd9Sstevel@tonic-gate if (verbose)
1507c478bd9Sstevel@tonic-gate (void) printf("processor %d already %s.\n", cpu,
1517c478bd9Sstevel@tonic-gate pac->p_state);
1527c478bd9Sstevel@tonic-gate return (1); /* no change */
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "processor %d %s %s.",
1567c478bd9Sstevel@tonic-gate cpu, pac->p_action, pac->p_state);
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate if (verbose)
1597c478bd9Sstevel@tonic-gate (void) printf("%s\n", buf);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate * Log the change.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate if (!log_open) {
1657c478bd9Sstevel@tonic-gate log_open = 1;
1667c478bd9Sstevel@tonic-gate openlog(cmdname, LOG_CONS, LOG_USER); /* open syslog */
1677c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_INFO));
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate ut.ut_pid = getpid();
1707c478bd9Sstevel@tonic-gate ut.ut_type = USER_PROCESS;
1717c478bd9Sstevel@tonic-gate (void) strncpy(ut.ut_user, "psradm", sizeof (ut.ut_user) - 1);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "%s", buf);
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * Update wtmp.
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate (void) snprintf(ut.ut_line, sizeof (ut.ut_line), PSRADM_MSG,
1807c478bd9Sstevel@tonic-gate cpu, pac->p_wtmp);
1817c478bd9Sstevel@tonic-gate (void) time(&now);
1827c478bd9Sstevel@tonic-gate ut.ut_xtime = now;
1837c478bd9Sstevel@tonic-gate updwtmpx(WTMPX_FILE, &ut);
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate return (1); /* the processor exists and no errors occurred */
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate static int
do_range(processorid_t first,processorid_t last,int action,psr_action_t * pac)1897c478bd9Sstevel@tonic-gate do_range(processorid_t first, processorid_t last, int action,
1907c478bd9Sstevel@tonic-gate psr_action_t *pac)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate processorid_t cpu;
1937c478bd9Sstevel@tonic-gate int error = 0;
1947c478bd9Sstevel@tonic-gate int rv;
1957c478bd9Sstevel@tonic-gate int found_one = 0;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate for (cpu = first; cpu <= last; cpu++) {
1987c478bd9Sstevel@tonic-gate if ((rv = psr_set_state(cpu, action, pac, 0)) > 0)
1997c478bd9Sstevel@tonic-gate found_one = 1;
2007c478bd9Sstevel@tonic-gate else if (rv < 0)
2017c478bd9Sstevel@tonic-gate error = 1;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate if (!found_one && error == 0) {
2047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: no processors in range %d-%d\n",
2057c478bd9Sstevel@tonic-gate cmdname, first, last);
2067c478bd9Sstevel@tonic-gate error = 1;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate return (error);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2127c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate int c;
2157c478bd9Sstevel@tonic-gate int action = 0;
2167c478bd9Sstevel@tonic-gate processorid_t cpu;
2177c478bd9Sstevel@tonic-gate processorid_t cpuid_max;
2187c478bd9Sstevel@tonic-gate char *errptr;
2197c478bd9Sstevel@tonic-gate int errors;
2207c478bd9Sstevel@tonic-gate psr_action_t *pac;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate cmdname = basename(argv[0]);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "afFinsv")) != EOF) {
2257c478bd9Sstevel@tonic-gate switch (c) {
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate case 'a': /* applies to all possible CPUs */
2287c478bd9Sstevel@tonic-gate all_flag = 1;
2297c478bd9Sstevel@tonic-gate break;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate case 'F':
2327c478bd9Sstevel@tonic-gate force = 1;
2337c478bd9Sstevel@tonic-gate break;
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate case 'f':
2367c478bd9Sstevel@tonic-gate case 'i':
2377c478bd9Sstevel@tonic-gate case 'n':
2387c478bd9Sstevel@tonic-gate case 's':
2397c478bd9Sstevel@tonic-gate if (action != 0 && action != c) {
2407c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2417c478bd9Sstevel@tonic-gate "%s: options -f, -n, -i, and -s are "
2427c478bd9Sstevel@tonic-gate "mutually exclusive.\n", cmdname);
2437c478bd9Sstevel@tonic-gate usage();
2447c478bd9Sstevel@tonic-gate return (2);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate action = c;
2477c478bd9Sstevel@tonic-gate break;
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate case 'v':
2507c478bd9Sstevel@tonic-gate verbose = 1;
2517c478bd9Sstevel@tonic-gate break;
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate default:
2547c478bd9Sstevel@tonic-gate usage();
2557c478bd9Sstevel@tonic-gate return (2);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate switch (action) {
2607c478bd9Sstevel@tonic-gate case 'f':
2617c478bd9Sstevel@tonic-gate action = P_OFFLINE;
2627c478bd9Sstevel@tonic-gate break;
2637c478bd9Sstevel@tonic-gate case 'i':
2647c478bd9Sstevel@tonic-gate action = P_NOINTR;
2657c478bd9Sstevel@tonic-gate break;
2667c478bd9Sstevel@tonic-gate case 'n':
2677c478bd9Sstevel@tonic-gate action = P_ONLINE;
2687c478bd9Sstevel@tonic-gate break;
2697c478bd9Sstevel@tonic-gate case 's':
2707c478bd9Sstevel@tonic-gate action = P_SPARE;
2717c478bd9Sstevel@tonic-gate break;
2727c478bd9Sstevel@tonic-gate default:
2737c478bd9Sstevel@tonic-gate if (force != 0) {
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * The -F option without other transition options
2767c478bd9Sstevel@tonic-gate * puts processor(s) into faulted state.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate action = P_FAULTED;
2797c478bd9Sstevel@tonic-gate break;
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2827c478bd9Sstevel@tonic-gate "%s: option -f, -n, -s or -i must "
2837c478bd9Sstevel@tonic-gate "be specified.\n", cmdname);
2847c478bd9Sstevel@tonic-gate usage();
2857c478bd9Sstevel@tonic-gate return (2);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate pac = psr_action_lookup(action);
289*34e48580Sdp assert(pac != NULL);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate errors = 0;
2927c478bd9Sstevel@tonic-gate if (all_flag) {
2937c478bd9Sstevel@tonic-gate if (argc != optind) {
2947c478bd9Sstevel@tonic-gate usage();
2957c478bd9Sstevel@tonic-gate return (2);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate cpuid_max = (processorid_t)sysconf(_SC_CPUID_MAX);
2987c478bd9Sstevel@tonic-gate for (cpu = 0; cpu <= cpuid_max; cpu++) {
2997c478bd9Sstevel@tonic-gate if (psr_set_state(cpu, action, pac, 0) < 0)
3007c478bd9Sstevel@tonic-gate errors = 1;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate } else {
3037c478bd9Sstevel@tonic-gate argc -= optind;
3047c478bd9Sstevel@tonic-gate if (argc <= 0) {
3057c478bd9Sstevel@tonic-gate usage(); /* not enough arguments */
3067c478bd9Sstevel@tonic-gate return (2);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate for (argv += optind; argc > 0; argv++, argc--) {
3097c478bd9Sstevel@tonic-gate if (strchr(*argv, '-') == NULL) {
3107c478bd9Sstevel@tonic-gate /* individual processor id */
3117c478bd9Sstevel@tonic-gate cpu = (processorid_t)
3127c478bd9Sstevel@tonic-gate strtol(*argv, &errptr, 10);
3137c478bd9Sstevel@tonic-gate if (errptr != NULL && *errptr != '\0') {
3147c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3157c478bd9Sstevel@tonic-gate "%s: invalid processor"
3167c478bd9Sstevel@tonic-gate " ID %s\n", cmdname, *argv);
3177c478bd9Sstevel@tonic-gate errors = 2;
3187c478bd9Sstevel@tonic-gate continue;
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate if (psr_set_state(cpu, action, pac, 1) < 0)
3217c478bd9Sstevel@tonic-gate errors = 1;
3227c478bd9Sstevel@tonic-gate } else {
3237c478bd9Sstevel@tonic-gate /* range of processors */
3247c478bd9Sstevel@tonic-gate processorid_t first, last;
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate first = (processorid_t)
3277c478bd9Sstevel@tonic-gate strtol(*argv, &errptr, 10);
3287c478bd9Sstevel@tonic-gate if (*errptr++ != '-') {
3297c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3307c478bd9Sstevel@tonic-gate "%s: invalid processor"
3317c478bd9Sstevel@tonic-gate " range %s\n", cmdname, *argv);
3327c478bd9Sstevel@tonic-gate errors = 2;
3337c478bd9Sstevel@tonic-gate continue;
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate last = (processorid_t)
3367c478bd9Sstevel@tonic-gate strtol(errptr, &errptr, 10);
3377c478bd9Sstevel@tonic-gate if ((errptr != NULL && *errptr != '\0') ||
3387c478bd9Sstevel@tonic-gate last < first || first < 0) {
3397c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3407c478bd9Sstevel@tonic-gate "%s: invalid processor"
3417c478bd9Sstevel@tonic-gate " range %s\n", cmdname, *argv);
3427c478bd9Sstevel@tonic-gate errors = 2;
3437c478bd9Sstevel@tonic-gate continue;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate if (do_range(first, last, action, pac))
3467c478bd9Sstevel@tonic-gate errors = 1;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate if (log_open) {
3517c478bd9Sstevel@tonic-gate closelog();
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate return (errors);
3547c478bd9Sstevel@tonic-gate }
355