Blame usr/lib/api/shrd_mem.c.in

Packit 8681c6
/*
Packit 8681c6
 * COPYRIGHT (c) International Business Machines Corp. 2001-2017
Packit 8681c6
 *
Packit 8681c6
 * This program is provided under the terms of the Common Public License,
Packit 8681c6
 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
Packit 8681c6
 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
Packit 8681c6
 * found in the file LICENSE file or at
Packit 8681c6
 * https://opensource.org/licenses/cpl1.0.php
Packit 8681c6
 */
Packit 8681c6
Packit 8681c6
Packit 8681c6
//
Packit 8681c6
// Pkcs11 Api Shared Memory Routines
Packit 8681c6
//
Packit 8681c6
Packit 8681c6
#if NGPTH
Packit 8681c6
#include <pth.h>
Packit 8681c6
#else
Packit 8681c6
#include <pthread.h>
Packit 8681c6
#endif
Packit 8681c6
Packit 8681c6
#include <stdlib.h>
Packit 8681c6
Packit 8681c6
#include <stdio.h>
Packit 8681c6
#include <dlfcn.h>
Packit 8681c6
#include <errno.h>
Packit 8681c6
Packit 8681c6
#include <sys/shm.h>
Packit 8681c6
#include <sys/ipc.h>
Packit 8681c6
#include <sys/stat.h>
Packit 8681c6
Packit 8681c6
#include <fcntl.h>
Packit 8681c6
#include <sys/mman.h>
Packit 8681c6
Packit 8681c6
Packit 8681c6
#include <pwd.h>
Packit 8681c6
#include <string.h>
Packit 8681c6
#include <unistd.h>
Packit 8681c6
#include <sys/types.h>
Packit 8681c6
#include <grp.h>
Packit 8681c6
Packit 8681c6
#include <slotmgr.h>
Packit 8681c6
#include <apictl.h>
Packit 8681c6
Packit 8681c6
#define MAPFILENAME "@CONFIG_PATH@/.apimap"
Packit 8681c6
Packit 8681c6
extern API_Proc_Struct_t *Anchor;
Packit 8681c6
//
Packit 8681c6
// Will attach to the shared memory that has been created
Packit 8681c6
// by the slot manager daemon.
Packit 8681c6
// A NULL pointer will return if the memory region is invalid
Packit 8681c6
// for any reason
Packit 8681c6
void *attach_shared_memory()
Packit 8681c6
{
Packit 8681c6
    int shmid;
Packit 8681c6
    char *shmp;
Packit 8681c6
    struct stat statbuf;
Packit a0fd05
#if 0
Packit 8681c6
    struct group *grp;
Packit 8681c6
    struct passwd *pw, *epw;
Packit 8681c6
    uid_t uid, euid;
Packit a0fd05
#endif
Packit 8681c6
Packit 8681c6
#if !(MMAP)
Packit 8681c6
    // Really should fstat the tok_path, since it will be the actual
Packit 8681c6
    // executable of the slotmgr, however at this time we won't bother
Packit 8681c6
    // for the prototype.  /tmp/slotmgr will have to be an existing file.
Packit 8681c6
Packit 8681c6
    if (stat(TOK_PATH, &statbuf) < 0) {
Packit 8681c6
        // The Stat token origin file does not work... Kick it out
Packit 8681c6
        return NULL;
Packit 8681c6
    }
Packit 8681c6
Packit a0fd05
#if 0
Packit 8681c6
    uid = getuid();
Packit 8681c6
    euid = geteuid();
Packit 8681c6
    // only check group membership if not root user
Packit 8681c6
    if (uid != 0 && euid != 0) {
Packit 8681c6
        int i, member = 0;
Packit 8681c6
        grp = getgrnam("pkcs11");
Packit 8681c6
        if (!grp) {
Packit 8681c6
            // group pkcs11 not known to the system
Packit 8681c6
            return NULL;
Packit 8681c6
        }
Packit 8681c6
        pw = getpwuid(uid);
Packit 8681c6
        epw = getpwuid(euid);
Packit 8681c6
        for (i = 0; grp->gr_mem[i]; i++) {
Packit 8681c6
            if (pw) {
Packit 8681c6
                if (!strncmp(pw->pw_name,
Packit 8681c6
                             grp->gr_mem[i],
Packit 8681c6
                             strlen(pw->pw_name))) {
Packit 8681c6
                    member = 1;
Packit 8681c6
                    break;
Packit 8681c6
                }
Packit 8681c6
            }
Packit 8681c6
            if (epw) {
Packit 8681c6
                if (!strncmp(epw->pw_name,
Packit 8681c6
                             grp->gr_mem[i], strlen(epw->pw_name))) {
Packit 8681c6
                    member = 1;
Packit 8681c6
                    break;
Packit 8681c6
                }
Packit 8681c6
            }
Packit 8681c6
        }
Packit 8681c6
        if (!member) {
Packit 8681c6
            return NULL;
Packit 8681c6
        }
Packit 8681c6
    }
Packit a0fd05
#endif
Packit 8681c6
Packit 8681c6
    Anchor->shm_tok = ftok(TOK_PATH, 'b');
Packit 8681c6
Packit 8681c6
    // Get the shared memory id.
Packit 8681c6
    shmid = shmget(Anchor->shm_tok, sizeof(Slot_Mgr_Shr_t),
Packit 8681c6
                   S_IWUSR | S_IWGRP | S_IRGRP | S_IRUSR);
Packit 8681c6
    if (shmid < 0) {
Packit 8681c6
        return NULL;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
Packit 8681c6
    shmp = (void *) shmat(shmid, NULL, 0);
Packit 8681c6
    if (!shmp) {
Packit 8681c6
        return NULL;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return shmp;
Packit 8681c6
#else
Packit 8681c6
    int fd;
Packit 8681c6
#warning "EXPERIMENTAL"
Packit 8681c6
    fd = open(MAPFILENAME, O_RDWR);
Packit 8681c6
Packit 8681c6
    if (fd < 0) {
Packit 8681c6
        return NULL;            //Failed  the file should exist and be valid
Packit 8681c6
    }
Packit 8681c6
    shmp = (char *) mmap(NULL, sizeof(Slot_Mgr_Shr_t), PROT_READ | PROT_WRITE,
Packit 8681c6
                         MAP_SHARED, fd, 0);
Packit 8681c6
    close(fd);
Packit 8681c6
    if (!shmp) {
Packit 8681c6
        return NULL;
Packit 8681c6
    }
Packit 8681c6
    return shmp;
Packit 8681c6
#endif
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
//
Packit 8681c6
//Detach the shared memory from the api when finished.
Packit 8681c6
//
Packit 8681c6
Packit 8681c6
void detach_shared_memory(char *shmp)
Packit 8681c6
{
Packit 8681c6
#if !(MMAP)
Packit 8681c6
    shmdt(shmp);
Packit 8681c6
#else
Packit 8681c6
    munmap(shmp, sizeof(Slot_Mgr_Shr_t));
Packit 8681c6
#endif
Packit 8681c6
}