mcfsutil.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Low-level protocol for MCFS.
  2. Copyright (C) 1995, 1996 Miguel de Icaza
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License
  5. as published by the Free Software Foundation; either version 2 of
  6. the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. #ifdef WITH_MCFS
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include <signal.h>
  21. #include <pwd.h>
  22. #include <sys/types.h>
  23. #include <netdb.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #ifdef HAVE_ARPA_INET_H
  27. #include <arpa/inet.h>
  28. #endif
  29. #ifdef HAVE_PMAP_SET
  30. #include <rpc/rpc.h>
  31. #include <rpc/pmap_prot.h>
  32. #ifdef HAVE_RPC_PMAP_CLNT_H
  33. #include <rpc/pmap_clnt.h>
  34. #endif
  35. #endif
  36. #include <errno.h>
  37. #include "../src/global.h"
  38. #include "../src/tty.h" /* enable/disable interrupt key */
  39. #include "../src/wtools.h" /* message() */
  40. #include "../src/main.h" /* print_vfs_message */
  41. #include "utilvfs.h"
  42. #include "mcfsutil.h"
  43. #include "tcputil.h"
  44. #include "mcfs.h" /* tcp_invalidate_socket() */
  45. #define CHECK_SIG_PIPE(sock) if (got_sigpipe) \
  46. { tcp_invalidate_socket (sock); return got_sigpipe = 0; }
  47. /* Reads a block on dest for len bytes from sock */
  48. /* Returns a boolean indicating the success status */
  49. int
  50. socket_read_block (int sock, char *dest, int len)
  51. {
  52. int nread, n;
  53. for (nread = 0; nread < len;) {
  54. n = read (sock, dest + nread, len - nread);
  55. if (n <= 0) {
  56. tcp_invalidate_socket (sock);
  57. return 0;
  58. }
  59. nread += n;
  60. }
  61. return 1;
  62. }
  63. int
  64. socket_write_block (int sock, const char *buffer, int len)
  65. {
  66. int left, status;
  67. for (left = len; left > 0;) {
  68. status = write (sock, buffer, left);
  69. CHECK_SIG_PIPE (sock);
  70. if (status < 0)
  71. return 0;
  72. left -= status;
  73. buffer += status;
  74. }
  75. return 1;
  76. }
  77. int
  78. rpc_send (int sock, ...)
  79. {
  80. long int tmp, len, cmd;
  81. char *text;
  82. va_list ap;
  83. va_start (ap, sock);
  84. for (;;) {
  85. cmd = va_arg (ap, int);
  86. switch (cmd) {
  87. case RPC_END:
  88. va_end (ap);
  89. return 1;
  90. case RPC_INT:
  91. tmp = htonl (va_arg (ap, int));
  92. write (sock, &tmp, sizeof (tmp));
  93. CHECK_SIG_PIPE (sock);
  94. break;
  95. case RPC_STRING:
  96. text = va_arg (ap, char *);
  97. len = strlen (text);
  98. tmp = htonl (len);
  99. write (sock, &tmp, sizeof (tmp));
  100. CHECK_SIG_PIPE (sock);
  101. write (sock, text, len);
  102. CHECK_SIG_PIPE (sock);
  103. break;
  104. case RPC_BLOCK:
  105. len = va_arg (ap, int);
  106. text = va_arg (ap, char *);
  107. tmp = htonl (len);
  108. write (sock, text, len);
  109. CHECK_SIG_PIPE (sock);
  110. break;
  111. default:
  112. vfs_die ("Unknown rpc message\n");
  113. }
  114. }
  115. }
  116. int
  117. rpc_get (int sock, ...)
  118. {
  119. long int tmp, len;
  120. char *text, **str_dest;
  121. int *dest, cmd;
  122. va_list ap;
  123. va_start (ap, sock);
  124. for (;;) {
  125. cmd = va_arg (ap, int);
  126. switch (cmd) {
  127. case RPC_END:
  128. va_end (ap);
  129. return 1;
  130. case RPC_INT:
  131. if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0) {
  132. va_end (ap);
  133. return 0;
  134. }
  135. dest = va_arg (ap, int *);
  136. *dest = ntohl (tmp);
  137. break;
  138. /* returns an allocated string */
  139. case RPC_LIMITED_STRING:
  140. case RPC_STRING:
  141. if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0) {
  142. va_end (ap);
  143. return 0;
  144. }
  145. len = ntohl (tmp);
  146. if (cmd == RPC_LIMITED_STRING)
  147. if (len > 16 * 1024) {
  148. /* silently die */
  149. abort ();
  150. }
  151. if (len > 128 * 1024)
  152. abort ();
  153. /* Don't use glib functions here - this code is used by mcserv */
  154. text = malloc (len + 1);
  155. if (socket_read_block (sock, text, len) == 0) {
  156. free (text);
  157. va_end (ap);
  158. return 0;
  159. }
  160. text[len] = '\0';
  161. str_dest = va_arg (ap, char **);
  162. *str_dest = text;
  163. break;
  164. case RPC_BLOCK:
  165. len = va_arg (ap, int);
  166. text = va_arg (ap, char *);
  167. if (socket_read_block (sock, text, len) == 0) {
  168. va_end (ap);
  169. return 0;
  170. }
  171. break;
  172. default:
  173. vfs_die ("Unknown rpc message\n");
  174. }
  175. }
  176. }
  177. #else
  178. void mcfsutil__unused(void)
  179. {
  180. /*
  181. CFLAGS="-ansi -pedantic -Wall -Wextra -Werror"
  182. */
  183. }
  184. #endif /* WITH_MCFS */