[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 |
---|
[2545646] | 20 | * |
---|
| 21 | * sem_open |
---|
| 22 | * |
---|
| 23 | * Opens a named semaphore. Used in conjunction with the sem_close |
---|
| 24 | * and sem_unlink commands. |
---|
[799c767] | 25 | * |
---|
| 26 | * 11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221 |
---|
| 27 | * |
---|
| 28 | * NOTE: When oflag is O_CREAT, then optional third and fourth |
---|
| 29 | * parameters must be present. |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | sem_t *sem_open( |
---|
| 33 | const char *name, |
---|
| 34 | int oflag, |
---|
| 35 | ... |
---|
| 36 | /* mode_t mode, */ |
---|
| 37 | /* unsigned int value */ |
---|
| 38 | ) |
---|
| 39 | { |
---|
| 40 | va_list arg; |
---|
| 41 | mode_t mode; |
---|
| 42 | unsigned int value = 0; |
---|
| 43 | int status; |
---|
| 44 | Objects_Id the_semaphore_id; |
---|
| 45 | POSIX_Semaphore_Control *the_semaphore; |
---|
| 46 | Objects_Locations location; |
---|
[2545646] | 47 | |
---|
| 48 | _Thread_Disable_dispatch(); |
---|
[799c767] | 49 | |
---|
| 50 | if ( oflag & O_CREAT ) { |
---|
| 51 | va_start(arg, oflag); |
---|
| 52 | mode = va_arg( arg, mode_t ); |
---|
| 53 | value = va_arg( arg, unsigned int ); |
---|
| 54 | va_end(arg); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id ); |
---|
| 58 | |
---|
| 59 | /* |
---|
| 60 | * If the name to id translation worked, then the semaphore exists |
---|
| 61 | * and we can just return a pointer to the id. Otherwise we may |
---|
| 62 | * need to check to see if this is a "semaphore does not exist" |
---|
| 63 | * or some other miscellaneous error on the name. |
---|
| 64 | */ |
---|
| 65 | |
---|
| 66 | if ( status ) { |
---|
| 67 | |
---|
[17879f4] | 68 | /* |
---|
[2545646] | 69 | * Unless provided a valid name that did not already exist |
---|
| 70 | * and we are willing to create then it is an error. |
---|
[17879f4] | 71 | */ |
---|
| 72 | |
---|
[2545646] | 73 | if ( !( status == ENOENT && (oflag & O_CREAT) ) ) { |
---|
| 74 | _Thread_Enable_dispatch(); |
---|
| 75 | set_errno_and_return_minus_one_cast( status, sem_t * ); |
---|
[799c767] | 76 | } |
---|
[17879f4] | 77 | } else { |
---|
| 78 | |
---|
| 79 | /* |
---|
| 80 | * Check for existence with creation. |
---|
| 81 | */ |
---|
[799c767] | 82 | |
---|
| 83 | if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) { |
---|
[2545646] | 84 | _Thread_Enable_dispatch(); |
---|
[799c767] | 85 | set_errno_and_return_minus_one_cast( EEXIST, sem_t * ); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location ); |
---|
| 89 | the_semaphore->open_count += 1; |
---|
[2545646] | 90 | _Thread_Enable_dispatch(); |
---|
| 91 | _Thread_Enable_dispatch(); |
---|
[799c767] | 92 | return (sem_t *)&the_semaphore->Object.id; |
---|
| 93 | |
---|
| 94 | } |
---|
| 95 | |
---|
[17879f4] | 96 | /* |
---|
[799c767] | 97 | * At this point, the semaphore does not exist and everything has been |
---|
| 98 | * checked. We should go ahead and create a semaphore. |
---|
| 99 | */ |
---|
| 100 | |
---|
[2545646] | 101 | status =_POSIX_Semaphore_Create_support( |
---|
[799c767] | 102 | name, |
---|
| 103 | FALSE, /* not shared across processes */ |
---|
| 104 | value, |
---|
| 105 | &the_semaphore |
---|
| 106 | ); |
---|
| 107 | |
---|
[17879f4] | 108 | /* |
---|
| 109 | * errno was set by Create_support, so don't set it again. |
---|
| 110 | */ |
---|
| 111 | |
---|
[2545646] | 112 | _Thread_Enable_dispatch(); |
---|
| 113 | |
---|
[799c767] | 114 | if ( status == -1 ) |
---|
[17879f4] | 115 | return SEM_FAILED; |
---|
[799c767] | 116 | |
---|
| 117 | return (sem_t *) &the_semaphore->Object.id; |
---|
| 118 | } |
---|
[2545646] | 119 | |
---|
| 120 | |
---|