1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999-2000 by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Postprocessor for NFS server logging. 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 33*7c478bd9Sstevel@tonic-gate #include <assert.h> 34*7c478bd9Sstevel@tonic-gate #include <deflt.h> 35*7c478bd9Sstevel@tonic-gate #include <errno.h> 36*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 37*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 38*7c478bd9Sstevel@tonic-gate #include <stdio.h> 39*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 40*7c478bd9Sstevel@tonic-gate #include <string.h> 41*7c478bd9Sstevel@tonic-gate #include <strings.h> 42*7c478bd9Sstevel@tonic-gate #include <signal.h> 43*7c478bd9Sstevel@tonic-gate #include <syslog.h> 44*7c478bd9Sstevel@tonic-gate #include <limits.h> 45*7c478bd9Sstevel@tonic-gate #include <libintl.h> 46*7c478bd9Sstevel@tonic-gate #include <locale.h> 47*7c478bd9Sstevel@tonic-gate #include <unistd.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 49*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> 52*7c478bd9Sstevel@tonic-gate #include <rpc/clnt_stat.h> 53*7c478bd9Sstevel@tonic-gate #include <nfs/nfs.h> 54*7c478bd9Sstevel@tonic-gate #include <nfs/export.h> 55*7c478bd9Sstevel@tonic-gate #include <nfs/nfs_log.h> 56*7c478bd9Sstevel@tonic-gate #include "fhtab.h" 57*7c478bd9Sstevel@tonic-gate #include "nfslogd.h" 58*7c478bd9Sstevel@tonic-gate #include "buffer_list.h" 59*7c478bd9Sstevel@tonic-gate #include "../lib/nfslog_config.h" 60*7c478bd9Sstevel@tonic-gate #include "../lib/nfslogtab.h" 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate enum pidfile_operation { 63*7c478bd9Sstevel@tonic-gate PID_STARTUP, PID_SHUTDOWN 64*7c478bd9Sstevel@tonic-gate }; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate static int nfslogtab_deactivate_after_boot(void); 67*7c478bd9Sstevel@tonic-gate static int 68*7c478bd9Sstevel@tonic-gate nfslogtab_remove(struct buffer_ent **, struct buffer_ent **, boolean_t); 69*7c478bd9Sstevel@tonic-gate static int cycle_logs(nfsl_config_t *, int); 70*7c478bd9Sstevel@tonic-gate static void enable_logcycling(void); 71*7c478bd9Sstevel@tonic-gate static int process_pidfile(enum pidfile_operation); 72*7c478bd9Sstevel@tonic-gate static void short_cleanup(void); 73*7c478bd9Sstevel@tonic-gate static void full_cleanup(void); 74*7c478bd9Sstevel@tonic-gate static void transactions_timeout(nfsl_config_t *); 75*7c478bd9Sstevel@tonic-gate static void close_all_translogs(nfsl_config_t *); 76*7c478bd9Sstevel@tonic-gate int cycle_log(char *, int); 77*7c478bd9Sstevel@tonic-gate static boolean_t is_cycle_needed(char *, void **, boolean_t, int *); 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate /* 80*7c478bd9Sstevel@tonic-gate * Configuration information. 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate int debug = 0; 84*7c478bd9Sstevel@tonic-gate boolean_t test = B_FALSE; 85*7c478bd9Sstevel@tonic-gate time_t mapping_update_interval = MAPPING_UPDATE_INTERVAL; 86*7c478bd9Sstevel@tonic-gate /* prune_timeout measures how old a database entry must be to be pruned */ 87*7c478bd9Sstevel@tonic-gate time_t prune_timeout = (SECSPERHOUR * 7 * 24); 88*7c478bd9Sstevel@tonic-gate int max_logs_preserve = MAX_LOGS_PRESERVE; 89*7c478bd9Sstevel@tonic-gate uint_t idle_time = IDLE_TIME; 90*7c478bd9Sstevel@tonic-gate static mode_t Umask = NFSLOG_UMASK; 91*7c478bd9Sstevel@tonic-gate static long cycle_frequency = CYCLE_FREQUENCY; 92*7c478bd9Sstevel@tonic-gate /* prune_frequency measures how often should prune_dbs be called */ 93*7c478bd9Sstevel@tonic-gate static long prune_frequency = (SECSPERHOUR * 24); 94*7c478bd9Sstevel@tonic-gate static int min_size = MIN_PROCESSING_SIZE; 95*7c478bd9Sstevel@tonic-gate static volatile bool_t need2cycle = FALSE; 96*7c478bd9Sstevel@tonic-gate static volatile bool_t need2prune = FALSE; 97*7c478bd9Sstevel@tonic-gate boolean_t keep_running = B_TRUE; 98*7c478bd9Sstevel@tonic-gate boolean_t quick_cleaning = B_FALSE; 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 101*7c478bd9Sstevel@tonic-gate int 102*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 103*7c478bd9Sstevel@tonic-gate { 104*7c478bd9Sstevel@tonic-gate struct rlimit rl; 105*7c478bd9Sstevel@tonic-gate int error = 0; 106*7c478bd9Sstevel@tonic-gate char *defp; 107*7c478bd9Sstevel@tonic-gate pid_t pid; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate timestruc_t logtab_update; 110*7c478bd9Sstevel@tonic-gate time_t process_start, last_prune = time(0); 111*7c478bd9Sstevel@tonic-gate time_t last_cycle = time(0); /* last time logs were cycled */ 112*7c478bd9Sstevel@tonic-gate int processed, buffers_processed; 113*7c478bd9Sstevel@tonic-gate struct buffer_ent *buffer_list = NULL, *bep, *next; 114*7c478bd9Sstevel@tonic-gate nfsl_config_t *config_list = NULL; 115*7c478bd9Sstevel@tonic-gate char *fhtable_to_prune = NULL; 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate /* 118*7c478bd9Sstevel@tonic-gate * Check to make sure user is root. 119*7c478bd9Sstevel@tonic-gate */ 120*7c478bd9Sstevel@tonic-gate if (geteuid() != 0) { 121*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s must be run as root\n"), 122*7c478bd9Sstevel@tonic-gate argv[0]); 123*7c478bd9Sstevel@tonic-gate exit(1); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate /* 127*7c478bd9Sstevel@tonic-gate * Read defaults file. 128*7c478bd9Sstevel@tonic-gate */ 129*7c478bd9Sstevel@tonic-gate if (defopen(NFSLOG_OPTIONS_FILE) == 0) { 130*7c478bd9Sstevel@tonic-gate if ((defp = defread("DEBUG=")) != NULL) { 131*7c478bd9Sstevel@tonic-gate debug = atoi(defp); 132*7c478bd9Sstevel@tonic-gate if (debug > 0) 133*7c478bd9Sstevel@tonic-gate (void) printf("debug=%d\n", debug); 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate if ((defp = defread("TEST=")) != NULL) { 136*7c478bd9Sstevel@tonic-gate if (strcmp(defp, "TRUE") == 0) 137*7c478bd9Sstevel@tonic-gate test = B_TRUE; 138*7c478bd9Sstevel@tonic-gate if (debug > 0) { 139*7c478bd9Sstevel@tonic-gate if (test) 140*7c478bd9Sstevel@tonic-gate (void) printf("test=TRUE\n"); 141*7c478bd9Sstevel@tonic-gate else 142*7c478bd9Sstevel@tonic-gate (void) printf("test=FALSE\n"); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate /* 146*7c478bd9Sstevel@tonic-gate * Set Umask for log and fhtable creation. 147*7c478bd9Sstevel@tonic-gate */ 148*7c478bd9Sstevel@tonic-gate if ((defp = defread("UMASK=")) != NULL) { 149*7c478bd9Sstevel@tonic-gate if (sscanf(defp, "%lo", &Umask) != 1) 150*7c478bd9Sstevel@tonic-gate Umask = NFSLOG_UMASK; 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate /* 153*7c478bd9Sstevel@tonic-gate * Minimum size buffer should reach before processing. 154*7c478bd9Sstevel@tonic-gate */ 155*7c478bd9Sstevel@tonic-gate if ((defp = defread("MIN_PROCESSING_SIZE=")) != NULL) { 156*7c478bd9Sstevel@tonic-gate min_size = atoi(defp); 157*7c478bd9Sstevel@tonic-gate if (debug > 0) 158*7c478bd9Sstevel@tonic-gate (void) printf("min_size=%d\n", min_size); 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate /* 161*7c478bd9Sstevel@tonic-gate * Number of seconds the daemon should 162*7c478bd9Sstevel@tonic-gate * sleep waiting for more work. 163*7c478bd9Sstevel@tonic-gate */ 164*7c478bd9Sstevel@tonic-gate if ((defp = defread("IDLE_TIME=")) != NULL) { 165*7c478bd9Sstevel@tonic-gate idle_time = (uint_t)atoi(defp); 166*7c478bd9Sstevel@tonic-gate if (debug > 0) 167*7c478bd9Sstevel@tonic-gate (void) printf("idle_time=%d\n", idle_time); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate /* 170*7c478bd9Sstevel@tonic-gate * Maximum number of logs to preserve. 171*7c478bd9Sstevel@tonic-gate */ 172*7c478bd9Sstevel@tonic-gate if ((defp = defread("MAX_LOGS_PRESERVE=")) != NULL) { 173*7c478bd9Sstevel@tonic-gate max_logs_preserve = atoi(defp); 174*7c478bd9Sstevel@tonic-gate if (debug > 0) { 175*7c478bd9Sstevel@tonic-gate (void) printf("max_logs_preserve=%d\n", 176*7c478bd9Sstevel@tonic-gate max_logs_preserve); 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate /* 180*7c478bd9Sstevel@tonic-gate * Frequency of atime updates. 181*7c478bd9Sstevel@tonic-gate */ 182*7c478bd9Sstevel@tonic-gate if ((defp = defread("MAPPING_UPDATE_INTERVAL=")) != NULL) { 183*7c478bd9Sstevel@tonic-gate mapping_update_interval = atoi(defp); 184*7c478bd9Sstevel@tonic-gate if (debug > 0) { 185*7c478bd9Sstevel@tonic-gate (void) printf("mapping_update_interval=%ld\n", 186*7c478bd9Sstevel@tonic-gate mapping_update_interval); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate } 189*7c478bd9Sstevel@tonic-gate /* 190*7c478bd9Sstevel@tonic-gate * Time to remove entries 191*7c478bd9Sstevel@tonic-gate */ 192*7c478bd9Sstevel@tonic-gate if ((defp = defread("PRUNE_TIMEOUT=")) != NULL) { 193*7c478bd9Sstevel@tonic-gate /* 194*7c478bd9Sstevel@tonic-gate * Prune timeout is in hours but we want 195*7c478bd9Sstevel@tonic-gate * deal with the time in seconds internally. 196*7c478bd9Sstevel@tonic-gate */ 197*7c478bd9Sstevel@tonic-gate prune_timeout = atoi(defp); 198*7c478bd9Sstevel@tonic-gate prune_timeout *= SECSPERHOUR; 199*7c478bd9Sstevel@tonic-gate if (prune_timeout < prune_frequency) 200*7c478bd9Sstevel@tonic-gate prune_frequency = prune_timeout; 201*7c478bd9Sstevel@tonic-gate if (debug > 0) { 202*7c478bd9Sstevel@tonic-gate (void) printf("prune_timeout=%ld\n", 203*7c478bd9Sstevel@tonic-gate prune_timeout); 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate /* 207*7c478bd9Sstevel@tonic-gate * fhtable to prune when start (for debug/test purposes) 208*7c478bd9Sstevel@tonic-gate */ 209*7c478bd9Sstevel@tonic-gate if ((defp = defread("PRUNE_FHTABLE=")) != NULL) { 210*7c478bd9Sstevel@tonic-gate /* 211*7c478bd9Sstevel@tonic-gate * Specify full pathname of fhtable to prune before 212*7c478bd9Sstevel@tonic-gate * any processing is to be done 213*7c478bd9Sstevel@tonic-gate */ 214*7c478bd9Sstevel@tonic-gate if (fhtable_to_prune = malloc(strlen(defp) + 1)) { 215*7c478bd9Sstevel@tonic-gate (void) strcpy(fhtable_to_prune, defp); 216*7c478bd9Sstevel@tonic-gate if (debug > 0) { 217*7c478bd9Sstevel@tonic-gate (void) printf("fhtable to prune=%s\n", 218*7c478bd9Sstevel@tonic-gate fhtable_to_prune); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate } else { 221*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 222*7c478bd9Sstevel@tonic-gate "malloc fhtable_to_prune error %s\n"), 223*7c478bd9Sstevel@tonic-gate strerror(errno)); 224*7c478bd9Sstevel@tonic-gate } 225*7c478bd9Sstevel@tonic-gate } 226*7c478bd9Sstevel@tonic-gate /* 227*7c478bd9Sstevel@tonic-gate * Log cycle frequency. 228*7c478bd9Sstevel@tonic-gate */ 229*7c478bd9Sstevel@tonic-gate if ((defp = defread("CYCLE_FREQUENCY=")) != NULL) { 230*7c478bd9Sstevel@tonic-gate cycle_frequency = atol(defp); 231*7c478bd9Sstevel@tonic-gate if (debug > 0) { 232*7c478bd9Sstevel@tonic-gate (void) printf("cycle_frequency=%ld\n", 233*7c478bd9Sstevel@tonic-gate cycle_frequency); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate /* 237*7c478bd9Sstevel@tonic-gate * defopen of NULL closes the open defaults file. 238*7c478bd9Sstevel@tonic-gate */ 239*7c478bd9Sstevel@tonic-gate (void) defopen((char *)NULL); 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate 242*7c478bd9Sstevel@tonic-gate if (Umask > ((mode_t)0777)) 243*7c478bd9Sstevel@tonic-gate Umask = NFSLOG_UMASK; 244*7c478bd9Sstevel@tonic-gate (void) umask(Umask); 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_FSIZE, &rl) < 0) { 247*7c478bd9Sstevel@tonic-gate error = errno; 248*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 249*7c478bd9Sstevel@tonic-gate "getrlimit failed error is %d - %s\n"), 250*7c478bd9Sstevel@tonic-gate error, strerror(error)); 251*7c478bd9Sstevel@tonic-gate exit(1); 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate if (min_size < 0 || min_size > rl.rlim_cur) { 254*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 255*7c478bd9Sstevel@tonic-gate "MIN_PROCESSING_SIZE out of range, should be >= 0 and " 256*7c478bd9Sstevel@tonic-gate "< %d. Check %s.\n"), rl.rlim_cur, NFSLOG_OPTIONS_FILE); 257*7c478bd9Sstevel@tonic-gate exit(1); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate if (idle_time > INT_MAX) { 260*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 261*7c478bd9Sstevel@tonic-gate "IDLE_TIME out of range, should be >= 0 and " 262*7c478bd9Sstevel@tonic-gate "< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE); 263*7c478bd9Sstevel@tonic-gate exit(1); 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate if (max_logs_preserve < 0 || max_logs_preserve > INT_MAX) { 266*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 267*7c478bd9Sstevel@tonic-gate "MAX_LOGS_PRESERVE out of range, should be >= 0 and " 268*7c478bd9Sstevel@tonic-gate "< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE); 269*7c478bd9Sstevel@tonic-gate exit(1); 270*7c478bd9Sstevel@tonic-gate } 271*7c478bd9Sstevel@tonic-gate if (mapping_update_interval < 0|| mapping_update_interval > INT_MAX) { 272*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 273*7c478bd9Sstevel@tonic-gate "MAPPING_UPDATE_INTERVAL out of range, " 274*7c478bd9Sstevel@tonic-gate "should be >= 0 and " 275*7c478bd9Sstevel@tonic-gate "< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE); 276*7c478bd9Sstevel@tonic-gate exit(1); 277*7c478bd9Sstevel@tonic-gate } 278*7c478bd9Sstevel@tonic-gate if (cycle_frequency < 0 || cycle_frequency > INT_MAX) { 279*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 280*7c478bd9Sstevel@tonic-gate "CYCLE_FREQUENCY out of range, should be >= 0 and " 281*7c478bd9Sstevel@tonic-gate "< %d. Check %s.\n"), INT_MAX, NFSLOG_OPTIONS_FILE); 282*7c478bd9Sstevel@tonic-gate exit(1); 283*7c478bd9Sstevel@tonic-gate } 284*7c478bd9Sstevel@tonic-gate /* get value in seconds */ 285*7c478bd9Sstevel@tonic-gate cycle_frequency = cycle_frequency * 60 * 60; 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate /* 288*7c478bd9Sstevel@tonic-gate * If we dump core, it will be /core 289*7c478bd9Sstevel@tonic-gate */ 290*7c478bd9Sstevel@tonic-gate if (chdir("/") < 0) 291*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("chdir /: %s"), strerror(errno)); 292*7c478bd9Sstevel@tonic-gate 293*7c478bd9Sstevel@tonic-gate /* 294*7c478bd9Sstevel@tonic-gate * Config errors to stderr 295*7c478bd9Sstevel@tonic-gate */ 296*7c478bd9Sstevel@tonic-gate nfsl_errs_to_syslog = B_FALSE; 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate #ifndef DEBUG 299*7c478bd9Sstevel@tonic-gate pid = fork(); 300*7c478bd9Sstevel@tonic-gate if (pid == -1) { 301*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: fork failure\n"), 302*7c478bd9Sstevel@tonic-gate argv[0]); 303*7c478bd9Sstevel@tonic-gate exit(1); 304*7c478bd9Sstevel@tonic-gate } 305*7c478bd9Sstevel@tonic-gate if (pid != 0) 306*7c478bd9Sstevel@tonic-gate exit(0); 307*7c478bd9Sstevel@tonic-gate /* 308*7c478bd9Sstevel@tonic-gate * Config errors to syslog 309*7c478bd9Sstevel@tonic-gate */ 310*7c478bd9Sstevel@tonic-gate nfsl_errs_to_syslog = B_TRUE; 311*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 314*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 315*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 316*7c478bd9Sstevel@tonic-gate #endif 317*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 318*7c478bd9Sstevel@tonic-gate 319*7c478bd9Sstevel@tonic-gate /* 320*7c478bd9Sstevel@tonic-gate * Check to see if nfslogd is already running. 321*7c478bd9Sstevel@tonic-gate */ 322*7c478bd9Sstevel@tonic-gate if (process_pidfile(PID_STARTUP) != 0) { 323*7c478bd9Sstevel@tonic-gate exit(1); 324*7c478bd9Sstevel@tonic-gate } 325*7c478bd9Sstevel@tonic-gate 326*7c478bd9Sstevel@tonic-gate (void) sigset(SIGUSR1, (void (*)(int))enable_logcycling); 327*7c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, (void (*)(int))full_cleanup); 328*7c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, (void(*)(int))short_cleanup); 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate #ifndef DEBUG 331*7c478bd9Sstevel@tonic-gate /* 332*7c478bd9Sstevel@tonic-gate * Close existing file descriptors, open "/dev/null" as 333*7c478bd9Sstevel@tonic-gate * standard input, output, and error, and detach from 334*7c478bd9Sstevel@tonic-gate * controlling terminal. 335*7c478bd9Sstevel@tonic-gate */ 336*7c478bd9Sstevel@tonic-gate if (!debug && !test) { 337*7c478bd9Sstevel@tonic-gate closefrom(0); 338*7c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 339*7c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY); 340*7c478bd9Sstevel@tonic-gate (void) dup(1); 341*7c478bd9Sstevel@tonic-gate } 342*7c478bd9Sstevel@tonic-gate (void) setsid(); 343*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 344*7c478bd9Sstevel@tonic-gate 345*7c478bd9Sstevel@tonic-gate openlog(argv[0], LOG_PID, LOG_DAEMON); 346*7c478bd9Sstevel@tonic-gate 347*7c478bd9Sstevel@tonic-gate public_fh.fh_len = NFS_FHMAXDATA; 348*7c478bd9Sstevel@tonic-gate public_fh.fh_xlen = NFS_FHMAXDATA; 349*7c478bd9Sstevel@tonic-gate 350*7c478bd9Sstevel@tonic-gate /* 351*7c478bd9Sstevel@tonic-gate * Call once at startup to handle the nfslogtab 352*7c478bd9Sstevel@tonic-gate */ 353*7c478bd9Sstevel@tonic-gate if (nfslogtab_deactivate_after_boot() == -1) 354*7c478bd9Sstevel@tonic-gate exit(1); 355*7c478bd9Sstevel@tonic-gate 356*7c478bd9Sstevel@tonic-gate /* 357*7c478bd9Sstevel@tonic-gate * Get a list of buffers that need to be processed. 358*7c478bd9Sstevel@tonic-gate */ 359*7c478bd9Sstevel@tonic-gate if (error = getbuffer_list(&buffer_list, &logtab_update)) { 360*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("Could not read %s: %s"), 361*7c478bd9Sstevel@tonic-gate NFSLOGTAB, strerror(error)); 362*7c478bd9Sstevel@tonic-gate goto done; 363*7c478bd9Sstevel@tonic-gate } 364*7c478bd9Sstevel@tonic-gate 365*7c478bd9Sstevel@tonic-gate /* 366*7c478bd9Sstevel@tonic-gate * Get the configuration list. 367*7c478bd9Sstevel@tonic-gate */ 368*7c478bd9Sstevel@tonic-gate if (error = nfsl_getconfig_list(&config_list)) { 369*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 370*7c478bd9Sstevel@tonic-gate "Could not obtain configuration list: %s"), 371*7c478bd9Sstevel@tonic-gate strerror(error)); 372*7c478bd9Sstevel@tonic-gate goto done; 373*7c478bd9Sstevel@tonic-gate } 374*7c478bd9Sstevel@tonic-gate 375*7c478bd9Sstevel@tonic-gate /* 376*7c478bd9Sstevel@tonic-gate * loop to process the work being generated by the NFS server 377*7c478bd9Sstevel@tonic-gate */ 378*7c478bd9Sstevel@tonic-gate while (keep_running) { 379*7c478bd9Sstevel@tonic-gate buffers_processed = 0; 380*7c478bd9Sstevel@tonic-gate (void) checkbuffer_list(&buffer_list, &logtab_update); 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate while (buffer_list == NULL) { 383*7c478bd9Sstevel@tonic-gate /* 384*7c478bd9Sstevel@tonic-gate * Nothing to do 385*7c478bd9Sstevel@tonic-gate */ 386*7c478bd9Sstevel@tonic-gate (void) sleep(idle_time); 387*7c478bd9Sstevel@tonic-gate 388*7c478bd9Sstevel@tonic-gate if (!keep_running) { 389*7c478bd9Sstevel@tonic-gate /* 390*7c478bd9Sstevel@tonic-gate * We have been interrupted and asked to 391*7c478bd9Sstevel@tonic-gate * flush our transactions and exit. 392*7c478bd9Sstevel@tonic-gate */ 393*7c478bd9Sstevel@tonic-gate close_all_translogs(config_list); 394*7c478bd9Sstevel@tonic-gate goto done; 395*7c478bd9Sstevel@tonic-gate } 396*7c478bd9Sstevel@tonic-gate (void) checkbuffer_list(&buffer_list, &logtab_update); 397*7c478bd9Sstevel@tonic-gate } 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate process_start = time(0); 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate if (error = nfsl_checkconfig_list(&config_list, NULL)) { 402*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 403*7c478bd9Sstevel@tonic-gate "Could not update configuration list: %s"), 404*7c478bd9Sstevel@tonic-gate strerror(error)); 405*7c478bd9Sstevel@tonic-gate nfsl_freeconfig_list(&config_list); 406*7c478bd9Sstevel@tonic-gate goto done; 407*7c478bd9Sstevel@tonic-gate } 408*7c478bd9Sstevel@tonic-gate 409*7c478bd9Sstevel@tonic-gate if (difftime(time(0), last_cycle) > cycle_frequency) 410*7c478bd9Sstevel@tonic-gate need2cycle = TRUE; 411*7c478bd9Sstevel@tonic-gate if (need2cycle) { 412*7c478bd9Sstevel@tonic-gate error = cycle_logs(config_list, max_logs_preserve); 413*7c478bd9Sstevel@tonic-gate if (error) { 414*7c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, gettext( 415*7c478bd9Sstevel@tonic-gate "One or more logfiles couldn't be cycled, " 416*7c478bd9Sstevel@tonic-gate "continuing regular processing")); 417*7c478bd9Sstevel@tonic-gate } 418*7c478bd9Sstevel@tonic-gate need2cycle = FALSE; 419*7c478bd9Sstevel@tonic-gate last_cycle = time(0); 420*7c478bd9Sstevel@tonic-gate } 421*7c478bd9Sstevel@tonic-gate if (difftime(time(0), last_prune) > prune_frequency) 422*7c478bd9Sstevel@tonic-gate need2prune = TRUE; 423*7c478bd9Sstevel@tonic-gate if (need2prune || fhtable_to_prune) { 424*7c478bd9Sstevel@tonic-gate error = prune_dbs(fhtable_to_prune); 425*7c478bd9Sstevel@tonic-gate if (error) { 426*7c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, gettext( 427*7c478bd9Sstevel@tonic-gate "Error in cleaning database files")); 428*7c478bd9Sstevel@tonic-gate } 429*7c478bd9Sstevel@tonic-gate need2prune = FALSE; 430*7c478bd9Sstevel@tonic-gate last_prune = time(0); 431*7c478bd9Sstevel@tonic-gate /* After the first time, use the normal procedure */ 432*7c478bd9Sstevel@tonic-gate free(fhtable_to_prune); 433*7c478bd9Sstevel@tonic-gate fhtable_to_prune = NULL; 434*7c478bd9Sstevel@tonic-gate } 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate for (bep = buffer_list; bep != NULL; bep = next) { 437*7c478bd9Sstevel@tonic-gate next = bep->be_next; 438*7c478bd9Sstevel@tonic-gate processed = 0; 439*7c478bd9Sstevel@tonic-gate error = process_buffer(bep, &config_list, 440*7c478bd9Sstevel@tonic-gate min_size, idle_time, &processed); 441*7c478bd9Sstevel@tonic-gate if (error == 0 && processed) { 442*7c478bd9Sstevel@tonic-gate if (bep->be_error) { 443*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 444*7c478bd9Sstevel@tonic-gate "Buffer file '%s' " 445*7c478bd9Sstevel@tonic-gate "processed successfully."), 446*7c478bd9Sstevel@tonic-gate bep->be_name); 447*7c478bd9Sstevel@tonic-gate } 448*7c478bd9Sstevel@tonic-gate error = 449*7c478bd9Sstevel@tonic-gate nfslogtab_remove(&buffer_list, &bep, B_FALSE); 450*7c478bd9Sstevel@tonic-gate } else if (error == ENOENT) { 451*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("Removed entry" 452*7c478bd9Sstevel@tonic-gate "\t\"%s\t%s\t%d\" from %s"), 453*7c478bd9Sstevel@tonic-gate bep->be_name, 454*7c478bd9Sstevel@tonic-gate bep->be_sharepnt->se_name, 455*7c478bd9Sstevel@tonic-gate bep->be_sharepnt->se_state, 456*7c478bd9Sstevel@tonic-gate NFSLOGTAB); 457*7c478bd9Sstevel@tonic-gate error = 458*7c478bd9Sstevel@tonic-gate nfslogtab_remove(&buffer_list, &bep, B_TRUE); 459*7c478bd9Sstevel@tonic-gate } else if (error && error != bep->be_error) { 460*7c478bd9Sstevel@tonic-gate /* 461*7c478bd9Sstevel@tonic-gate * An error different from what we've reported 462*7c478bd9Sstevel@tonic-gate * before occured. 463*7c478bd9Sstevel@tonic-gate */ 464*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 465*7c478bd9Sstevel@tonic-gate "Cannot process buffer file '%s' - " 466*7c478bd9Sstevel@tonic-gate "will retry on every iteration."), 467*7c478bd9Sstevel@tonic-gate bep->be_name); 468*7c478bd9Sstevel@tonic-gate } 469*7c478bd9Sstevel@tonic-gate 470*7c478bd9Sstevel@tonic-gate if (bep != NULL) 471*7c478bd9Sstevel@tonic-gate bep->be_error = error; 472*7c478bd9Sstevel@tonic-gate buffers_processed += processed; 473*7c478bd9Sstevel@tonic-gate } 474*7c478bd9Sstevel@tonic-gate 475*7c478bd9Sstevel@tonic-gate transactions_timeout(config_list); 476*7c478bd9Sstevel@tonic-gate 477*7c478bd9Sstevel@tonic-gate if (keep_running) { 478*7c478bd9Sstevel@tonic-gate uint_t process_time; 479*7c478bd9Sstevel@tonic-gate 480*7c478bd9Sstevel@tonic-gate /* 481*7c478bd9Sstevel@tonic-gate * Sleep idle_time minus however long it took us 482*7c478bd9Sstevel@tonic-gate * to process the buffers. 483*7c478bd9Sstevel@tonic-gate */ 484*7c478bd9Sstevel@tonic-gate process_time = 485*7c478bd9Sstevel@tonic-gate (uint_t)(difftime(time(0), process_start)); 486*7c478bd9Sstevel@tonic-gate if (process_time < idle_time) 487*7c478bd9Sstevel@tonic-gate (void) sleep(idle_time - process_time); 488*7c478bd9Sstevel@tonic-gate } 489*7c478bd9Sstevel@tonic-gate } 490*7c478bd9Sstevel@tonic-gate 491*7c478bd9Sstevel@tonic-gate done: 492*7c478bd9Sstevel@tonic-gate /* 493*7c478bd9Sstevel@tonic-gate * Make sure to clean house before we exit 494*7c478bd9Sstevel@tonic-gate */ 495*7c478bd9Sstevel@tonic-gate close_all_translogs(config_list); 496*7c478bd9Sstevel@tonic-gate free_buffer_list(&buffer_list); 497*7c478bd9Sstevel@tonic-gate nfsl_freeconfig_list(&config_list); 498*7c478bd9Sstevel@tonic-gate 499*7c478bd9Sstevel@tonic-gate (void) process_pidfile(PID_SHUTDOWN); 500*7c478bd9Sstevel@tonic-gate 501*7c478bd9Sstevel@tonic-gate return (error); 502*7c478bd9Sstevel@tonic-gate } 503*7c478bd9Sstevel@tonic-gate 504*7c478bd9Sstevel@tonic-gate static void 505*7c478bd9Sstevel@tonic-gate short_cleanup(void) 506*7c478bd9Sstevel@tonic-gate { 507*7c478bd9Sstevel@tonic-gate if (debug) { 508*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 509*7c478bd9Sstevel@tonic-gate "SIGTERM received, setting state to terminate...\n"); 510*7c478bd9Sstevel@tonic-gate } 511*7c478bd9Sstevel@tonic-gate quick_cleaning = B_TRUE; 512*7c478bd9Sstevel@tonic-gate keep_running = B_FALSE; 513*7c478bd9Sstevel@tonic-gate } 514*7c478bd9Sstevel@tonic-gate 515*7c478bd9Sstevel@tonic-gate static void 516*7c478bd9Sstevel@tonic-gate full_cleanup(void) 517*7c478bd9Sstevel@tonic-gate { 518*7c478bd9Sstevel@tonic-gate if (debug) { 519*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 520*7c478bd9Sstevel@tonic-gate "SIGHUP received, setting state to shutdown...\n"); 521*7c478bd9Sstevel@tonic-gate } 522*7c478bd9Sstevel@tonic-gate quick_cleaning = keep_running = B_FALSE; 523*7c478bd9Sstevel@tonic-gate } 524*7c478bd9Sstevel@tonic-gate 525*7c478bd9Sstevel@tonic-gate /* 526*7c478bd9Sstevel@tonic-gate * Removes nfslogtab entries matching the specified buffer_ent, 527*7c478bd9Sstevel@tonic-gate * if 'inactive_only' is set, then only inactive entries are removed. 528*7c478bd9Sstevel@tonic-gate * The buffer_list and sharepoint list entries are removed appropriately. 529*7c478bd9Sstevel@tonic-gate * Returns 0 on success, error otherwise. 530*7c478bd9Sstevel@tonic-gate */ 531*7c478bd9Sstevel@tonic-gate static int 532*7c478bd9Sstevel@tonic-gate nfslogtab_remove( 533*7c478bd9Sstevel@tonic-gate struct buffer_ent **buffer_list, 534*7c478bd9Sstevel@tonic-gate struct buffer_ent **bep, 535*7c478bd9Sstevel@tonic-gate boolean_t allstates) 536*7c478bd9Sstevel@tonic-gate { 537*7c478bd9Sstevel@tonic-gate FILE *fd; 538*7c478bd9Sstevel@tonic-gate int error = 0; 539*7c478bd9Sstevel@tonic-gate struct sharepnt_ent *sep, *next; 540*7c478bd9Sstevel@tonic-gate 541*7c478bd9Sstevel@tonic-gate fd = fopen(NFSLOGTAB, "r+"); 542*7c478bd9Sstevel@tonic-gate rewind(fd); 543*7c478bd9Sstevel@tonic-gate if (fd == NULL) { 544*7c478bd9Sstevel@tonic-gate error = errno; 545*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("%s - %s\n"), NFSLOGTAB, 546*7c478bd9Sstevel@tonic-gate strerror(error)); 547*7c478bd9Sstevel@tonic-gate return (error); 548*7c478bd9Sstevel@tonic-gate } 549*7c478bd9Sstevel@tonic-gate 550*7c478bd9Sstevel@tonic-gate if (lockf(fileno(fd), F_LOCK, 0L) < 0) { 551*7c478bd9Sstevel@tonic-gate error = errno; 552*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("cannot lock %s - %s\n"), NFSLOGTAB, 553*7c478bd9Sstevel@tonic-gate strerror(error)); 554*7c478bd9Sstevel@tonic-gate (void) fclose(fd); 555*7c478bd9Sstevel@tonic-gate return (error); 556*7c478bd9Sstevel@tonic-gate } 557*7c478bd9Sstevel@tonic-gate 558*7c478bd9Sstevel@tonic-gate for (sep = (*bep)->be_sharepnt; sep != NULL; sep = next) { 559*7c478bd9Sstevel@tonic-gate next = sep->se_next; 560*7c478bd9Sstevel@tonic-gate if (!allstates && sep->se_state == LES_ACTIVE) 561*7c478bd9Sstevel@tonic-gate continue; 562*7c478bd9Sstevel@tonic-gate if (error = logtab_rement(fd, (*bep)->be_name, sep->se_name, 563*7c478bd9Sstevel@tonic-gate NULL, sep->se_state)) { 564*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("cannot update %s\n"), 565*7c478bd9Sstevel@tonic-gate NFSLOGTAB); 566*7c478bd9Sstevel@tonic-gate error = EIO; 567*7c478bd9Sstevel@tonic-gate goto errout; 568*7c478bd9Sstevel@tonic-gate } 569*7c478bd9Sstevel@tonic-gate remove_sharepnt_ent(&((*bep)->be_sharepnt), sep); 570*7c478bd9Sstevel@tonic-gate } 571*7c478bd9Sstevel@tonic-gate 572*7c478bd9Sstevel@tonic-gate if ((*bep)->be_sharepnt == NULL) { 573*7c478bd9Sstevel@tonic-gate /* 574*7c478bd9Sstevel@tonic-gate * All sharepoints were removed from NFSLOGTAB. 575*7c478bd9Sstevel@tonic-gate * Remove this buffer from our list. 576*7c478bd9Sstevel@tonic-gate */ 577*7c478bd9Sstevel@tonic-gate remove_buffer_ent(buffer_list, *bep); 578*7c478bd9Sstevel@tonic-gate *bep = NULL; 579*7c478bd9Sstevel@tonic-gate } 580*7c478bd9Sstevel@tonic-gate 581*7c478bd9Sstevel@tonic-gate errout: (void) fclose(fd); 582*7c478bd9Sstevel@tonic-gate 583*7c478bd9Sstevel@tonic-gate return (error); 584*7c478bd9Sstevel@tonic-gate } 585*7c478bd9Sstevel@tonic-gate 586*7c478bd9Sstevel@tonic-gate /* 587*7c478bd9Sstevel@tonic-gate * Deactivates entries if nfslogtab is older than the boot time. 588*7c478bd9Sstevel@tonic-gate */ 589*7c478bd9Sstevel@tonic-gate static int 590*7c478bd9Sstevel@tonic-gate nfslogtab_deactivate_after_boot(void) 591*7c478bd9Sstevel@tonic-gate { 592*7c478bd9Sstevel@tonic-gate FILE *fd; 593*7c478bd9Sstevel@tonic-gate int error = 0; 594*7c478bd9Sstevel@tonic-gate 595*7c478bd9Sstevel@tonic-gate fd = fopen(NFSLOGTAB, "r+"); 596*7c478bd9Sstevel@tonic-gate if (fd == NULL) { 597*7c478bd9Sstevel@tonic-gate error = errno; 598*7c478bd9Sstevel@tonic-gate if (error != ENOENT) { 599*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("%s: %s\n"), NFSLOGTAB, 600*7c478bd9Sstevel@tonic-gate strerror(error)); 601*7c478bd9Sstevel@tonic-gate return (-1); 602*7c478bd9Sstevel@tonic-gate } 603*7c478bd9Sstevel@tonic-gate return (0); 604*7c478bd9Sstevel@tonic-gate } 605*7c478bd9Sstevel@tonic-gate 606*7c478bd9Sstevel@tonic-gate if (lockf(fileno(fd), F_LOCK, 0L) < 0) { 607*7c478bd9Sstevel@tonic-gate error = errno; 608*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("cannot lock %s: %s\n"), 609*7c478bd9Sstevel@tonic-gate NFSLOGTAB, strerror(error)); 610*7c478bd9Sstevel@tonic-gate (void) fclose(fd); 611*7c478bd9Sstevel@tonic-gate return (-1); 612*7c478bd9Sstevel@tonic-gate } 613*7c478bd9Sstevel@tonic-gate 614*7c478bd9Sstevel@tonic-gate if (logtab_deactivate_after_boot(fd) == -1) { 615*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 616*7c478bd9Sstevel@tonic-gate "Cannot deactivate all entries in %s\n"), NFSLOGTAB); 617*7c478bd9Sstevel@tonic-gate (void) fclose(fd); 618*7c478bd9Sstevel@tonic-gate return (-1); 619*7c478bd9Sstevel@tonic-gate } 620*7c478bd9Sstevel@tonic-gate 621*7c478bd9Sstevel@tonic-gate (void) fclose(fd); 622*7c478bd9Sstevel@tonic-gate return (0); 623*7c478bd9Sstevel@tonic-gate } 624*7c478bd9Sstevel@tonic-gate 625*7c478bd9Sstevel@tonic-gate /* 626*7c478bd9Sstevel@tonic-gate * Enables the log file cycling flag. 627*7c478bd9Sstevel@tonic-gate */ 628*7c478bd9Sstevel@tonic-gate static void 629*7c478bd9Sstevel@tonic-gate enable_logcycling(void) 630*7c478bd9Sstevel@tonic-gate { 631*7c478bd9Sstevel@tonic-gate need2cycle = TRUE; 632*7c478bd9Sstevel@tonic-gate } 633*7c478bd9Sstevel@tonic-gate 634*7c478bd9Sstevel@tonic-gate /* 635*7c478bd9Sstevel@tonic-gate * Cycle all log files that have been active since the last cycling. 636*7c478bd9Sstevel@tonic-gate * This means it's not simply listed in the configuration file, but 637*7c478bd9Sstevel@tonic-gate * there's information associated with it. 638*7c478bd9Sstevel@tonic-gate */ 639*7c478bd9Sstevel@tonic-gate static int 640*7c478bd9Sstevel@tonic-gate cycle_logs(nfsl_config_t *listp, int max_logs_preserve) 641*7c478bd9Sstevel@tonic-gate { 642*7c478bd9Sstevel@tonic-gate nfsl_config_t *clp; 643*7c478bd9Sstevel@tonic-gate void *processed_list = NULL; 644*7c478bd9Sstevel@tonic-gate int error = 0, total_errors = 0; 645*7c478bd9Sstevel@tonic-gate 646*7c478bd9Sstevel@tonic-gate for (clp = listp; clp != NULL; clp = clp->nc_next) { 647*7c478bd9Sstevel@tonic-gate error = 0; 648*7c478bd9Sstevel@tonic-gate 649*7c478bd9Sstevel@tonic-gate /* 650*7c478bd9Sstevel@tonic-gate * Process transpath log. 651*7c478bd9Sstevel@tonic-gate */ 652*7c478bd9Sstevel@tonic-gate if (clp->nc_logpath) { 653*7c478bd9Sstevel@tonic-gate if (is_cycle_needed(clp->nc_logpath, &processed_list, 654*7c478bd9Sstevel@tonic-gate B_FALSE, &error)) { 655*7c478bd9Sstevel@tonic-gate if (clp->nc_transcookie != NULL) { 656*7c478bd9Sstevel@tonic-gate nfslog_close_transactions( 657*7c478bd9Sstevel@tonic-gate &clp->nc_transcookie); 658*7c478bd9Sstevel@tonic-gate assert(clp->nc_transcookie == NULL); 659*7c478bd9Sstevel@tonic-gate } 660*7c478bd9Sstevel@tonic-gate error = cycle_log(clp->nc_logpath, 661*7c478bd9Sstevel@tonic-gate max_logs_preserve); 662*7c478bd9Sstevel@tonic-gate } else if (error) 663*7c478bd9Sstevel@tonic-gate goto errout; 664*7c478bd9Sstevel@tonic-gate } 665*7c478bd9Sstevel@tonic-gate total_errors += error; 666*7c478bd9Sstevel@tonic-gate 667*7c478bd9Sstevel@tonic-gate /* 668*7c478bd9Sstevel@tonic-gate * Process elfpath log. 669*7c478bd9Sstevel@tonic-gate */ 670*7c478bd9Sstevel@tonic-gate if (clp->nc_rpclogpath) { 671*7c478bd9Sstevel@tonic-gate if (is_cycle_needed(clp->nc_rpclogpath, &processed_list, 672*7c478bd9Sstevel@tonic-gate B_FALSE, &error)) { 673*7c478bd9Sstevel@tonic-gate error = cycle_log(clp->nc_rpclogpath, 674*7c478bd9Sstevel@tonic-gate max_logs_preserve); 675*7c478bd9Sstevel@tonic-gate } else if (error) 676*7c478bd9Sstevel@tonic-gate goto errout; 677*7c478bd9Sstevel@tonic-gate } 678*7c478bd9Sstevel@tonic-gate total_errors += error; 679*7c478bd9Sstevel@tonic-gate } 680*7c478bd9Sstevel@tonic-gate 681*7c478bd9Sstevel@tonic-gate errout: 682*7c478bd9Sstevel@tonic-gate /* 683*7c478bd9Sstevel@tonic-gate * Free the list of processed entries. 684*7c478bd9Sstevel@tonic-gate */ 685*7c478bd9Sstevel@tonic-gate (void) is_cycle_needed(NULL, &processed_list, B_TRUE, &error); 686*7c478bd9Sstevel@tonic-gate 687*7c478bd9Sstevel@tonic-gate return (total_errors); 688*7c478bd9Sstevel@tonic-gate } 689*7c478bd9Sstevel@tonic-gate 690*7c478bd9Sstevel@tonic-gate /* 691*7c478bd9Sstevel@tonic-gate * Returns TRUE if this log has not yet been cycled, FALSE otherwise. 692*7c478bd9Sstevel@tonic-gate * '*head' points to the list of entries that have been processed. 693*7c478bd9Sstevel@tonic-gate * If this is a new entry, it gets inserted at the beginning of the 694*7c478bd9Sstevel@tonic-gate * list, and returns TRUE. 695*7c478bd9Sstevel@tonic-gate * 696*7c478bd9Sstevel@tonic-gate * The list is freed if 'need2free' is set, and returns FALSE. 697*7c478bd9Sstevel@tonic-gate * Sets 'error' on failure, and returns FALSE. 698*7c478bd9Sstevel@tonic-gate */ 699*7c478bd9Sstevel@tonic-gate static boolean_t 700*7c478bd9Sstevel@tonic-gate is_cycle_needed(char *path, void **list, boolean_t need2free, int *error) 701*7c478bd9Sstevel@tonic-gate { 702*7c478bd9Sstevel@tonic-gate struct list { 703*7c478bd9Sstevel@tonic-gate char *log; 704*7c478bd9Sstevel@tonic-gate struct list *next; 705*7c478bd9Sstevel@tonic-gate } *head, *next, *p; 706*7c478bd9Sstevel@tonic-gate 707*7c478bd9Sstevel@tonic-gate head = (struct list *)(*list); 708*7c478bd9Sstevel@tonic-gate if (need2free) { 709*7c478bd9Sstevel@tonic-gate /* 710*7c478bd9Sstevel@tonic-gate * Free the list and return 711*7c478bd9Sstevel@tonic-gate */ 712*7c478bd9Sstevel@tonic-gate for (p = head; p != NULL; p = next) { 713*7c478bd9Sstevel@tonic-gate next = p->next; 714*7c478bd9Sstevel@tonic-gate free(p); 715*7c478bd9Sstevel@tonic-gate } 716*7c478bd9Sstevel@tonic-gate head = NULL; 717*7c478bd9Sstevel@tonic-gate return (B_FALSE); 718*7c478bd9Sstevel@tonic-gate } 719*7c478bd9Sstevel@tonic-gate 720*7c478bd9Sstevel@tonic-gate assert(path != NULL); 721*7c478bd9Sstevel@tonic-gate *error = 0; 722*7c478bd9Sstevel@tonic-gate for (p = head; p != NULL; p = p->next) { 723*7c478bd9Sstevel@tonic-gate /* 724*7c478bd9Sstevel@tonic-gate * Have we seen this before? 725*7c478bd9Sstevel@tonic-gate */ 726*7c478bd9Sstevel@tonic-gate if (strcmp(p->log, path) == 0) 727*7c478bd9Sstevel@tonic-gate return (B_FALSE); 728*7c478bd9Sstevel@tonic-gate } 729*7c478bd9Sstevel@tonic-gate 730*7c478bd9Sstevel@tonic-gate /* 731*7c478bd9Sstevel@tonic-gate * Add it to the list 732*7c478bd9Sstevel@tonic-gate */ 733*7c478bd9Sstevel@tonic-gate if ((p = (struct list *)malloc(sizeof (*p))) == NULL) { 734*7c478bd9Sstevel@tonic-gate *error = ENOMEM; 735*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("Cannot allocate memory.")); 736*7c478bd9Sstevel@tonic-gate return (B_FALSE); 737*7c478bd9Sstevel@tonic-gate } 738*7c478bd9Sstevel@tonic-gate p->log = path; 739*7c478bd9Sstevel@tonic-gate p->next = head; 740*7c478bd9Sstevel@tonic-gate head = p; 741*7c478bd9Sstevel@tonic-gate 742*7c478bd9Sstevel@tonic-gate return (B_TRUE); 743*7c478bd9Sstevel@tonic-gate } 744*7c478bd9Sstevel@tonic-gate 745*7c478bd9Sstevel@tonic-gate /* 746*7c478bd9Sstevel@tonic-gate * cycle given log file. 747*7c478bd9Sstevel@tonic-gate */ 748*7c478bd9Sstevel@tonic-gate int 749*7c478bd9Sstevel@tonic-gate cycle_log(char *filename, int max_logs_preserve) 750*7c478bd9Sstevel@tonic-gate { 751*7c478bd9Sstevel@tonic-gate int i; 752*7c478bd9Sstevel@tonic-gate char *file_1; 753*7c478bd9Sstevel@tonic-gate char *file_2; 754*7c478bd9Sstevel@tonic-gate int error = 0; 755*7c478bd9Sstevel@tonic-gate struct stat st; 756*7c478bd9Sstevel@tonic-gate 757*7c478bd9Sstevel@tonic-gate if (max_logs_preserve == 0) 758*7c478bd9Sstevel@tonic-gate return (0); 759*7c478bd9Sstevel@tonic-gate 760*7c478bd9Sstevel@tonic-gate if (stat(filename, &st) == -1) { 761*7c478bd9Sstevel@tonic-gate if (errno == ENOENT) { 762*7c478bd9Sstevel@tonic-gate /* 763*7c478bd9Sstevel@tonic-gate * Nothing to cycle. 764*7c478bd9Sstevel@tonic-gate */ 765*7c478bd9Sstevel@tonic-gate return (0); 766*7c478bd9Sstevel@tonic-gate } 767*7c478bd9Sstevel@tonic-gate return (errno); 768*7c478bd9Sstevel@tonic-gate } 769*7c478bd9Sstevel@tonic-gate file_1 = (char *)malloc(PATH_MAX); 770*7c478bd9Sstevel@tonic-gate file_2 = (char *)malloc(PATH_MAX); 771*7c478bd9Sstevel@tonic-gate for (i = max_logs_preserve - 2; i >= 0; i--) { 772*7c478bd9Sstevel@tonic-gate (void) sprintf(file_1, "%s.%d", filename, i); 773*7c478bd9Sstevel@tonic-gate (void) sprintf(file_2, "%s.%d", filename, (i + 1)); 774*7c478bd9Sstevel@tonic-gate if (rename(file_1, file_2) == -1) { 775*7c478bd9Sstevel@tonic-gate error = errno; 776*7c478bd9Sstevel@tonic-gate if (error != ENOENT) { 777*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 778*7c478bd9Sstevel@tonic-gate "cycle_log: can not rename %s to %s: %s"), 779*7c478bd9Sstevel@tonic-gate file_1, file_2, strerror(error)); 780*7c478bd9Sstevel@tonic-gate goto out; 781*7c478bd9Sstevel@tonic-gate } 782*7c478bd9Sstevel@tonic-gate } 783*7c478bd9Sstevel@tonic-gate } 784*7c478bd9Sstevel@tonic-gate (void) sprintf(file_1, "%s.0", filename); 785*7c478bd9Sstevel@tonic-gate if (rename(filename, file_1) == -1) { 786*7c478bd9Sstevel@tonic-gate error = errno; 787*7c478bd9Sstevel@tonic-gate if (error != ENOENT) { 788*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext( 789*7c478bd9Sstevel@tonic-gate "cycle_log: can not rename %s to %s: %s"), 790*7c478bd9Sstevel@tonic-gate filename, file_1, strerror(error)); 791*7c478bd9Sstevel@tonic-gate goto out; 792*7c478bd9Sstevel@tonic-gate } 793*7c478bd9Sstevel@tonic-gate } 794*7c478bd9Sstevel@tonic-gate error = 0; 795*7c478bd9Sstevel@tonic-gate 796*7c478bd9Sstevel@tonic-gate out: free(file_1); 797*7c478bd9Sstevel@tonic-gate free(file_2); 798*7c478bd9Sstevel@tonic-gate 799*7c478bd9Sstevel@tonic-gate return (error); 800*7c478bd9Sstevel@tonic-gate } 801*7c478bd9Sstevel@tonic-gate 802*7c478bd9Sstevel@tonic-gate /* 803*7c478bd9Sstevel@tonic-gate * If operation = PID_STARTUP then checks the nfslogd.pid file, it is opened 804*7c478bd9Sstevel@tonic-gate * if it exists, read and the pid is checked for an active process. If no 805*7c478bd9Sstevel@tonic-gate * active process is found, the pid of this process is written to the file, 806*7c478bd9Sstevel@tonic-gate * and 0 is returned, otherwise non-zero error is returned. 807*7c478bd9Sstevel@tonic-gate * 808*7c478bd9Sstevel@tonic-gate * If operation = PID_SHUTDOWN then removes the nfslogd.pid file and 0 is 809*7c478bd9Sstevel@tonic-gate * returned. 810*7c478bd9Sstevel@tonic-gate */ 811*7c478bd9Sstevel@tonic-gate static int 812*7c478bd9Sstevel@tonic-gate process_pidfile(enum pidfile_operation op) 813*7c478bd9Sstevel@tonic-gate { 814*7c478bd9Sstevel@tonic-gate int fd, read_count; 815*7c478bd9Sstevel@tonic-gate int error = 0; 816*7c478bd9Sstevel@tonic-gate pid_t pid, mypid; 817*7c478bd9Sstevel@tonic-gate char *PidFile = NFSLOGD_PIDFILE; 818*7c478bd9Sstevel@tonic-gate int open_flags; 819*7c478bd9Sstevel@tonic-gate 820*7c478bd9Sstevel@tonic-gate if (op == PID_STARTUP) 821*7c478bd9Sstevel@tonic-gate open_flags = O_RDWR | O_CREAT; 822*7c478bd9Sstevel@tonic-gate else { 823*7c478bd9Sstevel@tonic-gate assert(op == PID_SHUTDOWN); 824*7c478bd9Sstevel@tonic-gate open_flags = O_RDWR; 825*7c478bd9Sstevel@tonic-gate } 826*7c478bd9Sstevel@tonic-gate 827*7c478bd9Sstevel@tonic-gate if ((fd = open(PidFile, open_flags, 0600)) < 0) { 828*7c478bd9Sstevel@tonic-gate error = errno; 829*7c478bd9Sstevel@tonic-gate if (error == ENOENT && op == PID_SHUTDOWN) { 830*7c478bd9Sstevel@tonic-gate /* 831*7c478bd9Sstevel@tonic-gate * We were going to remove it anyway 832*7c478bd9Sstevel@tonic-gate */ 833*7c478bd9Sstevel@tonic-gate error = 0; 834*7c478bd9Sstevel@tonic-gate goto out; 835*7c478bd9Sstevel@tonic-gate } 836*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 837*7c478bd9Sstevel@tonic-gate "cannot open or create pid file %s\n"), PidFile); 838*7c478bd9Sstevel@tonic-gate goto out; 839*7c478bd9Sstevel@tonic-gate } 840*7c478bd9Sstevel@tonic-gate if (lockf(fd, F_LOCK, 0) < 0) { 841*7c478bd9Sstevel@tonic-gate error = errno; 842*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 843*7c478bd9Sstevel@tonic-gate "Cannot lock %s - %s\n"), PidFile, 844*7c478bd9Sstevel@tonic-gate strerror(error)); 845*7c478bd9Sstevel@tonic-gate goto out; 846*7c478bd9Sstevel@tonic-gate } 847*7c478bd9Sstevel@tonic-gate if ((read_count = read(fd, &pid, sizeof (pid))) < 0) { 848*7c478bd9Sstevel@tonic-gate error = errno; 849*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 850*7c478bd9Sstevel@tonic-gate "Can not read from file %s - %s\n"), PidFile, 851*7c478bd9Sstevel@tonic-gate strerror(error)); 852*7c478bd9Sstevel@tonic-gate } 853*7c478bd9Sstevel@tonic-gate 854*7c478bd9Sstevel@tonic-gate mypid = getpid(); 855*7c478bd9Sstevel@tonic-gate if (op == PID_STARTUP) { 856*7c478bd9Sstevel@tonic-gate if (read_count > 0) { 857*7c478bd9Sstevel@tonic-gate if (kill(pid, 0) == 0) { 858*7c478bd9Sstevel@tonic-gate error = EEXIST; 859*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 860*7c478bd9Sstevel@tonic-gate "Terminated - nfslogd(%ld) already " 861*7c478bd9Sstevel@tonic-gate "running.\n"), pid); 862*7c478bd9Sstevel@tonic-gate goto out; 863*7c478bd9Sstevel@tonic-gate } else if (errno != ESRCH) { 864*7c478bd9Sstevel@tonic-gate error = errno; 865*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 866*7c478bd9Sstevel@tonic-gate "Unexpected error returned %s\n"), 867*7c478bd9Sstevel@tonic-gate strerror(error)); 868*7c478bd9Sstevel@tonic-gate goto out; 869*7c478bd9Sstevel@tonic-gate } 870*7c478bd9Sstevel@tonic-gate } 871*7c478bd9Sstevel@tonic-gate pid = mypid; 872*7c478bd9Sstevel@tonic-gate /* 873*7c478bd9Sstevel@tonic-gate * rewind the file to overwrite old pid 874*7c478bd9Sstevel@tonic-gate */ 875*7c478bd9Sstevel@tonic-gate (void) lseek(fd, 0, SEEK_SET); 876*7c478bd9Sstevel@tonic-gate if (write(fd, &mypid, sizeof (mypid)) < 0) { 877*7c478bd9Sstevel@tonic-gate error = errno; 878*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 879*7c478bd9Sstevel@tonic-gate "Cannot update %s: %s\n"), 880*7c478bd9Sstevel@tonic-gate PidFile, strerror(error)); 881*7c478bd9Sstevel@tonic-gate } 882*7c478bd9Sstevel@tonic-gate } else { 883*7c478bd9Sstevel@tonic-gate assert(pid == mypid); 884*7c478bd9Sstevel@tonic-gate if (unlink(PidFile)) { 885*7c478bd9Sstevel@tonic-gate error = errno; 886*7c478bd9Sstevel@tonic-gate syslog(LOG_ERR, gettext("Cannot remove %s: %s"), 887*7c478bd9Sstevel@tonic-gate strerror(error)); 888*7c478bd9Sstevel@tonic-gate } 889*7c478bd9Sstevel@tonic-gate } 890*7c478bd9Sstevel@tonic-gate out: 891*7c478bd9Sstevel@tonic-gate if (fd >= 0) 892*7c478bd9Sstevel@tonic-gate (void) close(fd); 893*7c478bd9Sstevel@tonic-gate return (error); 894*7c478bd9Sstevel@tonic-gate } 895*7c478bd9Sstevel@tonic-gate 896*7c478bd9Sstevel@tonic-gate /* 897*7c478bd9Sstevel@tonic-gate * Forces a timeout on all open transactions. 898*7c478bd9Sstevel@tonic-gate */ 899*7c478bd9Sstevel@tonic-gate static void 900*7c478bd9Sstevel@tonic-gate transactions_timeout(nfsl_config_t *clp) 901*7c478bd9Sstevel@tonic-gate { 902*7c478bd9Sstevel@tonic-gate for (; clp != NULL; clp = clp->nc_next) { 903*7c478bd9Sstevel@tonic-gate if (clp->nc_transcookie != NULL) { 904*7c478bd9Sstevel@tonic-gate nfslog_process_trans_timeout( 905*7c478bd9Sstevel@tonic-gate (struct nfslog_trans_file *)clp->nc_transcookie, 906*7c478bd9Sstevel@tonic-gate FALSE); 907*7c478bd9Sstevel@tonic-gate } 908*7c478bd9Sstevel@tonic-gate } 909*7c478bd9Sstevel@tonic-gate } 910*7c478bd9Sstevel@tonic-gate 911*7c478bd9Sstevel@tonic-gate /* 912*7c478bd9Sstevel@tonic-gate * Closes all transaction logs causing outstanding transactions 913*7c478bd9Sstevel@tonic-gate * to be flushed to their respective log. 914*7c478bd9Sstevel@tonic-gate */ 915*7c478bd9Sstevel@tonic-gate static void 916*7c478bd9Sstevel@tonic-gate close_all_translogs(nfsl_config_t *clp) 917*7c478bd9Sstevel@tonic-gate { 918*7c478bd9Sstevel@tonic-gate for (; clp != NULL; clp = clp->nc_next) { 919*7c478bd9Sstevel@tonic-gate if (clp->nc_transcookie != NULL) { 920*7c478bd9Sstevel@tonic-gate nfslog_close_transactions(&clp->nc_transcookie); 921*7c478bd9Sstevel@tonic-gate assert(clp->nc_transcookie == NULL); 922*7c478bd9Sstevel@tonic-gate } 923*7c478bd9Sstevel@tonic-gate } 924*7c478bd9Sstevel@tonic-gate } 925