b48afd7c35a22d6e86c99b94ef0f18fa7bc0ce31
[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 RCSID("$Header$");
17
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 extern int      errno;
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     register add, del;
37     register c;
38     int res;
39     char *file;
40     mid_t kmid;
41     struct cfg_load cload;
42     struct cfg_kmod cmod;
43     FILE *fp;
44     
45 #ifdef  AFS_AIX32_ENV
46     /*
47      * The following signal action for AIX is necessary so that in case of a 
48      * crash (i.e. core is generated) we can include the user's data section 
49      * in the core dump. Unfortunately, by default, only a partial core is
50      * generated which, in many cases, isn't too useful.
51      */
52     struct sigaction nsa;
53     
54     sigemptyset(&nsa.sa_mask);
55     nsa.sa_handler = SIG_DFL;
56     nsa.sa_flags = SA_FULLDUMP;
57     sigaction(SIGSEGV, &nsa, NULL);
58 #endif
59     add = del = 0;
60     
61     while ((c = getopt(argc, argv, "a:d:")) != EOF) {
62         switch (c) {
63           case 'a':
64             add  = 1;
65             file = optarg;
66             if (!file)
67                 usage();
68             break;
69             
70           case 'd':
71             del = 1;
72             file = optarg;
73             if (!file)
74                 usage();
75             break;
76             
77           default:
78             usage();
79             break;
80         }
81     }
82     
83     if (!add && !del)
84         usage();
85     
86     if (add) {
87         char *buf[1024];
88         char PidFile[256];
89
90         buf[0] = "execerror";
91         buf[1] = "cfgafs";
92         cload.path = file;
93         res = sysconfig(SYS_KLOAD, &cload, sizeof(cload));
94         if (res != 0) {
95             perror("SYS_KLOAD");
96             loadquery(L_GETMESSAGES, &buf[2], sizeof buf - 8);
97             execvp("/etc/execerror", buf);
98             exit(1);
99         }
100         
101         cmod.kmid   = cload.kmid;
102         cmod.cmd    = CFG_INIT;
103         cmod.mdiptr = 0;
104         cmod.mdilen = 0;
105         
106         res = sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod));
107         if (res != 0) {
108             perror("SYS_CFGKMOD");
109             cload.kmid = cload.kmid;
110             sysconfig(SYS_KULOAD, &cload, sizeof(cload));
111             exit(1);
112         }
113 #ifdef notdef
114         printf("cfgafs -d 0x%x # to remove AFS\n", cload.kmid);
115 #endif
116         strcpy(PidFile, file);
117         strcat(PidFile, ".kmid");
118         fp = fopen(PidFile, "w");
119         if (fp) {
120             (void) fprintf(fp, "%d\n", cload.kmid);
121             (void) fclose(fp);
122         } else {
123             printf("Can't open for write file %s (error=%d); ignored\n", PidFile, errno);
124         }
125         exit(0);
126     } else if (del) {
127         char PidFile[256];
128
129         strcpy(PidFile, file);
130         strcat(PidFile, ".kmid");
131         fp = fopen(PidFile, "r");
132         if (!fp) {
133             printf("Can't read %s file (error=%d); aborting\n", PidFile, errno);
134             exit(1);
135         }
136         (void) fscanf(fp, "%d\n", &kmid);
137         (void) fclose(fp);
138         unlink(PidFile);
139         cmod.kmid   = kmid;
140         cmod.cmd    = CFG_TERM;
141         cmod.mdiptr = NULL;
142         cmod.mdilen = 0;
143         
144         if (sysconfig(SYS_CFGKMOD, &cmod, sizeof(cmod)) == -1) {
145             perror("SYS_CFGKMOD");
146             exit(1);
147         }
148         
149         cload.kmid = kmid;
150         if (sysconfig(SYS_KULOAD, &cload, sizeof(cload)) == -1) {
151             perror("SYS_KULOAD");
152             exit(1);
153         }
154         exit(0);
155     }
156 }
157
158 usage() {
159     
160     fprintf(stderr, "usage: cfgafs [-a mod_file] [-d mod_file]\n");
161     exit(1);
162 }