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>
22 #include <sys/types.h>
23 #include <sys/device.h>
24 #include <sys/sysconfig.h>
26 #include <sys/xcoff.h>
33 extern char *malloc(), *optarg;
34 extern int sysconfig(int cmd, void *arg, int len);
40 #include "AFS_component_version_number.c"
45 register add, del, opts;
49 struct cfg_load cload;
56 * The following signal action for AIX is necessary so that in case of a
57 * crash (i.e. core is generated) we can include the user's data section
58 * in the core dump. Unfortunately, by default, only a partial core is
59 * generated which, in many cases, isn't too useful.
63 sigemptyset(&nsa.sa_mask);
64 nsa.sa_handler = SIG_DFL;
65 nsa.sa_flags = SA_FULLDUMP;
66 sigaction(SIGSEGV, &nsa, NULL);
70 while ((c = getopt(argc, argv, "a:s:Z:d:")) != EOF) {
72 case 'Z': /* Zdebug option */
107 buf[0] = "execerror";
108 buf[1] = "cfgexport";
109 get_syms(&conf, syms);
112 if (sysconfig(SYS_KLOAD, &cload, sizeof(cload)) == -1) {
113 loadquery(L_GETMESSAGES, &buf[2], sizeof buf - 8);
114 execvp("/etc/execerror", buf);
119 cmod.kmid = cload.kmid;
121 cmod.mdiptr = (caddr_t) & conf;
122 cmod.mdilen = sizeof(conf);
124 if (sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod)) == -1) {
125 perror("SYS_CFGKMOD");
126 cload.kmid = cload.kmid;
127 sysconfig(SYS_KULOAD, &cload, sizeof(cload));
131 printf("cfgexport -d 0x%x # to remove EXPORT\n", cload.kmid);
133 strcpy(PidFile, file);
134 strcat(PidFile, ".kmid");
135 fp = fopen(PidFile, "w");
137 (void)fprintf(fp, "%d\n", cload.kmid);
140 printf("Can't open for write file %s (error=%d); ignored\n",
147 strcpy(PidFile, file);
148 strcat(PidFile, ".kmid");
149 fp = fopen(PidFile, "r");
151 printf("Can't read %s file (error=%d); aborting\n", PidFile,
155 (void)fscanf(fp, "%d\n", &kmid);
163 if (sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod)) == -1) {
164 perror("SYS_CFGKMOD");
169 if (sysconfig(SYS_KULOAD, &cload, sizeof(cload)) == -1) {
170 perror("SYS_KULOAD");
180 error("usage: cfgexport [-a mod_file [-s symbols]] [-d mod_file]\n");
184 * get_syms - get kernel symbol table info.
187 * conf - ^ to EXPORT extension configuration struct
188 * syms - ^ to name of file containing XCOFF symbols
194 sym_t *k_symtab, *ksp;
195 struct syment *x_symtab, *xsp, *xsq;
197 struct xcoffhdr hdr; /* XCOFF header from symbol file */
198 sym_t k_sym; /* export version of symbol */
199 struct syment xcoff_sym; /* xcoff version of symbol */
200 int i, nsyms, nksyms, nxsyms = 0;
201 int xstr_size, kstr_size;
205 fp = fopen(syms, "r");
209 if (fread(&hdr, sizeof(hdr), 1, fp) != 1)
212 if (hdr.filehdr.f_nsyms == 0)
213 error("%s: no symbols", syms);
215 switch (hdr.filehdr.f_magic) {
228 error("%s: funny magic number 0%o", syms, hdr.filehdr.f_magic);
231 nsyms = hdr.filehdr.f_nsyms;
233 printf("nsyms = %d\n", nsyms);
235 x_symtab = (struct syment *)malloc(nsyms * SYMESZ);
237 error("no memory for symbol table");
240 * try to snarf the string table: should be just past the
241 * symbol table: first 4 bytes is length of rest.
243 if (fseek(fp, hdr.filehdr.f_symptr + nsyms * SYMESZ, 0) < 0)
244 sys_error("%s: seek to strtab", syms);
246 if (fread(&xstr_size, sizeof(xstr_size), 1, fp) != 1)
247 error("%s: reading string table size", syms);
249 xstrings = malloc(xstr_size + sizeof(xstr_size));
251 error("no memory for string table");
254 * seek back to the start of the strings
256 if (fseek(fp, hdr.filehdr.f_symptr + nsyms * SYMESZ, 0) < 0)
257 sys_error("%s: seek to strtab", syms);
259 if (fread(xstrings, sizeof(*xstrings), xstr_size, fp) != xstr_size)
260 error("%s: reading string table");
263 * now seek back to the start of the symbol table, and read it
266 if (fseek(fp, hdr.filehdr.f_symptr, 0) < 0)
267 sys_error("%s: seek to symtab", syms);
271 for (i = nxsyms = 0; i < nsyms; ++i) {
274 if (fread(&xcoff_sym, SYMESZ, 1, fp) != 1)
275 error("%s: reading symbol entry", syms);
278 p = xstrings + xcoff_sym.n_offset;
280 if (xcoff_sym.n_zeroes == 0) {
282 * Need to relocate string table offset
284 p = xcoff_sym.n_nptr = xstrings + xcoff_sym.n_offset;
286 strncpy(name, xcoff_sym.n_name, 8);
293 dump_xsym(&xcoff_sym);
295 switch (xcoff_sym.n_sclass) {
296 case C_EXT: /* external */
297 case C_HIDEXT: /* hidden external (sic) */
299 * filtre out the ones with the strange names
301 if (strchr(p, '@') || strchr(p, '$') || p[0] == 0)
308 dump_xsym(&xcoff_sym);
313 if (xcoff_sym.n_numaux) {
314 fseek(fp, xcoff_sym.n_numaux * AUXESZ, 1);
315 i += xcoff_sym.n_numaux;
322 * sort the symbol table
324 qsort((char *)x_symtab, nxsyms, sizeof(*x_symtab), xsym_compar);
327 * we will need no more than `nxsyms' symbols.
329 k_symtab = (sym_t *) malloc(nxsyms * sizeof(sym_t));
331 error("no memory for EXPORT symbol table");
334 * uniquify it, and xlate to funny EXPORT format
336 xsp = xsq = x_symtab;
342 memset(xsq = &xcoff_sym, 0, sizeof(*xsq));
344 for (i = 1; i < nxsyms; ++i, xsq = xsp++) {
346 if (xsp->n_offset != xsq->n_offset || xsp->n_value != xsq->n_value) {
348 if (xsp->n_zeroes != xsq->n_zeroes || xsp->n_offset != xsq->n_offset
349 || xsp->n_value != xsq->n_value) {
351 xlate_xtok(xsp, ksp++, &kstrings, &kstr_size);
357 * place the symbol table info into the `conf' data structure
359 * XXXXX: for today only, leave the string table the same.
361 conf->nsyms = nksyms;
362 conf->symt_sz = nksyms * sizeof(sym_t);
363 conf->str_sz = kstr_size;
364 conf->symtab = (caddr_t) k_symtab;
365 conf->strtab = kstrings;
370 * xlate_xtok - xlate XCOFF to EXPORT format
373 * xp - ^ to XCOFF symbol
374 * kp - ^ to EXPORT symbol save area
375 * strp - ^ to ^ to EXPORT string table
376 * szp - ^ to EXPORT string table size
378 xlate_xtok(xp, kp, strp, szp)
379 register struct syment *xp;
385 static char *export_strings, *prev = "";
386 static left, offset, sz;
388 if (!export_strings) {
389 export_strings = malloc(sz = 1024);
391 error("no memory for EXPORT string table");
393 *strp = export_strings;
394 *szp = offset = sizeof(uint);
395 left = 1024 - offset;
397 export_strings += offset;
399 *(uint *) export_strings = 0; /* initial 4 bytes */
402 if (strcmp(prev, xstrings + xp->n_offset) == 0) {
404 * same name as previous entry: just use previous
406 kp->n_offset = offset - strlen(*strp + xp->n_offset) - 1;
409 (xstrings + xp->n_offset, *strp, offset, &kp->n_offset)) {
411 * found a string that we are a suffix of
416 * need to add to our string table
418 len = strlen(xstrings + xp->n_offset) + 1;
419 while (len >= left) {
420 export_strings = (char *)realloc(*strp, sz += 1024);
422 error("no memory for EXPORT string table");
423 *strp = export_strings;
425 prev = ""; /* lazy */
428 strcpy(prev = *strp + offset, xstrings + xp->n_offset);
430 kp->n_offset = offset;
436 if (kp->n_zeroes = xp->n_zeroes) { /* sic */
437 kp->n_zeroes = xp->n_zeroes;
438 kp->n_offset = xp->n_offset;
439 } else if (strcmp(prev, xp->n_nptr) == 0) {
441 * same name as previous entry: just use previous
443 kp->n_offset = offset - strlen(xp->n_nptr) - 1;
444 } else if (find_suffix(xp->n_nptr, *strp, offset, &kp->n_offset)) {
446 * found a string that we are a suffix of
451 * need to add to our string table
453 len = strlen(xp->n_nptr) + 1;
454 while (len >= left) {
455 export_strings = (char *)realloc(*strp, sz += 1024);
457 error("no memory for EXPORT string table");
458 *strp = export_strings;
460 prev = ""; /* lazy */
463 strcpy(prev = *strp + offset, xp->n_nptr);
465 kp->n_offset = offset;
472 kp->n_value = xp->n_value;
475 dump_ksym(kp, *strp);
479 * find_suffix - look for a string that arg string is suffix of
482 * p - ^ to string we hope is a suffix of another
483 * strings - ^ to string table
484 * max - max offset of valid string in strings
485 * offp - ^ to place to store offset, if containing string found
488 * 0 - no containing string found
489 * !0 - string found of which `p' is a suffix
492 * This is rather inefficient.
494 find_suffix(p, strings, max, offp)
495 register char *p, *strings;
498 register char *q, *e;
499 register len = strlen(p) - 1;
501 strings += sizeof(uint);
504 for (e = strings + max; e > strings;) {
506 * adjust `e' to point at last non-blank
513 for (q = p + len; q > p && *q == *e;)
518 printf("found_suffix: %s\n", p);
519 return *offp = e - strings + sizeof(uint);
523 while (*e && e > strings)
531 * xsym_compar - compare two XCOFF symbol table entries
533 * If the names are the same, sort by descending storage class, so that
537 register struct syment *xp, *xq;
539 register char *p, *q;
543 p = (xp->n_zeroes ? xp->n_name : xp->n_nptr);
544 q = (xq->n_zeroes ? xq->n_name : xq->n_nptr);
546 if (xp->n_zeroes || xq->n_zeroes)
547 compar = strncmp(p, q, 8);
550 p = xstrings + xp->n_offset;
551 q = xstrings + xq->n_offset;
553 compar = strcmp(p, q);
556 compar = xp->n_sclass - xq->n_sclass;
562 * dump_xsym - print to XCOFF symbol
571 ("nptr <%-8.8s %8.8s> val %8.8x sc# %4.4x type %4.4x sclass %2.2x naux %2.2x\n",
572 xsp->n_name, "", xsp->n_value, xsp->n_scnum & 0xffff,
573 xsp->n_type, xsp->n_sclass, xsp->n_numaux);
577 ("nptr <%-17.17s> val %8.8x sc# %4.4x type %4.4x sclass %2.2x naux %2.2x\n"
579 , xstrings + xsp->n_offset
583 , xsp->n_value, xsp->n_scnum & 0xffff, xsp->n_type,
584 xsp->n_sclass, xsp->n_numaux);
587 dump_ksym(ksp, strings)
594 printf("%8.8x %-8.8s\n", ksp->n_value, ksp->n_name);
597 printf("%8.8x %s\n", ksp->n_value, ksp->n_offset + strings);
600 error(p, a, b, c, d, e)
604 fprintf(stderr, p, a, b, c, d, e);
605 fprintf(stderr, "\n");
609 sys_error(p, a, b, c, d, e)
613 fprintf(stderr, p, a, b, c, d, e);
618 warn(p, a, b, c, d, e)
622 fprintf(stderr, p, a, b, c, d, e);
623 fprintf(stderr, "\n");