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