9d1a9f18070085c45fd5d2c73ee9144ffbfb5dc3
[openafs.git] / src / vlserver / vlserver.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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <afs/stds.h>
17 #include <sys/types.h>
18 #include <signal.h>
19 #include <sys/stat.h>
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #ifdef AFS_NT40_ENV
24 #include <winsock2.h>
25 #include <WINNT/afsevent.h>
26 #endif
27 #ifdef HAVE_SYS_FILE_H
28 #include <sys/file.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_NETDB_H
32 #include <netdb.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #include <stdio.h>
38
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #else
42 #ifdef HAVE_STRINGS_H
43 #include <strings.h>
44 #endif
45 #endif
46
47 #include <rx/xdr.h>
48 #include <rx/rx.h>
49 #include <rx/rx_globals.h>
50 #include <afs/cellconfig.h>
51 #include <afs/keys.h>
52 #include <afs/auth.h>
53 #include <lock.h>
54 #include <ubik.h>
55 #include <afs/afsutil.h>
56 #include "vlserver.h"
57
58
59 #define MAXLWP 16
60 const char *vl_dbaseName;
61 struct afsconf_dir *vldb_confdir = 0;   /* vldb configuration dir */
62 int lwps = 9;
63
64 struct vldstats dynamic_statistics;
65 struct ubik_dbase *VL_dbase;
66 afs_uint32 HostAddress[MAXSERVERID + 1];
67 extern int afsconf_CheckAuth();
68 extern int afsconf_ServerAuth();
69
70 static CheckSignal();
71 int LogLevel = 0;
72 int smallMem = 0;
73 int rxJumbograms = 1;           /* default is to send and receive jumbo grams */
74
75 static void
76 CheckSignal_Signal()
77 {
78     IOMGR_SoftSig(CheckSignal, 0);
79 }
80
81 static
82 CheckSignal()
83 {
84     register int i, errorcode;
85     struct ubik_trans *trans;
86
87     if (errorcode =
88         Init_VLdbase(&trans, LOCKREAD, VLGETSTATS - VL_LOWEST_OPCODE))
89         return errorcode;
90     VLog(0, ("Dump name hash table out\n"));
91     for (i = 0; i < HASHSIZE; i++) {
92         HashNDump(trans, i);
93     }
94     VLog(0, ("Dump id hash table out\n"));
95     for (i = 0; i < HASHSIZE; i++) {
96         HashIdDump(trans, i);
97     }
98     return (ubik_EndTrans(trans));
99 }                               /*CheckSignal */
100
101
102 /* Initialize the stats for the opcodes */
103 void
104 initialize_dstats()
105 {
106     int i;
107
108     dynamic_statistics.start_time = (afs_uint32) time(0);
109     for (i = 0; i < MAX_NUMBER_OPCODES; i++) {
110         dynamic_statistics.requests[i] = 0;
111         dynamic_statistics.aborts[i] = 0;
112     }
113 }
114
115 /* check whether caller is authorized to manage RX statistics */
116 int
117 vldb_rxstat_userok(call)
118      struct rx_call *call;
119 {
120     return afsconf_SuperUser(vldb_confdir, call, NULL);
121 }
122
123 /* Main server module */
124
125 #include "AFS_component_version_number.c"
126
127 main(argc, argv)
128      int argc;
129      char **argv;
130 {
131     register afs_int32 code;
132     afs_int32 myHost;
133     struct rx_service *tservice;
134     struct rx_securityClass *sc[3];
135     extern int VL_ExecuteRequest();
136     extern int RXSTATS_ExecuteRequest();
137     struct afsconf_dir *tdir;
138     struct ktc_encryptionKey tkey;
139     struct afsconf_cell info;
140     struct hostent *th;
141     char hostname[VL_MAXNAMELEN];
142     int noAuth = 0, index, i;
143     extern int rx_extraPackets;
144     char commandLine[150];
145     char clones[MAXHOSTSPERCELL];
146
147 #ifdef  AFS_AIX32_ENV
148     /*
149      * The following signal action for AIX is necessary so that in case of a 
150      * crash (i.e. core is generated) we can include the user's data section 
151      * in the core dump. Unfortunately, by default, only a partial core is
152      * generated which, in many cases, isn't too useful.
153      */
154     struct sigaction nsa;
155
156     rx_extraPackets = 100;      /* should be a switch, I guess... */
157     sigemptyset(&nsa.sa_mask);
158     nsa.sa_handler = SIG_DFL;
159     nsa.sa_flags = SA_FULLDUMP;
160     sigaction(SIGABRT, &nsa, NULL);
161     sigaction(SIGSEGV, &nsa, NULL);
162 #endif
163     /* Parse command line */
164     for (index = 1; index < argc; index++) {
165         if (strcmp(argv[index], "-noauth") == 0) {
166             noAuth = 1;
167
168         } else if (strcmp(argv[index], "-p") == 0) {
169             lwps = atoi(argv[++index]);
170             if (lwps > MAXLWP) {
171                 printf("Warning: '-p %d' is too big; using %d instead\n",
172                        lwps, MAXLWP);
173                 lwps = MAXLWP;
174             }
175
176         } else if (strcmp(argv[index], "-nojumbo") == 0) {
177             rxJumbograms = 0;
178
179         } else if (strcmp(argv[index], "-smallmem") == 0) {
180             smallMem = 1;
181
182         } else if (strcmp(argv[index], "-trace") == 0) {
183             extern char rxi_tracename[80];
184             strcpy(rxi_tracename, argv[++index]);
185
186        } else if (strcmp(argv[index], "-auditlog") == 0) {
187            int tempfd, flags;
188            FILE *auditout;
189            char oldName[MAXPATHLEN];
190            char *fileName = argv[++index];
191
192 #ifndef AFS_NT40_ENV
193            struct stat statbuf;
194
195            if ((lstat(fileName, &statbuf) == 0) 
196                && (S_ISFIFO(statbuf.st_mode))) {
197                flags = O_WRONLY | O_NONBLOCK;
198            } else 
199 #endif
200            {
201                strcpy(oldName, fileName);
202                strcat(oldName, ".old");
203                renamefile(fileName, oldName);
204                flags = O_WRONLY | O_TRUNC | O_CREAT;
205            }
206            tempfd = open(fileName, flags, 0666);
207            if (tempfd > -1) {
208                auditout = fdopen(tempfd, "a");
209                if (auditout) {
210                    osi_audit_file(auditout);
211                } else
212                    printf("Warning: auditlog %s not writable, ignored.\n", fileName);
213            } else
214                printf("Warning: auditlog %s not writable, ignored.\n", fileName);
215         } else if (strcmp(argv[index], "-enable_peer_stats") == 0) {
216             rx_enablePeerRPCStats();
217         } else if (strcmp(argv[index], "-enable_process_stats") == 0) {
218             rx_enableProcessRPCStats();
219 #ifndef AFS_NT40_ENV
220         } else if (strcmp(argv[index], "-syslog") == 0) {
221             /* set syslog logging flag */
222             serverLogSyslog = 1;
223         } else if (strncmp(argv[index], "-syslog=", 8) == 0) {
224             serverLogSyslog = 1;
225             serverLogSyslogFacility = atoi(argv[index] + 8);
226 #endif
227         } else {
228             /* support help flag */
229 #ifndef AFS_NT40_ENV
230             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
231                    "[-auditlog <log path>] "
232                    "[-syslog[=FACILITY]] "
233                    "[-enable_peer_stats] [-enable_process_stats] "
234                    "[-help]\n");
235 #else
236             printf("Usage: vlserver [-p <number of processes>] [-nojumbo] "
237                    "[-auditlog <log path>] "
238                    "[-enable_peer_stats] [-enable_process_stats] "
239                    "[-help]\n");
240 #endif
241             fflush(stdout);
242             exit(0);
243         }
244     }
245
246     /* Initialize dirpaths */
247     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
248 #ifdef AFS_NT40_ENV
249         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
250 #endif
251         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
252                 argv[0]);
253         exit(2);
254     }
255     vl_dbaseName = AFSDIR_SERVER_VLDB_FILEPATH;
256
257 #ifndef AFS_NT40_ENV
258     serverLogSyslogTag = "vlserver";
259 #endif
260     OpenLog(AFSDIR_SERVER_VLOG_FILEPATH);       /* set up logging */
261     SetupLogSignals();
262
263     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
264     if (!tdir) {
265         printf
266             ("vlserver: can't open configuration files in dir %s, giving up.\n",
267              AFSDIR_SERVER_ETC_DIRPATH);
268         exit(1);
269     }
270 #ifdef AFS_NT40_ENV
271     /* initialize winsock */
272     if (afs_winsockInit() < 0) {
273         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
274         fprintf(stderr, "vlserver: couldn't initialize winsock. \n");
275         exit(1);
276     }
277 #endif
278     /* get this host */
279     gethostname(hostname, sizeof(hostname));
280     th = gethostbyname(hostname);
281     if (!th) {
282         printf("vlserver: couldn't get address of this host (%s).\n",
283                hostname);
284         exit(1);
285     }
286     memcpy(&myHost, th->h_addr, sizeof(afs_int32));
287
288 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
289     signal(SIGXCPU, CheckSignal_Signal);
290 #endif
291     /* get list of servers */
292     code =
293         afsconf_GetExtendedCellInfo(tdir, NULL, AFSCONF_VLDBSERVICE, &info,
294                                     &clones);
295     if (code) {
296         printf("vlserver: Couldn't get cell server list for 'afsvldb'.\n");
297         exit(2);
298     }
299
300     vldb_confdir = tdir;        /* Preserve our configuration dir */
301     /* rxvab no longer supported */
302     memset(&tkey, 0, sizeof(tkey));
303
304     if (noAuth)
305         afsconf_SetNoAuthFlag(tdir, 1);
306
307     ubik_nBuffers = 512;
308     ubik_CRXSecurityProc = afsconf_ClientAuth;
309     ubik_CRXSecurityRock = (char *)tdir;
310     ubik_SRXSecurityProc = afsconf_ServerAuth;
311     ubik_SRXSecurityRock = (char *)tdir;
312     ubik_CheckRXSecurityProc = afsconf_CheckAuth;
313     ubik_CheckRXSecurityRock = (char *)tdir;
314     code =
315         ubik_ServerInitByInfo(myHost, htons(AFSCONF_VLDBPORT), &info, &clones,
316                               vl_dbaseName, &VL_dbase);
317     if (code) {
318         printf("vlserver: Ubik init failed with code %d\n", code);
319         exit(2);
320     }
321     if (!rxJumbograms) {
322         rx_SetNoJumbo();
323     }
324     rx_SetRxDeadTime(50);
325
326     memset(HostAddress, 0, sizeof(HostAddress));
327     initialize_dstats();
328
329     sc[0] = rxnull_NewServerSecurityObject();
330     sc[1] = (struct rx_securityClass *)0;
331     sc[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
332     tservice =
333         rx_NewService(0, USER_SERVICE_ID, "Vldb server", sc, 3,
334                       VL_ExecuteRequest);
335     if (tservice == (struct rx_service *)0) {
336         printf("vlserver: Could not create VLDB_SERVICE rx service\n");
337         exit(3);
338     }
339     rx_SetMinProcs(tservice, 2);
340     if (lwps < 4)
341         lwps = 4;
342     rx_SetMaxProcs(tservice, lwps);
343
344     tservice =
345         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", sc, 3,
346                       RXSTATS_ExecuteRequest);
347     if (tservice == (struct rx_service *)0) {
348         printf("vlserver: Could not create rpc stats rx service\n");
349         exit(3);
350     }
351     rx_SetMinProcs(tservice, 2);
352     rx_SetMaxProcs(tservice, 4);
353
354     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
355         if (i > 0)
356             strcat(commandLine, " ");
357         strcat(commandLine, argv[i]);
358     }
359     ViceLog(0,
360             ("Starting AFS vlserver %d (%s)\n", VLDBVERSION_4, commandLine));
361     printf("%s\n", cml_version_number); /* Goes to the log */
362
363     /* allow super users to manage RX statistics */
364     rx_SetRxStatUserOk(vldb_rxstat_userok);
365
366     rx_StartServer(1);          /* Why waste this idle process?? */
367 }