GRASS Programmer's Manual  6.4.2(2012)
io_sock.c
Go to the documentation of this file.
00001 
00002 #include <grass/config.h>
00003 
00004 #ifdef HAVE_SOCKET
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <signal.h>
00009 #include <string.h>
00010 #include <unistd.h>
00011 #include <errno.h>
00012 
00013 #ifdef __MINGW32__
00014 #include <winsock2.h>
00015 #define ECONNREFUSED WSAECONNREFUSED
00016 #define EADDRINUSE   WSAEADDRINUSE
00017 #define ENOTSOCK     WSAENOTSOCK
00018 #define ETIMEDOUT    WSAETIMEDOUT
00019 #endif
00020 
00021 #include <grass/gis.h>
00022 #include <grass/glocale.h>
00023 #include <grass/raster.h>
00024 
00025 #include "open.h"
00026 
00027 extern int _rfd, _wfd;
00028 extern int _quiet;
00029 
00030 extern int sync_driver(char *);
00031 
00032 static char *sockpath;
00033 
00049 int REM_open_driver(void)
00050 {
00051     int verbose;
00052     char *name;
00053 
00054     verbose = !_quiet;
00055     _quiet = 0;
00056 
00057     name = getenv("MONITOR_OVERRIDE");
00058     if (!name)
00059         name = G__getenv("MONITOR");
00060 
00061     if (!name) {
00062         if (verbose) {
00063             G_warning(_("No graphics monitor has been selected for output."));
00064             G_warning(_("Please run \"d.mon\" to select a graphics monitor."));
00065         }
00066         return (NO_MON);
00067     }
00068 
00069     /* Get the full path to the unix socket */
00070     if ((sockpath = G_sock_get_fname(name)) == NULL) {
00071         if (verbose)
00072             G_warning(_("Failed to get socket name for monitor <%s>."), name);
00073         return (NO_MON);
00074     }
00075 
00076     /* See if the socket exists, if it doesn't no point in trying to
00077      * connect to it.
00078      */
00079     if (!G_sock_exists(sockpath)) {
00080         if (verbose)
00081             G_warning(_("No socket to connect to for monitor <%s>."), name);
00082         return (NO_MON);
00083     }
00084 
00087     _wfd = G_sock_connect(sockpath);
00088     if (_wfd > 0) {             /* success */
00089         _rfd = dup(_wfd);
00090         sync_driver(name);
00091         return (OK);
00092     }
00093 
00094     switch (errno) {
00095     case ECONNREFUSED:
00096     case EADDRINUSE:
00097         if (verbose) {
00098             G_warning(_("Socket is already in use or not accepting connections."));
00099             G_warning(_("Use d.mon to select a monitor"));
00100         }
00101         return (NO_RUN);
00102     case EBADF:
00103     case ENOTSOCK:
00104         if (verbose) {
00105             G_warning(_("Trying to connect to something not a socket."));
00106             G_warning(_("Probably program error."));
00107         }
00108         return (NO_RUN);
00109     case ETIMEDOUT:
00110         if (verbose) {
00111             G_warning(_("Connect attempt timed out."));
00112             G_warning(_("Probably an error with the server."));
00113         }
00114         return (NO_RUN);
00115     default:
00116         break;
00117     }
00118 
00119     if (verbose)
00120         G_warning(_("Connection failed."));
00121 
00122     /* We couldn't connect... */
00123     return (NO_RUN);
00124 }
00125 
00126 #endif /* HAVE_SOCKET */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines