2 * Copyright 2006, Sine Nomine Associates and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * localhost interprocess communication for servers
13 * currently handled by a localhost socket
14 * (yes, this needs to be replaced someday)
18 #define FD_SETSIZE 65536
21 #include <afsconfig.h>
22 #include <afs/param.h>
27 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
53 #include <afs/afsint.h>
55 #include <afs/errors.h>
56 #include "daemon_com.h"
59 #include <afs/afssyscalls.h>
63 #include "partition.h"
64 #include <rx/rx_queue.h>
66 /*@printflike@*/ extern void Log(const char *format, ...);
71 #define osi_Assert(e) (void)(e)
73 int (*V_BreakVolumeCallbacks) ();
75 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
76 * move = dump+restore can run on single server */
78 #define MAX_BIND_TRIES 5 /* Number of times to retry socket bind */
80 static int getport(SYNC_client_state * state, struct sockaddr_in *addr);
81 static int SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res);
83 /* daemon com SYNC client interface */
86 SYNC_connect(SYNC_client_state * state)
88 struct sockaddr_in addr;
89 /* I can't believe the following is needed for localhost connections!! */
90 static time_t backoff[] =
91 { 3, 3, 3, 5, 5, 5, 7, 15, 16, 24, 32, 40, 48, 0 };
92 time_t *timeout = &backoff[0];
99 state->fd = getport(state, &addr);
100 if (connect(state->fd, (struct sockaddr *)&addr, sizeof(addr)) >= 0)
105 Log("SYNC_connect temporary failure (will retry)\n");
106 SYNC_disconnect(state);
109 perror("SYNC_connect failed (giving up!)");
114 SYNC_disconnect(SYNC_client_state * state)
117 closesocket(state->fd);
126 SYNC_closeChannel(SYNC_client_state * state)
131 SYNC_PROTO_BUF_DECL(ores);
136 memset(&com, 0, sizeof(com));
137 memset(&res, 0, sizeof(res));
139 res.payload.len = SYNC_PROTO_MAX_LEN;
140 res.payload.buf = ores;
142 com.hdr.command = SYNC_COM_CHANNEL_CLOSE;
143 com.hdr.command_len = sizeof(SYNC_command_hdr);
145 /* in case the other end dropped, don't do any retries */
146 state->retry_limit = 0;
147 state->hard_timeout = 0;
149 code = SYNC_ask(state, &com, &res);
151 if (code == SYNC_OK) {
152 if (res.hdr.response != SYNC_OK) {
153 Log("SYNC_closeChannel: channel shutdown request denied; closing socket anyway\n");
154 } else if (!(res.hdr.flags & SYNC_FLAG_CHANNEL_SHUTDOWN)) {
155 Log("SYNC_closeChannel: channel shutdown request mishandled by server\n");
158 Log("SYNC_closeChannel: channel communications problem");
161 SYNC_disconnect(state);
167 SYNC_reconnect(SYNC_client_state * state)
169 SYNC_disconnect(state);
170 return SYNC_connect(state);
173 /* private function to fill in the sockaddr struct for us */
175 getport(SYNC_client_state * state, struct sockaddr_in *addr)
179 memset(addr, 0, sizeof(*addr));
180 assert((sd = socket(AF_INET, SOCK_STREAM, 0)) >= 0);
181 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
182 addr->sin_len = sizeof(struct sockaddr_in);
184 addr->sin_addr.s_addr = htonl(0x7f000001);
185 addr->sin_family = AF_INET; /* was localhost->h_addrtype */
186 addr->sin_port = htons(state->port); /* XXXX htons not _really_ neccessary */
192 SYNC_ask(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
195 afs_uint32 now, timeout, code=SYNC_OK;
197 if (state->fatal_error) {
198 return SYNC_COM_ERROR;
201 if (state->fd == -1) {
205 if (state->fd == -1) {
206 state->fatal_error = 1;
207 return SYNC_COM_ERROR;
210 #ifdef AFS_DEMAND_ATTACH_FS
211 com->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
214 now = FT_ApproxTime();
215 timeout = now + state->hard_timeout;
217 (tries <= state->retry_limit) && (now <= timeout);
218 tries++, now = FT_ApproxTime()) {
219 code = SYNC_ask_internal(state, com, res);
220 if (code == SYNC_OK) {
222 } else if (code == SYNC_BAD_COMMAND) {
223 Log("SYNC_ask: protocol mismatch; make sure fileserver, volserver, salvageserver and salvager are same version\n");
225 } else if (code == SYNC_COM_ERROR) {
226 Log("SYNC_ask: protocol communications failure; attempting reconnect to server\n");
227 SYNC_reconnect(state);
230 /* unknown (probably protocol-specific) response code, pass it up to the caller, and let them deal with it */
235 if (code == SYNC_COM_ERROR) {
236 Log("SYNC_ask: fatal protocol error; disabling sync protocol to server running on port %d until next server restart\n",
238 state->fatal_error = 1;
245 SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
248 SYNC_PROTO_BUF_DECL(buf);
254 if (state->fd == -1) {
255 Log("SYNC_ask: invalid sync file descriptor\n");
256 res->hdr.response = SYNC_COM_ERROR;
260 if (com->hdr.command_len > SYNC_PROTO_MAX_LEN) {
261 Log("SYNC_ask: internal SYNC buffer too small; please file a bug\n");
262 res->hdr.response = SYNC_COM_ERROR;
266 com->hdr.proto_version = state->proto_version;
268 memcpy(buf, &com->hdr, sizeof(com->hdr));
269 if (com->payload.len) {
270 memcpy(buf + sizeof(com->hdr), com->payload.buf,
271 com->hdr.command_len - sizeof(com->hdr));
275 n = send(state->fd, buf, com->hdr.command_len, 0);
276 if (n != com->hdr.command_len) {
277 Log("SYNC_ask: write failed\n");
278 res->hdr.response = SYNC_COM_ERROR;
282 n = recv(state->fd, buf, SYNC_PROTO_MAX_LEN, 0);
283 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
284 Log("SYNC_ask: No response\n");
285 res->hdr.response = SYNC_COM_ERROR;
288 #else /* !AFS_NT40_ENV */
289 n = write(state->fd, buf, com->hdr.command_len);
290 if (com->hdr.command_len != n) {
291 Log("SYNC_ask: write failed\n");
292 res->hdr.response = SYNC_COM_ERROR;
296 /* receive the response */
297 iov[0].iov_base = (char *)&res->hdr;
298 iov[0].iov_len = sizeof(res->hdr);
299 if (res->payload.len) {
300 iov[1].iov_base = (char *)res->payload.buf;
301 iov[1].iov_len = res->payload.len;
306 n = readv(state->fd, iov, iovcnt);
307 if (n == 0 || (n < 0 && errno != EINTR)) {
308 Log("SYNC_ask: No response\n");
309 res->hdr.response = SYNC_COM_ERROR;
312 #endif /* !AFS_NT40_ENV */
316 if (n < sizeof(res->hdr)) {
317 Log("SYNC_ask: response too short\n");
318 res->hdr.response = SYNC_COM_ERROR;
322 memcpy(&res->hdr, buf, sizeof(res->hdr));
325 if ((n - sizeof(res->hdr)) > res->payload.len) {
326 Log("SYNC_ask: response too long\n");
327 res->hdr.response = SYNC_COM_ERROR;
331 memcpy(res->payload.buf, buf + sizeof(res->hdr), n - sizeof(res->hdr));
334 if (res->hdr.response_len != n) {
335 Log("SYNC_ask: length field in response inconsistent\n");
336 res->hdr.response = SYNC_COM_ERROR;
339 if (res->hdr.response == SYNC_DENIED) {
340 Log("SYNC_ask: negative response\n");
344 return res->hdr.response;
349 * daemon com SYNC server-side interfaces
354 SYNC_getCom(int fd, SYNC_command * com)
357 afs_int32 code = SYNC_OK;
359 SYNC_PROTO_BUF_DECL(buf);
366 n = recv(fd, buf, SYNC_PROTO_MAX_LEN, 0);
368 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
369 Log("SYNC_getCom: error receiving command\n");
370 code = SYNC_COM_ERROR;
373 #else /* !AFS_NT40_ENV */
374 iov[0].iov_base = (char *)&com->hdr;
375 iov[0].iov_len = sizeof(com->hdr);
376 if (com->payload.len) {
377 iov[1].iov_base = (char *)com->payload.buf;
378 iov[1].iov_len = com->payload.len;
384 n = readv(fd, iov, iovcnt);
385 if (n == 0 || (n < 0 && errno != EINTR)) {
386 Log("SYNC_getCom: error receiving command\n");
387 code = SYNC_COM_ERROR;
390 #endif /* !AFS_NT40_ENV */
394 if (n < sizeof(com->hdr)) {
395 Log("SYNC_getCom: command too short\n");
396 code = SYNC_COM_ERROR;
400 memcpy(&com->hdr, buf, sizeof(com->hdr));
403 if ((n - sizeof(com->hdr)) > com->payload.len) {
404 Log("SYNC_getCom: command too long\n");
405 code = SYNC_COM_ERROR;
409 memcpy(com->payload.buf, buf + sizeof(com->hdr), n - sizeof(com->hdr));
418 SYNC_putRes(int fd, SYNC_response * res)
421 afs_int32 code = SYNC_OK;
422 SYNC_PROTO_BUF_DECL(buf);
424 if (res->hdr.response_len > (sizeof(res->hdr) + res->payload.len)) {
425 Log("SYNC_putRes: response_len field in response header inconsistent\n");
426 code = SYNC_COM_ERROR;
430 if (res->hdr.response_len > SYNC_PROTO_MAX_LEN) {
431 Log("SYNC_putRes: internal SYNC buffer too small; please file a bug\n");
432 code = SYNC_COM_ERROR;
436 #ifdef AFS_DEMAND_ATTACH_FS
437 res->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
440 memcpy(buf, &res->hdr, sizeof(res->hdr));
441 if (res->payload.len) {
442 memcpy(buf + sizeof(res->hdr), res->payload.buf,
443 res->hdr.response_len - sizeof(res->hdr));
447 n = send(fd, buf, res->hdr.response_len, 0);
448 #else /* !AFS_NT40_ENV */
449 n = write(fd, buf, res->hdr.response_len);
450 #endif /* !AFS_NT40_ENV */
452 if (res->hdr.response_len != n) {
453 Log("SYNC_putRes: write failed\n");
454 res->hdr.response = SYNC_COM_ERROR;
462 /* return 0 for legal (null-terminated) string,
463 * 1 for illegal (unterminated) string */
465 SYNC_verifyProtocolString(char * buf, size_t len)
470 s_len = afs_strnlen(buf, len);
472 return (s_len == len) ? 1 : 0;