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); 163e1db7cd1Smrg catlist = (char *) FSalloc(size + 1); 164bbe1b32bSmrg if (!catlist) 165bbe1b32bSmrg goto bail; 166bbe1b32bSmrg *catlist = size; 16776028eb6Smrg memcpy(&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 218e1db7cd1Smrg a = alts = (AlternateServerPtr) FScalloc(num, sizeof(AlternateServerRec)); 219bbe1b32bSmrg if (!alts) 220bbe1b32bSmrg return FSBadAlloc; 221bbe1b32bSmrg 222bbe1b32bSmrg st = t = list; 223bbe1b32bSmrg a->namelen = 0; 224bbe1b32bSmrg while (*t) { 225bbe1b32bSmrg if (*t == ',') { 226e1db7cd1Smrg a->name = (char *) FSalloc(a->namelen); 227bbe1b32bSmrg if (!a->name) { 22876028eb6Smrg goto unwind; 229bbe1b32bSmrg } 23076028eb6Smrg memcpy(a->name, st, a->namelen); 231bbe1b32bSmrg a->subset = FALSE; /* XXX */ 232bbe1b32bSmrg a++; 233bbe1b32bSmrg t++; 234bbe1b32bSmrg st = t; 235bbe1b32bSmrg a->namelen = 0; 236bbe1b32bSmrg } else { 237bbe1b32bSmrg a->namelen++; 238bbe1b32bSmrg t++; 239bbe1b32bSmrg } 240bbe1b32bSmrg } 241e1db7cd1Smrg a->name = (char *) FSalloc(a->namelen); 242bbe1b32bSmrg if (!a->name) { 24376028eb6Smrg goto unwind; 244bbe1b32bSmrg } 24576028eb6Smrg memcpy(a->name, st, a->namelen); 246bbe1b32bSmrg a->subset = FALSE; /* XXX */ 247bbe1b32bSmrg 248bbe1b32bSmrg for (i = 0; i < num_alts; i++) { 249e1db7cd1Smrg FSfree((char *) alt_servers[i].name); 250bbe1b32bSmrg } 251e1db7cd1Smrg FSfree((char *) alt_servers); 252bbe1b32bSmrg num_alts = num; 253bbe1b32bSmrg alt_servers = alts; 254bbe1b32bSmrg return FSSuccess; 25576028eb6Smrg 25676028eb6Smrg unwind: 25776028eb6Smrg for (i = 0; i < num; i++) { 258e1db7cd1Smrg FSfree(alts[i].name); 25976028eb6Smrg } 260e1db7cd1Smrg FSfree(alts); 26176028eb6Smrg return FSBadAlloc; 262bbe1b32bSmrg} 263bbe1b32bSmrg 264bbe1b32bSmrgint 265bbe1b32bSmrgListAlternateServers(AlternateServerPtr *svrs) 266bbe1b32bSmrg{ 267bbe1b32bSmrg *svrs = alt_servers; 268bbe1b32bSmrg return num_alts; 269bbe1b32bSmrg} 270bbe1b32bSmrg 271bbe1b32bSmrg/* 272bbe1b32bSmrg * here's some fun stuff. in order to cleanly handle becoming overloaded, 273bbe1b32bSmrg * this allows us to clone ourselves. the parent keeps the Listen 274bbe1b32bSmrg * socket open, and sends it to itself. the child stops listening, 275bbe1b32bSmrg * and becomes a drone, hanging out till it loses all its clients. 276bbe1b32bSmrg */ 277bbe1b32bSmrg 278bbe1b32bSmrgint 279bbe1b32bSmrgCloneMyself(void) 280bbe1b32bSmrg{ 281bbe1b32bSmrg int child; 282bbe1b32bSmrg int i, j; 283bbe1b32bSmrg int lastfdesc; 284bbe1b32bSmrg 285bbe1b32bSmrg assert(!drone_server); /* a drone shouldn't hit this */ 286bbe1b32bSmrg 287bbe1b32bSmrg if (!CloneSelf) 288bbe1b32bSmrg return -1; 289bbe1b32bSmrg 290bbe1b32bSmrg lastfdesc = sysconf(_SC_OPEN_MAX) - 1; 291ce6676dbSmrg if ( (lastfdesc < 0) || (lastfdesc > MAXSOCKS)) { 292ce6676dbSmrg lastfdesc = MAXSOCKS; 293ce6676dbSmrg } 294bbe1b32bSmrg 295bbe1b32bSmrg NoticeF("attempting clone...\n"); 296bbe1b32bSmrg chdir("/"); 297bbe1b32bSmrg child = fork(); 298bbe1b32bSmrg if (child == -1) { 299bbe1b32bSmrg /* failed to fork */ 300bbe1b32bSmrg ErrorF("clone failed to fork()\n"); 301bbe1b32bSmrg return -1; 302bbe1b32bSmrg } 303bbe1b32bSmrg /* 304bbe1b32bSmrg * Note: they still share the same process group, and killing the parent 305bbe1b32bSmrg * will take out all the kids as well. this is considered a feature (at 306bbe1b32bSmrg * least until i'm convinced otherwise) 307bbe1b32bSmrg */ 308bbe1b32bSmrg if (child == 0) { 309bbe1b32bSmrg StopListening(); 310bbe1b32bSmrg NoticeF("clone: child becoming drone\n"); 311bbe1b32bSmrg drone_server = TRUE; 312bbe1b32bSmrg return 1; 313bbe1b32bSmrg } else { /* parent */ 3148f34cbf9Ssnj char old_listen_arg[256]; 3158f34cbf9Ssnj char portnum[8]; 3168f34cbf9Ssnj 317bbe1b32bSmrg NoticeF("clone: parent revitalizing as %s\n", progname); 318bbe1b32bSmrg CloseErrors(); 319bbe1b32bSmrg /* XXX should we close stdio as well? */ 320bbe1b32bSmrg for (i = 3; i < lastfdesc; i++) 321bbe1b32bSmrg { 322bbe1b32bSmrg for (j = 0; j < ListenTransCount; j++) 323bbe1b32bSmrg if (ListenTransFds[j] == i) 324bbe1b32bSmrg break; 325bbe1b32bSmrg 326bbe1b32bSmrg if (j >= ListenTransCount) 327bbe1b32bSmrg (void) close(i); 328bbe1b32bSmrg } 329bbe1b32bSmrg 3308f34cbf9Ssnj old_listen_arg[0] = '\0'; 3318f34cbf9Ssnj 332bbe1b32bSmrg for (i = 0; i < ListenTransCount; i++) 333bbe1b32bSmrg { 334bbe1b32bSmrg int trans_id, fd; 335bbe1b32bSmrg char *port; 3368f34cbf9Ssnj size_t arg_len; 337bbe1b32bSmrg 338bbe1b32bSmrg if (!_FontTransGetReopenInfo (ListenTransConns[i], 339bbe1b32bSmrg &trans_id, &fd, &port)) 340bbe1b32bSmrg continue; 341bbe1b32bSmrg 3428f34cbf9Ssnj arg_len = strlen(old_listen_arg); 3438f34cbf9Ssnj if (arg_len < sizeof(old_listen_arg)) { 3448f34cbf9Ssnj char *arg_ptr = old_listen_arg + arg_len; 3458f34cbf9Ssnj size_t actual_len; 3468f34cbf9Ssnj actual_len = snprintf (arg_ptr, sizeof(old_listen_arg) - arg_len, 3478f34cbf9Ssnj "%s%d/%d/%s", (arg_len > 0) ? "," : "", 3488f34cbf9Ssnj trans_id, fd, port); 3498f34cbf9Ssnj /* Ensure we don't leave a partial address if we ran out of 3508f34cbf9Ssnj room in the buffer */ 3518f34cbf9Ssnj if (actual_len >= (sizeof(old_listen_arg) - arg_len)) 3528f34cbf9Ssnj *arg_ptr = '\0'; 353bbe1b32bSmrg } 3548f34cbf9Ssnj free (port); 355bbe1b32bSmrg } 356bbe1b32bSmrg 3578f34cbf9Ssnj snprintf (portnum, sizeof(portnum), "%d", ListenPort); 358bbe1b32bSmrg if (*old_listen_arg != '\0') 359bbe1b32bSmrg execlp(progname, progname, 360bbe1b32bSmrg "-ls", old_listen_arg, 361bbe1b32bSmrg "-cf", configfilename, 362bbe1b32bSmrg "-port", portnum, 363bbe1b32bSmrg (void *)NULL); 364bbe1b32bSmrg 365bbe1b32bSmrg InitErrors(); /* reopen errors, since we don't want to lose 366bbe1b32bSmrg * this */ 367bbe1b32bSmrg Error("clone failed"); 368bbe1b32bSmrg FatalError("failed to clone self\n"); 369bbe1b32bSmrg } 370bbe1b32bSmrg /* NOTREACHED */ 371bbe1b32bSmrg return 0; 372bbe1b32bSmrg} 373