Index: Singular/cntrlc.cc
===================================================================
--- Singular/cntrlc.cc	(revision 01c0298072151ffa06aed1c49b5bdcc5d0c29690)
+++ Singular/cntrlc.cc	(revision b60bfe549e97ab070e151414080fbef95a165146)
@@ -10,5 +10,4 @@
 #define _XOPEN_SOURCE_EXTENDED
 #endif /* MP3-Y2 0.022UF */
-
 #include <stdio.h>
 #include <stddef.h>
@@ -22,22 +21,20 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
+
 #include <kernel/mod2.h>
 #include <omalloc/omalloc.h>
-
-#include <kernel/polys.h>
+#include <Singular/tok.h>
+#include <Singular/ipshell.h>
 #include <kernel/febase.h>
 void sig_chld_hdl(int sig); /*#include <Singular/links/ssiLink.h>*/
-//#include <Singular/feOpt.h>
-//#include <Singular/silink.h>
-//#include <Singular/ssiLink.h>
+#include <Singular/cntrlc.h>
+#include <Singular/feOpt.h>
 #include <Singular/si_signals.h>
-
-#include "tok.h"
-#include "ipshell.h"
-#include "cntrlc.h"
-#include "feOpt.h"
-#include "links/silink.h"
+#include <Singular/links/silink.h>
+#include <Singular/links/ssiLink.h>
 
 /* undef, if you don't want GDB to come up on error */
+
+#define CALL_GDB
 
 #if defined(__OPTIMIZE__) && defined(CALL_GDB)
@@ -93,11 +90,16 @@
 void sig_term_hdl(int /*sig*/)
 {
- while (ssiToBeClosed!=NULL)
- {
-   slClose(ssiToBeClosed->l);
-   if (ssiToBeClosed==NULL) break;
-   ssiToBeClosed=(link_list)ssiToBeClosed->next;
- }
- exit(1);
+  if (ssiToBeClosed_inactive)
+  {
+    ssiToBeClosed_inactive=FALSE;
+    while (ssiToBeClosed!=NULL)
+    {
+      slClose(ssiToBeClosed->l);
+      if (ssiToBeClosed==NULL) break;
+      ssiToBeClosed=(link_list)ssiToBeClosed->next;
+    }
+    exit(1);
+  }
+  //else: we already shutting down: let's do m2_end ist work
 }
 
@@ -121,5 +123,5 @@
  *
  *---------------------------------------------------------------------*/
-void sigint_handler(int sig);
+void sigint_handler(int /*sig*/);
 
 si_hdl_typ si_set_signal ( int sig, si_hdl_typ signal_handler);
@@ -283,4 +285,5 @@
   exit(0);
 }
+
 #else
 
@@ -310,5 +313,4 @@
   #endif /* __OPTIMIZE__ */
   #if defined(unix)
-  /* debug(..) does not work under HPUX (because ptrace does not work..) */
   #ifdef CALL_GDB
   if (sig!=SIGINT) debug(STACK_TRACE);
@@ -318,4 +320,5 @@
 }
 #endif
+
 
 /*2
Index: Singular/links/s_buff.cc
===================================================================
--- Singular/links/s_buff.cc	(revision 01c0298072151ffa06aed1c49b5bdcc5d0c29690)
+++ Singular/links/s_buff.cc	(revision b60bfe549e97ab070e151414080fbef95a165146)
@@ -7,4 +7,5 @@
 #include <signal.h>
 
+#include <kernel/mod2.h>
 #include <coeffs/si_gmp.h>
 
@@ -12,4 +13,5 @@
 #include <Singular/links/s_buff.h>
 #include <Singular/si_signals.h>
+
 //struct s_buff_s
 //{
@@ -25,17 +27,9 @@
 #define S_BUFF_LEN 4096
 
-sigset_t ssi_sigmask; // set in ssiLink.cc
-sigset_t ssi_oldmask; // set in ssiLink.cc
-
-#define SSI_BLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_sigmask, &ssi_oldmask)
-#define SSI_UNBLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_oldmask, NULL)
-
 s_buff s_open(int fd)
 {
-  SSI_BLOCK_CHLD;
   s_buff F=(s_buff)omAlloc0(sizeof(*F));
   F->fd=fd;
   F->buff=(char*)omAlloc(S_BUFF_LEN);
-  SSI_UNBLOCK_CHLD;
   return F;
 }
@@ -43,20 +37,24 @@
 s_buff s_open_by_name(const char *n)
 {
-  SSI_BLOCK_CHLD;
   int fd=si_open(n,O_RDONLY);
-  SSI_UNBLOCK_CHLD;
   return s_open(fd);
 }
 
-int    s_close(s_buff &F)
+int    s_free(s_buff &F)
 {
   if (F!=NULL)
   {
-    SSI_BLOCK_CHLD;
     omFreeSize(F->buff,S_BUFF_LEN);
-    int r=si_close(F->fd);
     omFreeSize(F,sizeof(*F));
     F=NULL;
-    SSI_UNBLOCK_CHLD;
+  }
+  return 0;
+}
+
+int    s_close(s_buff &F)
+{
+  if (F!=NULL)
+  {
+    int r=close(F->fd);
     return r;
   }
@@ -74,7 +72,5 @@
   {
     memset(F->buff,0,S_BUFF_LEN); /*debug*/
-    SSI_BLOCK_CHLD;
     int r=si_read(F->fd,F->buff,S_BUFF_LEN);
-    SSI_UNBLOCK_CHLD;
     if (r<=0)
     {
Index: Singular/links/s_buff.h
===================================================================
--- Singular/links/s_buff.h	(revision 01c0298072151ffa06aed1c49b5bdcc5d0c29690)
+++ Singular/links/s_buff.h	(revision b60bfe549e97ab070e151414080fbef95a165146)
@@ -17,4 +17,5 @@
 s_buff s_open(int fd);
 s_buff s_open_by_name(const char *n);
+int    s_free(s_buff &f);
 int    s_close(s_buff &f);
 
Index: Singular/links/silink.h
===================================================================
--- Singular/links/silink.h	(revision 01c0298072151ffa06aed1c49b5bdcc5d0c29690)
+++ Singular/links/silink.h	(revision b60bfe549e97ab070e151414080fbef95a165146)
@@ -125,4 +125,4 @@
 
 extern link_list ssiToBeClosed;
-extern BOOLEAN ssiToBeClosed_inactive;
+extern volatile BOOLEAN ssiToBeClosed_inactive;
 #endif // SILINK_H
Index: Singular/links/ssiLink.cc
===================================================================
--- Singular/links/ssiLink.cc	(revision 01c0298072151ffa06aed1c49b5bdcc5d0c29690)
+++ Singular/links/ssiLink.cc	(revision b60bfe549e97ab070e151414080fbef95a165146)
@@ -21,60 +21,38 @@
 #include <time.h>
 
-
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif /* HAVE_CONFIG_H */
 #include <kernel/mod2.h>
 #include <Singular/si_signals.h>
-
+// #include "mod2.h"
+
+#include <Singular/tok.h>
+#include <Singular/ipid.h>
+#include <Singular/ipshell.h>
 #include <omalloc/omalloc.h>
-
-#include <misc/intvec.h>
-#include <misc/options.h>
-
-#include <polys/monomials/ring.h>
-#include <polys/matpol.h>
-
-#include <coeffs/bigintmat.h>
-
-#include <kernel/ideals.h>
-#include <kernel/polys.h>
-#include <kernel/longrat.h>
-#include <kernel/ideals.h>
+#include <libpolys/polys/monomials/ring.h>
+#include <libpolys/polys/matpol.h>
+#include <libpolys/polys/simpleideals.h>
+#include <libpolys/polys/monomials/p_polys.h>
+#include <libpolys/coeffs/longrat.h>
+#include <libpolys/misc/intvec.h>
+#include <libpolys/coeffs/bigintmat.h>
+#include <libpolys/misc/options.h>
 #include <kernel/timer.h>
-
-#include <Singular/tok.h>
-#include <Singular/ipshell.h>
-#include <Singular/ipid.h>
-
 #include <Singular/subexpr.h>
+#include <Singular/links/silink.h>
 #include <Singular/cntrlc.h>
 #include <Singular/lists.h>
 #include <Singular/blackbox.h>
-
-#include <Singular/links/silink.h>
 #include <Singular/links/s_buff.h>
 #include <Singular/links/ssiLink.h>
 
-// TODO: use number n_InitMPZ(mpz_t n, coeffs_BIGINT) instead!?
-
-struct snumber_dummy
-{
-  mpz_t z;
-  mpz_t n;
-  #if defined(LDEBUG)
-  int debug;
-  #endif
-  BOOLEAN s;
-};
-typedef struct snumber_dummy  *number_dummy;
+#ifdef HAVE_MPSR
+#include <Singular/mpsr.h>
+#endif
 
 #ifdef HAVE_SIMPLEIPC
 #include <Singular/links/simpleipc.h>
 #endif
-//#if (_POSIX_C_SOURCE >= 200112L) || (_XOPEN_SOURCE >= 600)
-//#define HAVE_PSELECT
-//#endif
+
+#include <Singular/si_signals.h>
 
 #define SSI_VERSION 5
@@ -106,11 +84,5 @@
 
 link_list ssiToBeClosed=NULL;
-BOOLEAN ssiToBeClosed_inactive=TRUE;
-#define SSI_BLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_sigmask, &ssi_oldmask)
-#define SSI_UNBLOCK_CHLD sigprocmask(SIG_SETMASK, &ssi_oldmask, NULL)
-
-#define SR_INT 1L
-#define SR_HDL(A) ((long)(A))
-#define SR_TO_INT(SR)   (((long)SR) >> 2)
+volatile BOOLEAN ssiToBeClosed_inactive=TRUE;
 
 // the helper functions:
@@ -133,7 +105,5 @@
 void ssiWriteInt(ssiInfo *d,const int i)
 {
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d ",i);
-  SSI_UNBLOCK_CHLD;
   //if (d->f_debug!=NULL) fprintf(d->f_debug,"int: %d ",i);
 }
@@ -141,7 +111,5 @@
 void ssiWriteString(ssiInfo *d,const char *s)
 {
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d %s ",(int)strlen(s),s);
-  SSI_UNBLOCK_CHLD;
   //if (d->f_debug!=NULL) fprintf(d->f_debug,"stringi: %d \"%s\" ",strlen(s),s);
 }
@@ -153,5 +121,4 @@
   // case 2 Q:     3 4 <int>
   //        or     3 3 <mpz_t nominator>
-  SSI_BLOCK_CHLD;
   if(SR_HDL(n) & SR_INT)
   {
@@ -159,8 +126,8 @@
     //if (d->f_debug!=NULL) fprintf(d->f_debug,"bigint: short \"%ld\" ",SR_TO_INT(n));
   }
-  else if (((number_dummy)n)->s==3)
+  else if (n->s==3)
   {
     fputs("3 ",d->f_write);
-    mpz_out_str(d->f_write,10,((number_dummy)n)->z);
+    mpz_out_str(d->f_write,10,n->z);
     fputc(' ',d->f_write);
     //gmp_fprintf(d->f_write,"3 %Zd ",n->z);
@@ -168,5 +135,4 @@
   }
   else WerrorS("illiegal bigint");
-  SSI_UNBLOCK_CHLD;
 }
 
@@ -182,5 +148,4 @@
   //        or     3 6 <mpz_t raw nom.> <mpz_t raw denom.>
   //        or     3 7 <mpz_t raw nom.>
-  SSI_BLOCK_CHLD;
   if(rField_is_Zp(d->r))
   {
@@ -193,28 +158,28 @@
     {
       #if SIZEOF_LONG == 4
-      fprintf(d->f_write,"4 %d ",((LONG)SR_TO_INT(n)));
+      fprintf(d->f_write,"4 %ld ",((LONG)SR_TO_INT(n)));
       #else
       long nn=SR_TO_INT(n);
       if ((nn<POW_2_28)||(nn>= -POW_2_28))
-        fprintf(d->f_write,"4 %d ",((LONG)nn)); // NOTE: use %ld once LONG can be long !!!
+        fprintf(d->f_write,"4 %ld ",((LONG)nn));
       else
       {
         mpz_t tmp;
-        mpz_init_set_si(tmp,nn);
+	mpz_init_set_si(tmp,nn);
         fputs("8 ",d->f_write);
         mpz_out_str (d->f_write,32, tmp);
         fputc(' ',d->f_write);
-        mpz_clear(tmp);
+	mpz_clear(tmp);
       }
       #endif
       //if (d->f_debug!=NULL) fprintf(d->f_debug,"number: short \"%ld\" ",SR_TO_INT(n));
     }
-    else if (((number_dummy)n)->s<2)
+    else if (n->s<2)
     {
       //gmp_fprintf(d->f_write,"%d %Zd %Zd ",n->s,n->z,n->n);
-      fprintf(d->f_write,"%d ",((number_dummy)n)->s+5);
-      mpz_out_str (d->f_write,32, ((number_dummy)n)->z);
+      fprintf(d->f_write,"%d ",n->s+5);
+      mpz_out_str (d->f_write,32, n->z);
       fputc(' ',d->f_write);
-      mpz_out_str (d->f_write,32, ((number_dummy)n)->n);
+      mpz_out_str (d->f_write,32, n->n);
       fputc(' ',d->f_write);
 
@@ -225,5 +190,5 @@
       //gmp_fprintf(d->f_write,"3 %Zd ",n->z);
       fputs("8 ",d->f_write);
-      mpz_out_str (d->f_write,32, ((number_dummy)n)->z);
+      mpz_out_str (d->f_write,32, n->z);
       fputc(' ',d->f_write);
 
@@ -232,5 +197,4 @@
   }
   else WerrorS("coeff field not implemented");
-  SSI_UNBLOCK_CHLD;
 }
 
@@ -241,5 +205,4 @@
   d->r=r;
   d->r->ref++;
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d %d ",n_GetChar(r->cf),r->N);
 
@@ -284,13 +247,10 @@
     i++;
   }
-  SSI_UNBLOCK_CHLD;
-}
-
-void ssiWritePoly(ssiInfo *d, int /*typ*/, poly p)
-{
-  SSI_BLOCK_CHLD;
+}
+
+void ssiWritePoly(ssiInfo *d, int typ, poly p)
+{
   fprintf(d->f_write,"%d ",pLength(p));//number of terms
-  SSI_UNBLOCK_CHLD;
-  int j;
+  int i;
 
   while(p!=NULL)
@@ -298,13 +258,11 @@
     ssiWriteNumber(d,pGetCoeff(p));
     //nWrite(fich,pGetCoeff(p));
-    SSI_BLOCK_CHLD;
     fprintf(d->f_write,"%ld ",p_GetComp(p,d->r));//component
 
-    for(j=1;j<=rVar(d->r);j++)
+    for(int j=1;j<=rVar(d->r);j++)
     {
       fprintf(d->f_write,"%ld ",p_GetExp(p,j,d->r ));//x^j
     }
     pIter(p);
-    SSI_UNBLOCK_CHLD;
   }
 }
@@ -316,5 +274,4 @@
    matrix M=(matrix)I;
    int mn;
-   SSI_BLOCK_CHLD;
    if (typ==MATRIX_CMD)
    {
@@ -327,5 +284,4 @@
      fprintf(d->f_write,"%d ",IDELEMS(I));
    }
-    SSI_UNBLOCK_CHLD;
 
    int i;
@@ -344,7 +300,5 @@
   ssiInfo *d=(ssiInfo*)l->data;
   // syntax: <num ops> <operation> <op1> <op2> ....
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d %d ",D->argc,D->op);
-  SSI_UNBLOCK_CHLD;
   if (D->argc >0) ssiWrite(l, &(D->arg1));
   if (D->argc < 4)
@@ -369,7 +323,5 @@
   ssiInfo *d=(ssiInfo*)l->data;
   int Ll=lSize(dd);
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d ",Ll+1);
-  SSI_UNBLOCK_CHLD;
   int i;
   for(i=0;i<=Ll;i++)
@@ -380,5 +332,4 @@
 void ssiWriteIntvec(ssiInfo *d,intvec * v)
 {
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d ",v->length());
   int i;
@@ -387,9 +338,7 @@
     fprintf(d->f_write,"%d ",(*v)[i]);
   }
-  SSI_UNBLOCK_CHLD;
 }
 void ssiWriteIntmat(ssiInfo *d,intvec * v)
 {
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d %d ",v->rows(),v->cols());
   int i;
@@ -398,10 +347,8 @@
     fprintf(d->f_write,"%d ",(*v)[i]);
   }
-  SSI_UNBLOCK_CHLD;
 }
 
 void ssiWriteBigintmat(ssiInfo *d,bigintmat * v)
 {
-  SSI_BLOCK_CHLD;
   fprintf(d->f_write,"%d %d ",v->rows(),v->cols());
   int i;
@@ -410,5 +357,4 @@
     ssiWriteBigInt(d,(*v)[i]);
   }
-  SSI_UNBLOCK_CHLD;
 }
 
@@ -419,6 +365,6 @@
   l=s_readint(d->f_read);
   buf=(char*)omAlloc0(l+1);
-  /*int c =*/ (void) s_getc(d->f_read); /* skip ' '*/
-  /*int ll=*/ (void) s_readbytes(buf,l,d->f_read);
+  int c =s_getc(d->f_read); /* skip ' '*/
+  int ll=s_readbytes(buf,l,d->f_read);
   //if (ll!=l) printf("want %d, got %d bytes\n",l,ll);
   buf[l]='\0';
@@ -439,10 +385,7 @@
    case 3:
      {// read int or mpz_t or mpz_t, mpz_t
-       number n=(number)omAlloc0(sizeof(snumber_dummy));
-       #ifdef LDEBUG
-       ((number_dummy)n)->debug=123456;
-       #endif
-       s_readmpz(d->f_read,((number_dummy)n)->z);
-       ((number_dummy)n)->s=sub_type;
+       number n=nlRInit(0);
+       s_readmpz(d->f_read,n->z);
+       n->s=sub_type;
        return n;
      }
@@ -451,5 +394,5 @@
        int dd;
        dd=s_readint(d->f_read);
-       return n_Init(dd,d->r->cf);
+       return INT_TO_SR(dd);
      }
    default:
@@ -468,13 +411,9 @@
      case 1:
        {// read mpz_t, mpz_t
-         number n=(number)omAlloc0(sizeof(snumber_dummy));
-         #ifdef LDEBUG
-         ((number_dummy)n)->debug=123456;
-         #endif
-         mpz_init(((number_dummy)n)->z);
-         mpz_init(((number_dummy)n)->n);
-         s_readmpz(d->f_read,((number_dummy)n)->z);
-         s_readmpz(d->f_read,((number_dummy)n)->n);
-         ((number_dummy)n)->s=sub_type;
+         number n=nlRInit(0);
+         mpz_init(n->n);
+         s_readmpz(d->f_read,n->z);
+         s_readmpz(d->f_read,n->n);
+         n->s=sub_type;
          return n;
        }
@@ -482,41 +421,33 @@
      case 3:
        {// read mpz_t
-         number n=(number)omAlloc0(sizeof(snumber_dummy));
-         #ifdef LDEBUG
-         ((number_dummy)n)->debug=123456;
-         #endif
-         mpz_init(((number_dummy)n)->z);
-         s_readmpz(d->f_read,((number_dummy)n)->z);
-         ((number_dummy)n)->s=3; /*sub_type*/
+         number n=nlRInit(0);
+         s_readmpz(d->f_read,n->z);
+         n->s=3; /*sub_type*/
          return n;
        }
      case 4:
        {
-         int dd=s_readint(d->f_read);
-         return n_Init(dd,coeffs_BIGINT);
+         LONG dd=s_readlong(d->f_read);
+	 //#if SIZEOF_LONG == 8
+         return INT_TO_SR(dd);
+	 //#else
+	 //return nlInit(dd,NULL);
+	 //#endif
        }
      case 5:
      case 6:
        {// read raw mpz_t, mpz_t
-         number n=(number)omAlloc0(sizeof(snumber_dummy));
-         #ifdef LDEBUG
-         ((number_dummy)n)->debug=123456;
-         #endif
-         mpz_init(((number_dummy)n)->z);
-         mpz_init(((number_dummy)n)->n);
-         s_readmpz_base (d->f_read,((number_dummy)n)->z, 32);
-         s_readmpz_base (d->f_read,((number_dummy)n)->n, 32);
-         ((number_dummy)n)->s=sub_type-5;
+         number n=nlRInit(0);
+         mpz_init(n->n);
+         s_readmpz_base (d->f_read,n->z, 32);
+         s_readmpz_base (d->f_read,n->n, 32);
+         n->s=sub_type-5;
          return n;
        }
      case 8:
        {// read raw mpz_t
-         number n=(number)omAlloc0(sizeof(snumber_dummy));
-         #ifdef LDEBUG
-         ((number_dummy)n)->debug=123456;
-         #endif
-         mpz_init(((number_dummy)n)->z);
-         s_readmpz_base (d->f_read,((number_dummy)n)->z, 32);
-         ((number_dummy)n)->s=sub_type=3; /*subtype-5*/
+         number n=nlRInit(0);
+         s_readmpz_base (d->f_read,n->z, 32);
+         n->s=sub_type=3; /*subtype-5*/
          return n;
        }
@@ -536,6 +467,7 @@
   {
     // read int
-    int dd=s_readint(d->f_read);
-    return (number)dd;
+    int dd;
+    dd=s_readint(d->f_read);
+    return (number)(long)dd;
   }
   else Werror("coeffs not implemented");
@@ -546,5 +478,5 @@
 {
 /* syntax is <ch> <N> <l1> <v1> ...<lN> <vN> <number of orderings> <ord1> <block0_1> <block1_1> .... */
-  int ch, N,i/*,l*/;
+  int ch, N,i,l;
   char **names;
   ch=s_readint(d->f_read);
@@ -603,6 +535,6 @@
   //Print("poly: terms:%d\n",n);
   poly p;
-  // int j;
-  // j=0;
+  int j;
+  j=0;
   poly ret=NULL;
   poly prev=NULL;
@@ -612,5 +544,6 @@
     p=p_Init(D->r);
     pGetCoeff(p)=ssiReadNumber(D);
-    int d=s_readint(D->f_read);
+    int d;
+    d=s_readint(D->f_read);
     p_SetComp(p,d,D->r);
     for(i=1;i<=rVar(D->r);i++)
@@ -630,7 +563,8 @@
 ideal ssiReadIdeal(ssiInfo *d)
 {
-  int i;
-  int n=s_readint(d->f_read);
-  ideal I=idInit(n,1);
+  int n,i;
+  ideal I;
+  n=s_readint(d->f_read);
+  I=idInit(n,1);
   for(i=0;i<IDELEMS(I);i++) // read n terms
   {
@@ -642,6 +576,7 @@
 matrix ssiReadMatrix(ssiInfo *d)
 {
-  int m=s_readint(d->f_read);
-  int n=s_readint(d->f_read);
+  int n,m,i,j;
+  m=s_readint(d->f_read);
+  n=s_readint(d->f_read);
   matrix M=mpNew(m,n);
   poly p;
@@ -714,5 +649,6 @@
 {
   ssiInfo *d=(ssiInfo*)l->data;
-  int nr=s_readint(d->f_read);
+  int nr;
+  nr=s_readint(d->f_read);
   lists L=(lists)omAlloc(sizeof(*L));
   L->Init(nr);
@@ -730,5 +666,6 @@
 intvec* ssiReadIntvec(ssiInfo *d)
 {
-  int nr=s_readint(d->f_read);
+  int nr;
+  nr=s_readint(d->f_read);
   intvec *v=new intvec(nr);
   for(int i=0;i<nr;i++)
@@ -766,5 +703,6 @@
 {
   ssiInfo *d=(ssiInfo*)l->data;
-  /*int throwaway=*/ (void) s_readint(d->f_read);
+  int throwaway;
+  throwaway=s_readint(d->f_read);
   char *name=ssiReadString(d);
   int tok;
@@ -790,6 +728,4 @@
     const char *mode;
     ssiInfo *d=(ssiInfo*)omAlloc0(sizeof(ssiInfo));
-    sigprocmask(SIG_SETMASK, NULL, &ssi_sigmask);
-    sigaddset(&ssi_sigmask, SIGCHLD);
     if (flag & SI_LINK_OPEN)
     {
@@ -824,11 +760,15 @@
         int pc[2];
         int cp[2];
-        SSI_BLOCK_CHLD;
         pipe(pc);
         pipe(cp);
         pid_t pid=fork();
-        SSI_UNBLOCK_CHLD;
         if (pid==0) /*fork: child*/
         {
+          /* block SIGINT */
+          sigset_t sigint;
+          sigemptyset(&sigint);
+          sigaddset(&sigint, SIGINT);
+          sigprocmask(SIG_BLOCK, &sigint, NULL);
+
           link_list hh=(link_list)ssiToBeClosed->next;
           /* we know: l is the first entry in ssiToBeClosed-list */
@@ -837,8 +777,7 @@
             SI_LINK_SET_CLOSE_P(hh->l);
             ssiInfo *dd=(ssiInfo*)hh->l->data;
-            SSI_BLOCK_CHLD;
             s_close(dd->f_read);
+            s_free(dd->f_read);
             fclose(dd->f_write);
-            SSI_UNBLOCK_CHLD;
             if (dd->r!=NULL) rKill(dd->r);
             omFreeSize((ADDRESS)dd,(sizeof *dd));
@@ -849,8 +788,6 @@
           }
           ssiToBeClosed->next=NULL;
-          SSI_BLOCK_CHLD;
           si_close(pc[1]); si_close(cp[0]);
           d->f_write=fdopen(cp[1],"w");
-          SSI_UNBLOCK_CHLD;
           d->f_read=s_open(pc[0]);
           d->fd_read=pc[0];
@@ -886,8 +823,6 @@
         {
           d->pid=pid;
-          SSI_BLOCK_CHLD;
           si_close(pc[0]); si_close(cp[1]);
           d->f_write=fdopen(pc[1],"w");
-          SSI_UNBLOCK_CHLD;
           d->f_read=s_open(cp[0]);
           d->fd_read=cp[0];
@@ -909,5 +844,5 @@
         int sockfd, newsockfd, portno, clilen;
         struct sockaddr_in serv_addr, cli_addr;
-        // int n;
+        int n;
         sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if(sockfd < 0)
@@ -970,5 +905,5 @@
         int sockfd, newsockfd, portno, clilen;
         struct sockaddr_in serv_addr, cli_addr;
-        // int n;
+        int n;
         sockfd = socket(AF_INET, SOCK_STREAM, 0);
         if(sockfd < 0)
@@ -1038,13 +973,14 @@
         d->fd_write = newsockfd;
         d->f_read = s_open(newsockfd);
-        SSI_BLOCK_CHLD;
         d->f_write = fdopen(newsockfd, "w");
         si_close(sockfd);
-        SSI_UNBLOCK_CHLD;
         SI_LINK_SET_RW_OPEN_P(l);
         d->send_quit_at_exit=1;
-        SSI_BLOCK_CHLD;
+        link_list newlink=(link_list)omAlloc(sizeof(link_struct));
+        newlink->u=u;
+        newlink->l=l;
+        newlink->next=(void *)ssiToBeClosed;
+        ssiToBeClosed=newlink;
         fprintf(d->f_write,"98 %d %d %u %u\n",SSI_VERSION,MAX_TOK,si_opt_1,si_opt_2);
-        SSI_UNBLOCK_CHLD;
       }
       // ----------------------------------------------------------------------
@@ -1052,5 +988,5 @@
       {
         char* host = (char*)omAlloc(256);
-        int sockfd, portno/*, n*/;
+        int sockfd, portno, n;
         struct sockaddr_in serv_addr;
         struct hostent *server;
@@ -1078,5 +1014,4 @@
           d->fd_write=sockfd;
           SI_LINK_SET_RW_OPEN_P(l);
-          d->send_quit_at_exit=1;
           omFree(host);
         }
@@ -1108,22 +1043,16 @@
           }
         }
-        SSI_BLOCK_CHLD;
         outfile=myfopen(filename,mode);
-        SSI_UNBLOCK_CHLD;
         if (outfile!=NULL)
         {
           if (strcmp(l->mode,"r")==0)
           {
-            SSI_BLOCK_CHLD;
             fclose(outfile);
             d->f_read=s_open_by_name(filename);
-            SSI_UNBLOCK_CHLD;
           }
           else
           {
-            SSI_BLOCK_CHLD;
             d->f_write = outfile;
             fprintf(d->f_write,"98 %d %d %u %u\n",SSI_VERSION,MAX_TOK,si_opt_1,si_opt_2);
-            SSI_UNBLOCK_CHLD;
           }
         }
@@ -1151,8 +1080,6 @@
       if (d->send_quit_at_exit)
       {
-        SSI_BLOCK_CHLD;
         fputs("99\n",d->f_write);
         fflush(d->f_write);
-        SSI_UNBLOCK_CHLD;
       }
       d->quit_sent=1;
@@ -1173,8 +1100,6 @@
       && (d->quit_sent==0))
       {
-        SSI_BLOCK_CHLD;
         fputs("99\n",d->f_write);
         fflush(d->f_write);
-        SSI_UNBLOCK_CHLD;
       }
       if (d->r!=NULL) rKill(d->r);
@@ -1185,5 +1110,5 @@
         t.tv_sec=0;
         t.tv_nsec=50000000; // <=50 ms
-        int r=nanosleep(&t,NULL);
+        int r=si_nanosleep(&t,NULL);
         if((r==0) && (si_waitpid(d->pid,NULL,WNOHANG)==0))
         {
@@ -1191,5 +1116,5 @@
           t.tv_sec=0;
           t.tv_nsec=10000000; // <=10 ms
-          r=nanosleep(&t,NULL);
+          r=si_nanosleep(&t,NULL);
           if((r==0)&&(si_waitpid(d->pid,NULL,WNOHANG)==0))
           {
@@ -1200,4 +1125,5 @@
       }
       if (d->f_read!=NULL) s_close(d->f_read);
+      if (d->f_read!=NULL) s_free(d->f_read);
       if (d->f_write!=NULL) fclose(d->f_write);
       if ((strcmp(l->mode,"tcp")==0)
@@ -1244,5 +1170,5 @@
   {
     case 1:res->rtyp=INT_CMD;
-           res->data=(char *)ssiReadInt(d->f_read);
+           res->data=(char *)(long)ssiReadInt(d->f_read);
            break;
     case 2:res->rtyp=STRING_CMD;
@@ -1318,5 +1244,5 @@
     case 19: res->rtyp=BIGINTMAT_CMD;
              res->data=ssiReadBigintmat(d);
-             break;
+	     break;
     case 20: ssiReadBlackbox(res,l);
              break;
@@ -1340,5 +1266,5 @@
                 #endif
                 si_opt_1=n98_o1;
-                si_opt_2=n98_o2;
+	        si_opt_2=n98_o2;
                 return ssiRead1(l);
              }
@@ -1503,5 +1429,5 @@
   && (strcmp(request, "read") == 0))
   {
-    fd_set  mask/*, fdmask*/;
+    fd_set  mask, fdmask;
     struct timeval wt;
     if (s_isready(d->f_read)) return "ready";
@@ -1565,4 +1491,7 @@
   si_link l;
   ssiInfo *d;
+  #ifdef HAVE_MPSR
+  MP_Link_pt dd;
+  #endif
   int d_fd;
   fd_set  mask, fdmask;
@@ -1607,7 +1536,29 @@
         && (strcmp(l->mode,"launch")!=0) && (strcmp(l->mode,"connect")!=0)))
       {
-        WerrorS("all links must be of type ssi:fork, ssi:tcp, ssi:connect");
+        WerrorS("all links must be of type ssi:fork, ssi:tcp, ssi:connect,");
+        WerrorS(" MPtcp:fork or MPtcp:launch");
         return -2;
       }
+    #ifdef HAVE_MPSR
+      if (strcmp(l->m->type,"ssi")==0)
+      {
+        d=(ssiInfo*)l->data;
+        d_fd=d->fd_read;
+        if (!s_isready(d->f_read))
+        {
+          FD_SET(d_fd, &fdmask);
+          if (d_fd > max_fd) max_fd=d_fd;
+        }
+        else
+          return i+1;
+      }
+      else
+      {
+        dd=(MP_Link_pt)l->data;
+        d_fd=((MP_TCP_t *)dd->transp.private1)->sock;
+        FD_SET(d_fd, &fdmask);
+        if (d_fd > max_fd) max_fd=d_fd;
+      }
+    #else
       d=(ssiInfo*)l->data;
       d_fd=d->fd_read;
@@ -1619,4 +1570,5 @@
       else
         return i+1;
+    #endif
     }
   }
@@ -1661,7 +1613,23 @@
       {
         l=(si_link)L->m[i].Data();
+        #ifdef HAVE_MPSR
+        if (strcmp(l->m->type,"ssi")!=0)
+        {
+          // for MP links, return here:
+          dd=(MP_Link_pt)l->data;
+          d_fd=((MP_TCP_t *)dd->transp.private1)->sock;
+          if(j==d_fd) return i+1;
+        }
+        else
+        {
+          d=(ssiInfo*)l->data;
+          d_fd=d->fd_read;
+          if(j==d_fd) break;
+        }
+        #else
         d=(ssiInfo*)l->data;
         d_fd=d->fd_read;
         if(j==d_fd) break;
+        #endif
       }
     }
@@ -1754,5 +1722,5 @@
   }
   int portno;
-  // int n;
+  int n;
   ssiReserved_sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if(ssiReserved_sockfd < 0)
@@ -1826,6 +1794,4 @@
   l->ref=1;
   ssiInfo *d=(ssiInfo*)omAlloc0(sizeof(ssiInfo));
-  sigprocmask(SIG_SETMASK, NULL, &ssi_sigmask);
-  sigaddset(&ssi_sigmask, SIGCHLD);
   l->data=d;
   d->fd_read = newsockfd;
@@ -1856,5 +1822,5 @@
 **/
 /*---------------------------------------------------------------------*/
-void sig_chld_hdl(int /*sig*/)
+void sig_chld_hdl(int sig)
 {
   pid_t kidpid;
@@ -1875,5 +1841,5 @@
     //printf("Child %ld terminated\n", kidpid);
     link_list hh=ssiToBeClosed;
-    while(hh!=NULL)
+    while((hh!=NULL)&&(ssiToBeClosed_inactive))
     {
       if((hh->l!=NULL) && (hh->l->m->Open==ssiOpen))
