#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 <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/time.h>
#include <ctype.h>
#include <termio.h>
#include "misc.h"
#include "analyze.h"
#include "label.h"
#include "startup.h"
static void cleanup(int sig);
struct env *current_env = NULL;
static int stop_pending = 0;
struct ttystate ttystate;
static int aborting = 0;
static char *disk_4x_identifiers[] = { "sd", "id"};
#define N_DISK_4X_IDS (sizeof (disk_4x_identifiers)/sizeof (char *))
char *confirm_list[] = {
"yes",
"no",
NULL,
};
void *
zalloc(int count)
{
void *ptr;
if ((ptr = calloc(1, (unsigned)count)) == NULL) {
err_print("Error: unable to calloc more space.\n");
fullabort();
}
return (ptr);
}
void *
rezalloc(void *ptr, int count)
{
void *new_ptr;
if ((new_ptr = realloc((char *)ptr, (unsigned)count)) == NULL) {
err_print("Error: unable to realloc more space.\n");
fullabort();
}
return (new_ptr);
}
void
destroy_data(char *data)
{
free(data);
}
#ifdef not
char *
space2str(uint_t space)
{
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
int
check(char *question)
{
int answer;
u_ioparam_t ioparam;
if (option_f)
return (0);
ioparam.io_charlist = confirm_list;
answer = input(FIO_MSTR, question, '?', &ioparam, NULL, DATA_INPUT);
return (answer);
}
void
cmdabort(int sig __unused)
{
if (current_env == NULL || !(current_env->flags & ENV_USE))
fullabort();
if (current_env->flags & ENV_CRITICAL) {
current_env->flags |= ENV_ABORT;
return;
}
if (option_f)
fullabort();
fmt_print("\n");
cleanup(sig);
longjmp(current_env->env, 0);
}
void
onsusp(int sig __unused)
{
int fix_term;
#ifdef NOT_DEF
sigset_t sigmask;
#endif
if (current_env != NULL && current_env->flags & ENV_CRITICAL) {
stop_pending = 1;
return;
}
fix_term = ttystate.ttyflags;
fmt_print("\n");
cleanup(sig);
#ifdef NOT_DEF
sigmask.sigbits[0] = (ulong_t)0xffffffff;
if (sigprocmask(SIG_SETMASK, &sigmask, NULL) == -1)
err_print("sigprocmask failed %d\n", errno);
#endif
(void) signal(SIGTSTP, SIG_DFL);
(void) kill(0, SIGTSTP);
(void) signal(SIGTSTP, onsusp);
if (fix_term & TTY_ECHO_OFF)
echo_off();
if (fix_term & TTY_CBREAK_ON)
charmode_on();
}
void
onalarm(int sig __unused)
{
}
void
fullabort(void)
{
fmt_print("\n");
if (!aborting) {
aborting = 1;
cleanup(SIGKILL);
}
exit(1);
}
static void
cleanup(int sig)
{
enter_critical();
if (ttystate.ttyflags & TTY_CBREAK_ON) {
charmode_off();
}
if (ttystate.ttyflags & TTY_ECHO_OFF) {
echo_on();
}
if (cur_list.flags & LIST_DIRTY) {
cur_list.flags = 0;
if (!EMBEDDED_SCSI)
write_deflist(&cur_list);
}
if (cur_flags & LABEL_DIRTY) {
cur_flags &= ~LABEL_DIRTY;
(void) write_label();
}
if (log_file && scan_cur_block >= 0) {
pr_dblock(log_print, scan_cur_block);
log_print("\n");
}
if (scan_blocks_fixed >= 0)
fmt_print("Total of %lld defective blocks repaired.\n",
scan_blocks_fixed);
if (sig != SIGSTOP) {
scan_cur_block = scan_blocks_fixed = -1;
}
exit_critical();
}
void
enter_critical(void)
{
if (current_env == NULL)
return;
current_env->flags |= ENV_CRITICAL;
}
void
exit_critical(void)
{
if (current_env != NULL)
current_env->flags &= ~ENV_CRITICAL;
if (stop_pending) {
stop_pending = 0;
onsusp(SIGSTOP);
}
if (current_env == NULL)
return;
if (current_env->flags & ENV_ABORT) {
current_env->flags &= ~ENV_ABORT;
cmdabort(SIGINT);
}
}
void
echo_off(void)
{
if (ttystate.ttyflags == 0) {
if ((ttystate.ttyfile = open("/dev/tty",
O_RDWR | O_NDELAY)) < 0) {
err_print("Unable to open /dev/tty.\n");
fullabort();
}
}
if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
err_print("Unable to get tty parameters.\n");
fullabort();
}
ttystate.ttystate.c_lflag &= ~ECHO;
if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
err_print("Unable to set tty to echo off state.\n");
fullabort();
}
ttystate.ttyflags |= TTY_ECHO_OFF;
}
void
echo_on(void)
{
ttystate.ttystate.c_lflag |= ECHO;
if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
err_print("Unable to set tty to echo on state.\n");
fullabort();
}
ttystate.ttyflags &= ~TTY_ECHO_OFF;
if (ttystate.ttyflags == 0) {
(void) close(ttystate.ttyfile);
}
}
void
charmode_on(void)
{
if (ttystate.ttyflags == 0) {
if ((ttystate.ttyfile = open("/dev/tty",
O_RDWR | O_NDELAY)) < 0) {
err_print("Unable to open /dev/tty.\n");
fullabort();
}
}
if (tcgetattr(ttystate.ttyfile, &ttystate.ttystate) < 0) {
err_print("Unable to get tty parameters.\n");
fullabort();
}
ttystate.vmin = ttystate.ttystate.c_cc[VMIN];
ttystate.vtime = ttystate.ttystate.c_cc[VTIME];
ttystate.ttystate.c_lflag &= ~ICANON;
ttystate.ttystate.c_cc[VMIN] = 1;
ttystate.ttystate.c_cc[VTIME] = 0;
if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
err_print("Unable to set tty to cbreak on state.\n");
fullabort();
}
ttystate.ttyflags |= TTY_CBREAK_ON;
}
void
charmode_off(void)
{
ttystate.ttystate.c_lflag |= ICANON;
ttystate.ttystate.c_cc[VMIN] = ttystate.vmin;
ttystate.ttystate.c_cc[VTIME] = ttystate.vtime;
if (tcsetattr(ttystate.ttyfile, TCSANOW, &ttystate.ttystate) < 0) {
err_print("Unable to set tty to cbreak off state.\n");
fullabort();
}
ttystate.ttyflags &= ~TTY_CBREAK_ON;
if (ttystate.ttyflags == 0) {
(void) close(ttystate.ttyfile);
}
}
char *
alloc_string(char *s)
{
char *ns;
if (s == NULL) {
ns = zalloc(1);
} else {
ns = zalloc(strlen(s) + 1);
(void) strcpy(ns, s);
}
return (ns);
}
#define INITIAL_LISTSIZE 32
#define INCR_LISTSIZE 32
char **
build_argvlist(char **argvlist, int *size, int *alloc, char *str)
{
if (*size + 2 > *alloc) {
if (*alloc == 0) {
*alloc = INITIAL_LISTSIZE;
argvlist = zalloc(sizeof (char *) * (*alloc));
} else {
*alloc += INCR_LISTSIZE;
argvlist = rezalloc((void *) argvlist,
sizeof (char *) * (*alloc));
}
}
argvlist[*size] = alloc_string(str);
*size += 1;
argvlist[*size] = NULL;
return (argvlist);
}
#define must_be(s, c) if (*s++ != c) return (0)
#define skip_digits(s) while (isdigit(*s)) s++
#define skip_digit_or_hexupper(s) while (isdigit(*s) || \
(isxdigit(*s) && isupper(*s))) s++
int
conventional_name(char *name)
{
must_be(name, 'c');
skip_digits(name);
if (*name == 't') {
name++;
skip_digit_or_hexupper(name);
}
must_be(name, 'd');
skip_digits(name);
must_be(name, 's');
skip_digits(name);
return (*name == 0);
}
#ifdef i386
int
emcpower_name(char *name)
{
char *emcp = "emcpower";
char *devp = "/dev/dsk";
char *rdevp = "/dev/rdsk";
if (strncmp(devp, name, strlen(devp)) == 0) {
name += strlen(devp) + 1;
} else if (strncmp(rdevp, name, strlen(rdevp)) == 0) {
name += strlen(rdevp) + 1;
}
if (strncmp(emcp, name, strlen(emcp)) == 0) {
name += strlen(emcp);
if (isdigit(*name)) {
skip_digits(name);
if ((*name >= 'a') && (*name <= 'p')) {
name ++;
if ((*name >= '0') && (*name <= '4')) {
name++;
}
}
return (*name == '\0');
}
}
return (0);
}
#endif
int
fdisk_physical_name(char *name)
{
must_be(name, 'c');
skip_digits(name);
if (*name == 't') {
name++;
skip_digit_or_hexupper(name);
}
must_be(name, 'd');
skip_digits(name);
must_be(name, 'p');
skip_digits(name);
return (*name == 0);
}
int
whole_disk_name(char *name)
{
must_be(name, 'c');
skip_digits(name);
if (*name == 't') {
name++;
skip_digit_or_hexupper(name);
}
must_be(name, 'd');
skip_digits(name);
must_be(name, 's');
must_be(name, '2');
return (*name == 0);
}
int
canonical_name(char *name)
{
must_be(name, 'c');
skip_digits(name);
if (*name == 't') {
name++;
skip_digit_or_hexupper(name);
}
must_be(name, 'd');
skip_digits(name);
return (*name == 0);
}
int
canonical4x_name(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)) {
name += strlen(*p);
break;
}
}
if (i == 0)
return (0);
skip_digits(name);
return (*name == 0);
}
void
canonicalize_name(char *dst, char *src)
{
char *s;
s = strchr(src, 'c');
if (s != NULL) {
(void) strcpy(dst, s);
s = dst + strlen(dst) - 2;
if (*s == 's') {
*s = 0;
}
} else {
*dst = 0;
}
}
int
match_substr(char *s1, char *s2)
{
while (*s2 != 0) {
if (*s1++ != *s2++)
return (0);
}
return (1);
}
#define BYTES_PER_LINE 16
void
dump(char *hdr, caddr_t src, int nbytes, int format)
{
int i;
int n;
char *p;
char s[256];
assert(format == HEX_ONLY || format == HEX_ASCII);
(void) strcpy(s, hdr);
for (p = s; *p; p++) {
*p = ' ';
}
p = hdr;
while (nbytes > 0) {
err_print("%s", p);
p = s;
n = min(nbytes, BYTES_PER_LINE);
for (i = 0; i < n; i++) {
err_print("%02x ", src[i] & 0xff);
}
if (format == HEX_ASCII) {
for (i = BYTES_PER_LINE-n; i > 0; i--) {
err_print(" ");
}
err_print(" ");
for (i = 0; i < n; i++) {
err_print("%c", isprint(src[i]) ? src[i] : '.');
}
}
err_print("\n");
nbytes -= n;
src += n;
}
}
float
bn2mb(uint64_t nblks)
{
float n;
n = (float)nblks / 1024.0;
return ((n / 1024.0) * cur_blksz);
}
diskaddr_t
mb2bn(float mb)
{
diskaddr_t n;
n = (diskaddr_t)(mb * 1024.0 * (1024.0 / cur_blksz));
return (n);
}
float
bn2gb(uint64_t nblks)
{
float n;
n = (float)nblks / (1024.0 * 1024.0);
return ((n/1024.0) * cur_blksz);
}
float
bn2tb(uint64_t nblks)
{
float n;
n = (float)nblks / (1024.0 * 1024.0 * 1024.0);
return ((n/1024.0) * cur_blksz);
}
diskaddr_t
gb2bn(float gb)
{
diskaddr_t n;
n = (diskaddr_t)(gb * 1024.0 * 1024.0 * (1024.0 / cur_blksz));
return (n);
}
int
get_tty_lines(void)
{
int tty_lines = TTY_LINES;
struct winsize winsize;
if ((option_f == NULL) && isatty(0) == 1 && isatty(1) == 1) {
winsize.ws_row = 0;
if (ioctl(1, TIOCGWINSZ, &winsize) == 0) {
if (winsize.ws_row > 2) {
tty_lines = winsize.ws_row;
}
}
}
return (tty_lines);
}