Search Posts

code crash in multithread mode of FUSE

/*
 FUSE: Filesystem in Userspace
 Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>

 This program can be distributed under the terms of the GNU GPL.
 See the file COPYING.

 gcc -Wall hello.c `pkg-config fuse --cflags --libs` -o hello
 */

#define FUSE_USE_VERSION 26

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <jni.h>
#include <pthread.h>

static const char *peter_str = "Hello World!\n";
static const char *peter_path = "/hello";
pthread_mutex_t mutex1;
static jclass cls;
jmethodID readdir;
static JNIEnv *env;
static JavaVM *jvm;

void create_vm() {
	//JNIEnv *env;
	JavaVMInitArgs vm_args;
	JavaVMOption options;
	options.optionString =
			"-Djava.class.path=../target/PFSBuilder-20140619.jar";
	vm_args.version = JNI_VERSION_1_6;
	vm_args.nOptions = 1;
	vm_args.options = &options;
	vm_args.ignoreUnrecognized = 0;

	JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
	return env;
}

static int peter_getattr(const char *path, struct stat *stbuf) {
	printf("--------------- peter_getattr = %s\n", path);
	int res = 0;

	memset(stbuf, 0, sizeof(struct stat));
	if (strcmp(path, "/") == 0) {
		printf("--------------- root /\n");
		stbuf->st_mode = S_IFDIR | 0755;
		stbuf->st_nlink = 2;
	} else if (strcmp(path, peter_path) == 0) {
		stbuf->st_mode = S_IFREG | 0444;
		stbuf->st_nlink = 1;
		stbuf->st_size = strlen(peter_str);
	} else
		res = -ENOENT;

	return res;
}

static int peter_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
		off_t offset, struct fuse_file_info *fi) {
	printf("--------------- peter_readdir = %s\n", path);
	(void) offset;
	(void) fi;

	if (strcmp(path, "/") != 0)
		return -ENOENT;

	filler(buf, ".", NULL, 0);
	filler(buf, "..", NULL, 0);
	filler(buf, peter_path + 1, NULL, 0);

	pthread_mutex_lock(&mutex1);
	jstring paraReadDir = (*env)->NewStringUTF(env, "Peter cheung");
	(*env)->CallStaticVoidMethod(env, cls, readdir, paraReadDir);
	pthread_mutex_unlock(&mutex1);
	return 0;
}

static int peter_open(const char *path, struct fuse_file_info *fi) {
	printf("--------------- peter_open = %s\n", path);
	if (strcmp(path, peter_path) != 0)
		return -ENOENT;

	if ((fi->flags & 3) != O_RDONLY)
		return -EACCES;

	return 0;
}

static int peter_read(const char *path, char *buf, size_t size, off_t offset,
		struct fuse_file_info *fi) {
	printf("--------------- peter_read = %s\n", path);
	size_t len;
	(void) fi;
	if (strcmp(path, peter_path) != 0)
		return -ENOENT;

	len = strlen(peter_str);
	if (offset < len) {
		if (offset + size > len)
			size = len - offset;
		memcpy(buf, peter_str + offset, size);
	} else
		size = 0;

	return size;
}

static struct fuse_operations peter_oper = { .getattr = peter_getattr,
		.readdir = peter_readdir, .open = peter_open, .read = peter_read, };

int main(int argc, char *argv[]) {
	create_vm();
	printf("s2\n");
	cls = (*env)->FindClass(env, "com/pfsbuilder/fuse/FuseStub");

	printf("s4\n");
	if (cls == 0) {
		printf("load class error\n");
		exit(-1);
	}
	printf("s4\n");
	readdir = (*env)->GetStaticMethodID(env, cls, "readdir",
			"(Ljava/lang/String;)V");
	if (readdir == 0) {
		printf("load readdir error\n");
		exit(-1);
	}
	jstring paraReadDir = (*env)->NewStringUTF(env, "Peter cheung");
	(*env)->CallStaticVoidMethod(env, cls, readdir, paraReadDir);
	printf(
			"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");

	pthread_mutex_init(&mutex1, NULL);
	return fuse_main(argc, argv, &peter_oper, NULL);
}

1 comment on code crash in multithread mode of FUSE

  1. need these two things to me it works
    1) fuse_get_context
    2) (*userdata->jvm)->AttachCurrentThreadAsDaemon(userdata->jvm, (void**)&userdata->env, NULL);

Leave a Reply to admin Cancel reply

Your email address will not be published. Required fields are marked *