1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* extensions.c - core analysis suite
*
* Copyright (C) 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002, 2003, 2004 David Anderson
* Copyright (C) 2002, 2003, 2004 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* CVS: $Revision: 1.3 $ $Date: 2004/01/28 17:04:59 $
*/
#include "defs.h"
#include <dlfcn.h>
static void load_extension(char *);
static void unload_extension(char *);
#define DUMP_EXTENSIONS (0)
#define LOAD_EXTENSION (1)
#define UNLOAD_EXTENSION (2)
/*
* Load, unload, or list the extension libaries.
*/
void
cmd_extend(void)
{
int c;
int flag;
flag = DUMP_EXTENSIONS;
while ((c = getopt(argcnt, args, "lu")) != EOF) {
switch(c)
{
case 'l':
if (flag & UNLOAD_EXTENSION) {
error(INFO,
"-l and -u are mutually exclusive\n");
argerrs++;
} else
flag |= LOAD_EXTENSION;
break;
case 'u':
if (flag & LOAD_EXTENSION) {
error(INFO,
"-u and -l are mutually exclusive\n");
argerrs++;
} else
flag |= UNLOAD_EXTENSION;
break;
default:
argerrs++;
break;
}
}
if (argerrs)
cmd_usage(pc->curcmd, SYNOPSIS);
switch (flag)
{
case DUMP_EXTENSIONS:
if (!args[optind]) {
dump_extension_table(!VERBOSE);
return;
}
/* FALLTHROUGH */
case LOAD_EXTENSION:
if (!args[optind]) {
error(INFO,
"-l requires one or more extension library arguments\n");
cmd_usage(pc->curcmd, SYNOPSIS);
break;
}
while (args[optind]) {
load_extension(args[optind]);
optind++;
}
break;
case UNLOAD_EXTENSION:
if (!args[optind]) {
unload_extension(NULL);
break;
}
while (args[optind]) {
unload_extension(args[optind]);
optind++;
}
break;
}
}
/*
* List all extension libaries and their commands in either the extend
* command format or for "help -e" (verbose).
*/
void
dump_extension_table(int verbose)
{
struct extension_table *ext;
struct command_table_entry *cp;
char buf[BUFSIZE];
int longest, others;
if (!extension_table)
return;
if (verbose) {
for (ext = extension_table; ext; ext = ext->next) {
fprintf(fp, " filename: %s\n", ext->filename);
fprintf(fp, " handle: %lx\n", (ulong)ext->handle);
fprintf(fp, "command_table: %lx (",
(ulong)ext->command_table);
for (others = 0, cp = ext->command_table; cp->name;cp++)
fprintf(fp, "%s%s%s", others++ ? " " : "",
cp->name, cp->help_data ? "*" : "");
fprintf(fp, ")\n");
fprintf(fp, " flags: %lx (", ext->flags);
others = 0;
if (ext->flags & REGISTERED)
fprintf(fp, "%sREGISTERED", others++ ?
"|" : "");
fprintf(fp, ")\n");
fprintf(fp, " next: %lx\n", (ulong)ext->next);
fprintf(fp, " prev: %lx\n%s",
(ulong)ext->prev, ext->next ? "\n" : "");
}
return;
}
/*
* Print them out in the order they were loaded.
*/
for (longest = 0, ext = extension_table; ext; ext = ext->next) {
if (strlen(ext->filename) > longest)
longest = strlen(ext->filename);
}
fprintf(fp, "%s COMMANDS\n",
mkstring(buf, longest, LJUST, "SHARED OBJECT"));
longest = MAX(longest, strlen("SHARED OBJECT"));
for (ext = extension_table; ext; ext = ext->next)
if (ext->next == NULL)
break;
do {
fprintf(fp, "%s ",
mkstring(buf, longest, LJUST, ext->filename));
for (cp = ext->command_table; cp->name; cp++)
fprintf(fp, "%s ", cp->name);
fprintf(fp, "\n");
} while ((ext = ext->prev));
}
/*
* Load an extension library.
*/
static void
load_extension(char *lib)
{
struct extension_table *ext;
char buf[BUFSIZE];
size_t size;
for (ext = extension_table; ext; ext = ext->next) {
if (same_file(ext->filename, lib)) {
fprintf(fp, "%s: shared object already loaded\n", lib);
return;
}
}
size = sizeof(struct extension_table) + strlen(lib) + strlen("./") + 1;
if ((ext = (struct extension_table *)malloc(size)) == NULL)
error(FATAL, "cannot malloc extension_table space.");
BZERO(ext, size);
ext->filename = (char *)((ulong)ext + sizeof(struct extension_table));
/*
* If the library is not specified by an absolute pathname, dlopen()
* does not look in the current directory.
*/
if ((*lib != '.') && (*lib != '/') && is_elf_file(lib))
sprintf(ext->filename, "./%s", lib);
else
strcpy(ext->filename, lib);
/*
* register_extension() will be called by the shared object's
* _init() function before dlopen() returns below.
*/
pc->curext = ext;
ext->handle = dlopen(ext->filename, RTLD_NOW);
if (!ext->handle) {
strcpy(buf, dlerror());
error(INFO, "%s\n", buf);
if (strstr(buf, "undefined symbol: register_extension")) {
error(INFO, "%s may be statically linked: ",
pc->program_name);
fprintf(fp, "recompile without the -static flag\n");
}
free(ext);
return;
}
if (!(ext->flags & REGISTERED)) {
dlclose(ext->handle);
if (ext->flags & DUPLICATE_COMMAND_NAME)
error(INFO,
"%s: shared object unloaded\n", ext->filename);
else
error(INFO,
"%s: no commands registered: shared object unloaded\n",
ext->filename);
free(ext);
return;
}
fprintf(fp, "%s: shared object loaded\n", ext->filename);
/*
* Put new libraries at the head of the list.
*/
if (extension_table) {
extension_table->prev = ext;
ext->next = extension_table;
}
extension_table = ext;
help_init();
}
/*
* Unload all, or as specified, extension libraries.
*/
static void
unload_extension(char *lib)
{
struct extension_table *ext;
if (!lib) {
while (extension_table) {
ext = extension_table;
if (dlclose(ext->handle))
error(FATAL,
"dlclose: %s: shared object not open\n",
ext->filename);
fprintf(fp, "%s: shared object unloaded\n",
ext->filename);
extension_table = ext->next;
free(ext);
}
help_init();
return;
}
for (ext = extension_table; ext; ext = ext->next) {
if (same_file(lib, ext->filename)) {
if (dlclose(ext->handle))
error(INFO,
"dlclose: %s: shared object not open\n",
ext->filename);
else {
fprintf(fp, "%s: shared object unloaded\n",
ext->filename);
if (extension_table == ext) { /* first */
extension_table = ext->next;
if (ext->next)
ext->next->prev = NULL;
} else if (ext->next == NULL) /* last */
ext->prev->next = NULL;
else { /* middle */
ext->prev->next = ext->next;
ext->next->prev = ext->prev;
}
free(ext);
help_init();
}
}
}
}
/*
* Register the command_table as long as there are no command namespace
* clashes with the currently-existing command set. Also delete any aliases
* that clash, giving the registered command name priority.
*
* This function is called from the shared object's _init() function
* before the dlopen() call returns back to load_extension() above.
* The mark of approval for load_extension() is the setting of the
* REGISTERED bit in the "current" extension_table structure flags.
*/
void
register_extension(struct command_table_entry *command_table)
{
struct command_table_entry *cp;
for (cp = command_table; cp->name; cp++) {
if (get_command_table_entry(cp->name)) {
error(INFO,
"%s: \"%s\" is a duplicate of a currently-existing command\n",
pc->curext->filename, cp->name);
pc->curext->flags |= DUPLICATE_COMMAND_NAME;
return;
}
}
for (cp = command_table; cp->name; cp++) {
if (is_alias(cp->name)) {
error(INFO,
"alias \"%s\" deleted: name clash with extension command\n",
cp->name);
deallocate_alias(cp->name);
}
}
pc->curext->command_table = command_table;
pc->curext->flags |= REGISTERED; /* Mark of approval */
}
|