home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / pub / uniflex / ufkerm.uue / ufkerm.arc / UFPRIV.C < prev    next >
C/C++ Source or Header  |  1989-01-09  |  6KB  |  185 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.    char c,
  18.         *prm[2];
  19.    static char *tname = { "kermit_support" };
  20.  
  21.    in = 0;
  22.    out = 1;
  23.    prm[0] = tname;
  24.    prm[1] = 0;
  25.  
  26.    if ((pipe(pd1) == ERROR) ||          /* Create input pipe */
  27.        (pipe(pd2) == ERROR) ||          /* Create output pipe */
  28.        ((pid = fork()) == ERROR))       /* Create child process */
  29.    {
  30.       prterr(ER_PRIVSERVER);            /* Something wrong */
  31.       kerm_exit();
  32.    }
  33.    if (!pid)
  34.    {                                    /* child process */
  35.       close(pd1[READ]);                 /* Close one side for read */
  36.       close(pd2[WRITE]);                /* and the other side for write */
  37.       dup2(pd2[READ],in);               /* Copy to stdin */
  38.       dup2(pd1[WRITE],out);             /* and to stdout */
  39.       for (c=2; c < 10; c++)
  40.          close(c);                      /* close all files */
  41.  
  42.       execv(PRIVTASK,prm);              /* Try to start the support task */
  43.       c = 1;                            /* Too bad if we come here */
  44.       write(pd1[WRITE],&c,1);           /* Send error to parent */
  45.       exit(1);                          /* Error exit for child */
  46.    }
  47.    close(pd1[WRITE]);                   /* Close one side for write */
  48.    close(pd2[READ]);                    /* and the other side for read */
  49.  
  50.    pread(&c,1);                  /* Get acknowledge from server or child */
  51.    if (c)                               /* If true it is an error that the */
  52.    {                                    /* support task did not run */
  53.       prterr(ER_PRIVSERVER);            /* Something wrong */
  54.       kerm_exit();
  55.    }
  56.    prvsetdone = TRUE;                   /* setup ok */
  57. }
  58.  
  59. baud(dev,rate)
  60. char *dev;
  61. int rate;
  62. {
  63.    int len,
  64.        stat;
  65.  
  66.    if (!prvsetdone)                     /* If not been here before... */
  67.       prv_init();                       /* Do the necessary setup */
  68.  
  69.    len = strlen(dev) + 1;               /* must send terminator as well */
  70.    pwrite("b",1);                       /* 'baudrate' command */
  71.    pwrite(&len,2);                      /* length of device name */
  72.    pwrite(dev,len);                     /* device name */
  73.    pwrite(&rate,2);                     /* baudrate */
  74.    pread(&stat,2);                      /* read status */
  75.    if (stat)
  76.    {
  77.       sup_error();                      /* get and print support task error */
  78.       return TRUE;
  79.    }
  80.    return FALSE;
  81. }
  82.  
  83. do_break(dev)
  84. char *dev;
  85. {
  86.    int len,
  87.        stat;
  88.  
  89.    if (!prvsetdone)                     /* If not been here before... */
  90.       prv_init();                       /* Do the necessary setup */
  91.  
  92.    len = strlen(dev) + 1;               /* must send terminator as well */
  93.    pwrite("w",1);                       /* 'break' command */
  94.    pwrite(&len,2);                      /* length of device name */
  95.    pwrite(dev,len);                     /* device name */
  96.    pread(&stat,2);                      /* read status */
  97.    if (stat)
  98.    {
  99.       sup_error();                      /* get and print support task error */
  100.       return TRUE;
  101.    }
  102.    return FALSE;
  103. }
  104.  
  105. fset_date(file,date)
  106. char *file;
  107. long date;
  108. {
  109.    int len,
  110.        stat;
  111.  
  112.    if (!prvsetdone)                     /* If not been here before... */
  113.       prv_init();                       /* Do the necessary setup */
  114.  
  115.    len = strlen(file) + 1;              /* must send terminator as well */
  116.    pwrite("d",1);                       /* 'date' command */
  117.    pwrite(&len,2);                      /* length of file name */
  118.    pwrite(file,len);                    /* file name */
  119.    pwrite(&date,4);                     /* date */
  120.    pread(&stat,2);                      /* read status */
  121.    if (stat)
  122.    {
  123.       sup_error();                      /* get and print support task error */
  124.       return TRUE;
  125.    }
  126.    return FALSE;
  127. }
  128.  
  129. set_dir(dir)
  130. char *dir;
  131. {
  132.    int len,
  133.        stat;
  134.  
  135.    if (!prvsetdone)                     /* If not been here before... */
  136.       prv_init();                       /* Do the necessary setup */
  137.  
  138.    len = strlen(dir) + 1;               /* must send terminator as well */
  139.    pwrite("s",1);                       /* 'date' command */
  140.    pwrite(&len,2);                      /* length of file name */
  141.    pwrite(dir,len);                     /* file name */
  142.    pread(&stat,2);                      /* read status */
  143.    if (stat)
  144.    {
  145.       sup_error();                      /* get and print support task error */
  146.       return TRUE;
  147.    }
  148.    return FALSE;
  149. }
  150.  
  151. long get_free(dev)
  152. char *dev;
  153. {
  154.    int len;
  155.    long size;
  156.  
  157.    if (!prvsetdone)                     /* If not been here before... */
  158.       prv_init();                       /* Do the necessary setup */
  159.  
  160.    len = strlen(dev) + 1;               /* must send terminator as well */
  161.    pwrite("f",1);                       /* 'free blocks' command */
  162.    pwrite(&len,2);                      /* device name length */
  163.    pwrite(dev,len);                     /* device name itself */
  164.    pread(&size,4);                      /* Get size */
  165.    if (size == -1l)                     /* error ? */
  166.       sup_error();                      /* get and print support task error */
  167.    return size;
  168. }
  169.  
  170. sup_exit()
  171. {
  172.    if (prvsetdone)                      /* If setup done... */
  173.       pwrite("e",1);                    /* 'exit' command */
  174. }
  175.  
  176. sup_error()
  177. {
  178.    int len;
  179.    char data[100];
  180.  
  181.    pread(&len,2);                       /* get length of error string */
  182.    pread(data,len);                     /* get error string */
  183.    fprintf(stderr,"%s\n",data);
  184. }
  185.