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>
28 #include <afs/afsint.h>
29 #include <afs/errors.h>
32 #include "daemon_com.h"
35 #include <afs/afssyscalls.h>
39 #include "partition.h"
41 #include <rx/rx_queue.h>
43 #ifdef USE_UNIX_SOCKETS
44 #include <afs/afsutil.h>
48 int (*V_BreakVolumeCallbacks) (VolumeId);
50 #define MAXHANDLERS 4 /* Up to 4 clients; must be at least 2, so that
51 * move = dump+restore can run on single server */
53 #define MAX_BIND_TRIES 5 /* Number of times to retry socket bind */
55 static int SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res);
59 * On AIX, connect() and bind() require use of SUN_LEN() macro;
60 * sizeof(struct sockaddr_un) will not suffice.
62 #if defined(AFS_AIX_ENV) && defined(USE_UNIX_SOCKETS)
63 #define AFS_SOCKADDR_LEN(sa) SUN_LEN(sa)
65 #define AFS_SOCKADDR_LEN(sa) sizeof(*sa)
69 /* daemon com SYNC general interfaces */
72 * fill in sockaddr structure.
74 * @param[in] endpoint pointer to sync endpoint object
75 * @param[out] addr pointer to sockaddr structure
77 * @post sockaddr structure populated using information from
81 SYNC_getAddr(SYNC_endpoint_t * endpoint, SYNC_sockaddr_t * addr)
83 #ifdef USE_UNIX_SOCKETS
84 char tbuffer[AFSDIR_PATH_MAX];
85 #endif /* USE_UNIX_SOCKETS */
87 memset(addr, 0, sizeof(*addr));
89 #ifdef USE_UNIX_SOCKETS
90 strcompose(tbuffer, AFSDIR_PATH_MAX, AFSDIR_SERVER_LOCAL_DIRPATH, "/",
92 addr->sun_family = AF_UNIX;
93 strncpy(addr->sun_path, tbuffer, (sizeof(struct sockaddr_un) - sizeof(short)));
94 #else /* !USE_UNIX_SOCKETS */
95 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
96 addr->sin_len = sizeof(struct sockaddr_in);
98 addr->sin_addr.s_addr = htonl(0x7f000001);
99 addr->sin_family = AF_INET; /* was localhost->h_addrtype */
100 addr->sin_port = htons(endpoint->in); /* XXXX htons not _really_ neccessary */
101 #endif /* !USE_UNIX_SOCKETS */
105 * get a socket descriptor of the appropriate domain.
107 * @param[in] endpoint pointer to sync endpoint object
109 * @return socket descriptor
111 * @post socket of domain specified in endpoint structure is created and
112 * returned to caller.
115 SYNC_getSock(SYNC_endpoint_t * endpoint)
118 osi_Assert((sd = socket(endpoint->domain, SOCK_STREAM, 0)) >= 0);
122 /* daemon com SYNC client interface */
125 * open a client connection to a sync server
127 * @param[in] state pointer to sync client handle
129 * @return operation status
132 * @note at present, this routine aborts rather than returning an error code
135 SYNC_connect(SYNC_client_state * state)
137 SYNC_sockaddr_t addr;
138 /* I can't believe the following is needed for localhost connections!! */
139 static time_t backoff[] =
140 { 3, 3, 3, 5, 5, 5, 7, 15, 16, 24, 32, 40, 48, 0 };
141 time_t *timeout = &backoff[0];
143 if (state->fd != OSI_NULLSOCKET) {
147 SYNC_getAddr(&state->endpoint, &addr);
150 state->fd = SYNC_getSock(&state->endpoint);
151 if (connect(state->fd, (struct sockaddr *)&addr, AFS_SOCKADDR_LEN(&addr)) >= 0)
156 Log("SYNC_connect: temporary failure on circuit '%s' (will retry)\n",
158 SYNC_disconnect(state);
161 perror("SYNC_connect failed (giving up!)");
166 * forcibly disconnect a sync client handle.
168 * @param[in] state pointer to sync client handle
170 * @retval operation status
174 SYNC_disconnect(SYNC_client_state * state)
176 rk_closesocket(state->fd);
177 state->fd = OSI_NULLSOCKET;
182 * gracefully disconnect a sync client handle.
184 * @param[in] state pointer to sync client handle
186 * @return operation status
187 * @retval SYNC_OK success
190 SYNC_closeChannel(SYNC_client_state * state)
194 SYNC_PROTO_BUF_DECL(ores);
196 if (state->fd == OSI_NULLSOCKET)
199 memset(&com, 0, sizeof(com));
200 memset(&res, 0, sizeof(res));
202 res.payload.len = SYNC_PROTO_MAX_LEN;
203 res.payload.buf = ores;
205 com.hdr.command = SYNC_COM_CHANNEL_CLOSE;
206 com.hdr.command_len = sizeof(SYNC_command_hdr);
207 com.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
209 /* in case the other end dropped, don't do any retries */
210 state->retry_limit = 0;
211 state->hard_timeout = 0;
213 SYNC_ask(state, &com, &res);
214 SYNC_disconnect(state);
220 * forcibly break a client connection, and then create a new connection.
222 * @param[in] state pointer to sync client handle
224 * @post old connection dropped; new connection established
226 * @return @see SYNC_connect()
229 SYNC_reconnect(SYNC_client_state * state)
231 SYNC_disconnect(state);
232 return SYNC_connect(state);
236 * send a command to a sync server and wait for a response.
238 * @param[in] state pointer to sync client handle
239 * @param[in] com command object
240 * @param[out] res response object
242 * @return operation status
243 * @retval SYNC_OK success
244 * @retval SYNC_COM_ERROR communications error
245 * @retval SYNC_BAD_COMMAND server did not recognize command code
247 * @note this routine merely handles error processing; SYNC_ask_internal()
248 * handles the low-level details of communicating with the SYNC server.
250 * @see SYNC_ask_internal
253 SYNC_ask(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
256 afs_uint32 now, timeout, code=SYNC_OK;
258 if (state->fatal_error) {
259 return SYNC_COM_ERROR;
262 if (state->fd == OSI_NULLSOCKET) {
266 if (state->fd == OSI_NULLSOCKET) {
267 state->fatal_error = 1;
268 return SYNC_COM_ERROR;
271 #ifdef AFS_DEMAND_ATTACH_FS
272 com->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
275 now = FT_ApproxTime();
276 timeout = now + state->hard_timeout;
278 (tries <= state->retry_limit) && (now <= timeout);
279 tries++, now = FT_ApproxTime()) {
280 code = SYNC_ask_internal(state, com, res);
281 if (code == SYNC_OK) {
283 } else if (code == SYNC_BAD_COMMAND) {
284 Log("SYNC_ask: protocol mismatch on circuit '%s'; make sure "
285 "fileserver, volserver, salvageserver and salvager are same "
286 "version\n", state->proto_name);
288 } else if ((code == SYNC_COM_ERROR) && (tries < state->retry_limit)) {
289 Log("SYNC_ask: protocol communications failure on circuit '%s'; "
290 "attempting reconnect to server\n", state->proto_name);
291 SYNC_reconnect(state);
295 * unknown (probably protocol-specific) response code, pass it up to
296 * the caller, and let them deal with it
302 if (code == SYNC_COM_ERROR) {
303 Log("SYNC_ask: fatal protocol error on circuit '%s'; disabling sync "
304 "protocol until next server restart\n",
306 state->fatal_error = 1;
313 * send a command to a sync server and wait for a response.
315 * @param[in] state pointer to sync client handle
316 * @param[in] com command object
317 * @param[out] res response object
319 * @return operation status
320 * @retval SYNC_OK success
321 * @retval SYNC_COM_ERROR communications error
326 SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
329 SYNC_PROTO_BUF_DECL(buf);
335 if (state->fd == OSI_NULLSOCKET) {
336 Log("SYNC_ask: invalid sync file descriptor on circuit '%s'\n",
338 res->hdr.response = SYNC_COM_ERROR;
342 if (com->hdr.command_len > SYNC_PROTO_MAX_LEN) {
343 Log("SYNC_ask: internal SYNC buffer too small on circuit '%s'; "
344 "please file a bug\n", state->proto_name);
345 res->hdr.response = SYNC_COM_ERROR;
350 * fill in some common header fields
352 com->hdr.proto_version = state->proto_version;
353 com->hdr.pkt_seq = ++state->pkt_seq;
354 com->hdr.com_seq = ++state->com_seq;
359 com->hdr.pid = getpid();
360 #ifdef AFS_PTHREAD_ENV
361 com->hdr.tid = afs_pointer_to_int(pthread_self());
364 PROCESS handle = LWP_ThreadId();
365 com->hdr.tid = (handle) ? handle->index : 0;
367 #endif /* !AFS_PTHREAD_ENV */
368 #endif /* !AFS_NT40_ENV */
370 memcpy(buf, &com->hdr, sizeof(com->hdr));
371 if (com->payload.len) {
372 memcpy(buf + sizeof(com->hdr), com->payload.buf,
373 com->hdr.command_len - sizeof(com->hdr));
377 n = send(state->fd, buf, com->hdr.command_len, 0);
378 if (n != com->hdr.command_len) {
379 Log("SYNC_ask: write failed on circuit '%s'\n", state->proto_name);
380 res->hdr.response = SYNC_COM_ERROR;
384 if (com->hdr.command == SYNC_COM_CHANNEL_CLOSE) {
385 /* short circuit close channel requests */
386 res->hdr.response = SYNC_OK;
390 n = recv(state->fd, buf, SYNC_PROTO_MAX_LEN, 0);
391 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
392 Log("SYNC_ask: No response on circuit '%s'\n", state->proto_name);
393 res->hdr.response = SYNC_COM_ERROR;
396 #else /* !AFS_NT40_ENV */
397 n = write(state->fd, buf, com->hdr.command_len);
398 if (com->hdr.command_len != n) {
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 /* receive the response */
411 iov[0].iov_base = (char *)&res->hdr;
412 iov[0].iov_len = sizeof(res->hdr);
413 if (res->payload.len) {
414 iov[1].iov_base = (char *)res->payload.buf;
415 iov[1].iov_len = res->payload.len;
420 n = readv(state->fd, iov, iovcnt);
421 if (n == 0 || (n < 0 && errno != EINTR)) {
422 Log("SYNC_ask: No response on circuit '%s'\n", state->proto_name);
423 res->hdr.response = SYNC_COM_ERROR;
426 #endif /* !AFS_NT40_ENV */
430 if (n < sizeof(res->hdr)) {
431 Log("SYNC_ask: response too short on circuit '%s'\n",
433 res->hdr.response = SYNC_COM_ERROR;
437 memcpy(&res->hdr, buf, sizeof(res->hdr));
440 if ((n - sizeof(res->hdr)) > res->payload.len) {
441 Log("SYNC_ask: response too long on circuit '%s'\n",
443 res->hdr.response = SYNC_COM_ERROR;
447 memcpy(res->payload.buf, buf + sizeof(res->hdr), n - sizeof(res->hdr));
450 if (res->hdr.response_len != n) {
451 Log("SYNC_ask: length field in response inconsistent "
452 "on circuit '%s'\n", state->proto_name);
453 res->hdr.response = SYNC_COM_ERROR;
456 if (res->hdr.response == SYNC_DENIED) {
457 Log("SYNC_ask: negative response on circuit '%s'\n", state->proto_name);
461 return res->hdr.response;
466 * daemon com SYNC server-side interfaces
470 * receive a command structure off a sync socket.
472 * @param[in] state pointer to server-side state object
473 * @param[in] fd file descriptor on which to perform i/o
474 * @param[out] com sync command object to be populated
476 * @return operation status
477 * @retval SYNC_OK command received
478 * @retval SYNC_COM_ERROR there was a socket communications error
481 SYNC_getCom(SYNC_server_state_t * state,
486 afs_int32 code = SYNC_OK;
488 SYNC_PROTO_BUF_DECL(buf);
495 n = recv(fd, buf, SYNC_PROTO_MAX_LEN, 0);
497 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
498 Log("SYNC_getCom: error receiving command\n");
499 code = SYNC_COM_ERROR;
502 #else /* !AFS_NT40_ENV */
503 iov[0].iov_base = (char *)&com->hdr;
504 iov[0].iov_len = sizeof(com->hdr);
505 if (com->payload.len) {
506 iov[1].iov_base = (char *)com->payload.buf;
507 iov[1].iov_len = com->payload.len;
513 n = readv(fd, iov, iovcnt);
514 if (n == 0 || (n < 0 && errno != EINTR)) {
515 Log("SYNC_getCom: error receiving command\n");
516 code = SYNC_COM_ERROR;
519 #endif /* !AFS_NT40_ENV */
523 if (n < sizeof(com->hdr)) {
524 Log("SYNC_getCom: command too short\n");
525 code = SYNC_COM_ERROR;
529 memcpy(&com->hdr, buf, sizeof(com->hdr));
532 if ((n - sizeof(com->hdr)) > com->payload.len) {
533 Log("SYNC_getCom: command too long\n");
534 code = SYNC_COM_ERROR;
538 memcpy(com->payload.buf, buf + sizeof(com->hdr), n - sizeof(com->hdr));
546 * write a response structure to a sync socket.
548 * @param[in] state handle to server-side state object
549 * @param[in] fd file descriptor on which to perform i/o
550 * @param[in] res handle to response packet
552 * @return operation status
554 * @retval SYNC_COM_ERROR
557 SYNC_putRes(SYNC_server_state_t * state,
562 afs_int32 code = SYNC_OK;
563 SYNC_PROTO_BUF_DECL(buf);
565 if (res->hdr.response_len > (sizeof(res->hdr) + res->payload.len)) {
566 Log("SYNC_putRes: response_len field in response header inconsistent\n");
567 code = SYNC_COM_ERROR;
571 if (res->hdr.response_len > SYNC_PROTO_MAX_LEN) {
572 Log("SYNC_putRes: internal SYNC buffer too small; please file a bug\n");
573 code = SYNC_COM_ERROR;
577 #ifdef AFS_DEMAND_ATTACH_FS
578 res->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
580 res->hdr.proto_version = state->proto_version;
581 res->hdr.pkt_seq = ++state->pkt_seq;
582 res->hdr.res_seq = ++state->res_seq;
584 memcpy(buf, &res->hdr, sizeof(res->hdr));
585 if (res->payload.len) {
586 memcpy(buf + sizeof(res->hdr), res->payload.buf,
587 res->hdr.response_len - sizeof(res->hdr));
591 n = send(fd, buf, res->hdr.response_len, 0);
592 #else /* !AFS_NT40_ENV */
593 n = write(fd, buf, res->hdr.response_len);
594 #endif /* !AFS_NT40_ENV */
596 if (res->hdr.response_len != n) {
597 Log("SYNC_putRes: write failed\n");
598 res->hdr.response = SYNC_COM_ERROR;
606 /* return 0 for legal (null-terminated) string,
607 * 1 for illegal (unterminated) string */
609 SYNC_verifyProtocolString(char * buf, size_t len)
613 s_len = strnlen(buf, len);
615 return (s_len == len) ? 1 : 0;
619 * clean up old sockets.
621 * @param[in] state server state object
623 * @post unix domain sockets are cleaned up
626 SYNC_cleanupSock(SYNC_server_state_t * state)
628 #ifdef USE_UNIX_SOCKETS
629 remove(state->addr.sun_path);
634 * bind socket and set it to listen state.
636 * @param[in] state server state object
638 * @return operation status
640 * @retval nonzero failure
642 * @post socket bound and set to listen state
645 SYNC_bindSock(SYNC_server_state_t * state)
651 /* Reuseaddr needed because system inexplicably leaves crud lying around */
653 setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
656 Log("SYNC_bindSock: setsockopt failed with (%d)\n", errno);
658 for (numTries = 0; numTries < state->bind_retry_limit; numTries++) {
659 code = bind(state->fd,
660 (struct sockaddr *)&state->addr,
661 AFS_SOCKADDR_LEN(&state->addr));
664 Log("SYNC_bindSock: bind failed with (%d), will sleep and retry\n",
668 listen(state->fd, state->listen_depth);