My Project
Toggle main menu visibility
Loading...
Searching...
No Matches
Singular
links
pipeLink.cc
Go to the documentation of this file.
1
/****************************************
2
* Computer Algebra System SINGULAR *
3
****************************************/
4
/***************************************************************
5
* File: pipeLink.h
6
* Purpose: declaration of sl_link routines for pipe
7
***************************************************************/
8
9
#include "
kernel/mod2.h
"
10
11
#include "
reporter/si_signals.h
"
12
13
#include "
tok.h
"
14
#include "
ipid.h
"
15
#include "
subexpr.h
"
16
#include "
links/silink.h
"
17
#include "
lists.h
"
18
#include "
pipeLink.h
"
19
#include "
Singular/feOpt.h
"
20
21
#include <errno.h>
22
#include <sys/types.h>
/* for portability */
23
24
EXTERN_VAR
BOOLEAN
FE_OPT_NO_SHELL_FLAG
;
25
26
typedef
struct
27
{
28
FILE *
f_read
;
29
FILE *
f_write
;
30
pid_t
pid
;
/* only valid for fork/tcp mode*/
31
int
fd_read
,
fd_write
;
/* only valid for fork/tcp mode*/
32
char
level
;
33
}
pipeInfo
;
34
35
//**************************************************************************/
36
static
BOOLEAN
pipeOpen
(
si_link
l
,
short
/*flag*/
,
leftv
/*u*/
)
37
{
38
if
(
FE_OPT_NO_SHELL_FLAG
) {
WerrorS
(
"no links allowed"
);
return
TRUE
;}
39
int
cpus = (long)
feOptValue
(FE_OPT_CPUS);
40
if
(cpus<1)
41
{
42
WerrorS
(
"no sub-processes allowed"
);
43
l
->flags=0;
44
return
TRUE
;
45
}
46
pipeInfo
*d=(
pipeInfo
*)
omAlloc0
(
sizeof
(
pipeInfo
));
47
int
pc[2];
48
int
cp[2];
49
int
err1=pipe(pc);
50
int
err2=pipe(cp);
51
if
(err1 || err2)
52
{
53
Werror
(
"pipe failed with %d\n"
,errno);
54
omFreeSize
(d,
sizeof
(*d));
55
l
->flags=0;
56
return
TRUE
;
57
}
58
/* else */
59
pid_t pid=fork();
60
if
(pid==0)
/*child*/
61
{
62
/* close unnecessary pipe descriptors for a clean environment */
63
si_close(pc[1]); si_close(cp[0]);
64
/* dup pipe read/write to stdin/stdout */
65
si_dup2( pc[0],
STDIN_FILENO
);
66
si_dup2( cp[1],
STDOUT_FILENO
);
67
int
r=system(
l
->name);
68
si_close(pc[0]);
69
si_close(cp[1]);
70
_exit(r);
71
/* never reached*/
72
}
73
else
if
(pid>0)
74
{
75
d->
pid
=pid;
76
si_close(pc[0]); si_close(cp[1]);
77
d->
f_read
=fdopen(cp[0],
"r"
);
78
d->
fd_read
=cp[0];
79
d->
f_write
=fdopen(pc[1],
"w"
);
80
d->
fd_write
=pc[1];
81
SI_LINK_SET_RW_OPEN_P
(
l
);
82
}
83
else
84
{
85
Werror
(
"fork failed (%d)"
,errno);
86
omFreeSize
(d,
sizeof
(*d));
87
l
->flags=0;
88
return
TRUE
;
89
}
90
l
->data=d;
91
return
FALSE
;
92
}
93
94
//**************************************************************************/
95
static
BOOLEAN
pipeClose
(
si_link
l
)
96
{
97
pipeInfo
*d = (
pipeInfo
*)
l
->data;
98
if
(d!=
NULL
)
99
{
100
BOOLEAN
unidirectional=
TRUE
;
101
if
( (d->
f_read
!=
NULL
) && (d->
f_write
!=
NULL
))
102
unidirectional=
FALSE
;
103
104
if
(d->
f_read
!=
NULL
)
105
{
106
fclose(d->
f_read
);
107
d->
f_read
=
NULL
;
108
SI_LINK_SET_CLOSE_P
(
l
);
109
SI_LINK_SET_R_OPEN_P
(
l
);
110
}
111
if
(unidirectional && (d->
f_write
!=
NULL
))
112
{
113
fclose(d->
f_write
);
114
d->
f_write
=
NULL
;
115
SI_LINK_SET_CLOSE_P
(
l
);
116
}
117
if
(unidirectional && (d->
pid
!=0))
118
{ kill(d->
pid
,15); kill(d->
pid
,9); }
119
}
120
else
SI_LINK_SET_CLOSE_P
(
l
);
121
return
FALSE
;
122
}
123
124
//**************************************************************************/
125
static
BOOLEAN
pipeKill
(
si_link
l
)
126
{
127
if
(
SI_LINK_OPEN_P
(
l
))
pipeClose
(
l
);
128
pipeInfo
*d = (
pipeInfo
*)
l
->data;
129
if
(d!=
NULL
)
130
{
131
omFreeSize
((
ADDRESS
)d,(
sizeof
*d));
132
}
133
l
->data=
NULL
;
134
return
FALSE
;
135
}
136
137
//**************************************************************************/
138
static
leftv
pipeRead1
(
si_link
l
)
139
{
140
pipeInfo
*d = (
pipeInfo
*)
l
->data;
141
leftv
res
=(
leftv
)
omAlloc0
(
sizeof
(
sleftv
));
142
char
*
s
=(
char
*)
omAlloc0
(1024);
143
char
*ss=fgets(
s
,1024,d->
f_read
);
144
if
(ss==
NULL
) {
omFreeSize
(
s
,1024);
pipeClose
(
l
);
return
NULL
; }
145
int
i
=strlen(
s
)-1;
146
if
((
i
>=0) && (
s
[
i
]==
'\n'
))
s
[
i
]=
'\0'
;
147
res
->rtyp=
STRING_CMD
;
148
res
->data=
s
;
149
return
res
;
150
}
151
//**************************************************************************/
152
EXTERN_VAR
si_link
pipeLastLink
;
153
static
BOOLEAN
pipeWrite
(
si_link
l
,
leftv
data)
154
{
155
if
(!
SI_LINK_W_OPEN_P
(
l
))
slOpen
(
l
,
SI_LINK_OPEN
|
SI_LINK_WRITE
,
NULL
);
156
pipeInfo
*d = (
pipeInfo
*)
l
->data;
157
FILE *outfile=d->
f_write
;;
158
BOOLEAN
err=
FALSE
;
159
char
*
s
;
160
pipeLastLink
=
l
;
161
while
(data!=
NULL
)
162
{
163
s
= data->
String
();
164
// free data ??
165
if
(
s
!=
NULL
)
166
{
167
fprintf(outfile,
"%s\n"
,
s
);
168
omFree
((
ADDRESS
)
s
);
169
}
170
else
171
{
172
WerrorS
(
"cannot convert to string"
);
173
err=
TRUE
;
174
}
175
if
(
pipeLastLink
==
NULL
)
return
TRUE
;
176
data = data->
next
;
177
}
178
fflush(outfile);
179
pipeLastLink
=
NULL
;
180
return
err;
181
}
182
183
static
const
char
*
slStatusPipe
(
si_link
l
,
const
char
* request)
184
{
185
pipeInfo
*d=(
pipeInfo
*)
l
->data;
186
if
(d==
NULL
)
return
"not open"
;
187
if
(strcmp(request,
"read"
) == 0)
188
{
189
int
s
;
190
if
((!
SI_LINK_R_OPEN_P
(
l
)) || (feof(d->
f_read
)))
s
=0;
191
else
if
(FD_SETSIZE<=d->fd_read)
192
{
193
Werror
(
"file descriptor number too high (%d)"
,d->
fd_read
);
194
s
=-1;
195
}
196
else
197
{
198
fd_set mask
/*, fdmask*/
;
199
struct
timeval wt;
200
/* Don't block. Return socket status immediately. */
201
wt.tv_sec = 0;
202
wt.tv_usec = 0;
203
204
FD_ZERO(&mask);
205
FD_SET(d->
fd_read
, &mask);
206
//Print("test fd %d\n",d->fd_read);
207
/* check with select: chars waiting: no -> not ready */
208
s
=si_select(d->
fd_read
+1, &mask,
NULL
,
NULL
, &wt);
209
}
210
switch
(
s
)
211
{
212
case
0:
/* not ready */
return
"not ready"
;
213
case
-1:
/*error*/
return
"error"
;
214
default
:
/*1: ready ? */
return
"ready"
;
215
}
216
}
217
else
if
(strcmp(request,
"write"
) == 0)
218
{
219
if
(
SI_LINK_W_OPEN_P
(
l
))
return
"ready"
;
220
return
"not ready"
;
221
}
222
return
"unknown status request"
;
223
}
224
225
si_link_extension
slInitPipeExtension
(si_link_extension
s
)
226
{
227
s
->Open=
pipeOpen
;
228
s
->Close=
pipeClose
;
229
s
->Kill=
pipeKill
;
230
s
->Read=
pipeRead1
;
231
s
->Read2=(
slRead2Proc
)
NULL
;
232
s
->Write=
pipeWrite
;
233
234
s
->Status=
slStatusPipe
;
235
s
->type=
"pipe"
;
236
return
s
;
237
}
BOOLEAN
int BOOLEAN
Definition
auxiliary.h:88
TRUE
#define TRUE
Definition
auxiliary.h:101
FALSE
#define FALSE
Definition
auxiliary.h:97
ADDRESS
void * ADDRESS
Definition
auxiliary.h:120
l
int l
Definition
cfEzgcd.cc:100
i
int i
Definition
cfEzgcd.cc:132
sleftv
Class used for (list of) interpreter objects.
Definition
subexpr.h:83
sleftv::next
leftv next
Definition
subexpr.h:86
sleftv::String
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..).
Definition
subexpr.cc:765
pipeLastLink
VAR si_link pipeLastLink
Definition
cntrlc.cc:61
FE_OPT_NO_SHELL_FLAG
EXTERN_VAR BOOLEAN FE_OPT_NO_SHELL_FLAG
Definition
extra.cc:166
s
const CanonicalForm int s
Definition
facAbsFact.cc:51
res
CanonicalForm res
Definition
facAbsFact.cc:60
WerrorS
void WerrorS(const char *s)
Definition
feFopen.cc:24
feOpt.h
feOptValue
static void * feOptValue(feOptIndex opt)
Definition
feOpt.h:40
STDOUT_FILENO
#define STDOUT_FILENO
Definition
feread.cc:43
STDIN_FILENO
#define STDIN_FILENO
Definition
fereadl.c:52
EXTERN_VAR
#define EXTERN_VAR
Definition
globaldefs.h:6
ipid.h
lists.h
mod2.h
omFreeSize
#define omFreeSize(addr, size)
Definition
omAllocDecl.h:260
omFree
#define omFree(addr)
Definition
omAllocDecl.h:261
omAlloc0
#define omAlloc0(size)
Definition
omAllocDecl.h:211
NULL
#define NULL
Definition
omList.c:12
pipeInfo::f_read
FILE * f_read
Definition
pipeLink.cc:28
pipeInfo::f_write
FILE * f_write
Definition
pipeLink.cc:29
slStatusPipe
static const char * slStatusPipe(si_link l, const char *request)
Definition
pipeLink.cc:183
slInitPipeExtension
si_link_extension slInitPipeExtension(si_link_extension s)
Definition
pipeLink.cc:225
pipeOpen
static BOOLEAN pipeOpen(si_link l, short, leftv)
Definition
pipeLink.cc:36
pipeInfo::fd_write
int fd_write
Definition
pipeLink.cc:31
pipeInfo::level
char level
Definition
pipeLink.cc:32
pipeKill
static BOOLEAN pipeKill(si_link l)
Definition
pipeLink.cc:125
pipeRead1
static leftv pipeRead1(si_link l)
Definition
pipeLink.cc:138
pipeInfo::pid
pid_t pid
Definition
pipeLink.cc:30
pipeInfo::fd_read
int fd_read
Definition
pipeLink.cc:31
pipeClose
static BOOLEAN pipeClose(si_link l)
Definition
pipeLink.cc:95
pipeWrite
static BOOLEAN pipeWrite(si_link l, leftv data)
Definition
pipeLink.cc:153
pipeInfo
Definition
pipeLink.cc:27
pipeLink.h
Werror
void Werror(const char *fmt,...)
Definition
reporter.cc:189
si_signals.h
slOpen
BOOLEAN slOpen(si_link l, short flag, leftv h)
Definition
silink.cc:194
silink.h
SI_LINK_WRITE
#define SI_LINK_WRITE
Definition
silink.h:67
SI_LINK_W_OPEN_P
#define SI_LINK_W_OPEN_P(l)
Definition
silink.h:72
slRead2Proc
leftv(* slRead2Proc)(si_link l, leftv a)
Definition
silink.h:30
SI_LINK_SET_R_OPEN_P
#define SI_LINK_SET_R_OPEN_P(l)
Definition
silink.h:79
si_link
ip_link * si_link
Definition
silink.h:20
SI_LINK_SET_CLOSE_P
#define SI_LINK_SET_CLOSE_P(l)
Definition
silink.h:76
SI_LINK_OPEN
#define SI_LINK_OPEN
Definition
silink.h:65
SI_LINK_R_OPEN_P
#define SI_LINK_R_OPEN_P(l)
Definition
silink.h:73
SI_LINK_SET_RW_OPEN_P
#define SI_LINK_SET_RW_OPEN_P(l)
Definition
silink.h:80
SI_LINK_OPEN_P
#define SI_LINK_OPEN_P(l)
Definition
silink.h:71
leftv
sleftv * leftv
Definition
structs.h:53
subexpr.h
tok.h
STRING_CMD
@ STRING_CMD
Definition
tok.h:187
Generated on
for My Project by
doxygen 1.17.0
for
Singular