osglue.c revision 8f34cbf9
1/* 2Copyright 1987, 1998 The Open Group 3 4Permission to use, copy, modify, distribute, and sell this software and its 5documentation for any purpose is hereby granted without fee, provided that 6the above copyright notice appear in all copies and that both that 7copyright notice and this permission notice appear in supporting 8documentation. 9 10The above copyright notice and this permission notice shall be included in 11all copies or substantial portions of the Software. 12 13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 17AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 20Except as contained in this notice, the name of The Open Group shall not be 21used in advertising or otherwise to promote the sale, use or other dealings 22in 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 65Bool drone_server = FALSE; 66 67static int num_alts; 68static 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 80static const char *catalogue_name = "all"; 81 82static Bool /* stolen from R4 Match() */ 83pattern_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 152int 153ListCatalogues(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 memmove( &catlist[1], catalogue_name, size); 168 size++; /* for length */ 169 count++; 170 } 171 } 172bail: 173 *len = size; 174 *catalogues = catlist; 175 return count; 176} 177 178/* 179 * check if catalogue list is valid 180 */ 181 182int 183ValidateCatalogues(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 200int 201SetAlternateServers(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) fsalloc(sizeof(AlternateServerRec) * num); 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 /* XXX -- leak */ 229 return FSBadAlloc; 230 } 231 memmove( a->name, st, a->namelen); 232 a->subset = FALSE; /* XXX */ 233 a++; 234 t++; 235 st = t; 236 a->namelen = 0; 237 } else { 238 a->namelen++; 239 t++; 240 } 241 } 242 a->name = (char *) fsalloc(a->namelen); 243 if (!a->name) { 244 /* XXX -- leak */ 245 return FSBadAlloc; 246 } 247 memmove( a->name, st, a->namelen); 248 a->subset = FALSE; /* XXX */ 249 250 for (i = 0; i < num_alts; i++) { 251 fsfree((char *) alt_servers[i].name); 252 } 253 fsfree((char *) alt_servers); 254 num_alts = num; 255 alt_servers = alts; 256 return FSSuccess; 257} 258 259int 260ListAlternateServers(AlternateServerPtr *svrs) 261{ 262 *svrs = alt_servers; 263 return num_alts; 264} 265 266/* 267 * here's some fun stuff. in order to cleanly handle becoming overloaded, 268 * this allows us to clone ourselves. the parent keeps the Listen 269 * socket open, and sends it to itself. the child stops listening, 270 * and becomes a drone, hanging out till it loses all its clients. 271 */ 272 273int 274CloneMyself(void) 275{ 276 int child; 277 int i, j; 278 int lastfdesc; 279 280 assert(!drone_server); /* a drone shouldn't hit this */ 281 282 if (!CloneSelf) 283 return -1; 284 285 lastfdesc = sysconf(_SC_OPEN_MAX) - 1; 286 if ( (lastfdesc < 0) || (lastfdesc > MAXSOCKS)) { 287 lastfdesc = MAXSOCKS; 288 } 289 290 NoticeF("attempting clone...\n"); 291 chdir("/"); 292 child = fork(); 293 if (child == -1) { 294 /* failed to fork */ 295 ErrorF("clone failed to fork()\n"); 296 return -1; 297 } 298 /* 299 * Note: they still share the same process group, and killing the parent 300 * will take out all the kids as well. this is considered a feature (at 301 * least until i'm convinced otherwise) 302 */ 303 if (child == 0) { 304 StopListening(); 305 NoticeF("clone: child becoming drone\n"); 306 drone_server = TRUE; 307 return 1; 308 } else { /* parent */ 309 char old_listen_arg[256]; 310 char portnum[8]; 311 312 NoticeF("clone: parent revitalizing as %s\n", progname); 313 CloseErrors(); 314 /* XXX should we close stdio as well? */ 315 for (i = 3; i < lastfdesc; i++) 316 { 317 for (j = 0; j < ListenTransCount; j++) 318 if (ListenTransFds[j] == i) 319 break; 320 321 if (j >= ListenTransCount) 322 (void) close(i); 323 } 324 325 old_listen_arg[0] = '\0'; 326 327 for (i = 0; i < ListenTransCount; i++) 328 { 329 int trans_id, fd; 330 char *port; 331 size_t arg_len; 332 333 if (!_FontTransGetReopenInfo (ListenTransConns[i], 334 &trans_id, &fd, &port)) 335 continue; 336 337 arg_len = strlen(old_listen_arg); 338 if (arg_len < sizeof(old_listen_arg)) { 339 char *arg_ptr = old_listen_arg + arg_len; 340 size_t actual_len; 341 actual_len = snprintf (arg_ptr, sizeof(old_listen_arg) - arg_len, 342 "%s%d/%d/%s", (arg_len > 0) ? "," : "", 343 trans_id, fd, port); 344 /* Ensure we don't leave a partial address if we ran out of 345 room in the buffer */ 346 if (actual_len >= (sizeof(old_listen_arg) - arg_len)) 347 *arg_ptr = '\0'; 348 } 349 free (port); 350 } 351 352 snprintf (portnum, sizeof(portnum), "%d", ListenPort); 353 if (*old_listen_arg != '\0') 354 execlp(progname, progname, 355 "-ls", old_listen_arg, 356 "-cf", configfilename, 357 "-port", portnum, 358 (void *)NULL); 359 360 InitErrors(); /* reopen errors, since we don't want to lose 361 * this */ 362 Error("clone failed"); 363 FatalError("failed to clone self\n"); 364 } 365 /* NOTREACHED */ 366 return 0; 367} 368