prototypes-fixes-20020821
[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 RCSID("$Header$");
14
15 #include <sys/types.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 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #else
29 #ifdef HAVE_STRINGS_H
30 #include <strings.h>
31 #endif
32 #endif
33 #include <rx/xdr.h>
34 #include <afs/afsint.h>
35 #include <stdio.h>
36 #include <signal.h>
37 #include <afs/assert.h>
38 #include <afs/prs_fs.h>
39 #include <afs/nfs.h>
40 #include <lwp.h>
41 #include <lock.h>
42 #include <afs/afssyscalls.h>
43 #include <afs/ihandle.h>
44 #ifdef AFS_NT40_ENV
45 #include <afs/ntops.h>
46 #endif
47 #include <afs/vnode.h>
48 #include <afs/volume.h>
49 #include <afs/partition.h>
50 #include <rx/rx.h>
51 #include <rx/rx_globals.h>
52 #include <afs/auth.h>
53 #include <rx/rxkad.h>
54 #include <afs/cellconfig.h>
55 #include <afs/keys.h>
56 #include <ubik.h>
57
58 #include "volser.h"
59 #include <errno.h>
60 #include <afs/audit.h>
61 #include <afs/afsutil.h>
62
63 #define VolserVersion "2.0"
64 #define N_SECURITY_OBJECTS 3
65
66 extern int (*vol_PollProc)();
67 extern struct volser_trans *TransList();
68 extern int IOMGR_Poll();
69 char *GlobalNameHack = NULL;
70 int hackIsIn = 0;
71 afs_int32 GlobalVolCloneId, GlobalVolParentId;
72 int GlobalVolType;
73 int VolumeChanged;  /* XXXX */
74 static char busyFlags[MAXHELPERS];
75 struct volser_trans *QI_GlobalWriteTrans = 0;
76 extern int QI_write();
77 extern int QI_flush();
78 extern int (*VolWriteProc)();
79 extern int (*VolFlushProc)();
80 extern void AFSVolExecuteRequest();
81 extern void RXSTATS_ExecuteRequest();
82 extern Log();
83 struct afsconf_dir *tdir;
84 static afs_int32 runningCalls=0;
85 int DoLogging = 0;
86 #define MAXLWP 16
87 int lwps = 9;
88 int     udpBufSize = 0;   /* UDP buffer size for receive*/
89
90 int Testing = 0; /* for ListViceInodes */
91
92 #define VS_EXIT(code)  {                                          \
93                           osi_audit(VS_ExitEvent, code, AUD_END); \
94                           exit(code);                             \
95                        }
96
97
98 static MyBeforeProc (struct rx_call *acall)
99 {
100     runningCalls++;
101     return 0;
102 }
103
104 static MyAfterProc (struct rx_call *acall, afs_int32 code)
105 {
106     runningCalls--;
107     return 0;
108 }
109
110 /* Called every GCWAKEUP seconds to try to unlock all our partitions,
111  * if we're idle and there are no active transactions 
112  */
113 static TryUnlock() {
114     /* if there are no running calls, and there are no active transactions, then
115        it should be safe to release any partition locks we've accumulated */
116     if (runningCalls == 0 && TransList() == (struct volser_trans *) 0) {
117         VPFullUnlock();         /* in volprocs.c */
118     }
119 }
120
121 /* background daemon for timing out transactions */
122 static BKGLoop() {
123     struct timeval tv;
124     int loop=0;
125
126     while (1) {
127         tv.tv_sec = GCWAKEUP;
128         tv.tv_usec = 0;
129         (void) IOMGR_Select(0, 0, 0, 0, &tv);
130         GCTrans();
131         TryUnlock();
132         loop++;
133         if ( loop == 10 )       
134         {                       /* reopen log every 5 minutes */
135                 loop = 0; 
136                 ReOpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
137         }
138     }
139 }
140
141 /* Background daemon for sleeping so the volserver does not become I/O bound */
142 afs_int32 TTsleep, TTrun;
143 static BKGSleep()
144 {
145   struct volser_trans *tt;
146
147   if (TTsleep) {
148      while (1) {
149         IOMGR_Sleep(TTrun);
150         for (tt=TransList(); tt; tt=tt->next) {
151            if ( (strcmp(tt->lastProcName,"DeleteVolume") == 0) ||
152                 (strcmp(tt->lastProcName,"Clone")        == 0) ||
153                 (strcmp(tt->lastProcName,"ReClone")      == 0) ||
154                 (strcmp(tt->lastProcName,"Forward")      == 0) ||
155                 (strcmp(tt->lastProcName,"Restore")      == 0) ||
156                 (strcmp(tt->lastProcName,"ForwardMulti") == 0) )
157               break;
158         }
159         if (tt) {
160            sleep(TTsleep);
161         }
162      }
163   }
164 }
165
166 #ifndef AFS_NT40_ENV
167 int volser_syscall(a3, a4, a5)
168 afs_uint32 a3, a4;
169 void * a5;
170 {
171   afs_uint32 rcode;
172   void (*old)();
173         
174 #ifndef AFS_LINUX20_ENV
175   old = signal(SIGSYS, SIG_IGN);        
176 #endif
177   rcode = syscall (AFS_SYSCALL /* AFS_SYSCALL */, 28 /* AFSCALL_CALL */, a3, a4, a5);
178 #ifndef AFS_LINUX20_ENV
179   signal(SIGSYS, old);  
180 #endif
181
182   return rcode;
183 }
184 #endif
185
186
187 /* check whether caller is authorized to manage RX statistics */
188 int vol_rxstat_userok(call)
189     struct rx_call *call;
190 {
191     return afsconf_SuperUser(tdir, call, NULL);
192 }
193
194 #include "AFS_component_version_number.c"
195 main(argc, argv)
196 int argc;
197 char **argv; {
198     char *pid;
199     register afs_int32 code;
200     struct rx_securityClass *(securityObjects[3]);
201     struct rx_service *service;
202     struct ktc_encryptionKey tkey;
203     int rxpackets = 100;
204     char commandLine[150];
205     int i;
206     int rxJumbograms = 1; /* default is to send and receive jumbograms. */
207     int bufSize = 0;      /* temp variable to read in udp socket buf size*/
208
209 #ifdef  AFS_AIX32_ENV
210     /*
211      * The following signal action for AIX is necessary so that in case of a 
212      * crash (i.e. core is generated) we can include the user's data section 
213      * in the core dump. Unfortunately, by default, only a partial core is
214      * generated which, in many cases, isn't too useful.
215      */
216     struct sigaction nsa;
217     
218     sigemptyset(&nsa.sa_mask);
219     nsa.sa_handler = SIG_DFL;
220     nsa.sa_flags = SA_FULLDUMP;
221     sigaction(SIGABRT, &nsa, NULL);
222     sigaction(SIGSEGV, &nsa, NULL);
223 #endif
224     osi_audit(VS_StartEvent, 0, AUD_END);
225
226     /* Initialize dirpaths */
227     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
228 #ifdef AFS_NT40_ENV
229         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0],0);
230 #endif
231         fprintf(stderr,"%s: Unable to obtain AFS server directory.\n", argv[0]);
232         exit(2);
233     }
234     
235     for (commandLine[0] = '\0', i=0; i<argc; i++) {
236         if (i > 0) strcat(commandLine, " ");
237         strcat(commandLine, argv[i]);
238     }
239
240     TTsleep = TTrun = 0;
241
242     /* parse cmd line */
243     for(code=1;code<argc;code++) {
244         if (strcmp(argv[code], "-log")==0) {
245             /* set extra logging flag */
246             DoLogging = 1;
247         }
248         else if (strcmp(argv[code], "-help")==0) {
249             goto usage;
250         }
251         else if (strcmp(argv[code], "-p") == 0) {
252             lwps = atoi(argv[++code]);
253             if (lwps > MAXLWP) {
254                 printf("Warning: '-p %d' is too big; using %d instead\n",
255                        lwps, MAXLWP);
256                 lwps = MAXLWP;
257             }
258         }
259         else if (strcmp(argv[code], "-nojumbo")==0) {
260             rxJumbograms = 0;
261         }
262         else if (strcmp(argv[code], "-sleep")==0) {
263             sscanf(argv[++code], "%d/%d", &TTsleep, &TTrun);
264             if ((TTsleep < 0) || (TTrun <= 0)) {
265                 printf("Warning: '-sleep %d/%d' is incorrect; ignoring\n",
266                        TTsleep, TTrun);
267                 TTsleep = TTrun = 0;
268             }
269         }
270         else if ( strcmp(argv[code], "-udpsize")==0) {
271             if ( (code+1) >= argc ) {
272                 printf("You have to specify -udpsize <integer value>\n");
273                 exit(1);
274             }
275             sscanf(argv[++code], "%d", &bufSize);
276             if ( bufSize < rx_GetMinUdpBufSize() ) 
277                  printf("Warning:udpsize %d is less than minimum %d; ignoring\n",
278                         bufSize, rx_GetMinUdpBufSize() );
279             else
280                 udpBufSize = bufSize;
281         }       
282         else if (strcmp(argv[code], "-enable_peer_stats")==0) {
283             rx_enablePeerRPCStats();
284         }
285         else if (strcmp(argv[code], "-enable_process_stats")==0) {
286             rx_enableProcessRPCStats();
287         }
288 #ifndef AFS_NT40_ENV
289         else if (strcmp(argv[code], "-syslog")==0) {
290             /* set syslog logging flag */
291             serverLogSyslog = 1;
292         } 
293         else if (strncmp(argv[code], "-syslog=", 8)==0) {
294             serverLogSyslog = 1;
295             serverLogSyslogFacility = atoi(argv[code]+8);
296         }
297 #endif
298         else {
299             printf("volserver: unrecognized flag '%s'\n", argv[code]);
300 usage:
301 #ifndef AFS_NT40_ENV
302             printf("Usage: volserver [-log] [-p <number of processes>] "
303                    "[-udpsize <size of socket buffer in bytes>] "
304                    "[-syslog[=FACILITY]] "
305                    "[-enable_peer_stats] [-enable_process_stats] "
306                    "[-help]\n");
307 #else
308             printf("Usage: volserver [-log] [-p <number of processes>] "
309                    "[-udpsize <size of socket buffer in bytes>] "
310                    "[-enable_peer_stats] [-enable_process_stats] "
311                    "[-help]\n");
312 #endif
313             VS_EXIT(1);
314         }
315     }
316 #ifdef AFS_SGI_VNODE_GLUE
317     if (afs_init_kernel_config(-1) <0) {
318         printf("Can't determine NUMA configuration, not starting volserver.\n");
319         exit(1);
320     }
321 #endif
322     InitErrTabs();
323
324 #ifdef AFS_NT40_ENV
325     if (afs_winsockInit()<0) {
326         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
327         printf("Volume server unable to start winsock, exiting.\n");
328         exit(1);
329     }
330 #endif
331     VInitVolumePackage(volumeUtility, 0, 0, CONNECT_FS, 0);
332     DInit(40);
333     vol_PollProc = IOMGR_Poll;  /* tell vol pkg to poll io system periodically */
334 #ifndef AFS_NT40_ENV
335     rxi_syscallp = volser_syscall;
336 #endif
337     rx_nPackets = rxpackets; /* set the max number of packets */
338     if ( udpBufSize)
339         rx_SetUdpBufSize(udpBufSize);/* set the UDP buffer size for receive */
340     code = rx_Init((int)htons(AFSCONF_VOLUMEPORT));
341     if (code) {
342         fprintf(stderr,"rx init failed on socket AFSCONF_VOLUMEPORT %u\n",AFSCONF_VOLUMEPORT);
343         VS_EXIT(1);
344     }
345     if (!rxJumbograms) {
346         /* Don't allow 3.4 vos clients to send jumbograms and we don't send. */
347         rx_SetNoJumbo();
348     }
349     rx_GetIFInfo();
350     rx_SetRxDeadTime(420);
351     memset(busyFlags, 0, sizeof(busyFlags));
352
353     /* Open FileLog and map stdout, stderr into it */
354     OpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
355     SetupLogSignals();
356
357     /* create the lwp to garbage-collect old transactions and sleep periodically */
358     LWP_CreateProcess(BKGLoop, 16*1024, 3, (void *) 0, "vol bkg daemon", &pid);
359     LWP_CreateProcess(BKGSleep,16*1024, 3, (void *) 0, "vol slp daemon", &pid);
360
361     /* 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 */
362
363     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
364     if (!tdir) {
365         Abort("volser: could not open conf files in %s\n", AFSDIR_SERVER_ETC_DIRPATH);
366         VS_EXIT(1);
367     }
368     afsconf_GetKey(tdir, 999, &tkey);
369     securityObjects[0] = rxnull_NewServerSecurityObject();
370     securityObjects[1] = (struct rx_securityClass *) 0; /* don't bother with rxvab */
371     securityObjects[2] = rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
372     if (securityObjects[0] == (struct rx_securityClass *) 0) Abort("rxnull_NewServerSecurityObject");
373     service = rx_NewService(0, VOLSERVICE_ID, "VOLSER", securityObjects, 3, AFSVolExecuteRequest);
374     if (service == (struct rx_service *) 0) Abort("rx_NewService");
375     rx_SetBeforeProc(service, MyBeforeProc);
376     rx_SetAfterProc(service, MyAfterProc);
377     rx_SetIdleDeadTime(service, 0);     /* never timeout */
378     if (lwps < 4)
379         lwps = 4;
380     rx_SetMaxProcs(service, lwps);
381     rx_SetStackSize(service, 32768);
382
383     service = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", securityObjects, 3, RXSTATS_ExecuteRequest);
384     if (service == (struct rx_service *) 0) Abort("rx_NewService");
385     rx_SetMinProcs(service, 2);
386     rx_SetMaxProcs(service, 4);
387
388     Log("Starting AFS Volserver %s (%s)\n", VolserVersion, commandLine);
389     if (TTsleep) {
390        Log("Will sleep %d second%s every %d second%s\n",
391            TTsleep, (TTsleep>1)?"s":"", 
392            TTrun+TTsleep, (TTrun+TTsleep>1)?"s":"");
393     }
394
395     /* allow super users to manage RX statistics */
396     rx_SetRxStatUserOk(vol_rxstat_userok);
397
398     rx_StartServer(1); /* Donate this process to the server process pool */
399
400     osi_audit(VS_FinishEvent, (-1), AUD_END);
401     Abort("StartServer returned?");
402 }
403