Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / export / cfgafs.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
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
8 */
9
10 /*
11 * cfgafs - load/configure the AFS kernel extension
12 */
13 #include <afsconfig.h>
14 #include <afs/param.h>
15
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/device.h>
22 #include <sys/sysconfig.h>
23 #include <sys/uio.h>
24 #include <sys/ldr.h>
25 #include <setjmp.h>
26 #include <signal.h>
27
28 extern char *malloc(), *optarg;
29
30 extern int sysconfig(int cmd, void *arg, int len);
31
32 #include "AFS_component_version_number.c"
33
34 main(argc, argv)
35 char **argv;
36 {
37 int add, del;
38 int c;
39 int res;
40 char *file;
41 mid_t kmid;
42 struct cfg_load cload;
43 struct cfg_kmod cmod;
44 FILE *fp;
45
46 #ifdef AFS_AIX32_ENV
47 /*
48 * The following signal action for AIX is necessary so that in case of a
49 * crash (i.e. core is generated) we can include the user's data section
50 * in the core dump. Unfortunately, by default, only a partial core is
51 * generated which, in many cases, isn't too useful.
52 */
53 struct sigaction nsa;
54
55 sigemptyset(&nsa.sa_mask);
56 nsa.sa_handler = SIG_DFL;
57 nsa.sa_flags = SA_FULLDUMP;
58 sigaction(SIGSEGV, &nsa, NULL);
59 #endif
60 add = del = 0;
61
62 while ((c = getopt(argc, argv, "a:d:")) != EOF) {
63 switch (c) {
64 case 'a':
65 add = 1;
66 file = optarg;
67 if (!file)
68 usage();
69 break;
70
71 case 'd':
72 del = 1;
73 file = optarg;
74 if (!file)
75 usage();
76 break;
77
78 default:
79 usage();
80 break;
81 }
82 }
83
84 if (!add && !del)
85 usage();
86
87 if (add) {
88 char *buf[1024];
89 char PidFile[256];
90
91 buf[0] = "execerror";
92 buf[1] = "cfgafs";
93 cload.path = file;
94 res = sysconfig(SYS_KLOAD, &cload, sizeof(cload));
95 if (res != 0) {
96 perror("SYS_KLOAD");
97 loadquery(L_GETMESSAGES, &buf[2], sizeof buf - 8);
98 execvp("/etc/execerror", buf);
99 exit(1);
100 }
101
102 cmod.kmid = cload.kmid;
103 cmod.cmd = CFG_INIT;
104 cmod.mdiptr = 0;
105 cmod.mdilen = 0;
106
107 res = sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod));
108 if (res != 0) {
109 perror("SYS_CFGKMOD");
110 cload.kmid = cload.kmid;
111 sysconfig(SYS_KULOAD, &cload, sizeof(cload));
112 exit(1);
113 }
114 #ifdef notdef
115 printf("cfgafs -d 0x%x # to remove AFS\n", cload.kmid);
116 #endif
117 strcpy(PidFile, file);
118 strcat(PidFile, ".kmid");
119 fp = fopen(PidFile, "w");
120 if (fp) {
121 (void)fprintf(fp, "%d\n", cload.kmid);
122 (void)fclose(fp);
123 } else {
124 printf("Can't open for write file %s (error=%d); ignored\n",
125 PidFile, errno);
126 }
127 exit(0);
128 } else if (del) {
129 char PidFile[256];
130
131 strcpy(PidFile, file);
132 strcat(PidFile, ".kmid");
133 fp = fopen(PidFile, "r");
134 if (!fp) {
135 printf("Can't read %s file (error=%d); aborting\n", PidFile,
136 errno);
137 exit(1);
138 }
139 (void)fscanf(fp, "%d\n", &kmid);
140 (void)fclose(fp);
141 unlink(PidFile);
142 cmod.kmid = kmid;
143 cmod.cmd = CFG_TERM;
144 cmod.mdiptr = NULL;
145 cmod.mdilen = 0;
146
147 if (sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod)) == -1) {
148 perror("SYS_CFGKMOD");
149 exit(1);
150 }
151
152 cload.kmid = kmid;
153 if (sysconfig(SYS_KULOAD, &cload, sizeof(cload)) == -1) {
154 perror("SYS_KULOAD");
155 exit(1);
156 }
157 exit(0);
158 }
159 }
160
161 usage()
162 {
163
164 fprintf(stderr, "usage: cfgafs [-a mod_file] [-d mod_file]\n");
165 exit(1);
166 }