]>
code.delx.au - pulseaudio/blob - src/util.c
7 #include <netinet/in.h>
10 #include <sys/types.h>
11 #include <netinet/tcp.h>
12 #include <netinet/ip.h>
16 void pa_make_nonblock_fd(int fd
) {
19 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
20 if (!(v
& O_NONBLOCK
))
21 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
24 void pa_peer_to_string(char *c
, size_t l
, int fd
) {
27 assert(c
&& l
&& fd
>= 0);
29 if (fstat(fd
, &st
) < 0) {
30 snprintf(c
, l
, "Invalid client fd");
34 if (S_ISSOCK(st
.st_mode
)) {
37 struct sockaddr_in in
;
38 struct sockaddr_un un
;
40 socklen_t sa_len
= sizeof(sa
);
42 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
44 if (sa
.sa
.sa_family
== AF_INET
) {
45 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
47 snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
52 ntohs(sa
.in
.sin_port
));
54 } else if (sa
.sa
.sa_family
== AF_LOCAL
) {
55 snprintf(c
, l
, "UNIX socket client");
60 snprintf(c
, l
, "Unknown network client");
62 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
63 snprintf(c
, l
, "STDIN/STDOUT client");
67 snprintf(c
, l
, "Unknown client");
70 int pa_make_secure_dir(const char* dir
) {
73 if (mkdir(dir
, 0700) < 0)
77 if (lstat(dir
, &st
) < 0)
80 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
90 int pa_socket_low_delay(int fd
) {
95 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &priority
, sizeof(priority
)) < 0)
101 int pa_socket_tcp_low_delay(int fd
) {
106 ret
= pa_socket_low_delay(fd
);
109 /* if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
112 tos
= IPTOS_LOWDELAY
;
113 if (setsockopt(fd
, SOL_IP
, IP_TOS
, &tos
, sizeof(tos
)) < 0)
120 int pa_socket_set_rcvbuf(int fd
, size_t l
) {
123 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &l
, sizeof(l
)) < 0)
129 int pa_socket_set_sndbuf(int fd
, size_t l
) {
132 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &l
, sizeof(l
)) < 0)
138 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
140 assert(fd
>= 0 && data
&& size
);
145 if ((r
= read(fd
, data
, size
)) < 0)
159 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
161 assert(fd
>= 0 && data
&& size
);
166 if ((r
= write(fd
, data
, size
)) < 0)
180 int pa_unix_socket_is_stale(const char *fn
) {
181 struct sockaddr_un sa
;
182 int fd
= -1, ret
= -1;
184 if ((fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
185 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
189 sa
.sun_family
= AF_LOCAL
;
190 strncpy(sa
.sun_path
, fn
, sizeof(sa
.sun_path
)-1);
191 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
193 if (connect(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
194 if (errno
== ECONNREFUSED
)
206 int pa_unix_socket_remove_stale(const char *fn
) {
209 if ((r
= pa_unix_socket_is_stale(fn
)) < 0)
210 return errno
!= ENOENT
? -1 : 0;
215 /* Yes, here is a race condition. But who cares? */
222 void pa_check_for_sigpipe(void) {
225 if (sigaction(SIGPIPE
, NULL
, &sa
) < 0) {
226 fprintf(stderr
, __FILE__
": sigaction() failed: %s\n", strerror(errno
));
230 if (sa
.sa_handler
== SIG_DFL
)
231 fprintf(stderr
, "polypaudio: WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");