]>
code.delx.au - pulseaudio/blob - src/util.c
6 #include <netinet/in.h>
13 void make_nonblock_fd(int fd
) {
16 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
17 if (!(v
& O_NONBLOCK
))
18 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
21 void peer_to_string(char *c
, size_t l
, int fd
) {
24 assert(c
&& l
&& fd
>= 0);
26 if (fstat(fd
, &st
) < 0) {
27 snprintf(c
, l
, "Invalid client fd");
31 if (S_ISSOCK(st
.st_mode
)) {
34 struct sockaddr_in in
;
35 struct sockaddr_un un
;
37 socklen_t sa_len
= sizeof(sa
);
39 if (getpeername(fd
, &sa
.sa
, &sa_len
) >= 0) {
41 if (sa
.sa
.sa_family
== AF_INET
) {
42 uint32_t ip
= ntohl(sa
.in
.sin_addr
.s_addr
);
44 snprintf(c
, l
, "TCP/IP client from %i.%i.%i.%i:%u",
49 ntohs(sa
.in
.sin_port
));
51 } else if (sa
.sa
.sa_family
== AF_LOCAL
) {
52 snprintf(c
, l
, "UNIX client for %s", sa
.un
.sun_path
);
57 snprintf(c
, l
, "Unknown network client");
59 } else if (S_ISCHR(st
.st_mode
) && (fd
== 0 || fd
== 1)) {
60 snprintf(c
, l
, "STDIN/STDOUT client");
64 snprintf(c
, l
, "Unknown client");
67 int make_secure_dir(const char* dir
) {
70 if (mkdir(dir
, 0700) < 0)
74 if (lstat(dir
, &st
) < 0)
77 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))