9 #include <netinet/in.h>
12 #include <sys/types.h>
13 #include <netinet/tcp.h>
14 #include <netinet/ip.h>
18 void pa_make_nonblock_fd(int fd) {
21 if ((v = fcntl(fd, F_GETFL)) >= 0)
22 if (!(v & O_NONBLOCK))
23 fcntl(fd, F_SETFL, v|O_NONBLOCK);
26 void pa_peer_to_string(char *c, size_t l, int fd) {
29 assert(c && l && fd >= 0);
31 if (fstat(fd, &st) < 0) {
32 snprintf(c, l, "Invalid client fd");
36 if (S_ISSOCK(st.st_mode)) {
39 struct sockaddr_in in;
40 struct sockaddr_un un;
42 socklen_t sa_len = sizeof(sa);
44 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
46 if (sa.sa.sa_family == AF_INET) {
47 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
49 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
54 ntohs(sa.in.sin_port));
56 } else if (sa.sa.sa_family == AF_LOCAL) {
57 snprintf(c, l, "UNIX socket client");
62 snprintf(c, l, "Unknown network client");
64 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
65 snprintf(c, l, "STDIN/STDOUT client");
69 snprintf(c, l, "Unknown client");
72 int pa_make_secure_dir(const char* dir) {
75 if (mkdir(dir, 0700) < 0)
79 if (lstat(dir, &st) < 0)
82 if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
92 int pa_socket_low_delay(int fd) {
97 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
103 int pa_socket_tcp_low_delay(int fd) {
108 ret = pa_socket_low_delay(fd);
111 /* if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
114 tos = IPTOS_LOWDELAY;
115 if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
122 int pa_socket_set_rcvbuf(int fd, size_t l) {
125 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0)
131 int pa_socket_set_sndbuf(int fd, size_t l) {
134 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0)
140 ssize_t pa_loop_read(int fd, void*data, size_t size) {
142 assert(fd >= 0 && data && size);
147 if ((r = read(fd, data, size)) < 0)
161 ssize_t pa_loop_write(int fd, const void*data, size_t size) {
163 assert(fd >= 0 && data && size);
168 if ((r = write(fd, data, size)) < 0)
182 int pa_unix_socket_is_stale(const char *fn) {
183 struct sockaddr_un sa;
184 int fd = -1, ret = -1;
186 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
187 fprintf(stderr, "socket(): %s\n", strerror(errno));
191 sa.sun_family = AF_LOCAL;
192 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
193 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
195 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
196 if (errno == ECONNREFUSED)
208 int pa_unix_socket_remove_stale(const char *fn) {
211 if ((r = pa_unix_socket_is_stale(fn)) < 0)
212 return errno != ENOENT ? -1 : 0;
217 /* Yes, here is a race condition. But who cares? */
224 void pa_check_for_sigpipe(void) {
227 if (sigaction(SIGPIPE, NULL, &sa) < 0) {
228 fprintf(stderr, __FILE__": sigaction() failed: %s\n", strerror(errno));
232 if (sa.sa_handler == SIG_DFL)
233 fprintf(stderr, "polypaudio: WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
236 /* The following is based on an example from the GNU libc documentation */
237 char *pa_sprintf_malloc(const char *format, ...) {
247 c = realloc(c, size);
250 va_start(ap, format);
251 r = vsnprintf(c, size, format, ap);
254 if (r > -1 && r < size)
257 if (r > -1) /* glibc 2.1 */