GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
io_sock.c
Go to the documentation of this file.
1 
2 #include <grass/config.h>
3 
4 #ifdef HAVE_SOCKET
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <errno.h>
12 
13 #ifdef __MINGW32__
14 #include <winsock2.h>
15 #define ECONNREFUSED WSAECONNREFUSED
16 #define EADDRINUSE WSAEADDRINUSE
17 #define ENOTSOCK WSAENOTSOCK
18 #define ETIMEDOUT WSAETIMEDOUT
19 #endif
20 
21 #include <grass/gis.h>
22 #include <grass/glocale.h>
23 #include <grass/raster.h>
24 
25 #include "open.h"
26 
27 extern int _rfd, _wfd;
28 extern int _quiet;
29 
30 extern int sync_driver(char *);
31 
32 static char *sockpath;
33 
49 int REM_open_driver(void)
50 {
51  int verbose;
52  char *name;
53 
54  verbose = !_quiet;
55  _quiet = 0;
56 
57  name = getenv("MONITOR_OVERRIDE");
58  if (!name)
59  name = G__getenv("MONITOR");
60 
61  if (!name) {
62  if (verbose) {
63  G_warning(_("No graphics monitor has been selected for output."));
64  G_warning(_("Please run \"d.mon\" to select a graphics monitor."));
65  }
66  return (NO_MON);
67  }
68 
69  /* Get the full path to the unix socket */
70  if ((sockpath = G_sock_get_fname(name)) == NULL) {
71  if (verbose)
72  G_warning(_("Failed to get socket name for monitor <%s>."), name);
73  return (NO_MON);
74  }
75 
76  /* See if the socket exists, if it doesn't no point in trying to
77  * connect to it.
78  */
79  if (!G_sock_exists(sockpath)) {
80  if (verbose)
81  G_warning(_("No socket to connect to for monitor <%s>."), name);
82  return (NO_MON);
83  }
84 
87  _wfd = G_sock_connect(sockpath);
88  if (_wfd > 0) { /* success */
89  _rfd = dup(_wfd);
90  sync_driver(name);
91  return (OK);
92  }
93 
94  switch (errno) {
95  case ECONNREFUSED:
96  case EADDRINUSE:
97  if (verbose) {
98  G_warning(_("Socket is already in use or not accepting connections."));
99  G_warning(_("Use d.mon to select a monitor"));
100  }
101  return (NO_RUN);
102  case EBADF:
103  case ENOTSOCK:
104  if (verbose) {
105  G_warning(_("Trying to connect to something not a socket."));
106  G_warning(_("Probably program error."));
107  }
108  return (NO_RUN);
109  case ETIMEDOUT:
110  if (verbose) {
111  G_warning(_("Connect attempt timed out."));
112  G_warning(_("Probably an error with the server."));
113  }
114  return (NO_RUN);
115  default:
116  break;
117  }
118 
119  if (verbose)
120  G_warning(_("Connection failed."));
121 
122  /* We couldn't connect... */
123  return (NO_RUN);
124 }
125 
126 #endif /* HAVE_SOCKET */
char * G__getenv(const char *name)
Get environment variable.
Definition: env.c:312
string name
Definition: render.py:1314
#define NO_MON
Definition: open.h:13
char * getenv()
#define OK
Definition: plot.c:384
int REM_open_driver(void)
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int errno
def verbose
Display a verbose message using g.message -v
Definition: core.py:332
#define NO_RUN
Definition: open.h:11