Home | History | Annotate | Line # | Download | only in os
      1 /*
      2 Copyright 1987, 1998  The Open Group
      3 
      4 Permission to use, copy, modify, distribute, and sell this software and its
      5 documentation for any purpose is hereby granted without fee, provided that
      6 the above copyright notice appear in all copies and that both that
      7 copyright notice and this permission notice appear in supporting
      8 documentation.
      9 
     10 The above copyright notice and this permission notice shall be included in
     11 all copies or substantial portions of the Software.
     12 
     13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     16 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     17 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     19 
     20 Except as contained in this notice, the name of The Open Group shall not be
     21 used in advertising or otherwise to promote the sale, use or other dealings
     22 in this Software without prior written authorization from The Open Group.
     23  * Copyright 1990, 1991 Network Computing Devices;
     24  * Portions Copyright 1987 by Digital Equipment Corporation
     25  *
     26  * Permission to use, copy, modify, distribute, and sell this software and its
     27  * documentation for any purpose is hereby granted without fee, provided that
     28  * the above copyright notice appear in all copies and that both that
     29  * copyright notice and this permission notice appear in supporting
     30  * documentation, and that the names of Network Computing Devices,
     31  * or Digital not be used in advertising or
     32  * publicity pertaining to distribution of the software without specific,
     33  * written prior permission.  Network Computing Devices, or Digital
     34  * make no representations about the
     35  * suitability of this software for any purpose.  It is provided "as is"
     36  * without express or implied warranty.
     37  *
     38  * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
     39  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
     40  * AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES, OR DIGITAL BE
     41  * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     42  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
     43  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     44  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     45  *
     46  */
     47 
     48 /*
     49  * this is miscellaneous OS specific stuff.
     50  *
     51  * Catalogue support, alternate servers, and cloneing
     52  */
     53 
     54 #include "config.h"
     55 
     56 #include <X11/Xtrans/Xtrans.h>
     57 #include "osstruct.h"
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #define  XK_LATIN1
     61 #include <X11/keysymdef.h>
     62 #include "globals.h"
     63 #include "osdep.h"
     64 
     65 Bool        drone_server = FALSE;
     66 
     67 static int  num_alts;
     68 static AlternateServerPtr alt_servers = (AlternateServerPtr) 0;
     69 
     70 /*
     71  * XXX
     72  *
     73  * Catalogue support is absolutely minimal.  Some guts are here, but
     74  * we don't actually do anything with them so the only one exported is
     75  * 'all'.  Be warned that other parts of the server may incorrectly
     76  * assume the catalogue list is global, and will therefore need fixing.
     77  *
     78  */
     79 
     80 static const char *catalogue_name = "all";
     81 
     82 static Bool			/* stolen from R4 Match() */
     83 pattern_match(const char *pat, int plen, const char *string)
     84 {
     85     register int i,
     86                 l;
     87     int         j,
     88                 m,
     89                 res;
     90     register char cp,
     91                 cs;
     92     int         head,
     93                 tail;
     94 
     95     head = 0;
     96     tail = plen;
     97 
     98     res = -1;
     99     for (i = 0; i < head; i++) {
    100 	cp = pat[i];
    101 	if (cp == XK_question) {
    102 	    if (!string[i])
    103 		return res;
    104 	    res = 0;
    105 	} else if (cp != string[i])
    106 	    return res;
    107     }
    108     if (head == plen)
    109 	return (string[head] ? res : 1);
    110     l = head;
    111     while (++i < tail) {
    112 	/* we just skipped an asterisk */
    113 	j = i;
    114 	m = l;
    115 	while ((cp = pat[i]) != XK_asterisk) {
    116 	    if (!(cs = string[l]))
    117 		return 0;
    118 	    if ((cp != cs) && (cp != XK_question)) {
    119 		m++;
    120 		cp = pat[j];
    121 		if (cp == XK_asterisk) {
    122 		    if (!string[m])
    123 			return 0;
    124 		} else {
    125 		    while ((cs = string[m]) != cp) {
    126 			if (!cs)
    127 			    return 0;
    128 			m++;
    129 		    }
    130 		}
    131 		l = m;
    132 		i = j;
    133 	    }
    134 	    l++;
    135 	    i++;
    136 	}
    137     }
    138     m = strlen(&string[l]);
    139     j = plen - tail;
    140     if (m < j)
    141 	return 0;
    142     l = (l + m) - j;
    143     while ((cp = pat[i])) {
    144 	if ((cp != string[l]) && (cp != XK_question))
    145 	    return 0;
    146 	l++;
    147 	i++;
    148     }
    149     return 1;
    150 }
    151 
    152 int
    153 ListCatalogues(const char *pattern, int patlen, int maxnames,
    154 	       char **catalogues, int *len)
    155 {
    156     int         count = 0;
    157     char       *catlist = NULL;
    158     int         size = 0;
    159 
    160     if (maxnames) {
    161 	if (pattern_match(pattern, patlen, catalogue_name)) {
    162 	    size = strlen(catalogue_name);
    163 	    catlist = (char *) FSalloc(size + 1);
    164 	    if (!catlist)
    165 		goto bail;
    166 	    *catlist = size;
    167 	    memcpy(&catlist[1], catalogue_name, size);
    168 	    size++;		/* for length */
    169 	    count++;
    170 	}
    171     }
    172 bail:
    173     *len = size;
    174     *catalogues = catlist;
    175     return count;
    176 }
    177 
    178 /*
    179  * check if catalogue list is valid
    180  */
    181 
    182 int
    183 ValidateCatalogues(int *num, char *cats)
    184 {
    185     char       *c = cats;
    186     int         i,
    187                 len;
    188 
    189     for (i = 0; i < *num; i++) {
    190 	len = *c++;
    191 	if (strncmp(c, catalogue_name, len)) {
    192 	    *num = i;		/* return bad entry index */
    193 	    return FSBadName;
    194 	}
    195 	c += len;
    196     }
    197     return FSSuccess;
    198 }
    199 
    200 int
    201 SetAlternateServers(char *list)
    202 {
    203     char       *t,
    204                *st;
    205     AlternateServerPtr alts,
    206                 a;
    207     int         num,
    208                 i;
    209 
    210     t = list;
    211     num = 1;
    212     while (*t) {
    213 	if (*t == ',')
    214 	    num++;
    215 	t++;
    216     }
    217 
    218     a = alts = (AlternateServerPtr) FScalloc(num, sizeof(AlternateServerRec));
    219     if (!alts)
    220 	return FSBadAlloc;
    221 
    222     st = t = list;
    223     a->namelen = 0;
    224     while (*t) {
    225 	if (*t == ',') {
    226 	    a->name = (char *) FSalloc(a->namelen);
    227 	    if (!a->name) {
    228 		goto unwind;
    229 	    }
    230 	    memcpy(a->name, st, a->namelen);
    231 	    a->subset = FALSE;	/* XXX */
    232 	    a++;
    233 	    t++;
    234 	    st = t;
    235 	    a->namelen = 0;
    236 	} else {
    237 	    a->namelen++;
    238 	    t++;
    239 	}
    240     }
    241     a->name = (char *) FSalloc(a->namelen);
    242     if (!a->name) {
    243 	goto unwind;
    244     }
    245     memcpy(a->name, st, a->namelen);
    246     a->subset = FALSE;		/* XXX */
    247 
    248     for (i = 0; i < num_alts; i++) {
    249 	FSfree((char *) alt_servers[i].name);
    250     }
    251     FSfree((char *) alt_servers);
    252     num_alts = num;
    253     alt_servers = alts;
    254     return FSSuccess;
    255 
    256   unwind:
    257     for (i = 0; i < num; i++) {
    258 	FSfree(alts[i].name);
    259     }
    260     FSfree(alts);
    261     return FSBadAlloc;
    262 }
    263 
    264 int
    265 ListAlternateServers(AlternateServerPtr *svrs)
    266 {
    267     *svrs = alt_servers;
    268     return num_alts;
    269 }
    270 
    271 /*
    272  * here's some fun stuff.  in order to cleanly handle becoming overloaded,
    273  * this allows us to clone ourselves.  the parent keeps the Listen
    274  * socket open, and sends it to itself.  the child stops listening,
    275  * and becomes a drone, hanging out till it loses all its clients.
    276  */
    277 
    278 int
    279 CloneMyself(void)
    280 {
    281     int         child;
    282     int         i, j;
    283     int         lastfdesc;
    284 
    285     assert(!drone_server);	/* a drone shouldn't hit this */
    286 
    287     if (!CloneSelf)
    288 	return -1;
    289 
    290     lastfdesc = sysconf(_SC_OPEN_MAX) - 1;
    291     if ( (lastfdesc < 0) || (lastfdesc > MAXSOCKS)) {
    292 	lastfdesc = MAXSOCKS;
    293     }
    294 
    295     NoticeF("attempting clone...\n");
    296     chdir("/");
    297     child = fork();
    298     if (child == -1) {
    299 	/* failed to fork */
    300 	ErrorF("clone failed to fork()\n");
    301 	return -1;
    302     }
    303     /*
    304      * Note:  they still share the same process group, and killing the parent
    305      * will take out all the kids as well.  this is considered a feature (at
    306      * least until i'm convinced otherwise)
    307      */
    308     if (child == 0) {
    309 	StopListening();
    310 	NoticeF("clone: child becoming drone\n");
    311 	drone_server = TRUE;
    312 	return 1;
    313     } else {			/* parent */
    314 	char	old_listen_arg[256];
    315 	char	portnum[8];
    316 
    317 	NoticeF("clone: parent revitalizing as %s\n", progname);
    318 	CloseErrors();
    319 	/* XXX should we close stdio as well? */
    320 	for (i = 3; i < lastfdesc; i++)
    321 	{
    322 	    for (j = 0; j < ListenTransCount; j++)
    323 		if (ListenTransFds[j] == i)
    324 		    break;
    325 
    326 	    if (j >= ListenTransCount)
    327 		(void) close(i);
    328 	}
    329 
    330 	old_listen_arg[0] = '\0';
    331 
    332 	for (i = 0; i < ListenTransCount; i++)
    333 	{
    334 	    int trans_id, fd;
    335 	    char *port;
    336 	    size_t arg_len;
    337 
    338 	    if (!_FontTransGetReopenInfo (ListenTransConns[i],
    339 		&trans_id, &fd, &port))
    340 		continue;
    341 
    342 	    arg_len = strlen(old_listen_arg);
    343 	    if (arg_len < sizeof(old_listen_arg)) {
    344 		char *arg_ptr = old_listen_arg + arg_len;
    345 		size_t actual_len;
    346 		actual_len = snprintf (arg_ptr, sizeof(old_listen_arg) - arg_len,
    347 				       "%s%d/%d/%s", (arg_len > 0) ? "," : "",
    348 				       trans_id, fd, port);
    349 		/* Ensure we don't leave a partial address if we ran out of
    350 		   room in the buffer */
    351 		if (actual_len >= (sizeof(old_listen_arg) - arg_len))
    352 		    *arg_ptr = '\0';
    353 	    }
    354 	    free (port);
    355 	}
    356 
    357 	snprintf (portnum, sizeof(portnum), "%d", ListenPort);
    358 	if (*old_listen_arg != '\0')
    359 	    execlp(progname, progname,
    360 		   "-ls", old_listen_arg,
    361 		   "-cf", configfilename,
    362 		   "-port", portnum,
    363 		   (void *)NULL);
    364 
    365 	InitErrors();		/* reopen errors, since we don't want to lose
    366 				 * this */
    367 	Error("clone failed");
    368 	FatalError("failed to clone self\n");
    369     }
    370     /* NOTREACHED */
    371     return 0;
    372 }
    373