Do not use O_RDWR on semaphores on FreeBSD
[jack2.git] / posix / JackPosixSemaphore.cpp
bloba18b6b709067fd311cff7c55a8ed4544413fe000
1 /*
2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define _POSIX_C_SOURCE 200112L
22 #include "JackPosixSemaphore.h"
23 #include "JackTools.h"
24 #include "JackConstants.h"
25 #include "JackError.h"
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29 #ifdef __linux__
30 #include "promiscuous.h"
31 #endif
33 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
34 #define JACK_SEM_PREFIX "/jack_sem"
35 #define SEM_DEFAULT_O 0
36 #else
37 #define JACK_SEM_PREFIX "jack_sem"
38 #define SEM_DEFAULT_O O_RDWR
39 #endif
41 namespace Jack
44 JackPosixSemaphore::JackPosixSemaphore() : JackSynchro(), fSemaphore(NULL)
46 const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
47 fPromiscuous = (promiscuous != NULL);
48 #ifdef __linux__
49 fPromiscuousGid = jack_group2gid(promiscuous);
50 #endif
53 void JackPosixSemaphore::BuildName(const char* client_name, const char* server_name, char* res, int size)
55 char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
56 JackTools::RewriteName(client_name, ext_client_name);
57 #if __APPLE__ // POSIX semaphore names are limited to 32 characters...
58 snprintf(res, 32, "js_%s", ext_client_name);
59 #else
60 if (fPromiscuous) {
61 snprintf(res, size, JACK_SEM_PREFIX ".%s_%s", server_name, ext_client_name);
62 } else {
63 snprintf(res, size, JACK_SEM_PREFIX ".%d_%s_%s", JackTools::GetUID(), server_name, ext_client_name);
65 #endif
68 bool JackPosixSemaphore::Signal()
70 int res;
72 if (!fSemaphore) {
73 jack_error("JackPosixSemaphore::Signal name = %s already deallocated!!", fName);
74 return false;
77 if (fFlush) {
78 return true;
81 if ((res = sem_post(fSemaphore)) != 0) {
82 jack_error("JackPosixSemaphore::Signal name = %s err = %s", fName, strerror(errno));
84 return (res == 0);
87 bool JackPosixSemaphore::SignalAll()
89 int res;
91 if (!fSemaphore) {
92 jack_error("JackPosixSemaphore::SignalAll name = %s already deallocated!!", fName);
93 return false;
96 if (fFlush) {
97 return true;
100 if ((res = sem_post(fSemaphore)) != 0) {
101 jack_error("JackPosixSemaphore::SignalAll name = %s err = %s", fName, strerror(errno));
103 return (res == 0);
106 bool JackPosixSemaphore::Wait()
108 int res;
110 if (!fSemaphore) {
111 jack_error("JackPosixSemaphore::Wait name = %s already deallocated!!", fName);
112 return false;
115 while ((res = sem_wait(fSemaphore) < 0)) {
116 jack_error("JackPosixSemaphore::Wait name = %s err = %s", fName, strerror(errno));
117 if (errno != EINTR) {
118 break;
121 return (res == 0);
124 bool JackPosixSemaphore::TimedWait(long usec)
126 int res;
127 struct timeval now;
128 timespec time;
130 if (!fSemaphore) {
131 jack_error("JackPosixSemaphore::TimedWait name = %s already deallocated!!", fName);
132 return false;
134 gettimeofday(&now, 0);
135 time.tv_sec = now.tv_sec + usec / 1000000;
136 long tv_usec = (now.tv_usec + (usec % 1000000));
137 time.tv_sec += tv_usec / 1000000;
138 time.tv_nsec = (tv_usec % 1000000) * 1000;
140 while ((res = sem_timedwait(fSemaphore, &time)) < 0) {
141 jack_error("JackPosixSemaphore::TimedWait err = %s", strerror(errno));
142 jack_log("JackPosixSemaphore::TimedWait now : %ld %ld ", now.tv_sec, now.tv_usec);
143 jack_log("JackPosixSemaphore::TimedWait next : %ld %ld ", time.tv_sec, time.tv_nsec/1000);
144 if (errno != EINTR) {
145 break;
148 return (res == 0);
151 // Server side : publish the semaphore in the global namespace
152 bool JackPosixSemaphore::Allocate(const char* name, const char* server_name, int value)
154 BuildName(name, server_name, fName, sizeof(fName));
155 jack_log("JackPosixSemaphore::Allocate name = %s val = %ld", fName, value);
157 if ((fSemaphore = sem_open(fName, O_CREAT | SEM_DEFAULT_O, 0777, value)) == (sem_t*)SEM_FAILED) {
158 jack_error("Allocate: can't check in named semaphore name = %s err = %s", fName, strerror(errno));
159 return false;
160 } else {
161 #ifdef __linux__
162 if (fPromiscuous) {
163 char sempath[SYNC_MAX_NAME_SIZE+13];
164 snprintf(sempath, sizeof(sempath), "/dev/shm/sem.%s", fName);
165 if (jack_promiscuous_perms(-1, sempath, fPromiscuousGid) < 0)
166 return false;
168 #endif
169 return true;
173 // Client side : get the published semaphore from server
174 bool JackPosixSemaphore::ConnectInput(const char* name, const char* server_name)
176 BuildName(name, server_name, fName, sizeof(fName));
177 jack_log("JackPosixSemaphore::Connect name = %s", fName);
179 // Temporary...
180 if (fSemaphore) {
181 jack_log("Already connected name = %s", name);
182 return true;
185 if ((fSemaphore = sem_open(fName, SEM_DEFAULT_O)) == (sem_t*)SEM_FAILED) {
186 jack_error("Connect: can't connect named semaphore name = %s err = %s", fName, strerror(errno));
187 return false;
188 } else if (fSemaphore) {
189 int val = 0;
190 sem_getvalue(fSemaphore, &val);
191 jack_log("JackPosixSemaphore::Connect sem_getvalue %ld", val);
192 return true;
193 } else {
194 jack_error("Connect: fSemaphore not initialized!");
195 return false;
199 bool JackPosixSemaphore::Connect(const char* name, const char* server_name)
201 return ConnectInput(name, server_name);
204 bool JackPosixSemaphore::ConnectOutput(const char* name, const char* server_name)
206 return ConnectInput(name, server_name);
209 bool JackPosixSemaphore::Disconnect()
211 if (fSemaphore) {
212 jack_log("JackPosixSemaphore::Disconnect name = %s", fName);
213 if (sem_close(fSemaphore) != 0) {
214 jack_error("Disconnect: can't disconnect named semaphore name = %s err = %s", fName, strerror(errno));
215 return false;
216 } else {
217 fSemaphore = NULL;
218 return true;
220 } else {
221 return true;
225 // Server side : destroy the semaphore
226 void JackPosixSemaphore::Destroy()
228 if (fSemaphore != NULL) {
229 jack_log("JackPosixSemaphore::Destroy name = %s", fName);
230 sem_unlink(fName);
231 if (sem_close(fSemaphore) != 0) {
232 jack_error("Destroy: can't destroy semaphore name = %s err = %s", fName, strerror(errno));
234 fSemaphore = NULL;
235 } else {
236 jack_error("JackPosixSemaphore::Destroy semaphore == NULL");
240 } // end of namespace