/* * * This file is part of bufferpool * * Copyright (C) 2007 by LScube team * See AUTHORS for more details * * bufferpool is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * bufferpool is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with bufferpool; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include #include #include #include #include #include #include BPBuffer *bp_shm_map(char *shm_name) { BPBuffer *buffer; BPSlot *slots; BPControl *control; // uint32_tindex; char *shm_file_name; int fd; struct stat fdstat; // *** control struct mapping in shared memory *** if (!(shm_file_name = bp_ipc_name(shm_name, BPBUFF_SHM_CTRLNAME))) return NULL; fd = shm_open(shm_file_name, O_RDWR, 0); free(shm_file_name); if ((fd < 0)) { bp_log(FNC_LOG_ERR, "Could not open POSIX shared memory (BPControl): is Felix running?\n"); return NULL; } if ((fstat(fd, &fdstat) < 0)) { bp_log(FNC_LOG_ERR, "Could not stat %s\n", BPBUFF_SHM_CTRLNAME); close(fd); return NULL; } if (((size_t) fdstat.st_size != sizeof(BPControl))) { bp_log(FNC_LOG_ERR, "Strange size for BPControl shared memory! (not an integer number of slots)\n"); close(fd); return NULL; } control = mmap(NULL, fdstat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if (control == MAP_FAILED) { bp_log(FNC_LOG_FATAL, "SHM: error in mmap\n"); return NULL; } // *** XXX inizialization of control struct made by creator! // control->refs = 0; // control->nslots = buffer_size-1; // if ( pthread_mutexattr_init(&mutex_attr) ) // return NULL; // if ( pthread_mutex_init(&control->syn, &mutex_attr) ) // return NULL; // *** wait for completion af all operations, especially for initializzation pthread_mutex_lock(&control->syn); pthread_mutex_unlock(&control->syn); // *** slots mapping in shared memory *** if (!(shm_file_name = bp_ipc_name(shm_name, BPBUFF_SHM_SLOTSNAME))) return NULL; fd = shm_open(shm_file_name, O_RDWR, 0); free(shm_file_name); if ((fd < 0)) { bp_log(FNC_LOG_ERR, "Could not open POSIX shared memory (BPSlots): is Felix running?\n"); munmap(control, sizeof(BPControl)); return NULL; } if ((fstat(fd, &fdstat) < 0)) { bp_log(FNC_LOG_ERR, "Could not stat %s\n", BPBUFF_SHM_SLOTSNAME); close(fd); munmap(control, sizeof(BPControl)); return NULL; } if (((size_t) fdstat.st_size != control->nslots * sizeof(BPSlot))) { bp_log(FNC_LOG_ERR, "Strange size for shared memory! (not an integer number of slots)\n"); close(fd); munmap(control, sizeof(BPControl)); return NULL; } slots = mmap(NULL, fdstat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if (slots == MAP_FAILED) { bp_log(FNC_LOG_FATAL, "SHM: error in mmap\n"); munmap(control, sizeof(BPControl)); return NULL; } // initialized by creator! // for(index=0; indexnslots-1; index++) // (slots[index]).next = BPSLOT_SETADDR(buffer, &(slots[index+1])); // (slots[index]).next=BPSLOT_SETADDR(buffer, slots); /*end of the list back to the head*/ if (!(buffer = (BPBuffer *) malloc(sizeof(BPBuffer)))) { munmap(slots, control->nslots * sizeof(BPSlot)); munmap(control, sizeof(BPControl)); return NULL; } buffer->type = buff_shm; buffer->slots = slots; buffer->known_slots = control->nslots; strncpy(buffer->filename, shm_name, sizeof(buffer->filename) - 1); // buffer->fd = -1; // buffer->fd = NULL; buffer->slots = slots; buffer->control = control; return buffer; }