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 <omalloc/omalloc.h>
12 #include <reporter/si_signals.h>
13 
14 #include "tok.h"
15 #include "ipid.h"
16 #include "subexpr.h"
17 #include "links/silink.h"
18 #include "lists.h"
19 #include "pipeLink.h"
20 
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <sys/types.h> /* for portability */
28 #include <sys/select.h>
29 #include <sys/socket.h>
30 
31 typedef struct
32 {
33  FILE *f_read;
34  FILE *f_write;
35  pid_t pid; /* only valid for fork/tcp mode*/
36  int fd_read,fd_write; /* only valid for fork/tcp mode*/
37  char level;
38 } pipeInfo;
39 
40 //**************************************************************************/
41 BOOLEAN pipeOpen(si_link l, short flag, leftv /*u*/)
42 {
43  pipeInfo *d=(pipeInfo*)omAlloc0(sizeof(pipeInfo));
44  if (flag & SI_LINK_OPEN)
45  {
47  }
48  int pc[2];
49  int cp[2];
50  pipe(pc);
51  pipe(cp);
52  pid_t pid=fork();
53  if (pid==0) /*child*/
54  {
55  /* close unnecessary pipe descriptors for a clean environment */
56  si_close(pc[1]); si_close(cp[0]);
57  /* dup pipe read/write to stdin/stdout */
58  si_dup2( pc[0], STDIN_FILENO );
59  si_dup2( cp[1], STDOUT_FILENO );
60  int r=system(l->name);
61  si_close(pc[0]);
62  si_close(cp[1]);
63  exit(r);
64  /* never reached*/
65  }
66  else if (pid>0)
67  {
68  d->pid=pid;
69  si_close(pc[0]); si_close(cp[1]);
70  d->f_read=fdopen(cp[0],"r");
71  d->fd_read=cp[0];
72  d->f_write=fdopen(pc[1],"w");
73  d->fd_write=pc[1];
75  }
76  else
77  {
78  Werror("fork failed (%d)",errno);
79  omFreeSize(d,sizeof(*d));
80  return TRUE;
81  }
82  l->data=d;
83  return FALSE;
84 }
85 
86 //**************************************************************************/
88 {
89  pipeInfo *d = (pipeInfo *)l->data;
90  if (d!=NULL)
91  {
92  if (d->f_read!=NULL) fclose(d->f_read);
93  if (d->f_write!=NULL) fclose(d->f_write);
94  if (d->pid!=0) { kill(d->pid,15); kill(d->pid,9); }
95  }
97  return FALSE;
98 }
99 
100 //**************************************************************************/
102 {
103  if(SI_LINK_OPEN_P(l)) pipeClose(l);
104  pipeInfo *d = (pipeInfo *)l->data;
105  if (d!=NULL)
106  {
107  omFreeSize((ADDRESS)d,(sizeof *d));
108  }
109  l->data=NULL;
110  return FALSE;
111 }
112 
113 //**************************************************************************/
115 {
116  pipeInfo *d = (pipeInfo *)l->data;
117  leftv res=(leftv)omAlloc0(sizeof(sleftv));
118  char *s=(char *)omAlloc0(1024);
119  char *ss=fgets(s,1024,d->f_read);
120  if (ss==NULL) { omFreeSize(s,1024); pipeClose(l);return NULL; }
121  int i=strlen(s)-1;
122  if ((i>=0) && (s[i]=='\n')) s[i]='\0';
123  res->rtyp=STRING_CMD;
124  res->data=s;
125  return res;
126 }
127 //**************************************************************************/
128 extern si_link pipeLastLink;
130 {
132  pipeInfo *d = (pipeInfo *)l->data;
133  FILE *outfile=d->f_write;;
134  BOOLEAN err=FALSE;
135  char *s;
136  pipeLastLink=l;
137  while (data!=NULL)
138  {
139  s = data->String();
140  // free data ??
141  if (s!=NULL)
142  {
143  fprintf(outfile,"%s\n",s);
144  omFree((ADDRESS)s);
145  }
146  else
147  {
148  WerrorS("cannot convert to string");
149  err=TRUE;
150  }
151  if (pipeLastLink==NULL) return TRUE;
152  data = data->next;
153  }
154  fflush(outfile);
156  return err;
157 }
158 
159 const char* slStatusPipe(si_link l, const char* request)
160 {
161  pipeInfo *d=(pipeInfo*)l->data;
162  if (d==NULL) return "not open";
163  if(strcmp(request, "read") == 0)
164  {
165  int s;
166  if ((!SI_LINK_R_OPEN_P(l)) || (feof(d->f_read))) s=0;
167  else
168  {
169  fd_set mask/*, fdmask*/;
170  struct timeval wt;
171  /* Don't block. Return socket status immediately. */
172  wt.tv_sec = 0;
173  wt.tv_usec = 0;
174 
175  FD_ZERO(&mask);
176  FD_SET(d->fd_read, &mask);
177  //Print("test fd %d\n",d->fd_read);
178  /* check with select: chars waiting: no -> not ready */
179  s=si_select(d->fd_read+1, &mask, NULL, NULL, &wt);
180  }
181  switch (s)
182  {
183  case 0: /* not ready */ return "not ready";
184  case -1: /*error*/ return "error";
185  default: /*1: ready ? */return "ready";
186  }
187  }
188  else if (strcmp(request, "write") == 0)
189  {
190  if (SI_LINK_W_OPEN_P(l)) return "ready";
191  return "not ready";
192  }
193  return "unknown status request";
194 }
195 
196 si_link_extension slInitPipeExtension(si_link_extension s)
197 {
198  s->Open=pipeOpen;
199  s->Close=pipeClose;
200  s->Kill=pipeKill;
201  s->Read=pipeRead1;
202  s->Read2=(slRead2Proc)NULL;
203  s->Write=pipeWrite;
204 
205  s->Status=slStatusPipe;
206  s->type="pipe";
207  return s;
208 }
#define STDOUT_FILENO
Definition: feread.cc:45
const CanonicalForm int s
Definition: facAbsFact.cc:55
Class used for (list of) interpreter objects.
Definition: subexpr.h:82
if(0 > strat->sl)
Definition: myNF.cc:73
#define FALSE
Definition: auxiliary.h:94
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define LINKAGE
Definition: mod2.h:142
#define STDIN_FILENO
Definition: fereadl.c:54
#define TRUE
Definition: auxiliary.h:98
void * ADDRESS
Definition: auxiliary.h:115
void WerrorS(const char *s)
Definition: feFopen.cc:24
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..)
Definition: subexpr.cc:751
poly res
Definition: myNF.cc:322
const ring r
Definition: syzextra.cc:208
#define omFree(addr)
Definition: omAllocDecl.h:261
while(1)
Definition: libparse.cc:1442
void system(sys)
int i
Definition: cfEzgcd.cc:123
leftv next
Definition: subexpr.h:86
#define NULL
Definition: omList.c:10
int BOOLEAN
Definition: auxiliary.h:85
void Werror(const char *fmt,...)
Definition: reporter.cc:189
#define omAlloc0(size)
Definition: omAllocDecl.h:211
int l
Definition: cfEzgcd.cc:94