gboolean mcview_hexedit_save_changes (mcview_t * view) { int answer = 0; if (view->change_list == NULL) return TRUE; while (answer == 0) { int fp; char *text; struct hexedit_change_node *curr, *next; #ifdef HAVE_ASSERT_H assert (view->filename_vpath != NULL); #endif fp = mc_open (view->filename_vpath, O_WRONLY); if (fp != -1) { for (curr = view->change_list; curr != NULL; curr = next) { next = curr->next; if (mc_lseek (fp, curr->offset, SEEK_SET) == -1 || mc_write (fp, &(curr->value), 1) != 1) goto save_error; /* delete the saved item from the change list */ view->change_list = next; view->dirty++; mcview_set_byte (view, curr->offset, curr->value); g_free (curr); } view->change_list = NULL; if (view->locked) view->locked = unlock_file (view->filename_vpath); if (mc_close (fp) == -1) message (D_ERROR, _("Save file"), _("Error while closing the file:\n%s\n" "Data may have been written or not"), unix_error_string (errno)); view->dirty++; return TRUE; } save_error: text = g_strdup_printf (_("Cannot save file:\n%s"), unix_error_string (errno)); (void) mc_close (fp); answer = query_dialog (_("Save file"), text, D_ERROR, 2, _("&Retry"), _("&Cancel")); g_free (text); } return FALSE; }
static int mc_def_ungetlocalcopy (const vfs_path_t * filename_vpath, const vfs_path_t * local_vpath, gboolean has_changed) { int fdin = -1, fdout = -1; const char *local; local = vfs_path_get_last_path_str (local_vpath); if (has_changed) { char buffer[BUF_1K * 8]; ssize_t i; if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL) goto failed; fdin = open (local, O_RDONLY); if (fdin == -1) goto failed; fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC); if (fdout == -1) goto failed; while ((i = read (fdin, buffer, sizeof (buffer))) > 0) if (mc_write (fdout, buffer, (size_t) i) != i) goto failed; if (i == -1) goto failed; if (close (fdin) == -1) { fdin = -1; goto failed; } fdin = -1; if (mc_close (fdout) == -1) { fdout = -1; goto failed; } } unlink (local); return 0; failed: message (D_ERROR, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath)); if (fdout != -1) mc_close (fdout); if (fdin != -1) close (fdin); unlink (local); return -1; }
static vfs_path_t * mc_def_getlocalcopy (const vfs_path_t * filename_vpath) { vfs_path_t *tmp_vpath = NULL; int fdin = -1, fdout = -1; ssize_t i; char buffer[BUF_1K * 8]; struct stat mystat; fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR); if (fdin == -1) goto fail; fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath)); if (fdout == -1) goto fail; while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) { if (write (fdout, buffer, i) != i) goto fail; } if (i == -1) goto fail; i = mc_close (fdin); fdin = -1; if (i == -1) goto fail; i = close (fdout); fdout = -1; if (i == -1) { fdout = -1; goto fail; } if (mc_stat (filename_vpath, &mystat) != -1) mc_chmod (tmp_vpath, mystat.st_mode); return tmp_vpath; fail: vfs_path_free (tmp_vpath); if (fdout != -1) close (fdout); if (fdin != -1) mc_close (fdin); return NULL; }
/* event callback */ gboolean clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name, gpointer init_data, gpointer data) { int file; vfs_path_t *fname_vpath = NULL; size_t str_len; const char *text = (const char *) data; (void) event_group_name; (void) event_name; (void) init_data; if (text == NULL) return FALSE; fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE); file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY); vfs_path_free (fname_vpath); if (file == -1) return TRUE; str_len = strlen (text); { ssize_t ret; ret = mc_write (file, (char *) text, str_len); (void) ret; } mc_close (file); return TRUE; }
static gboolean mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path) { gchar *data, *written_data; gsize len, total_written; gboolean ret; int fd; ssize_t cur_written; data = g_key_file_to_data (mc_config->handle, &len, NULL); if (!exist_file (ini_path)) { ret = g_file_set_contents (ini_path, data, len, NULL); g_free (data); return ret; } mc_util_make_backup_if_possible (ini_path, "~"); fd = mc_open (ini_path, O_WRONLY | O_TRUNC | O_SYNC, 0); if (fd == -1) return FALSE; for (written_data = data, total_written = len; (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0; written_data += cur_written, total_written -= cur_written); mc_close (fd); g_free (data); if (cur_written == -1) { mc_util_restore_from_backup_if_possible (ini_path, "~"); return FALSE; } mc_util_unlink_backup_if_possible (ini_path, "~"); return TRUE; }
void mcview_close_datasource (mcview_t * view) { switch (view->datasource) { case DS_NONE: break; case DS_STDIO_PIPE: if (view->ds_stdio_pipe != NULL) { (void) pclose (view->ds_stdio_pipe); mcview_display (view); close_error_pipe (D_NORMAL, NULL); view->ds_stdio_pipe = NULL; } mcview_growbuf_free (view); break; case DS_VFS_PIPE: if (view->ds_vfs_pipe != -1) { (void) mc_close (view->ds_vfs_pipe); view->ds_vfs_pipe = -1; } mcview_growbuf_free (view); break; case DS_FILE: (void) mc_close (view->ds_file_fd); view->ds_file_fd = -1; MC_PTR_FREE (view->ds_file_data); break; case DS_STRING: MC_PTR_FREE (view->ds_string_data); break; default: #ifdef HAVE_ASSERT_H assert (!"Unknown datasource type") #endif ; } view->datasource = DS_NONE; }
/* Returns fd of the open tar file */ static int tar_open_archive (vfs *me, char *name, vfs_s_super *archive) { int result, type; long size; mode_t mode; struct vfs_s_inode *root; result = mc_open (name, O_RDONLY); if (result == -1) { message_2s (1, MSG_ERROR, _("Couldn't open tar archive\n%s"), name); ERRNOR (ENOENT, -1); } archive->name = strdup (name); mc_stat (name, &(archive->u.tar.tarstat)); archive->u.tar.fd = -1; /* Find out the method to handle this tar file */ size = is_gunzipable (result, &type); mc_lseek (result, 0, SEEK_SET); if (size > 0) { char *s; mc_close( result ); s = copy_strings( archive->name, decompress_extension (type), NULL ); result = mc_open (s, O_RDONLY); if (result == -1) message_2s (1, MSG_ERROR, _("Couldn't open tar archive\n%s"), s); free(s); if (result == -1) ERRNOR (ENOENT, -1); } archive->u.tar.fd = result; mode = archive->u.tar.tarstat.st_mode & 07777; if (mode & 0400) mode |= 0100; if (mode & 0040) mode |= 0010; if (mode & 0004) mode |= 0001; mode |= S_IFDIR; root = vfs_s_new_inode (me, archive, &archive->u.tar.tarstat); root->st.st_mode = mode; root->u.tar.data_offset = -1; root->st.st_nlink++; root->st.st_dev = MEDATA->rdev++; vfs_s_add_dots (me, root, NULL); archive->root = root; return result; }
static gboolean mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** mcerror) { gchar *data, *written_data; gsize len, total_written; gboolean ret; int fd; ssize_t cur_written; vfs_path_t *ini_vpath; mc_return_val_if_error (mcerror, FALSE); data = g_key_file_to_data (mc_config->handle, &len, NULL); if (!exist_file (ini_path)) { ret = g_file_set_contents (ini_path, data, len, mcerror); g_free (data); return ret; } mc_util_make_backup_if_possible (ini_path, "~"); ini_vpath = vfs_path_from_str (ini_path); fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0); vfs_path_free (ini_vpath); if (fd == -1) { mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno)); g_free (data); return FALSE; } for (written_data = data, total_written = len; (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0; written_data += cur_written, total_written -= cur_written) ; mc_close (fd); g_free (data); if (cur_written == -1) { mc_util_restore_from_backup_if_possible (ini_path, "~"); mc_propagate_error (mcerror, 0, "%s", unix_error_string (errno)); return FALSE; } mc_util_unlink_backup_if_possible (ini_path, "~"); return TRUE; }
void mcview_growbuf_done (WView * view) { view->growbuf_finished = TRUE; if (view->datasource == DS_STDIO_PIPE) { mc_pclose (view->ds_stdio_pipe, NULL); view->ds_stdio_pipe = NULL; } else /* view->datasource == DS_VFS_PIPE */ { (void) mc_close (view->ds_vfs_pipe); view->ds_vfs_pipe = -1; } }
int child_mc_size_error(int cmd) { int rc; struct ctx myctx; struct ctx *p_ctx = &myctx; __u64 size=0; int invalid=0; pid = getpid(); rc =mc_init(); CHECK_RC(rc, "mc_init failed"); rc = ctx_init(p_ctx); CHECK_RC(rc, "ctx init failed"); rc = mc_register(master_dev_path, p_ctx->ctx_hndl, (volatile __u64 *)p_ctx->p_host_map,&p_ctx->mc_hndl); CHECK_RC(rc, "ctx reg failed"); rc = mc_open(p_ctx->mc_hndl,MC_RDWR, &p_ctx->res_hndl); CHECK_RC(rc, "opening res_hndl"); if(1 == cmd) { //invalid MCH rc = mc_size((mc_hndl_t)&invalid, p_ctx->res_hndl,1,&size); rc = rc ? 1:0; } else if( 2 == cmd) { //invalid RSH rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl+20,1,&size); rc = rc ? 2:0; } else if(3 == cmd) { //NULL size rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl,1, NULL); rc = rc ? 3:0; } else if(4 == cmd) { //after mc_close mc_close(p_ctx->mc_hndl, p_ctx->res_hndl); rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl,1, &size); rc = rc ? 4:0; } else if(5 == cmd) { //after mc_unregister mc_unregister(p_ctx->mc_hndl); rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl,1, &size); rc = rc ? 5:0; } ctx_close(p_ctx); mc_term(); return rc; }
int mc_invalid_ioarcb(int cmd) { int rc; struct ctx myctx; struct ctx *p_ctx = &myctx; __u64 chunks=16; __u64 actual_size=0; __u64 vlba =0; __u32 *p_u32; __u64 stride; pthread_t thread; mc_stat_t l_mc_stat; int i; rc = mc_init(); CHECK_RC(rc, "mc_init failed"); debug("mc_init success :%d\n",rc); rc = ctx_init(p_ctx); CHECK_RC(rc, "Context init failed"); pthread_create(&thread,NULL,ctx_rrq_rx, p_ctx); rc = mc_register(master_dev_path, p_ctx->ctx_hndl, (volatile __u64 *)p_ctx->p_host_map,&p_ctx->mc_hndl); CHECK_RC(rc, "ctx reg failed"); rc = mc_open(p_ctx->mc_hndl,MC_RDWR, &p_ctx->res_hndl); CHECK_RC(rc, "opening res_hndl"); rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl,chunks, &actual_size); CHECK_RC(rc, "mc_size"); rc = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat); CHECK_RC(rc, "mc_stat"); stride = 1 << l_mc_stat.nmask; pid = getpid(); vlba = (actual_size * (1 << l_mc_stat.nmask))-1; fill_send_write(p_ctx, vlba, pid, stride, VLBA); for(i = 0; i < NUM_CMDS; i++) { if (1 == cmd){ //invalid upcode debug("invalid upcode(0xFA) action = %d\n",cmd); p_ctx->cmd[i].rcb.cdb[0] = 0xFA; }else if (2 == cmd) {//EA = NULL debug("EA = NULL action = %d\n",cmd); p_ctx->cmd[i].rcb.data_ea = (__u64)NULL; }else if(3 == cmd){ //invalid flgas p_ctx->cmd[i].rcb.req_flags = SISL_REQ_FLAGS_RES_HNDL; p_ctx->cmd[i].rcb.req_flags |= SISL_REQ_FLAGS_HOST_READ; debug("invalid flag = 0X%X\n",p_ctx->cmd[i].rcb.req_flags); }else if(5 == cmd) {//SISL_AFU_RC_RHT_INVALID p_ctx->cmd[i].rcb.res_hndl = p_ctx->res_hndl + 2; }else if( 6 == cmd) {//SISL_AFU_RC_RHT_OUT_OF_BOUNDS p_ctx->cmd[i].rcb.res_hndl = MAX_RES_HANDLE; }else if(7 == cmd) { //invalid address for page fault debug("setting EA = 0x1234 to generate error page fault\n"); p_ctx->cmd[i].rcb.data_ea = (__u64)0x1234; }else if(8 == cmd) { //invalid ctx_id debug("%d : sending invalid ctx id\n", pid); p_ctx->cmd[i].rcb.ctx_id = p_ctx->ctx_hndl +10; }else if(9 == cmd) { //test flag underrun p_ctx->cmd[i].rcb.data_len = sizeof(p_ctx->wbuf[0])/2; }else if(10 == cmd) {// test flag overrun p_ctx->cmd[i].rcb.data_len = sizeof(p_ctx->wbuf[0]) +2; }else if(11 == cmd) { //rc scsi_rc_check p_u32 = (__u32*)&p_ctx->cmd[i].rcb.cdb[10]; write_32(p_u32, LBA_BLK +1); }else if(12 == cmd) { //data len 0 in ioarcb p_ctx->cmd[i].rcb.data_len = 0; }else if(13 == cmd) { //NUM BLK to write 0 p_u32 = (__u32*)&p_ctx->cmd[i].rcb.cdb[10]; write_32(p_u32, 0); } } //send_single_cmd(p_ctx); send_cmd(p_ctx); //rc = wait_single_resp(p_ctx); rc = wait_resp(p_ctx); if( cmd >= 9 && cmd <= 13) { if(!rc_flags) { if(!dont_displa_err_msg) fprintf(stderr, "%d : Expecting rc flags non zero\n", pid); rc = -1; } } if(4 == cmd) {//invalid fc port & lun id debug("invalid fc port(0xFF)&lun id(0X1200), action=%d",cmd); fill_send_write(p_ctx, vlba, pid, stride, PLBA); for(i = 0; i < NUM_CMDS; i++) { p_ctx->cmd[i].rcb.lun_id = 0x12000; p_ctx->cmd[i].rcb.port_sel = 0xff; } //send_single_cmd(p_ctx); send_cmd(p_ctx); rc = wait_resp(p_ctx); } pthread_cancel(thread); mc_close(p_ctx->mc_hndl,p_ctx->res_hndl); mc_unregister(p_ctx->mc_hndl); ctx_close(p_ctx); mc_term(); return rc; }
static void tar_free_archive (vfs *me, vfs_s_super *archive) { if (archive->u.tar.fd != -1) mc_close(archive->u.tar.fd); }
gboolean mcview_load (mcview_t * view, const char *command, const char *file, int start_line) { gboolean retval = FALSE; vfs_path_t *vpath = NULL; #ifdef HAVE_ASSERT_H assert (view->bytes_per_line != 0); #endif view->filename_vpath = vfs_path_from_str (file); /* get working dir */ if (file != NULL && file[0] != '\0') { vfs_path_free (view->workdir_vpath); if (!g_path_is_absolute (file)) { vfs_path_t *p; p = vfs_path_clone (vfs_get_raw_current_dir ()); view->workdir_vpath = vfs_path_append_new (p, file, (char *) NULL); vfs_path_free (p); } else { /* try extract path form filename */ const char *fname; char *dir; fname = x_basename (file); dir = g_strndup (file, (size_t) (fname - file)); view->workdir_vpath = vfs_path_from_str (dir); g_free (dir); } } if (!mcview_is_in_panel (view)) view->dpy_text_column = 0; mcview_set_codeset (view); if (command != NULL && (view->magic_mode || file == NULL || file[0] == '\0')) retval = mcview_load_command_output (view, command); else if (file != NULL && file[0] != '\0') { int fd; char tmp[BUF_MEDIUM]; struct stat st; /* Open the file */ vpath = vfs_path_from_str (file); fd = mc_open (vpath, O_RDONLY | O_NONBLOCK); if (fd == -1) { g_snprintf (tmp, sizeof (tmp), _("Cannot open \"%s\"\n%s"), file, unix_error_string (errno)); mcview_show_error (view, tmp); vfs_path_free (view->filename_vpath); view->filename_vpath = NULL; vfs_path_free (view->workdir_vpath); view->workdir_vpath = NULL; goto finish; } /* Make sure we are working with a regular file */ if (mc_fstat (fd, &st) == -1) { mc_close (fd); g_snprintf (tmp, sizeof (tmp), _("Cannot stat \"%s\"\n%s"), file, unix_error_string (errno)); mcview_show_error (view, tmp); vfs_path_free (view->filename_vpath); view->filename_vpath = NULL; vfs_path_free (view->workdir_vpath); view->workdir_vpath = NULL; goto finish; } if (!S_ISREG (st.st_mode)) { mc_close (fd); mcview_show_error (view, _("Cannot view: not a regular file")); vfs_path_free (view->filename_vpath); view->filename_vpath = NULL; vfs_path_free (view->workdir_vpath); view->workdir_vpath = NULL; goto finish; } if (st.st_size == 0 || mc_lseek (fd, 0, SEEK_SET) == -1) { /* Must be one of those nice files that grow (/proc) */ mcview_set_datasource_vfs_pipe (view, fd); } else { int type; type = get_compression_type (fd, file); if (view->magic_mode && (type != COMPRESSION_NONE)) { char *tmp_filename; vfs_path_t *vpath1; int fd1; tmp_filename = g_strconcat (file, decompress_extension (type), (char *) NULL); vpath1 = vfs_path_from_str (tmp_filename); fd1 = mc_open (vpath1, O_RDONLY | O_NONBLOCK); if (fd1 == -1) { g_snprintf (tmp, sizeof (tmp), _("Cannot open \"%s\" in parse mode\n%s"), file, unix_error_string (errno)); mcview_show_error (view, tmp); } else { mc_close (fd); fd = fd1; mc_fstat (fd, &st); } vfs_path_free (vpath1); g_free (tmp_filename); } mcview_set_datasource_file (view, fd, &st); } retval = TRUE; } finish: view->command = g_strdup (command); view->dpy_start = 0; view->search_start = 0; view->search_end = 0; view->dpy_text_column = 0; mcview_compute_areas (view); mcview_update_bytes_per_line (view); if (mcview_remember_file_position && view->filename_vpath != NULL && start_line == 0) { long line, col; off_t new_offset, max_offset; load_file_position (view->filename_vpath, &line, &col, &new_offset, &view->saved_bookmarks); max_offset = mcview_get_filesize (view) - 1; if (max_offset < 0) new_offset = 0; else new_offset = min (new_offset, max_offset); if (!view->hex_mode) view->dpy_start = mcview_bol (view, new_offset, 0); else { view->dpy_start = new_offset - new_offset % view->bytes_per_line; view->hex_cursor = new_offset; } } else if (start_line > 0) mcview_moveto (view, start_line - 1, 0); view->hexedit_lownibble = FALSE; view->hexview_in_text = FALSE; view->change_list = NULL; vfs_path_free (vpath); return retval; }
void server_mc_event_cb(struct bufferevent *bev, short events, void *ctx) { struct server *s = (struct server *)ctx; dump_flags(events); if (events & BEV_EVENT_CONNECTED) { struct mc *mc; /* * If we received the notification that the connection is established, * then we move the corresponding struct mc from s->pending_peers to * s->peers. */ mc = v_mc_find_if(s->pending_peers, (void *)find_bev, bev); if (mc != v_mc_end(s->pending_peers)) { struct mc tmp; struct endpoint e; endpoint_init(&e, mc->p.address, mc->p.len); /* Check for certificate */ if (mc->ssl_flags & TLS_ENABLE) { X509 *cert; SSL *ssl; EVP_PKEY *pubkey; char name[512]; ssl = bufferevent_openssl_get_ssl(mc->bev); cert = SSL_get_peer_certificate(ssl); if (cert == NULL) { log_info("[META] [TLS] %s doesn't share it's certificate.", mc_presentation(mc, name, sizeof name)); v_mc_erase(s->pending_peers, mc); mc_close(mc); return ; } pubkey = X509_get_pubkey(cert); //UNUSED ? } log_info("[META] [%s] connexion established with %s", mc->ssl_flags & TLS_ENABLE ? "TLS" : "TCP", endpoint_presentation(&e)); memcpy(&tmp, mc, sizeof(tmp)); v_mc_erase(s->pending_peers, mc); mc = v_mc_insert(s->peers, &tmp); mc_hello(mc, s->udp); mc_establish_tunnel(mc, s->udp); } } else if (events & BEV_EVENT_EOF) { /* Disconnected */ struct mc *mc; struct udp_peer *up; mc = v_mc_find_if(s->peers, (void *)find_bev, bev); if (mc != v_mc_end(s->peers)) { char name[INET6_ADDRSTRLEN]; struct sockaddr *sock = mc->p.address; up = v_udp_find_if(s->udp->udp_peers, find_udppeer, sock); if (up != v_udp_end(s->udp->udp_peers)) { v_udp_erase(s->udp->udp_peers, up); log_debug("[%s] stop peering with %s", (up->ssl_flags & DTLS_ENABLE) ? "DTLS" : "UDP", endpoint_presentation(&up->peer_addr)); } log_debug("[META] stop the meta-connexion with %s", mc_presentation(mc, name, sizeof(name))); mc_close(mc); v_mc_erase(s->peers, mc); } } else if (events & BEV_EVENT_ERROR) { struct mc *mc; int everr; int sslerr; everr = EVUTIL_SOCKET_ERROR(); if (everr != 0) { log_warnx("[META] unexpected shutdown of the meta-connexion: (%d) %s", everr, evutil_socket_error_to_string(everr)); } while ((sslerr = bufferevent_get_openssl_error(bev)) != 0) { log_warnx("[META] SSL error code (%d): %s in %s %s", sslerr, ERR_reason_error_string(sslerr), ERR_lib_error_string(sslerr), ERR_func_error_string(sslerr)); } /* * Find if the exception come from a pending peer or a * regular peer and close it. */ mc = v_mc_find_if(s->pending_peers, (void *)find_bev, bev); if (mc != v_mc_end(s->pending_peers)) { char name[128]; log_debug("[META] %s removed from the pending list", mc_presentation(mc, name, sizeof name)); mc_close(mc); v_mc_erase(s->pending_peers, mc); } else { mc = v_mc_find_if(s->peers, (void *)find_bev, bev); if (mc != v_mc_end(s->peers)) { mc_close(mc); v_mc_erase(s->peers, mc); log_debug("[META] socket removed from the peer list"); } } } }
void mcview_growbuf_read_until (mcview_t * view, off_t ofs) { ssize_t nread; byte *p; size_t bytesfree; gboolean short_read; assert (view->growbuf_in_use); if (view->growbuf_finished) return; short_read = FALSE; while (mcview_growbuf_filesize (view) < ofs || short_read) { if (view->growbuf_lastindex == VIEW_PAGE_SIZE) { /* Append a new block to the growing buffer */ byte *newblock = g_try_malloc (VIEW_PAGE_SIZE); if (newblock == NULL) return; g_ptr_array_add (view->growbuf_blockptr, newblock); view->growbuf_lastindex = 0; } p = g_ptr_array_index (view->growbuf_blockptr, view->growbuf_blockptr->len - 1) + view->growbuf_lastindex; bytesfree = VIEW_PAGE_SIZE - view->growbuf_lastindex; if (view->datasource == DS_STDIO_PIPE) { nread = fread (p, 1, bytesfree, view->ds_stdio_pipe); if (nread == 0) { view->growbuf_finished = TRUE; (void) pclose (view->ds_stdio_pipe); mcview_display (view); close_error_pipe (D_NORMAL, NULL); view->ds_stdio_pipe = NULL; return; } } else { assert (view->datasource == DS_VFS_PIPE); do { nread = mc_read (view->ds_vfs_pipe, p, bytesfree); } while (nread == -1 && errno == EINTR); if (nread == -1 || nread == 0) { view->growbuf_finished = TRUE; (void) mc_close (view->ds_vfs_pipe); view->ds_vfs_pipe = -1; return; } } short_read = ((size_t) nread < bytesfree); view->growbuf_lastindex += nread; } }
int child_mc_xlate_error(int cmd) { int rc; struct ctx myctx; struct ctx *p_ctx = &myctx; int invalid=0; __u64 plba; __u64 size; mc_stat_t l_mc_stat; if(mc_init() !=0 ) { fprintf(stderr, "mc_init failed.\n"); return -1; } debug("mc_init success.\n"); rc = ctx_init(p_ctx); if(rc != 0) { fprintf(stderr, "Context init failed, errno %d\n", errno); return -1; } rc = mc_register(master_dev_path, p_ctx->ctx_hndl, (volatile __u64 *)p_ctx->p_host_map,&p_ctx->mc_hndl); if(rc != 0) { fprintf(stderr, "mc_register: failed. ctx_hndl %d, rc %d\n",p_ctx->ctx_hndl, rc ); return -1; } rc = mc_open(p_ctx->mc_hndl,MC_RDWR,&p_ctx->res_hndl); if(rc != 0) { fprintf(stderr, "ctx: %d:mc_open: failed,rc %d\n", p_ctx->ctx_hndl,rc); return -1; } rc = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat); CHECK_RC(rc, "mc_stat"); if(1 == cmd) //without mc_size { rc = mc_xlate_lba(p_ctx->mc_hndl, p_ctx->res_hndl, 0,&plba); rc = rc ? 1:0; } else { rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl,1,&size); if(2 == cmd) //MCH NULL { rc = mc_xlate_lba(NULL,p_ctx->res_hndl,0,&plba); debug("MCH NULL rc = %d\n",rc); rc = rc ? 2:0; } else if(3 == cmd) //invalid RCH { rc = mc_xlate_lba(p_ctx->mc_hndl,(p_ctx->res_hndl +4),0,&plba); rc = rc ? 3:0; } else if(4 == cmd) //invalid VLBA { rc = mc_xlate_lba(p_ctx->mc_hndl,p_ctx->res_hndl,((1 << l_mc_stat.nmask)+5),&plba); rc = rc ? 4:0; } else if(5 == cmd) //NULL to plba { rc = mc_xlate_lba(p_ctx->mc_hndl,p_ctx->res_hndl,0,NULL); rc = rc ? 5:0; } else if(6 == cmd) //diff MCH(no mc_open) & RCH with mc_size { struct ctx tctx; struct ctx *p_tctx= &tctx; rc = ctx_init(p_tctx); rc = mc_register(master_dev_path, p_tctx->ctx_hndl, (volatile __u64 *)p_tctx->p_host_map,&p_tctx->mc_hndl); rc = mc_open(p_tctx->mc_hndl,MC_RDWR,&p_tctx->res_hndl); rc = mc_xlate_lba(p_tctx->mc_hndl,p_ctx->res_hndl,0,&plba); rc = rc ? 6:0; mc_close(p_tctx->mc_hndl,p_tctx->res_hndl); mc_unregister(p_tctx->mc_hndl); ctx_close(p_tctx); } else if(7 == cmd) //invaliud MCH { rc = mc_xlate_lba((mc_hndl_t)&invalid,p_ctx->res_hndl,0,&plba); rc = rc ? 7:0; } } mc_close(p_ctx->mc_hndl, p_ctx->res_hndl); if(8 == cmd) //after mc_close { rc = mc_xlate_lba(p_ctx->mc_hndl,p_ctx->res_hndl,0,&plba); rc = rc ? 8:0; } mc_unregister(p_ctx->mc_hndl); ctx_close(p_ctx); mc_term(); return rc; }
/* * create two ctx process & 2 resource handler each ctx * use diff ctx handler in diff process, get another process * ctx handler through PIPE. */ int mc_test_inter_prcs_ctx_int(int cmd) { int rc; struct ctx myctx; struct ctx *p_ctx = &myctx; res_hndl_t res_hndl; ctx_hndl_t ctx_hndl; int pdes[2]; pid_t cpid; pthread_t thread; __u64 stride = 0x1000; int i; //create pipe, child open for write // parent open for read pipe(pdes); cpid = fork(); if( 0 == cpid) { //child one running pid = getpid(); debug("%d : child do init_mc \n", pid); rc = init_mc(p_ctx, &res_hndl); if(rc) { fprintf(stderr, "%d : exiting due to init_mc\n:", pid); exit(rc); } //do write into pipe & wait until parent kill me close(pdes[0]); //close read des write(pdes[1], &p_ctx->ctx_hndl, sizeof(ctx_hndl_t)); while(1); } else { //parent close(pdes[1]); //close write des //lets child do there work & wait for me sleep(1); pid = getpid(); rc = init_mc(p_ctx, &res_hndl); if(rc) { kill(cpid, SIGKILL); return rc; } pthread_create(&thread,NULL,ctx_rrq_rx, p_ctx); read(pdes[0], &ctx_hndl, sizeof(ctx_hndl_t)); fill_send_write(p_ctx, 0, pid, stride, VLBA); //set another process ctx debug("%d : use child(%d)process ctx hndl: %d\n", pid, cpid, ctx_hndl); for(i = 0; i< NUM_CMDS; i++) { p_ctx->cmd[i].rcb.ctx_id = ctx_hndl; } if(2 == cmd) { //another test is to close one of my ctx res hndl //and use child ctx handler here //(child has opened 2 res handler) mc_close(p_ctx->mc_hndl, res_hndl); debug("%d : close res_hndl(%d) but child (%d)has opened\n", pid, res_hndl, cpid); for(i = 0; i< NUM_CMDS; i++) { p_ctx->cmd[i].rcb.res_hndl = res_hndl; } } send_cmd(p_ctx); rc = wait_resp(p_ctx); kill(cpid, SIGKILL); pthread_cancel(thread); } return rc; }
int check_mc_null_params(int cmd) { int rc; struct ctx myctx; struct ctx *p_ctx = &myctx; __u64 size = 10; __u64 actual_size=0; if(mc_init() !=0 ) { fprintf(stderr, "mc_init failed.\n"); return -1; } debug("mc_init success.\n"); rc = ctx_init(p_ctx); if(rc != 0) { fprintf(stderr, "Context init failed, errno %d\n", errno); return -1; } if(1 == cmd) { //mc_reg with NULL MCH rc = mc_register(master_dev_path, p_ctx->ctx_hndl, (volatile __u64 *) p_ctx->p_host_map,NULL); rc = rc ? 1:0; } else { rc = mc_register(master_dev_path, p_ctx->ctx_hndl, (volatile __u64 *)p_ctx->p_host_map,&p_ctx->mc_hndl); if(rc != 0) { fprintf(stderr, "ctx _reg failed, ctx_hndl %d,rc %d\n", p_ctx->ctx_hndl, rc ); return -1; } if(2 == cmd){ rc = mc_unregister(NULL); rc = rc ? 2:0; } else if(3 == cmd){ //mc_open NULL rc = mc_open(NULL, MC_RDWR, &p_ctx->res_hndl); rc = rc ? 3:0; } else if (4 == cmd) { rc = mc_hdup(NULL, p_ctx->mc_hndl); rc = rc ? 4:0; } else { rc = mc_open(p_ctx->mc_hndl, MC_RDWR, &p_ctx->res_hndl); if(5 == cmd) { rc = mc_close(NULL, p_ctx->res_hndl); rc = rc ? 5:0; }else if( 6 == cmd) { rc = mc_size(NULL, p_ctx->res_hndl, size, &actual_size); rc = rc ? 6:0; }else if(7 == cmd) { rc = mc_clone(NULL, p_ctx->mc_hndl, MC_RDWR); rc = rc ? 7:0; } } } return rc; }
// Upon return no more I/O is possible. The stream is closed. // // If ne_close indicates a recoverable error, then the object is // logged to the "degraded object log". int mc_sync(DAL_Context* ctx) { ENTRY(); ObjectStream* os = MC_OS(ctx); ne_handle handle = MC_HANDLE(ctx); MC_Config* config = MC_CONFIG(ctx); MC_Context* mc_context = MC_CONTEXT(ctx); if(! (os->flags & OSF_OPEN)) { LOG(LOG_ERR, "%s isn't open\n", os->url); errno = EINVAL; return -1; } // the result of close for a handle opened for reading is an // indicator of whether the data is degraded and, if so, which // block is corrupt or missing. int error_pattern = ne_close(handle); if(error_pattern > 0) { // Keeping the log message as well as writing to the degraded // object file for debugging purposes. LOG(LOG_INFO, "WARNING: Object %s degraded. Error pattern: 0x%x." " (N: %d, E: %d, Start: %d).\n", mc_context->path_template, error_pattern, config->n, config->e, mc_context->start_block); // we shouldn't need more then 512 bytes to hold the extra data // needed for rebuild char buf[MC_MAX_LOG_LEN]; snprintf(buf, MC_MAX_LOG_LEN, MC_DEGRADED_LOG_FORMAT, mc_context->path_template, config->n, config->e, mc_context->start_block, error_pattern, MC_FH(ctx)->info.pre.repo->name, mc_context->pod, mc_context->cap); WAIT(&config->lock); // If the degraded log file has not already been opened, open it now. if(config->degraded_log_fd == -1) { config->degraded_log_fd = open_degraded_object_log(config->degraded_log_path); if(config->degraded_log_fd < 0) { LOG(LOG_ERR, "failed to open degraded log file\n"); } else { // If we successfully opened it, then free the resources // used to store the path. free(config->degraded_log_path); config->degraded_log_path = NULL; } } if(write(config->degraded_log_fd, buf, strlen(buf)) != strlen(buf)) { LOG(LOG_ERR, "Failed to write to degraded object log\n"); // theoretically the data is still safe, so we can just log // and ignore the failure. } POST(&config->lock); } else if(error_pattern < 0) { // close the stream, a failed sync renders the ne_handle // invalid calling mc_close should prevent marfs from ever // trying to use it again. mc_close(ctx); os->flags |= OSF_ERRORS; LOG(LOG_ERR, "ne_close failed on %s", mc_context->path_template); return -1; } EXIT(); return 0; }