Make rxgen emit prototype for ExecuteRequest
[openafs.git] / src / volser / volmain.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
14 #include <sys/types.h>
15 #include <string.h>
16 #ifdef AFS_NT40_ENV
17 #include <time.h>
18 #include <fcntl.h>
19 #include <windows.h>
20 #include <WINNT/afsevent.h>
21 #else
22 #include <sys/time.h>
23 #include <sys/file.h>
24 #include <netinet/in.h>
25 #include <unistd.h>
26 #endif
27 #include <rx/xdr.h>
28 #include <afs/afsint.h>
29 #include <stdio.h>
30 #include <signal.h>
31 #ifdef AFS_PTHREAD_ENV
32 #include <assert.h>
33 #else /* AFS_PTHREAD_ENV */
34 #include <afs/assert.h>
35 #endif /* AFS_PTHREAD_ENV */
36 #include <afs/prs_fs.h>
37 #include <afs/nfs.h>
38 #include <lwp.h>
39 #include <lock.h>
40 #include <afs/afssyscalls.h>
41 #include <afs/ihandle.h>
42 #ifdef AFS_NT40_ENV
43 #include <afs/ntops.h>
44 #endif
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #include <afs/partition.h>
48 #include <rx/rx.h>
49 #include <rx/rxstat.h>
50 #include <rx/rx_globals.h>
51 #include <afs/auth.h>
52 #include <afs/cellconfig.h>
53 #include <afs/keys.h>
54 #include <afs/dir.h>
55 #include <ubik.h>
56 #include <fcntl.h>
57 #include <sys/stat.h>
58
59 #include "volser.h"
60 #include <errno.h>
61 #include <afs/audit.h>
62 #include <afs/afsutil.h>
63 #include <lwp.h>
64 #include "volser.h"
65 #include "volint.h"
66 #include "volser_prototypes.h"
67
68 /*@printflike@*/ extern void Log(const char *format, ...);
69 /*@printflike@*/ extern void Abort(const char *format, ...);
70
71 #define VolserVersion "2.0"
72 #define N_SECURITY_OBJECTS 3
73
74 extern struct Lock localLock;
75 char *GlobalNameHack = NULL;
76 int hackIsIn = 0;
77 afs_int32 GlobalVolCloneId, GlobalVolParentId;
78 int GlobalVolType;
79 int VolumeChanged;              /* XXXX */
80 static char busyFlags[MAXHELPERS];
81 struct volser_trans *QI_GlobalWriteTrans = 0;
82 struct afsconf_dir *tdir;
83 static afs_int32 runningCalls = 0;
84 int DoLogging = 0;
85 int debuglevel = 0; 
86 #define MAXLWP 128
87 int lwps = 9;
88 int udpBufSize = 0;             /* UDP buffer size for receive */
89
90 int rxBind = 0;
91 int rxkadDisableDotCheck = 0;
92
93 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
94 afs_uint32 SHostAddrs[ADDRSPERSITE];
95
96 #define VS_EXIT(code)  {                                          \
97                           osi_audit(VS_ExitEvent, code, AUD_END); \
98                           exit(code);                             \
99                        }
100
101 #if defined(AFS_PTHREAD_ENV)
102 int
103 threadNum(void)
104 {
105     return (int)pthread_getspecific(rx_thread_id_key);
106 }
107 #endif
108
109 static void
110 MyBeforeProc(struct rx_call *acall)
111 {
112     VTRANS_LOCK;
113     runningCalls++;
114     VTRANS_UNLOCK;
115     return;
116 }
117
118 static void
119 MyAfterProc(struct rx_call *acall, afs_int32 code)
120 {
121     VTRANS_LOCK;
122     runningCalls--;
123     VTRANS_UNLOCK;
124     return;
125 }
126
127 /* Called every GCWAKEUP seconds to try to unlock all our partitions,
128  * if we're idle and there are no active transactions 
129  */
130 static void
131 TryUnlock(void)
132 {
133     /* if there are no running calls, and there are no active transactions, then
134      * it should be safe to release any partition locks we've accumulated */
135     VTRANS_LOCK;
136     if (runningCalls == 0 && TransList() == (struct volser_trans *)0) {
137         VTRANS_UNLOCK;
138         VPFullUnlock();         /* in volprocs.c */
139     } else
140         VTRANS_UNLOCK;
141 }
142
143 /* background daemon for timing out transactions */
144 static void*
145 BKGLoop(void *unused)
146 {
147     struct timeval tv;
148     int loop = 0;
149
150     while (1) {
151         tv.tv_sec = GCWAKEUP;
152         tv.tv_usec = 0;
153 #ifdef AFS_PTHREAD_ENV
154         select(0, 0, 0, 0, &tv);
155 #else
156         (void)IOMGR_Select(0, 0, 0, 0, &tv);
157 #endif
158         GCTrans();
159         TryUnlock();
160         loop++;
161         if (loop == 10) {       /* reopen log every 5 minutes */
162             loop = 0;
163             ReOpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
164         }
165     }
166
167     return NULL;
168 }
169
170 /* Background daemon for sleeping so the volserver does not become I/O bound */
171 afs_int32 TTsleep, TTrun;
172 static void *
173 BKGSleep(void *unused)
174 {
175     struct volser_trans *tt;
176
177     if (TTsleep) {
178         while (1) {
179 #ifdef AFS_PTHREAD_ENV
180             sleep(TTrun);
181 #else /* AFS_PTHREAD_ENV */
182             IOMGR_Sleep(TTrun);
183 #endif
184             VTRANS_LOCK;
185             for (tt = TransList(); tt; tt = tt->next) {
186                 if ((strcmp(tt->lastProcName, "DeleteVolume") == 0)
187                     || (strcmp(tt->lastProcName, "Clone") == 0)
188                     || (strcmp(tt->lastProcName, "ReClone") == 0)
189                     || (strcmp(tt->lastProcName, "Forward") == 0)
190                     || (strcmp(tt->lastProcName, "Restore") == 0)
191                     || (strcmp(tt->lastProcName, "ForwardMulti") == 0))
192                     break;
193             }
194             if (tt) {
195                 VTRANS_UNLOCK;
196                 sleep(TTsleep);
197             } else
198                 VTRANS_UNLOCK;
199         }
200     }
201     return NULL;
202 }
203
204 #ifndef AFS_NT40_ENV
205 int
206 volser_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
207 {
208     afs_uint32 rcode;
209 #ifndef AFS_LINUX20_ENV
210     void (*old) (int);
211
212     old = signal(SIGSYS, SIG_IGN);
213 #endif
214     rcode =
215         syscall(AFS_SYSCALL /* AFS_SYSCALL */ , 28 /* AFSCALL_CALL */ , a3,
216                 a4, a5);
217 #ifndef AFS_LINUX20_ENV
218     signal(SIGSYS, old);
219 #endif
220
221     return rcode;
222 }
223 #endif
224
225
226 /* check whether caller is authorized to manage RX statistics */
227 int
228 vol_rxstat_userok(struct rx_call *call)
229 {
230     return afsconf_SuperUser(tdir, call, NULL);
231 }
232
233 #include "AFS_component_version_number.c"
234 int 
235 main(int argc, char **argv)
236 {
237     register afs_int32 code;
238     struct rx_securityClass *(securityObjects[3]);
239     struct rx_service *service;
240     struct ktc_encryptionKey tkey;
241     int rxpackets = 100;
242     char commandLine[150];
243     int i;
244     int rxJumbograms = 0;       /* default is to send and receive jumbograms. */
245     int rxMaxMTU = -1;
246     int bufSize = 0;            /* temp variable to read in udp socket buf size */
247     afs_uint32 host = ntohl(INADDR_ANY);
248     char *auditFileName = NULL;
249
250 #ifdef  AFS_AIX32_ENV
251     /*
252      * The following signal action for AIX is necessary so that in case of a 
253      * crash (i.e. core is generated) we can include the user's data section 
254      * in the core dump. Unfortunately, by default, only a partial core is
255      * generated which, in many cases, isn't too useful.
256      */
257     struct sigaction nsa;
258
259     sigemptyset(&nsa.sa_mask);
260     nsa.sa_handler = SIG_DFL;
261     nsa.sa_flags = SA_FULLDUMP;
262     sigaction(SIGABRT, &nsa, NULL);
263     sigaction(SIGSEGV, &nsa, NULL);
264 #endif
265     osi_audit_init();
266     osi_audit(VS_StartEvent, 0, AUD_END);
267
268     /* Initialize dirpaths */
269     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
270 #ifdef AFS_NT40_ENV
271         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
272 #endif
273         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
274                 argv[0]);
275         exit(2);
276     }
277
278     for (commandLine[0] = '\0', i = 0; i < argc; i++) {
279         if (i > 0)
280             strcat(commandLine, " ");
281         strcat(commandLine, argv[i]);
282     }
283
284     TTsleep = TTrun = 0;
285
286     /* parse cmd line */
287     for (code = 1; code < argc; code++) {
288         if (strcmp(argv[code], "-log") == 0) {
289             /* set extra logging flag */
290             DoLogging = 1;
291         } else if (strcmp(argv[code], "-help") == 0) {
292             goto usage;
293         } else if (strcmp(argv[code], "-rxbind") == 0) {
294             rxBind = 1;
295         } else if (strcmp(argv[code], "-allow-dotted-principals") == 0) {
296             rxkadDisableDotCheck = 1;
297         } else if (strcmp(argv[code], "-d") == 0) {
298             if ((code + 1) >= argc) {
299                 fprintf(stderr, "missing argument for -d\n"); 
300                 return -1; 
301             }
302             debuglevel = atoi(argv[++code]);
303             LogLevel = debuglevel;
304         } else if (strcmp(argv[code], "-p") == 0) {
305             lwps = atoi(argv[++code]);
306             if (lwps > MAXLWP) {
307                 printf("Warning: '-p %d' is too big; using %d instead\n",
308                        lwps, MAXLWP);
309                 lwps = MAXLWP;
310             }
311         } else if (strcmp(argv[code], "-auditlog") == 0) {
312             auditFileName = argv[++code];
313
314         } else if (strcmp(argv[code], "-audit-interface") == 0) {
315             char *interface = argv[++code];
316
317             if (osi_audit_interface(interface)) {
318                 printf("Invalid audit interface '%s'\n", interface);
319                 return -1;
320             }
321         } else if (strcmp(argv[code], "-nojumbo") == 0) {
322             rxJumbograms = 0;
323         } else if (strcmp(argv[code], "-jumbo") == 0) {
324             rxJumbograms = 1;
325         } else if (!strcmp(argv[code], "-rxmaxmtu")) {
326             if ((code + 1) >= argc) {
327                 fprintf(stderr, "missing argument for -rxmaxmtu\n"); 
328                 exit(1); 
329             }
330             rxMaxMTU = atoi(argv[++code]);
331             if ((rxMaxMTU < RX_MIN_PACKET_SIZE) || 
332                 (rxMaxMTU > RX_MAX_PACKET_DATA_SIZE)) {
333                 printf("rxMaxMTU %d invalid; must be between %d-%lu\n",
334                        rxMaxMTU, RX_MIN_PACKET_SIZE, 
335                        RX_MAX_PACKET_DATA_SIZE);
336                 exit(1);
337             }
338         } else if (strcmp(argv[code], "-sleep") == 0) {
339             sscanf(argv[++code], "%d/%d", &TTsleep, &TTrun);
340             if ((TTsleep < 0) || (TTrun <= 0)) {
341                 printf("Warning: '-sleep %d/%d' is incorrect; ignoring\n",
342                        TTsleep, TTrun);
343                 TTsleep = TTrun = 0;
344             }
345         } else if (strcmp(argv[code], "-udpsize") == 0) {
346             if ((code + 1) >= argc) {
347                 printf("You have to specify -udpsize <integer value>\n");
348                 exit(1);
349             }
350             sscanf(argv[++code], "%d", &bufSize);
351             if (bufSize < rx_GetMinUdpBufSize())
352                 printf
353                     ("Warning:udpsize %d is less than minimum %d; ignoring\n",
354                      bufSize, rx_GetMinUdpBufSize());
355             else
356                 udpBufSize = bufSize;
357         } else if (strcmp(argv[code], "-enable_peer_stats") == 0) {
358             rx_enablePeerRPCStats();
359         } else if (strcmp(argv[code], "-enable_process_stats") == 0) {
360             rx_enableProcessRPCStats();
361         }
362 #ifndef AFS_NT40_ENV
363         else if (strcmp(argv[code], "-syslog") == 0) {
364             /* set syslog logging flag */
365             serverLogSyslog = 1;
366         } else if (strncmp(argv[code], "-syslog=", 8) == 0) {
367             serverLogSyslog = 1;
368             serverLogSyslogFacility = atoi(argv[code] + 8);
369         }
370 #endif
371         else {
372             printf("volserver: unrecognized flag '%s'\n", argv[code]);
373           usage:
374 #ifndef AFS_NT40_ENV
375             printf("Usage: volserver [-log] [-p <number of processes>] "
376                    "[-auditlog <log path>] [-d <debug level>] "
377                    "[-nojumbo] [-jumbo] [-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
378                    "[-udpsize <size of socket buffer in bytes>] "
379                    "[-syslog[=FACILITY]] "
380                    "[-enable_peer_stats] [-enable_process_stats] "
381                    "[-help]\n");
382 #else
383             printf("Usage: volserver [-log] [-p <number of processes>] "
384                    "[-auditlog <log path>] [-d <debug level>] "
385                    "[-nojumbo] [-jumbo] [-rxmaxmtu <bytes>] [-rxbind] [-allow-dotted-principals] "
386                    "[-udpsize <size of socket buffer in bytes>] "
387                    "[-enable_peer_stats] [-enable_process_stats] "
388                    "[-help]\n");
389 #endif
390             VS_EXIT(1);
391         }
392     }
393
394     if (auditFileName) {
395         osi_audit_file(auditFileName);
396         osi_audit(VS_StartEvent, 0, AUD_END);
397     }
398 #ifdef AFS_SGI_VNODE_GLUE
399     if (afs_init_kernel_config(-1) < 0) {
400         printf
401             ("Can't determine NUMA configuration, not starting volserver.\n");
402         exit(1);
403     }
404 #endif
405     InitErrTabs();
406
407 #ifdef AFS_PTHREAD_ENV
408     SetLogThreadNumProgram( threadNum );
409 #endif
410
411 #ifdef AFS_NT40_ENV
412     if (afs_winsockInit() < 0) {
413         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
414         printf("Volume server unable to start winsock, exiting.\n");
415         exit(1);
416     }
417 #endif
418     /* Open VolserLog and map stdout, stderr into it; VInitVolumePackage can
419        log, so we need to do this here */
420     OpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
421
422     VInitVolumePackage(volumeUtility, 0, 0, CONNECT_FS, 0);
423     /* For nuke() */
424     Lock_Init(&localLock);
425     DInit(40);
426 #ifndef AFS_PTHREAD_ENV
427     vol_PollProc = IOMGR_Poll;  /* tell vol pkg to poll io system periodically */
428 #endif
429 #ifndef AFS_NT40_ENV
430     rxi_syscallp = volser_syscall;
431 #endif
432     rx_nPackets = rxpackets;    /* set the max number of packets */
433     if (udpBufSize)
434         rx_SetUdpBufSize(udpBufSize);   /* set the UDP buffer size for receive */
435     if (rxBind) {
436         afs_int32 ccode;
437         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || 
438             AFSDIR_SERVER_NETINFO_FILEPATH) {
439             char reason[1024];
440             ccode = parseNetFiles(SHostAddrs, NULL, NULL,
441                                            ADDRSPERSITE, reason,
442                                            AFSDIR_SERVER_NETINFO_FILEPATH,
443                                            AFSDIR_SERVER_NETRESTRICT_FILEPATH);
444         } else 
445         {
446             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
447         }
448         if (ccode == 1) 
449             host = SHostAddrs[0];
450     }
451
452     code = rx_InitHost(host, (int)htons(AFSCONF_VOLUMEPORT));
453     if (code) {
454         fprintf(stderr, "rx init failed on socket AFSCONF_VOLUMEPORT %u\n",
455                 AFSCONF_VOLUMEPORT);
456         VS_EXIT(1);
457     }
458     if (!rxJumbograms) {
459         /* Don't allow 3.4 vos clients to send jumbograms and we don't send. */
460         rx_SetNoJumbo();
461     }
462     if (rxMaxMTU != -1) {
463         rx_SetMaxMTU(rxMaxMTU);
464     }
465     rx_GetIFInfo();
466     rx_SetRxDeadTime(420);
467     memset(busyFlags, 0, sizeof(busyFlags));
468
469     SetupLogSignals();
470
471     {
472 #ifdef AFS_PTHREAD_ENV
473         pthread_t tid;
474         pthread_attr_t tattr;
475         assert(pthread_attr_init(&tattr) == 0);
476         assert(pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) == 0);
477
478         assert(pthread_create(&tid, &tattr, BKGLoop, NULL) == 0);
479 #else
480         PROCESS pid;
481         LWP_CreateProcess(BKGLoop, 16*1024, 3, 0, "vol bkg daemon", &pid);
482         LWP_CreateProcess(BKGSleep,16*1024, 3, 0, "vol slp daemon", &pid);
483 #endif
484     }
485
486     /* Create a single security object, in this case the null security object, for unauthenticated connections, which will be used to control security on connections made to this server */
487
488     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
489     if (!tdir) {
490         Abort("volser: could not open conf files in %s\n",
491               AFSDIR_SERVER_ETC_DIRPATH);
492         VS_EXIT(1);
493     }
494     afsconf_GetKey(tdir, 999, &tkey);
495     securityObjects[0] = rxnull_NewServerSecurityObject();
496     securityObjects[1] = (struct rx_securityClass *)0;  /* don't bother with rxvab */
497     securityObjects[2] =
498         rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
499     if (securityObjects[0] == (struct rx_securityClass *)0)
500         Abort("rxnull_NewServerSecurityObject");
501     service =
502         rx_NewServiceHost(host, 0, VOLSERVICE_ID, "VOLSER", securityObjects, 3,
503                       AFSVolExecuteRequest);
504     if (service == (struct rx_service *)0)
505         Abort("rx_NewService");
506     rx_SetBeforeProc(service, MyBeforeProc);
507     rx_SetAfterProc(service, MyAfterProc);
508     rx_SetIdleDeadTime(service, 0);     /* never timeout */
509     if (lwps < 4)
510         lwps = 4;
511     rx_SetMaxProcs(service, lwps);
512 #if defined(AFS_XBSD_ENV)
513     rx_SetStackSize(service, (128 * 1024));
514 #elif defined(AFS_SGI_ENV)
515     rx_SetStackSize(service, (48 * 1024));
516 #else
517     rx_SetStackSize(service, (32 * 1024));
518 #endif
519
520     if (rxkadDisableDotCheck) {
521         rx_SetSecurityConfiguration(service, RXS_CONFIG_FLAGS,
522                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
523     }
524
525     service =
526         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", securityObjects, 3,
527                       RXSTATS_ExecuteRequest);
528     if (service == (struct rx_service *)0)
529         Abort("rx_NewService");
530     rx_SetMinProcs(service, 2);
531     rx_SetMaxProcs(service, 4);
532
533     Log("Starting AFS Volserver %s (%s)\n", VolserVersion, commandLine);
534     if (TTsleep) {
535         Log("Will sleep %d second%s every %d second%s\n", TTsleep,
536             (TTsleep > 1) ? "s" : "", TTrun + TTsleep,
537             (TTrun + TTsleep > 1) ? "s" : "");
538     }
539
540     /* allow super users to manage RX statistics */
541     rx_SetRxStatUserOk(vol_rxstat_userok);
542
543     rx_StartServer(1);          /* Donate this process to the server process pool */
544
545     osi_audit(VS_FinishEvent, (-1), AUD_END);
546     Abort("StartServer returned?");
547     return 0; /* not reached */
548 }