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