10Sstevel@tonic-gate /*LINTLIBRARY*/
20Sstevel@tonic-gate /*PROTOLIB1*/
30Sstevel@tonic-gate /*
4*1053Smaheshvs * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
50Sstevel@tonic-gate * Use is subject to license terms.
60Sstevel@tonic-gate */
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
90Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
100Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
110Sstevel@tonic-gate */
120Sstevel@tonic-gate
130Sstevel@tonic-gate /* line below is from UCB 5.4 12/11/85 */
140Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
150Sstevel@tonic-gate
160Sstevel@tonic-gate #include <myrcmd.h>
170Sstevel@tonic-gate #include <stdio.h>
180Sstevel@tonic-gate #include <locale.h>
190Sstevel@tonic-gate #include <ctype.h>
200Sstevel@tonic-gate #include <pwd.h>
210Sstevel@tonic-gate #include <string.h>
220Sstevel@tonic-gate #include <signal.h>
230Sstevel@tonic-gate #include <sys/mtio.h>
240Sstevel@tonic-gate #include <sys/socket.h>
250Sstevel@tonic-gate #include <unistd.h>
260Sstevel@tonic-gate #include <netdb.h>
270Sstevel@tonic-gate #include <locale.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <rmt.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #define sigvec sigaction
340Sstevel@tonic-gate #define sv_handler sa_handler
350Sstevel@tonic-gate
360Sstevel@tonic-gate #include <netinet/in.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate extern int32_t tp_bsize;
390Sstevel@tonic-gate
400Sstevel@tonic-gate #define TS_CLOSED 0
410Sstevel@tonic-gate #define TS_OPEN 1
420Sstevel@tonic-gate
430Sstevel@tonic-gate static int rmtstate = TS_CLOSED;
440Sstevel@tonic-gate static int rmtape = -1;
450Sstevel@tonic-gate static int rmtversion = 0;
460Sstevel@tonic-gate static char *rmtpeer, *rmtpeer_malloc;
470Sstevel@tonic-gate static uint_t ntrec; /* blocking factor on tape */
480Sstevel@tonic-gate
490Sstevel@tonic-gate static char *domainname = "hsm_libdump"; /* for dgettext() */
500Sstevel@tonic-gate
510Sstevel@tonic-gate #ifdef __STDC__
520Sstevel@tonic-gate static void rmtmsg(const char *, ...); /* package print routine */
530Sstevel@tonic-gate static void rmtconnaborted(int);
540Sstevel@tonic-gate static void rmtgetconn(void);
550Sstevel@tonic-gate static int rmtstatus_extended(struct mtget *);
560Sstevel@tonic-gate static int rmtioctl_extended(int, long);
570Sstevel@tonic-gate static int map_extended_ioctl(int);
580Sstevel@tonic-gate static int okname(char *);
590Sstevel@tonic-gate static int rmtcall(char *, char *);
600Sstevel@tonic-gate static int rmtreply(char *);
610Sstevel@tonic-gate static int rmtpush(char *, uint_t);
620Sstevel@tonic-gate static void rmtgets(char *, int);
630Sstevel@tonic-gate
640Sstevel@tonic-gate static void (*print)(const char *, ...); /* print routine */
650Sstevel@tonic-gate static void (*Exit)(int); /* exit routine */
660Sstevel@tonic-gate #else
670Sstevel@tonic-gate static void rmtmsg();
680Sstevel@tonic-gate static void rmtconnaborted();
690Sstevel@tonic-gate static void rmtgetconn();
700Sstevel@tonic-gate static int okname();
710Sstevel@tonic-gate static int rmtstatus_extended();
720Sstevel@tonic-gate static int rmtioctl_extended();
730Sstevel@tonic-gate static int map_extended_ioctl();
740Sstevel@tonic-gate static int rmtcall();
750Sstevel@tonic-gate static int rmtreply();
760Sstevel@tonic-gate static int rmtpush();
770Sstevel@tonic-gate static void rmtgets();
780Sstevel@tonic-gate
790Sstevel@tonic-gate static void (*print)();
800Sstevel@tonic-gate static void (*Exit)();
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate * Get a program-specific print and exit routine into
850Sstevel@tonic-gate * the package. This is primarily for dump's benefit.
860Sstevel@tonic-gate * This routine is optional -- if not called the two
870Sstevel@tonic-gate * default to fprintf(stderr) and exit.
880Sstevel@tonic-gate */
890Sstevel@tonic-gate #ifdef __STDC__
900Sstevel@tonic-gate void
rmtinit(void (* errmsg)(const char *,...),void (* errexit)(int))910Sstevel@tonic-gate rmtinit(
920Sstevel@tonic-gate void (*errmsg)(const char *, ...), /* print routine */
930Sstevel@tonic-gate void (*errexit)(int)) /* exit routine */
940Sstevel@tonic-gate #else
950Sstevel@tonic-gate void
96*1053Smaheshvs rmtinit(void (*errmsg)(), void (*errexit)())
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate {
990Sstevel@tonic-gate print = errmsg;
1000Sstevel@tonic-gate Exit = errexit;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
103*1053Smaheshvs int
rmthost(char * host,uint_t blocksize)104*1053Smaheshvs rmthost(char *host, uint_t blocksize)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate struct sigvec sv;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate #ifdef __STDC__
1090Sstevel@tonic-gate if (print == (void (*)(const char *, ...))0)
1100Sstevel@tonic-gate #else
1110Sstevel@tonic-gate if (print == (void (*)())0)
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate print = rmtmsg;
1140Sstevel@tonic-gate #ifdef __STDC__
1150Sstevel@tonic-gate if (Exit == (void (*)(int))0)
1160Sstevel@tonic-gate #else
1170Sstevel@tonic-gate if (Exit == (void (*)())0)
1180Sstevel@tonic-gate #endif
1190Sstevel@tonic-gate Exit = exit;
1200Sstevel@tonic-gate if (rmtape >= 0 && rmtstate != TS_OPEN) {
1210Sstevel@tonic-gate (void) close(rmtape);
1220Sstevel@tonic-gate rmtape = -1;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate if (rmtpeer_malloc)
1250Sstevel@tonic-gate (void) free(rmtpeer_malloc);
1260Sstevel@tonic-gate rmtpeer = rmtpeer_malloc = strdup(host);
1270Sstevel@tonic-gate if (rmtpeer == (char *)0)
1280Sstevel@tonic-gate return (0);
1290Sstevel@tonic-gate ntrec = blocksize;
1300Sstevel@tonic-gate sv.sa_flags = SA_RESTART;
1310Sstevel@tonic-gate (void) sigemptyset(&sv.sa_mask);
1320Sstevel@tonic-gate sv.sv_handler = rmtconnaborted;
1330Sstevel@tonic-gate (void) sigvec(SIGPIPE, &sv, (struct sigvec *)0);
1340Sstevel@tonic-gate rmtgetconn();
1350Sstevel@tonic-gate if (rmtape < 0)
1360Sstevel@tonic-gate return (0);
1370Sstevel@tonic-gate return (1);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*ARGSUSED*/
1410Sstevel@tonic-gate static void
rmtconnaborted(int sig)142*1053Smaheshvs rmtconnaborted(int sig)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate print(dgettext(domainname, "Lost connection to remote host.\n"));
1450Sstevel@tonic-gate Exit(1);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate static void
1490Sstevel@tonic-gate #ifdef __STDC__
rmtgetconn(void)1500Sstevel@tonic-gate rmtgetconn(void)
1510Sstevel@tonic-gate #else
1520Sstevel@tonic-gate rmtgetconn()
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate static struct servent *sp = 0;
1560Sstevel@tonic-gate static struct passwd *pwd = 0;
1570Sstevel@tonic-gate char *tuser, *host, *device;
1580Sstevel@tonic-gate uint_t size;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (sp == 0) {
1610Sstevel@tonic-gate sp = getservbyname("shell", "tcp");
1620Sstevel@tonic-gate if (sp == 0) {
1630Sstevel@tonic-gate print(dgettext(domainname,
1640Sstevel@tonic-gate "shell/tcp: unknown service\n"));
1650Sstevel@tonic-gate Exit(1);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate pwd = getpwuid(getuid());
1680Sstevel@tonic-gate if (pwd == 0) {
1690Sstevel@tonic-gate print(dgettext(domainname,
1700Sstevel@tonic-gate "Cannot find password entry for uid %d\n"),
1710Sstevel@tonic-gate getuid());
1720Sstevel@tonic-gate Exit(1);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate /* Was strrchr(), be consistent with dump */
1760Sstevel@tonic-gate host = strchr(rmtpeer, '@');
1770Sstevel@tonic-gate if (host) {
1780Sstevel@tonic-gate tuser = rmtpeer;
1790Sstevel@tonic-gate *host++ = 0;
1800Sstevel@tonic-gate rmtpeer = host;
1810Sstevel@tonic-gate if (!okname(tuser))
1820Sstevel@tonic-gate Exit(1);
1830Sstevel@tonic-gate } else {
1840Sstevel@tonic-gate host = rmtpeer;
1850Sstevel@tonic-gate tuser = pwd->pw_name;
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate /* Was strrchr() - be consistent with dump and restore */
1880Sstevel@tonic-gate device = strchr(host, ':');
1890Sstevel@tonic-gate if (device)
1900Sstevel@tonic-gate *device = 0; /* throw away device name */
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * myrcmd() replaces the contents of rmtpeer with a pointer
1930Sstevel@tonic-gate * to a static copy of the canonical host name. However,
1940Sstevel@tonic-gate * since we never refer to rmtpeer again (other than to
1950Sstevel@tonic-gate * overwrite it in the next rmthost() invocation), we don't
1960Sstevel@tonic-gate * really care.
1970Sstevel@tonic-gate */
1980Sstevel@tonic-gate /* LINTED sp->s_port is an int, even though port numbers are 1..65535 */
1990Sstevel@tonic-gate rmtape = myrcmd(&rmtpeer, (ushort_t)sp->s_port, pwd->pw_name,
2000Sstevel@tonic-gate tuser, "/etc/rmt");
2010Sstevel@tonic-gate if (rmtape < 0) {
2020Sstevel@tonic-gate if (*myrcmd_stderr)
2030Sstevel@tonic-gate print("%s", myrcmd_stderr);
2040Sstevel@tonic-gate } else {
2050Sstevel@tonic-gate size = ntrec * tp_bsize;
2060Sstevel@tonic-gate while (size > tp_bsize &&
2070Sstevel@tonic-gate setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, (char *)&size,
2080Sstevel@tonic-gate sizeof (size)) < 0)
2090Sstevel@tonic-gate size -= tp_bsize;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate static int
okname(char * cp0)214*1053Smaheshvs okname(char *cp0)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate char *cp;
2170Sstevel@tonic-gate uchar_t c;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate for (cp = cp0; *cp; cp++) {
2200Sstevel@tonic-gate c = (uchar_t)*cp;
2210Sstevel@tonic-gate if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
2220Sstevel@tonic-gate print(dgettext(domainname,
2230Sstevel@tonic-gate "invalid user name %s\n"), cp0);
2240Sstevel@tonic-gate return (0);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate return (1);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate
230*1053Smaheshvs int
rmtopen(char * tape,int mode)231*1053Smaheshvs rmtopen(char *tape, int mode)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate struct mtget mt;
2340Sstevel@tonic-gate char buf[256];
2350Sstevel@tonic-gate int fd;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "O%s\n%d\n", tape, mode);
2380Sstevel@tonic-gate rmtstate = TS_OPEN;
2390Sstevel@tonic-gate fd = rmtcall(tape, buf);
2400Sstevel@tonic-gate if (fd != -1) {
2410Sstevel@tonic-gate /* see if the rmt server supports the extended protocol */
2420Sstevel@tonic-gate rmtversion = rmtioctl(-1, 0);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate * Some rmt daemons apparently close the connection
2460Sstevel@tonic-gate * when they get a bogus ioctl. See 1210852 (ignore
2470Sstevel@tonic-gate * the evaluation). Make sure we can still talk to
2480Sstevel@tonic-gate * the device, re-opening it if necessary.
2490Sstevel@tonic-gate */
2500Sstevel@tonic-gate if (rmtversion < 1) {
2510Sstevel@tonic-gate if (rmtstatus(&mt) < 0) {
2520Sstevel@tonic-gate rmtclose();
2530Sstevel@tonic-gate rmtgetconn();
2540Sstevel@tonic-gate rmtversion = 0;
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate return (fd);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate void
2620Sstevel@tonic-gate #ifdef __STDC__
rmtclose(void)2630Sstevel@tonic-gate rmtclose(void)
2640Sstevel@tonic-gate #else
2650Sstevel@tonic-gate rmtclose()
2660Sstevel@tonic-gate #endif
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate if (rmtstate != TS_OPEN)
2690Sstevel@tonic-gate return;
2700Sstevel@tonic-gate (void) rmtcall("close", "C\n");
2710Sstevel@tonic-gate rmtstate = TS_CLOSED;
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
274*1053Smaheshvs int
rmtstatus(struct mtget * mt)275*1053Smaheshvs rmtstatus(struct mtget *mt)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate char *buf = (char *)mt;
2780Sstevel@tonic-gate int n, i, cc;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if (rmtversion > 0)
2810Sstevel@tonic-gate return (rmtstatus_extended(mt));
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate n = rmtcall("status", "S");
2840Sstevel@tonic-gate if (n < 0) {
2850Sstevel@tonic-gate return (-1);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate if ((unsigned)n > sizeof (*mt)) {
2880Sstevel@tonic-gate print(dgettext(domainname,
2890Sstevel@tonic-gate "rmtstatus: expected response size %d, got %d\n"),
2900Sstevel@tonic-gate sizeof (struct mtget), n);
2910Sstevel@tonic-gate print(dgettext(domainname,
2920Sstevel@tonic-gate "This means the remote rmt daemon is not compatible.\n"));
2930Sstevel@tonic-gate rmtconnaborted(0);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate i = 0;
2960Sstevel@tonic-gate while (i < n) {
2970Sstevel@tonic-gate cc = read(rmtape, buf+i, n - i);
2980Sstevel@tonic-gate if (cc <= 0)
2990Sstevel@tonic-gate rmtconnaborted(0);
3000Sstevel@tonic-gate i += cc;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate return (n);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate static int
rmtstatus_extended(struct mtget * mt)306*1053Smaheshvs rmtstatus_extended(struct mtget *mt)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate if ((mt->mt_type = rmtcall("status", "sT")) == -1)
3090Sstevel@tonic-gate return (-1);
3100Sstevel@tonic-gate mt->mt_dsreg = rmtcall("status", "sD");
3110Sstevel@tonic-gate mt->mt_erreg = rmtcall("status", "sE");
3120Sstevel@tonic-gate mt->mt_resid = rmtcall("status", "sR");
3130Sstevel@tonic-gate mt->mt_fileno = rmtcall("status", "sF");
3140Sstevel@tonic-gate mt->mt_blkno = rmtcall("status", "sB");
3150Sstevel@tonic-gate mt->mt_flags = rmtcall("status", "sf");
3160Sstevel@tonic-gate mt->mt_bf = rmtcall("status", "sb");
3170Sstevel@tonic-gate return (0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
320*1053Smaheshvs int
rmtread(char * buf,uint_t count)321*1053Smaheshvs rmtread(char *buf, uint_t count)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate char line[30];
3240Sstevel@tonic-gate int n, i, cc;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate (void) snprintf(line, sizeof (line), "R%d\n", count);
3270Sstevel@tonic-gate n = rmtcall("read", line);
3280Sstevel@tonic-gate if (n < 0) {
3290Sstevel@tonic-gate return (-1);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate if (n > count) {
3320Sstevel@tonic-gate print(dgettext(domainname,
3330Sstevel@tonic-gate "rmtread: expected response size %d, got %d\n"),
3340Sstevel@tonic-gate count, n);
3350Sstevel@tonic-gate print(dgettext(domainname,
3360Sstevel@tonic-gate "This means the remote rmt daemon is not compatible.\n"));
3370Sstevel@tonic-gate rmtconnaborted(0);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate i = 0;
3400Sstevel@tonic-gate while (i < n) {
3410Sstevel@tonic-gate cc = read(rmtape, buf+i, n - i);
3420Sstevel@tonic-gate if (cc <= 0)
3430Sstevel@tonic-gate rmtconnaborted(0);
3440Sstevel@tonic-gate i += cc;
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate return (n);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
349*1053Smaheshvs int
rmtwrite(char * buf,uint_t count)350*1053Smaheshvs rmtwrite(char *buf, uint_t count)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate int retval;
3530Sstevel@tonic-gate char line[64]; /* numbers can get big */
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate (void) snprintf(line, sizeof (line), "W%d\n", count);
3560Sstevel@tonic-gate retval = rmtpush(line, strlen(line));
3570Sstevel@tonic-gate if (retval <= 0)
3580Sstevel@tonic-gate return (-1);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate retval = rmtpush(buf, count);
3610Sstevel@tonic-gate if (retval <= 0)
3620Sstevel@tonic-gate return (-1);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate return (rmtreply("write"));
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate int
rmtpush(char * buf,uint_t count)368*1053Smaheshvs rmtpush(char *buf, uint_t count)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate int retval;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate do {
3730Sstevel@tonic-gate retval = write(rmtape, buf, count);
3740Sstevel@tonic-gate buf += retval;
3750Sstevel@tonic-gate count -= retval;
3760Sstevel@tonic-gate } while (count && retval > 0);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate return (retval);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate int
rmtseek(int offset,int pos)382*1053Smaheshvs rmtseek(int offset, int pos)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate char line[80];
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate (void) snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
3870Sstevel@tonic-gate return (rmtcall("seek", line));
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate int
rmtioctl(int cmd,long count)391*1053Smaheshvs rmtioctl(int cmd, long count)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate char buf[256];
3940Sstevel@tonic-gate int xcmd;
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate if (count < 0)
3970Sstevel@tonic-gate return (-1);
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate if ((xcmd = map_extended_ioctl(cmd)) != -1)
4000Sstevel@tonic-gate return (rmtioctl_extended(xcmd, count));
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "I%d\n%ld\n", cmd, count);
4030Sstevel@tonic-gate return (rmtcall("ioctl", buf));
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate * Map from the standard Sun ioctl commands into the extended version,
4080Sstevel@tonic-gate * if possible.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate static int
map_extended_ioctl(int cmd)411*1053Smaheshvs map_extended_ioctl(int cmd)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate int xcmd;
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate if (rmtversion <= 0)
4160Sstevel@tonic-gate return (-1); /* extended protocol not supported */
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate switch (cmd) {
4190Sstevel@tonic-gate case MTRETEN:
4200Sstevel@tonic-gate xcmd = 2;
4210Sstevel@tonic-gate break;
4220Sstevel@tonic-gate case MTERASE:
4230Sstevel@tonic-gate xcmd = 3;
4240Sstevel@tonic-gate break;
4250Sstevel@tonic-gate case MTEOM:
4260Sstevel@tonic-gate xcmd = 4;
4270Sstevel@tonic-gate break;
4280Sstevel@tonic-gate case MTNBSF:
4290Sstevel@tonic-gate xcmd = 5;
4300Sstevel@tonic-gate break;
4310Sstevel@tonic-gate default:
4320Sstevel@tonic-gate xcmd = -1; /* not supported */
4330Sstevel@tonic-gate break;
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate return (xcmd);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate static int
rmtioctl_extended(int cmd,long count)439*1053Smaheshvs rmtioctl_extended(int cmd, long count)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate char buf[256];
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "i%d\n%ld\n", cmd, count);
4440Sstevel@tonic-gate return (rmtcall("ioctl", buf));
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate static int
rmtcall(char * cmd,char * buf)448*1053Smaheshvs rmtcall(char *cmd, char *buf)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate if (rmtpush(buf, strlen(buf)) != strlen(buf))
4510Sstevel@tonic-gate rmtconnaborted(0);
4520Sstevel@tonic-gate return (rmtreply(cmd));
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate static int
rmtreply(char * cmd)456*1053Smaheshvs rmtreply(char *cmd)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate char code[30], emsg[BUFSIZ];
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate rmtgets(code, sizeof (code));
4610Sstevel@tonic-gate if (*code == 'E' || *code == 'F') {
4620Sstevel@tonic-gate rmtgets(emsg, sizeof (emsg));
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate * don't print error message for ioctl or status;
4650Sstevel@tonic-gate * or if we are opening up a full path (i.e. device)
4660Sstevel@tonic-gate * and the tape is not loaded (EIO error)
4670Sstevel@tonic-gate */
4680Sstevel@tonic-gate if (strcmp(cmd, "ioctl") != 0 &&
4690Sstevel@tonic-gate strcmp(cmd, "status") != 0 &&
4700Sstevel@tonic-gate !(cmd[0] == '/' && atoi(code + 1) == EIO))
4710Sstevel@tonic-gate print("%s: %s\n", cmd, emsg);
4720Sstevel@tonic-gate errno = atoi(code + 1);
4730Sstevel@tonic-gate if (*code == 'F') {
4740Sstevel@tonic-gate rmtstate = TS_CLOSED;
4750Sstevel@tonic-gate return (-1);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate return (-1);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate if (*code != 'A') {
4800Sstevel@tonic-gate print(dgettext(domainname,
4810Sstevel@tonic-gate "Protocol to remote tape server botched (code %s?).\n"),
4820Sstevel@tonic-gate code);
4830Sstevel@tonic-gate rmtconnaborted(0);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate return (atoi(code + 1));
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate static void
rmtgets(char * cp,int len)489*1053Smaheshvs rmtgets(char *cp, int len)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate int i, n;
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate n = recv(rmtape, cp, len-1, MSG_PEEK);
4940Sstevel@tonic-gate for (i = 0; i < n; i++)
4950Sstevel@tonic-gate if (cp[i] == '\n')
4960Sstevel@tonic-gate break;
4970Sstevel@tonic-gate n = i + 1; /* characters to read at once */
4980Sstevel@tonic-gate for (i = 0; i < len; i += n, n = 1) {
4990Sstevel@tonic-gate n = read(rmtape, cp, n);
5000Sstevel@tonic-gate if (n <= 0)
5010Sstevel@tonic-gate rmtconnaborted(0);
5020Sstevel@tonic-gate cp += n;
5030Sstevel@tonic-gate if (cp[-1] == '\n') {
5040Sstevel@tonic-gate cp[-1] = '\0';
5050Sstevel@tonic-gate return;
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate print(dgettext(domainname,
5090Sstevel@tonic-gate "Protocol to remote tape server botched (in rmtgets).\n"));
5100Sstevel@tonic-gate rmtconnaborted(0);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate #ifdef __STDC__
5140Sstevel@tonic-gate #include <stdarg.h>
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate /* VARARGS1 */
5170Sstevel@tonic-gate static void
rmtmsg(const char * fmt,...)5180Sstevel@tonic-gate rmtmsg(const char *fmt, ...)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate va_list args;
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate va_start(args, fmt);
5230Sstevel@tonic-gate (void) vfprintf(stderr, fmt, args);
5240Sstevel@tonic-gate (void) fflush(stderr);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate #else
5270Sstevel@tonic-gate #include <varargs.h>
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate /* VARARGS */
5300Sstevel@tonic-gate static void
rmtmsg(va_dcl)531*1053Smaheshvs rmtmsg(va_dcl)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate va_list args;
5340Sstevel@tonic-gate char *fmt;
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate va_start(args);
5370Sstevel@tonic-gate fmt = va_arg(args, char *);
5380Sstevel@tonic-gate (void) vfprintf(stderr, fmt, args);
5390Sstevel@tonic-gate (void) fflush(stderr);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate #endif
542