home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / sbin / dump / dumprmt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-18  |  6.0 KB  |  320 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)dumprmt.c    5.15 (Berkeley) 6/18/92";
  36. #endif /* not lint */
  37.  
  38. #ifdef sunos
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <sys/param.h>
  42. #include <sys/mtio.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/socket.h>
  45. #include <sys/stat.h>
  46. #else
  47. #include <sys/param.h>
  48. #include <sys/mtio.h>
  49. #include <sys/ioctl.h>
  50. #include <sys/socket.h>
  51. #include <sys/time.h>
  52. #include <stdio.h>
  53. #endif
  54. #include <ufs/ufs/dinode.h>
  55. #include <signal.h>
  56.  
  57. #include <netinet/in.h>
  58.  
  59. #include <netdb.h>
  60. #include <protocols/dumprestore.h>
  61. #include <pwd.h>
  62. #ifdef __STDC__
  63. #include <unistd.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #endif
  67. #include "pathnames.h"
  68.  
  69. #define    TS_CLOSED    0
  70. #define    TS_OPEN        1
  71.  
  72. static    int rmtstate = TS_CLOSED;
  73. int    rmtape;
  74. void    rmtgetconn();
  75. void    rmtconnaborted();
  76. int    rmtreply();
  77. int    rmtgetb();
  78. void    rmtgets();
  79. int    rmtcall();
  80. char    *rmtpeer;
  81.  
  82. extern int ntrec;        /* blocking factor on tape */
  83. extern void msg();
  84.  
  85. extern void exit();
  86.  
  87. int
  88. rmthost(host)
  89.     char *host;
  90. {
  91.  
  92.     rmtpeer = host;
  93.     signal(SIGPIPE, rmtconnaborted);
  94.     rmtgetconn();
  95.     if (rmtape < 0)
  96.         return (0);
  97.     return (1);
  98. }
  99.  
  100. void
  101. rmtconnaborted()
  102. {
  103.  
  104.     (void) fprintf(stderr, "rdump: Lost connection to remote host.\n");
  105.     (void) exit(1);
  106. }
  107.  
  108. void
  109. rmtgetconn()
  110. {
  111.     static struct servent *sp = 0;
  112.     struct passwd *pw;
  113.     char *name = "root";
  114.     int size;
  115.  
  116.     if (sp == 0) {
  117.         sp = getservbyname("shell", "tcp");
  118.         if (sp == 0) {
  119.             (void) fprintf(stderr,
  120.                 "rdump: shell/tcp: unknown service\n");
  121.             (void) exit(1);
  122.         }
  123.     }
  124.     pw = getpwuid(getuid());
  125.     if (pw && pw->pw_name)
  126.         name = pw->pw_name;
  127.     rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, name, name, _PATH_RMT,
  128.         (int *)0);
  129.     size = ntrec * TP_BSIZE;
  130.     while (size > TP_BSIZE &&
  131.         setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
  132.         size -= TP_BSIZE;
  133. }
  134.  
  135. int
  136. rmtopen(tape, mode)
  137.     char *tape;
  138.     int mode;
  139. {
  140.     char buf[256];
  141.  
  142.     (void)sprintf(buf, "O%s\n%d\n", tape, mode);
  143.     rmtstate = TS_OPEN;
  144.     return (rmtcall(tape, buf));
  145. }
  146.  
  147. void
  148. rmtclose()
  149. {
  150.  
  151.     if (rmtstate != TS_OPEN)
  152.         return;
  153.     rmtcall("close", "C\n");
  154.     rmtstate = TS_CLOSED;
  155. }
  156.  
  157. int
  158. rmtread(buf, count)
  159.     char *buf;
  160.     int count;
  161. {
  162.     char line[30];
  163.     int n, i, cc;
  164.     extern errno;
  165.  
  166.     (void)sprintf(line, "R%d\n", count);
  167.     n = rmtcall("read", line);
  168.     if (n < 0) {
  169.         errno = n;
  170.         return (-1);
  171.     }
  172.     for (i = 0; i < n; i += cc) {
  173.         cc = read(rmtape, buf+i, n - i);
  174.         if (cc <= 0) {
  175.             rmtconnaborted();
  176.         }
  177.     }
  178.     return (n);
  179. }
  180.  
  181. int
  182. rmtwrite(buf, count)
  183.     char *buf;
  184.     int count;
  185. {
  186.     char line[30];
  187.  
  188.     (void)sprintf(line, "W%d\n", count);
  189.     write(rmtape, line, strlen(line));
  190.     write(rmtape, buf, count);
  191.     return (rmtreply("write"));
  192. }
  193.  
  194. void
  195. rmtwrite0(count)
  196.     int count;
  197. {
  198.     char line[30];
  199.  
  200.     (void)sprintf(line, "W%d\n", count);
  201.     write(rmtape, line, strlen(line));
  202. }
  203.  
  204. void
  205. rmtwrite1(buf, count)
  206.     char *buf;
  207.     int count;
  208. {
  209.  
  210.     write(rmtape, buf, count);
  211. }
  212.  
  213. int
  214. rmtwrite2()
  215. {
  216.  
  217.     return (rmtreply("write"));
  218. }
  219.  
  220. int
  221. rmtseek(offset, pos)
  222.     int offset, pos;
  223. {
  224.     char line[80];
  225.  
  226.     (void)sprintf(line, "L%d\n%d\n", offset, pos);
  227.     return (rmtcall("seek", line));
  228. }
  229.  
  230. struct    mtget mts;
  231.  
  232. struct mtget *
  233. rmtstatus()
  234. {
  235.     register int i;
  236.     register char *cp;
  237.  
  238.     if (rmtstate != TS_OPEN)
  239.         return (0);
  240.     rmtcall("status", "S\n");
  241.     for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
  242.         *cp++ = rmtgetb();
  243.     return (&mts);
  244. }
  245.  
  246. int
  247. rmtioctl(cmd, count)
  248.     int cmd, count;
  249. {
  250.     char buf[256];
  251.  
  252.     if (count < 0)
  253.         return (-1);
  254.     (void)sprintf(buf, "I%d\n%d\n", cmd, count);
  255.     return (rmtcall("ioctl", buf));
  256. }
  257.  
  258. int
  259. rmtcall(cmd, buf)
  260.     char *cmd, *buf;
  261. {
  262.  
  263.     if (write(rmtape, buf, strlen(buf)) != strlen(buf))
  264.         rmtconnaborted();
  265.     return (rmtreply(cmd));
  266. }
  267.  
  268. int
  269. rmtreply(cmd)
  270.     char *cmd;
  271. {
  272.     char code[30], emsg[BUFSIZ];
  273.  
  274.     rmtgets(code, sizeof (code));
  275.     if (*code == 'E' || *code == 'F') {
  276.         rmtgets(emsg, sizeof (emsg));
  277.         msg("%s: %s\n", cmd, emsg, code + 1);
  278.         if (*code == 'F') {
  279.             rmtstate = TS_CLOSED;
  280.             return (-1);
  281.         }
  282.         return (-1);
  283.     }
  284.     if (*code != 'A') {
  285.         msg("Protocol to remote tape server botched (code %s?).\n",
  286.             code);
  287.         rmtconnaborted();
  288.     }
  289.     return (atoi(code + 1));
  290. }
  291.  
  292. int
  293. rmtgetb()
  294. {
  295.     char c;
  296.  
  297.     if (read(rmtape, &c, 1) != 1)
  298.         rmtconnaborted();
  299.     return (c);
  300. }
  301.  
  302. void
  303. rmtgets(cp, len)
  304.     char *cp;
  305.     int len;
  306. {
  307.  
  308.     while (len > 1) {
  309.         *cp = rmtgetb();
  310.         if (*cp == '\n') {
  311.             cp[1] = 0;
  312.             return;
  313.         }
  314.         cp++;
  315.         len--;
  316.     }
  317.     msg("Protocol to remote tape server botched (in rmtgets).\n");
  318.     rmtconnaborted();
  319. }
  320.