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 #ifndef __MINGW32__
00028
00029 #include <grass/gis.h>
00030 #include <grass/version.h>
00031 #include <stdio.h>
00032 #include <stddef.h>
00033 #include <stdlib.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #include <unistd.h>
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <sys/socket.h>
00040 #include <sys/un.h>
00041
00045 #ifndef AF_LOCAL
00046 #define AF_LOCAL AF_UNIX
00047 #endif
00048 #ifndef PF_LOCAL
00049 #define PF_LOCAL PF_UNIX
00050 #endif
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 static char *
00061 _get_make_sock_path (void)
00062 {
00063 char *path, *user, *lock;
00064 const char *prefix = "/tmp/grass6";
00065 int len, status;
00066 struct stat theStat;
00067
00068 user = G_whoami();
00069 if (user == NULL)
00070 return NULL;
00071 else if (user[0] == '?')
00072 {
00073 return NULL;
00074 }
00075
00076 if ( (lock = getenv ( "GIS_LOCK" )) == NULL )
00077 G_fatal_error ("Cannot get GIS_LOCK enviroment variable value");
00078
00079 len = strlen(prefix) + strlen(user) + strlen(GRASS_VERSION_MAJOR) + strlen(GRASS_VERSION_MINOR) + strlen(lock) + 3;
00080 path = G_malloc (len);
00081
00082 sprintf (path, "%s-%s-%s", prefix, user, lock);
00083
00084 if ((status = lstat (path, &theStat)) != 0)
00085 {
00086 status = mkdir (path, S_IRWXU);
00087 }
00088 else
00089 {
00090 if (!S_ISDIR (theStat.st_mode))
00091 {
00092 status = -1;
00093 }
00094 else
00095 {
00096 status = chmod (path, S_IRWXU);
00097 }
00098 }
00099
00100 if (status)
00101 {
00102 G_free (path);
00103 path = NULL;
00104 }
00105
00106 return path;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115 char *
00116 G_sock_get_fname (const char *name)
00117 {
00118 char *path, *dirpath;
00119 int len;
00120
00121 if (name == NULL)
00122 return NULL;
00123
00124 dirpath = _get_make_sock_path();
00125
00126 if (dirpath == NULL)
00127 return NULL;
00128
00129 len = strlen (dirpath) + strlen(name) + 2;
00130 path = G_malloc (len);
00131 sprintf (path, "%s/%s", dirpath, name);
00132 G_free (dirpath);
00133
00134 return path;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143 int
00144 G_sock_exists (const char *name)
00145 {
00146 struct stat theStat;
00147
00148 if (name == NULL || stat (name, &theStat) != 0)
00149 return 0;
00150
00151 if (S_ISSOCK (theStat.st_mode))
00152 return 1;
00153 else
00154 return 0;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 int
00166 G_sock_bind (const char *name)
00167 {
00168 int sockfd;
00169 size_t size;
00170 struct sockaddr_un addr;
00171
00172 if (name == NULL)
00173 return -1;
00174
00175
00176
00177
00178
00179 if (G_sock_exists (name))
00180 {
00181 errno = EADDRINUSE;
00182 return -1;
00183 }
00184
00185
00186 memset (&addr, 0, sizeof(addr));
00187
00188
00189 if (sizeof (addr.sun_path) < strlen(name) + 1)
00190 return -1;
00191
00192 strncpy (addr.sun_path, name, sizeof (addr.sun_path) - 1);
00193
00194 addr.sun_family = AF_LOCAL;
00195
00196 sockfd = socket (PF_LOCAL, SOCK_STREAM, 0);
00197
00198 size = (offsetof (struct sockaddr_un, sun_path)
00199 + strlen (addr.sun_path) + 1);
00200
00201 if (bind (sockfd, (struct sockaddr *) &addr, size) != 0)
00202 return -1;
00203
00204 return sockfd;
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 int
00214 G_sock_listen (int sockfd, unsigned int queue_len)
00215 {
00216 return listen (sockfd, queue_len);
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 int
00229 G_sock_accept (int sockfd)
00230 {
00231 struct sockaddr_un addr;
00232 int len = sizeof(addr);
00233 return accept (sockfd, (struct sockaddr *) &addr, &len);
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 int
00246 G_sock_connect (const char *name)
00247 {
00248 int sockfd;
00249 struct sockaddr_un addr;
00250
00251 if (!G_sock_exists (name))
00252 return -1;
00253
00254
00255 memset (&addr, 0, sizeof(addr));
00256
00257
00258 if (sizeof (addr.sun_path) < strlen(name) + 1)
00259 return -1;
00260
00261 strncpy (addr.sun_path, name, sizeof (addr.sun_path) - 1);
00262
00263 addr.sun_family = AF_LOCAL;
00264
00265 sockfd = socket (PF_LOCAL, SOCK_STREAM, 0);
00266
00267 if (connect (sockfd, (struct sockaddr *) &addr, sizeof (addr)) != 0)
00268 return -1;
00269 else
00270 return sockfd;
00271 }
00272
00273
00274
00275 int
00276 G_sock_socketpair(int family, int type, int protocol, int *fd)
00277 {
00278 int n;
00279
00280 if ( (n = socketpair(family, type, protocol, fd)) < 0)
00281 return -1;
00282 else
00283 return 0;
00284 }
00285
00286 #endif