GRASS 8 Programmer's Manual
8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
win32_pipes.c
Go to the documentation of this file.
1
/*****************************************************************************
2
*
3
* LIBRARY: unix_socks.c -- Routines related to using UNIX domain
4
* sockets for IPC mechanisms (such as XDRIVER).
5
*
6
* AUTHOR(S): Eric G. Miller
7
*
8
* PURPOSE: Historically GRASS has used FIFO for interprocess communic-
9
* ations for display functions. Unfortunately, FIFO's are
10
* not available on all target platforms. An attempt has been
11
* made to use IPC message passing, but the semantics are
12
* variable and it also isn't available on all target platforms.
13
* UNIX sockets, or local or domain sockets, are much more
14
* widely available and consistent. NOTE: This implementation
15
* of UNIX sockets provides zero security checking so should
16
* not be used from untrusted clients.
17
*
18
* SPDX-FileCopyrightText: 2000 GRASS Development Team
19
* SPDX-License-Identifier: GPL-2.0-or-later
20
*
21
*****************************************************************************/
22
23
#ifndef __MINGW32__
/* TODO */
24
#ifdef __MINGW32__
25
26
#include <
grass/gis.h
>
27
#include <windows.h>
28
#include <io.h>
29
#include <errno.h>
30
#include <
string.h
>
31
#include <
stdio.h
>
32
#include <stddef.h>
33
#include <
stdlib.h
>
34
#include <
sys/stat.h
>
35
36
#define PIPE_TIMEOUT 5000
37
#define BUFSIZE 2048
38
39
/* ---------------------------------------------------------------------
40
* _get_make_pipe_path(), builds and tests the path for the socket
41
* directory. Returns NULL on any failure, otherwise it returns the
42
* directory path. The path will be like "/tmp/grass-$USER".
43
* ---------------------------------------------------------------------*/
44
static
char
*
_get_make_pipe_path
(
void
)
45
{
46
char
*
path
, *user;
47
const
char
*
prefix
=
"c:/grass-"
;
48
char
*
whoami
=
"mingw-anon-user"
;
49
int
len, status;
50
struct
_stat
theStat
;
51
52
user =
G_whoami
();
/* Don't G_free () return value ever! */
53
if
(user ==
NULL
) {
54
user =
whoami
;
55
}
56
len =
strlen
(
prefix
) +
strlen
(user) + 1;
57
path
=
G_malloc
(len);
58
snprintf
(
path
, len,
"%s%s"
,
prefix
, user);
59
60
if
((status =
G_lstat
(
path
, &
theStat
)) != 0) {
61
status =
G_mkdir
(
path
);
62
}
63
else
{
64
if
(!
S_ISDIR
(
theStat
.st_mode)) {
65
status = -1;
/* not a directory ?? */
66
}
67
else
{
68
status =
chmod
(
path
,
S_IRWXU
);
/* fails if we don't own it */
69
}
70
}
71
72
if
(status) {
/* something's wrong if non-zero */
73
G_free
(
path
);
74
path
=
NULL
;
75
}
76
77
return
path
;
78
}
79
80
/* ----------------------------------------------------------------------
81
* G_pipe_get_fname(), builds the full path for a UNIX socket. Caller
82
* should G_free () the return value when it is no longer needed. Returns
83
* NULL on failure.
84
* ---------------------------------------------------------------------*/
85
char
*
G_pipe_get_fname
(
char
*
name
)
86
{
87
char
*
path
, *
dirpath
;
88
int
len;
89
90
if
(
name
==
NULL
)
91
return
NULL
;
92
93
dirpath
=
_get_make_pipe_path
();
94
95
if
(
dirpath
==
NULL
)
96
return
NULL
;
97
98
len =
strlen
(
dirpath
) +
strlen
(
name
) + 2;
99
path
=
G_malloc
(len);
100
snprintf
(
path
, len,
"%s/%s"
,
dirpath
,
name
);
101
G_free
(
dirpath
);
102
103
return
path
;
104
}
105
106
/* -------------------------------------------------------------------
107
* G_pipe_exists(char *): Returns 1 if path is to a UNIX socket that
108
* already exists, 0 otherwise.
109
* -------------------------------------------------------------------*/
110
111
int
G_pipe_exists
(
char
*
name
)
112
{
113
int
rv
= 0;
114
HANDLE
hFile
=
hFile
=
115
CreateFile
(
name
,
GENERIC_READ
,
FILE_SHARE_READ
,
NULL
,
OPEN_EXISTING
,
116
FILE_ATTRIBUTE_NORMAL
,
NULL
);
117
118
if
(
hFile
!=
INVALID_HANDLE_VALUE
) {
119
if
(
name
==
NULL
|| (
FILE_TYPE_PIPE
!=
GetFileType
(
hFile
))) {
120
rv
= 0;
121
}
122
else
{
123
rv
= 1;
124
CloseFile
(
hFile
);
125
}
126
}
127
return
(
rv
);
128
}
129
130
/* -----------------------------------------------------------------
131
* G_pipe_bind (char *): Takes the full pathname for a UNIX socket
132
* and returns the file descriptor to the socket after a successful
133
* call to bind(). On error, it returns -1. Check "errno" if you
134
* want to find out why this failed (clear it before the call).
135
* ----------------------------------------------------------------*/
136
137
HANDLE
G_pipe_bind
(
char
*
name
)
138
{
139
HANDLE
hPipe
;
140
141
if
(
name
==
NULL
) {
142
return
-1;
143
}
144
if
(
G_pipe_exists
(
name
)) {
145
/*errno = EADDRINUSE; */
146
return
-1;
147
}
148
149
hPipe
=
CreateNamedPipe
(
name
,
// pipe name
150
PIPE_ACCESS_DUPLEX
,
// read/write access
151
PIPE_TYPE_MESSAGE
|
// message type pipe
152
PIPE_READMODE_MESSAGE
|
// message-read mode
153
PIPE_WAIT
,
// blocking mode
154
PIPE_UNLIMITED_INSTANCES
,
// max. instances
155
BUFSIZE
,
// output buffer size
156
BUFSIZE
,
// input buffer size
157
PIPE_TIMEOUT
,
// client time-out
158
NULL
);
// no security attribute
159
160
if
(
hPipe
==
INVALID_HANDLE_VALUE
) {
161
return
(-1);
162
}
163
return
(
hPipe
);
164
}
165
166
/* ---------------------------------------------------------------------
167
* G_pipe_listen(int, unsigned int): Wrapper around the listen()
168
* function.
169
* --------------------------------------------------------------------*/
170
171
int
G_pipe_listen
(
HANDLE
hPipe
,
unsigned
int
queue_len
)
172
{
173
return
(0);
174
}
175
176
/* -----------------------------------------------------------------------
177
* G_pipe_accept (int sockfd):
178
* Wrapper around the accept() function. No client info is returned, but
179
* that's not generally useful for local sockets anyway. Function returns
180
* the file descriptor or an error code generated by accept(). Note,
181
* this call will usually block until a connection arrives. You can use
182
* select() for a time out on the call.
183
* ---------------------------------------------------------------------*/
184
185
HANDLE
G_pipe_accept
(
HANDLE
hPipe
)
186
{
187
BOOL
fConnected
;
188
HANDLE
rv
=
hPipe
;
189
190
fConnected
=
ConnectNamedPipe
(
hPipe
,
NULL
)
191
?
TRUE
192
: (
GetLastError
() ==
ERROR_PIPE_CONNECTED
);
193
if
(
fConnected
) {
194
rv
=
NULL
;
195
}
196
return
(
rv
);
197
}
198
199
/* ----------------------------------------------------------------------
200
* G_pipe_connect (char *name): Tries to connect to the unix socket
201
* specified by "name". Returns the file descriptor if successful, or
202
* -1 if unsuccessful. Global errno is set by connect() if return is -1
203
* (though you should zero errno first, since this function doesn't set
204
* it for a couple conditions).
205
* --------------------------------------------------------------------*/
206
207
HANDLE
G_pipe_connect
(
char
*
name
)
208
{
209
HANDLE
hPipe
= -1;
210
211
if
(!
G_pipe_exists
(
name
)) {
212
return
hPipe
;
213
}
214
215
while
(1) {
216
hPipe
=
CreateFile
(
name
,
// pipe name
217
GENERIC_READ
|
// read and write access
218
GENERIC_WRITE
,
219
0,
// no sharing
220
NULL
,
// no security attributes
221
OPEN_EXISTING
,
// opens existing pipe
222
0,
// default attributes
223
NULL
);
// no template file
224
225
if
(
hPipe
!=
INVALID_HANDLE_VALUE
) {
226
break
;
227
}
228
if
(
GetLastError
() !=
ERROR_PIPE_BUSY
) {
229
return
(-1);
230
}
231
/* Wait for 5 seconds */
232
if
(!
WaitNamedPipe
(
name
,
PIPE_TIMEOUT
)) {
233
return
(-1);
234
}
235
}
236
return
(
hPipe
);
237
}
238
239
#endif
/* __MINGW32__ */
240
extern
int
dummy_for_iso_compilers
;
/* suppress -Wempty-translation-unit */
241
#endif
/* __MINGW32__ */
NULL
#define NULL
Definition
ccmath.h:32
AMI_STREAM
Definition
ami_stream.h:153
AMI_STREAM::AMI_STREAM
AMI_STREAM()
Definition
ami_stream.h:227
UntypedStream::path
char path[BUFSIZ]
Definition
ami_stream.h:127
G_free
void G_free(void *)
Free allocated memory.
Definition
gis/alloc.c:145
G_malloc
#define G_malloc(n)
Definition
defs/gis.h:136
G_lstat
int G_lstat(const char *, struct stat *)
Get file status.
Definition
paths.c:145
G_whoami
const char * G_whoami(void)
Gets user's name.
Definition
gis/whoami.c:32
G_mkdir
int G_mkdir(const char *)
Creates a new directory.
Definition
paths.c:27
gis.h
TRUE
#define TRUE
Definition
gis.h:75
name
const char * name
Definition
named_colr.c:6
stat.h
S_ISDIR
#define S_ISDIR(mode)
Definition
stat.h:6
stdio.h
stdlib.h
string.h
path
Definition
path.h:15
dummy_for_iso_compilers
int dummy_for_iso_compilers
lib
gis
win32_pipes.c
Generated on Thu Sep 10 2026 06:57:37 for GRASS 8 Programmer's Manual by
1.9.8