1
/*********************************************************
2
* Copyright (C) 2007 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published
6
* by the Free Software Foundation version 2.1 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11
* License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*********************************************************/
23
* Provides the QueryLocationsDB routine for finding keys in the locations
24
* database. Because our application is a setuid binary and we want to
25
* minimize risk, we retain the duplicated functionality here rather than
26
* link against lib/unixinstall.
29
#if !defined(sun) && !defined(__FreeBSD__) && !defined(linux)
30
# error This program is not supported on your platform.
33
#include <sys/param.h>
39
#include "vm_basic_types.h"
49
* Mappings between queries and search strings
52
typedef struct Mapping {
53
const char *answer; /* string to match for "answer FOO" */
54
const char *remove; /* string to match for "remove_answer FOO" */
55
size_t answerSize; /* size of answer buffer */
56
size_t removeSize; /* size of remove buffer */
60
* queryMappings between Selector => search strings. Used by QueryLocationsDB.
63
#define ANSWER_LIBDIR "answer LIBDIR"
64
#define REMOVE_LIBDIR "remove_answer LIBDIR"
65
#define ANSWER_BINDIR "answer BINDIR"
66
#define REMOVE_BINDIR "remove_answer BINDIR"
68
static Mapping queryMappings[] = {
69
{ ANSWER_LIBDIR, REMOVE_LIBDIR, sizeof ANSWER_LIBDIR, sizeof REMOVE_LIBDIR },
70
{ ANSWER_BINDIR, REMOVE_BINDIR, sizeof ANSWER_BINDIR, sizeof REMOVE_BINDIR },
76
* Global functions (definitions)
81
*----------------------------------------------------------------------------
85
* Based on the caller's Selector, determines the directory selected as
86
* "LIBDIR", "BINDIR", etc. when the Tools were last configured.
89
* TRUE on success, FALSE on failure. queryDir is filled in on success.
94
*----------------------------------------------------------------------------
98
QueryLocationsDB(const char *locations, // IN : path of locations database
99
Selector selector, // IN : DB query to search for
100
char *queryDir, // OUT: address to write dirname to
101
size_t queryDirSize) // IN : size of queryDir buffer
104
char buf[MAXPATHLEN];
108
if (selector < 0 || selector >= QUERY_MAX) {
109
Error("Internal logic error. This is a bug.");
113
file = fopen(locations, "r");
118
map = &queryMappings[selector];
121
* We need to inspect the entire locations database since there are both
122
* "answer"s and "remove_answer"s. We want to provide the last answer that
123
* has not been removed.
125
while (fgets(buf, sizeof buf, file)) {
126
if (strncmp(buf, map->answer, map->answerSize - 1) == 0) {
129
strncpy(queryDir, buf + map->answerSize, queryDirSize);
130
if (queryDir[queryDirSize - 1] != '\0') {
135
/* Truncate the string at the newline character, if it's present. */
136
newline = strchr(queryDir, '\n');
137
if (newline && newline - queryDir < queryDirSize) {
142
} else if (strncmp(buf, map->remove, map->removeSize - 1) == 0) {