コード例 #1
0
ファイル: xc_shm_mmap.c プロジェクト: alepharchives/xcache
/* }}} */
static XC_SHM_TO_READWRITE(xc_mmap_to_readwrite) /* {{{ */
{
	if (shm->diff) {
		assert(xc_mmap_is_readonly(shm, p));
		p = PTR_SUB(p, shm->diff);
	}
	assert(xc_mmap_is_readwrite(shm, p));
	return p;
}
コード例 #2
0
static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
{
	struct save_area *sa;
	void *ptr_start = ptr;
	int i;

	ptr = nt_prpsinfo(ptr);

	for (i = 0; zfcpdump_save_areas[i]; i++) {
		sa = zfcpdump_save_areas[i];
		if (sa->pref_reg == 0)
			continue;
		ptr = fill_cpu_elf_notes(ptr, sa);
	}
	ptr = nt_vmcoreinfo(ptr);
	memset(phdr, 0, sizeof(*phdr));
	phdr->p_type = PT_NOTE;
	phdr->p_offset = relocate(notes_offset);
	phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
	phdr->p_memsz = phdr->p_filesz;
	return ptr;
}
コード例 #3
0
ファイル: xc_shm_mmap.c プロジェクト: alepharchives/xcache
/* }}} */
static XC_SHM_INIT(xc_mmap_init) /* {{{ */
{
#ifdef ZEND_WIN32
#	define TMP_PATH "XCache"
#else
#	define TMP_PATH "/tmp/XCache"
	int fd = -1;
#endif
	xc_shm_t *shm = NULL;
	int ro_ok;
	volatile void *romem;
	char tmpname[sizeof(TMP_PATH) - 1 + 4 * 10 + 100] = { 0 };
	const char *errstr = NULL;
	const char *path = (const char *) arg1;
	static int instanceId = 0;

	CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
	shm->size = size;

	if (path == NULL || !path[0]) {
		snprintf(tmpname, sizeof(tmpname) - 1, "%s.%d.%d.%d", TMP_PATH, (int) getuid(), (int) getpid(), ++instanceId);
		path = tmpname;
	}
#ifdef ZEND_WIN32
	else {
		snprintf(tmpname, sizeof(tmpname) - 1, "%s.%d.%d.%d", path, (int) getuid(), (int) getpid(), ++instanceId);
		path = tmpname;
	}
#endif

	shm->name = strdup(path);

#ifndef ZEND_WIN32
#	define XCACHE_MMAP_PERMISSION (S_IRUSR | S_IWUSR)
	fd = open(shm->name, O_RDWR, XCACHE_MMAP_PERMISSION);
	if (fd == -1) {
		/* do not create file in /dev */
		if (strncmp(shm->name, "/dev", 4) == 0) {
			perror(shm->name);
			errstr = "Cannot open file set by xcache.mmap_path, check the xcache.size/var_size against system limitation";
			goto err;
		}
		fd = open(shm->name, O_CREAT | O_RDWR, XCACHE_MMAP_PERMISSION);
		shm->newfile = 1;
		if (fd == -1) {
			perror(shm->name);
			errstr = "Cannot open or create file set by xcache.mmap_path, check the path permission or check xcache.size/var_size against system limitation";
			goto err;
		}
	}

	if (ftruncate(fd, size) != 0 && errno != EINVAL) {
		perror(shm->name);
		errstr = "Failed to ftruncate the file";
		goto err;
	}
#endif

#ifdef ZEND_WIN32
	shm->hmap = XCacheCreateFileMapping(size, PAGE_READWRITE, shm->name);
	shm->ptr = (LPSTR) MapViewOfFile(shm->hmap, FILE_MAP_WRITE, 0, 0, 0);
#else
	shm->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
#endif

	if (shm->ptr == XCACHE_MAP_FAILED) {
		perror(shm->name);
		errstr = "Failed creating file mapping";
		shm->ptr = NULL;
		goto err;
	}

	/* {{{ readonly protection, mmap it readonly and check if ptr_ro works */
	if (readonly_protection) {
		ro_ok = 0;

#ifdef ZEND_WIN32
		shm->hmap_ro = XCacheCreateFileMapping(size, PAGE_READONLY, shm->name);
		shm->ptr_ro = (LPSTR) MapViewOfFile(shm->hmap_ro, FILE_MAP_READ, 0, 0, 0);
#else
		shm->ptr_ro = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
#endif
		if (shm->ptr_ro == XCACHE_MAP_FAILED) {
			shm->ptr_ro = NULL;
		}
		romem = shm->ptr_ro;

		do {
			if (romem == NULL || romem == shm->ptr) {
				break;
			}
			*(char *)shm->ptr = 1;
			if (*(char *)romem != 1) {
				break;
			}
			*(char *)shm->ptr = 2;
			if (*(char *)romem != 2) {
				break;
			}
			ro_ok = 1;
		} while (0);

		if (ro_ok) {
			shm->diff = PTR_SUB(shm->ptr_ro, (char *) shm->ptr);
			/* no overlap */
			assert((xc_shmsize_t) abs(shm->diff) >= size);
		}
		else {
			if (shm->ptr_ro) {
				munmap(shm->ptr_ro, size);
			}
#ifdef ZEND_WIN32
			if (shm->hmap_ro) {
				CloseHandle(shm->hmap_ro);
			}
#endif
			shm->ptr_ro = NULL;
			shm->diff = 0;
		}
	}

	/* }}} */

#ifndef ZEND_WIN32
	close(fd);

#	ifndef __CYGWIN__
	if (shm->newfile) {
		unlink(shm->name);
	}
#	endif
#endif

	return shm;

err:
#ifndef ZEND_WIN32
	if (fd != -1) {
		close(fd);
	}
#endif
	if (shm) {
		xc_mmap_destroy(shm);
	}
	if (errstr) {
		fprintf(stderr, "%s\n", errstr);
		zend_error(E_ERROR, "%s", errstr);
	}
	return NULL;
}