00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 extern "C" {
00040 #include <unistd.h>
00041 #include <fcntl.h>
00042 }
00043
00044 #include <sstream>
00045
00046 #include "consmgr.h"
00047 #include "ttyconn.h"
00048 #include "guard.h"
00049
00050 TTYConnector::TTYConnector(const std::string &dev) : saved_state(NULL)
00051 {
00052
00053 int ret = pthread_mutex_init(&mutex, NULL);
00054 PTHREAD_CHECK_AND_THROW(ret, "mutex_init(mutex)");
00055
00056
00057 open_device(dev);
00058 connect();
00059 }
00060
00061 TTYConnector::TTYConnector(const int in, const int out) : saved_state(NULL)
00062 {
00063
00064 int ret = pthread_mutex_init(&mutex, NULL);
00065 PTHREAD_CHECK_AND_THROW(ret, "mutex_init(mutex)");
00066
00067
00068 setup_fds(in, out);
00069 connect();
00070 }
00071
00072 TTYConnector::~TTYConnector(void) throw()
00073 {
00074 int ret;
00075
00076 disconnect();
00077 #if SHOULD_NOT_BE_HERE
00078
00079
00080
00081
00082 clog << "Calling shutdown() from TTYConnector(" << this << ")::~TTYConnector" << endl;
00083 try {
00084 shutdown();
00085 } CATCH_OR_DIE(17) {
00086 cerr << "Connection::shutdown from TTYConnector(" << this << ") failed!"
00087 << endl;
00088 exit(17);
00089 }
00090 #endif
00091
00092 ret = pthread_mutex_destroy(&mutex);
00093 if (ret != 0)
00094 cerr << "TTYConnector::~TTYConnector: Unable to destroy mutex ("
00095 << &mutex << "): " << strerror(ret) << endl;
00096
00097 clog << "TTYConnector::~TTYConnector() returning" << endl;
00098
00099 return;
00100 }
00101
00102 #if 0
00103 void
00104 TTYConnector::notify_on_disconnect(const Messageable *owner)
00105 {
00106 add_recipient(owner);
00107 }
00108 #endif
00109
00110
00111
00112
00113
00114 void
00115 TTYConnector::setup_fds(int r, int w)
00116 {
00117 struct termios tio;
00118
00119 {
00120
00121
00122 Guard locker(&mutex);
00123
00124 read_fd = r;
00125 write_fd = w;
00126 }
00127
00128
00129
00130 if (isatty(r)) {
00131 clog << "RAW'ing fd#" << r << " (" << ttyname(r) << ")" << endl;
00132 saved_state = new struct termios;
00133
00134 if (tcgetattr(r, saved_state) < 0) {
00135 throw(fireball(48,
00136 "TTYConnector::setup_fds() - tcgetattr failed", true));
00137 }
00138 memcpy(&tio, saved_state, sizeof(struct termios));
00139 cfmakeraw(&tio);
00140 tio.c_cflag |= CLOCAL;
00141 tcsetattr(r, TCSAFLUSH, &tio);
00142 clog << "tcsetaddr completed" << endl;
00143 }
00144
00145 clog << "TTYConnector::setup_fds(" << r << ", " << w << ")";
00146
00147 return;
00148 }
00149
00150
00151 void
00152 TTYConnector::open_device(string dev_fn)
00153 {
00154 int fd;
00155
00156 fd = ::open(dev_fn.c_str(), O_RDWR, 0);
00157 if (fd < 0) {
00158 throw "Error from ::open(): " + string(strerror(errno));
00159 }
00160
00161 Guard locker(&mutex);
00162
00163
00164 read_fd = write_fd = fd;
00165
00166 return;
00167 }
00168
00169
00170
00171
00172
00173 void
00174 TTYConnector::connect(void)
00175 {
00176
00177
00178 Guard locker(&mutex);
00179
00180
00181
00182 if ((read_fd < 0) || (write_fd < 0))
00183 throw(fireball(21, "Ack. Unset read_fd or write_fd!"));
00184
00185
00186
00187 #if 0
00188
00189 this->set_connected(true);
00190 #endif
00191
00192 return;
00193 }
00194
00197 void
00198 TTYConnector::disconnect(void)
00199 {
00200 clog << "TTYConnector(" << this << ")::disconnect: ";
00201
00202 if (saved_state) {
00203 clog << "Restoring saved tty state..." << endl;
00204 tcsetattr(read_fd, TCSANOW, saved_state);
00205 } else
00206 clog << "no saved tty state to restore..." << endl;
00207
00208 close(read_fd);
00209 if (write_fd != read_fd)
00210 close(write_fd);
00211 }
00212
00213
00214
00215
00216
00217 string
00218 TTYConnector::get_id(void) const
00219 {
00220 std::ostringstream id_str;
00221
00222 id_str << "TTYConnector(read " << read_fd << "; write " << write_fd << ")";
00223
00224
00225 return(id_str.str());
00226 }
00227
00231 bool
00232 TTYConnector::connected(void) const
00233 {
00234 Guard locker(&mutex);
00235
00236 if ((read_fd >= 0) && (write_fd >= 0))
00237 return true;
00238 else
00239 return false;
00240 }
00241
00242
00243
00244 Data *
00245 TTYConnector::read()
00246 {
00247 char *buf = new char[Connector::BufSize];
00248 int len;
00249 while ((len = ::read(read_fd, buf, Connector::BufSize)) <= 0) {
00250
00251 }
00252
00253
00254
00255
00256
00257 return new Data((u_char *)buf, len);
00258 }
00259
00260 ssize_t
00261 TTYConnector::write(const Data &b)
00262 {
00263 return ::write(write_fd, b.data(), b.len());
00264 }