… | |
… | |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | *----------------------------------------------------------------------*/ |
21 | *----------------------------------------------------------------------*/ |
22 | |
22 | |
23 | #include <stdint.h> |
|
|
24 | #include <cstdlib> |
23 | #include <cstdlib> |
25 | #include <cstring> |
24 | #include <cstring> |
26 | #include <cstdio> |
25 | #include <cstdio> |
27 | |
26 | |
|
|
27 | #include <inttypes.h> |
28 | #include <unistd.h> |
28 | #include <unistd.h> |
|
|
29 | #include <errno.h> |
|
|
30 | #include <sys/types.h> |
29 | #include <sys/utsname.h> |
31 | #include <sys/utsname.h> |
30 | #include <limits.h> |
32 | #include <limits.h> |
31 | |
33 | |
32 | #include "rxvtdaemon.h" |
34 | #include "rxvtdaemon.h" |
|
|
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 | } |
33 | |
48 | |
34 | char *rxvt_connection::unix_sockname () |
49 | char *rxvt_connection::unix_sockname () |
35 | { |
50 | { |
36 | char name[PATH_MAX]; |
51 | char name[PATH_MAX]; |
37 | char *path = getenv ("RXVT_SOCKET"); |
52 | char *path = getenv ("RXVT_SOCKET"); |
… | |
… | |
40 | { |
55 | { |
41 | struct utsname u; |
56 | struct utsname u; |
42 | uname (&u); |
57 | uname (&u); |
43 | |
58 | |
44 | path = getenv ("HOME"); |
59 | path = getenv ("HOME"); |
45 | snprintf (name, PATH_MAX, "%s/.rxvt-%s", |
60 | snprintf (name, PATH_MAX, "%s/.rxvt-unicode-%s", |
46 | path ? path : "/tmp", |
61 | path ? path : "/tmp", |
47 | u.nodename); |
62 | u.nodename); |
48 | |
63 | |
49 | path = name; |
64 | path = name; |
50 | } |
65 | } |
… | |
… | |
70 | bool rxvt_connection::recv (auto_str &data, int *len) |
85 | bool rxvt_connection::recv (auto_str &data, int *len) |
71 | { |
86 | { |
72 | uint8_t s[2]; |
87 | uint8_t s[2]; |
73 | int l; |
88 | int l; |
74 | |
89 | |
75 | if (read (fd, s, 2) != 2) |
90 | if (read_ (fd, s, 2) != 2) |
76 | return false; |
91 | return false; |
77 | |
92 | |
78 | l = (s[0] << 8) + s[1]; |
93 | l = (s[0] << 8) + s[1]; |
79 | if (l > 4096) |
94 | if (l > 4096) |
80 | return false; |
95 | return false; |
… | |
… | |
85 | data = new char[l + 1]; |
100 | data = new char[l + 1]; |
86 | |
101 | |
87 | if (!data) |
102 | if (!data) |
88 | return false; |
103 | return false; |
89 | |
104 | |
90 | if (read (fd, data, l) != l) |
105 | if (read_ (fd, data, l) != l) |
91 | return false; |
106 | return false; |
92 | |
107 | |
93 | data[l] = 0; |
108 | data[l] = 0; |
94 | |
109 | |
95 | return true; |
110 | return true; |
… | |
… | |
106 | |
121 | |
107 | bool rxvt_connection::recv (int &data) |
122 | bool rxvt_connection::recv (int &data) |
108 | { |
123 | { |
109 | uint8_t s[4]; |
124 | uint8_t s[4]; |
110 | |
125 | |
111 | if (read (fd, s, 4) != 4) |
126 | if (read_ (fd, s, 4) != 4) |
112 | return false; |
127 | return false; |
113 | |
128 | |
114 | data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
129 | data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
115 | |
130 | |
116 | return true; |
131 | return true; |