volser: update log formatting in dump and restore
[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 #include <roken.h>
14 #include <afs/opr.h>
15 #ifdef AFS_PTHREAD_ENV
16 # include <opr/lock.h>
17 #endif
18
19 #ifdef AFS_NT40_ENV
20 #include <windows.h>
21 #include <WINNT/afsevent.h>
22 #endif
23
24 #include <rx/rx_queue.h>
25 #include <afs/afsint.h>
26 #include <afs/prs_fs.h>
27 #include <afs/nfs.h>
28 #include <lwp.h>
29 #include <lock.h>
30 #include <afs/afssyscalls.h>
31 #include <afs/ihandle.h>
32 #ifdef AFS_NT40_ENV
33 #include <afs/ntops.h>
34 #endif
35 #include <afs/vnode.h>
36 #include <afs/volume.h>
37 #include <afs/partition.h>
38 #include <rx/rx.h>
39 #include <rx/rxstat.h>
40 #include <rx/rx_globals.h>
41 #include <afs/auth.h>
42 #include <afs/cellconfig.h>
43 #include <afs/keys.h>
44 #include <afs/dir.h>
45 #include <ubik.h>
46 #include <afs/audit.h>
47 #include <afs/afsutil.h>
48 #include <afs/cmd.h>
49 #include <lwp.h>
50
51 #include "volser.h"
52 #include "volint.h"
53 #include "volser_internal.h"
54
55 #define VolserVersion "2.0"
56 #define N_SECURITY_OBJECTS 3
57
58 extern struct Lock localLock;
59 char *GlobalNameHack = NULL;
60 int hackIsIn = 0;
61 afs_int32 GlobalVolCloneId, GlobalVolParentId;
62 int GlobalVolType;
63 int VolumeChanged;              /* XXXX */
64 static char busyFlags[MAXHELPERS];
65 struct volser_trans *QI_GlobalWriteTrans = 0;
66 struct afsconf_dir *tdir;
67 static afs_int32 runningCalls = 0;
68 int DoLogging = 0;
69 int debuglevel = 0;
70 #define MAXLWP 128
71 int lwps = 9;
72 int udpBufSize = 0;             /* UDP buffer size for receive */
73 int restrictedQueryLevel = RESTRICTED_QUERY_ANYUSER;
74
75 int rxBind = 0;
76 int rxkadDisableDotCheck = 0;
77 int DoPreserveVolumeStats = 0;
78 int rxJumbograms = 0;   /* default is to not send and receive jumbograms. */
79 int rxMaxMTU = -1;
80 char *auditFileName = NULL;
81 char *logFile = NULL;
82 char *configDir = NULL;
83
84 #define ADDRSPERSITE 16         /* Same global is in rx/rx_user.c */
85 afs_uint32 SHostAddrs[ADDRSPERSITE];
86
87 #define VS_EXIT(code)  {                                          \
88                           osi_audit(VS_ExitEvent, code, AUD_END); \
89                           exit(code);                             \
90                        }
91
92 static void
93 MyBeforeProc(struct rx_call *acall)
94 {
95     VTRANS_LOCK;
96     runningCalls++;
97     VTRANS_UNLOCK;
98     return;
99 }
100
101 static void
102 MyAfterProc(struct rx_call *acall, afs_int32 code)
103 {
104     VTRANS_LOCK;
105     runningCalls--;
106     VTRANS_UNLOCK;
107     return;
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 void
114 TryUnlock(void)
115 {
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     VTRANS_LOCK;
119     if (runningCalls == 0 && TransList() == (struct volser_trans *)0) {
120         VTRANS_UNLOCK;
121         VPFullUnlock();         /* in volprocs.c */
122     } else
123         VTRANS_UNLOCK;
124 }
125
126 /* background daemon for timing out transactions */
127 static void*
128 BKGLoop(void *unused)
129 {
130     struct timeval tv;
131     int loop = 0;
132
133     afs_pthread_setname_self("vol bkg");
134     while (1) {
135         tv.tv_sec = GCWAKEUP;
136         tv.tv_usec = 0;
137 #ifdef AFS_PTHREAD_ENV
138 #ifdef AFS_NT40_ENV
139         Sleep(GCWAKEUP * 1000);
140 #else
141         select(0, 0, 0, 0, &tv);
142 #endif
143 #else
144         (void)IOMGR_Select(0, 0, 0, 0, &tv);
145 #endif
146         GCTrans();
147         TryUnlock();
148         loop++;
149         if (loop == 10) {       /* reopen log every 5 minutes */
150             loop = 0;
151             ReOpenLog(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
152         }
153     }
154
155     return NULL;
156 }
157
158 #ifdef AFS_NT40_ENV
159 /* no volser_syscall */
160 #elif defined(AFS_SUN511_ENV)
161 int
162 volser_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
163 {
164     int err, code;
165     code = ioctl_sun_afs_syscall(28 /* AFSCALL_CALL */, a3, a4, a5, 0, 0, 0,
166                                  &err);
167     if (code) {
168         err = code;
169     }
170     return err;
171 }
172 #else
173 int
174 volser_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
175 {
176     afs_uint32 rcode;
177 #ifndef AFS_LINUX20_ENV
178     void (*old) (int);
179
180     old = signal(SIGSYS, SIG_IGN);
181 #endif
182     rcode =
183         syscall(AFS_SYSCALL /* AFS_SYSCALL */ , 28 /* AFSCALL_CALL */ , a3,
184                 a4, a5);
185 #ifndef AFS_LINUX20_ENV
186     signal(SIGSYS, old);
187 #endif
188
189     return rcode;
190 }
191 #endif
192
193
194 /* check whether caller is authorized to manage RX statistics */
195 int
196 vol_rxstat_userok(struct rx_call *call)
197 {
198     return afsconf_SuperUser(tdir, call, NULL);
199 }
200
201 /**
202  * Return true if this name is a member of the local realm.
203  */
204 static int
205 vol_IsLocalRealmMatch(void *rock, char *name, char *inst, char *cell)
206 {
207     struct afsconf_dir *dir = (struct afsconf_dir *)rock;
208     afs_int32 islocal = 0;      /* default to no */
209     int code;
210
211     code = afsconf_IsLocalRealmMatch(dir, &islocal, name, inst, cell);
212     if (code) {
213         ViceLog(0,
214                 ("Failed local realm check; code=%d, name=%s, inst=%s, cell=%s\n",
215                  code, name, inst, cell));
216     }
217     return islocal;
218 }
219
220 enum optionsList {
221     OPT_log,
222     OPT_rxbind,
223     OPT_dotted,
224     OPT_debug,
225     OPT_threads,
226     OPT_auditlog,
227     OPT_audit_interface,
228     OPT_nojumbo,
229     OPT_jumbo,
230     OPT_rxmaxmtu,
231     OPT_sleep,
232     OPT_udpsize,
233     OPT_peer,
234     OPT_process,
235     OPT_preserve_vol_stats,
236     OPT_sync,
237     OPT_syslog,
238     OPT_logfile,
239     OPT_config,
240     OPT_restricted_query
241 };
242
243 static int
244 ParseArgs(int argc, char **argv) {
245     int code;
246     int optval;
247     char *optstring = NULL;
248     struct cmd_syndesc *opts;
249     char *sleepSpec = NULL;
250     char *sync_behavior = NULL;
251     char *restricted_query_parameter = NULL;
252
253     opts = cmd_CreateSyntax(NULL, NULL, NULL, 0, NULL);
254     cmd_AddParmAtOffset(opts, OPT_log, "-log", CMD_FLAG, CMD_OPTIONAL,
255            "log vos users");
256     cmd_AddParmAtOffset(opts, OPT_rxbind, "-rxbind", CMD_FLAG, CMD_OPTIONAL,
257            "bind only to the primary interface");
258     cmd_AddParmAtOffset(opts, OPT_dotted, "-allow-dotted-principals", CMD_FLAG, CMD_OPTIONAL,
259            "permit Kerberos 5 principals with dots");
260     cmd_AddParmAtOffset(opts, OPT_debug, "-d", CMD_SINGLE, CMD_OPTIONAL,
261            "debug level");
262     cmd_AddParmAtOffset(opts, OPT_threads, "-p", CMD_SINGLE, CMD_OPTIONAL,
263            "number of threads");
264     cmd_AddParmAtOffset(opts, OPT_auditlog, "-auditlog", CMD_SINGLE,
265            CMD_OPTIONAL, "location of audit log");
266     cmd_AddParmAtOffset(opts, OPT_audit_interface, "-audit-interface",
267            CMD_SINGLE, CMD_OPTIONAL, "interface to use for audit logging");
268     cmd_AddParmAtOffset(opts, OPT_nojumbo, "-nojumbo", CMD_FLAG, CMD_OPTIONAL,
269             "disable jumbograms");
270     cmd_AddParmAtOffset(opts, OPT_jumbo, "-jumbo", CMD_FLAG, CMD_OPTIONAL,
271             "enable jumbograms");
272     cmd_AddParmAtOffset(opts, OPT_rxmaxmtu, "-rxmaxmtu", CMD_SINGLE,
273             CMD_OPTIONAL, "maximum MTU for RX");
274     cmd_AddParmAtOffset(opts, OPT_udpsize, "-udpsize", CMD_SINGLE,
275             CMD_OPTIONAL, "size of socket buffer in bytes");
276     cmd_AddParmAtOffset(opts, OPT_sleep, "-sleep", CMD_SINGLE,
277             CMD_OPTIONAL, "make background daemon sleep (LWP only)");
278     cmd_AddParmAtOffset(opts, OPT_peer, "-enable_peer_stats", CMD_FLAG,
279             CMD_OPTIONAL, "enable RX transport statistics");
280     cmd_AddParmAtOffset(opts, OPT_process, "-enable_process_stats", CMD_FLAG,
281             CMD_OPTIONAL, "enable RX RPC statistics");
282     cmd_AddParmAtOffset(opts, OPT_preserve_vol_stats, "-preserve-vol-stats", CMD_FLAG,
283             CMD_OPTIONAL, "preserve volume statistics");
284 #if !defined(AFS_NT40_ENV)
285     cmd_AddParmAtOffset(opts, OPT_syslog, "-syslog", CMD_SINGLE_OR_FLAG,
286             CMD_OPTIONAL, "log to syslog");
287 #endif
288     cmd_AddParmAtOffset(opts, OPT_sync, "-sync",
289             CMD_SINGLE, CMD_OPTIONAL, "always | onclose | never");
290     cmd_AddParmAtOffset(opts, OPT_logfile, "-logfile", CMD_SINGLE,
291            CMD_OPTIONAL, "location of log file");
292     cmd_AddParmAtOffset(opts, OPT_config, "-config", CMD_SINGLE,
293            CMD_OPTIONAL, "configuration location");
294     cmd_AddParmAtOffset(opts, OPT_restricted_query, "-restricted_query",
295             CMD_SINGLE, CMD_OPTIONAL, "anyuser | admin");
296
297     code = cmd_Parse(argc, argv, &opts);
298     if (code == CMD_HELP) {
299         exit(0);
300     }
301     if (code)
302         return 1;
303
304     cmd_OptionAsFlag(opts, OPT_log, &DoLogging);
305     cmd_OptionAsFlag(opts, OPT_rxbind, &rxBind);
306     cmd_OptionAsFlag(opts, OPT_dotted, &rxkadDisableDotCheck);
307     cmd_OptionAsFlag(opts, OPT_preserve_vol_stats, &DoPreserveVolumeStats);
308     cmd_OptionAsInt(opts, OPT_debug, &LogLevel);
309     if (cmd_OptionPresent(opts, OPT_peer))
310         rx_enablePeerRPCStats();
311     if (cmd_OptionPresent(opts, OPT_process))
312         rx_enableProcessRPCStats();
313     if (cmd_OptionPresent(opts, OPT_nojumbo))
314         rxJumbograms = 0;
315     if (cmd_OptionPresent(opts, OPT_jumbo))
316         rxJumbograms = 1;
317 #ifndef AFS_NT40_ENV
318     if (cmd_OptionPresent(opts, OPT_syslog)) {
319         serverLogSyslog = 1;
320         cmd_OptionAsInt(opts, OPT_syslog, &serverLogSyslogFacility);
321     }
322 #endif
323     cmd_OptionAsInt(opts, OPT_rxmaxmtu, &rxMaxMTU);
324     if (cmd_OptionAsInt(opts, OPT_udpsize, &optval) == 0) {
325         if (optval < rx_GetMinUdpBufSize()) {
326             printf("Warning:udpsize %d is less than minimum %d; ignoring\n",
327                     optval, rx_GetMinUdpBufSize());
328         } else
329             udpBufSize = optval;
330     }
331     cmd_OptionAsString(opts, OPT_auditlog, &auditFileName);
332
333     if (cmd_OptionAsString(opts, OPT_audit_interface, &optstring) == 0) {
334         if (osi_audit_interface(optstring)) {
335             printf("Invalid audit interface '%s'\n", optstring);
336             return -1;
337         }
338         free(optstring);
339         optstring = NULL;
340     }
341     if (cmd_OptionAsInt(opts, OPT_threads, &lwps) == 0) {
342         if (lwps > MAXLWP) {
343             printf("Warning: '-p %d' is too big; using %d instead\n", lwps, MAXLWP);
344             lwps = MAXLWP;
345         }
346     }
347     if (cmd_OptionAsString(opts, OPT_sleep, &sleepSpec) == 0) {
348         printf("Warning: -sleep option ignored; this option is obsolete\n");
349     }
350     if (cmd_OptionAsString(opts, OPT_sync, &sync_behavior) == 0) {
351         if (ih_SetSyncBehavior(sync_behavior)) {
352             printf("Invalid -sync value %s\n", sync_behavior);
353             return -1;
354         }
355     }
356     cmd_OptionAsString(opts, OPT_logfile, &logFile);
357     cmd_OptionAsString(opts, OPT_config, &configDir);
358     if (cmd_OptionAsString(opts, OPT_restricted_query,
359                            &restricted_query_parameter) == 0) {
360         if (strcmp(restricted_query_parameter, "anyuser") == 0)
361             restrictedQueryLevel = RESTRICTED_QUERY_ANYUSER;
362         else if (strcmp(restricted_query_parameter, "admin") == 0)
363             restrictedQueryLevel = RESTRICTED_QUERY_ADMIN;
364         else {
365             printf("invalid argument for -restricted_query: %s\n",
366                    restricted_query_parameter);
367             return -1;
368         }
369         free(restricted_query_parameter);
370     }
371
372     return 0;
373 }
374
375 #include "AFS_component_version_number.c"
376 int
377 main(int argc, char **argv)
378 {
379     afs_int32 code;
380     struct rx_securityClass **securityClasses;
381     afs_int32 numClasses;
382     struct rx_service *service;
383     int rxpackets = 100;
384     afs_uint32 host = ntohl(INADDR_ANY);
385     VolumePackageOptions opts;
386
387 #ifdef  AFS_AIX32_ENV
388     /*
389      * The following signal action for AIX is necessary so that in case of a
390      * crash (i.e. core is generated) we can include the user's data section
391      * in the core dump. Unfortunately, by default, only a partial core is
392      * generated which, in many cases, isn't too useful.
393      */
394     struct sigaction nsa;
395
396     sigemptyset(&nsa.sa_mask);
397     nsa.sa_handler = SIG_DFL;
398     nsa.sa_flags = SA_FULLDUMP;
399     sigaction(SIGABRT, &nsa, NULL);
400     sigaction(SIGSEGV, &nsa, NULL);
401 #endif
402     osi_audit_init();
403     osi_audit(VS_StartEvent, 0, AUD_END);
404
405     /* Initialize dirpaths */
406     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
407 #ifdef AFS_NT40_ENV
408         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
409 #endif
410         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
411                 argv[0]);
412         exit(2);
413     }
414
415     configDir = strdup(AFSDIR_SERVER_ETC_DIRPATH);
416     logFile = strdup(AFSDIR_SERVER_VOLSERLOG_FILEPATH);
417
418     if (ParseArgs(argc, argv)) {
419         exit(1);
420     }
421
422     if (auditFileName) {
423         osi_audit_file(auditFileName);
424         osi_audit(VS_StartEvent, 0, AUD_END);
425     }
426 #ifdef AFS_SGI_VNODE_GLUE
427     if (afs_init_kernel_config(-1) < 0) {
428         printf
429             ("Can't determine NUMA configuration, not starting volserver.\n");
430         exit(1);
431     }
432 #endif
433     InitErrTabs();
434
435 #ifdef AFS_PTHREAD_ENV
436     SetLogThreadNumProgram( rx_GetThreadNum );
437 #endif
438
439 #ifdef AFS_NT40_ENV
440     if (afs_winsockInit() < 0) {
441         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
442         printf("Volume server unable to start winsock, exiting.\n");
443         exit(1);
444     }
445 #endif
446     /* Open VolserLog and map stdout, stderr into it; VInitVolumePackage2 can
447        log, so we need to do this here */
448     OpenLog(logFile);
449
450     VOptDefaults(volumeServer, &opts);
451     if (VInitVolumePackage2(volumeServer, &opts)) {
452         Log("Shutting down: errors encountered initializing volume package\n");
453         exit(1);
454     }
455     /* For nuke() */
456     Lock_Init(&localLock);
457     DInit(40);
458 #ifndef AFS_PTHREAD_ENV
459     vol_PollProc = IOMGR_Poll;  /* tell vol pkg to poll io system periodically */
460 #endif
461 #ifndef AFS_NT40_ENV
462     rxi_syscallp = volser_syscall;
463 #endif
464     rx_nPackets = rxpackets;    /* set the max number of packets */
465     if (udpBufSize)
466         rx_SetUdpBufSize(udpBufSize);   /* set the UDP buffer size for receive */
467     if (rxBind) {
468         afs_int32 ccode;
469         if (AFSDIR_SERVER_NETRESTRICT_FILEPATH ||
470             AFSDIR_SERVER_NETINFO_FILEPATH) {
471             char reason[1024];
472             ccode = afsconf_ParseNetFiles(SHostAddrs, NULL, NULL,
473                                           ADDRSPERSITE, reason,
474                                           AFSDIR_SERVER_NETINFO_FILEPATH,
475                                           AFSDIR_SERVER_NETRESTRICT_FILEPATH);
476         } else
477         {
478             ccode = rx_getAllAddr(SHostAddrs, ADDRSPERSITE);
479         }
480         if (ccode == 1)
481             host = SHostAddrs[0];
482     }
483
484     code = rx_InitHost(host, (int)htons(AFSCONF_VOLUMEPORT));
485     if (code) {
486         fprintf(stderr, "rx init failed on socket AFSCONF_VOLUMEPORT %u\n",
487                 AFSCONF_VOLUMEPORT);
488         VS_EXIT(1);
489     }
490     if (!rxJumbograms) {
491         /* Don't allow 3.4 vos clients to send jumbograms and we don't send. */
492         rx_SetNoJumbo();
493     }
494     if (rxMaxMTU != -1) {
495         if (rx_SetMaxMTU(rxMaxMTU) != 0) {
496             fprintf(stderr, "rxMaxMTU %d is invalid\n", rxMaxMTU);
497             VS_EXIT(1);
498         }
499     }
500     rx_GetIFInfo();
501     rx_SetRxDeadTime(420);
502     memset(busyFlags, 0, sizeof(busyFlags));
503
504     SetupLogSignals();
505
506     {
507 #ifdef AFS_PTHREAD_ENV
508         pthread_t tid;
509         pthread_attr_t tattr;
510         opr_Verify(pthread_attr_init(&tattr) == 0);
511         opr_Verify(pthread_attr_setdetachstate(&tattr,
512                                                PTHREAD_CREATE_DETACHED) == 0);
513         opr_Verify(pthread_create(&tid, &tattr, BKGLoop, NULL) == 0);
514 #else
515         PROCESS pid;
516         LWP_CreateProcess(BKGLoop, 16*1024, 3, 0, "vol bkg daemon", &pid);
517 #endif
518     }
519
520     /* 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 */
521
522     tdir = afsconf_Open(configDir);
523     if (!tdir) {
524         Abort("volser: could not open conf files in %s\n",
525               configDir);
526         VS_EXIT(1);
527     }
528
529     /* initialize audit user check */
530     osi_audit_set_user_check(tdir, vol_IsLocalRealmMatch);
531
532     afsconf_BuildServerSecurityObjects(tdir, &securityClasses, &numClasses);
533     if (securityClasses[0] == NULL)
534         Abort("rxnull_NewServerSecurityObject");
535     service =
536         rx_NewServiceHost(host, 0, VOLSERVICE_ID, "VOLSER", securityClasses,
537                           numClasses, AFSVolExecuteRequest);
538     if (service == (struct rx_service *)0)
539         Abort("rx_NewService");
540     rx_SetBeforeProc(service, MyBeforeProc);
541     rx_SetAfterProc(service, MyAfterProc);
542     rx_SetIdleDeadTime(service, 0);     /* never timeout */
543     if (lwps < 4)
544         lwps = 4;
545     rx_SetMaxProcs(service, lwps);
546 #if defined(AFS_XBSD_ENV)
547     rx_SetStackSize(service, (128 * 1024));
548 #elif defined(AFS_SGI_ENV)
549     rx_SetStackSize(service, (48 * 1024));
550 #else
551     rx_SetStackSize(service, (32 * 1024));
552 #endif
553
554     if (rxkadDisableDotCheck) {
555         rx_SetSecurityConfiguration(service, RXS_CONFIG_FLAGS,
556                                     (void *)RXS_CONFIG_FLAGS_DISABLE_DOTCHECK);
557     }
558
559     service =
560         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", securityClasses,
561                       numClasses, RXSTATS_ExecuteRequest);
562     if (service == (struct rx_service *)0)
563         Abort("rx_NewService");
564     rx_SetMinProcs(service, 2);
565     rx_SetMaxProcs(service, 4);
566
567     LogCommandLine(argc, argv, "Volserver", VolserVersion, "Starting AFS",
568                    Log);
569     if (afsconf_GetLatestKey(tdir, NULL, NULL) == 0) {
570         LogDesWarning();
571     }
572
573     /* allow super users to manage RX statistics */
574     rx_SetRxStatUserOk(vol_rxstat_userok);
575
576     rx_StartServer(1);          /* Donate this process to the server process pool */
577
578     osi_audit(VS_FinishEvent, (-1), AUD_END);
579     Abort("StartServer returned?");
580     return 0; /* not reached */
581 }