~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to util/ipc.c

  • Committer: Neil
  • Date: 2010-06-18 23:43:02 UTC
  • mfrom: (1195.1.47 main)
  • Revision ID: neil@pall-20100618234302-ylt6pznuzk7j09pw
latest merge from main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
#include <stdlib.h>
62
62
#include <sys/types.h>
63
63
#include <semaphore.h>
 
64
#define _FILE_OFFSET_BITS 64
64
65
#include <sys/stat.h>
65
66
#include <sys/ipc.h>
66
67
#include <sys/sem.h>
67
68
#include <fcntl.h> /* For O_* */
68
 
#include <pthread.h>
 
69
#include <string.h>
 
70
#include <strings.h>
 
71
 
69
72
#include "misc.h" /* logprintfl */
70
73
#include "ipc.h"
71
74
 
74
77
 
75
78
#define DECLARE_ARG union semun { int val; struct semid_ds *buf; ushort *array; } arg
76
79
 
77
 
sem * sem_alloc (const int val, const char * name) 
 
80
sem * sem_alloc (const int val, const char * name)
 
81
{
 
82
    return sem_realloc (val, name, O_EXCL);
 
83
}
 
84
 
 
85
sem * sem_realloc (const int val, const char * name, int flags) 
78
86
{
79
87
    DECLARE_ARG;
80
88
 
82
90
    if (s==NULL) return NULL;
83
91
    bzero (s, sizeof (sem));
84
92
    s->sysv = -1;
85
 
 
86
 
 
 
93
    s->flags = flags;
 
94
    
87
95
    if (name && !strcmp(name, "mutex")) { /* use pthread mutex */
88
96
      s->usemutex = 1;
89
97
      s->mutcount = val;
91
99
      pthread_mutex_init(&(s->mutex), NULL);
92
100
      pthread_cond_init(&(s->cond), NULL);
93
101
    } else if (name) { /* named semaphores */
94
 
        if ( sem_unlink (name) == 0) { /* clean up in case previous sem holder crashed */
95
 
            logprintfl (EUCAINFO, "sem_alloc(): cleaning up old semaphore %s\n", name);
 
102
        if (s->flags & O_EXCL) {
 
103
            if ( sem_unlink (name) == 0) { /* clean up in case previous sem holder crashed */
 
104
                logprintfl (EUCAINFO, "sem_alloc(): cleaning up old semaphore %s\n", name);
 
105
            }
96
106
        }
97
 
        if ((s->posix = sem_open (name, O_CREAT | O_EXCL, 0644, val))==SEM_FAILED) {
 
107
        if ((s->posix = sem_open (name, O_CREAT | flags, 0644, val))==SEM_FAILED) {
98
108
            free (s);
99
109
            return NULL;
100
110
        }
136
146
    }
137
147
 
138
148
    if (s && s->posix) {
139
 
        return sem_wait(s->posix);
 
149
        return sem_wait (s->posix);
140
150
    }
141
151
 
142
152
    if (s && s->sysv > 0) {
161
171
    }
162
172
 
163
173
    if (s && s->posix) {
164
 
        return sem_post(s->posix);
 
174
        return sem_post (s->posix);
165
175
    }
166
176
 
167
177
    if (s && s->sysv > 0) {
178
188
    
179
189
    if (s && s->posix) {
180
190
        sem_close (s->posix);
181
 
        sem_unlink (s->name);
 
191
        if (s->flags & O_EXCL) {
 
192
            sem_unlink (s->name);            
 
193
        }
182
194
        free (s->name);
183
195
    }
184
196