30 #include <grass/config.h>
40 #include <sys/types.h>
46 #define EADDRINUSE WSAEADDRINUSE
48 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #define INVALID_SOCKET (-1)
54 #include <grass/gis.h>
55 #include <grass/version.h>
56 #include <grass/glocale.h>
62 static char *_get_make_sock_path(
void);
64 static void init_sockets(
void)
75 WSAStartup(0x0001, &wsadata);
87 static char *_get_make_sock_path(
void)
89 char *path, *user, *lock;
90 const char *prefix =
"/tmp/grass6";
97 else if (user[0] ==
'?') {
102 G_fatal_error(_(
"Unable to get GIS_LOCK environment variable value"));
104 len = strlen(prefix) + strlen(user) + strlen(lock) + 3;
105 path = G_malloc(len);
107 sprintf(path,
"%s-%s-%s", prefix, user, lock);
109 if ((status =
G_lstat(path, &theStat)) != 0) {
113 if (!S_ISDIR(theStat.st_mode)) {
117 status = chmod(path, S_IRWXU);
131 #define PROTO PF_INET
132 typedef struct sockaddr_in sockaddr_t;
134 static int set_port(
const char *
name,
int port)
136 FILE *fp = fopen(name,
"w");
141 fprintf(fp,
"%d\n", port);
148 static int get_port(
const char *name)
150 FILE *fp = fopen(name,
"r");
156 if (fscanf(fp,
"%d", &port) != 1)
164 static int save_port(
int sockfd,
const char *name)
167 socklen_t
size =
sizeof(addr);
169 if (getsockname(sockfd, (
struct sockaddr *)&addr, &size) != 0)
172 if (set_port(name, ntohs(addr.sin_port)) < 0)
178 static int make_address(sockaddr_t * addr,
const char *name,
int exists)
180 int port = exists ? get_port(name) : 0;
185 addr->sin_family = AF_INET;
186 addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
187 addr->sin_port = htons((
unsigned short)port);
194 #define PROTO PF_UNIX
195 typedef struct sockaddr_un sockaddr_t;
197 static int make_address(sockaddr_t * addr,
const char *name,
int exists)
199 addr->sun_family = AF_UNIX;
202 if (
sizeof(addr->sun_path) < strlen(name) + 1)
205 strncpy(addr->sun_path, name,
sizeof(addr->sun_path) - 1);
223 char *G_sock_get_fname(
const char *name)
225 char *path, *dirpath;
231 dirpath = _get_make_sock_path();
236 len = strlen(dirpath) + strlen(name) + 2;
237 path = G_malloc(len);
238 sprintf(path,
"%s/%s", dirpath, name);
253 int G_sock_exists(
const char *name)
257 if (name ==
NULL ||
stat(name, &theStat) != 0)
261 if (S_ISREG(theStat.st_mode))
263 if (S_ISSOCK(theStat.st_mode))
282 int G_sock_bind(
const char *name)
297 if (G_sock_exists(name)) {
303 memset(&addr, 0,
sizeof(addr));
307 if (make_address(&addr, name, 0) < 0)
310 sockfd = socket(PROTO, SOCK_STREAM, 0);
311 if (sockfd == INVALID_SOCKET)
314 if (
bind(sockfd, (
const struct sockaddr *)&addr, size) != 0)
318 if (save_port(sockfd, name) < 0)
334 int G_sock_listen(
int sockfd,
unsigned int queue_len)
336 return listen(sockfd, queue_len);
351 int G_sock_accept(
int sockfd)
354 socklen_t len =
sizeof(addr);
356 return accept(sockfd, (
struct sockaddr *)&addr, &len);
368 int G_sock_connect(
const char *name)
375 if (!G_sock_exists(name))
379 memset(&addr, 0,
sizeof(addr));
381 if (make_address(&addr, name, 1) < 0)
384 sockfd = socket(PROTO, SOCK_STREAM, 0);
385 if (sockfd == INVALID_SOCKET)
388 if (connect(sockfd, (
struct sockaddr *)&addr,
sizeof(addr)) != 0)
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
int G_mkdir(const char *path)
Creates a new directory.
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
char * G_whoami(void)
Gets user's name.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.