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

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