2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * cfgexport - load/configure the EXPORT kernel extension
13 #include <afsconfig.h>
14 #include <afs/param.h>
20 #include <sys/types.h>
21 #include <sys/device.h>
22 #include <sys/sysconfig.h>
24 #include <sys/xcoff.h>
31 extern char *malloc(), *optarg;
33 extern int sysconfig(int cmd, void *arg, int len);
39 #include "AFS_component_version_number.c"
43 register add, del, opts;
47 struct cfg_load cload;
54 * The following signal action for AIX is necessary so that in case of a
55 * crash (i.e. core is generated) we can include the user's data section
56 * in the core dump. Unfortunately, by default, only a partial core is
57 * generated which, in many cases, isn't too useful.
61 sigemptyset(&nsa.sa_mask);
62 nsa.sa_handler = SIG_DFL;
63 nsa.sa_flags = SA_FULLDUMP;
64 sigaction(SIGSEGV, &nsa, NULL);
68 while ((c = getopt(argc, argv, "a:s:Z:d:")) != EOF) {
70 case 'Z': /* Zdebug option */
105 buf[0] = "execerror";
106 buf[1] = "cfgexport";
107 get_syms(&conf, syms);
110 if (sysconfig(SYS_KLOAD, &cload, sizeof(cload)) == -1) {
111 loadquery(L_GETMESSAGES, &buf[2], sizeof buf - 8);
112 execvp("/etc/execerror", buf);
117 cmod.kmid = cload.kmid;
119 cmod.mdiptr = (caddr_t) &conf;
120 cmod.mdilen = sizeof (conf);
122 if (sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod)) == -1) {
123 perror("SYS_CFGKMOD");
124 cload.kmid = cload.kmid;
125 sysconfig(SYS_KULOAD, &cload, sizeof(cload));
129 printf("cfgexport -d 0x%x # to remove EXPORT\n", cload.kmid);
131 strcpy(PidFile, file);
132 strcat(PidFile, ".kmid");
133 fp = fopen(PidFile, "w");
135 (void) fprintf(fp, "%d\n", cload.kmid);
138 printf("Can't open for write file %s (error=%d); ignored\n", PidFile, errno);
144 strcpy(PidFile, file);
145 strcat(PidFile, ".kmid");
146 fp = fopen(PidFile, "r");
148 printf("Can't read %s file (error=%d); aborting\n", PidFile, errno);
151 (void) fscanf(fp, "%d\n", &kmid);
159 if (sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod)) == -1) {
160 perror("SYS_CFGKMOD");
165 if (sysconfig(SYS_KULOAD, &cload, sizeof(cload)) == -1) {
166 perror("SYS_KULOAD");
175 error("usage: cfgexport [-a mod_file [-s symbols]] [-d mod_file]\n");
179 * get_syms - get kernel symbol table info.
182 * conf - ^ to EXPORT extension configuration struct
183 * syms - ^ to name of file containing XCOFF symbols
189 sym_t *k_symtab, *ksp;
190 struct syment *x_symtab, *xsp, *xsq;
192 struct xcoffhdr hdr; /* XCOFF header from symbol file*/
193 sym_t k_sym; /* export version of symbol */
194 struct syment xcoff_sym; /* xcoff version of symbol */
195 int i, nsyms, nksyms, nxsyms = 0;
196 int xstr_size, kstr_size;
200 fp = fopen(syms, "r");
204 if (fread(&hdr, sizeof (hdr), 1, fp) != 1)
207 if (hdr.filehdr.f_nsyms == 0)
208 error("%s: no symbols", syms);
210 switch (hdr.filehdr.f_magic) {
221 error("%s: funny magic number 0%o"
222 , syms, hdr.filehdr.f_magic);
225 nsyms = hdr.filehdr.f_nsyms;
227 printf("nsyms = %d\n", nsyms);
229 x_symtab = (struct syment *) malloc(nsyms * SYMESZ);
231 error("no memory for symbol table");
234 * try to snarf the string table: should be just past the
235 * symbol table: first 4 bytes is length of rest.
237 if (fseek(fp, hdr.filehdr.f_symptr + nsyms * SYMESZ, 0) < 0)
238 sys_error("%s: seek to strtab", syms);
240 if (fread(&xstr_size, sizeof (xstr_size), 1, fp) != 1)
241 error("%s: reading string table size", syms);
243 xstrings = malloc(xstr_size + sizeof (xstr_size));
245 error("no memory for string table");
248 * seek back to the start of the strings
250 if (fseek(fp, hdr.filehdr.f_symptr + nsyms * SYMESZ, 0) < 0)
251 sys_error("%s: seek to strtab", syms);
253 if (fread(xstrings, sizeof (*xstrings), xstr_size, fp) != xstr_size)
254 error("%s: reading string table");
257 * now seek back to the start of the symbol table, and read it
260 if (fseek(fp, hdr.filehdr.f_symptr, 0) < 0)
261 sys_error("%s: seek to symtab", syms);
265 for (i = nxsyms = 0; i < nsyms; ++i) {
268 if (fread(&xcoff_sym, SYMESZ, 1, fp) != 1)
269 error("%s: reading symbol entry", syms);
272 p = xstrings + xcoff_sym.n_offset;
274 if (xcoff_sym.n_zeroes == 0) {
276 * Need to relocate string table offset
278 p = xcoff_sym.n_nptr = xstrings + xcoff_sym.n_offset;
280 strncpy(name, xcoff_sym.n_name, 8);
287 dump_xsym(&xcoff_sym);
289 switch (xcoff_sym.n_sclass) {
290 case C_EXT: /* external */
291 case C_HIDEXT: /* hidden external (sic) */
293 * filtre out the ones with the strange names
295 if (strchr(p, '@') || strchr(p, '$') || p[0] == 0)
302 dump_xsym(&xcoff_sym);
307 if (xcoff_sym.n_numaux) {
308 fseek(fp, xcoff_sym.n_numaux * AUXESZ, 1);
309 i += xcoff_sym.n_numaux;
316 * sort the symbol table
318 qsort((char *) x_symtab, nxsyms, sizeof (*x_symtab), xsym_compar);
321 * we will need no more than `nxsyms' symbols.
323 k_symtab = (sym_t *) malloc(nxsyms * sizeof (sym_t));
325 error("no memory for EXPORT symbol table");
328 * uniquify it, and xlate to funny EXPORT format
330 xsp = xsq = x_symtab;
336 memset(xsq = &xcoff_sym, 0, sizeof (*xsq));
338 for (i = 1; i < nxsyms; ++i, xsq = xsp++) {
340 if (xsp->n_offset != xsq->n_offset
341 || xsp->n_value != xsq->n_value) {
343 if (xsp->n_zeroes != xsq->n_zeroes
344 || xsp->n_offset != xsq->n_offset
345 || xsp->n_value != xsq->n_value) {
347 xlate_xtok(xsp, ksp++, &kstrings, &kstr_size);
353 * place the symbol table info into the `conf' data structure
355 * XXXXX: for today only, leave the string table the same.
357 conf->nsyms = nksyms;
358 conf->symt_sz = nksyms * sizeof (sym_t);
359 conf->str_sz = kstr_size;
360 conf->symtab = (caddr_t) k_symtab;
361 conf->strtab = kstrings;
366 * xlate_xtok - xlate XCOFF to EXPORT format
369 * xp - ^ to XCOFF symbol
370 * kp - ^ to EXPORT symbol save area
371 * strp - ^ to ^ to EXPORT string table
372 * szp - ^ to EXPORT string table size
374 xlate_xtok(xp, kp, strp, szp)
375 register struct syment *xp;
380 static char *export_strings, *prev = "";
381 static left, offset, sz;
383 if (!export_strings) {
384 export_strings = malloc(sz=1024);
386 error("no memory for EXPORT string table");
388 *strp = export_strings;
389 *szp = offset = sizeof (uint);
390 left = 1024 - offset;
392 export_strings += offset;
394 *(uint *) export_strings = 0; /* initial 4 bytes */
398 if (strcmp(prev, xstrings + xp->n_offset) == 0) {
400 * same name as previous entry: just use previous
402 kp->n_offset = offset - strlen( *strp + xp->n_offset) - 1;
403 } else if (find_suffix(xstrings + xp->n_offset, *strp, offset, &kp->n_offset)) {
405 * found a string that we are a suffix of
410 * need to add to our string table
412 len = strlen(xstrings + xp->n_offset) + 1;
413 while (len >= left) {
414 export_strings = (char *)realloc(*strp, sz += 1024);
416 error("no memory for EXPORT string table");
417 *strp = export_strings;
419 prev = ""; /* lazy */
422 strcpy(prev = *strp + offset, xstrings + xp->n_offset);
424 kp->n_offset = offset;
430 if (kp->n_zeroes = xp->n_zeroes) { /* sic */
431 kp->n_zeroes = xp->n_zeroes;
432 kp->n_offset = xp->n_offset;
433 } else if (strcmp(prev, xp->n_nptr) == 0) {
435 * same name as previous entry: just use previous
437 kp->n_offset = offset - strlen(xp->n_nptr) - 1;
438 } else if (find_suffix(xp->n_nptr, *strp, offset, &kp->n_offset)) {
440 * found a string that we are a suffix of
445 * need to add to our string table
447 len = strlen(xp->n_nptr) + 1;
448 while (len >= left) {
449 export_strings = (char *)realloc(*strp, sz += 1024);
451 error("no memory for EXPORT string table");
452 *strp = export_strings;
454 prev = ""; /* lazy */
457 strcpy(prev = *strp + offset, xp->n_nptr);
459 kp->n_offset = offset;
466 kp->n_value = xp->n_value;
469 dump_ksym(kp, *strp);
473 * find_suffix - look for a string that arg string is suffix of
476 * p - ^ to string we hope is a suffix of another
477 * strings - ^ to string table
478 * max - max offset of valid string in strings
479 * offp - ^ to place to store offset, if containing string found
482 * 0 - no containing string found
483 * !0 - string found of which `p' is a suffix
486 * This is rather inefficient.
488 find_suffix(p, strings, max, offp)
489 register char *p, *strings;
491 register char *q, *e;
492 register len = strlen(p) - 1;
494 strings += sizeof (uint);
495 max -= sizeof (uint);
497 for (e = strings + max; e > strings; ) {
499 * adjust `e' to point at last non-blank
506 for (q = p + len; q > p && *q == *e; )
511 printf("found_suffix: %s\n", p);
512 return *offp = e - strings + sizeof (uint);
516 while (*e && e > strings)
524 * xsym_compar - compare two XCOFF symbol table entries
526 * If the names are the same, sort by descending storage class, so that
530 register struct syment *xp, *xq; {
531 register char *p, *q;
535 p = (xp->n_zeroes ? xp->n_name : xp->n_nptr);
536 q = (xq->n_zeroes ? xq->n_name : xq->n_nptr);
538 if (xp->n_zeroes || xq->n_zeroes)
539 compar = strncmp(p, q, 8);
542 p = xstrings + xp->n_offset;
543 q = xstrings + xq->n_offset;
545 compar = strcmp(p, q);
548 compar = xp->n_sclass - xq->n_sclass;
554 * dump_xsym - print to XCOFF symbol
557 struct syment *xsp; {
562 "nptr <%-8.8s %8.8s> val %8.8x sc# %4.4x type %4.4x sclass %2.2x naux %2.2x\n"
566 , xsp->n_scnum & 0xffff
574 "nptr <%-17.17s> val %8.8x sc# %4.4x type %4.4x sclass %2.2x naux %2.2x\n"
576 , xstrings + xsp->n_offset
581 , xsp->n_scnum & 0xffff
587 dump_ksym(ksp, strings)
593 printf("%8.8x %-8.8s\n", ksp->n_value, ksp->n_name);
596 printf("%8.8x %s\n", ksp->n_value, ksp->n_offset + strings);
599 error(p, a, b, c, d, e)
602 fprintf(stderr, p, a, b, c, d, e);
603 fprintf(stderr, "\n");
607 sys_error(p, a, b, c, d, e)
611 fprintf(stderr, p, a, b, c, d, e);
616 warn(p, a, b, c, d, e)
620 fprintf(stderr, p, a, b, c, d, e);
621 fprintf(stderr, "\n");