Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:22

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup POSIXAPI
0007  *
0008  * @brief Function Creates New POSIX semaphore or Opens an existing Semaphore
0009  */
0010 
0011 /*
0012  *  COPYRIGHT (c) 1989-2007.
0013  *  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include <rtems/posix/semaphoreimpl.h>
0042 #include <rtems/score/wkspace.h>
0043 #include <rtems/sysinit.h>
0044 
0045 #include <stdarg.h>
0046 #include <fcntl.h>
0047 #include <limits.h>
0048 
0049 static sem_t *_POSIX_Semaphore_Create_support(
0050   const char   *name_arg,
0051   size_t        name_len,
0052   unsigned int  value
0053 )
0054 {
0055   POSIX_Semaphore_Control *the_semaphore;
0056   char                    *name;
0057 
0058   if ( value > SEM_VALUE_MAX ) {
0059     rtems_set_errno_and_return_value( EINVAL, SEM_FAILED );
0060   }
0061 
0062   /*
0063    * Make a copy of the user's string for name just in case it was
0064    * dynamically constructed.
0065    */
0066   name = _Workspace_String_duplicate( name_arg, name_len );
0067   if ( name == NULL ) {
0068     rtems_set_errno_and_return_value( ENOMEM, SEM_FAILED );
0069   }
0070 
0071   the_semaphore = _POSIX_Semaphore_Allocate_unprotected();
0072   if ( the_semaphore == NULL ) {
0073     _Workspace_Free( name );
0074     rtems_set_errno_and_return_value( ENOSPC, SEM_FAILED );
0075   }
0076 
0077   the_semaphore->open_count = 1;
0078   the_semaphore->linked = true;
0079 
0080   /*
0081    *  POSIX does not appear to specify what the discipline for
0082    *  blocking tasks on this semaphore should be.  It could somehow
0083    *  be derived from the current scheduling policy.  One
0084    *  thing is certain, no matter what we decide, it won't be
0085    *  the same as  all other POSIX implementations. :)
0086    */
0087   _POSIX_Semaphore_Initialize( &the_semaphore->Semaphore, name, value );
0088 
0089   /*
0090    *  Make the semaphore available for use.
0091    */
0092   _Objects_Open_string(
0093     &_POSIX_Semaphore_Information,
0094     &the_semaphore->Object,
0095     name
0096   );
0097 
0098   return &the_semaphore->Semaphore;
0099 }
0100 
0101 /*
0102  *  sem_open
0103  *
0104  *  Opens a named semaphore.  Used in conjunction with the sem_close
0105  *  and sem_unlink commands.
0106  *
0107  *  11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
0108  *
0109  *  NOTE: When oflag is O_CREAT, then optional third and fourth
0110  *        parameters must be present.
0111  */
0112 sem_t *sem_open(
0113   const char *name,
0114   int         oflag,
0115   ...
0116   /* mode_t mode, */
0117   /* unsigned int value */
0118 )
0119 {
0120   /*
0121    * mode is set but never used. GCC gives a warning for this
0122    * and we need to tell GCC not to complain. But we have to
0123    * have it because we have to work through the variable
0124    * arguments to get to attr.
0125    */
0126   mode_t                     mode RTEMS_UNUSED;
0127 
0128   va_list                    arg;
0129   unsigned int               value = 0;
0130   POSIX_Semaphore_Control   *the_semaphore;
0131   size_t                     name_len;
0132   Objects_Get_by_name_error  error;
0133   sem_t                     *sem;
0134 
0135   if ( oflag & O_CREAT ) {
0136     va_start(arg, oflag);
0137     mode = va_arg( arg, mode_t );
0138     value = va_arg( arg, unsigned int );
0139     va_end(arg);
0140   }
0141 
0142   _Objects_Allocator_lock();
0143   the_semaphore = _POSIX_Semaphore_Get_by_name( name, &name_len, &error );
0144 
0145   /*
0146    *  If the name to id translation worked, then the semaphore exists
0147    *  and we can just return a pointer to the id.  Otherwise we may
0148    *  need to check to see if this is a "semaphore does not exist"
0149    *  or some other miscellaneous error on the name.
0150    */
0151 
0152   if ( the_semaphore == NULL ) {
0153 
0154     /*
0155      * Unless provided a valid name that did not already exist
0156      * and we are willing to create then it is an error.
0157      */
0158 
0159     if ( !( error == OBJECTS_GET_BY_NAME_NO_OBJECT && (oflag & O_CREAT) ) ) {
0160       _Objects_Allocator_unlock();
0161       rtems_set_errno_and_return_value(
0162         _POSIX_Get_by_name_error( error ),
0163         SEM_FAILED
0164       );
0165     }
0166   } else {
0167 
0168     /*
0169      * Check for existence with creation.
0170      */
0171 
0172     if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
0173       _Objects_Allocator_unlock();
0174       rtems_set_errno_and_return_value( EEXIST, SEM_FAILED );
0175     }
0176 
0177     the_semaphore->open_count += 1;
0178     _Objects_Allocator_unlock();
0179     return &the_semaphore->Semaphore;
0180   }
0181 
0182   /*
0183    *  At this point, the semaphore does not exist and everything has been
0184    *  checked. We should go ahead and create a semaphore.
0185    */
0186 
0187   sem = _POSIX_Semaphore_Create_support(
0188     name,
0189     name_len,
0190     value
0191   );
0192 
0193   _Objects_Allocator_unlock();
0194   return sem;
0195 }
0196 
0197 static void _POSIX_Semaphore_Manager_initialization( void )
0198 {
0199   _Objects_Initialize_information( &_POSIX_Semaphore_Information );
0200 }
0201 
0202 RTEMS_SYSINIT_ITEM(
0203   _POSIX_Semaphore_Manager_initialization,
0204   RTEMS_SYSINIT_POSIX_SEMAPHORE,
0205   RTEMS_SYSINIT_ORDER_MIDDLE
0206 );