예제 #1
0
int init_mc(struct ctx *p_ctx, res_hndl_t *res_hndl)
{
    int rc;
    __u64 chunks=16;
    __u64 actual_size=0;

    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");

    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_open(p_ctx->mc_hndl,MC_RDWR, res_hndl);
    CHECK_RC(rc, "opening res_hndl");

    rc = mc_size(p_ctx->mc_hndl, *res_hndl, chunks, &actual_size);
    CHECK_RC(rc, "mc_size");
	return 0;
}
예제 #2
0
/* 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;
}
예제 #3
0
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;
}
예제 #4
0
/* 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;
}
예제 #5
0
파일: common.c 프로젝트: sfionov/mc-dev
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;
}
예제 #6
0
파일: interface.c 프로젝트: soulik/mc
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;
}
예제 #7
0
파일: common.c 프로젝트: ginggs/maemo-mc
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;
}
예제 #8
0
파일: interface.c 프로젝트: ryanlee/mc
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;
}
예제 #9
0
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;
}
예제 #10
0
파일: mcviewer.c 프로젝트: CTU-OSP/mc
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;
}
예제 #11
0
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;
}
예제 #12
0
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;
}
예제 #13
0
int test_mc_invalid_opcode()
{
    int rc;
    struct ctx myctx;
    struct ctx *p_ctx = &myctx;
    __u64 chunks=10;
    __u64 actual_size=0;
    __u64 vlba =0;
    __u64 *p_u64;
    __u32 *p_u32;
	mc_stat_t l_mc_stat;
    pthread_t thread;

    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;
    }
    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);
    if(rc != 0)
    {
        fprintf(stderr, "ctx _reg 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, "error opening res_hndl rc %d\n", rc);
        return -1;
    }

    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");
	
	pid = getpid();
	vlba = (actual_size * (1 << l_mc_stat.nmask))-1;
	fill_buf((__u64*)&p_ctx->wbuf[0][0],
		sizeof(p_ctx->wbuf[0])/sizeof(__u64),pid);

	memset(&p_ctx->cmd[0].rcb.cdb[0], 0, sizeof(p_ctx->cmd[0].rcb.cdb));
	p_u64 = (__u64*)&p_ctx->cmd[0].rcb.cdb[2];

	p_ctx->cmd[0].rcb.res_hndl = p_ctx->res_hndl;
	p_ctx->cmd[0].rcb.req_flags = SISL_REQ_FLAGS_RES_HNDL;
	p_ctx->cmd[0].rcb.req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
	write_64(p_u64, vlba); // write(16) Virtual LBA

	p_ctx->cmd[0].rcb.data_ea = (__u64) &p_ctx->wbuf[0][0];

	p_ctx->cmd[0].rcb.data_len = sizeof(p_ctx->wbuf[0]);
	p_ctx->cmd[0].rcb.cdb[0] = 0xFA; // invalid opcode

	p_u32 = (__u32*)&p_ctx->cmd[0].rcb.cdb[10];
	write_32(p_u32, 8); // 8 LBAs for 4K

	p_ctx->cmd[0].sa.host_use[0] = 0; // 0 means active
	p_ctx->cmd[0].sa.ioasc = 0;
	send_single_cmd(p_ctx);
	rc = wait_single_resp(p_ctx);
	return rc;
}
예제 #14
0
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;
}
예제 #15
0
int child_mc_reg_error(int cmd)
{
	int rc;
	struct ctx myctx;
	struct ctx *p_ctx = &myctx;
	__u64 *map=(__u64 *)0xabcdf;
	__u64 actual_size=0;
	__u64 stride;
	__u64 st_lba =0;
	__u64 nlba;
	mc_hndl_t new_mc_hndl, dup_mc_hndl;
	int rc1, rc2, rc3, rc4, rc5;
	pthread_t thread;
	mc_stat_t l_mc_stat;
	__u64 size = 128;

	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;
	}
	pid = getpid();
	if(1 == cmd) //mc_reg with NULL MMIOP
	{
		pthread_create(&thread,NULL,ctx_rrq_rx, p_ctx);
		rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
				NULL,&p_ctx->mc_hndl);
		if(rc) return rc;
		rc = mc_open(p_ctx->mc_hndl,MC_RDWR, &p_ctx->res_hndl);
		if(rc) return rc;
		rc = mc_open(p_ctx->mc_hndl,MC_RDWR, &p_ctx->res_hndl);
		if(rc) return rc;
		rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl, 2, &actual_size);
		if(rc) return rc;
		rc = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat);
		if(rc) return rc;
		pid = getpid();
		stride = (1 << l_mc_stat.nmask);
		st_lba = (actual_size * (1 << l_mc_stat.nmask))-1;
		rc = send_write(p_ctx, st_lba ,stride, pid, VLBA);	
		if(rc) return rc;
		rc = send_read(p_ctx, st_lba ,stride, VLBA);
		if(rc) return rc;
		rc = rw_cmp_buf(p_ctx, st_lba);
		rc = rc ? 1:0;
	}
	else if(2 == cmd) //NULL device path
	{
		rc = mc_register(NULL, p_ctx->ctx_hndl,
				(volatile __u64 *) p_ctx->p_host_map,&p_ctx->mc_hndl);
		rc = rc ? 2:0;
	}
	else if(3 == cmd) //with afu_path device 
	{
		rc = mc_register(afu_path, p_ctx->ctx_hndl,
				(volatile __u64 *) p_ctx->p_host_map,&p_ctx->mc_hndl);
		rc = rc ? 3:0;
	}
	else if(4 == cmd) //with invalid device  path
	{
		rc = mc_register("/dev/cxl/afu50.0m", p_ctx->ctx_hndl,
				(volatile __u64 *) p_ctx->p_host_map,&p_ctx->mc_hndl);
		rc = rc ? 4:0;
	}
	else if(5 == cmd) //with invalid ctx hndl(not assigned)
	{
		debug("actual ctx hndl :%d\n", p_ctx->ctx_hndl);
		p_ctx->ctx_hndl = p_ctx->ctx_hndl + 4;
		debug("invalid ctx hndl :%d\n", p_ctx->ctx_hndl);
		rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
				(volatile __u64 *) p_ctx->p_host_map,&p_ctx->mc_hndl);
		rc = rc ? 5:0;
	}
	else if(6 == cmd) //with invalid mmap adress
	{
		rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
				(volatile __u64 *)map,&p_ctx->mc_hndl);
		rc = rc ? 6:0;
	}
	else if(7 == cmd) //twice mc_reg
	{
		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, "mc_register");

		rc = mc_open(p_ctx->mc_hndl, MC_RDWR, &p_ctx->res_hndl);
		CHECK_RC(rc, "mc_open");

		rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl, size, &actual_size);
		CHECK_RC(rc, "mc_size");

		rc = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat);

		//twice mc_register on same ctx
		rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
				(volatile __u64 *)p_ctx->p_host_map, &new_mc_hndl);
		//send write on 1st mc hndl
		rc1 = send_single_write(p_ctx, 0, pid);
		//do mc_size & open on old mc_reg
		rc2 = mc_open(p_ctx->mc_hndl, MC_RDWR, &p_ctx->res_hndl);
		rc3 = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl, size, &actual_size);
		rc4 = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat);
		rc5 = mc_hdup(p_ctx->mc_hndl, &dup_mc_hndl);
		debug("mc_hdup  rc is : %d\n", rc5);

		//now do mc_unreg on old one
		rc = mc_unregister(p_ctx->mc_hndl);
		CHECK_RC(rc, "mc_unregister");
		//do everything on new mc hndl
		p_ctx->mc_hndl = new_mc_hndl;
		rc = mc_open(p_ctx->mc_hndl, MC_RDWR, &p_ctx->res_hndl);
		CHECK_RC(rc, "mc_open");

		rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl, size, &actual_size);
		CHECK_RC(rc, "mc_size");

		rc = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat);
		nlba = l_mc_stat.size * (1 << l_mc_stat.nmask);
		stride = 1 << l_mc_stat.nmask;
		for(st_lba = 0; st_lba < nlba; st_lba += (stride * NUM_CMDS)) {
			rc = send_write(p_ctx, st_lba, stride, pid, VLBA);
			CHECK_RC(rc, "send_write");
		}
		if(rc1 && rc2 && rc3 && rc4 && rc5) {
			rc = 7;
		}
		pthread_cancel(thread);
		mc_unregister(p_ctx->mc_hndl);
	}
	else if(8 == cmd) //mc_reg twice from 2 diff process
	{

		if(fork() == 0) {//mc_reg in child process as well
			pid = getpid();
			rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
				(volatile __u64 *)p_ctx->p_host_map, &p_ctx->mc_hndl);
			sleep(1);
			rc = mc_open(p_ctx->mc_hndl,MC_RDWR, &p_ctx->res_hndl);
			if(!rc) {
				fprintf(stderr, "%d : mc_open should fail rc = %d\n", pid, rc);
				exit(-1);
			}
			else {
				debug("%d : mc_open failed as expectd\n", pid);
			}
			rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl, 2, &actual_size);
			if(!rc) {
				fprintf(stderr, "%d : mc_size should fail rc = %d\n", pid, rc);
				exit(-1);
			}
			else {
				debug("%d : mc_size failed as expectd\n", pid);
			}
			rc = rc ? 8:0;
			exit(rc);
		}
		else
		{
			sleep(1); //let child proc cal mc_reg 1str
			rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
			(volatile __u64 *)p_ctx->p_host_map, &p_ctx->mc_hndl);
			CHECK_RC(rc, "mc_register");

			pthread_create(&thread,NULL,ctx_rrq_rx, p_ctx);
			rc = mc_open(p_ctx->mc_hndl,MC_RDWR, &p_ctx->res_hndl);
			CHECK_RC(rc, "mc_open");
			rc = mc_size(p_ctx->mc_hndl, p_ctx->res_hndl, 2, &actual_size);
			CHECK_RC(rc, "mc_mc_size");
			rc = mc_stat(p_ctx->mc_hndl, p_ctx->res_hndl, &l_mc_stat);
			CHECK_RC(rc, "mc_stat");

			st_lba = (actual_size * (1 << l_mc_stat.nmask))-1;
			rc += send_single_write(p_ctx, st_lba, pid);
			wait(&rc);
			pthread_cancel(thread);
			if (WIFEXITED(rc)) {
				rc =  WEXITSTATUS(rc);
				rc = rc ? 8:0;
			}
			mc_unregister(p_ctx->mc_hndl);
		}
	}

	ctx_close(p_ctx);	
	if(9 == cmd) //mc_reg with closed ctx
	{
		pthread_create(&thread,NULL,ctx_rrq_rx, p_ctx);
		printf("calling mc_reg api after ctx close..\n");
		rc = mc_register(master_dev_path, p_ctx->ctx_hndl,
			(volatile __u64 *)p_ctx->p_host_map, &p_ctx->mc_hndl);
		rc = rc ? 9:0;
	}

	mc_term();
	return rc;
}