Esempio n. 1
0
static int sock_av_close(struct fid *fid)
{
    struct sock_av *av;
    int ret = 0;
    av = container_of(fid, struct sock_av, av_fid.fid);
    if (atomic_get(&av->ref))
        return -FI_EBUSY;

    if (!av->name)
        free(av->table_hdr);
    else {
        shm_unlink(av->name);
        free(av->name);
        ret = munmap(av->table_hdr, SOCK_AV_TABLE_SZ(av->attr.count, av->name));
        if (ret)
            SOCK_LOG_ERROR("munmap failed: %s\n", strerror(errno));
        close(av->shared_fd);
    }

    atomic_dec(&av->domain->ref);
    free(av);
    return 0;
}
Esempio n. 2
0
int
main(int argc, char **argv)
{
	int		fd;
	struct shmstruct	*ptr;

	if (argc != 3)
		err_quit("usage: server1 <shmname> <semname>");

	shm_unlink(Px_ipc_name(argv[1]));		/* OK if this fails */
		/* 4create shm, set its size, map it, close descriptor */
	fd = Shm_open(Px_ipc_name(argv[1]), O_RDWR | O_CREAT | O_EXCL, FILE_MODE);
	Ftruncate(fd, sizeof(struct shmstruct));
	ptr = Mmap(NULL, sizeof(struct shmstruct), PROT_READ | PROT_WRITE,
			   MAP_SHARED, fd, 0);
	Close(fd);

	sem_unlink(Px_ipc_name(argv[2]));		/* OK if this fails */
	mutex = Sem_open(Px_ipc_name(argv[2]), O_CREAT | O_EXCL, FILE_MODE, 1);
	Sem_close(mutex);

	exit(0);
}
Esempio n. 3
0
int FileLib::createUnnamedSharedMemoryTemp(void) {
	int shmfd = -1;
	char tmpfn[1024];
	pid_t pid = getpid();

	do {
		snprintf(tmpfn, sizeof(tmpfn), "/.%d_tmp_%d", pid, rand());

		errno = 0;
		shmfd = shm_open(tmpfn, O_RDWR | O_CREAT | O_EXCL, 0400);
		if (-1 == shmfd) {
			if (errno != EEXIST) {
				break;
			}
		} else {
			
			shm_unlink(tmpfn);
			break;
		}
	} while (true);

	return shmfd;
}
Esempio n. 4
0
void destroy()
{
	mysql_library_end();

	int r = munmap(pIdentityTag, region_size);
	if (r != 0)
	{
		syslog(LOG_ERR, "munmap failed");
	}

	  r = shm_unlink(shared_memory_name);
	  if (r != 0)
	  {
		  syslog(LOG_ERR, "shm_unlink failed");
	  }


	config_destroy(&config);

	syslog (LOG_INFO, "Terminating");

	closelog();
}
Esempio n. 5
0
static void THRefcountedMapAllocator_free(void* ctx_, void* data) {
  THMapAllocatorContext *ctx = ctx_;

#ifdef _WIN32
  if(UnmapViewOfFile(data) == 0)
    THError("could not unmap the shared memory file");
#else /* _WIN32 */

  THMapInfo *info = (THMapInfo*)(((char*)data) - TH_ALLOC_ALIGNMENT);
  if (THAtomicDecrementRef(&info->refcount)) {
#ifdef HAVE_SHM_UNLINK
    if (shm_unlink(ctx->filename) == -1)
      THError("could not unlink the shared memory file %s", ctx->filename);
#else
    THError("could not unlink the shared memory file %s, shm_unlink not available on platform", ctx->filename);
#endif /* HAVE_SHM_UNLINK */
  }
  if (munmap(info, ctx->size))
    THError("could not unmap the shared memory file %s", ctx->filename);
#endif /* _WIN32 */

  THMapAllocatorContext_free(ctx);
}
Esempio n. 6
0
/** \brief Destroy the POSIX shared memory object related to name
 *
 * First \b os_posix_sharedMemoryDestroy finds the shared object identifier
 * related to \b name by calling \b os_posix_getShmObjName. If the identifier is
 * found, the shared object is detroyed by calling \b shm_unlink.
 * After that the key file related to name is detroyed by calling
 * \b os_posix_destroyKeyFile.
 */
os_result
os_posix_sharedMemoryDestroy (
    const char *name)
{
    char *shmname;
    os_result rv = os_resultSuccess;

    assert (name != NULL);
    shmname = os_posix_getShmObjName (name, NULL, 0);
    if (shmname != NULL) {
        if (shm_unlink (shmname) == -1) {
            OS_REPORT (OS_WARNING, "os_posix_sharedMemoryDestroy", 1,
                "shm_unlink failed with error %d (%s)", os_getErrno(), name);
            rv = os_resultFail;
        }
        if (os_posix_destroyKeyFile (name) == -1) {
            rv = os_resultFail;
        }
        os_free (shmname);
    }
    
    return rv;
}
Esempio n. 7
0
void ipc_destroy(ipc_t conn, int owner) {

    if (conn == NULL) {
        return;
    }

    // It goes without saying that the order of these calls is important
    sem_close(conn->lock);

    if (owner) {
        ipc_sem_destroy(conn->queue->readWait);
        ipc_sem_destroy(conn->queue->writeSem);
    }

    munmap(conn->queue, SHMEM_SIZE);

    if (owner) {
        shm_unlink(conn->name);
        sem_unlink(conn->name);
    }

    free(conn);
}
Esempio n. 8
0
int main(int argc,char **argv)
{
    int shm_id;
    char *ptr;
    sem_t *sem;
    
    if(argc!=2)
    {
        printf("usage:shm_open<pathname>\n");
        exit(1);
    }
    shm_id=shm_open(argv[1],O_RDWR|O_CREAT,0644);
    ftruncate(shm_id,100);
    sem=sem_open(argv[1],O_CREAT,0644,1);
    ptr=mmap(NULL,100,PROT_READ|PROT_WRITE,MAP_SHARED,shm_id,0);
    strcpy(ptr,"\0");
    while(1)
    {
        if((strcmp(ptr,"\0"))==0)
        {
           continue;
        }
        else
        {
            if((strcmp(ptr,"q\n"))==0)
             {
                 break;
             }
             sem_wait(sem);
             printf("server:%s",ptr);
             strcpy(ptr,"\0");
             sem_post(sem);
        }
        sem_unlink(argv[1]);
        shm_unlink(argv[1]);
    }
}
Esempio n. 9
0
int main(int argc, char *argv[])
{
	int r;
	const char *memname = "/mymem";
	const size_t region_size = 1024;
	/* 打开共享内存文件 */
	int fd = shm_open(memname, O_CREAT | O_TRUNC | O_RDWR, 0666);
	if (fd == -1) error_out("shm_open");
	/* 将文件截取至指定的大小 */
	r = ftruncate(fd, region_size);
	if (r != 0) error_out("ftruncate");
	/* 将文件映射为内存 */
	void *ptr = mmap(0, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (ptr == MAP_FAILED) error_out("mmap");
	/* 关闭文件 */
	close(fd);
	/* 产生新进程 */
	pid_t pid = fork();
	if (pid == 0) {
		/* 子进程向共享内存写入数据 */
		unsigned long *d = (unsigned long *) ptr;
		*d = 0xdeadbeef;
		exit(0);
	} else {
		/* 父进程等待子进程结束并读共享内存 */
		int status;
		waitpid(pid, &status, 0);
		printf("The data child wrote is %#lx\n", *(unsigned long *)ptr);
	}
	/* 取消内存映射 */
	r = munmap(ptr, region_size);
	if (r != 0) error_out("munmap");
	/* 删除共享内存文件 */
	r = shm_unlink(memname);
	if (r != 0) error_out("shm_unlink");
	return 0;
}
Esempio n. 10
0
void MemArena::GrabLowMemSpace(size_t size)
{
#ifdef _WIN32
    hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, (DWORD)(size), nullptr);
    GetSystemInfo(&sysInfo);
#elif defined(ANDROID)
    // Use ashmem so we don't have to allocate a file on disk!
    fd = ashmem_create_region("PPSSPP_RAM", size);
    // Note that it appears that ashmem is pinned by default, so no need to pin.
    if (fd < 0)
    {
        LOG_ERROR(Common_Memory, "Failed to grab ashmem space of size: %08x  errno: %d", (int)size, (int)(errno));
        return;
    }
#else
    // Try to find a non-existing filename for our shared memory.
    // In most cases the first one will be available, but it's nicer to search
    // a bit more.
    for (int i = 0; i < 10000; i++)
    {
        std::string file_name = Common::StringFromFormat("/citramem.%d", i);
        fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
        if (fd != -1)
        {
            shm_unlink(file_name.c_str());
            break;
        }
        else if (errno != EEXIST)
        {
            LOG_ERROR(Common_Memory, "shm_open failed: %s", strerror(errno));
            return;
        }
    }
    if (ftruncate(fd, size) < 0)
        LOG_ERROR(Common_Memory, "Failed to allocate low memory space");
#endif
}
Esempio n. 11
0
File: dri.c Progetto: L3oV1nc3/VMGL
static Bool
DRIFreePixmapImp(DrawablePtr pDrawable) {
    DRIPixmapBufferPtr shared;
    PixmapPtr pPix;

    if(pDrawable->type != DRAWABLE_PIXMAP)
	return FALSE;

    pPix = (PixmapPtr)pDrawable;

    shared = dixLookupPrivate(&pPix->devPrivates, DRIPixmapBufferPrivKey);

    if(NULL == shared)
	return FALSE;

    close(shared->fd);
    munmap(shared->buffer, shared->length);
    shm_unlink(shared->shmPath);
    xfree(shared);

    dixSetPrivate(&pPix->devPrivates, DRIPixmapBufferPrivKey, (pointer)NULL);

    return TRUE;
}
Esempio n. 12
0
/**
 * @brief Cleanup shared memory resources.
 *
 * @param[in] ni
 */
static void release_shmem_resources(ni_t *ni)
{
    pool_fini(&ni->sbuf_pool);

    if (ni->shmem.comm_pad != MAP_FAILED) {
        munmap(ni->shmem.comm_pad, ni->shmem.comm_pad_size);
        ni->shmem.comm_pad = MAP_FAILED;
    }

    if (ni->shmem.comm_pad_shm_name) {
        /* Destroy the mmaped file so it doesn't pollute.
         * All ranks try it in case rank 0 died. */
        shm_unlink(ni->shmem.comm_pad_shm_name);

        free(ni->shmem.comm_pad_shm_name);
        ni->shmem.comm_pad_shm_name = NULL;
    }

    knem_fini(ni);

#if !USE_KNEM
    PTL_FASTLOCK_DESTROY(&ni->shmem.noknem_lock);
#endif
}
Esempio n. 13
0
static PyObject *
_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
/*[clinic end generated code: output=42f8b23d134b9ff5 input=8dc0f87143e3b300]*/
{
    int rv;
    int async_err = 0;
    const char *name = PyUnicode_AsUTF8(path);
    if (name == NULL) {
        return NULL;
    }
    do {
        Py_BEGIN_ALLOW_THREADS
        rv = shm_unlink(name);
        Py_END_ALLOW_THREADS
    } while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));

    if (rv < 0) {
        if (!async_err)
            PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
        return NULL;
    }

    Py_RETURN_NONE;
}
Esempio n. 14
0
static void cleanup(void)
{
    if (tmpshmem) {
        munmap(tmpshmem, TMPSHMSIZE);
        tmpshmem = NULL;
    }

    if (tmpshm) {
        shm_unlink(tmpshm);
        g_free(tmpshm);
        tmpshm = NULL;
    }

    if (tmpserver) {
        g_unlink(tmpserver);
        g_free(tmpserver);
        tmpserver = NULL;
    }

    if (tmpdir) {
        g_rmdir(tmpdir);
        tmpdir = NULL;
    }
}
Esempio n. 15
0
int main() {
	int fd, result;

	result = shm_unlink(SHM_NAME);
	if (result != 0 && errno != ENOENT) {
		/* The shared memory object exist and shm_unlink can not
		   remove it. */
		perror("An error occurs when calling shm_unlink()");
		return PTS_UNRESOLVED;
	}

	fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR);

	if (fd == -1 && errno == ENOENT) {
		printf("Test PASSED\n");
		return PTS_PASS;
	} else if (fd != -1) {
		printf("shm_open() success.\n");
		return PTS_FAIL;
	}

	perror("Unexpected error");
	return PTS_FAIL;
}
Esempio n. 16
0
static void THMapAllocator_free(void* ctx_, void* data) {
  if (data == NULL)
    return;

  THMapAllocatorContext *ctx = ctx_;

#ifdef _WIN32
  if ((ctx->flags & TH_ALLOCATOR_MAPPED_KEEPFD) || (ctx->flags & TH_ALLOCATOR_MAPPED_SHAREDMEM))
    CloseHandle(ctx->handle);
  if(UnmapViewOfFile(data) == 0)
    THError("could not unmap the shared memory file");
#else /* _WIN32 */
  if (ctx->flags & TH_ALLOCATOR_MAPPED_KEEPFD) {
    if (close(ctx->fd) == -1)
      THError("could not close file descriptor %d", ctx->fd);
  }

  if (munmap(data, ctx->size))
    THError("could not unmap the shared memory file");

  if (!(ctx->flags & (TH_ALLOCATOR_MAPPED_FROMFD | TH_ALLOCATOR_MAPPED_UNLINK)))
  {
    if (ctx->flags & TH_ALLOCATOR_MAPPED_SHAREDMEM)
    {
#ifdef HAVE_SHM_UNLINK
      if (shm_unlink(ctx->filename) == -1)
        THError("could not unlink the shared memory file %s", ctx->filename);
#else
      THError("could not unlink the shared memory file %s, shm_unlink not available on platform", ctx->filename);
#endif
    }
  }
#endif /* _WIN32 */

  THMapAllocatorContext_free(ctx);
}
Esempio n. 17
0
int release_shared_mem(const char *name, int shm, int *mem_ptr, int size)
{
    int res = munmap(mem_ptr, size);
    if (res)
    {
        perror("release_shared_mem (munmap)");
        return res;
    }
    res = close(shm);
    if (res)
    {
        perror("release_shared_mem (close)");
        return res;
    }
    res = shm_unlink(name);
    if (res)
    {
        perror("release_shared_mem (shm_unlink)");
        return res;
    }

    printf("released shared memory %s, size = %d\n", name, size);
    return res;
}
Esempio n. 18
0
	int
main(int argc, char **argv)
{
	int		fd1, fd2, *ptr1, *ptr2;
	pid_t	childpid;
	struct stat	stat;

	if (argc != 2)
		handle_error("usage: test3 <name>");

	shm_unlink(argv[1]);
	fd1 = shm_open(argv[1], O_RDWR | O_CREAT | O_EXCL, 0666);
	ftruncate(fd1, sizeof(int));
	fd2 = open("/etc/motd", O_RDONLY);
	fstat(fd2, &stat);

	if ( (childpid = fork()) == 0) {
		/* 4child */
		ptr2 = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd2, 0);
		ptr1 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
				MAP_SHARED, fd1, 0);
		printf("child: shm ptr = %p, motd ptr = %p\n", ptr1, ptr2);

		sleep(5);
		printf("shared memory integer = %d\n", *ptr1);
		exit(0);
	}
	/* 4parent: mmap in reverse order from child */
	ptr1 = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
	ptr2 = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd2, 0);
	printf("parent: shm ptr = %p, motd ptr = %p\n", ptr1, ptr2);
	*ptr1 = 777;
	waitpid(childpid, NULL, 0);

	exit(0);
}
Esempio n. 19
0
int SBCELT_Init() {
	char shmfn[50];
	if (snprintf(&shmfn[0], 50, "/sbcelt-%lu", (unsigned long) getpid()) < 0) {
		return -1;
	}

	shm_unlink(&shmfn[0]);
	int fd = shm_open(&shmfn[0], O_CREAT|O_RDWR, 0600);
	if (fd == -1) {
		return -1;
	} else {
		if (ftruncate(fd, SBCELT_PAGES*SBCELT_PAGE_SIZE) == -1) {
			debugf("unable to truncate: %s (%i)", strerror(errno), errno);
			return -1;
		}

		void *addr = mmap(NULL, SBCELT_PAGES*SBCELT_PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
		if (addr == MAP_FAILED) {
			debugf("unable to mmap: %s (%i)", strerror(errno), errno);
			return -1;
		}
		memset(addr, 0, SBCELT_PAGES*SBCELT_PAGE_SIZE);

		workpage = addr;
		decpage = addr+SBCELT_PAGE_SIZE;

		workpage->busywait = sysconf(_SC_NPROCESSORS_ONLN) > 0;

		int i;
		for (i = 0; i < SBCELT_SLOTS; i++) {
			decpage->slots[i].available = 1;
		}
	}

	return 0;
}
Esempio n. 20
0
void test_read_write(bool write) {
  int MEMSIZE = 10000;

  int fd;
  if (write) {
    fd = shm_open(MEMNAME, O_RDWR | O_CREAT | O_EXCL, (mode_t)0666);
  } else {
    fd = shm_open(MEMNAME, O_RDWR | O_CREAT | O_EXCL, (mode_t)0666);
  }

  if (fd == -1) {
    perror("shm_open1");
    return;
  }

  // if (write) {
  int res = ftruncate(fd, MEMSIZE);
  if (res == -1) {
    perror("TRUNC");
    return;
  }
  printf("NEW: mem truncated:%u bytes\n", MEMSIZE);
  // }

  int* map = (int*)mmap(NULL, MEMSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (write) {
    map[0] = 22;
    map[1] = 33;
    map[2] = 44;
  }

  std::cout << map[0] << map[1] << map[2] << std::endl;

  munmap(map, MEMSIZE);
  shm_unlink(MEMNAME);
}
Esempio n. 21
0
void* shmem_open(void)
{
	void* ptr;
	int fd;
	char* filename = "/shm_mempolicy";
	int err;
	int file_len = 4096 * 3;

	fd = shm_open(filename, O_RDWR|O_CREAT|O_TRUNC, 0622);
	if (fd < 0)
		perror("shm_open"), exit(1);
	(void)shm_unlink(filename);

	err = ftruncate(fd, file_len);
	if (err < 0)
		perror("ftruncate"), exit(1);

	ptr = mmap(NULL, file_len, PROT_READ|PROT_WRITE, MAP_SHARED,
		   fd, 0);
	if (ptr == MAP_FAILED)
		perror("mmap"), exit(1);

	return ptr;
}
Esempio n. 22
0
int main() {
	int fd, child_pid;
	char *buf;

	child_pid = fork();
	if(child_pid == -1) {
		perror("An error occurs when calling fork()");
		return PTS_UNRESOLVED;
	} else if(child_pid == 0) {
		return child_process();
	}
	
	wait(NULL);

	fd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR|S_IWUSR);
	if(fd == -1) {
		perror("An error occurs when calling shm_open()");
		return PTS_UNRESOLVED;
	}

	buf = mmap(NULL, BUF_SIZE, PROT_READ, MAP_SHARED, fd, 0);
	if( buf == MAP_FAILED) {
		perror("An error occurs when calling mmap()");
		return PTS_UNRESOLVED;	
	}	

	shm_unlink(SHM_NAME);

	if(strcmp(buf, str) == 0) {
		printf("Test PASSED\n");
		return PTS_PASS;
	}

	printf("Test FAILED\n");
	return PTS_FAIL;
}
Esempio n. 23
0
void memory_release(int memid)
{
    if (!use_allocator) return;

    memory_t* memptr = &memory_table[memid];
    if (memptr->in_use == 1)
    {
        if (memptr->shm_mspace != NULL)
            destroy_mspace(memptr->shm_mspace);
        if (memptr->shm_base != NULL)
            munmap(memptr->shm_base, memptr->mem_size);
        if (memptr->shm_id >= 0)
            close(memptr->shm_id);
        if (memptr->do_create == 1 && need_unlink)
            shm_unlink(memptr->shm_filename);

        memptr->in_use = 0;
        memptr->shm_base = NULL;
        memptr->client_shm_base = NULL;
        memptr->shm_mspace = NULL;
        memptr->shm_id = -1;
        memptr->mem_size = 0;
    }
}
Esempio n. 24
0
File: prod.c Progetto: h31nr1ch/SOP
int main(int argc, char *argv[])
{
    sem_t *sem_full, *sem_empty;
    int *ptr;
    /* abre objeto de memoria compartilhada, criando se nao existir */
    int fd = shm_open("/shm", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if (-1 == fd) {
	perror("/shm");
	exit(1);
    }
    /* define o tamanho da regiao de memoria compartilhada */
    int ret = ftruncate(fd, sizeof(int));
    if (ret == -1) {
	perror("/shm");
	exit(2);
    }
    /* obtem um ponteiro para a regiao de memoria compartilhada */
    ptr = mmap(0, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED) {
	perror("shm-mmap");
	exit(3);
    }
    /* cria o semaforo "/full" e o inicializa em 0 */
    sem_full = sem_open("/full", O_CREAT, 0664, 0);
    if (sem_full == SEM_FAILED) {
	perror("full");
	exit(4);
    }
    /* cria o semaforo "/empty" e o inicializa em 1 */
    sem_empty = sem_open("/empty", O_CREAT, 0664, 1);
    if (sem_empty == SEM_FAILED) {
	perror("empty");
	exit(5);
    }
    signal(SIGINT, sair);
    while (!sai) {
	if (sem_wait(sem_empty) == -1) {  /* => down(&sem_empty) + verificacao de erro */
	    perror("sem_wait");
	    break;
	}
	*ptr = produz();
	printf("'A espera de ser consumido: %d\n\n", *ptr);
	if (sem_post(sem_full) == -1) {   /* => up(&sem_full) + verificacao de erro */
	    perror("sem_post");
	    break;
	}
        if (*ptr == 0) sai = 1;
    }

    /* 
     * Retirando a chamada `a funcao signal(), este codigo nunca sera' 
     * executado. E' preciso fazer rm /dev/shm/sem.{empty,full} /dev/shm/shm  
     */

    ret = munmap(ptr, sizeof(int));
    if (-1 == ret)
	perror("shm-munmap");
    ret = shm_unlink("/shm");
    if (-1 == ret)
	perror("shm-unlink");
    ret = sem_close(sem_full);
    if (-1 == ret)
	perror("sem_full");
    ret = sem_unlink("/full");
    if (-1 == ret)
	perror("sem_full");
    ret = sem_close(sem_empty);
    if (-1 == ret)
	perror("sem_empty");
    ret = sem_unlink("/empty");
    if (-1 == ret)
	perror("sem_empty");
    return 0;
}
Esempio n. 25
0
/* munmaps and unlinks the shared memory */
void shm_destroy(char *fname, void *p, size_t size)
{
	shm_unmap(p, size);
	if (shm_unlink(fname) == -1)
		fail_en("shm_unlink");
}
Esempio n. 26
0
void cras_shm_close_unlink (const char *name, int fd)
{
	shm_unlink(name);
	close(fd);
}
Esempio n. 27
0
extern "C" int32_t SystemNative_ShmUnlink(const char* name)
{
    int32_t result;
    while (CheckInterrupted(result = shm_unlink(name)));
    return result;
}
Esempio n. 28
0
static Eina_Bool
_load(Eina_File *ef, const char *key,
      Evas_Image_Property *prop,
      Evas_Image_Load_Opts *opts,
      void *pixels,
      int *error, Eina_Bool get_data)
{
   Eina_Bool res = EINA_FALSE;
   int w = 0, h = 0, alpha = 0;
   const char *dot1 = NULL, *dot2 = NULL, *end, *p;
   char *cmd = NULL, decoders[3][128], buf[4096];
   char *loader = "/evas/utils/evas_image_loader";
   char *img_loader = NULL;
   const char *libdir;
   // eg $libdir/evas/generic_loaders
   int cmd_len, len, decoders_num = 0, try_count = 0;
   int read_data = 0;
   char *tmpfname = NULL, *shmfname = NULL;
   DATA32 *body;
   FILE *f = NULL;

   libdir = _evas_module_libdir_get();
   cmd_len = strlen(libdir);
   cmd_len += strlen(loader);
   img_loader = alloca(cmd_len + 1);
   strcpy(img_loader, libdir);
   strcat(img_loader, loader);

   // params excluding file, key and loadopts
   cmd_len += 1024;
   cmd_len += strlen(eina_file_filename_get(ef)) * 2;
   if (key) cmd_len += strlen(key) * 2;
   cmd = alloca(cmd_len + 1);

   len = strlen(eina_file_filename_get(ef));
   if (len < 1)
     {
        *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
	return EINA_FALSE;
     }
   end = eina_file_filename_get(ef) + len;
   for (p = end - 1; p >= eina_file_filename_get(ef); p--)
     {
        if      ((!dot1) && (*p == '.')) dot1 = p;
        else if ((!dot2) && (*p == '.')) dot2 = p;
        else if ((dot1) && (dot2)) break;
    }
   if (dot2)
     {
        // double extn not too long
        if (((end - dot2) <= 10) && (!illegal_char(dot2)))
          {
             strncpy(&(decoders[decoders_num][0]), img_loader, 127);
             decoders[decoders_num][127] = 0;
             dotcat(&(decoders[decoders_num][0]), dot2);
             decoders_num++;
          }
        // single extn not too long
        if (((end - dot1) <= 5) && (!illegal_char(dot1)))
          {
             strncpy(&(decoders[decoders_num][0]), img_loader, 127);
             decoders[decoders_num][127] = 0;
             dotcat(&(decoders[decoders_num][0]), dot1);
             decoders_num++;
          }
        strncpy(decoders[decoders_num], img_loader, 127);
        decoders[decoders_num][127] = 0;
        decoders_num++;
     }
   else if (dot1)
     {
        // single extn not too long
        if (((end - dot1) <= 5) && (!illegal_char(dot1)))
          {
             strncpy(&(decoders[decoders_num][0]), img_loader, 127);
             decoders[decoders_num][127] = 0;
             dotcat(&(decoders[decoders_num][0]), dot1);
             decoders_num++;
          }
        strncpy(decoders[decoders_num], img_loader, 127);
        decoders[decoders_num][127] = 0;
        decoders_num++;
     }
   else
     {
        strncpy(decoders[decoders_num], img_loader, 127);
        decoders[decoders_num][127] = 0;
        decoders_num++;
     }

   for (try_count = 0; try_count < decoders_num; try_count++)
     {
        // FIXME: strcats could be more efficient, not that it matters much
        // here as we are about to build a cmd to exec via a shell that
        // will interpret shell stuff and path hunt that will then exec the
        // program itself that will dynamically link that will again
        // parse the arguments and finally do something...
        if (access(decoders[try_count], X_OK)) continue;

        strcpy(cmd, decoders[try_count]);
        strcat(cmd, " ");
        // filename first arg
        len = strlen(cmd);
        escape_copy(eina_file_filename_get(ef), cmd + len);
        if (!get_data)
          {
             strcat(cmd, " -head ");
          }
        if (key)
          {
             strcat(cmd, " -key ");
             len = strlen(cmd);
             escape_copy(key, cmd + len);
          }
        if (opts->scale_down_by > 1)
          {
             strcat(cmd, " -opt-scale-down-by ");
             snprintf(buf, sizeof(buf), "%i", opts->scale_down_by);
             strcat(cmd, buf);
          }
        if (opts->dpi > 0.0)
          {
             strcat(cmd, " -opt-dpi ");
             snprintf(buf, sizeof(buf), "%i", (int)(opts->dpi * 1000.0));
             strcat(cmd, buf);
          }
        if ((opts->w > 0) &&
            (opts->h > 0))
          {
             strcat(cmd, " -opt-size ");
             snprintf(buf, sizeof(buf), "%i %i", opts->w, opts->h);
             strcat(cmd, buf);
         }
        f = popen(cmd, "r");
        if (f) break;
     }
   if (!f)
     {
	*error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
	return EINA_FALSE;
     }
   while (fgets(buf, sizeof(buf), f))
     {
        len = strlen(buf);
        if (len > 0)
          {
             if (buf[len - 1] == '\n') buf[len - 1] = 0;
             if (!strncmp(buf, "size ", 5))
               {
                  int tw = 0, th = 0;

                  len = sscanf(buf, "%*s %i %i", &tw, &th);
                  if (len == 2)
                    {
                       if ((tw > 0) && (th > 0))
                         {
                            w = tw;
                            h = th;
                         }
                    }
               }
             else if (!strncmp(buf, "alpha ", 6))
               {
                  int ta;

                  len = sscanf(buf, "%*s %i", &ta);
                  if (len == 1)
                    {
                       alpha = ta;
                    }
               }
             else if (!strncmp(buf, "tmpfile ", 8))
               {
                  tmpfname = buf + 8;
                  goto getdata;
               }
#ifdef HAVE_SHM_OPEN
             else if (!strncmp(buf, "shmfile ", 8))
               {
                  shmfname = buf + 8;
                  goto getdata;
               }
#endif
             else if (!strncmp(buf, "data", 4))
               {
                  read_data = 1;
                  goto getdata;
               }
             else if (!strncmp(buf, "done", 4))
               {
                  read_data = 2;
                  goto getdata;
               }
          }
     }
getdata:
   if ((!read_data) && (!tmpfname) && (!shmfname))
     {
	*error = EVAS_LOAD_ERROR_CORRUPT_FILE;
	goto on_error;
     }
   if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) ||
       IMG_TOO_BIG(w, h))
     {
	*error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
	goto on_error;
     }

   if (!get_data)
     {
        if (alpha) prop->alpha = 1;
        prop->w = w;
        prop->h = h;
     }
   else
     {
        if ((int)prop->w != w ||
            (int)prop->h != h)
          {
             *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
             goto on_error;
          }
        body = pixels;

        if ((tmpfname) || (shmfname))
          {
             int fd = -1;

             // open
             if (tmpfname)
                fd = open(tmpfname, O_RDONLY, S_IRUSR);
#ifdef HAVE_SHM_OPEN
             else if (shmfname)
                fd = shm_open(shmfname, O_RDONLY, S_IRUSR);
#endif
             if (fd >= 0)
               {
                  void *addr;

                  eina_mmap_safety_enabled_set(EINA_TRUE);

                  // mmap
                  addr = mmap(NULL, w * h * sizeof(DATA32),
                              PROT_READ, MAP_SHARED, fd, 0);
                  if (addr != MAP_FAILED)
                    {
                       memcpy(body, addr, w * h * sizeof(DATA32));
                       munmap(addr, w * h * sizeof(DATA32));
                    }
                  // close
                  if (tmpfname)
                    {
                       close(fd);
                       unlink(tmpfname);
                    }
#ifdef HAVE_SHM_OPEN
                  else if (shmfname)
                    {
                       close(fd);
                       shm_unlink(shmfname);
                    }
#endif
               }
             else
               {
                  *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
                  goto on_error;
               }
          }
        else if (read_data)
          {
             if (fread(body, w * h * sizeof(DATA32), 1, f) != 1)
               {
                  *error = EVAS_LOAD_ERROR_CORRUPT_FILE;
                  goto on_error;
               }
          }
     }

   res = EINA_TRUE;
   *error = EVAS_LOAD_ERROR_NONE;

 on_error:
   if (f) pclose(f);
   return res;
}
Esempio n. 29
0
void QSharedMemory::destroy ()
{
  shm_unlink (shmFile.latin1 ());
}
Esempio n. 30
0
EIF_INTEGER posix_shm_unlink(EIF_POINTER name)
{
  return shm_unlink (name);
}