Example #1
0
/* Deserialize a handle.
 * str_hnd  : A null-terminated string of len str_hnd_len that
 *              contains the serialized handle.
 * hnd      : If the call succeeds the user gets back a handle,hnd, to
 *           shared mem - deserialized from strhnd. This handle
 *           will refer to the shm seg referred by the serialized
 *           handle.
 */
int MPL_shm_hnd_deserialize(MPL_shm_hnd_t hnd, const char *str_hnd, size_t str_hnd_len)
{
    int rc = -1;
    MPLI_shm_hnd_reset_val(hnd);
    rc = MPLI_shm_ghnd_alloc(hnd, MPL_MEM_SHM);
    rc = MPLI_shm_ghnd_set_by_val(hnd, "%s", str_hnd);
    rc = MPL_shm_seg_open(hnd, 0);
    return rc;
}
Example #2
0
/* A template function which creates/attaches shm seg handle
 * to the shared memory. Used by user-exposed functions below
 */
static inline int MPL_shm_seg_create_attach_templ(MPL_shm_hnd_t hnd, intptr_t seg_sz,
                                                  void **shm_addr_ptr, int offset, int flag)
{
    int rc = MPL_SHM_SUCCESS;
    int lhnd = -1;

    if (flag & MPLI_SHM_FLAG_SHM_CREATE) {
        lhnd = shmget(IPC_PRIVATE, seg_sz, IPC_CREAT | S_IRWXU);
        MPLI_shm_lhnd_set(hnd, lhnd);
        rc = MPLI_shm_ghnd_alloc(hnd, MPL_MEM_SHM);
        if (rc) {
            goto fn_exit;
        }
        rc = MPLI_shm_ghnd_set_by_val(hnd, "%d", lhnd);
        if (rc != MPL_SHM_SUCCESS) {
            goto fn_exit;
        }
    } else {
        /* Open an existing shared memory seg */
        if (!MPLI_shm_lhnd_is_valid(hnd)) {
            lhnd = atoi(MPLI_shm_ghnd_get_by_ref(hnd));
            MPLI_shm_lhnd_set(hnd, lhnd);
        }
    }

    if (flag & MPLI_SHM_FLAG_SHM_ATTACH) {
        const void *start_addr = NULL;

        /* Caller ensures that shmaddr must be a page-aligned address
         * at which the attach occurs. EINVAL error would result if a
         * mapping already exists in this address range or the address
         * is not page-aligned. */
        if (flag & MPLI_SHM_FLAG_FIXED_ADDR)
            start_addr = (const void *) *shm_addr_ptr;

        /* Attach to shared mem seg */
        *shm_addr_ptr = shmat(MPLI_shm_lhnd_get(hnd), start_addr, 0x0);
        if (*shm_addr_ptr == (void *) -1) {
            rc = MPL_SHM_EINVAL;
        }
    }

  fn_exit:
    return rc;
}