void dfs_utils_init(char *n, char *dbname, char *logfile) { process_name = strdup(n); struct sigvec vec; // if (dbname && dfs_db_open(dbname)) { if (dbname) { if (dfs_db_open(dbname)) dfs_die("Can't open database: %s\n", dbname); if (dfs_use_pragmas) { dfs_db_do("PRAGMA synchronous=OFF;"); // very useful //dfs_db_do("PRAGMA locking_mode=EXCLUSIVE;"); // seems to do little } dfs_db_start(); } vec.sv_handler = dfs_stats; vec.sv_mask = 0; vec.sv_flags = 0; int res = sigvec(SIGINT, &vec, NULL); if (res) dfs_die("No set sigvec correctly: %d\n", res); }
static int _dfs_open(const char *path, struct fuse_file_info *fi) { DfsFile *f; dfs_out("\n\tFUSE OPEN '%s'\n\n", path); if (!(f = findFile((char *)path))) { return -ENOENT; } long flags = fi ? fi->flags : 0; dfs_out("\tOPEN : '%s', flags %o, len %d, reclen %d, recipe %x, data %x\n", path, flags, f->len, f->recipelen, f->recipe, f->data); if (f->stat.st_mode & S_IFDIR) { return -EISDIR; } if (0 && !(fi->flags & f->stat.st_mode)) { dfs_out("OPEN permissions problem: %o, %o\n", fi->flags, f->stat.st_mode); return -EACCES; } // Why? don't remember.... fi->fh = (uint64_t)f->stat.st_mode; if (f->len && !f->data) { assert(f->recipe); f->data = malloc(f->len); assert(f->data); char *data = f->data, *dataEnd = f->data + f->len; char *sig = f->recipe, *sigEnd = f->recipe + f->recipelen; while ((data < dataEnd) && (sig < sigEnd)) { Extent *ex = get_extent(sig); if (!ex) dfs_die("No get signature '%s'\n", sig); memcpy(data, ex->data, ex->sz); data += ex->sz; sig += strlen(sig) + 1; free(ex); } assert((data == dataEnd) && (sig == sigEnd)); } return 0; }
void dfs_stats() { int i; dfs_db_commit(); fprintf(stderr, "\nSENT: %6d messages, %11d bytes\n", dfs_messages_sent, dfs_bytes_sent); fprintf(stderr, "RECV: %6d messages, %11d bytes\n\n", dfs_messages_received, dfs_bytes_received); for (i = 0; i < MAX_MSGS; i++) { if (dfs_time_accum[i].count || dfs_msgs_sent[i]) fprintf(stderr, "%3d ('%17s'): %5ld avging %7ld usecs, %7d bytes each, %.2f\n", i, dfs_msgname(i), dfs_msgs_sent[i] ? dfs_msgs_sent[i] : dfs_time_accum[i].count, dfs_time_accum[i].usecs / (dfs_time_accum[i].count ? dfs_time_accum[i].count : 1), dfs_bytes_sent_type[i] / (dfs_msgs_sent[i] ? dfs_msgs_sent[i] : 1), dfs_time_accum[i].usecs / 1000000.0); } dfs_die("goodbye\n"); }
static void *listener(void *arg) { int fd = *(int *)arg; dfs_out("\n\tLISTEN PROC IN, sock %d!\n\n", fd); Msg *m; while (m = comm_read(1, fd)) { switch (m->type) { case DFS_MSG_PUSH_LOG: /* Lock tree and replay log. Assumes we get the whole log. */ pthread_mutex_lock(&treeMut); pthread_mutex_lock(&replyLogserverMut); dfs_out("Push calling play log\n"); char *log; size_t log_sz; assert(tuple_unserialize_log(&log, &log_sz, m->data, m->len) == 0); playLog(log, log_sz); free(log); pthread_mutex_unlock(&replyLogserverMut); pthread_mutex_unlock(&treeMut); break; case MSG_REPLY: /* Put on reply queue. Waits until main thread gets the item and sets the replyQueue to NULL to prevent losing messages */ pthread_mutex_lock(&replyLogserverMut); assert(replyQueue == NULL); replyQueue = m; pthread_cond_signal(&replyLogserverCond); pthread_cond_wait(&replyLogserverCond, &replyLogserverMut); pthread_mutex_unlock(&replyLogserverMut); break; default: dfs_die("BAD MSG TYPE %d\n", m->type); } } dfs_out("\n\tLISTEN PROC EXIT, sock %d!\n\n", fd); return NULL; }
// Puts sig into sigbuf. '0' on success. char *put_extent(char *buf, long sz) { int i; assert(buf && sz && (sz < (1024L * 1024L * 1024L))); char *s = cry_hash_bytes(buf, sz); if (poll_extent(s)) { printf("Server already has EXTENT\n"); return s; } char *serialized; size_t serialized_sz; assert(tuple_serialize_sig_extent(s, &serialized, &serialized_sz, buf, sz) == 0); Msg *reply = comm_send_and_reply(0, extentSock, DFS_MSG_PUT_EXTENT, serialized, serialized_sz, NULL); free(serialized); if (!reply) dfs_die("No get reply\n"); dfs_out("extent '%s' CREATED\n", s); free(reply); return s; }
static void * dfs_init(struct fuse_conn_info *conn) { pthread_mutex_lock(&treeMut); dfs_out("INIT!\n"); root = mkNode("", "", NULL, DEF_DIR_MODE); comm_register_msgtypes(sizeof(messages) / sizeof(messages[0]), messages); if (!(extentSock = comm_client_socket(xname, xport))) dfs_die("NO setup client socket to extent server at %d on '%s'\n", xport, xname); if (sname && ((opLog.net_fd = comm_client_socket(sname, sport)) > 0)) { // Here is where we auth assert(chit); // Send the server an OHAI Msg *reply = comm_send_and_reply(0, opLog.net_fd, DFS_OHAI_SERVER, NULL); char *reply_data = malloc(reply->len + 1); memcpy(reply_data, reply->data, reply->len); reply_data[reply->len] = '\0'; char server_nonce = reply_data[0]; char *server_pk = reply_data + 1; printf("Server gave us a nonce [%d] and a pk[%s]. He liked our picture!\n", (int) server_nonce, server_pk); // TODO Verify PK // Make an AES session key cry_create_nonce(16, session_key); printf("Sym Key[%x%x%x%x]\n", *(session_key), *(session_key + 4), *(session_key + 8), *(session_key + 12)); // Encrypt session key with PK char *session_key_encrypted; int session_key_encrypted_sz; cry_asym_encrypt(&session_key_encrypted, &session_key_encrypted_sz, session_key, 16, server_pk); cry_sym_init(session_key); int out_chit_len = strlen(chit) + 3; char *out_chit = malloc(out_chit_len); char client_nonce; cry_create_nonce(1, &client_nonce); printf("client nonce[%d]\n", (int)client_nonce); out_chit[0] = client_nonce; out_chit[1] = server_nonce; strcpy(out_chit + 2, chit); out_chit[out_chit_len - 1] = '\0'; char *out_chit_encrypted; int out_chit_encrypted_sz; cry_sym_encrypt(&out_chit_encrypted, &out_chit_encrypted_sz, out_chit, out_chit_len); reply = comm_send_and_reply(0, opLog.net_fd, DFS_TAKE_CHIT_SERVER, session_key_encrypted, session_key_encrypted_sz, out_chit_encrypted, out_chit_encrypted_sz, NULL); char *out_nonce; int out_nonce_sz; cry_sym_init(session_key); cry_sym_decrypt(&out_nonce, &out_nonce_sz, reply->data, reply->len); printf("Nonce check[%d][%d]\n", (int)client_nonce, (int)(*out_nonce)); assert(*out_nonce == (char)(client_nonce + 1)); free(reply); // create a new thread reading this socket pthread_t tid; pthread_create(&tid, NULL, listener, &opLog.net_fd); // grab current FS from server reply = comm_send_and_reply_mutex(1, &replyLogserverMut, &replyLogserverCond, opLog.net_fd, DFS_MSG_GET_LOG, NULL); char *log; size_t log_sz; assert(tuple_unserialize_log(&log, &log_sz, reply->data, reply->len) == 0); if (reply) { if (reply->len) { playLog(log, log_sz); opLog.served = log_sz; } free(reply); } free(log); } else { dfs_die("NO setup client socket to log server at %d on '%s'\n", sport, sname); } pthread_mutex_unlock(&treeMut); }