1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: rxvtdaemon.C |
2 | * File: rxvtdaemon.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2007 Marc Lehmann <schmorp@schmorp.de> |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your option) any later version. |
… | |
… | |
27 | #include <inttypes.h> |
27 | #include <inttypes.h> |
28 | #include <unistd.h> |
28 | #include <unistd.h> |
29 | #include <errno.h> |
29 | #include <errno.h> |
30 | #include <sys/types.h> |
30 | #include <sys/types.h> |
31 | #include <sys/utsname.h> |
31 | #include <sys/utsname.h> |
32 | #include <limits.h> |
|
|
33 | |
32 | |
34 | #include "rxvtdaemon.h" |
33 | #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 | } |
|
|
48 | |
34 | |
49 | char *rxvt_connection::unix_sockname () |
35 | char *rxvt_connection::unix_sockname () |
50 | { |
36 | { |
51 | char name[PATH_MAX]; |
37 | char name[PATH_MAX]; |
52 | char *path = getenv ("RXVT_SOCKET"); |
38 | char *path = getenv ("RXVT_SOCKET"); |
… | |
… | |
69 | |
55 | |
70 | void rxvt_connection::send (const char *data, int len) |
56 | void rxvt_connection::send (const char *data, int len) |
71 | { |
57 | { |
72 | uint8_t s[2]; |
58 | uint8_t s[2]; |
73 | |
59 | |
|
|
60 | if (len > 65535) |
|
|
61 | len = 65535; |
|
|
62 | |
74 | s[0] = len >> 8; s[1] = len; |
63 | s[0] = len >> 8; s[1] = len; |
75 | |
64 | |
76 | write (fd, s, 2); |
65 | write (fd, s, 2); |
77 | write (fd, data, len); |
66 | write (fd, data, len); |
78 | } |
67 | } |
… | |
… | |
85 | bool rxvt_connection::recv (auto_str &data, int *len) |
74 | bool rxvt_connection::recv (auto_str &data, int *len) |
86 | { |
75 | { |
87 | uint8_t s[2]; |
76 | uint8_t s[2]; |
88 | int l; |
77 | int l; |
89 | |
78 | |
90 | if (read_ (fd, s, 2) != 2) |
79 | if (read (fd, s, 2) != 2) |
91 | return false; |
80 | return false; |
92 | |
81 | |
93 | l = (s[0] << 8) + s[1]; |
82 | l = (s[0] << 8) + s[1]; |
94 | if (l > 4096) |
83 | if (l > 65535) |
95 | return false; |
84 | return false; |
96 | |
85 | |
97 | if (len) |
86 | if (len) |
98 | *len = l; |
87 | *len = l; |
99 | |
88 | |
100 | data = new char[l + 1]; |
89 | data = new char[l + 1]; |
101 | |
90 | |
102 | if (!data) |
91 | if (!data) |
103 | return false; |
92 | return false; |
104 | |
93 | |
105 | if (read_ (fd, data, l) != l) |
94 | if (read (fd, data, l) != l) |
106 | return false; |
95 | return false; |
107 | |
96 | |
108 | data[l] = 0; |
97 | data[l] = 0; |
109 | |
98 | |
110 | return true; |
99 | return true; |
… | |
… | |
121 | |
110 | |
122 | bool rxvt_connection::recv (int &data) |
111 | bool rxvt_connection::recv (int &data) |
123 | { |
112 | { |
124 | uint8_t s[4]; |
113 | uint8_t s[4]; |
125 | |
114 | |
126 | if (read_ (fd, s, 4) != 4) |
115 | if (read (fd, s, 4) != 4) |
127 | return false; |
116 | return false; |
128 | |
117 | |
129 | data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
118 | data = (((((s[0] << 8) | s[1]) << 8) | s[2]) << 8) | s[3]; |
130 | |
119 | |
131 | return true; |
120 | return true; |