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