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