… | |
… | |
31 | #include <sys/utsname.h> |
31 | #include <sys/utsname.h> |
32 | #include <limits.h> |
32 | #include <limits.h> |
33 | |
33 | |
34 | #include "rxvtdaemon.h" |
34 | #include "rxvtdaemon.h" |
35 | |
35 | |
36 | // works around linux kernel bug, returns EAGAIN on a blocking socket |
|
|
37 | static ssize_t |
|
|
38 | read_ (int fd, void *buf, size_t count) |
|
|
39 | { |
|
|
40 | int ret; |
|
|
41 | |
|
|
42 | do |
|
|
43 | ret = read (fd, buf, count); |
|
|
44 | while (ret < 0 && errno == EAGAIN); |
|
|
45 | |
|
|
46 | return ret; |
|
|
47 | } |
|
|
48 | |
|
|
49 | char *rxvt_connection::unix_sockname () |
36 | char *rxvt_connection::unix_sockname () |
50 | { |
37 | { |
51 | char name[PATH_MAX]; |
38 | char name[PATH_MAX]; |
52 | char *path = getenv ("RXVT_SOCKET"); |
39 | char *path = getenv ("RXVT_SOCKET"); |
53 | |
40 | |
… | |
… | |
69 | |
56 | |
70 | void rxvt_connection::send (const char *data, int len) |
57 | void rxvt_connection::send (const char *data, int len) |
71 | { |
58 | { |
72 | uint8_t s[2]; |
59 | uint8_t s[2]; |
73 | |
60 | |
|
|
61 | if (len > 65535) |
|
|
62 | len = 65535; |
|
|
63 | |
74 | s[0] = len >> 8; s[1] = len; |
64 | s[0] = len >> 8; s[1] = len; |
75 | |
65 | |
76 | write (fd, s, 2); |
66 | write (fd, s, 2); |
77 | write (fd, data, len); |
67 | write (fd, data, len); |
78 | } |
68 | } |
… | |
… | |
85 | bool rxvt_connection::recv (auto_str &data, int *len) |
75 | bool rxvt_connection::recv (auto_str &data, int *len) |
86 | { |
76 | { |
87 | uint8_t s[2]; |
77 | uint8_t s[2]; |
88 | int l; |
78 | int l; |
89 | |
79 | |
90 | if (read_ (fd, s, 2) != 2) |
80 | if (read (fd, s, 2) != 2) |
91 | return false; |
81 | return false; |
92 | |
82 | |
93 | l = (s[0] << 8) + s[1]; |
83 | l = (s[0] << 8) + s[1]; |
94 | if (l > 4096) |
84 | if (l > 65535) |
95 | return false; |
85 | return false; |
96 | |
86 | |
97 | if (len) |
87 | if (len) |
98 | *len = l; |
88 | *len = l; |
99 | |
89 | |
100 | data = new char[l + 1]; |
90 | data = new char[l + 1]; |
101 | |
91 | |
102 | if (!data) |
92 | if (!data) |
103 | return false; |
93 | return false; |
104 | |
94 | |
105 | if (read_ (fd, data, l) != l) |
95 | if (read (fd, data, l) != l) |
106 | return false; |
96 | return false; |
107 | |
97 | |
108 | data[l] = 0; |
98 | data[l] = 0; |
109 | |
99 | |
110 | return true; |
100 | return true; |
… | |
… | |
121 | |
111 | |
122 | bool rxvt_connection::recv (int &data) |
112 | bool rxvt_connection::recv (int &data) |
123 | { |
113 | { |
124 | uint8_t s[4]; |
114 | uint8_t s[4]; |
125 | |
115 | |
126 | if (read_ (fd, s, 4) != 4) |
116 | if (read (fd, s, 4) != 4) |
127 | return false; |
117 | return false; |
128 | |
118 | |
129 | data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
119 | data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
130 | |
120 | |
131 | return true; |
121 | return true; |