		/* Copyright 2000-2004 The Apache Software Foundation
		 *
		 * Licensed under the Apache License, Version 2.0 (the "License");
		 * you may not use this file except in compliance with the License.
		 * You may obtain a copy of the License at
		 *
		 *     http://www.apache.org/licenses/LICENSE-2.0
		 *
		 * Unless required by applicable law or agreed to in writing, software
		 * distributed under the License is distributed on an "AS IS" BASIS,
		 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		 * See the License for the specific language governing permissions and
		 * limitations under the License.
		 */
		
		#include "apr_arch_shm.h"
		
		#include "apr_general.h"
		#include "apr_errno.h"
		#include "apr_user.h"
		#include "apr_strings.h"
		
		static apr_status_t shm_cleanup_owner(void *m_)
           9    {
           9        apr_shm_t *m = (apr_shm_t *)m_;
		
		    /* anonymous shared memory */
           9        if (m->filename == NULL) {
		#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
           8            if (munmap(m->base, m->realsize) == -1) {
      ######                return errno;
		        }
           8            return APR_SUCCESS;
		#endif
		#if APR_USE_SHMEM_SHMGET_ANON
		        if (shmdt(m->base) == -1) {
		            return errno;
		        }
		        /* This segment will automatically remove itself after all
		         * references have detached. */
		        return APR_SUCCESS;
		#endif
		    }
		
		    /* name-based shared memory */
		    else {
		#if APR_USE_SHMEM_MMAP_TMP
		        apr_status_t rv;
		
		        if (munmap(m->base, m->realsize) == -1) {
		            return errno;
		        }
		        rv = apr_file_remove(m->filename, m->pool);
		        if (rv != APR_SUCCESS) {
		            return rv;
		        }
		        return APR_SUCCESS;
		#endif
		#if APR_USE_SHMEM_MMAP_SHM
		        if (munmap(m->base, m->realsize) == -1) {
		            return errno;
		        }
		        if (shm_unlink(m->filename) == -1) {
		            return errno;
		        }
		        return APR_SUCCESS;
		#endif
		#if APR_USE_SHMEM_SHMGET
           1            apr_status_t rv;
		
		        /* Indicate that the segment is to be destroyed as soon
		         * as all processes have detached. This also disallows any
		         * new attachments to the segment. */
           1            if (shmctl(m->shmid, IPC_RMID, NULL) == -1) {
      ######                return errno;
		        }
           1            if (shmdt(m->base) == -1) {
      ######                return errno;
		        }
           1            rv = apr_file_remove(m->filename, m->pool);
           1            if (rv != APR_SUCCESS) {
      ######                return rv;
		        }
           1            return APR_SUCCESS;
		#endif
		    }
		
           9        return APR_ENOTIMPL;
		}
		
		APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
		                                         apr_size_t reqsize, 
		                                         const char *filename,
		                                         apr_pool_t *pool)
           6    {
           6        apr_shm_t *new_m;
           6        apr_status_t status;
		#if APR_USE_SHMEM_SHMGET || APR_USE_SHMEM_SHMGET_ANON
           6        struct shmid_ds shmbuf;
           6        apr_uid_t uid;
           6        apr_gid_t gid;
		#endif
		#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM || \
		    APR_USE_SHMEM_MMAP_ZERO
		    int tmpfd;
		#endif
		#if APR_USE_SHMEM_SHMGET
           6        apr_size_t nbytes;
           6        key_t shmkey;
		#endif
		#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_SHMGET || \
		    APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
           6        apr_file_t *file;   /* file where metadata is stored */
		#endif
		
		    /* Check if they want anonymous or name-based shared memory */
           6        if (filename == NULL) {
		#if APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON
           5            new_m = apr_palloc(pool, sizeof(apr_shm_t));
           5            if (!new_m) {
      ######                return APR_ENOMEM;
		        }
           5            new_m->pool = pool;
           5            new_m->reqsize = reqsize;
           5            new_m->realsize = reqsize + 
		            APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */
           5            new_m->filename = NULL;
		    
		#if APR_USE_SHMEM_MMAP_ZERO
		        status = apr_file_open(&file, "/dev/zero", APR_READ | APR_WRITE, 
		                               APR_OS_DEFAULT, pool);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		        status = apr_os_file_get(&tmpfd, file);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
		                           MAP_SHARED, tmpfd, 0);
		        if (new_m->base == (void *)MAP_FAILED) {
		            return errno;
		        }
		
		        status = apr_file_close(file);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        /* store the real size in the metadata */
		        *(apr_size_t*)(new_m->base) = new_m->realsize;
		        /* metadata isn't usable */
		        new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
		
		        apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
		                                  apr_pool_cleanup_null);
		        *m = new_m;
		        return APR_SUCCESS;
		
		#elif APR_USE_SHMEM_MMAP_ANON
           5            new_m->base = mmap(NULL, new_m->realsize, PROT_READ|PROT_WRITE,
		                           MAP_ANON|MAP_SHARED, -1, 0);
           5            if (new_m->base == (void *)MAP_FAILED) {
      ######                return errno;
		        }
		
		        /* store the real size in the metadata */
           5            *(apr_size_t*)(new_m->base) = new_m->realsize;
		        /* metadata isn't usable */
           5            new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
		
           5            apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
		                                  apr_pool_cleanup_null);
           5            *m = new_m;
           5            return APR_SUCCESS;
		
		#endif /* APR_USE_SHMEM_MMAP_ZERO */
		#endif /* APR_USE_SHMEM_MMAP_ZERO || APR_USE_SHMEM_MMAP_ANON */
		#if APR_USE_SHMEM_SHMGET_ANON
		
		        new_m = apr_palloc(pool, sizeof(apr_shm_t));
		        if (!new_m) {
		            return APR_ENOMEM;
		        }
		        new_m->pool = pool;
		        new_m->reqsize = reqsize;
		        new_m->realsize = reqsize;
		        new_m->filename = NULL;
		
		        if ((new_m->shmid = shmget(IPC_PRIVATE, new_m->realsize,
		                                   SHM_R | SHM_W | IPC_CREAT)) < 0) {
		            return errno;
		        }
		
		        if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
		            return errno;
		        }
		        new_m->usable = new_m->base;
		
		        if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
		            return errno;
		        }
		        apr_uid_current(&uid, &gid, pool);
		        shmbuf.shm_perm.uid = uid;
		        shmbuf.shm_perm.gid = gid;
		        if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
		            return errno;
		        }
		
		        /* Remove the segment once use count hits zero.
		         * We will not attach to this segment again, since it is
		         * anonymous memory, so it is ok to mark it for deletion.
		         */
		        if (shmctl(new_m->shmid, IPC_RMID, NULL) == -1) {
		            return errno;
		        }
		
		        apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
		                                  apr_pool_cleanup_null);
		        *m = new_m;
		        return APR_SUCCESS;
		#endif /* APR_USE_SHMEM_SHMGET_ANON */
		        /* It is an error if they want anonymous memory but we don't have it. */
           1            return APR_ENOTIMPL; /* requested anonymous but we don't have it */
		    }
		
		    /* Name-based shared memory */
		    else {
           1            new_m = apr_palloc(pool, sizeof(apr_shm_t));
           1            if (!new_m) {
      ######                return APR_ENOMEM;
		        }
           1            new_m->pool = pool;
           1            new_m->reqsize = reqsize;
           1            new_m->filename = apr_pstrdup(pool, filename);
		
		#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
		        new_m->realsize = reqsize + 
		            APR_ALIGN_DEFAULT(sizeof(apr_size_t)); /* room for metadata */
		        /* FIXME: Ignore error for now. *
		         * status = apr_file_remove(file, pool);*/
		        status = APR_SUCCESS;
		    
		#if APR_USE_SHMEM_MMAP_TMP
		        /* FIXME: Is APR_OS_DEFAULT sufficient? */
		        status = apr_file_open(&file, filename, 
		                               APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
		                               APR_OS_DEFAULT, pool);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        status = apr_os_file_get(&tmpfd, file);
		        if (status != APR_SUCCESS) {
		            apr_file_close(file); /* ignore errors, we're failing */
		            apr_file_remove(new_m->filename, new_m->pool);
		            return status;
		        }
		
		        status = apr_file_trunc(file, new_m->realsize);
		        if (status != APR_SUCCESS) {
		            apr_file_close(file); /* ignore errors, we're failing */
		            apr_file_remove(new_m->filename, new_m->pool);
		            return status;
		        }
		
		        new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
		                           MAP_SHARED, tmpfd, 0);
		        /* FIXME: check for errors */
		
		        status = apr_file_close(file);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		#endif /* APR_USE_SHMEM_MMAP_TMP */
		#if APR_USE_SHMEM_MMAP_SHM
		        /* FIXME: Is APR_OS_DEFAULT sufficient? */
		        tmpfd = shm_open(filename, O_RDWR | O_CREAT | O_EXCL, APR_OS_DEFAULT);
		        if (tmpfd == -1) {
		            return errno;
		        }
		
		        status = apr_os_file_put(&file, &tmpfd,
		                                 APR_READ | APR_WRITE | APR_CREATE | APR_EXCL,
		                                 pool); 
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        status = apr_file_trunc(file, new_m->realsize);
		        if (status != APR_SUCCESS) {
		            shm_unlink(filename); /* we're failing, remove the object */
		            return status;
		        }
		        new_m->base = mmap(NULL, reqsize, PROT_READ | PROT_WRITE,
		                           MAP_SHARED, tmpfd, 0);
		
		        /* FIXME: check for errors */
		
		        /* FIXME: Is it ok to close this file when using shm_open?? */
		        status = apr_file_close(file);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		#endif /* APR_USE_SHMEM_MMAP_SHM */
		
		        /* store the real size in the metadata */
		        *(apr_size_t*)(new_m->base) = new_m->realsize;
		        /* metadata isn't usable */
		        new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
		
		        apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
		                                  apr_pool_cleanup_null);
		        *m = new_m;
		        return APR_SUCCESS;
		
		#endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
		
		#if APR_USE_SHMEM_SHMGET
           1            new_m->realsize = reqsize;
		
		        /* FIXME: APR_OS_DEFAULT is too permissive, switch to 600 I think. */
           1            status = apr_file_open(&file, filename, 
		                               APR_WRITE | APR_CREATE | APR_EXCL,
		                               APR_OS_DEFAULT, pool);
           1            if (status != APR_SUCCESS) {
      ######                return status;
		        }
		
		        /* ftok() (on solaris at least) requires that the file actually
		         * exist before calling ftok(). */
           1            shmkey = ftok(filename, 1);
           1            if (shmkey == (key_t)-1) {
      ######                return errno;
		        }
		
           1            if ((new_m->shmid = shmget(shmkey, new_m->realsize,
		                                   SHM_R | SHM_W | IPC_CREAT | IPC_EXCL)) < 0) {
      ######                return errno;
		        }
		
           1            if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
      ######                return errno;
		        }
           1            new_m->usable = new_m->base;
		
           1            if (shmctl(new_m->shmid, IPC_STAT, &shmbuf) == -1) {
      ######                return errno;
		        }
           1            apr_uid_current(&uid, &gid, pool);
           1            shmbuf.shm_perm.uid = uid;
           1            shmbuf.shm_perm.gid = gid;
           1            if (shmctl(new_m->shmid, IPC_SET, &shmbuf) == -1) {
      ######                return errno;
		        }
		
           1            nbytes = sizeof(reqsize);
           1            status = apr_file_write(file, (const void *)&reqsize,
		                                &nbytes);
           1            if (status != APR_SUCCESS) {
      ######                return status;
		        }
           1            status = apr_file_close(file);
           1            if (status != APR_SUCCESS) {
      ######                return status;
		        }
		
           1            apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_owner,
		                                  apr_pool_cleanup_null);
           1            *m = new_m; 
           1            return APR_SUCCESS;
		
		#endif /* APR_USE_SHMEM_SHMGET */
		    }
		
           6        return APR_ENOTIMPL;
		}
		
		APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
           5    {
           5        return apr_pool_cleanup_run(m->pool, m, shm_cleanup_owner);
		}
		
		static apr_status_t shm_cleanup_attach(void *m_)
           2    {
           2        apr_shm_t *m = (apr_shm_t *)m_;
		
           2        if (m->filename == NULL) {
		        /* It doesn't make sense to detach from an anonymous memory segment. */
      ######            return APR_EINVAL;
		    }
		    else {
		#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
		        if (munmap(m->base, m->realsize) == -1) {
		            return errno;
		        }
		        return APR_SUCCESS;
		#endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
		#if APR_USE_SHMEM_SHMGET
           2            if (shmdt(m->base) == -1) {
      ######                return errno;
		        }
           2            return APR_SUCCESS;
		#endif
		    }
		
           2        return APR_ENOTIMPL;
		}
		
		APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
		                                         const char *filename,
		                                         apr_pool_t *pool)
           2    {
           2        if (filename == NULL) {
		        /* It doesn't make sense to attach to a segment if you don't know
		         * the filename. */
      ######            return APR_EINVAL;
		    }
		    else {
		#if APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM
		        apr_shm_t *new_m;
		        apr_status_t status;
		        int tmpfd;
		        apr_file_t *file;   /* file where metadata is stored */
		        apr_size_t nbytes;
		
		        new_m = apr_palloc(pool, sizeof(apr_shm_t));
		        if (!new_m) {
		            return APR_ENOMEM;
		        }
		        new_m->pool = pool;
		        new_m->filename = apr_pstrdup(pool, filename);
		
		        status = apr_file_open(&file, filename, 
		                               APR_READ | APR_WRITE,
		                               APR_OS_DEFAULT, pool);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		        status = apr_os_file_get(&tmpfd, file);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        nbytes = sizeof(new_m->realsize);
		        status = apr_file_read(file, (void *)&(new_m->realsize),
		                               &nbytes);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        status = apr_os_file_get(&tmpfd, file);
		        if (status != APR_SUCCESS) {
		            apr_file_close(file); /* ignore errors, we're failing */
		            apr_file_remove(new_m->filename, new_m->pool);
		            return status;
		        }
		
		        new_m->reqsize = new_m->realsize - sizeof(apr_size_t);
		
		        new_m->base = mmap(NULL, new_m->realsize, PROT_READ | PROT_WRITE,
		                           MAP_SHARED, tmpfd, 0);
		        /* FIXME: check for errors */
		        
		        status = apr_file_close(file);
		        if (status != APR_SUCCESS) {
		            return status;
		        }
		
		        /* metadata isn't part of the usable segment */
		        new_m->usable = (char *)new_m->base + APR_ALIGN_DEFAULT(sizeof(apr_size_t));
		
		        apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
		                                  apr_pool_cleanup_null);
		        *m = new_m;
		        return APR_SUCCESS;
		
		#endif /* APR_USE_SHMEM_MMAP_TMP || APR_USE_SHMEM_MMAP_SHM */
		#if APR_USE_SHMEM_SHMGET
           2            apr_shm_t *new_m;
           2            apr_status_t status;
           2            apr_file_t *file;   /* file where metadata is stored */
           2            apr_size_t nbytes;
           2            key_t shmkey;
		
           2            new_m = apr_palloc(pool, sizeof(apr_shm_t));
           2            if (!new_m) {
      ######                return APR_ENOMEM;
		        }
		
		        /* FIXME: does APR_OS_DEFAULT matter for reading? */
           2            status = apr_file_open(&file, filename, 
		                               APR_READ, APR_OS_DEFAULT, pool);
           2            if (status != APR_SUCCESS) {
      ######                return status;
		        }
		
           2            nbytes = sizeof(new_m->reqsize);
           2            status = apr_file_read(file, (void *)&(new_m->reqsize),
		                               &nbytes);
           2            if (status != APR_SUCCESS) {
      ######                return status;
		        }
           2            status = apr_file_close(file);
           2            if (status != APR_SUCCESS) {
      ######                return status;
		        }
		
           2            new_m->filename = apr_pstrdup(pool, filename);
           2            new_m->pool = pool;
           2            shmkey = ftok(filename, 1);
           2            if (shmkey == (key_t)-1) {
      ######                return errno;
		        }
           2            if ((new_m->shmid = shmget(shmkey, 0, SHM_R | SHM_W)) == -1) {
      ######                return errno;
		        }
           2            if ((new_m->base = shmat(new_m->shmid, NULL, 0)) == (void *)-1) {
      ######                return errno;
		        }
           2            new_m->usable = new_m->base;
           2            new_m->realsize = new_m->reqsize;
		
           2            apr_pool_cleanup_register(new_m->pool, new_m, shm_cleanup_attach,
		                                  apr_pool_cleanup_null);
           2            *m = new_m;
           2            return APR_SUCCESS;
		
		#endif /* APR_USE_SHMEM_SHMGET */
		    }
		
           2        return APR_ENOTIMPL;
		}
		
		APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
           2    {
           2        apr_status_t rv = shm_cleanup_attach(m);
           2        apr_pool_cleanup_kill(m->pool, m, shm_cleanup_attach);
           2        return rv;
		}
		
		APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
           6    {
           6        return m->usable;
		}
		
		APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
           3    {
           3        return m->reqsize;
		}
		
      ######    APR_POOL_IMPLEMENT_ACCESSOR(shm)
		
		APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
		                                         apr_shm_t *shm)
		{
      ######        return APR_ENOTIMPL;
		}
		
		APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
		                                         apr_os_shm_t *osshm,
		                                         apr_pool_t *pool)
      ######    {
      ######        return APR_ENOTIMPL;
		}    
		
