jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 lines
9.1KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackSocket.h"
  16. #include "JackConstants.h"
  17. #include "JackTools.h"
  18. #include "JackError.h"
  19. #include "promiscuous.h"
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <pthread.h>
  23. #include <fcntl.h>
  24. namespace Jack
  25. {
  26. static void BuildName(const char* client_name, char* res, const char* dir, int which, int size, bool promiscuous)
  27. {
  28. char ext_client_name[SYNC_MAX_NAME_SIZE + 1];
  29. JackTools::RewriteName(client_name, ext_client_name);
  30. if (promiscuous) {
  31. snprintf(res, size, "%s/jack_%s_%d", dir, ext_client_name, which);
  32. } else {
  33. snprintf(res, size, "%s/jack_%s_%d_%d", dir, ext_client_name, JackTools::GetUID(), which);
  34. }
  35. }
  36. JackClientSocket::JackClientSocket(): JackClientRequestInterface(), fSocket(-1), fTimeOut(0)
  37. {
  38. const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
  39. fPromiscuous = (promiscuous != NULL);
  40. fPromiscuousGid = jack_group2gid(promiscuous);
  41. }
  42. JackClientSocket::JackClientSocket(int socket): JackClientRequestInterface(), fSocket(socket),fTimeOut(0), fPromiscuous(false), fPromiscuousGid(-1)
  43. {}
  44. #if defined(__sun__) || defined(sun)
  45. void JackClientSocket::SetReadTimeOut(long sec)
  46. {
  47. int flags;
  48. fTimeOut = sec;
  49. if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
  50. jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_GETFL");
  51. return;
  52. }
  53. flags |= O_NONBLOCK;
  54. if (fcntl(fSocket, F_SETFL, flags) < 0) {
  55. jack_error("JackClientSocket::SetReadTimeOut error in fcntl F_SETFL");
  56. return;
  57. }
  58. }
  59. void JackClientSocket::SetWriteTimeOut(long sec)
  60. {
  61. int flags;
  62. fTimeOut = sec;
  63. if ((flags = fcntl(fSocket, F_GETFL, 0)) < 0) {
  64. jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_GETFL");
  65. return;
  66. }
  67. flags |= O_NONBLOCK;
  68. if (fcntl(fSocket, F_SETFL, flags) < 0) {
  69. jack_error("JackClientSocket::SetWriteTimeOut error in fcntl F_SETFL");
  70. return;
  71. }
  72. }
  73. #else
  74. void JackClientSocket::SetReadTimeOut(long sec)
  75. {
  76. struct timeval timout;
  77. timout.tv_sec = sec;
  78. timout.tv_usec = 0;
  79. if (setsockopt(fSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
  80. jack_error("SetReadTimeOut fd = %ld err = %s", fSocket, strerror(errno));
  81. }
  82. }
  83. void JackClientSocket::SetWriteTimeOut(long sec)
  84. {
  85. struct timeval timout;
  86. timout.tv_sec = sec ;
  87. timout.tv_usec = 0;
  88. if (setsockopt(fSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timout, sizeof(timeval)) < 0) {
  89. jack_error("SetWriteTimeOut fd = %ld err = %s", fSocket, strerror(errno));
  90. }
  91. }
  92. #endif
  93. void JackClientSocket::SetNonBlocking(bool onoff)
  94. {
  95. if (onoff) {
  96. long flags = 0;
  97. if (fcntl(fSocket, F_SETFL, flags | O_NONBLOCK) < 0) {
  98. jack_error("SetNonBlocking fd = %ld err = %s", fSocket, strerror(errno));
  99. }
  100. }
  101. }
  102. int JackClientSocket::Connect(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
  103. {
  104. struct sockaddr_un addr;
  105. if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  106. jack_error("Cannot create socket err = %s", strerror(errno));
  107. return -1;
  108. }
  109. addr.sun_family = AF_UNIX;
  110. BuildName(name, addr.sun_path, dir, which, sizeof(addr.sun_path), fPromiscuous);
  111. jack_log("JackClientSocket::Connect : addr.sun_path %s", addr.sun_path);
  112. if (connect(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  113. jack_error("Cannot connect to server socket err = %s", strerror(errno));
  114. close(fSocket);
  115. fSocket = -1;
  116. return -1;
  117. }
  118. #ifdef __APPLE__
  119. int on = 1;
  120. if (setsockopt(fSocket, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&on, sizeof(on)) < 0) {
  121. jack_log("setsockopt SO_NOSIGPIPE fd = %ld err = %s", fSocket, strerror(errno));
  122. }
  123. #endif
  124. return 0;
  125. }
  126. int JackClientSocket::Close()
  127. {
  128. jack_log("JackClientSocket::Close");
  129. if (fSocket > 0) {
  130. shutdown(fSocket, SHUT_RDWR);
  131. close(fSocket);
  132. fSocket = -1;
  133. return 0;
  134. } else {
  135. return -1;
  136. }
  137. }
  138. int JackClientSocket::Read(void* data, int len)
  139. {
  140. int res;
  141. #if defined(__sun__) || defined(sun)
  142. if (fTimeOut > 0) {
  143. struct timeval tv;
  144. fd_set fdset;
  145. ssize_t res;
  146. tv.tv_sec = fTimeOut;
  147. tv.tv_usec = 0;
  148. FD_ZERO(&fdset);
  149. FD_SET(fSocket, &fdset);
  150. do {
  151. res = select(fSocket + 1, &fdset, NULL, NULL, &tv);
  152. } while (res < 0 && errno == EINTR);
  153. if (res < 0) {
  154. return res;
  155. } else if (res == 0) {
  156. return -1;
  157. }
  158. }
  159. #endif
  160. if ((res = read(fSocket, data, len)) != len) {
  161. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  162. jack_log("JackClientSocket::Read time out");
  163. // For a non blocking socket, a read failure is not considered as an error
  164. return 0;
  165. } else if (res != 0) {
  166. jack_error("Cannot read socket fd = %d res = %d err = %s", fSocket, res, strerror(errno));
  167. //return 0;
  168. return -1;
  169. } else if (errno == 0 || errno == ENOTCONN) {
  170. // aborted reading due to shutdown
  171. return JACK_REQUEST_ERR_ABORTED;
  172. } else {
  173. jack_error("Cannot read socket fd = %d err = %s", fSocket, strerror(errno));
  174. return -1;
  175. }
  176. } else {
  177. return 0;
  178. }
  179. }
  180. int JackClientSocket::Write(void* data, int len)
  181. {
  182. int res;
  183. #if defined(__sun__) || defined(sun)
  184. if (fTimeOut > 0) {
  185. struct timeval tv;
  186. fd_set fdset;
  187. ssize_t res;
  188. tv.tv_sec = fTimeOut;
  189. tv.tv_usec = 0;
  190. FD_ZERO(&fdset);
  191. FD_SET(fSocket, &fdset);
  192. do {
  193. res = select(fSocket + 1, NULL, &fdset, NULL, &tv);
  194. } while (res < 0 && errno == EINTR);
  195. if (res < 0) {
  196. return res;
  197. } else if (res == 0) {
  198. return -1;
  199. }
  200. }
  201. #endif
  202. if ((res = write(fSocket, data, len)) != len) {
  203. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  204. jack_log("JackClientSocket::Write time out");
  205. return 0; // For a non blocking socket, a write failure is not considered as an error
  206. } else if (res != 0) {
  207. jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
  208. //return 0;
  209. return -1;
  210. } else {
  211. jack_error("Cannot write socket fd = %ld err = %s", fSocket, strerror(errno));
  212. return -1;
  213. }
  214. } else {
  215. return 0;
  216. }
  217. }
  218. JackServerSocket::JackServerSocket(): fSocket( -1)
  219. {
  220. const char* promiscuous = getenv("JACK_PROMISCUOUS_SERVER");
  221. fPromiscuous = (promiscuous != NULL);
  222. fPromiscuousGid = jack_group2gid(promiscuous);
  223. }
  224. int JackServerSocket::Bind(const char* dir, const char* name, int which) // A revoir : utilisation de "which"
  225. {
  226. struct sockaddr_un addr;
  227. if ((fSocket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  228. jack_error("Cannot create server socket err = %s", strerror(errno));
  229. return -1;
  230. }
  231. addr.sun_family = AF_UNIX;
  232. // Socket name has to be kept in fName to be "unlinked".
  233. BuildName(name, fName, dir, which, sizeof(addr.sun_path), fPromiscuous);
  234. strncpy(addr.sun_path, fName, sizeof(addr.sun_path) - 1);
  235. jack_log("JackServerSocket::Bind : addr.sun_path %s", addr.sun_path);
  236. unlink(fName); // Security...
  237. if (bind(fSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  238. jack_error("Cannot bind server to socket err = %s", strerror(errno));
  239. goto error;
  240. }
  241. if (listen(fSocket, 100) < 0) {
  242. jack_error("Cannot enable listen on server socket err = %s", strerror(errno));
  243. goto error;
  244. }
  245. if (fPromiscuous && (jack_promiscuous_perms(-1, fName, fPromiscuousGid) < 0))
  246. goto error;
  247. return 0;
  248. error:
  249. unlink(fName);
  250. close(fSocket);
  251. fSocket = -1;
  252. return -1;
  253. }
  254. JackClientSocket* JackServerSocket::Accept()
  255. {
  256. struct sockaddr_un client_addr;
  257. socklen_t client_addrlen;
  258. memset(&client_addr, 0, sizeof(client_addr));
  259. client_addrlen = sizeof(client_addr);
  260. int fd = accept(fSocket, (struct sockaddr*)&client_addr, &client_addrlen);
  261. if (fd < 0) {
  262. jack_error("Cannot accept new connection err = %s", strerror(errno));
  263. return 0;
  264. } else {
  265. return new JackClientSocket(fd);
  266. }
  267. }
  268. int JackServerSocket::Close()
  269. {
  270. if (fSocket > 0) {
  271. jack_log("JackServerSocket::Close %s", fName);
  272. shutdown(fSocket, SHUT_RDWR);
  273. close(fSocket);
  274. unlink(fName);
  275. fSocket = -1;
  276. return 0;
  277. } else {
  278. return -1;
  279. }
  280. }
  281. } // end of namespace