int file_table_close(int fd) { static char m1[] = "("; static char m2[] = "@"; static char m3[] = ") disconnected"; char *msg; if (fd < 0 || fd >= file_table_size || file_table[fd].conn == NULL) return (EBADF); /* disconnect, do logging */ msg = malloc(sizeof(m1) - 1 + strlen(file_table[fd].user) + sizeof(m2) - 1 + strlen(file_table[fd].host) + sizeof(m3)); if (msg == NULL) { gflog_notice("(:@not enough memory) disconnected", file_table[fd].user); } else { sprintf(msg, "%s%s%s%s%s", m1, file_table[fd].user, m2, file_table[fd].host, m3); gflog_notice(msg, NULL); free(msg); } while (file_table[fd].jobs != NULL) job_table_remove(file_table[fd].jobs->id, file_table[fd].user, &file_table[fd].jobs); file_table[fd].jobs = NULL; free(file_table[fd].user); file_table[fd].user = NULL; free(file_table[fd].host); file_table[fd].host = NULL; xxx_connection_free(file_table[fd].conn); file_table[fd].conn = NULL; if (fd == file_table_max) { while (--file_table_max >= 0) if (file_table[file_table_max].conn != NULL) break; } return (0); }
static gfarm_error_t failover0(struct gfm_connection *gfm_server, const char *host0, int port, const char *user0) { gfarm_error_t e; struct gfarm_filesystem *fs; struct gfs_file_list *gfl = NULL; char *host = NULL, *user = NULL; int fc, i, ok = 0; if (gfm_server) { fs = gfarm_filesystem_get_by_connection(gfm_server); if (gfarm_filesystem_in_failover_process(fs)) { e = GFARM_ERR_OPERATION_NOT_PERMITTED; gflog_debug(GFARM_MSG_1003861, "gfmd connection failover process called " "recursively"); goto error_all; } gfarm_filesystem_set_in_failover_process(fs, 1); fc = gfarm_filesystem_failover_count(fs); if ((host = strdup(gfm_client_hostname(gfm_server))) == NULL) { e = GFARM_ERR_NO_MEMORY; gflog_debug(GFARM_MSG_1003388, "%s", gfarm_error_string(e)); goto error_all; } if ((user = strdup(gfm_client_username(gfm_server))) == NULL) { e = GFARM_ERR_NO_MEMORY; gflog_debug(GFARM_MSG_1003389, "%s", gfarm_error_string(e)); goto error_all; } port = gfm_client_port(gfm_server); if (fc > gfm_client_connection_failover_count(gfm_server)) { /* already failover occurred */ e = acquire_valid_connection(gfm_server, host, port, user, fs); goto end; } } else { fs = gfarm_filesystem_get(host0, port); assert(fs != NULL); if (gfarm_filesystem_in_failover_process(fs)) { e = GFARM_ERR_OPERATION_NOT_PERMITTED; gflog_debug(GFARM_MSG_1003862, "gfmd connection failover process called " "recursively"); goto error_all; } gfarm_filesystem_set_in_failover_process(fs, 1); fc = gfarm_filesystem_failover_count(fs); if ((host = strdup(host0)) == NULL) { e = GFARM_ERR_NO_MEMORY; gflog_debug(GFARM_MSG_1003863, "%s", gfarm_error_string(e)); goto error_all; } if ((user = strdup(user0)) == NULL) { e = GFARM_ERR_NO_MEMORY; gflog_debug(GFARM_MSG_1003864, "%s", gfarm_error_string(e)); goto error_all; } } gfl = gfarm_filesystem_opened_file_list(fs); /* * failover_count must be incremented before acquire connection * because failover_count is set at creating new connection * and we use failover_count to indicate whether or not the acquired * connection is new connection. */ gfarm_filesystem_set_failover_count(fs, fc + 1); for (i = 0; i < NUM_FAILOVER_RETRY; ++i) { /* reconnect to gfmd */ if ((e = gfm_client_connection_and_process_acquire( host, port, user, &gfm_server)) != GFARM_ERR_NO_ERROR) { gflog_debug(GFARM_MSG_1003390, "gfm_client_connection_acquire failed : %s", gfarm_error_string(e)); continue; } if (gfm_client_connection_failover_count(gfm_server) != fc + 1) { /* * When this function is called from * gfm_client_connection_failover_pre_connect(), * the acquired connection is possibly an old * connection. */ gfm_client_purge_from_cache(gfm_server); gfm_client_connection_free(gfm_server); --i; continue; } /* * close fd, release gfm_connection and set invalid fd, * reset processes and reopen files */ ok = reset_and_reopen_all(gfm_server, gfl); gfm_client_connection_free(gfm_server); if (ok) break; } if (ok) { gfarm_filesystem_set_failover_detected(fs, 0); e = GFARM_ERR_NO_ERROR; gflog_notice(GFARM_MSG_1003391, "connection to metadb server was failed over successfully"); } else { gfarm_filesystem_set_failover_detected(fs, 1); e = GFARM_ERR_OPERATION_TIMED_OUT; gflog_debug(GFARM_MSG_1003392, "falied to fail over: %s", gfarm_error_string(e)); } end: free(host); free(user); gfarm_filesystem_set_in_failover_process(fs, 0); return (e); error_all: free(host); free(user); if (gfl != NULL) gfs_pio_file_list_foreach(gfl, set_error, &e); gfarm_filesystem_set_in_failover_process(fs, 0); return (e); }