[799c767] | 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 | |
---|
| 27 | sem_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 = va_arg( arg, mode_t ); |
---|
| 47 | value = va_arg( arg, unsigned int ); |
---|
| 48 | va_end(arg); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id ); |
---|
| 52 | |
---|
| 53 | /* |
---|
| 54 | * If the name to id translation worked, then the semaphore exists |
---|
| 55 | * and we can just return a pointer to the id. Otherwise we may |
---|
| 56 | * need to check to see if this is a "semaphore does not exist" |
---|
| 57 | * or some other miscellaneous error on the name. |
---|
| 58 | */ |
---|
| 59 | |
---|
| 60 | if ( status ) { |
---|
| 61 | |
---|
[17879f4] | 62 | /* |
---|
| 63 | * Unless we are willing to create name -> ID translation failure is |
---|
| 64 | * an error. |
---|
| 65 | */ |
---|
| 66 | |
---|
| 67 | if ( status == EINVAL ) { |
---|
| 68 | if ( !(oflag & O_CREAT) ) { |
---|
[799c767] | 69 | set_errno_and_return_minus_one_cast( ENOENT, sem_t * ); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
[17879f4] | 73 | } else { |
---|
| 74 | |
---|
| 75 | /* |
---|
| 76 | * Check for existence with creation. |
---|
| 77 | */ |
---|
[799c767] | 78 | |
---|
| 79 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
| 80 | set_errno_and_return_minus_one_cast( EEXIST, sem_t * ); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location ); |
---|
| 84 | the_semaphore->open_count += 1; |
---|
| 85 | return (sem_t *)&the_semaphore->Object.id; |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | |
---|
[17879f4] | 89 | /* |
---|
[799c767] | 90 | * At this point, the semaphore does not exist and everything has been |
---|
| 91 | * checked. We should go ahead and create a semaphore. |
---|
| 92 | */ |
---|
| 93 | |
---|
| 94 | status = _POSIX_Semaphore_Create_support( |
---|
| 95 | name, |
---|
| 96 | FALSE, /* not shared across processes */ |
---|
| 97 | value, |
---|
| 98 | &the_semaphore |
---|
| 99 | ); |
---|
| 100 | |
---|
[17879f4] | 101 | /* |
---|
| 102 | * errno was set by Create_support, so don't set it again. |
---|
| 103 | */ |
---|
| 104 | |
---|
[799c767] | 105 | if ( status == -1 ) |
---|
[17879f4] | 106 | return SEM_FAILED; |
---|
[799c767] | 107 | |
---|
| 108 | return (sem_t *) &the_semaphore->Object.id; |
---|
| 109 | } |
---|