You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncviewer.cxx 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  3. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <signal.h>
  29. #include <locale.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <sys/stat.h>
  33. #ifdef WIN32
  34. #include <os/winerrno.h>
  35. #include <direct.h>
  36. #define mkdir(path, mode) _mkdir(path)
  37. #endif
  38. #if !defined(WIN32) && !defined(__APPLE__)
  39. #include <X11/Xlib.h>
  40. #include <X11/XKBlib.h>
  41. #endif
  42. #include <rfb/Logger_stdio.h>
  43. #ifdef HAVE_GNUTLS
  44. #include <rfb/CSecurityTLS.h>
  45. #endif
  46. #include <rfb/LogWriter.h>
  47. #include <rfb/Timer.h>
  48. #include <rfb/Exception.h>
  49. #include <network/TcpSocket.h>
  50. #include <os/os.h>
  51. #include <FL/Fl.H>
  52. #include <FL/Fl_Widget.H>
  53. #include <FL/Fl_PNG_Image.H>
  54. #include <FL/Fl_Sys_Menu_Bar.H>
  55. #include <FL/fl_ask.H>
  56. #include <FL/x.H>
  57. #include "i18n.h"
  58. #include "parameters.h"
  59. #include "CConn.h"
  60. #include "ServerDialog.h"
  61. #include "UserDialog.h"
  62. #include "touch.h"
  63. #include "vncviewer.h"
  64. #include "fltk_layout.h"
  65. #ifdef WIN32
  66. #include "resource.h"
  67. #include "win32.h"
  68. #endif
  69. rfb::LogWriter vlog("main");
  70. using namespace network;
  71. using namespace rfb;
  72. using namespace std;
  73. char vncServerName[VNCSERVERNAMELEN] = { '\0' };
  74. static const char *argv0 = NULL;
  75. static bool inMainloop = false;
  76. static bool exitMainloop = false;
  77. static const char *exitError = NULL;
  78. static const char *about_text()
  79. {
  80. static char buffer[1024];
  81. // This is used in multiple places with potentially different
  82. // encodings, so we need to make sure we get a fresh string every
  83. // time.
  84. snprintf(buffer, sizeof(buffer),
  85. _("TigerVNC Viewer %d-bit v%s\n"
  86. "Built on: %s\n"
  87. "Copyright (C) 1999-%d TigerVNC Team and many others (see README.rst)\n"
  88. "See https://www.tigervnc.org for information on TigerVNC."),
  89. (int)sizeof(size_t)*8, PACKAGE_VERSION,
  90. BUILD_TIMESTAMP, 2021);
  91. return buffer;
  92. }
  93. void exit_vncviewer(const char *error, ...)
  94. {
  95. // Prioritise the first error we get as that is probably the most
  96. // relevant one.
  97. if ((error != NULL) && (exitError == NULL)) {
  98. va_list ap;
  99. va_start(ap, error);
  100. exitError = (char*)malloc(1024);
  101. if (exitError)
  102. (void) vsnprintf((char*)exitError, 1024, error, ap);
  103. va_end(ap);
  104. }
  105. if (inMainloop)
  106. exitMainloop = true;
  107. else {
  108. // We're early in the startup. Assume we can just exit().
  109. if (alertOnFatalError && (exitError != NULL))
  110. fl_alert("%s", exitError);
  111. exit(EXIT_FAILURE);
  112. }
  113. }
  114. bool should_exit()
  115. {
  116. return exitMainloop;
  117. }
  118. void about_vncviewer()
  119. {
  120. fl_message_title(_("About TigerVNC Viewer"));
  121. fl_message("%s", about_text());
  122. }
  123. void run_mainloop()
  124. {
  125. int next_timer;
  126. next_timer = Timer::checkTimeouts();
  127. if (next_timer == 0)
  128. next_timer = INT_MAX;
  129. if (Fl::wait((double)next_timer / 1000.0) < 0.0) {
  130. vlog.error(_("Internal FLTK error. Exiting."));
  131. exit(-1);
  132. }
  133. }
  134. #ifdef __APPLE__
  135. static void about_callback(Fl_Widget *widget, void *data)
  136. {
  137. about_vncviewer();
  138. }
  139. static void new_connection_cb(Fl_Widget *widget, void *data)
  140. {
  141. const char *argv[2];
  142. pid_t pid;
  143. pid = fork();
  144. if (pid == -1) {
  145. vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno));
  146. return;
  147. }
  148. if (pid != 0)
  149. return;
  150. argv[0] = argv0;
  151. argv[1] = NULL;
  152. execvp(argv[0], (char * const *)argv);
  153. vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno));
  154. _exit(1);
  155. }
  156. #endif
  157. static void CleanupSignalHandler(int sig)
  158. {
  159. // CleanupSignalHandler allows C++ object cleanup to happen because it calls
  160. // exit() rather than the default which is to abort.
  161. vlog.info(_("Termination signal %d has been received. TigerVNC Viewer will now exit."), sig);
  162. exit(1);
  163. }
  164. static void init_fltk()
  165. {
  166. // Basic text size (10pt @ 96 dpi => 13px)
  167. FL_NORMAL_SIZE = 13;
  168. // Select a FLTK scheme and background color that looks somewhat
  169. // close to modern systems
  170. Fl::scheme("gtk+");
  171. Fl::background(220, 220, 220);
  172. // macOS has a slightly brighter default background though
  173. #ifdef __APPLE__
  174. Fl::background(240, 240, 240);
  175. #endif
  176. // Proper Gnome Shell integration requires that we set a sensible
  177. // WM_CLASS for the window.
  178. Fl_Window::default_xclass("vncviewer");
  179. // Set the default icon for all windows.
  180. #ifdef WIN32
  181. HICON lg, sm;
  182. lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
  183. IMAGE_ICON, GetSystemMetrics(SM_CXICON),
  184. GetSystemMetrics(SM_CYICON),
  185. LR_DEFAULTCOLOR | LR_SHARED);
  186. sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
  187. IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
  188. GetSystemMetrics(SM_CYSMICON),
  189. LR_DEFAULTCOLOR | LR_SHARED);
  190. Fl_Window::default_icons(lg, sm);
  191. #elif ! defined(__APPLE__)
  192. const int icon_sizes[] = {48, 32, 24, 16};
  193. Fl_PNG_Image *icons[4];
  194. int count;
  195. count = 0;
  196. // FIXME: Follow icon theme specification
  197. for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) {
  198. char icon_path[PATH_MAX];
  199. bool exists;
  200. sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png",
  201. CMAKE_INSTALL_FULL_DATADIR, icon_sizes[i], icon_sizes[i]);
  202. #ifndef WIN32
  203. struct stat st;
  204. if (stat(icon_path, &st) != 0)
  205. #else
  206. struct _stat st;
  207. if (_stat(icon_path, &st) != 0)
  208. return(false);
  209. #endif
  210. exists = false;
  211. else
  212. exists = true;
  213. if (exists) {
  214. icons[count] = new Fl_PNG_Image(icon_path);
  215. if (icons[count]->w() == 0 ||
  216. icons[count]->h() == 0 ||
  217. icons[count]->d() != 4) {
  218. delete icons[count];
  219. continue;
  220. }
  221. count++;
  222. }
  223. }
  224. Fl_Window::default_icons((const Fl_RGB_Image**)icons, count);
  225. for (int i = 0;i < count;i++)
  226. delete icons[i];
  227. #endif
  228. // This makes the "icon" in dialogs rounded, which fits better
  229. // with the above schemes.
  230. fl_message_icon()->box(FL_UP_BOX);
  231. // Turn off the annoying behaviour where popups track the mouse.
  232. fl_message_hotspot(false);
  233. // Avoid empty titles for popups
  234. fl_message_title_default(_("TigerVNC Viewer"));
  235. #ifdef WIN32
  236. // Most "normal" Windows apps use this font for UI elements.
  237. Fl::set_font(FL_HELVETICA, "Tahoma");
  238. #endif
  239. // FLTK exposes these so that we can translate them.
  240. fl_no = _("No");
  241. fl_yes = _("Yes");
  242. fl_ok = _("OK");
  243. fl_cancel = _("Cancel");
  244. fl_close = _("Close");
  245. #ifdef __APPLE__
  246. /* Needs trailing space */
  247. static char fltk_about[16];
  248. snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About"));
  249. Fl_Mac_App_Menu::about = fltk_about;
  250. static char fltk_hide[16];
  251. snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide"));
  252. Fl_Mac_App_Menu::hide = fltk_hide;
  253. static char fltk_quit[16];
  254. snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit"));
  255. Fl_Mac_App_Menu::quit = fltk_quit;
  256. Fl_Mac_App_Menu::print = ""; // Don't want the print item
  257. Fl_Mac_App_Menu::services = _("Services");
  258. Fl_Mac_App_Menu::hide_others = _("Hide Others");
  259. Fl_Mac_App_Menu::show = _("Show All");
  260. fl_mac_set_about(about_callback, NULL);
  261. Fl_Sys_Menu_Bar *menubar;
  262. char buffer[1024];
  263. menubar = new Fl_Sys_Menu_Bar(0, 0, 500, 25);
  264. // Fl_Sys_Menu_Bar overrides methods without them being virtual,
  265. // which means we cannot use our generic Fl_Menu_ helpers.
  266. if (fltk_menu_escape(p_("SysMenu|", "&File"),
  267. buffer, sizeof(buffer)) < sizeof(buffer))
  268. menubar->add(buffer, 0, 0, 0, FL_SUBMENU);
  269. if (fltk_menu_escape(p_("SysMenu|File|", "&New Connection"),
  270. buffer, sizeof(buffer)) < sizeof(buffer))
  271. menubar->insert(1, buffer, FL_COMMAND | 'n', new_connection_cb);
  272. #endif
  273. }
  274. static void mkvnchomedir()
  275. {
  276. // Create .vnc in the user's home directory if it doesn't already exist
  277. char* homeDir = NULL;
  278. if (getvnchomedir(&homeDir) == -1) {
  279. vlog.error(_("Could not create VNC home directory: can't obtain home "
  280. "directory path."));
  281. } else {
  282. int result = mkdir(homeDir, 0755);
  283. if (result == -1 && errno != EEXIST)
  284. vlog.error(_("Could not create VNC home directory: %s."), strerror(errno));
  285. delete [] homeDir;
  286. }
  287. }
  288. static void usage(const char *programName)
  289. {
  290. #ifdef WIN32
  291. // If we don't have a console then we need to create one for output
  292. if (GetConsoleWindow() == NULL) {
  293. HANDLE handle;
  294. int fd;
  295. AllocConsole();
  296. handle = GetStdHandle(STD_ERROR_HANDLE);
  297. fd = _open_osfhandle((intptr_t)handle, O_TEXT);
  298. *stderr = *fdopen(fd, "w");
  299. }
  300. #endif
  301. fprintf(stderr,
  302. "\n"
  303. "usage: %s [parameters] [host][:displayNum]\n"
  304. " %s [parameters] [host][::port]\n"
  305. #ifndef WIN32
  306. " %s [parameters] [unix socket]\n"
  307. #endif
  308. " %s [parameters] -listen [port]\n"
  309. " %s [parameters] [.tigervnc file]\n",
  310. programName, programName,
  311. #ifndef WIN32
  312. programName,
  313. #endif
  314. programName, programName);
  315. #if !defined(WIN32) && !defined(__APPLE__)
  316. fprintf(stderr,"\n"
  317. "Options:\n\n"
  318. " -display Xdisplay - Specifies the X display for the viewer window\n"
  319. " -geometry geometry - Initial position of the main VNC viewer window. See the\n"
  320. " man page for details.\n");
  321. #endif
  322. fprintf(stderr,"\n"
  323. "Parameters can be turned on with -<param> or off with -<param>=0\n"
  324. "Parameters which take a value can be specified as "
  325. "-<param> <value>\n"
  326. "Other valid forms are <param>=<value> -<param>=<value> "
  327. "--<param>=<value>\n"
  328. "Parameter names are case-insensitive. The parameters are:\n\n");
  329. Configuration::listParams(79, 14);
  330. #ifdef WIN32
  331. // Just wait for the user to kill the console window
  332. Sleep(INFINITE);
  333. #endif
  334. exit(1);
  335. }
  336. static void
  337. potentiallyLoadConfigurationFile(char *vncServerName)
  338. {
  339. const bool hasPathSeparator = (strchr(vncServerName, '/') != NULL ||
  340. (strchr(vncServerName, '\\')) != NULL);
  341. if (hasPathSeparator) {
  342. #ifndef WIN32
  343. struct stat sb;
  344. // This might be a UNIX socket, we need to check
  345. if (stat(vncServerName, &sb) == -1) {
  346. // Some access problem; let loadViewerParameters() deal with it...
  347. } else {
  348. if ((sb.st_mode & S_IFMT) == S_IFSOCK)
  349. return;
  350. }
  351. #endif
  352. try {
  353. const char* newServerName;
  354. newServerName = loadViewerParameters(vncServerName);
  355. // This might be empty, but we still need to clear it so we
  356. // don't try to connect to the filename
  357. strncpy(vncServerName, newServerName, VNCSERVERNAMELEN-1);
  358. vncServerName[VNCSERVERNAMELEN-1] = '\0';
  359. } catch (rfb::Exception& e) {
  360. vlog.error("%s", e.str());
  361. exit_vncviewer(_("Error reading configuration file \"%s\":\n\n%s"),
  362. vncServerName, e.str());
  363. }
  364. }
  365. }
  366. #ifndef WIN32
  367. static int
  368. interpretViaParam(char *remoteHost, int *remotePort, int localPort)
  369. {
  370. const int SERVER_PORT_OFFSET = 5900;
  371. char *pos = strchr(vncServerName, ':');
  372. if (pos == NULL)
  373. *remotePort = SERVER_PORT_OFFSET;
  374. else {
  375. int portOffset = SERVER_PORT_OFFSET;
  376. size_t len;
  377. *pos++ = '\0';
  378. len = strlen(pos);
  379. if (*pos == ':') {
  380. /* Two colons is an absolute port number, not an offset. */
  381. pos++;
  382. len--;
  383. portOffset = 0;
  384. }
  385. if (!len || strspn (pos, "-0123456789") != len )
  386. return 1;
  387. *remotePort = atoi(pos) + portOffset;
  388. }
  389. if (*vncServerName != '\0')
  390. strcpy(remoteHost, vncServerName);
  391. else
  392. strcpy(remoteHost, "localhost");
  393. snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort);
  394. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  395. return 0;
  396. }
  397. static void
  398. createTunnel(const char *gatewayHost, const char *remoteHost,
  399. int remotePort, int localPort)
  400. {
  401. const char *cmd = getenv("VNC_VIA_CMD");
  402. char *cmd2, *percent;
  403. char lport[10], rport[10];
  404. sprintf(lport, "%d", localPort);
  405. sprintf(rport, "%d", remotePort);
  406. setenv("G", gatewayHost, 1);
  407. setenv("H", remoteHost, 1);
  408. setenv("R", rport, 1);
  409. setenv("L", lport, 1);
  410. if (!cmd)
  411. cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20";
  412. /* Compatibility with TigerVNC's method. */
  413. cmd2 = strdup(cmd);
  414. while ((percent = strchr(cmd2, '%')) != NULL)
  415. *percent = '$';
  416. system(cmd2);
  417. free(cmd2);
  418. }
  419. static int mktunnel()
  420. {
  421. const char *gatewayHost;
  422. char remoteHost[VNCSERVERNAMELEN];
  423. int localPort = findFreeTcpPort();
  424. int remotePort;
  425. if (interpretViaParam(remoteHost, &remotePort, localPort) != 0)
  426. return 1;
  427. gatewayHost = (const char*)via;
  428. createTunnel(gatewayHost, remoteHost, remotePort, localPort);
  429. return 0;
  430. }
  431. #endif /* !WIN32 */
  432. int main(int argc, char** argv)
  433. {
  434. UserDialog dlg;
  435. argv0 = argv[0];
  436. setlocale(LC_ALL, "");
  437. bindtextdomain(PACKAGE_NAME, CMAKE_INSTALL_FULL_LOCALEDIR);
  438. textdomain(PACKAGE_NAME);
  439. // Write about text to console, still using normal locale codeset
  440. fprintf(stderr,"\n%s\n", about_text());
  441. // Set gettext codeset to what our GUI toolkit uses. Since we are
  442. // passing strings from strerror/gai_strerror to the GUI, these must
  443. // be in GUI codeset as well.
  444. bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
  445. bind_textdomain_codeset("libc", "UTF-8");
  446. rfb::initStdIOLoggers();
  447. #ifdef WIN32
  448. rfb::initFileLogger("C:\\temp\\vncviewer.log");
  449. #else
  450. rfb::initFileLogger("/tmp/vncviewer.log");
  451. #endif
  452. rfb::LogWriter::setLogParams("*:stderr:30");
  453. #ifdef SIGHUP
  454. signal(SIGHUP, CleanupSignalHandler);
  455. #endif
  456. signal(SIGINT, CleanupSignalHandler);
  457. signal(SIGTERM, CleanupSignalHandler);
  458. Configuration::enableViewerParams();
  459. /* Load the default parameter settings */
  460. char defaultServerName[VNCSERVERNAMELEN] = "";
  461. try {
  462. const char* configServerName;
  463. configServerName = loadViewerParameters(NULL);
  464. if (configServerName != NULL) {
  465. strncpy(defaultServerName, configServerName, VNCSERVERNAMELEN-1);
  466. defaultServerName[VNCSERVERNAMELEN-1] = '\0';
  467. }
  468. } catch (rfb::Exception& e) {
  469. vlog.error("%s", e.str());
  470. }
  471. for (int i = 1; i < argc;) {
  472. /* We need to resolve an ambiguity for booleans */
  473. if (argv[i][0] == '-' && i+1 < argc) {
  474. VoidParameter *param;
  475. param = Configuration::getParam(&argv[i][1]);
  476. if ((param != NULL) &&
  477. (dynamic_cast<BoolParameter*>(param) != NULL)) {
  478. if ((strcasecmp(argv[i+1], "0") == 0) ||
  479. (strcasecmp(argv[i+1], "1") == 0) ||
  480. (strcasecmp(argv[i+1], "true") == 0) ||
  481. (strcasecmp(argv[i+1], "false") == 0) ||
  482. (strcasecmp(argv[i+1], "yes") == 0) ||
  483. (strcasecmp(argv[i+1], "no") == 0)) {
  484. param->setParam(argv[i+1]);
  485. i += 2;
  486. continue;
  487. }
  488. }
  489. }
  490. if (Configuration::setParam(argv[i])) {
  491. i++;
  492. continue;
  493. }
  494. if (argv[i][0] == '-') {
  495. if (i+1 < argc) {
  496. if (Configuration::setParam(&argv[i][1], argv[i+1])) {
  497. i += 2;
  498. continue;
  499. }
  500. }
  501. usage(argv[0]);
  502. }
  503. strncpy(vncServerName, argv[i], VNCSERVERNAMELEN);
  504. vncServerName[VNCSERVERNAMELEN - 1] = '\0';
  505. i++;
  506. }
  507. #if !defined(WIN32) && !defined(__APPLE__)
  508. if (strcmp(display, "") != 0) {
  509. Fl::display(display);
  510. }
  511. fl_open_display();
  512. XkbSetDetectableAutoRepeat(fl_display, True, NULL);
  513. #endif
  514. init_fltk();
  515. enable_touch();
  516. // Check if the server name in reality is a configuration file
  517. potentiallyLoadConfigurationFile(vncServerName);
  518. mkvnchomedir();
  519. CSecurity::upg = &dlg;
  520. #ifdef HAVE_GNUTLS
  521. CSecurityTLS::msg = &dlg;
  522. #endif
  523. Socket *sock = NULL;
  524. #ifndef WIN32
  525. /* Specifying -via and -listen together is nonsense */
  526. if (listenMode && strlen(via.getValueStr()) > 0) {
  527. // TRANSLATORS: "Parameters" are command line arguments, or settings
  528. // from a file or the Windows registry.
  529. vlog.error(_("Parameters -listen and -via are incompatible"));
  530. exit_vncviewer(_("Parameters -listen and -via are incompatible"));
  531. return 1; /* Not reached */
  532. }
  533. #endif
  534. if (listenMode) {
  535. std::list<SocketListener*> listeners;
  536. try {
  537. int port = 5500;
  538. if (isdigit(vncServerName[0]))
  539. port = atoi(vncServerName);
  540. createTcpListeners(&listeners, 0, port);
  541. vlog.info(_("Listening on port %d"), port);
  542. /* Wait for a connection */
  543. while (sock == NULL) {
  544. fd_set rfds;
  545. FD_ZERO(&rfds);
  546. for (std::list<SocketListener*>::iterator i = listeners.begin();
  547. i != listeners.end();
  548. i++)
  549. FD_SET((*i)->getFd(), &rfds);
  550. int n = select(FD_SETSIZE, &rfds, 0, 0, 0);
  551. if (n < 0) {
  552. if (errno == EINTR) {
  553. vlog.debug("Interrupted select() system call");
  554. continue;
  555. } else {
  556. throw rdr::SystemException("select", errno);
  557. }
  558. }
  559. for (std::list<SocketListener*>::iterator i = listeners.begin ();
  560. i != listeners.end();
  561. i++)
  562. if (FD_ISSET((*i)->getFd(), &rfds)) {
  563. sock = (*i)->accept();
  564. if (sock)
  565. /* Got a connection */
  566. break;
  567. }
  568. }
  569. } catch (rdr::Exception& e) {
  570. vlog.error("%s", e.str());
  571. exit_vncviewer(_("Failure waiting for incoming VNC connection:\n\n%s"), e.str());
  572. return 1; /* Not reached */
  573. }
  574. while (!listeners.empty()) {
  575. delete listeners.back();
  576. listeners.pop_back();
  577. }
  578. } else {
  579. if (vncServerName[0] == '\0') {
  580. ServerDialog::run(defaultServerName, vncServerName);
  581. if (vncServerName[0] == '\0')
  582. return 1;
  583. }
  584. #ifndef WIN32
  585. if (strlen (via.getValueStr()) > 0 && mktunnel() != 0)
  586. usage(argv[0]);
  587. #endif
  588. }
  589. CConn *cc = new CConn(vncServerName, sock);
  590. inMainloop = true;
  591. while (!exitMainloop)
  592. run_mainloop();
  593. inMainloop = false;
  594. delete cc;
  595. if (exitError != NULL && alertOnFatalError)
  596. fl_alert("%s", exitError);
  597. return 0;
  598. }