home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / uniflex.zip / ufpriv.c < prev    next >
C/C++ Source or Header  |  1993-08-23  |  6KB  |  190 lines

  1. /*
  2.    Kermit's privileged services.
  3.  
  4.    These services include the setting of the baudrate of a port,
  5.    and the retrieval of the number of free blocks on the specified
  6.    device. This is done by means of a privileged server task, which
  7.    does the dirty work. We talk to this task with two pipes.
  8. */
  9.  
  10. #include "ufk.h"
  11.  
  12. prv_init()
  13. {
  14.    int pid,
  15.        in,
  16.        out,
  17.        p;
  18.    char c,
  19.         *prm[2];
  20.    static char *tname = { "kermit_support" };
  21.  
  22.    if (prvsetdone)                      /* If setup already done... */
  23.       return;                           /* then get the hell out of here */
  24.  
  25.    in = 0;
  26.    out = 1;
  27.    prm[0] = tname;
  28.    prm[1] = 0;
  29.  
  30.    if ((pipe(pd1) == ERROR) ||          /* Create input pipe */
  31.        (pipe(pd2) == ERROR) ||          /* Create output pipe */
  32.        ((pid = fork()) == ERROR))       /* Create child process */
  33.    {
  34.       prterr(ER_PRIVSERVER);            /* Something wrong */
  35.       kerm_exit();
  36.    }
  37.    if (!pid)
  38.    {                                    /* child process */
  39.       close(pd1[READ]);                 /* Close one side for read */
  40.       close(pd2[WRITE]);                /* and the other side for write */
  41.       dup2(pd2[READ],in);               /* Copy to stdin */
  42.       dup2(pd1[WRITE],out);             /* and to stdout */
  43.       for (c=2; c < 10; c++)
  44.          close(c);                      /* close all files */
  45.  
  46.       execv(PRIVTASK,prm);              /* Try to start the support task */
  47.       c = 0;                            /* Too bad if we come here */
  48.       write(out,&c,1);                  /* Send error to parent */
  49.       exit(1);                          /* Error exit for child */
  50.    }
  51.    close(pd1[WRITE]);                   /* Close one side for write */
  52.    close(pd2[READ]);                    /* and the other side for read */
  53.  
  54.    pread(&c,1);                  /* Get acknowledge from server or child */
  55.    if (c == 0)                          /* If false then the support task */
  56.    {                                    /* did not run */
  57.       prterr(ER_PRIVSERVER);            /* Something wrong */
  58.       kerm_exit();
  59.    }
  60.    prvsetdone = TRUE;                   /* setup ok */
  61.    if (c < PROTOCOL_VERSION)            /* Check for correct protocol */
  62.    {
  63.       prterr(ER_PRIVPROT);              /* Invalid protocol */
  64.       kerm_exit();
  65.    }
  66. }
  67.  
  68. baud(dev,rate)
  69. char *dev;
  70. int rate;
  71. {
  72.    int len,
  73.        stat;
  74.  
  75.    prv_init();                          /* Do the necessary setup */
  76.  
  77.    len = strlen(dev) + 1;               /* must send terminator as well */
  78.    pwrite("b",1);                       /* 'baudrate' command */
  79.    pwrite(&len,2);                      /* length of device name */
  80.    pwrite(dev,len);                     /* device name */
  81.    pwrite(&rate,2);                     /* baudrate */
  82.    pwrite(&maskflag,1);                 /* mask flag for RTS & DCD */
  83.    pread(&stat,2);                      /* read status */
  84.    if (stat)
  85.    {
  86.       sup_error();                      /* get and print support task error */
  87.       return TRUE;
  88.    }
  89.    return FALSE;
  90. }
  91.  
  92. do_break(dev)
  93. char *dev;
  94. {
  95.    int len,
  96.        stat;
  97.  
  98.    prv_init();                          /* Do the necessary setup */
  99.  
  100.    len = strlen(dev) + 1;               /* must send terminator as well */
  101.    pwrite("w",1);                       /* 'break' command */
  102.    pwrite(&len,2);                      /* length of device name */
  103.    pwrite(dev,len);                     /* device name */
  104.    pread(&stat,2);                      /* read status */
  105.    if (stat)
  106.    {
  107.       sup_error();                      /* get and print support task error */
  108.       return TRUE;
  109.    }
  110.    return FALSE;
  111. }
  112.  
  113. fset_date(file,date)
  114. char *file;
  115. long date;
  116. {
  117.    int len,
  118.        stat;
  119.  
  120.    prv_init();                          /* Do the necessary setup */
  121.  
  122.    len = strlen(file) + 1;              /* must send terminator as well */
  123.    pwrite("d",1);                       /* 'date' command */
  124.    pwrite(&len,2);                      /* length of file name */
  125.    pwrite(file,len);                    /* file name */
  126.    pwrite(&date,4);                     /* date */
  127.    pread(&stat,2);                      /* read status */
  128.    if (stat)
  129.    {
  130.       sup_error();                      /* get and print support task error */
  131.       return TRUE;
  132.    }
  133.    return FALSE;
  134. }
  135.  
  136. set_dir(dir)
  137. char *dir;
  138. {
  139.    int len,
  140.        stat;
  141.  
  142.    prv_init();                          /* Do the necessary setup */
  143.  
  144.    len = strlen(dir) + 1;               /* must send terminator as well */
  145.    pwrite("s",1);                       /* 'date' command */
  146.    pwrite(&len,2);                      /* length of file name */
  147.    pwrite(dir,len);                     /* file name */
  148.    pread(&stat,2);                      /* read status */
  149.    if (stat)
  150.    {
  151.       sup_error();                      /* get and print support task error */
  152.       return TRUE;
  153.    }
  154.    return FALSE;
  155. }
  156.  
  157. long get_free(dev)
  158. char *dev;
  159. {
  160.    int len;
  161.    long size;
  162.  
  163.    prv_init();                          /* Do the necessary setup */
  164.  
  165.    len = strlen(dev) + 1;               /* must send terminator as well */
  166.    pwrite("f",1);                       /* 'free blocks' command */
  167.    pwrite(&len,2);                      /* device name length */
  168.    pwrite(dev,len);                     /* device name itself */
  169.    pread(&size,4);                      /* Get size */
  170.    if (size == -1l)                     /* error ? */
  171.       sup_error();                      /* get and print support task error */
  172.    return size;
  173. }
  174.  
  175. sup_exit()
  176. {
  177.    if (prvsetdone)                      /* If setup done... */
  178.       pwrite("e",1);                    /* 'exit' command */
  179. }
  180.  
  181. sup_error()
  182. {
  183.    int len;
  184.    char data[100];
  185.  
  186.    pread(&len,2);                       /* get length of error string */
  187.    pread(data,len);                     /* get error string */
  188.    fprintf(stderr,"%s\n",data);
  189. }
  190.