2 * Copyright 2006-2008, 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>
25 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
45 #include <afs/afsint.h>
47 #include <afs/errors.h>
48 #include "daemon_com.h"
51 #include <afs/afssyscalls.h>
55 #include "partition.h"
57 #include <rx/rx_queue.h>
59 #ifdef USE_UNIX_SOCKETS
60 #include <afs/afsutil.h>
64 int (*V_BreakVolumeCallbacks) (VolumeId);
66 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
67 * move = dump+restore can run on single server */
69 #define MAX_BIND_TRIES 5 /* Number of times to retry socket bind */
71 static int SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res);
75 * On AIX, connect() and bind() require use of SUN_LEN() macro;
76 * sizeof(struct sockaddr_un) will not suffice.
78 #if defined(AFS_AIX_ENV) && defined(USE_UNIX_SOCKETS)
79 #define AFS_SOCKADDR_LEN(sa) SUN_LEN(sa)
81 #define AFS_SOCKADDR_LEN(sa) sizeof(*sa)
85 /* daemon com SYNC general interfaces */
88 * fill in sockaddr structure.
90 * @param[in] endpoint pointer to sync endpoint object
91 * @param[out] addr pointer to sockaddr structure
93 * @post sockaddr structure populated using information from
97 SYNC_getAddr(SYNC_endpoint_t * endpoint, SYNC_sockaddr_t * addr)
99 #ifdef USE_UNIX_SOCKETS
100 char tbuffer[AFSDIR_PATH_MAX];
101 #endif /* USE_UNIX_SOCKETS */
103 memset(addr, 0, sizeof(*addr));
105 #ifdef USE_UNIX_SOCKETS
106 strcompose(tbuffer, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH, "/",
108 addr->sun_family = AF_UNIX;
109 strncpy(addr->sun_path, tbuffer, (sizeof(struct sockaddr_un) - sizeof(short)));
110 #else /* !USE_UNIX_SOCKETS */
111 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
112 addr->sin_len = sizeof(struct sockaddr_in);
114 addr->sin_addr.s_addr = htonl(0x7f000001);
115 addr->sin_family = AF_INET; /* was localhost->h_addrtype */
116 addr->sin_port = htons(endpoint->in); /* XXXX htons not _really_ neccessary */
117 #endif /* !USE_UNIX_SOCKETS */
121 * get a socket descriptor of the appropriate domain.
123 * @param[in] endpoint pointer to sync endpoint object
125 * @return socket descriptor
127 * @post socket of domain specified in endpoint structure is created and
128 * returned to caller.
131 SYNC_getSock(SYNC_endpoint_t * endpoint)
134 assert((sd = socket(endpoint->domain, SOCK_STREAM, 0)) >= 0);
138 /* daemon com SYNC client interface */
141 * open a client connection to a sync server
143 * @param[in] state pointer to sync client handle
145 * @return operation status
148 * @note at present, this routine aborts rather than returning an error code
151 SYNC_connect(SYNC_client_state * state)
153 SYNC_sockaddr_t addr;
154 /* I can't believe the following is needed for localhost connections!! */
155 static time_t backoff[] =
156 { 3, 3, 3, 5, 5, 5, 7, 15, 16, 24, 32, 40, 48, 0 };
157 time_t *timeout = &backoff[0];
159 if (state->fd >= 0) {
163 SYNC_getAddr(&state->endpoint, &addr);
166 state->fd = SYNC_getSock(&state->endpoint);
167 if (connect(state->fd, (struct sockaddr *)&addr, AFS_SOCKADDR_LEN(&addr)) >= 0)
172 Log("SYNC_connect: temporary failure on circuit '%s' (will retry)\n",
174 SYNC_disconnect(state);
177 perror("SYNC_connect failed (giving up!)");
182 * forcibly disconnect a sync client handle.
184 * @param[in] state pointer to sync client handle
186 * @retval operation status
190 SYNC_disconnect(SYNC_client_state * state)
193 closesocket(state->fd);
202 * gracefully disconnect a sync client handle.
204 * @param[in] state pointer to sync client handle
206 * @return operation status
207 * @retval SYNC_OK success
210 SYNC_closeChannel(SYNC_client_state * state)
214 SYNC_PROTO_BUF_DECL(ores);
219 memset(&com, 0, sizeof(com));
220 memset(&res, 0, sizeof(res));
222 res.payload.len = SYNC_PROTO_MAX_LEN;
223 res.payload.buf = ores;
225 com.hdr.command = SYNC_COM_CHANNEL_CLOSE;
226 com.hdr.command_len = sizeof(SYNC_command_hdr);
227 com.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
229 /* in case the other end dropped, don't do any retries */
230 state->retry_limit = 0;
231 state->hard_timeout = 0;
233 SYNC_ask(state, &com, &res);
234 SYNC_disconnect(state);
240 * forcibly break a client connection, and then create a new connection.
242 * @param[in] state pointer to sync client handle
244 * @post old connection dropped; new connection established
246 * @return @see SYNC_connect()
249 SYNC_reconnect(SYNC_client_state * state)
251 SYNC_disconnect(state);
252 return SYNC_connect(state);
256 * send a command to a sync server and wait for a response.
258 * @param[in] state pointer to sync client handle
259 * @param[in] com command object
260 * @param[out] res response object
262 * @return operation status
263 * @retval SYNC_OK success
264 * @retval SYNC_COM_ERROR communications error
265 * @retval SYNC_BAD_COMMAND server did not recognize command code
267 * @note this routine merely handles error processing; SYNC_ask_internal()
268 * handles the low-level details of communicating with the SYNC server.
270 * @see SYNC_ask_internal
273 SYNC_ask(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
276 afs_uint32 now, timeout, code=SYNC_OK;
278 if (state->fatal_error) {
279 return SYNC_COM_ERROR;
282 if (state->fd == -1) {
286 if (state->fd == -1) {
287 state->fatal_error = 1;
288 return SYNC_COM_ERROR;
291 #ifdef AFS_DEMAND_ATTACH_FS
292 com->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
295 now = FT_ApproxTime();
296 timeout = now + state->hard_timeout;
298 (tries <= state->retry_limit) && (now <= timeout);
299 tries++, now = FT_ApproxTime()) {
300 code = SYNC_ask_internal(state, com, res);
301 if (code == SYNC_OK) {
303 } else if (code == SYNC_BAD_COMMAND) {
304 Log("SYNC_ask: protocol mismatch on circuit '%s'; make sure "
305 "fileserver, volserver, salvageserver and salvager are same "
306 "version\n", state->proto_name);
308 } else if ((code == SYNC_COM_ERROR) && (tries < state->retry_limit)) {
309 Log("SYNC_ask: protocol communications failure on circuit '%s'; "
310 "attempting reconnect to server\n", state->proto_name);
311 SYNC_reconnect(state);
315 * unknown (probably protocol-specific) response code, pass it up to
316 * the caller, and let them deal with it
322 if (code == SYNC_COM_ERROR) {
323 Log("SYNC_ask: fatal protocol error on circuit '%s'; disabling sync "
324 "protocol until next server restart\n",
326 state->fatal_error = 1;
333 * send a command to a sync server and wait for a response.
335 * @param[in] state pointer to sync client handle
336 * @param[in] com command object
337 * @param[out] res response object
339 * @return operation status
340 * @retval SYNC_OK success
341 * @retval SYNC_COM_ERROR communications error
346 SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
349 SYNC_PROTO_BUF_DECL(buf);
355 if (state->fd == -1) {
356 Log("SYNC_ask: invalid sync file descriptor on circuit '%s'\n",
358 res->hdr.response = SYNC_COM_ERROR;
362 if (com->hdr.command_len > SYNC_PROTO_MAX_LEN) {
363 Log("SYNC_ask: internal SYNC buffer too small on circuit '%s'; "
364 "please file a bug\n", state->proto_name);
365 res->hdr.response = SYNC_COM_ERROR;
370 * fill in some common header fields
372 com->hdr.proto_version = state->proto_version;
373 com->hdr.pkt_seq = ++state->pkt_seq;
374 com->hdr.com_seq = ++state->com_seq;
379 com->hdr.pid = getpid();
380 #ifdef AFS_PTHREAD_ENV
381 com->hdr.tid = afs_pointer_to_int(pthread_self());
384 PROCESS handle = LWP_ThreadId();
385 com->hdr.tid = (handle) ? handle->index : 0;
387 #endif /* !AFS_PTHREAD_ENV */
388 #endif /* !AFS_NT40_ENV */
390 memcpy(buf, &com->hdr, sizeof(com->hdr));
391 if (com->payload.len) {
392 memcpy(buf + sizeof(com->hdr), com->payload.buf,
393 com->hdr.command_len - sizeof(com->hdr));
397 n = send(state->fd, buf, com->hdr.command_len, 0);
398 if (n != com->hdr.command_len) {
399 Log("SYNC_ask: write failed on circuit '%s'\n", state->proto_name);
400 res->hdr.response = SYNC_COM_ERROR;
404 if (com->hdr.command == SYNC_COM_CHANNEL_CLOSE) {
405 /* short circuit close channel requests */
406 res->hdr.response = SYNC_OK;
410 n = recv(state->fd, buf, SYNC_PROTO_MAX_LEN, 0);
411 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
412 Log("SYNC_ask: No response on circuit '%s'\n", state->proto_name);
413 res->hdr.response = SYNC_COM_ERROR;
416 #else /* !AFS_NT40_ENV */
417 n = write(state->fd, buf, com->hdr.command_len);
418 if (com->hdr.command_len != n) {
419 Log("SYNC_ask: write failed on circuit '%s'\n", state->proto_name);
420 res->hdr.response = SYNC_COM_ERROR;
424 if (com->hdr.command == SYNC_COM_CHANNEL_CLOSE) {
425 /* short circuit close channel requests */
426 res->hdr.response = SYNC_OK;
430 /* receive the response */
431 iov[0].iov_base = (char *)&res->hdr;
432 iov[0].iov_len = sizeof(res->hdr);
433 if (res->payload.len) {
434 iov[1].iov_base = (char *)res->payload.buf;
435 iov[1].iov_len = res->payload.len;
440 n = readv(state->fd, iov, iovcnt);
441 if (n == 0 || (n < 0 && errno != EINTR)) {
442 Log("SYNC_ask: No response on circuit '%s'\n", state->proto_name);
443 res->hdr.response = SYNC_COM_ERROR;
446 #endif /* !AFS_NT40_ENV */
450 if (n < sizeof(res->hdr)) {
451 Log("SYNC_ask: response too short on circuit '%s'\n",
453 res->hdr.response = SYNC_COM_ERROR;
457 memcpy(&res->hdr, buf, sizeof(res->hdr));
460 if ((n - sizeof(res->hdr)) > res->payload.len) {
461 Log("SYNC_ask: response too long on circuit '%s'\n",
463 res->hdr.response = SYNC_COM_ERROR;
467 memcpy(res->payload.buf, buf + sizeof(res->hdr), n - sizeof(res->hdr));
470 if (res->hdr.response_len != n) {
471 Log("SYNC_ask: length field in response inconsistent "
472 "on circuit '%s'\n", state->proto_name);
473 res->hdr.response = SYNC_COM_ERROR;
476 if (res->hdr.response == SYNC_DENIED) {
477 Log("SYNC_ask: negative response on circuit '%s'\n", state->proto_name);
481 return res->hdr.response;
486 * daemon com SYNC server-side interfaces
490 * receive a command structure off a sync socket.
492 * @param[in] state pointer to server-side state object
493 * @param[in] fd file descriptor on which to perform i/o
494 * @param[out] com sync command object to be populated
496 * @return operation status
497 * @retval SYNC_OK command received
498 * @retval SYNC_COM_ERROR there was a socket communications error
501 SYNC_getCom(SYNC_server_state_t * state,
506 afs_int32 code = SYNC_OK;
508 SYNC_PROTO_BUF_DECL(buf);
515 n = recv(fd, buf, SYNC_PROTO_MAX_LEN, 0);
517 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
518 Log("SYNC_getCom: error receiving command\n");
519 code = SYNC_COM_ERROR;
522 #else /* !AFS_NT40_ENV */
523 iov[0].iov_base = (char *)&com->hdr;
524 iov[0].iov_len = sizeof(com->hdr);
525 if (com->payload.len) {
526 iov[1].iov_base = (char *)com->payload.buf;
527 iov[1].iov_len = com->payload.len;
533 n = readv(fd, iov, iovcnt);
534 if (n == 0 || (n < 0 && errno != EINTR)) {
535 Log("SYNC_getCom: error receiving command\n");
536 code = SYNC_COM_ERROR;
539 #endif /* !AFS_NT40_ENV */
543 if (n < sizeof(com->hdr)) {
544 Log("SYNC_getCom: command too short\n");
545 code = SYNC_COM_ERROR;
549 memcpy(&com->hdr, buf, sizeof(com->hdr));
552 if ((n - sizeof(com->hdr)) > com->payload.len) {
553 Log("SYNC_getCom: command too long\n");
554 code = SYNC_COM_ERROR;
558 memcpy(com->payload.buf, buf + sizeof(com->hdr), n - sizeof(com->hdr));
566 * write a response structure to a sync socket.
568 * @param[in] state handle to server-side state object
569 * @param[in] fd file descriptor on which to perform i/o
570 * @param[in] res handle to response packet
572 * @return operation status
574 * @retval SYNC_COM_ERROR
577 SYNC_putRes(SYNC_server_state_t * state,
582 afs_int32 code = SYNC_OK;
583 SYNC_PROTO_BUF_DECL(buf);
585 if (res->hdr.response_len > (sizeof(res->hdr) + res->payload.len)) {
586 Log("SYNC_putRes: response_len field in response header inconsistent\n");
587 code = SYNC_COM_ERROR;
591 if (res->hdr.response_len > SYNC_PROTO_MAX_LEN) {
592 Log("SYNC_putRes: internal SYNC buffer too small; please file a bug\n");
593 code = SYNC_COM_ERROR;
597 #ifdef AFS_DEMAND_ATTACH_FS
598 res->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
600 res->hdr.proto_version = state->proto_version;
601 res->hdr.pkt_seq = ++state->pkt_seq;
602 res->hdr.res_seq = ++state->res_seq;
604 memcpy(buf, &res->hdr, sizeof(res->hdr));
605 if (res->payload.len) {
606 memcpy(buf + sizeof(res->hdr), res->payload.buf,
607 res->hdr.response_len - sizeof(res->hdr));
611 n = send(fd, buf, res->hdr.response_len, 0);
612 #else /* !AFS_NT40_ENV */
613 n = write(fd, buf, res->hdr.response_len);
614 #endif /* !AFS_NT40_ENV */
616 if (res->hdr.response_len != n) {
617 Log("SYNC_putRes: write failed\n");
618 res->hdr.response = SYNC_COM_ERROR;
626 /* return 0 for legal (null-terminated) string,
627 * 1 for illegal (unterminated) string */
629 SYNC_verifyProtocolString(char * buf, size_t len)
633 s_len = afs_strnlen(buf, len);
635 return (s_len == len) ? 1 : 0;
639 * clean up old sockets.
641 * @param[in] state server state object
643 * @post unix domain sockets are cleaned up
646 SYNC_cleanupSock(SYNC_server_state_t * state)
648 #ifdef USE_UNIX_SOCKETS
649 remove(state->addr.sun_path);
654 * bind socket and set it to listen state.
656 * @param[in] state server state object
658 * @return operation status
660 * @retval nonzero failure
662 * @post socket bound and set to listen state
665 SYNC_bindSock(SYNC_server_state_t * state)
671 /* Reuseaddr needed because system inexplicably leaves crud lying around */
673 setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
676 Log("SYNC_bindSock: setsockopt failed with (%d)\n", errno);
678 for (numTries = 0; numTries < state->bind_retry_limit; numTries++) {
679 code = bind(state->fd,
680 (struct sockaddr *)&state->addr,
681 AFS_SOCKADDR_LEN(&state->addr));
684 Log("SYNC_bindSock: bind failed with (%d), will sleep and retry\n",
688 listen(state->fd, state->listen_depth);