misc.c revision 589271a44eaf1e2b6b05d80b025dc8b94e009aef
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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
* 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
*/
/*
*/
/*
* This file contains miscellaneous routines.
*/
#include "global.h"
#include <stdlib.h>
#include <signal.h>
#include <malloc.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <termio.h>
#include "misc.h"
#include "analyze.h"
#include "label.h"
#include "startup.h"
#ifdef __STDC__
/* Function prototypes for ANSI C Compilers */
#else /* __STDC__ */
/* Function prototypes for non-ANSI C Compilers */
static void cleanup();
#endif /* __STDC__ */
static int stop_pending = 0; /* ctrl-Z is pending */
static int aborting = 0; /* in process of aborting */
/*
* For 4.x, limit the choices of valid disk names to this set.
*/
#define N_DISK_4X_IDS (sizeof (disk_4x_identifiers)/sizeof (char *))
/*
*/
char *confirm_list[] = {
"yes",
"no",
NULL,
};
/*
* This routine is a wrapper for malloc. It allocates pre-zeroed space,
* and checks the return value so the caller doesn't have to.
*/
void *
int count;
{
void *ptr;
err_print("Error: unable to calloc more space.\n");
fullabort();
}
return (ptr);
}
/*
* This routine is a wrapper for realloc. It reallocates the given
* space, and checks the return value so the caller doesn't have to.
* Note that the any space added by this call is NOT necessarily
* zeroed.
*/
void *
void *ptr;
int count;
{
void *new_ptr;
err_print("Error: unable to realloc more space.\n");
fullabort();
}
return (new_ptr);
}
/*
* This routine is a wrapper for free.
*/
void
char *data;
{
}
#ifdef not
/*
* This routine takes the space number returned by an ioctl call and
* returns a mnemonic name for that space.
*/
char *
{
char *name;
switch (space&SP_BUSMASK) {
case SP_VIRTUAL:
name = "virtual";
break;
case SP_OBMEM:
name = "obmem";
break;
case SP_OBIO:
name = "obio";
break;
case SP_MBMEM:
name = "mbmem";
break;
case SP_MBIO:
name = "mbio";
break;
default:
err_print("Error: unknown address space type encountered.\n");
fullabort();
}
return (name);
}
#endif /* not */
/*
* the response.
*/
int
char *question;
{
int answer;
/*
* If we are running out of a command file, assume a yes answer.
*/
if (option_f)
return (0);
/*
* Ask the user.
*/
(int *)NULL, DATA_INPUT);
return (answer);
}
/*
* This routine aborts the current command. It is called by a ctrl-C
* interrupt and also under certain error conditions.
*/
/*ARGSUSED*/
void
int sig;
{
/*
* If there is no usable saved environment, gracefully exit. This
* allows the user to interrupt the program even when input is from
* a file, or if there is no current menu, like at the "Select disk:"
* prompt.
*/
fullabort();
/*
* If we are in a critical zone, note the attempt and return.
*/
return;
}
/*
* All interruptions when we are running out of a command file
* cause the program to gracefully exit.
*/
if (option_f)
fullabort();
fmt_print("\n");
/*
* Clean up any state left by the interrupted command.
*/
/*
* Jump to the saved environment.
*/
}
/*
* This routine implements the ctrl-Z suspend mechanism. It is called
* when a suspend signal is received.
*/
/*ARGSUSED*/
void
int sig;
{
int fix_term;
#ifdef NOT_DEF
#endif /* NOT_DEF */
/*
* If we are in a critical zone, note the attempt and return.
*/
stop_pending = 1;
return;
}
/*
* If the terminal is mucked up, note that we will need to
* re-muck it when we start up again.
*/
fmt_print("\n");
/*
* Clean up any state left by the interrupted command.
*/
#ifdef NOT_DEF
/* Investigate whether all this is necessary */
/*
* Stop intercepting the suspend signal, then send ourselves one
* to cause us to stop.
*/
#endif /* NOT_DEF */
/*
* PC stops here
*/
/*
* We are started again. Set us up to intercept the suspend
* signal once again.
*/
/*
* Re-muck the terminal if necessary.
*/
if (fix_term & TTY_ECHO_OFF)
echo_off();
if (fix_term & TTY_CBREAK_ON)
charmode_on();
}
/*
* This routine implements the timing function used during long-term
* disk operations (e.g. formatting). It is called when an alarm signal
* is received.
*/
/*ARGSUSED*/
void
int sig;
{
}
/*
* This routine gracefully exits the program.
*/
void
{
fmt_print("\n");
/*
* Clean up any state left by an interrupted command.
* Avoid infinite loops caused by a clean-up
* routine failing again...
*/
if (!aborting) {
aborting = 1;
}
exit(1);
/*NOTREACHED*/
}
/*
* This routine cleans up the state of the world. It is a hodge-podge
* of kludges to allow us to interrupt commands whenever possible.
*
* Some cleanup actions may depend on the type of signal.
*/
static void
{
/*
* Lock out interrupts to avoid recursion.
*/
/*
* Fix up the tty if necessary.
*/
charmode_off();
}
echo_on();
}
/*
* If the defect list is dirty, write it out.
*/
if (!EMBEDDED_SCSI)
}
/*
* If the label is dirty, write it out.
*/
if (cur_flags & LABEL_DIRTY) {
cur_flags &= ~LABEL_DIRTY;
(void) write_label();
}
/*
* If we are logging and just interrupted a scan, print out
* some summary info to the log file.
*/
if (log_file && scan_cur_block >= 0) {
log_print("\n");
}
if (scan_blocks_fixed >= 0)
fmt_print("Total of %lld defective blocks repaired.\n",
}
}
/*
* This routine causes the program to enter a critical zone. Within the
* critical zone, no interrupts are allowed. Note that calls to this
* routine for the same environment do NOT nest, so there is not
* necessarily pairing between calls to enter_critical() and exit_critical().
*/
void
{
/*
* If there is no saved environment, interrupts will be ignored.
*/
if (current_env == NULL)
return;
/*
* Mark the environment to be in a critical zone.
*/
}
/*
* This routine causes the program to exit a critical zone. Note that
* calls to enter_critical() for the same environment do NOT nest, so
* one call to exit_critical() will erase any number of such calls.
*/
void
{
/*
* If there is a saved environment, mark it to be non-critical.
*/
if (current_env != NULL)
/*
* If there is a stop pending, execute the stop.
*/
if (stop_pending) {
stop_pending = 0;
}
/*
* If there is an abort pending, execute the abort.
*/
if (current_env == NULL)
return;
}
}
/*
* This routine turns off echoing on the controlling tty for the program.
*/
void
echo_off()
{
/*
* Open the tty and store the file pointer for later.
*/
fullabort();
}
}
/*
* Get the parameters for the tty, turn off echoing and set them.
*/
err_print("Unable to get tty parameters.\n");
fullabort();
}
err_print("Unable to set tty to echo off state.\n");
fullabort();
}
/*
* Remember that we've successfully turned
* ECHO mode off, so we know to fix it later.
*/
}
/*
* This routine turns on echoing on the controlling tty for the program.
*/
void
echo_on()
{
/*
* Using the saved parameters, turn echoing on and set them.
*/
err_print("Unable to set tty to echo on state.\n");
fullabort();
}
/*
* Close the tty and mark it ok again.
*/
}
}
/*
* This routine turns off single character entry mode for tty.
*/
void
{
/*
* If tty unopened, open the tty and store the file pointer for later.
*/
fullabort();
}
}
/*
* Get the parameters for the tty, turn on char mode.
*/
err_print("Unable to get tty parameters.\n");
fullabort();
}
err_print("Unable to set tty to cbreak on state.\n");
fullabort();
}
/*
* Remember that we've successfully turned
* CBREAK mode on, so we know to fix it later.
*/
}
/*
* This routine turns on single character entry mode for tty.
* Note, this routine must be called before echo_on.
*/
void
{
/*
* Using the saved parameters, turn char mode on.
*/
err_print("Unable to set tty to cbreak off state.\n");
fullabort();
}
/*
* Close the tty and mark it ok again.
*/
}
}
/*
* Allocate space for and return a pointer to a string
* on the stack. If the string is null, create
* an empty string.
* Use destroy_data() to free when no longer used.
*/
char *
alloc_string(s)
char *s;
{
char *ns;
if (s == (char *)NULL) {
} else {
}
return (ns);
}
/*
* This function can be used to build up an array of strings
* dynamically, with a trailing NULL to terminate the list.
*
* Parameters:
* argvlist: a pointer to the base of the current list.
* does not have to be initialized.
* size: pointer to an integer, indicating the number
* of string installed in the list. Must be
* initialized to zero.
* alloc: pointer to an integer, indicating the amount
* of space allocated. Must be initialized to
* zero. For efficiency, we allocate the list
* in chunks and use it piece-by-piece.
* str: the string to be inserted in the list.
* A copy of the string is malloc'ed, and
* appended at the end of the list.
* Returns:
* a pointer to the possibly-moved argvlist.
*
* No attempt to made to free unused memory when the list is
* completed, although this would not be hard to do. For
* reasonably small lists, this should suffice.
*/
#define INITIAL_LISTSIZE 32
#define INCR_LISTSIZE 32
char **
char **argvlist;
int *size;
int *alloc;
char *str;
{
if (*alloc == 0) {
argvlist = (char **)
} else {
*alloc += INCR_LISTSIZE;
argvlist = (char **)
sizeof (char *) * (*alloc));
}
}
*size += 1;
return (argvlist);
}
/*
* Useful parsing macros
*/
#define must_be(s, c) if (*s++ != c) return (0)
#define skip_digits(s) while (isdigit(*s)) s++
/* Parsing macro below is created to handle fabric devices which contains */
/* upper hex digits like c2t210000203708B8CEd0s0. */
/* To get the target id(tid) the digit and hex upper digit need to */
/* be processed. */
#define skip_digit_or_hexupper(s) while (isdigit(*s) || \
/*
* Return true if a device name matches the conventions
* for the particular system.
*/
int
conventional_name(char *name)
{
if (*name == 't') {
name++;
}
return (*name == 0);
}
#ifdef i386
/*
* Return true if a device name match the emc powerpath name scheme:
* emcpowerN[a-p,p0,p1,p2,p3,p4]
*/
int
emcpower_name(char *name)
{
char *emcp = "emcpower";
}
name ++;
name++;
}
}
return (*name == '\0');
}
}
return (0);
}
#endif
/*
* Return true if a device name matches the intel physical name conventions
* for the particular system.
*/
int
fdisk_physical_name(char *name)
{
if (*name == 't') {
name++;
}
return (*name == 0);
}
/*
* Return true if a device name matches the conventions
* for a "whole disk" name for the particular system.
* The name in this case must match exactly that which
* would appear in the device directory itself.
*/
int
char *name;
{
if (*name == 't') {
name++;
}
return (*name == 0);
}
/*
* Return true if a name is in the internal canonical form
*/
int
char *name;
{
if (*name == 't') {
name++;
}
return (*name == 0);
}
/*
* Return true if a name is in the internal canonical form for 4.x
* Used to support 4.x naming conventions under 5.0.
*/
int
char *name;
{
char **p;
int i;
p = disk_4x_identifiers;
for (i = N_DISK_4X_IDS; i > 0; i--, p++) {
if (match_substr(name, *p)) {
break;
}
}
if (i == 0)
return (0);
return (*name == 0);
}
/*
* Map a conventional name into the internal canonical form:
*
*/
void
char *dst;
char *src;
{
char *s;
/*
* Copy from the 'c' to the end to the destination string...
*/
if (s != NULL) {
/*
* Remove the trailing slice (partition) reference
*/
if (*s == 's') {
*s = 0;
}
} else {
*dst = 0; /* be tolerant of garbage input */
}
}
/*
* Return true if we find an occurance of s2 at the
* beginning of s1. We don't have to match all of
* s1, but we do have to match all of s2
*/
int
char *s1;
char *s2;
{
while (*s2 != 0) {
return (0);
}
return (1);
}
/*
* Dump a structure in hexadecimal, for diagnostic purposes
*/
#define BYTES_PER_LINE 16
void
char *hdr;
int nbytes;
int format;
{
int i;
int n;
char *p;
char s[256];
for (p = s; *p; p++) {
*p = ' ';
}
p = hdr;
while (nbytes > 0) {
err_print("%s", p);
p = s;
for (i = 0; i < n; i++) {
}
for (i = BYTES_PER_LINE-n; i > 0; i--) {
err_print(" ");
}
err_print(" ");
for (i = 0; i < n; i++) {
err_print("%c",
}
}
err_print("\n");
nbytes -= n;
src += n;
}
}
float
{
float n;
n = (float)nblks / 1024.0;
return ((n / 1024.0) * cur_blksz);
}
{
diskaddr_t n;
return (n);
}
float
{
float n;
return ((n/1024.0) * cur_blksz);
}
float
{
float n;
return ((n/1024.0) * cur_blksz);
}
{
diskaddr_t n;
return (n);
}
/*
* This routine finds out the number of lines (rows) in a terminal
* window. The default value of TTY_LINES is returned on error.
*/
int
{
/*
* We have a real terminal for std input and output
*/
/*
* Should be atleast 2 lines, for division
* by (tty_lines - 1, tty_lines - 2) to work.
*/
}
}
}
return (tty_lines);
}