This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsock.c
78 lines (75 loc) · 1.49 KB
/
sock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <sys/un.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include "rt.h"
int
findsock()
{
DIR *dir;
struct dirent *e;
int rv, pid;
char buf[1024];
char s[1024];
unsigned long inode, i;
int sock, sk, len;
struct sockaddr *sa;
struct sockaddr_un *sau;
struct sockaddr_in *sai;
pid = getpid();
snprintf(s, sizeof(s), "/proc/%d/fd", pid);
dir = opendir(s);
if (!dir)
return -1;
sock = inode = -1;
while ((e = readdir(dir))) {
snprintf(s, sizeof(s), "/proc/%d/fd/%s", pid, e->d_name);
rv = readlink(s, buf, sizeof(buf));
if (0 > rv)
continue;
if (rv >= sizeof(buf)) {
rs_log("buffer overflow\n");
continue;
}
buf[rv] = '\0';
rv = sscanf(buf, "socket:[%lu]", &i);
if (rv != 1)
continue; /* not a socket */
sk = atoi(e->d_name);
sa = (struct sockaddr *)s;
len = sizeof(s);
rv = getpeername(sk, sa, &len);
if (0 > rv) {
perror("getpeername");
continue;
}
switch (sa->sa_family) {
case AF_INET:
sai = (struct sockaddr_in *)sa;
if (sai->sin_port < htons(6000)
|| sai->sin_port >= htons(6010))
continue;
break;
case AF_UNIX:
sau = (struct sockaddr_un *)sa;
if (!strstr(sau->sun_path, "X11"))
continue;
break;
default:
continue;
}
if (sock >= 0 && inode != i) {
rs_log("WARNING: multiple X connections!\n");
continue;
}
sock = sk;
inode = i;
}
return sock;
}