2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $TOG: DtsMM.c /main/16 1998/10/23 13:48:28 mgreess $
26 * RESTRICTED CONFIDENTIAL INFORMATION:
28 * (c) Copyright 1993,1994,1995 Sun Microsystems, Inc.
29 * All rights reserved.
35 #include <sys/types.h>
40 #include <sys/utsname.h>
43 #include <sys/param.h>
46 #define X_INCLUDE_DIRENT_H
47 #define XOS_USE_XT_LOCKING
48 #include <X11/Xos_r.h>
49 #include <Dt/DbReader.h>
51 #include "Dt/DtNlUtils.h"
52 #include <Dt/UserMsg.h>
53 #include "DtSvcLock.h"
55 extern char *strdup(const char *);
56 static int MMValidateDb(DtDirPaths *dirs, char *suffix);
57 static int _debug_print_name(char *name, char *label);
59 typedef int (*genfunc)(const void *, const void *);
61 static DtDtsMMDatabase *db_list;
62 static caddr_t mmaped_db = 0;
63 static size_t mmaped_size = 0;
64 static int mmaped_fd = 0;
65 static DtDtsMMHeader *head = 0;
68 _DtDtsMMGetPtr(int index)
70 DtShmIntList int_list;
77 int_list = (DtShmIntList)&mmaped_db[sizeof(DtDtsMMHeader)];
78 _DtSvcProcessUnlock();
79 return((void *)&int_list[index]);
83 _DtDtsMMGetPtrSize(int index)
85 DtShmIntList int_list;
92 int_list = (DtShmIntList)&mmaped_db[sizeof(DtDtsMMHeader)];
93 _DtSvcProcessUnlock();
94 return(int_list[index-1]);
98 _DtDtsMMGetDCNameIndex(int *size)
103 *size = _DtDtsMMGetPtrSize(head->name_list_offset);
104 result = (int*) _DtDtsMMGetPtr(head->name_list_offset);
105 _DtSvcProcessUnlock();
110 _DtDtsMMGetDbName(DtDtsMMDatabase *db, DtShmBoson boson)
112 DtShmInttab tab = (DtShmInttab)_DtDtsMMGetPtr(db->nameIndex);
113 return((int *)_DtShmFindIntTabEntry(tab, boson));
117 _DtDtsMMGetNoNameIndex(int *size)
123 if(head->no_name_offset == -1)
126 _DtSvcProcessUnlock();
129 *size = _DtDtsMMGetPtrSize(head->no_name_offset);
130 result = (int *) _DtDtsMMGetPtr(head->no_name_offset);
131 _DtSvcProcessUnlock();
135 /* returns the pointer to buffer only name list */
137 _DtDtsMMGetBufferIndex(int *size)
139 int *list = (int*)_DtDtsMMGetNoNameIndex(size);
143 *size -= head->buffer_start_index;
144 bufferIndex = &list[head->buffer_start_index];
145 _DtSvcProcessUnlock();
151 _DtDtsMMGetFileList(void)
153 DtShmInttab file_index;
156 file_index = (DtShmStrtab)_DtDtsMMGetPtr(head->files_offset);
157 _DtSvcProcessUnlock();
162 _DtDtsMMBosonToString(DtShmBoson boson)
164 DtShmStrtab str_table;
175 str_table = (DtShmStrtab)_DtDtsMMGetPtr(head->str_tbl_offset);
176 _DtSvcProcessUnlock();
178 return(_DtShmBosonToString(str_table, boson));
182 _DtDtsMMStringToBoson(const char *string)
184 DtShmStrtab str_table;
186 if ((string == (char *)NULL) || (*string == '\0'))
195 str_table = (DtShmStrtab)_DtDtsMMGetPtr(head->str_tbl_offset);
196 _DtSvcProcessUnlock();
198 return(_DtShmStringToBoson(str_table, string));
202 _DtDtsMMPrintFld(int fld, DtDtsMMField *fld_ptr, FILE *fd_in)
210 tmp = _DtDtsMMBosonToString(fld_ptr->fieldName);
211 tmpv = _DtDtsMMBosonToString(fld_ptr->fieldValue);
212 fprintf(fd, "\t\t[%d]\t%s(%d)\t%s(%d)\n", fld, tmp,fld_ptr->fieldName,
213 tmpv?tmpv:"(NULL)", fld_ptr->fieldValue);
217 _DtDtsMMPrintRec(int rec, DtDtsMMRecord *rec_ptr, FILE *fd_in)
220 DtDtsMMField *fld_ptr;
221 DtDtsMMField *fld_ptr_list;
227 tmp = _DtDtsMMBosonToString(rec_ptr->recordName);
228 fprintf(fd, "\tRec[%d] name = %s(%d)\n\t%d Fields\n", rec,
229 tmp, rec_ptr->recordName,
230 rec_ptr->fieldCount);
231 fld_ptr_list = _DtDtsMMGetPtr(rec_ptr->fieldList);
232 for(fld = 0; fld < rec_ptr->fieldCount; fld++)
234 fld_ptr = &fld_ptr_list[fld];
235 _DtDtsMMPrintFld(fld, fld_ptr, fd);
240 _DtDtsMMPrintDb(int db, DtDtsMMDatabase *db_ptr, FILE *fd_in)
243 DtDtsMMRecord *rec_ptr;
244 DtDtsMMRecord *rec_ptr_list;
250 fprintf(fd, "DB[%d] ", db);
251 tmp = _DtDtsMMBosonToString(db_ptr->databaseName);
252 fprintf(fd, "name = %s(%d)\n", tmp, db_ptr->databaseName);
253 fprintf(fd, "%d Records\n", db_ptr->recordCount);
254 rec_ptr_list = _DtDtsMMGetPtr(db_ptr->recordList);
255 for(rec = 0; rec < db_ptr->recordCount; rec++)
257 rec_ptr = &rec_ptr_list[rec];
258 _DtDtsMMPrintRec(rec, rec_ptr, fd);
263 _DtDtsMMPrint(FILE *org_fd)
266 DtDtsMMDatabase *db_ptr;
276 for(db = 0; db < head->num_db; db++)
278 db_ptr = &db_list[db];
282 tmp = _DtDtsMMBosonToString(db_ptr->databaseName);
283 if((fd = fopen(tmp, "w")) == NULL)
286 DtProgName, DtError, NULL,
291 _DtDtsMMPrintDb(db, db_ptr, fd);
298 _DtSvcProcessUnlock();
302 _DtDtsMMCompareRecordNames(DtDtsMMRecord *a, DtDtsMMRecord *b)
304 return (a->recordName - b->recordName);
308 _DtDtsMMCompareFieldNames(DtDtsMMField *a, DtDtsMMField *b)
310 return (a->fieldName - b->fieldName);
316 _DtDtsMMInit(int override)
318 DtDirPaths *dirs = _DtGetDatabaseDirPaths();
319 char *CacheFile = _DtDtsMMCacheName(1);
322 if (!_DtDtsMMCreateDb(dirs, CacheFile, override))
325 _DtFreeDatabaseDirPaths(dirs);
328 _debug_print_name(CacheFile, "Init");
332 int success = _DtDtsMMapDB(CacheFile);
335 if(!MMValidateDb(dirs, ".dt"))
341 _debug_print_name(CacheFile, "Mapped");
347 CacheFile = _DtDtsMMCacheName(0);
348 _debug_print_name(CacheFile, "Private");
349 /* Check return status, and pass status to caller. */
350 if (!_DtDtsMMCreateDb(dirs, CacheFile, override))
353 _DtFreeDatabaseDirPaths(dirs);
359 _DtFreeDatabaseDirPaths(dirs);
375 list = (char **)malloc((head->num_db+1)*sizeof(char *));
376 for ( i = 0; i < head->num_db; i++ )
378 list[i] = (char *)_DtDtsMMBosonToString(db_list[i].databaseName);
381 _DtSvcProcessUnlock();
387 _DtDtsMMGet(const char *name)
390 DtShmBoson boson = _DtDtsMMStringToBoson(name);
391 DtDtsMMDatabase *ret_db;
398 for(i = 0; i < head->num_db; i++)
400 if(db_list[i].databaseName == boson)
402 ret_db = &db_list[i];
403 _DtSvcProcessUnlock();
408 _DtSvcProcessUnlock();
414 _DtDtsMMGetField(DtDtsMMRecord *rec, const char *name)
418 DtDtsMMField *fld_ptr;
419 DtDtsMMField *fld_ptr_list;
422 * Field names have been quarked so quark 'name' and
423 * do a linear search for the quark'ed field name.
425 DtShmBoson tmp = _DtDtsMMStringToBoson (name);
427 fld_ptr_list = _DtDtsMMGetPtr(rec->fieldList);
428 for (i = 0; i < rec->fieldCount; i++)
430 fld_ptr = &fld_ptr_list[i];
431 if (fld_ptr->fieldName == tmp)
440 _DtDtsMMGetFieldByName(DtDtsMMRecord *rec, const char *name)
442 DtDtsMMField *result;
444 result = _DtDtsMMGetField(rec, name);
447 return(_DtDtsMMBosonToString(result->fieldValue));
457 _DtDtsMMGetRecordByName(DtDtsMMDatabase *db, const char *name)
460 DtDtsMMRecord *result;
461 DtDtsMMRecord *s = &srch;
463 DtShmBoson name_quark = _DtDtsMMStringToBoson(name);
464 DtDtsMMRecord *rec_ptr;
465 DtDtsMMRecord *rec_ptr_list;
468 * If the fields are not sorted in alphanumeric order
469 * by name a binary search will fail. So do the slow but
470 * sure linear search.
472 rec_ptr_list = _DtDtsMMGetPtr(db->recordList);
474 for (i = 0; i < db->recordCount; i++)
476 rec_ptr = &rec_ptr_list[i];
477 if (rec_ptr->recordName == name_quark)
485 _DtDtsMMPathHash(DtDirPaths *dirs)
489 struct dirent *dp = NULL;
494 char *suffix = ".dt";
496 char *cur_dir = getcwd(0,MAXPATHLEN);
499 _Xreaddirparams dirEntryBuf;
500 struct dirent *result;
502 for(i = 0; dirs->paths[i] ; i++)
504 if(chdir(dirs->paths[i]) == -1)
508 dirp = opendir (".");
509 while ((result = _XReaddir(dirp, dirEntryBuf)) != NULL)
511 if ((int)strlen (result->d_name) >= (int)strlen(suffix))
513 suffixLen = DtCharCount(suffix);
514 nameLen = DtCharCount(result->d_name);
515 file_suffix = (char *)_DtGetNthChar(result->d_name,
516 nameLen - suffixLen);
517 stat(result->d_name, &buf);
519 (strcmp(file_suffix, suffix) == 0) &&
520 (buf.st_mode&S_IFREG))
522 char *c = dirs->paths[i];
540 _DtDtsMMCacheName(int override)
542 char *dsp = getenv("DISPLAY");
548 results = malloc(strlen(_DTDTSMMTEMPDIR)+
549 strlen(_DTDTSMMTEMPFILE)+
551 sprintf(results, "%s/%s%s\0",
555 c = strchr(results, ':');
564 /* tempnam(3) is affected by the TMPDIR environment variable. */
565 /* This creates problems for rename() if "tmpfile" and "cacheFile" */
566 /* are on different file systems. Use tmpnam(3) to create the */
567 /* unique file name instead. */
568 char tmpnam_buf[L_tmpnam + 1];
570 results = (char *)malloc(sizeof(_DTDTSMMTEMPDIR) +
571 sizeof(_DTDTSMMTEMPFILE) +
574 sprintf(results, "%s/%s%s", _DTDTSMMTEMPDIR, _DTDTSMMTEMPFILE,
575 basename(tmpnam_buf));
582 _DtDtsMMapDB(const char *CacheFile)
591 /* Already have a file memory-mapped. Unload it. */
595 mmaped_fd = open(CacheFile, O_RDONLY, 0400);
598 if(fstat(mmaped_fd, &buf) == 0 && buf.st_uid == getuid())
600 mmaped_db = (char *)mmap(NULL,
603 #if defined(sun) || defined(USL)
604 /* MAP_NORESERVE is only supported
605 on sun and novell platforms */
606 MAP_SHARED|MAP_NORESERVE,
612 if(mmaped_db != (void *) -1)
615 mmaped_size = buf.st_size;
616 head = (DtDtsMMHeader *)mmaped_db;
617 db_list = (DtDtsMMDatabase *)_DtDtsMMGetPtr(head->db_offset);
622 DtProgName, DtError, NULL,
623 (char*) CacheFile, NULL);
631 _DtSvcProcessUnlock();
636 MMValidateDb(DtDirPaths *dirs, char *suffix)
639 struct dirent *direntp;
642 int size = sizeof(buf.st_mtime);
643 DtShmBoson *boson_list = 0;
648 int pathhash = _DtDtsMMPathHash(dirs);
651 if(head->pathhash != pathhash)
653 _DtSvcProcessUnlock();
657 count = head->files_count;
658 mtime_list = _DtDtsMMGetPtr(head->mtimes_offset);
659 boson_list = _DtDtsMMGetPtr(head->files_offset);
661 for(i = 0; i < count; i++)
663 file = _DtDtsMMBosonToString(boson_list[i]);
665 if(mtime_list[i] != buf.st_mtime)
667 _DtSvcProcessUnlock();
672 _DtSvcProcessUnlock();
678 _DtDtsMMExpandValue(const char *value)
686 newval = (char *)malloc(1024);
688 strcpy(newval, value);
689 _DtDbFillVariables(&newval);
694 _DtDtsMMSafeFree(char *value)
696 if(value && !_DtDtsMMIsMemory(value))
703 _DtDtsMMIsMemory(const char *value)
706 if((caddr_t)value < mmaped_db || (caddr_t)value > mmaped_db+mmaped_size)
708 _DtSvcProcessUnlock();
713 _DtSvcProcessUnlock();
727 _DtSvcProcessUnlock();
730 if(munmap(mmaped_db, mmaped_size) == -1)
732 _DtSimpleError(DtProgName, DtError, NULL,
733 "munmap of dts_cache file", NULL);
736 if(close(mmaped_fd) == -1)
738 _DtSimpleError(DtProgName, DtError, NULL,
739 "close of dts_cache file", NULL);
747 _DtSvcProcessUnlock();
751 #include "Dt/UserMsg.h"
754 _debug_print_name(char *name, char *label)
757 static char *db = (char *)-1;
762 db = getenv("MMAP_DEBUG");
764 _DtSvcProcessUnlock();
770 "%s - db name = %s\n", label,