home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / fonts / server / os / osglue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  7.2 KB  |  317 lines

  1. /* $XConsortium: osglue.c,v 1.5 91/10/24 21:46:59 rws Exp $ */
  2. /*
  3.  * Copyright 1990, 1991 Network Computing Devices;
  4.  * Portions Copyright 1987 by Digital Equipment Corporation and the
  5.  * Massachusetts Institute of Technology
  6.  *
  7.  * Permission to use, copy, modify, and distribute this protoype software
  8.  * and its documentation to Members and Affiliates of the MIT X Consortium
  9.  * any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the names of Network Computing Devices, Digital or
  13.  * MIT not be used in advertising or publicity pertaining to distribution of
  14.  * the software without specific, written prior permission.
  15.  *
  16.  * NETWORK COMPUTING DEVICES, DIGITAL AND MIT DISCLAIM ALL WARRANTIES WITH
  17.  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18.  * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, DIGITAL OR MIT BE
  19.  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  20.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  21.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  22.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  *
  24.  * $NCDId: @(#)osglue.c,v 4.6 1991/07/09 14:07:30 lemke Exp $
  25.  *
  26.  */
  27.  
  28. /*
  29.  * this is miscellaneous OS specific stuff.
  30.  *
  31.  * Catalogue support, alternate servers, and cloneing
  32.  */
  33.  
  34. #include "osstruct.h"
  35. #include <stdio.h>
  36. #define  XK_LATIN1
  37. #include <X11/keysymdef.h>
  38.  
  39. Bool        drone_server = FALSE;
  40. extern Bool CloneSelf;
  41. extern int  ListenSock;
  42. extern char *progname;
  43. extern char *configfilename;
  44.  
  45. static int  num_alts;
  46. static AlternateServerPtr alt_servers = (AlternateServerPtr) 0;
  47.  
  48. /*
  49.  * XXX
  50.  *
  51.  * Catalogue support is absolutely minimal.  Some guts are here, but
  52.  * we don't actually do anything with them so the only one exported is
  53.  * 'all'.  Be warned that other parts of the server may incorrectly
  54.  * assume the catalogue list is global, and will therefore need fixing.
  55.  *
  56.  */
  57.  
  58. static char *catalogue_name = "all";
  59.  
  60. static Bool            /* stolen from R4 Match() */
  61. pattern_match(pat, plen, string)
  62.     char       *pat;
  63.     int         plen;
  64.     char       *string;
  65. {
  66.     register int i,
  67.                 l;
  68.     int         j,
  69.                 m,
  70.                 res;
  71.     register char cp,
  72.                 cs;
  73.     int         head,
  74.                 tail;
  75.  
  76.     head = 0;
  77.     tail = plen;
  78.  
  79.     res = -1;
  80.     for (i = 0; i < head; i++) {
  81.     cp = pat[i];
  82.     if (cp == XK_question) {
  83.         if (!string[i])
  84.         return res;
  85.         res = 0;
  86.     } else if (cp != string[i])
  87.         return res;
  88.     }
  89.     if (head == plen)
  90.     return (string[head] ? res : 1);
  91.     l = head;
  92.     while (++i < tail) {
  93.     /* we just skipped an asterisk */
  94.     j = i;
  95.     m = l;
  96.     while ((cp = pat[i]) != XK_asterisk) {
  97.         if (!(cs = string[l]))
  98.         return 0;
  99.         if ((cp != cs) && (cp != XK_question)) {
  100.         m++;
  101.         cp = pat[j];
  102.         if (cp == XK_asterisk) {
  103.             if (!string[m])
  104.             return 0;
  105.         } else {
  106.             while ((cs = string[m]) != cp) {
  107.             if (!cs)
  108.                 return 0;
  109.             m++;
  110.             }
  111.         }
  112.         l = m;
  113.         i = j;
  114.         }
  115.         l++;
  116.         i++;
  117.     }
  118.     }
  119.     m = strlen(&string[l]);
  120.     j = plen - tail;
  121.     if (m < j)
  122.     return 0;
  123.     l = (l + m) - j;
  124.     while (cp = pat[i]) {
  125.     if ((cp != string[l]) && (cp != XK_question))
  126.         return 0;
  127.     l++;
  128.     i++;
  129.     }
  130.     return 1;
  131. }
  132.  
  133. int
  134. ListCatalogues(pattern, patlen, maxnames, catalogues, len)
  135.     char       *pattern;
  136.     int         patlen;
  137.     int         maxnames;
  138.     char      **catalogues;
  139.     int        *len;
  140. {
  141.     int         count = 0;
  142.     char       *catlist = NULL;
  143.     int         size = 0;
  144.  
  145.     if (maxnames) {
  146.     if (pattern_match(pattern, patlen, catalogue_name)) {
  147.         size = strlen(catalogue_name);
  148.         catlist = (char *) fsalloc(size + 1);
  149.         if (!catlist)
  150.         goto bail;
  151.         *catlist = size;
  152.         bcopy(catalogue_name, &catlist[1], size);
  153.         size++;        /* for length */
  154.         count++;
  155.     }
  156.     }
  157. bail:
  158.     *len = size;
  159.     *catalogues = catlist;
  160.     return count;
  161. }
  162.  
  163. /*
  164.  * check if catalogue list is valid
  165.  */
  166.  
  167. int
  168. ValidateCatalogues(num, cats)
  169.     int        *num;
  170.     char       *cats;
  171. {
  172.     char       *c = cats;
  173.     int         i,
  174.                 len;
  175.  
  176.     for (i = 0; i < *num; i++) {
  177.     len = *c++;
  178.     if (strncmp(c, catalogue_name, len)) {
  179.         *num = i;        /* return bad entry index */
  180.         return FSBadName;
  181.     }
  182.     c += len;
  183.     }
  184.     return FSSuccess;
  185. }
  186.  
  187. int
  188. SetAlternateServers(list)
  189.     char       *list;
  190. {
  191.     char       *t,
  192.                *st;
  193.     AlternateServerPtr alts,
  194.                 a;
  195.     int         num,
  196.                 i;
  197.  
  198.     t = list;
  199.     num = 1;
  200.     while (*t) {
  201.     if (*t == ',')
  202.         num++;
  203.     t++;
  204.     }
  205.  
  206.     a = alts = (AlternateServerPtr) fsalloc(sizeof(AlternateServerRec) * num);
  207.     if (!alts)
  208.     return FSBadAlloc;
  209.  
  210.     st = t = list;
  211.     a->namelen = 0;
  212.     while (*t) {
  213.     if (*t == ',') {
  214.         a->name = (char *) fsalloc(a->namelen);
  215.         if (!a->name) {
  216.         /* XXX  -- leak */
  217.         return FSBadAlloc;
  218.         }
  219.         bcopy(st, a->name, a->namelen);
  220.         a->subset = FALSE;    /* XXX */
  221.         a++;
  222.         t++;
  223.         st = t;
  224.         a->namelen = 0;
  225.     } else {
  226.         a->namelen++;
  227.         t++;
  228.     }
  229.     }
  230.     a->name = (char *) fsalloc(a->namelen);
  231.     if (!a->name) {
  232.     /* XXX  -- leak */
  233.     return FSBadAlloc;
  234.     }
  235.     bcopy(st, a->name, a->namelen);
  236.     a->subset = FALSE;        /* XXX */
  237.  
  238.     for (i = 0; i < num_alts; i++) {
  239.     fsfree((char *) alt_servers[i].name);
  240.     }
  241.     fsfree((char *) alt_servers);
  242.     num_alts = num;
  243.     alt_servers = alts;
  244.     return FSSuccess;
  245. }
  246.  
  247. int
  248. ListAlternateServers(svrs)
  249.     AlternateServerPtr *svrs;
  250. {
  251.     *svrs = alt_servers;
  252.     return num_alts;
  253. }
  254.  
  255. /*
  256.  * here's some fun stuff.  in order to cleanly handle becoming overloaded,
  257.  * this allows us to clone ourselves.  the parent keeps the Listen
  258.  * socket open, and sends it to itself.  the child stops listening,
  259.  * and becomes a drone, hanging out till it loses all its clients.
  260.  */
  261. int
  262. CloneMyself()
  263. {
  264.     int         child;
  265.     char        sockarg[32];
  266.     int         i;
  267.     int         lastfdesc;
  268.  
  269.     assert(!drone_server);    /* a drone shouldn't hit this */
  270.  
  271.     if (!CloneSelf)
  272.     return -1;
  273.  
  274. #if defined(hpux) || defined(SVR4)
  275.     lastfdesc = _NFILE - 1;
  276. #else
  277.     lastfdesc = getdtablesize() - 1;
  278. #endif                /* hpux */
  279.  
  280.     NoticeF("attempting clone...\n");
  281.     child = fork();
  282.     if (child == -1) {
  283.     /* failed to fork */
  284.     ErrorF("Clone failed to fork()\n");
  285.     return -1;
  286.     }
  287.     /*
  288.      * Note:  they still share the same process group, and killing the parent
  289.      * will take out all the kids as well.  this is considered a feature (at
  290.      * least until i'm convinced otherwise)
  291.      */
  292.     if (child == 0) {
  293.     StopListening();
  294.     NoticeF("Clone: child becoming drone\n");
  295.     drone_server = TRUE;
  296.     return 1;
  297.     } else {            /* parent */
  298.     NoticeF("Clone: parent revitalizing as %s\n", progname);
  299.     CloseErrors();
  300.     /* XXX should we close stdio as well? */
  301.     for (i = 3; i < lastfdesc; i++) {
  302.         if (i != ListenSock)
  303.         (void) close(i);
  304.     }
  305.     sprintf(sockarg, "%d", ListenSock);
  306.     execlp(progname, progname,
  307.            "-ls", sockarg,
  308.            "-cf", configfilename,
  309.            NULL);
  310.     InitErrors();        /* reopen errors, since we don't want to lose
  311.                  * this */
  312.     Error("Clone failed");
  313.     FatalError("Failed to clone self\n");
  314.     }
  315.     /* NOTREACHED */
  316. }
  317.