source: rtems/c/src/exec/posix/src/semopen.c @ 811fae1

4.104.114.84.95
Last change on this file since 811fae1 was 799c767, checked in by Joel Sherrill <joel.sherrill@…>, on 11/02/99 at 18:00:15

Split the POSIX semaphore manager into multiple files.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/*
2 *  $Id$
3 */
4
5#include <stdarg.h>
6
7#include <errno.h>
8#include <fcntl.h>
9#include <pthread.h>
10#include <semaphore.h>
11#include <limits.h>
12
13#include <rtems/system.h>
14#include <rtems/score/object.h>
15#include <rtems/posix/semaphore.h>
16#include <rtems/posix/time.h>
17#include <rtems/posix/seterr.h>
18
19/*PAGE
20 *
21 *  11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
22 *
23 *  NOTE: When oflag is O_CREAT, then optional third and fourth
24 *        parameters must be present.
25 */
26
27sem_t *sem_open(
28  const char *name,
29  int         oflag,
30  ...
31  /* mode_t mode, */
32  /* unsigned int value */
33)
34{
35  va_list                    arg;
36  mode_t                     mode;
37  unsigned int               value = 0;
38  int                        status;
39  Objects_Id                 the_semaphore_id;
40  POSIX_Semaphore_Control   *the_semaphore;
41  Objects_Locations          location;
42 
43
44  if ( oflag & O_CREAT ) {
45    va_start(arg, oflag);
46    /*mode = (mode_t) va_arg( arg, mode_t * );*/
47    mode = va_arg( arg, mode_t );
48    /*value = (unsigned int) va_arg( arg, unsigned int * );*/
49    value = va_arg( arg, unsigned int );
50    va_end(arg);
51  }
52
53  status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id );
54   
55  /*
56   *  If the name to id translation worked, then the semaphore exists
57   *  and we can just return a pointer to the id.  Otherwise we may
58   *  need to check to see if this is a "semaphore does not exist"
59   *  or some other miscellaneous error on the name.
60   */
61
62  if ( status ) {
63
64    if ( status == EINVAL ) {      /* name -> ID translation failed */
65      if ( !(oflag & O_CREAT) ) {  /* willing to create it? */
66        set_errno_and_return_minus_one_cast( ENOENT, sem_t * );
67      }
68      /* we are willing to create it */
69    }
70    /* some type of error */
71    /*set_errno_and_return_minus_one_cast( status, sem_t * );*/
72
73  } else {                /* name -> ID translation succeeded */
74
75    if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
76      set_errno_and_return_minus_one_cast( EEXIST, sem_t * );
77    }
78
79    /*
80     * XXX In this case we need to do an ID->pointer conversion to
81     *     check the mode.   This is probably a good place for a subroutine.
82     */
83
84    the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location );
85    the_semaphore->open_count += 1;
86
87    return (sem_t *)&the_semaphore->Object.id;
88
89  }
90 
91  /* XXX verify this comment...
92   *
93   *  At this point, the semaphore does not exist and everything has been
94   *  checked. We should go ahead and create a semaphore.
95   */
96
97  status = _POSIX_Semaphore_Create_support(
98    name,
99    FALSE,         /* not shared across processes */
100    value,
101    &the_semaphore
102  );
103 
104  if ( status == -1 )
105    return (sem_t *) -1;
106
107  return (sem_t *) &the_semaphore->Object.id;
108 
109}
Note: See TracBrowser for help on using the repository browser.