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
00040
00041 extern "C" {
00042 #include <unistd.h>
00043 #include <netinet/in.h>
00044 }
00045
00046 #include <fstream>
00047
00048 #include "consmgr.h"
00049 #include "cmconfig.h"
00050 #include "incoming.h"
00051 #include "logfile.h"
00052 #include "history.h"
00053 #include "ttyconn.h"
00054 #include "udslistener.h"
00055
00056
00057 static void usage(void);
00058
00059 int
00060 main(int argc, char *argv[])
00061 {
00062 int ret, ch;
00063
00064
00065 std::ofstream *null = new std::ofstream("/dev/null");
00066 clog.rdbuf(null->rdbuf());
00067
00068 bool debug = false;
00069 bool foreground = false;
00070 pthread_t server_pt, client_pt;
00071 std::vector<pthread_t> threads;
00072 Config *cnf = NULL;
00073 BiDirConn *serv = NULL;
00074
00075
00076 try {
00077 while ((ch = getopt(argc, argv, "c:fDv")) != -1) {
00078 switch (ch) {
00079 case 'c':
00080 if (cnf)
00081 err(5,"Only one configuration file [currently] allowed");
00082 else
00083 cnf = ConfigFactory::config_object(optarg);
00084 break;
00085 case 'D':
00086 debug = true;
00087 break;
00088 case 'f':
00089 foreground = true;
00090 break;
00091 case 'v':
00092 std::cout << "ttysrv, part of consmgr, built " << __DATE__
00093 << " " << __TIME__ << endl;
00094 break;
00095 case '?':
00096 usage();
00097 exit(1);
00098 }
00099 }
00100
00101 if (cnf == NULL)
00102 cnf = ConfigFactory::config_object("/etc/consmgr.conf");
00103
00104 argc -= optind;
00105 argv += optind;
00106
00107 if (argc != 1) {
00108 usage();
00109 exit(1);
00110 }
00111 } catch (const string &error) {
00112 cerr << error << endl;
00113 exit(7);
00114 } catch (...) {
00115 cerr << "Other random exception thrown while parsing arguments"
00116 << endl;
00117 exit(7);
00118 }
00119
00120
00121 string entry(argv[0]);
00122
00123
00124
00125 if (debug) {
00126 clog.rdbuf(cerr.rdbuf());
00127 delete null;
00128 }
00129
00130
00131 try {
00132 cnf->load_entry(argv[0]);
00133 } CATCH_STR_OR_DIE(2) {
00134 cerr << "** Uncaught exception in cnf.load_entry(" << entry
00135 << ")!!" << endl;
00136 exit(2);
00137 }
00138
00139
00140 try {
00141 if (cnf->find("device") == cnf->end())
00142 throw "No 'device' parameter in configuration for '" + entry + "'";
00143 ConnectorP dev(new TTYConnector((*cnf)["device"]));
00144 serv = new BiDirConn(dev);
00145 } CATCH_STR_OR_DIE(3) {
00146 cerr << "** Uncaught exception in open_dev(cnf.str(\"device\"))!!"
00147 << endl;
00148 exit(3);
00149 }
00150
00151 #if 0
00152 ret = pthread_create(&server_pt, NULL, BiDirConn::establish_wrapper,
00153 serv);
00154 PTHREAD_CHECK_AND_THROW(ret, "pthread_create(server_pt)");
00155 threads.push_back(server_pt);
00156 #endif
00157
00158
00159
00160 if (cnf->find("logfile") != cnf->end()) {
00161
00162 try {
00163
00164
00165 ConnectorP lf(new LogFile((*cnf)["logfile"]));
00166 #if 0
00167 pthread_t log_pt;
00168
00169
00170 ret = pthread_create(&log_pt, NULL, Outlet::establish, lf);
00171 PTHREAD_CHECK_AND_THROW(ret, "pthread_create(log_pt)");
00172
00173 threads.push_back(log_pt);
00174 #endif
00175 serv->add_recipient(new Outlet(lf));
00176 } CATCH_STR_OR_DIE(4) {
00177 cerr << "Unable to open and attach logfile '" << (*cnf)["logfile"]
00178 << "'." << endl;
00179 }
00180 }
00181 #if defined(HISTORY) && (HISTORY > 0)
00182
00183 try {
00184 ConnectorP hist(new History(HISTORY));
00185 #if 0
00186 pthread_t hist_pt;
00187
00188 ret = pthread_create(&hist_pt, NULL, Outlet::establish, hist);
00189 PTHREAD_CHECK_AND_THROW(ret, "pthread_create(hist_pt)");
00190
00191 threads.push_back(hist_pt);
00192 #endif
00193 serv->add_recipient(new Outlet(hist));
00194 } CATCH_STR_OR_DIE(5) {
00195 cerr << "Unable to set up History module (size " << HISTORY
00196 << ")." << endl;
00197 }
00198 #endif
00199
00200
00201 std::string socket_path;
00202
00203 if (cnf->find("local_socket") != cnf->end())
00204 try {
00205 socket_path = (*cnf)["local_socket"];
00206 } CATCH_STR_OR_DIE(5) {
00207 cerr << "Unable to get/set UDSConn address from configuration." << endl;
00208 exit(5);
00209 }
00210 else {
00211 cerr << "No 'local_socket' parameter is specified for " << argv[0]
00212 << "; cannot proceed." << endl;
00213 exit(5);
00214 }
00215
00216 SocketAddressP client_socket(new UDSSocketAddress(socket_path));
00217 ListenerP base(new UDSListener(client_socket));
00218
00219 Incoming client(base, CONNECT_TO_ONE(serv));
00220 #if 0
00221 ret = pthread_create(&client_pt, NULL, Listener::listen_for_connections,
00222 &client);
00223 PTHREAD_CHECK_AND_THROW(ret, "pthread_create(client_pt)");
00224 threads.push_back(client_pt);
00225 #endif
00226
00227
00228
00229
00230
00231
00232
00233 ret = pthread_join(server_pt, NULL);
00234 PTHREAD_CHECK_AND_THROW(ret, "pthread_join");
00235
00236 cerr << "Exiting..." << endl;
00237
00238
00239 base->disconnect();
00240
00241 exit(0);
00242 }
00243
00244 static void
00245 usage(void)
00246 {
00247 cerr << "Usage: " << __progname << " [-D] [-t] [-c <config-file>]"
00248 << " <connection-name>" << endl;
00249 return;
00250 }
00251