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
5342440ecSPrasad Singamsetty * Common Development and Distribution License (the "License").
6342440ecSPrasad Singamsetty * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22589271a4SSheng-Liang Eric Zhang * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate * This file contains miscellaneous routines.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate #include "global.h"
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <malloc.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
397c478bd9Sstevel@tonic-gate #include <sys/time.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include <termio.h>
427c478bd9Sstevel@tonic-gate #include "misc.h"
437c478bd9Sstevel@tonic-gate #include "analyze.h"
447c478bd9Sstevel@tonic-gate #include "label.h"
457c478bd9Sstevel@tonic-gate #include "startup.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
487c478bd9Sstevel@tonic-gate static void cleanup(int sig);
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate struct env *current_env = NULL; /* ptr to current environment */
517c478bd9Sstevel@tonic-gate static int stop_pending = 0; /* ctrl-Z is pending */
527c478bd9Sstevel@tonic-gate struct ttystate ttystate; /* tty info */
537c478bd9Sstevel@tonic-gate static int aborting = 0; /* in process of aborting */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate * For 4.x, limit the choices of valid disk names to this set.
577c478bd9Sstevel@tonic-gate */
587c478bd9Sstevel@tonic-gate static char *disk_4x_identifiers[] = { "sd", "id"};
597c478bd9Sstevel@tonic-gate #define N_DISK_4X_IDS (sizeof (disk_4x_identifiers)/sizeof (char *))
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate * This is the list of legal inputs for all yes/no questions.
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate char *confirm_list[] = {
667c478bd9Sstevel@tonic-gate "yes",
677c478bd9Sstevel@tonic-gate "no",
687c478bd9Sstevel@tonic-gate NULL,
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * This routine is a wrapper for malloc. It allocates pre-zeroed space,
737c478bd9Sstevel@tonic-gate * and checks the return value so the caller doesn't have to.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate void *
zalloc(int count)76*b12aaafbSToomas Soome zalloc(int count)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate void *ptr;
797c478bd9Sstevel@tonic-gate
80*b12aaafbSToomas Soome if ((ptr = calloc(1, (unsigned)count)) == NULL) {
817c478bd9Sstevel@tonic-gate err_print("Error: unable to calloc more space.\n");
827c478bd9Sstevel@tonic-gate fullabort();
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate return (ptr);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * This routine is a wrapper for realloc. It reallocates the given
897c478bd9Sstevel@tonic-gate * space, and checks the return value so the caller doesn't have to.
907c478bd9Sstevel@tonic-gate * Note that the any space added by this call is NOT necessarily
917c478bd9Sstevel@tonic-gate * zeroed.
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate void *
rezalloc(void * ptr,int count)94*b12aaafbSToomas Soome rezalloc(void *ptr, int count)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate void *new_ptr;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate
99*b12aaafbSToomas Soome if ((new_ptr = realloc((char *)ptr, (unsigned)count)) == NULL) {
1007c478bd9Sstevel@tonic-gate err_print("Error: unable to realloc more space.\n");
1017c478bd9Sstevel@tonic-gate fullabort();
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate return (new_ptr);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * This routine is a wrapper for free.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate void
destroy_data(char * data)110*b12aaafbSToomas Soome destroy_data(char *data)
1117c478bd9Sstevel@tonic-gate {
112*b12aaafbSToomas Soome free(data);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate #ifdef not
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * This routine takes the space number returned by an ioctl call and
1187c478bd9Sstevel@tonic-gate * returns a mnemonic name for that space.
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate char *
space2str(uint_t space)121*b12aaafbSToomas Soome space2str(uint_t space)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate char *name;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate switch (space&SP_BUSMASK) {
1267c478bd9Sstevel@tonic-gate case SP_VIRTUAL:
1277c478bd9Sstevel@tonic-gate name = "virtual";
1287c478bd9Sstevel@tonic-gate break;
1297c478bd9Sstevel@tonic-gate case SP_OBMEM:
1307c478bd9Sstevel@tonic-gate name = "obmem";
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate case SP_OBIO:
1337c478bd9Sstevel@tonic-gate name = "obio";
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate case SP_MBMEM:
1367c478bd9Sstevel@tonic-gate name = "mbmem";
1377c478bd9Sstevel@tonic-gate break;
1387c478bd9Sstevel@tonic-gate case SP_MBIO:
1397c478bd9Sstevel@tonic-gate name = "mbio";
1407c478bd9Sstevel@tonic-gate break;
1417c478bd9Sstevel@tonic-gate default:
1427c478bd9Sstevel@tonic-gate err_print("Error: unknown address space type encountered.\n");
1437c478bd9Sstevel@tonic-gate fullabort();
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate return (name);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate #endif /* not */
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * This routine asks the user the given yes/no question and returns
1517c478bd9Sstevel@tonic-gate * the response.
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate int
check(char * question)154*b12aaafbSToomas Soome check(char *question)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate int answer;
1577c478bd9Sstevel@tonic-gate u_ioparam_t ioparam;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate * If we are running out of a command file, assume a yes answer.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate if (option_f)
1637c478bd9Sstevel@tonic-gate return (0);
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * Ask the user.
1667c478bd9Sstevel@tonic-gate */
1677c478bd9Sstevel@tonic-gate ioparam.io_charlist = confirm_list;
168*b12aaafbSToomas Soome answer = input(FIO_MSTR, question, '?', &ioparam, NULL, DATA_INPUT);
1697c478bd9Sstevel@tonic-gate return (answer);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate * This routine aborts the current command. It is called by a ctrl-C
1747c478bd9Sstevel@tonic-gate * interrupt and also under certain error conditions.
1757c478bd9Sstevel@tonic-gate */
1767c478bd9Sstevel@tonic-gate void
cmdabort(int sig __unused)177*b12aaafbSToomas Soome cmdabort(int sig __unused)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate * If there is no usable saved environment, gracefully exit. This
1817c478bd9Sstevel@tonic-gate * allows the user to interrupt the program even when input is from
1827c478bd9Sstevel@tonic-gate * a file, or if there is no current menu, like at the "Select disk:"
1837c478bd9Sstevel@tonic-gate * prompt.
1847c478bd9Sstevel@tonic-gate */
1857c478bd9Sstevel@tonic-gate if (current_env == NULL || !(current_env->flags & ENV_USE))
1867c478bd9Sstevel@tonic-gate fullabort();
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * If we are in a critical zone, note the attempt and return.
1907c478bd9Sstevel@tonic-gate */
1917c478bd9Sstevel@tonic-gate if (current_env->flags & ENV_CRITICAL) {
1927c478bd9Sstevel@tonic-gate current_env->flags |= ENV_ABORT;
1937c478bd9Sstevel@tonic-gate return;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate * All interruptions when we are running out of a command file
1977c478bd9Sstevel@tonic-gate * cause the program to gracefully exit.
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate if (option_f)
2007c478bd9Sstevel@tonic-gate fullabort();
2017c478bd9Sstevel@tonic-gate fmt_print("\n");
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * Clean up any state left by the interrupted command.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate cleanup(sig);
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * Jump to the saved environment.
2087c478bd9Sstevel@tonic-gate */
2097c478bd9Sstevel@tonic-gate longjmp(current_env->env, 0);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * This routine implements the ctrl-Z suspend mechanism. It is called
2147c478bd9Sstevel@tonic-gate * when a suspend signal is received.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate void
onsusp(int sig __unused)217*b12aaafbSToomas Soome onsusp(int sig __unused)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate int fix_term;
2207c478bd9Sstevel@tonic-gate #ifdef NOT_DEF
2217c478bd9Sstevel@tonic-gate sigset_t sigmask;
2227c478bd9Sstevel@tonic-gate #endif /* NOT_DEF */
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * If we are in a critical zone, note the attempt and return.
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate if (current_env != NULL && current_env->flags & ENV_CRITICAL) {
2287c478bd9Sstevel@tonic-gate stop_pending = 1;
2297c478bd9Sstevel@tonic-gate return;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * If the terminal is mucked up, note that we will need to
2337c478bd9Sstevel@tonic-gate * re-muck it when we start up again.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate fix_term = ttystate.ttyflags;
2367c478bd9Sstevel@tonic-gate fmt_print("\n");
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate * Clean up any state left by the interrupted command.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate cleanup(sig);
2417c478bd9Sstevel@tonic-gate #ifdef NOT_DEF
2427c478bd9Sstevel@tonic-gate /* Investigate whether all this is necessary */
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate * Stop intercepting the suspend signal, then send ourselves one
2457c478bd9Sstevel@tonic-gate * to cause us to stop.
2467c478bd9Sstevel@tonic-gate */
2477c478bd9Sstevel@tonic-gate sigmask.sigbits[0] = (ulong_t)0xffffffff;
248*b12aaafbSToomas Soome if (sigprocmask(SIG_SETMASK, &sigmask, NULL) == -1)
2497c478bd9Sstevel@tonic-gate err_print("sigprocmask failed %d\n", errno);
2507c478bd9Sstevel@tonic-gate #endif /* NOT_DEF */
2517c478bd9Sstevel@tonic-gate (void) signal(SIGTSTP, SIG_DFL);
2527c478bd9Sstevel@tonic-gate (void) kill(0, SIGTSTP);
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate * PC stops here
2557c478bd9Sstevel@tonic-gate */
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate * We are started again. Set us up to intercept the suspend
2587c478bd9Sstevel@tonic-gate * signal once again.
2597c478bd9Sstevel@tonic-gate */
2607c478bd9Sstevel@tonic-gate (void) signal(SIGTSTP, onsusp);
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate * Re-muck the terminal if necessary.
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate if (fix_term & TTY_ECHO_OFF)
2657c478bd9Sstevel@tonic-gate echo_off();
2667c478bd9Sstevel@tonic-gate if (fix_term & TTY_CBREAK_ON)
2677c478bd9Sstevel@tonic-gate charmode_on();
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * This routine implements the timing function used during long-term
2727c478bd9Sstevel@tonic-gate * disk operations (e.g. formatting). It is called when an alarm signal
2737c478bd9Sstevel@tonic-gate * is received.
2747c478bd9Sstevel@tonic-gate */
2757c478bd9Sstevel@tonic-gate void
onalarm(int sig __unused)276*b12aaafbSToomas Soome onalarm(int sig __unused)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate * This routine gracefully exits the program.
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate void
fullabort(void)285*b12aaafbSToomas Soome fullabort(void)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate fmt_print("\n");
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate * Clean up any state left by an interrupted command.
2917c478bd9Sstevel@tonic-gate * Avoid infinite loops caused by a clean-up
2927c478bd9Sstevel@tonic-gate * routine failing again...
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate if (!aborting) {
2957c478bd9Sstevel@tonic-gate aborting = 1;
2967c478bd9Sstevel@tonic-gate cleanup(SIGKILL);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate exit(1);
2997c478bd9Sstevel@tonic-gate /*NOTREACHED*/
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate * This routine cleans up the state of the world. It is a hodge-podge
3047c478bd9Sstevel@tonic-gate * of kludges to allow us to interrupt commands whenever possible.
3057c478bd9Sstevel@tonic-gate *
3067c478bd9Sstevel@tonic-gate * Some cleanup actions may depend on the type of signal.
3077c478bd9Sstevel@tonic-gate */
3087c478bd9Sstevel@tonic-gate static void
cleanup(int sig)3097c478bd9Sstevel@tonic-gate cleanup(int sig)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * Lock out interrupts to avoid recursion.
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate enter_critical();
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate * Fix up the tty if necessary.
3187c478bd9Sstevel@tonic-gate */
3197c478bd9Sstevel@tonic-gate if (ttystate.ttyflags & TTY_CBREAK_ON) {
3207c478bd9Sstevel@tonic-gate charmode_off();
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate if (ttystate.ttyflags & TTY_ECHO_OFF) {
3237c478bd9Sstevel@tonic-gate echo_on();
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * If the defect list is dirty, write it out.
3287c478bd9Sstevel@tonic-gate */
3297c478bd9Sstevel@tonic-gate if (cur_list.flags & LIST_DIRTY) {
3307c478bd9Sstevel@tonic-gate cur_list.flags = 0;
3317c478bd9Sstevel@tonic-gate if (!EMBEDDED_SCSI)
3327c478bd9Sstevel@tonic-gate write_deflist(&cur_list);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate * If the label is dirty, write it out.
3367c478bd9Sstevel@tonic-gate */
3377c478bd9Sstevel@tonic-gate if (cur_flags & LABEL_DIRTY) {
3387c478bd9Sstevel@tonic-gate cur_flags &= ~LABEL_DIRTY;
3397c478bd9Sstevel@tonic-gate (void) write_label();
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * If we are logging and just interrupted a scan, print out
3437c478bd9Sstevel@tonic-gate * some summary info to the log file.
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate if (log_file && scan_cur_block >= 0) {
3467c478bd9Sstevel@tonic-gate pr_dblock(log_print, scan_cur_block);
3477c478bd9Sstevel@tonic-gate log_print("\n");
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate if (scan_blocks_fixed >= 0)
3507c478bd9Sstevel@tonic-gate fmt_print("Total of %lld defective blocks repaired.\n",
3517c478bd9Sstevel@tonic-gate scan_blocks_fixed);
3527c478bd9Sstevel@tonic-gate if (sig != SIGSTOP) { /* Don't reset on suspend (converted to stop) */
3537c478bd9Sstevel@tonic-gate scan_cur_block = scan_blocks_fixed = -1;
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate exit_critical();
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate * This routine causes the program to enter a critical zone. Within the
3607c478bd9Sstevel@tonic-gate * critical zone, no interrupts are allowed. Note that calls to this
3617c478bd9Sstevel@tonic-gate * routine for the same environment do NOT nest, so there is not
3627c478bd9Sstevel@tonic-gate * necessarily pairing between calls to enter_critical() and exit_critical().
3637c478bd9Sstevel@tonic-gate */
3647c478bd9Sstevel@tonic-gate void
enter_critical(void)365*b12aaafbSToomas Soome enter_critical(void)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate * If there is no saved environment, interrupts will be ignored.
3707c478bd9Sstevel@tonic-gate */
3717c478bd9Sstevel@tonic-gate if (current_env == NULL)
3727c478bd9Sstevel@tonic-gate return;
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate * Mark the environment to be in a critical zone.
3757c478bd9Sstevel@tonic-gate */
3767c478bd9Sstevel@tonic-gate current_env->flags |= ENV_CRITICAL;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate * This routine causes the program to exit a critical zone. Note that
3817c478bd9Sstevel@tonic-gate * calls to enter_critical() for the same environment do NOT nest, so
3827c478bd9Sstevel@tonic-gate * one call to exit_critical() will erase any number of such calls.
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate void
exit_critical(void)385*b12aaafbSToomas Soome exit_critical(void)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate * If there is a saved environment, mark it to be non-critical.
3907c478bd9Sstevel@tonic-gate */
3917c478bd9Sstevel@tonic-gate if (current_env != NULL)
3927c478bd9Sstevel@tonic-gate current_env->flags &= ~ENV_CRITICAL;
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate * If there is a stop pending, execute the stop.
3957c478bd9Sstevel@tonic-gate */
3967c478bd9Sstevel@tonic-gate if (stop_pending) {
3977c478bd9Sstevel@tonic-gate stop_pending = 0;
3987c478bd9Sstevel@tonic-gate onsusp(SIGSTOP);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate * If there is an abort pending, execute the abort.
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate if (current_env == NULL)
4047c478bd9Sstevel@tonic-gate return;
4057c478bd9Sstevel@tonic-gate if (current_env->flags & ENV_ABORT) {
4067c478bd9Sstevel@tonic-gate current_env->flags &= ~ENV_ABORT;
4077c478bd9Sstevel@tonic-gate cmdabort(SIGINT);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate * This routine turns off echoing on the controlling tty for the program.
4137c478bd9Sstevel@tonic-gate */
4147c478bd9Sstevel@tonic-gate void
echo_off(void)415*b12aaafbSToomas Soome echo_off(void)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate * Open the tty and store the file pointer for later.
4197c478bd9Sstevel@tonic-gate */
4207c478bd9Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
4217c478bd9Sstevel@tonic-gate if ((ttystate.ttyfile = open("/dev/tty",
4227c478bd9Sstevel@tonic-gate O_RDWR | O_NDELAY)) < 0) {
4237c478bd9Sstevel@tonic-gate err_print("Unable to open /dev/tty.\n");
4247c478bd9Sstevel@tonic-gate fullabort();
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate * Get the parameters for the tty, turn off echoing and set them.
4297c478bd9Sstevel@tonic-gate */
4307c478bd9Sstevel@tonic-gate if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
4317c478bd9Sstevel@tonic-gate err_print("Unable to get tty parameters.\n");
4327c478bd9Sstevel@tonic-gate fullabort();
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate ttystate.ttystate.c_lflag &= ~ECHO;
4357c478bd9Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
4367c478bd9Sstevel@tonic-gate err_print("Unable to set tty to echo off state.\n");
4377c478bd9Sstevel@tonic-gate fullabort();
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate * Remember that we've successfully turned
4427c478bd9Sstevel@tonic-gate * ECHO mode off, so we know to fix it later.
4437c478bd9Sstevel@tonic-gate */
4447c478bd9Sstevel@tonic-gate ttystate.ttyflags |= TTY_ECHO_OFF;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate * This routine turns on echoing on the controlling tty for the program.
4497c478bd9Sstevel@tonic-gate */
4507c478bd9Sstevel@tonic-gate void
echo_on(void)451*b12aaafbSToomas Soome echo_on(void)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate * Using the saved parameters, turn echoing on and set them.
4567c478bd9Sstevel@tonic-gate */
4577c478bd9Sstevel@tonic-gate ttystate.ttystate.c_lflag |= ECHO;
4587c478bd9Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
4597c478bd9Sstevel@tonic-gate err_print("Unable to set tty to echo on state.\n");
4607c478bd9Sstevel@tonic-gate fullabort();
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate * Close the tty and mark it ok again.
4647c478bd9Sstevel@tonic-gate */
4657c478bd9Sstevel@tonic-gate ttystate.ttyflags &= ~TTY_ECHO_OFF;
4667c478bd9Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
4677c478bd9Sstevel@tonic-gate (void) close(ttystate.ttyfile);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate * This routine turns off single character entry mode for tty.
4737c478bd9Sstevel@tonic-gate */
4747c478bd9Sstevel@tonic-gate void
charmode_on(void)475*b12aaafbSToomas Soome charmode_on(void)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * If tty unopened, open the tty and store the file pointer for later.
4807c478bd9Sstevel@tonic-gate */
4817c478bd9Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
4827c478bd9Sstevel@tonic-gate if ((ttystate.ttyfile = open("/dev/tty",
4837c478bd9Sstevel@tonic-gate O_RDWR | O_NDELAY)) < 0) {
4847c478bd9Sstevel@tonic-gate err_print("Unable to open /dev/tty.\n");
4857c478bd9Sstevel@tonic-gate fullabort();
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate * Get the parameters for the tty, turn on char mode.
4907c478bd9Sstevel@tonic-gate */
4917c478bd9Sstevel@tonic-gate if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
4927c478bd9Sstevel@tonic-gate err_print("Unable to get tty parameters.\n");
4937c478bd9Sstevel@tonic-gate fullabort();
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate ttystate.vmin = ttystate.ttystate.c_cc[VMIN];
4967c478bd9Sstevel@tonic-gate ttystate.vtime = ttystate.ttystate.c_cc[VTIME];
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate ttystate.ttystate.c_lflag &= ~ICANON;
4997c478bd9Sstevel@tonic-gate ttystate.ttystate.c_cc[VMIN] = 1;
5007c478bd9Sstevel@tonic-gate ttystate.ttystate.c_cc[VTIME] = 0;
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
5037c478bd9Sstevel@tonic-gate err_print("Unable to set tty to cbreak on state.\n");
5047c478bd9Sstevel@tonic-gate fullabort();
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate * Remember that we've successfully turned
5097c478bd9Sstevel@tonic-gate * CBREAK mode on, so we know to fix it later.
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate ttystate.ttyflags |= TTY_CBREAK_ON;
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate * This routine turns on single character entry mode for tty.
5167c478bd9Sstevel@tonic-gate * Note, this routine must be called before echo_on.
5177c478bd9Sstevel@tonic-gate */
5187c478bd9Sstevel@tonic-gate void
charmode_off(void)519*b12aaafbSToomas Soome charmode_off(void)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate * Using the saved parameters, turn char mode on.
5247c478bd9Sstevel@tonic-gate */
5257c478bd9Sstevel@tonic-gate ttystate.ttystate.c_lflag |= ICANON;
5267c478bd9Sstevel@tonic-gate ttystate.ttystate.c_cc[VMIN] = ttystate.vmin;
5277c478bd9Sstevel@tonic-gate ttystate.ttystate.c_cc[VTIME] = ttystate.vtime;
5287c478bd9Sstevel@tonic-gate if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
5297c478bd9Sstevel@tonic-gate err_print("Unable to set tty to cbreak off state.\n");
5307c478bd9Sstevel@tonic-gate fullabort();
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * Close the tty and mark it ok again.
5347c478bd9Sstevel@tonic-gate */
5357c478bd9Sstevel@tonic-gate ttystate.ttyflags &= ~TTY_CBREAK_ON;
5367c478bd9Sstevel@tonic-gate if (ttystate.ttyflags == 0) {
5377c478bd9Sstevel@tonic-gate (void) close(ttystate.ttyfile);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate * Allocate space for and return a pointer to a string
5447c478bd9Sstevel@tonic-gate * on the stack. If the string is null, create
5457c478bd9Sstevel@tonic-gate * an empty string.
5467c478bd9Sstevel@tonic-gate * Use destroy_data() to free when no longer used.
5477c478bd9Sstevel@tonic-gate */
5487c478bd9Sstevel@tonic-gate char *
alloc_string(char * s)549*b12aaafbSToomas Soome alloc_string(char *s)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate char *ns;
5527c478bd9Sstevel@tonic-gate
553*b12aaafbSToomas Soome if (s == NULL) {
554*b12aaafbSToomas Soome ns = zalloc(1);
5557c478bd9Sstevel@tonic-gate } else {
556*b12aaafbSToomas Soome ns = zalloc(strlen(s) + 1);
5577c478bd9Sstevel@tonic-gate (void) strcpy(ns, s);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate return (ns);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate * This function can be used to build up an array of strings
5667c478bd9Sstevel@tonic-gate * dynamically, with a trailing NULL to terminate the list.
5677c478bd9Sstevel@tonic-gate *
5687c478bd9Sstevel@tonic-gate * Parameters:
5697c478bd9Sstevel@tonic-gate * argvlist: a pointer to the base of the current list.
5707c478bd9Sstevel@tonic-gate * does not have to be initialized.
5717c478bd9Sstevel@tonic-gate * size: pointer to an integer, indicating the number
5727c478bd9Sstevel@tonic-gate * of string installed in the list. Must be
5737c478bd9Sstevel@tonic-gate * initialized to zero.
5747c478bd9Sstevel@tonic-gate * alloc: pointer to an integer, indicating the amount
5757c478bd9Sstevel@tonic-gate * of space allocated. Must be initialized to
5767c478bd9Sstevel@tonic-gate * zero. For efficiency, we allocate the list
5777c478bd9Sstevel@tonic-gate * in chunks and use it piece-by-piece.
5787c478bd9Sstevel@tonic-gate * str: the string to be inserted in the list.
5797c478bd9Sstevel@tonic-gate * A copy of the string is malloc'ed, and
5807c478bd9Sstevel@tonic-gate * appended at the end of the list.
5817c478bd9Sstevel@tonic-gate * Returns:
5827c478bd9Sstevel@tonic-gate * a pointer to the possibly-moved argvlist.
5837c478bd9Sstevel@tonic-gate *
5847c478bd9Sstevel@tonic-gate * No attempt to made to free unused memory when the list is
5857c478bd9Sstevel@tonic-gate * completed, although this would not be hard to do. For
5867c478bd9Sstevel@tonic-gate * reasonably small lists, this should suffice.
5877c478bd9Sstevel@tonic-gate */
5887c478bd9Sstevel@tonic-gate #define INITIAL_LISTSIZE 32
5897c478bd9Sstevel@tonic-gate #define INCR_LISTSIZE 32
5907c478bd9Sstevel@tonic-gate
5917c478bd9Sstevel@tonic-gate char **
build_argvlist(char ** argvlist,int * size,int * alloc,char * str)592*b12aaafbSToomas Soome build_argvlist(char **argvlist, int *size, int *alloc, char *str)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate if (*size + 2 > *alloc) {
5957c478bd9Sstevel@tonic-gate if (*alloc == 0) {
5967c478bd9Sstevel@tonic-gate *alloc = INITIAL_LISTSIZE;
597*b12aaafbSToomas Soome argvlist = zalloc(sizeof (char *) * (*alloc));
5987c478bd9Sstevel@tonic-gate } else {
5997c478bd9Sstevel@tonic-gate *alloc += INCR_LISTSIZE;
600*b12aaafbSToomas Soome argvlist = rezalloc((void *) argvlist,
6017c478bd9Sstevel@tonic-gate sizeof (char *) * (*alloc));
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate
6057c478bd9Sstevel@tonic-gate argvlist[*size] = alloc_string(str);
6067c478bd9Sstevel@tonic-gate *size += 1;
6077c478bd9Sstevel@tonic-gate argvlist[*size] = NULL;
6087c478bd9Sstevel@tonic-gate
6097c478bd9Sstevel@tonic-gate return (argvlist);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /*
6147c478bd9Sstevel@tonic-gate * Useful parsing macros
6157c478bd9Sstevel@tonic-gate */
6167c478bd9Sstevel@tonic-gate #define must_be(s, c) if (*s++ != c) return (0)
6177c478bd9Sstevel@tonic-gate #define skip_digits(s) while (isdigit(*s)) s++
6187c478bd9Sstevel@tonic-gate /* Parsing macro below is created to handle fabric devices which contains */
6197c478bd9Sstevel@tonic-gate /* upper hex digits like c2t210000203708B8CEd0s0. */
6207c478bd9Sstevel@tonic-gate /* To get the target id(tid) the digit and hex upper digit need to */
6217c478bd9Sstevel@tonic-gate /* be processed. */
6227c478bd9Sstevel@tonic-gate #define skip_digit_or_hexupper(s) while (isdigit(*s) || \
6237c478bd9Sstevel@tonic-gate (isxdigit(*s) && isupper(*s))) s++
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate * Return true if a device name matches the conventions
6277c478bd9Sstevel@tonic-gate * for the particular system.
6287c478bd9Sstevel@tonic-gate */
6297c478bd9Sstevel@tonic-gate int
conventional_name(char * name)6307c478bd9Sstevel@tonic-gate conventional_name(char *name)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate must_be(name, 'c');
6337c478bd9Sstevel@tonic-gate skip_digits(name);
6347c478bd9Sstevel@tonic-gate if (*name == 't') {
6357c478bd9Sstevel@tonic-gate name++;
6367c478bd9Sstevel@tonic-gate skip_digit_or_hexupper(name);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate must_be(name, 'd');
6397c478bd9Sstevel@tonic-gate skip_digits(name);
6407c478bd9Sstevel@tonic-gate must_be(name, 's');
6417c478bd9Sstevel@tonic-gate skip_digits(name);
6427c478bd9Sstevel@tonic-gate return (*name == 0);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate
645589271a4SSheng-Liang Eric Zhang #ifdef i386
646589271a4SSheng-Liang Eric Zhang /*
647589271a4SSheng-Liang Eric Zhang * Return true if a device name match the emc powerpath name scheme:
648589271a4SSheng-Liang Eric Zhang * emcpowerN[a-p,p0,p1,p2,p3,p4]
649589271a4SSheng-Liang Eric Zhang */
650589271a4SSheng-Liang Eric Zhang int
emcpower_name(char * name)651589271a4SSheng-Liang Eric Zhang emcpower_name(char *name)
652589271a4SSheng-Liang Eric Zhang {
653589271a4SSheng-Liang Eric Zhang char *emcp = "emcpower";
654589271a4SSheng-Liang Eric Zhang char *devp = "/dev/dsk";
655589271a4SSheng-Liang Eric Zhang char *rdevp = "/dev/rdsk";
656589271a4SSheng-Liang Eric Zhang
657589271a4SSheng-Liang Eric Zhang if (strncmp(devp, name, strlen(devp)) == 0) {
658589271a4SSheng-Liang Eric Zhang name += strlen(devp) + 1;
659589271a4SSheng-Liang Eric Zhang } else if (strncmp(rdevp, name, strlen(rdevp)) == 0) {
660589271a4SSheng-Liang Eric Zhang name += strlen(rdevp) + 1;
661589271a4SSheng-Liang Eric Zhang }
662589271a4SSheng-Liang Eric Zhang if (strncmp(emcp, name, strlen(emcp)) == 0) {
663589271a4SSheng-Liang Eric Zhang name += strlen(emcp);
664589271a4SSheng-Liang Eric Zhang if (isdigit(*name)) {
665589271a4SSheng-Liang Eric Zhang skip_digits(name);
666589271a4SSheng-Liang Eric Zhang if ((*name >= 'a') && (*name <= 'p')) {
667589271a4SSheng-Liang Eric Zhang name ++;
668589271a4SSheng-Liang Eric Zhang if ((*name >= '0') && (*name <= '4')) {
669589271a4SSheng-Liang Eric Zhang name++;
670589271a4SSheng-Liang Eric Zhang }
671589271a4SSheng-Liang Eric Zhang }
672589271a4SSheng-Liang Eric Zhang return (*name == '\0');
673589271a4SSheng-Liang Eric Zhang }
674589271a4SSheng-Liang Eric Zhang }
675589271a4SSheng-Liang Eric Zhang return (0);
676589271a4SSheng-Liang Eric Zhang }
677589271a4SSheng-Liang Eric Zhang #endif
678589271a4SSheng-Liang Eric Zhang
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate * Return true if a device name matches the intel physical name conventions
6817c478bd9Sstevel@tonic-gate * for the particular system.
6827c478bd9Sstevel@tonic-gate */
6837c478bd9Sstevel@tonic-gate int
fdisk_physical_name(char * name)6847c478bd9Sstevel@tonic-gate fdisk_physical_name(char *name)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate must_be(name, 'c');
6877c478bd9Sstevel@tonic-gate skip_digits(name);
6887c478bd9Sstevel@tonic-gate if (*name == 't') {
6897c478bd9Sstevel@tonic-gate name++;
6907c478bd9Sstevel@tonic-gate skip_digit_or_hexupper(name);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate must_be(name, 'd');
6937c478bd9Sstevel@tonic-gate skip_digits(name);
6947c478bd9Sstevel@tonic-gate must_be(name, 'p');
6957c478bd9Sstevel@tonic-gate skip_digits(name);
6967c478bd9Sstevel@tonic-gate return (*name == 0);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate * Return true if a device name matches the conventions
7017c478bd9Sstevel@tonic-gate * for a "whole disk" name for the particular system.
7027c478bd9Sstevel@tonic-gate * The name in this case must match exactly that which
7037c478bd9Sstevel@tonic-gate * would appear in the device directory itself.
7047c478bd9Sstevel@tonic-gate */
7057c478bd9Sstevel@tonic-gate int
whole_disk_name(char * name)706*b12aaafbSToomas Soome whole_disk_name(char *name)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate must_be(name, 'c');
7097c478bd9Sstevel@tonic-gate skip_digits(name);
7107c478bd9Sstevel@tonic-gate if (*name == 't') {
7117c478bd9Sstevel@tonic-gate name++;
7127c478bd9Sstevel@tonic-gate skip_digit_or_hexupper(name);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate must_be(name, 'd');
7157c478bd9Sstevel@tonic-gate skip_digits(name);
7167c478bd9Sstevel@tonic-gate must_be(name, 's');
7177c478bd9Sstevel@tonic-gate must_be(name, '2');
7187c478bd9Sstevel@tonic-gate return (*name == 0);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate * Return true if a name is in the internal canonical form
7247c478bd9Sstevel@tonic-gate */
7257c478bd9Sstevel@tonic-gate int
canonical_name(char * name)726*b12aaafbSToomas Soome canonical_name(char *name)
7277c478bd9Sstevel@tonic-gate {
7287c478bd9Sstevel@tonic-gate must_be(name, 'c');
7297c478bd9Sstevel@tonic-gate skip_digits(name);
7307c478bd9Sstevel@tonic-gate if (*name == 't') {
7317c478bd9Sstevel@tonic-gate name++;
7327c478bd9Sstevel@tonic-gate skip_digit_or_hexupper(name);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate must_be(name, 'd');
7357c478bd9Sstevel@tonic-gate skip_digits(name);
7367c478bd9Sstevel@tonic-gate return (*name == 0);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate * Return true if a name is in the internal canonical form for 4.x
7427c478bd9Sstevel@tonic-gate * Used to support 4.x naming conventions under 5.0.
7437c478bd9Sstevel@tonic-gate */
7447c478bd9Sstevel@tonic-gate int
canonical4x_name(char * name)745*b12aaafbSToomas Soome canonical4x_name(char *name)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate char **p;
7487c478bd9Sstevel@tonic-gate int i;
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate p = disk_4x_identifiers;
7517c478bd9Sstevel@tonic-gate for (i = N_DISK_4X_IDS; i > 0; i--, p++) {
7527c478bd9Sstevel@tonic-gate if (match_substr(name, *p)) {
7537c478bd9Sstevel@tonic-gate name += strlen(*p);
7547c478bd9Sstevel@tonic-gate break;
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate if (i == 0)
7587c478bd9Sstevel@tonic-gate return (0);
7597c478bd9Sstevel@tonic-gate skip_digits(name);
7607c478bd9Sstevel@tonic-gate return (*name == 0);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate * Map a conventional name into the internal canonical form:
7667c478bd9Sstevel@tonic-gate *
7677c478bd9Sstevel@tonic-gate * /dev/rdsk/c0t0d0s0 -> c0t0d0
7687c478bd9Sstevel@tonic-gate */
7697c478bd9Sstevel@tonic-gate void
canonicalize_name(char * dst,char * src)770*b12aaafbSToomas Soome canonicalize_name(char *dst, char *src)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate char *s;
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate * Copy from the 'c' to the end to the destination string...
7767c478bd9Sstevel@tonic-gate */
7777c478bd9Sstevel@tonic-gate s = strchr(src, 'c');
7787c478bd9Sstevel@tonic-gate if (s != NULL) {
7797c478bd9Sstevel@tonic-gate (void) strcpy(dst, s);
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate * Remove the trailing slice (partition) reference
7827c478bd9Sstevel@tonic-gate */
7837c478bd9Sstevel@tonic-gate s = dst + strlen(dst) - 2;
7847c478bd9Sstevel@tonic-gate if (*s == 's') {
7857c478bd9Sstevel@tonic-gate *s = 0;
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate } else {
7887c478bd9Sstevel@tonic-gate *dst = 0; /* be tolerant of garbage input */
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate * Return true if we find an occurance of s2 at the
7957c478bd9Sstevel@tonic-gate * beginning of s1. We don't have to match all of
7967c478bd9Sstevel@tonic-gate * s1, but we do have to match all of s2
7977c478bd9Sstevel@tonic-gate */
7987c478bd9Sstevel@tonic-gate int
match_substr(char * s1,char * s2)799*b12aaafbSToomas Soome match_substr(char *s1, char *s2)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate while (*s2 != 0) {
8027c478bd9Sstevel@tonic-gate if (*s1++ != *s2++)
8037c478bd9Sstevel@tonic-gate return (0);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate return (1);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate * Dump a structure in hexadecimal, for diagnostic purposes
8127c478bd9Sstevel@tonic-gate */
8137c478bd9Sstevel@tonic-gate #define BYTES_PER_LINE 16
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate void
dump(char * hdr,caddr_t src,int nbytes,int format)816*b12aaafbSToomas Soome dump(char *hdr, caddr_t src, int nbytes, int format)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate int i;
8197c478bd9Sstevel@tonic-gate int n;
8207c478bd9Sstevel@tonic-gate char *p;
8217c478bd9Sstevel@tonic-gate char s[256];
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate assert(format == HEX_ONLY || format == HEX_ASCII);
8247c478bd9Sstevel@tonic-gate
8257c478bd9Sstevel@tonic-gate (void) strcpy(s, hdr);
8267c478bd9Sstevel@tonic-gate for (p = s; *p; p++) {
8277c478bd9Sstevel@tonic-gate *p = ' ';
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate p = hdr;
8317c478bd9Sstevel@tonic-gate while (nbytes > 0) {
8327c478bd9Sstevel@tonic-gate err_print("%s", p);
8337c478bd9Sstevel@tonic-gate p = s;
8347c478bd9Sstevel@tonic-gate n = min(nbytes, BYTES_PER_LINE);
8357c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
8367c478bd9Sstevel@tonic-gate err_print("%02x ", src[i] & 0xff);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate if (format == HEX_ASCII) {
8397c478bd9Sstevel@tonic-gate for (i = BYTES_PER_LINE-n; i > 0; i--) {
8407c478bd9Sstevel@tonic-gate err_print(" ");
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate err_print(" ");
8437c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) {
844*b12aaafbSToomas Soome err_print("%c", isprint(src[i]) ? src[i] : '.');
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate err_print("\n");
8487c478bd9Sstevel@tonic-gate nbytes -= n;
8497c478bd9Sstevel@tonic-gate src += n;
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate
8547c478bd9Sstevel@tonic-gate float
bn2mb(uint64_t nblks)8557c478bd9Sstevel@tonic-gate bn2mb(uint64_t nblks)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate float n;
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate n = (float)nblks / 1024.0;
86065908c77Syu, larry liu - Sun Microsystems - Beijing China return ((n / 1024.0) * cur_blksz);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate
864342440ecSPrasad Singamsetty diskaddr_t
mb2bn(float mb)8657c478bd9Sstevel@tonic-gate mb2bn(float mb)
8667c478bd9Sstevel@tonic-gate {
867342440ecSPrasad Singamsetty diskaddr_t n;
8687c478bd9Sstevel@tonic-gate
86965908c77Syu, larry liu - Sun Microsystems - Beijing China n = (diskaddr_t)(mb * 1024.0 * (1024.0 / cur_blksz));
8707c478bd9Sstevel@tonic-gate return (n);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate float
bn2gb(uint64_t nblks)8747c478bd9Sstevel@tonic-gate bn2gb(uint64_t nblks)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate float n;
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate n = (float)nblks / (1024.0 * 1024.0);
87965908c77Syu, larry liu - Sun Microsystems - Beijing China return ((n/1024.0) * cur_blksz);
8807c478bd9Sstevel@tonic-gate
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate
8837c478bd9Sstevel@tonic-gate float
bn2tb(uint64_t nblks)8847c478bd9Sstevel@tonic-gate bn2tb(uint64_t nblks)
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate float n;
8877c478bd9Sstevel@tonic-gate
8887c478bd9Sstevel@tonic-gate n = (float)nblks / (1024.0 * 1024.0 * 1024.0);
88965908c77Syu, larry liu - Sun Microsystems - Beijing China return ((n/1024.0) * cur_blksz);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate
892342440ecSPrasad Singamsetty diskaddr_t
gb2bn(float gb)8937c478bd9Sstevel@tonic-gate gb2bn(float gb)
8947c478bd9Sstevel@tonic-gate {
895342440ecSPrasad Singamsetty diskaddr_t n;
8967c478bd9Sstevel@tonic-gate
89765908c77Syu, larry liu - Sun Microsystems - Beijing China n = (diskaddr_t)(gb * 1024.0 * 1024.0 * (1024.0 / cur_blksz));
8987c478bd9Sstevel@tonic-gate return (n);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate * This routine finds out the number of lines (rows) in a terminal
9037c478bd9Sstevel@tonic-gate * window. The default value of TTY_LINES is returned on error.
9047c478bd9Sstevel@tonic-gate */
9057c478bd9Sstevel@tonic-gate int
get_tty_lines(void)906*b12aaafbSToomas Soome get_tty_lines(void)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate int tty_lines = TTY_LINES;
9097c478bd9Sstevel@tonic-gate struct winsize winsize;
9107c478bd9Sstevel@tonic-gate
911*b12aaafbSToomas Soome if ((option_f == NULL) && isatty(0) == 1 && isatty(1) == 1) {
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate * We have a real terminal for std input and output
9147c478bd9Sstevel@tonic-gate */
9157c478bd9Sstevel@tonic-gate winsize.ws_row = 0;
9167c478bd9Sstevel@tonic-gate if (ioctl(1, TIOCGWINSZ, &winsize) == 0) {
9177c478bd9Sstevel@tonic-gate if (winsize.ws_row > 2) {
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate * Should be atleast 2 lines, for division
9207c478bd9Sstevel@tonic-gate * by (tty_lines - 1, tty_lines - 2) to work.
9217c478bd9Sstevel@tonic-gate */
9227c478bd9Sstevel@tonic-gate tty_lines = winsize.ws_row;
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate return (tty_lines);
9277c478bd9Sstevel@tonic-gate }
928