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>
26 #include <afs/afs_assert.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)
177 closesocket(state->fd);
181 state->fd = OSI_NULLSOCKET;
186 * gracefully disconnect a sync client handle.
188 * @param[in] state pointer to sync client handle
190 * @return operation status
191 * @retval SYNC_OK success
194 SYNC_closeChannel(SYNC_client_state * state)
198 SYNC_PROTO_BUF_DECL(ores);
200 if (state->fd == OSI_NULLSOCKET)
203 memset(&com, 0, sizeof(com));
204 memset(&res, 0, sizeof(res));
206 res.payload.len = SYNC_PROTO_MAX_LEN;
207 res.payload.buf = ores;
209 com.hdr.command = SYNC_COM_CHANNEL_CLOSE;
210 com.hdr.command_len = sizeof(SYNC_command_hdr);
211 com.hdr.flags |= SYNC_FLAG_CHANNEL_SHUTDOWN;
213 /* in case the other end dropped, don't do any retries */
214 state->retry_limit = 0;
215 state->hard_timeout = 0;
217 SYNC_ask(state, &com, &res);
218 SYNC_disconnect(state);
224 * forcibly break a client connection, and then create a new connection.
226 * @param[in] state pointer to sync client handle
228 * @post old connection dropped; new connection established
230 * @return @see SYNC_connect()
233 SYNC_reconnect(SYNC_client_state * state)
235 SYNC_disconnect(state);
236 return SYNC_connect(state);
240 * send a command to a sync server and wait for a response.
242 * @param[in] state pointer to sync client handle
243 * @param[in] com command object
244 * @param[out] res response object
246 * @return operation status
247 * @retval SYNC_OK success
248 * @retval SYNC_COM_ERROR communications error
249 * @retval SYNC_BAD_COMMAND server did not recognize command code
251 * @note this routine merely handles error processing; SYNC_ask_internal()
252 * handles the low-level details of communicating with the SYNC server.
254 * @see SYNC_ask_internal
257 SYNC_ask(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
260 afs_uint32 now, timeout, code=SYNC_OK;
262 if (state->fatal_error) {
263 return SYNC_COM_ERROR;
266 if (state->fd == OSI_NULLSOCKET) {
270 if (state->fd == OSI_NULLSOCKET) {
271 state->fatal_error = 1;
272 return SYNC_COM_ERROR;
275 #ifdef AFS_DEMAND_ATTACH_FS
276 com->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
279 now = FT_ApproxTime();
280 timeout = now + state->hard_timeout;
282 (tries <= state->retry_limit) && (now <= timeout);
283 tries++, now = FT_ApproxTime()) {
284 code = SYNC_ask_internal(state, com, res);
285 if (code == SYNC_OK) {
287 } else if (code == SYNC_BAD_COMMAND) {
288 Log("SYNC_ask: protocol mismatch on circuit '%s'; make sure "
289 "fileserver, volserver, salvageserver and salvager are same "
290 "version\n", state->proto_name);
292 } else if ((code == SYNC_COM_ERROR) && (tries < state->retry_limit)) {
293 Log("SYNC_ask: protocol communications failure on circuit '%s'; "
294 "attempting reconnect to server\n", state->proto_name);
295 SYNC_reconnect(state);
299 * unknown (probably protocol-specific) response code, pass it up to
300 * the caller, and let them deal with it
306 if (code == SYNC_COM_ERROR) {
307 Log("SYNC_ask: fatal protocol error on circuit '%s'; disabling sync "
308 "protocol until next server restart\n",
310 state->fatal_error = 1;
317 * send a command to a sync server and wait for a response.
319 * @param[in] state pointer to sync client handle
320 * @param[in] com command object
321 * @param[out] res response object
323 * @return operation status
324 * @retval SYNC_OK success
325 * @retval SYNC_COM_ERROR communications error
330 SYNC_ask_internal(SYNC_client_state * state, SYNC_command * com, SYNC_response * res)
333 SYNC_PROTO_BUF_DECL(buf);
339 if (state->fd == OSI_NULLSOCKET) {
340 Log("SYNC_ask: invalid sync file descriptor on circuit '%s'\n",
342 res->hdr.response = SYNC_COM_ERROR;
346 if (com->hdr.command_len > SYNC_PROTO_MAX_LEN) {
347 Log("SYNC_ask: internal SYNC buffer too small on circuit '%s'; "
348 "please file a bug\n", state->proto_name);
349 res->hdr.response = SYNC_COM_ERROR;
354 * fill in some common header fields
356 com->hdr.proto_version = state->proto_version;
357 com->hdr.pkt_seq = ++state->pkt_seq;
358 com->hdr.com_seq = ++state->com_seq;
363 com->hdr.pid = getpid();
364 #ifdef AFS_PTHREAD_ENV
365 com->hdr.tid = afs_pointer_to_int(pthread_self());
368 PROCESS handle = LWP_ThreadId();
369 com->hdr.tid = (handle) ? handle->index : 0;
371 #endif /* !AFS_PTHREAD_ENV */
372 #endif /* !AFS_NT40_ENV */
374 memcpy(buf, &com->hdr, sizeof(com->hdr));
375 if (com->payload.len) {
376 memcpy(buf + sizeof(com->hdr), com->payload.buf,
377 com->hdr.command_len - sizeof(com->hdr));
381 n = send(state->fd, buf, com->hdr.command_len, 0);
382 if (n != com->hdr.command_len) {
383 Log("SYNC_ask: write failed on circuit '%s'\n", state->proto_name);
384 res->hdr.response = SYNC_COM_ERROR;
388 if (com->hdr.command == SYNC_COM_CHANNEL_CLOSE) {
389 /* short circuit close channel requests */
390 res->hdr.response = SYNC_OK;
394 n = recv(state->fd, buf, SYNC_PROTO_MAX_LEN, 0);
395 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
396 Log("SYNC_ask: No response on circuit '%s'\n", state->proto_name);
397 res->hdr.response = SYNC_COM_ERROR;
400 #else /* !AFS_NT40_ENV */
401 n = write(state->fd, buf, com->hdr.command_len);
402 if (com->hdr.command_len != n) {
403 Log("SYNC_ask: write failed on circuit '%s'\n", state->proto_name);
404 res->hdr.response = SYNC_COM_ERROR;
408 if (com->hdr.command == SYNC_COM_CHANNEL_CLOSE) {
409 /* short circuit close channel requests */
410 res->hdr.response = SYNC_OK;
414 /* receive the response */
415 iov[0].iov_base = (char *)&res->hdr;
416 iov[0].iov_len = sizeof(res->hdr);
417 if (res->payload.len) {
418 iov[1].iov_base = (char *)res->payload.buf;
419 iov[1].iov_len = res->payload.len;
424 n = readv(state->fd, iov, iovcnt);
425 if (n == 0 || (n < 0 && errno != EINTR)) {
426 Log("SYNC_ask: No response on circuit '%s'\n", state->proto_name);
427 res->hdr.response = SYNC_COM_ERROR;
430 #endif /* !AFS_NT40_ENV */
434 if (n < sizeof(res->hdr)) {
435 Log("SYNC_ask: response too short on circuit '%s'\n",
437 res->hdr.response = SYNC_COM_ERROR;
441 memcpy(&res->hdr, buf, sizeof(res->hdr));
444 if ((n - sizeof(res->hdr)) > res->payload.len) {
445 Log("SYNC_ask: response too long on circuit '%s'\n",
447 res->hdr.response = SYNC_COM_ERROR;
451 memcpy(res->payload.buf, buf + sizeof(res->hdr), n - sizeof(res->hdr));
454 if (res->hdr.response_len != n) {
455 Log("SYNC_ask: length field in response inconsistent "
456 "on circuit '%s'\n", state->proto_name);
457 res->hdr.response = SYNC_COM_ERROR;
460 if (res->hdr.response == SYNC_DENIED) {
461 Log("SYNC_ask: negative response on circuit '%s'\n", state->proto_name);
465 return res->hdr.response;
470 * daemon com SYNC server-side interfaces
474 * receive a command structure off a sync socket.
476 * @param[in] state pointer to server-side state object
477 * @param[in] fd file descriptor on which to perform i/o
478 * @param[out] com sync command object to be populated
480 * @return operation status
481 * @retval SYNC_OK command received
482 * @retval SYNC_COM_ERROR there was a socket communications error
485 SYNC_getCom(SYNC_server_state_t * state,
490 afs_int32 code = SYNC_OK;
492 SYNC_PROTO_BUF_DECL(buf);
499 n = recv(fd, buf, SYNC_PROTO_MAX_LEN, 0);
501 if (n == 0 || (n < 0 && WSAEINTR != WSAGetLastError())) {
502 Log("SYNC_getCom: error receiving command\n");
503 code = SYNC_COM_ERROR;
506 #else /* !AFS_NT40_ENV */
507 iov[0].iov_base = (char *)&com->hdr;
508 iov[0].iov_len = sizeof(com->hdr);
509 if (com->payload.len) {
510 iov[1].iov_base = (char *)com->payload.buf;
511 iov[1].iov_len = com->payload.len;
517 n = readv(fd, iov, iovcnt);
518 if (n == 0 || (n < 0 && errno != EINTR)) {
519 Log("SYNC_getCom: error receiving command\n");
520 code = SYNC_COM_ERROR;
523 #endif /* !AFS_NT40_ENV */
527 if (n < sizeof(com->hdr)) {
528 Log("SYNC_getCom: command too short\n");
529 code = SYNC_COM_ERROR;
533 memcpy(&com->hdr, buf, sizeof(com->hdr));
536 if ((n - sizeof(com->hdr)) > com->payload.len) {
537 Log("SYNC_getCom: command too long\n");
538 code = SYNC_COM_ERROR;
542 memcpy(com->payload.buf, buf + sizeof(com->hdr), n - sizeof(com->hdr));
550 * write a response structure to a sync socket.
552 * @param[in] state handle to server-side state object
553 * @param[in] fd file descriptor on which to perform i/o
554 * @param[in] res handle to response packet
556 * @return operation status
558 * @retval SYNC_COM_ERROR
561 SYNC_putRes(SYNC_server_state_t * state,
566 afs_int32 code = SYNC_OK;
567 SYNC_PROTO_BUF_DECL(buf);
569 if (res->hdr.response_len > (sizeof(res->hdr) + res->payload.len)) {
570 Log("SYNC_putRes: response_len field in response header inconsistent\n");
571 code = SYNC_COM_ERROR;
575 if (res->hdr.response_len > SYNC_PROTO_MAX_LEN) {
576 Log("SYNC_putRes: internal SYNC buffer too small; please file a bug\n");
577 code = SYNC_COM_ERROR;
581 #ifdef AFS_DEMAND_ATTACH_FS
582 res->hdr.flags |= SYNC_FLAG_DAFS_EXTENSIONS;
584 res->hdr.proto_version = state->proto_version;
585 res->hdr.pkt_seq = ++state->pkt_seq;
586 res->hdr.res_seq = ++state->res_seq;
588 memcpy(buf, &res->hdr, sizeof(res->hdr));
589 if (res->payload.len) {
590 memcpy(buf + sizeof(res->hdr), res->payload.buf,
591 res->hdr.response_len - sizeof(res->hdr));
595 n = send(fd, buf, res->hdr.response_len, 0);
596 #else /* !AFS_NT40_ENV */
597 n = write(fd, buf, res->hdr.response_len);
598 #endif /* !AFS_NT40_ENV */
600 if (res->hdr.response_len != n) {
601 Log("SYNC_putRes: write failed\n");
602 res->hdr.response = SYNC_COM_ERROR;
610 /* return 0 for legal (null-terminated) string,
611 * 1 for illegal (unterminated) string */
613 SYNC_verifyProtocolString(char * buf, size_t len)
617 s_len = strnlen(buf, len);
619 return (s_len == len) ? 1 : 0;
623 * clean up old sockets.
625 * @param[in] state server state object
627 * @post unix domain sockets are cleaned up
630 SYNC_cleanupSock(SYNC_server_state_t * state)
632 #ifdef USE_UNIX_SOCKETS
633 remove(state->addr.sun_path);
638 * bind socket and set it to listen state.
640 * @param[in] state server state object
642 * @return operation status
644 * @retval nonzero failure
646 * @post socket bound and set to listen state
649 SYNC_bindSock(SYNC_server_state_t * state)
655 /* Reuseaddr needed because system inexplicably leaves crud lying around */
657 setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
660 Log("SYNC_bindSock: setsockopt failed with (%d)\n", errno);
662 for (numTries = 0; numTries < state->bind_retry_limit; numTries++) {
663 code = bind(state->fd,
664 (struct sockaddr *)&state->addr,
665 AFS_SOCKADDR_LEN(&state->addr));
668 Log("SYNC_bindSock: bind failed with (%d), will sleep and retry\n",
672 listen(state->fd, state->listen_depth);