10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 231Sgt29601 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * sm_statd.c consists of routines used for the intermediate 440Sstevel@tonic-gate * statd implementation(3.2 rpc.statd); 450Sstevel@tonic-gate * it creates an entry in "current" directory for each site that it monitors; 460Sstevel@tonic-gate * after crash and recovery, it moves all entries in "current" 470Sstevel@tonic-gate * to "backup" directory, and notifies the corresponding statd of its recovery. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include <stdio.h> 510Sstevel@tonic-gate #include <stdlib.h> 520Sstevel@tonic-gate #include <unistd.h> 530Sstevel@tonic-gate #include <string.h> 540Sstevel@tonic-gate #include <syslog.h> 550Sstevel@tonic-gate #include <netdb.h> 560Sstevel@tonic-gate #include <sys/types.h> 570Sstevel@tonic-gate #include <sys/stat.h> 580Sstevel@tonic-gate #include <sys/file.h> 590Sstevel@tonic-gate #include <sys/param.h> 600Sstevel@tonic-gate #include <arpa/inet.h> 610Sstevel@tonic-gate #include <dirent.h> 620Sstevel@tonic-gate #include <rpc/rpc.h> 630Sstevel@tonic-gate #include <rpcsvc/sm_inter.h> 640Sstevel@tonic-gate #include <rpcsvc/nsm_addr.h> 650Sstevel@tonic-gate #include <errno.h> 660Sstevel@tonic-gate #include <memory.h> 670Sstevel@tonic-gate #include <signal.h> 680Sstevel@tonic-gate #include <synch.h> 690Sstevel@tonic-gate #include <thread.h> 700Sstevel@tonic-gate #include <limits.h> 710Sstevel@tonic-gate #include <strings.h> 720Sstevel@tonic-gate #include "sm_statd.h" 730Sstevel@tonic-gate 740Sstevel@tonic-gate 750Sstevel@tonic-gate int LOCAL_STATE; 760Sstevel@tonic-gate 770Sstevel@tonic-gate sm_hash_t mon_table[MAX_HASHSIZE]; 780Sstevel@tonic-gate static sm_hash_t record_table[MAX_HASHSIZE]; 790Sstevel@tonic-gate static sm_hash_t recov_q; 800Sstevel@tonic-gate 810Sstevel@tonic-gate static name_entry *find_name(name_entry **namepp, char *name); 820Sstevel@tonic-gate static name_entry *insert_name(name_entry **namepp, char *name, 830Sstevel@tonic-gate int need_alloc); 840Sstevel@tonic-gate static void delete_name(name_entry **namepp, char *name); 850Sstevel@tonic-gate static void remove_name(char *name, int op, int startup); 860Sstevel@tonic-gate static int statd_call_statd(char *name); 870Sstevel@tonic-gate static void pr_name(char *name, int flag); 880Sstevel@tonic-gate static void *thr_statd_init(); 890Sstevel@tonic-gate static void *sm_try(); 900Sstevel@tonic-gate static void *thr_call_statd(void *); 910Sstevel@tonic-gate static void remove_single_name(char *name, char *dir1, char *dir2); 920Sstevel@tonic-gate static int move_file(char *fromdir, char *file, char *todir); 930Sstevel@tonic-gate static int count_symlinks(char *dir, char *name, int *count); 940Sstevel@tonic-gate static char *family2string(sa_family_t family); 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * called when statd first comes up; it searches /etc/sm to gather 980Sstevel@tonic-gate * all entries to notify its own failure 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate void 1010Sstevel@tonic-gate statd_init() 1020Sstevel@tonic-gate { 103*871Scasper struct dirent *dirp; 1040Sstevel@tonic-gate DIR *dp; 1050Sstevel@tonic-gate FILE *fp, *fp_tmp; 1060Sstevel@tonic-gate int i, tmp_state; 1070Sstevel@tonic-gate char state_file[MAXPATHLEN+SM_MAXPATHLEN]; 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate if (debug) 1100Sstevel@tonic-gate (void) printf("enter statd_init\n"); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * First try to open the file. If that fails, try to create it. 1140Sstevel@tonic-gate * If that fails, give up. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate if ((fp = fopen(STATE, "r+")) == (FILE *)NULL) 1170Sstevel@tonic-gate if ((fp = fopen(STATE, "w+")) == (FILE *)NULL) { 1180Sstevel@tonic-gate syslog(LOG_ERR, "can't open %s: %m", STATE); 1190Sstevel@tonic-gate exit(1); 1200Sstevel@tonic-gate } else 1210Sstevel@tonic-gate (void) chmod(STATE, 0644); 1220Sstevel@tonic-gate if ((fscanf(fp, "%d", &LOCAL_STATE)) == EOF) { 1230Sstevel@tonic-gate if (debug >= 2) 1240Sstevel@tonic-gate (void) printf("empty file\n"); 1250Sstevel@tonic-gate LOCAL_STATE = 0; 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * Scan alternate paths for largest "state" number 1300Sstevel@tonic-gate */ 1310Sstevel@tonic-gate for (i = 0; i < pathix; i++) { 1320Sstevel@tonic-gate (void) sprintf(state_file, "%s/statmon/state", path_name[i]); 1330Sstevel@tonic-gate if ((fp_tmp = fopen(state_file, "r+")) == (FILE *)NULL) { 1340Sstevel@tonic-gate if ((fp_tmp = fopen(state_file, "w+")) 1350Sstevel@tonic-gate == (FILE *)NULL) { 1360Sstevel@tonic-gate if (debug) 1370Sstevel@tonic-gate syslog(LOG_ERR, 1380Sstevel@tonic-gate "can't open %s: %m", 1390Sstevel@tonic-gate state_file); 1400Sstevel@tonic-gate continue; 1410Sstevel@tonic-gate } else 1420Sstevel@tonic-gate (void) chmod(state_file, 0644); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate if ((fscanf(fp_tmp, "%d", &tmp_state)) == EOF) { 1450Sstevel@tonic-gate if (debug) 1460Sstevel@tonic-gate syslog(LOG_ERR, 1470Sstevel@tonic-gate "statd: %s: file empty\n", state_file); 1480Sstevel@tonic-gate (void) fclose(fp_tmp); 1490Sstevel@tonic-gate continue; 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate if (tmp_state > LOCAL_STATE) { 1520Sstevel@tonic-gate LOCAL_STATE = tmp_state; 1530Sstevel@tonic-gate if (debug) 1540Sstevel@tonic-gate (void) printf("Update LOCAL STATE: %d\n", 1550Sstevel@tonic-gate tmp_state); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate (void) fclose(fp_tmp); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate LOCAL_STATE = ((LOCAL_STATE%2) == 0) ? LOCAL_STATE+1 : LOCAL_STATE+2; 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* IF local state overflows, reset to value 1 */ 1630Sstevel@tonic-gate if (LOCAL_STATE < 0) { 1640Sstevel@tonic-gate LOCAL_STATE = 1; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* Copy the LOCAL_STATE value back to all stat files */ 1680Sstevel@tonic-gate if (fseek(fp, 0, 0) == -1) { 1690Sstevel@tonic-gate syslog(LOG_ERR, "statd: fseek failed\n"); 1700Sstevel@tonic-gate exit(1); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate (void) fprintf(fp, "%-10d", LOCAL_STATE); 1740Sstevel@tonic-gate (void) fflush(fp); 1750Sstevel@tonic-gate if (fsync(fileno(fp)) == -1) { 1760Sstevel@tonic-gate syslog(LOG_ERR, "statd: fsync failed\n"); 1770Sstevel@tonic-gate exit(1); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate (void) fclose(fp); 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate for (i = 0; i < pathix; i++) { 1820Sstevel@tonic-gate (void) sprintf(state_file, "%s/statmon/state", path_name[i]); 1830Sstevel@tonic-gate if ((fp_tmp = fopen(state_file, "r+")) == (FILE *)NULL) { 1840Sstevel@tonic-gate if ((fp_tmp = fopen(state_file, "w+")) 1850Sstevel@tonic-gate == (FILE *)NULL) { 1860Sstevel@tonic-gate syslog(LOG_ERR, 1870Sstevel@tonic-gate "can't open %s: %m", state_file); 1880Sstevel@tonic-gate continue; 1890Sstevel@tonic-gate } else 1900Sstevel@tonic-gate (void) chmod(state_file, 0644); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate (void) fprintf(fp_tmp, "%-10d", LOCAL_STATE); 1930Sstevel@tonic-gate (void) fflush(fp_tmp); 1940Sstevel@tonic-gate if (fsync(fileno(fp_tmp)) == -1) { 1950Sstevel@tonic-gate syslog(LOG_ERR, 1960Sstevel@tonic-gate "statd: %s: fsync failed\n", state_file); 1970Sstevel@tonic-gate (void) fclose(fp_tmp); 1980Sstevel@tonic-gate exit(1); 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate (void) fclose(fp_tmp); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if (debug) 2040Sstevel@tonic-gate (void) printf("local state = %d\n", LOCAL_STATE); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate if ((mkdir(CURRENT, SM_DIRECTORY_MODE)) == -1) { 2070Sstevel@tonic-gate if (errno != EEXIST) { 2080Sstevel@tonic-gate syslog(LOG_ERR, "statd: mkdir current, error %m\n"); 2090Sstevel@tonic-gate exit(1); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate if ((mkdir(BACKUP, SM_DIRECTORY_MODE)) == -1) { 2130Sstevel@tonic-gate if (errno != EEXIST) { 2140Sstevel@tonic-gate syslog(LOG_ERR, "statd: mkdir backup, error %m\n"); 2150Sstevel@tonic-gate exit(1); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* get all entries in CURRENT into BACKUP */ 2200Sstevel@tonic-gate if ((dp = opendir(CURRENT)) == (DIR *)NULL) { 2210Sstevel@tonic-gate syslog(LOG_ERR, "statd: open current directory, error %m\n"); 2220Sstevel@tonic-gate exit(1); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 225*871Scasper while ((dirp = readdir(dp)) != NULL) { 2260Sstevel@tonic-gate if (strcmp(dirp->d_name, ".") != 0 && 2270Sstevel@tonic-gate strcmp(dirp->d_name, "..") != 0) { 2280Sstevel@tonic-gate /* rename all entries from CURRENT to BACKUP */ 2290Sstevel@tonic-gate (void) move_file(CURRENT, dirp->d_name, BACKUP); 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate (void) closedir(dp); 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* Contact hosts' statd */ 2360Sstevel@tonic-gate if (thr_create(NULL, NULL, thr_statd_init, NULL, THR_DETACHED, 0)) { 2370Sstevel@tonic-gate syslog(LOG_ERR, 2380Sstevel@tonic-gate "statd: unable to create thread for thr_statd_init\n"); 2390Sstevel@tonic-gate exit(1); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate /* 2440Sstevel@tonic-gate * Work thread which contacts hosts' statd. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate void * 2470Sstevel@tonic-gate thr_statd_init() 2480Sstevel@tonic-gate { 249*871Scasper struct dirent *dirp; 2500Sstevel@tonic-gate DIR *dp; 2510Sstevel@tonic-gate int num_threads; 2520Sstevel@tonic-gate int num_join; 2530Sstevel@tonic-gate int i; 2540Sstevel@tonic-gate char *name; 2550Sstevel@tonic-gate char buf[MAXPATHLEN+SM_MAXPATHLEN]; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* Go thru backup directory and contact hosts */ 2580Sstevel@tonic-gate if ((dp = opendir(BACKUP)) == (DIR *)NULL) { 2590Sstevel@tonic-gate syslog(LOG_ERR, "statd: open backup directory, error %m\n"); 2600Sstevel@tonic-gate exit(1); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Create "UNDETACHED" threads for each symlink and (unlinked) 2650Sstevel@tonic-gate * regular file in backup directory to initiate statd_call_statd. 2660Sstevel@tonic-gate * NOTE: These threads are the only undetached threads in this 2670Sstevel@tonic-gate * program and thus, the thread id is not needed to join the threads. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate num_threads = 0; 270*871Scasper while ((dirp = readdir(dp)) != NULL) { 2710Sstevel@tonic-gate /* 2720Sstevel@tonic-gate * If host file is not a symlink, don't bother to 2730Sstevel@tonic-gate * spawn a thread for it. If any link(s) refer to 2740Sstevel@tonic-gate * it, the host will be contacted using the link(s). 2750Sstevel@tonic-gate * If not, we'll deal with it during the legacy pass. 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", BACKUP, dirp->d_name); 2780Sstevel@tonic-gate if (is_symlink(buf) == 0) { 2790Sstevel@tonic-gate continue; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* 2830Sstevel@tonic-gate * If the num_threads has exceeded, wait until 2840Sstevel@tonic-gate * a certain amount of threads have finished. 2850Sstevel@tonic-gate * Currently, 10% of threads created should be joined. 2860Sstevel@tonic-gate */ 2870Sstevel@tonic-gate if (num_threads > MAX_THR) { 2880Sstevel@tonic-gate num_join = num_threads/PERCENT_MINJOIN; 2890Sstevel@tonic-gate for (i = 0; i < num_join; i++) 2900Sstevel@tonic-gate thr_join(0, 0, 0); 2910Sstevel@tonic-gate num_threads -= num_join; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * If can't alloc name then print error msg and 2960Sstevel@tonic-gate * continue to next item on list. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate name = strdup(dirp->d_name); 2990Sstevel@tonic-gate if (name == (char *)NULL) { 3000Sstevel@tonic-gate syslog(LOG_ERR, 3010Sstevel@tonic-gate "statd: unable to allocate space for name %s\n", 3020Sstevel@tonic-gate dirp->d_name); 3030Sstevel@tonic-gate continue; 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate /* Create a thread to do a statd_call_statd for name */ 3070Sstevel@tonic-gate if (thr_create(NULL, NULL, thr_call_statd, 3080Sstevel@tonic-gate (void *) name, 0, 0)) { 3090Sstevel@tonic-gate syslog(LOG_ERR, 3100Sstevel@tonic-gate "statd: unable to create thr_call_statd() for name %s.\n", 3110Sstevel@tonic-gate dirp->d_name); 3120Sstevel@tonic-gate free(name); 3130Sstevel@tonic-gate continue; 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate num_threads++; 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Join the other threads created above before processing the 3200Sstevel@tonic-gate * legacies. This allows all symlinks and the regular files 3210Sstevel@tonic-gate * to which they correspond to be processed and deleted. 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate for (i = 0; i < num_threads; i++) { 3240Sstevel@tonic-gate thr_join(0, 0, 0); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate /* 3280Sstevel@tonic-gate * The second pass checks for `legacies': regular files which 3290Sstevel@tonic-gate * never had symlinks pointing to them at all, just like in the 3300Sstevel@tonic-gate * good old (pre-1184192 fix) days. Once a machine has cleaned 3310Sstevel@tonic-gate * up its legacies they should only reoccur due to catastrophes 3320Sstevel@tonic-gate * (e.g., severed symlinks). 3330Sstevel@tonic-gate */ 3340Sstevel@tonic-gate rewinddir(dp); 3350Sstevel@tonic-gate num_threads = 0; 336*871Scasper while ((dirp = readdir(dp)) != NULL) { 3370Sstevel@tonic-gate if (strcmp(dirp->d_name, ".") == 0 || 3380Sstevel@tonic-gate strcmp(dirp->d_name, "..") == 0) { 3390Sstevel@tonic-gate continue; 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", BACKUP, dirp->d_name); 3430Sstevel@tonic-gate if (is_symlink(buf)) { 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * We probably couldn't reach this host and it's 3460Sstevel@tonic-gate * been put on the recovery queue for retry. 3470Sstevel@tonic-gate * Skip it and keep looking for regular files. 3480Sstevel@tonic-gate */ 3490Sstevel@tonic-gate continue; 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate if (debug) { 3530Sstevel@tonic-gate (void) printf("thr_statd_init: legacy %s\n", 3540Sstevel@tonic-gate dirp->d_name); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate /* 3580Sstevel@tonic-gate * If the number of threads exceeds the maximum, wait 3590Sstevel@tonic-gate * for some fraction of them to finish before 3600Sstevel@tonic-gate * continuing. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate if (num_threads > MAX_THR) { 3630Sstevel@tonic-gate num_join = num_threads/PERCENT_MINJOIN; 3640Sstevel@tonic-gate for (i = 0; i < num_join; i++) 3650Sstevel@tonic-gate thr_join(0, 0, 0); 3660Sstevel@tonic-gate num_threads -= num_join; 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * If can't alloc name then print error msg and 3710Sstevel@tonic-gate * continue to next item on list. 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate name = strdup(dirp->d_name); 3740Sstevel@tonic-gate if (name == (char *)NULL) { 3750Sstevel@tonic-gate syslog(LOG_ERR, 3760Sstevel@tonic-gate "statd: unable to allocate space for name %s\n", 3770Sstevel@tonic-gate dirp->d_name); 3780Sstevel@tonic-gate continue; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* Create a thread to do a statd_call_statd for name */ 3820Sstevel@tonic-gate if (thr_create(NULL, NULL, thr_call_statd, 3830Sstevel@tonic-gate (void *) name, 0, 0)) { 3840Sstevel@tonic-gate syslog(LOG_ERR, 3850Sstevel@tonic-gate "statd: unable to create thr_call_statd() for name %s.\n", 3860Sstevel@tonic-gate dirp->d_name); 3870Sstevel@tonic-gate free(name); 3880Sstevel@tonic-gate continue; 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate num_threads++; 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate (void) closedir(dp); 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate /* 3960Sstevel@tonic-gate * Join the other threads created above before creating thread 3970Sstevel@tonic-gate * to process items in recovery table. 3980Sstevel@tonic-gate */ 3990Sstevel@tonic-gate for (i = 0; i < num_threads; i++) { 4000Sstevel@tonic-gate thr_join(0, 0, 0); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* 4040Sstevel@tonic-gate * Need to only copy /var/statmon/sm.bak to alternate paths, since 4050Sstevel@tonic-gate * the only hosts in /var/statmon/sm should be the ones currently 4060Sstevel@tonic-gate * being monitored and already should be in alternate paths as part 4070Sstevel@tonic-gate * of insert_mon(). 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate for (i = 0; i < pathix; i++) { 4100Sstevel@tonic-gate (void) sprintf(buf, "%s/statmon/sm.bak", path_name[i]); 4110Sstevel@tonic-gate if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) { 4120Sstevel@tonic-gate if (errno != EEXIST) 4130Sstevel@tonic-gate syslog(LOG_ERR, "statd: mkdir %s error %m\n", 4140Sstevel@tonic-gate buf); 4150Sstevel@tonic-gate else 4160Sstevel@tonic-gate copydir_from_to(BACKUP, buf); 4170Sstevel@tonic-gate } else 4180Sstevel@tonic-gate copydir_from_to(BACKUP, buf); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * Reset the die and in_crash variable and signal other threads 4240Sstevel@tonic-gate * that have issued an sm_crash and are waiting. 4250Sstevel@tonic-gate */ 4260Sstevel@tonic-gate mutex_lock(&crash_lock); 4270Sstevel@tonic-gate die = 0; 4280Sstevel@tonic-gate in_crash = 0; 4290Sstevel@tonic-gate mutex_unlock(&crash_lock); 4300Sstevel@tonic-gate cond_broadcast(&crash_finish); 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (debug) 4330Sstevel@tonic-gate (void) printf("Creating thread for sm_try\n"); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* Continue to notify statd on hosts that were unreachable. */ 4360Sstevel@tonic-gate if (thr_create(NULL, NULL, sm_try, NULL, THR_DETACHED, 0)) 4370Sstevel@tonic-gate syslog(LOG_ERR, 4380Sstevel@tonic-gate "statd: unable to create thread for sm_try().\n"); 4390Sstevel@tonic-gate thr_exit((void *) 0); 4400Sstevel@tonic-gate #ifdef lint 4410Sstevel@tonic-gate return (0); 4420Sstevel@tonic-gate #endif 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate /* 4460Sstevel@tonic-gate * Work thread to make call to statd_call_statd. 4470Sstevel@tonic-gate */ 4480Sstevel@tonic-gate void * 4490Sstevel@tonic-gate thr_call_statd(void *namep) 4500Sstevel@tonic-gate { 4510Sstevel@tonic-gate char *name = (char *)namep; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate /* 4540Sstevel@tonic-gate * If statd of name is unreachable, add name to recovery table 4550Sstevel@tonic-gate * otherwise if statd_call_statd was successful, remove from backup. 4560Sstevel@tonic-gate */ 4570Sstevel@tonic-gate if (statd_call_statd(name) != 0) { 4580Sstevel@tonic-gate int n; 4590Sstevel@tonic-gate char *tail; 4600Sstevel@tonic-gate char path[MAXPATHLEN]; 4610Sstevel@tonic-gate /* 4620Sstevel@tonic-gate * since we are constructing this pathname below we add 4630Sstevel@tonic-gate * another space for the terminating NULL so we don't 4640Sstevel@tonic-gate * overflow our buffer when we do the readlink 4650Sstevel@tonic-gate */ 4660Sstevel@tonic-gate char rname[MAXNAMELEN + 1]; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate if (debug) { 4690Sstevel@tonic-gate (void) printf( 4700Sstevel@tonic-gate "statd call failed, inserting %s in recov_q\n", name); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate mutex_lock(&recov_q.lock); 4730Sstevel@tonic-gate (void) insert_name(&recov_q.sm_recovhdp, name, 0); 4740Sstevel@tonic-gate mutex_unlock(&recov_q.lock); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate /* 4770Sstevel@tonic-gate * If we queued a symlink name in the recovery queue, 4780Sstevel@tonic-gate * we now clean up the regular file to which it referred. 4790Sstevel@tonic-gate * This may leave a severed symlink if multiple links 4800Sstevel@tonic-gate * referred to one regular file; this is unaesthetic but 4810Sstevel@tonic-gate * it works. The big benefit is that it prevents us 4820Sstevel@tonic-gate * from recovering the same host twice (as symlink and 4830Sstevel@tonic-gate * as regular file) needlessly, usually on separate reboots. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate (void) strcpy(path, BACKUP); 4860Sstevel@tonic-gate (void) strcat(path, "/"); 4870Sstevel@tonic-gate (void) strcat(path, name); 4880Sstevel@tonic-gate if (is_symlink(path)) { 4890Sstevel@tonic-gate n = readlink(path, rname, MAXNAMELEN); 4900Sstevel@tonic-gate if (n <= 0) { 4910Sstevel@tonic-gate if (debug >= 2) { 4920Sstevel@tonic-gate (void) printf( 4930Sstevel@tonic-gate "thr_call_statd: can't read link %s\n", 4940Sstevel@tonic-gate path); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate } else { 4970Sstevel@tonic-gate rname[n] = '\0'; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate tail = strrchr(path, '/') + 1; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate if ((strlen(BACKUP) + strlen(rname) + 2) <= 5020Sstevel@tonic-gate MAXPATHLEN) { 5030Sstevel@tonic-gate (void) strcpy(tail, rname); 5040Sstevel@tonic-gate delete_file(path); 5050Sstevel@tonic-gate } else if (debug) { 5060Sstevel@tonic-gate printf("thr_call_statd: path over" 5070Sstevel@tonic-gate "maxpathlen!\n"); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate if (debug) 5140Sstevel@tonic-gate pr_name(name, 0); 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate } else { 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * If `name' is an IP address symlink to a name file, 5190Sstevel@tonic-gate * remove it now. If it is the last such symlink, 5200Sstevel@tonic-gate * remove the name file as well. Regular files with 5210Sstevel@tonic-gate * no symlinks to them are assumed to be legacies and 5220Sstevel@tonic-gate * are removed as well. 5230Sstevel@tonic-gate */ 5240Sstevel@tonic-gate remove_name(name, 1, 1); 5250Sstevel@tonic-gate free(name); 5260Sstevel@tonic-gate } 5270Sstevel@tonic-gate thr_exit((void *) 0); 5280Sstevel@tonic-gate #ifdef lint 5290Sstevel@tonic-gate return (0); 5300Sstevel@tonic-gate #endif 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * Notifies the statd of host specified by name to indicate that 5350Sstevel@tonic-gate * state has changed for this server. 5360Sstevel@tonic-gate */ 5370Sstevel@tonic-gate static int 5380Sstevel@tonic-gate statd_call_statd(name) 5390Sstevel@tonic-gate char *name; 5400Sstevel@tonic-gate { 5410Sstevel@tonic-gate enum clnt_stat clnt_stat; 5420Sstevel@tonic-gate struct timeval tottimeout; 5430Sstevel@tonic-gate CLIENT *clnt; 5440Sstevel@tonic-gate char *name_or_addr; 5450Sstevel@tonic-gate stat_chge ntf; 5460Sstevel@tonic-gate int i; 5470Sstevel@tonic-gate int rc; 5480Sstevel@tonic-gate int dummy1, dummy2, dummy3, dummy4; 5490Sstevel@tonic-gate char ascii_addr[MAXNAMELEN]; 5500Sstevel@tonic-gate size_t unq_len; 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate ntf.mon_name = hostname; 5530Sstevel@tonic-gate ntf.state = LOCAL_STATE; 5540Sstevel@tonic-gate if (debug) 5550Sstevel@tonic-gate (void) printf("statd_call_statd at %s\n", name); 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate /* 5580Sstevel@tonic-gate * If it looks like an ASCII <address family>.<address> specifier, 5590Sstevel@tonic-gate * strip off the family - we just want the address when obtaining 5600Sstevel@tonic-gate * a client handle. 5610Sstevel@tonic-gate * If it's anything else, just pass it on to create_client(). 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate unq_len = strcspn(name, "."); 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate if ((strncmp(name, SM_ADDR_IPV4, unq_len) == 0) || 5660Sstevel@tonic-gate (strncmp(name, SM_ADDR_IPV6, unq_len) == 0)) { 5670Sstevel@tonic-gate name_or_addr = strchr(name, '.') + 1; 5680Sstevel@tonic-gate } else { 5690Sstevel@tonic-gate name_or_addr = name; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * NOTE: We depend here upon the fact that the RPC client code 5740Sstevel@tonic-gate * allows us to use ASCII dotted quad `names', i.e. "192.9.200.1". 5750Sstevel@tonic-gate * This may change in a future release. 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate if (debug) { 5780Sstevel@tonic-gate (void) printf("statd_call_statd: calling create_client(%s)\n", 5790Sstevel@tonic-gate name_or_addr); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate tottimeout.tv_sec = SM_RPC_TIMEOUT; 5830Sstevel@tonic-gate tottimeout.tv_usec = 0; 5840Sstevel@tonic-gate 5851Sgt29601 if ((clnt = create_client(name_or_addr, SM_PROG, SM_VERS, 5861Sgt29601 &tottimeout)) == (CLIENT *) NULL) { 5871Sgt29601 return (-1); 5881Sgt29601 } 5891Sgt29601 5900Sstevel@tonic-gate /* Perform notification to client */ 5910Sstevel@tonic-gate rc = 0; 5920Sstevel@tonic-gate clnt_stat = clnt_call(clnt, SM_NOTIFY, xdr_stat_chge, (char *)&ntf, 5930Sstevel@tonic-gate xdr_void, NULL, tottimeout); 5940Sstevel@tonic-gate if (debug) { 5950Sstevel@tonic-gate (void) printf("clnt_stat=%s(%d)\n", 5960Sstevel@tonic-gate clnt_sperrno(clnt_stat), clnt_stat); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate if (clnt_stat != (int)RPC_SUCCESS) { 5990Sstevel@tonic-gate syslog(LOG_WARNING, 6000Sstevel@tonic-gate "statd: cannot talk to statd at %s, %s(%d)\n", 6010Sstevel@tonic-gate name_or_addr, clnt_sperrno(clnt_stat), clnt_stat); 6020Sstevel@tonic-gate rc = -1; 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* For HA systems and multi-homed hosts */ 6060Sstevel@tonic-gate ntf.state = LOCAL_STATE; 6070Sstevel@tonic-gate for (i = 0; i < addrix; i++) { 6080Sstevel@tonic-gate ntf.mon_name = host_name[i]; 6090Sstevel@tonic-gate if (debug) 6100Sstevel@tonic-gate (void) printf("statd_call_statd at %s\n", name_or_addr); 6110Sstevel@tonic-gate clnt_stat = clnt_call(clnt, SM_NOTIFY, xdr_stat_chge, 6120Sstevel@tonic-gate (char *)&ntf, xdr_void, NULL, 6130Sstevel@tonic-gate tottimeout); 6140Sstevel@tonic-gate if (clnt_stat != (int)RPC_SUCCESS) { 6150Sstevel@tonic-gate syslog(LOG_WARNING, 6160Sstevel@tonic-gate "statd: cannot talk to statd at %s, %s(%d)\n", 6170Sstevel@tonic-gate name_or_addr, clnt_sperrno(clnt_stat), clnt_stat); 6180Sstevel@tonic-gate rc = -1; 6190Sstevel@tonic-gate } 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate clnt_destroy(clnt); 6220Sstevel@tonic-gate return (rc); 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate /* 6260Sstevel@tonic-gate * Continues to contact hosts in recovery table that were unreachable. 6270Sstevel@tonic-gate * NOTE: There should only be one sm_try thread executing and 6280Sstevel@tonic-gate * thus locks are not needed for recovery table. Die is only cleared 6290Sstevel@tonic-gate * after all the hosts has at least been contacted once. The reader/writer 6300Sstevel@tonic-gate * lock ensures to finish this code before an sm_crash is started. Die 6310Sstevel@tonic-gate * variable will signal it. 6320Sstevel@tonic-gate */ 6330Sstevel@tonic-gate void * 6340Sstevel@tonic-gate sm_try() 6350Sstevel@tonic-gate { 6360Sstevel@tonic-gate name_entry *nl, *next; 6370Sstevel@tonic-gate timestruc_t wtime; 6380Sstevel@tonic-gate int delay = 0; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate rw_rdlock(&thr_rwlock); 6410Sstevel@tonic-gate if (mutex_trylock(&sm_trylock)) 6420Sstevel@tonic-gate goto out; 6430Sstevel@tonic-gate mutex_lock(&crash_lock); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate while (!die) { 6460Sstevel@tonic-gate wtime.tv_sec = delay; 6470Sstevel@tonic-gate wtime.tv_nsec = 0; 6480Sstevel@tonic-gate /* 6490Sstevel@tonic-gate * Wait until signalled to wakeup or time expired. 6500Sstevel@tonic-gate * If signalled to be awoken, then a crash has occurred 6510Sstevel@tonic-gate * or otherwise time expired. 6520Sstevel@tonic-gate */ 6530Sstevel@tonic-gate if (cond_reltimedwait(&retrywait, &crash_lock, &wtime) == 0) { 6540Sstevel@tonic-gate break; 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* Exit loop if queue is empty */ 6580Sstevel@tonic-gate if ((next = recov_q.sm_recovhdp) == NULL) 6590Sstevel@tonic-gate break; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate mutex_unlock(&crash_lock); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate while (((nl = next) != (name_entry *)NULL) && (!die)) { 6640Sstevel@tonic-gate next = next->nxt; 6650Sstevel@tonic-gate if (statd_call_statd(nl->name) == 0) { 6660Sstevel@tonic-gate /* remove name from BACKUP */ 6670Sstevel@tonic-gate remove_name(nl->name, 1, 0); 6680Sstevel@tonic-gate mutex_lock(&recov_q.lock); 6690Sstevel@tonic-gate /* remove entry from recovery_q */ 6700Sstevel@tonic-gate delete_name(&recov_q.sm_recovhdp, nl->name); 6710Sstevel@tonic-gate mutex_unlock(&recov_q.lock); 6720Sstevel@tonic-gate } else { 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * Print message only once since unreachable 6750Sstevel@tonic-gate * host can be contacted forever. 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate if (delay == 0) 6780Sstevel@tonic-gate syslog(LOG_WARNING, 6790Sstevel@tonic-gate "statd: host %s is not responding\n", 6800Sstevel@tonic-gate nl->name); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate /* 6840Sstevel@tonic-gate * Increment the amount of delay before restarting again. 6850Sstevel@tonic-gate * The amount of delay should not exceed the MAX_DELAYTIME. 6860Sstevel@tonic-gate */ 6870Sstevel@tonic-gate if (delay <= MAX_DELAYTIME) 6880Sstevel@tonic-gate delay += INC_DELAYTIME; 6890Sstevel@tonic-gate mutex_lock(&crash_lock); 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate mutex_unlock(&crash_lock); 6930Sstevel@tonic-gate mutex_unlock(&sm_trylock); 6940Sstevel@tonic-gate out: 6950Sstevel@tonic-gate rw_unlock(&thr_rwlock); 6960Sstevel@tonic-gate if (debug) 6970Sstevel@tonic-gate (void) printf("EXITING sm_try\n"); 6980Sstevel@tonic-gate thr_exit((void *) 0); 6990Sstevel@tonic-gate #ifdef lint 7000Sstevel@tonic-gate return (0); 7010Sstevel@tonic-gate #endif 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * Malloc's space and returns the ptr to malloc'ed space. NULL if unsuccessful. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate char * 7080Sstevel@tonic-gate xmalloc(len) 7090Sstevel@tonic-gate unsigned len; 7100Sstevel@tonic-gate { 7110Sstevel@tonic-gate char *new; 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate if ((new = malloc(len)) == 0) { 7140Sstevel@tonic-gate syslog(LOG_ERR, "statd: malloc, error %m\n"); 7150Sstevel@tonic-gate return ((char *)NULL); 7160Sstevel@tonic-gate } else { 7170Sstevel@tonic-gate (void) memset(new, 0, len); 7180Sstevel@tonic-gate return (new); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate } 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate /* 7230Sstevel@tonic-gate * the following two routines are very similar to 7240Sstevel@tonic-gate * insert_mon and delete_mon in sm_proc.c, except the structture 7250Sstevel@tonic-gate * is different 7260Sstevel@tonic-gate */ 7270Sstevel@tonic-gate static name_entry * 7280Sstevel@tonic-gate insert_name(namepp, name, need_alloc) 7290Sstevel@tonic-gate name_entry **namepp; 7300Sstevel@tonic-gate char *name; 7310Sstevel@tonic-gate int need_alloc; 7320Sstevel@tonic-gate { 7330Sstevel@tonic-gate name_entry *new; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate new = (name_entry *)xmalloc(sizeof (name_entry)); 7360Sstevel@tonic-gate if (new == (name_entry *) NULL) 7370Sstevel@tonic-gate return (NULL); 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate /* Allocate name when needed which is only when adding to record_t */ 7400Sstevel@tonic-gate if (need_alloc) { 7410Sstevel@tonic-gate if ((new->name = strdup(name)) == (char *)NULL) { 7420Sstevel@tonic-gate syslog(LOG_ERR, "statd: strdup, error %m\n"); 7430Sstevel@tonic-gate free(new); 7440Sstevel@tonic-gate return (NULL); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate } else 7470Sstevel@tonic-gate new->name = name; 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate new->nxt = *namepp; 7500Sstevel@tonic-gate if (new->nxt != (name_entry *)NULL) 7510Sstevel@tonic-gate new->nxt->prev = new; 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate new->prev = (name_entry *) NULL; 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate *namepp = new; 7560Sstevel@tonic-gate if (debug) { 7570Sstevel@tonic-gate (void) printf("insert_name: inserted %s at %p\n", 7580Sstevel@tonic-gate name, (void *)namepp); 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate return (new); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * Deletes name from specified list (namepp). 7660Sstevel@tonic-gate */ 7670Sstevel@tonic-gate static void 7680Sstevel@tonic-gate delete_name(namepp, name) 7690Sstevel@tonic-gate name_entry **namepp; 7700Sstevel@tonic-gate char *name; 7710Sstevel@tonic-gate { 7720Sstevel@tonic-gate name_entry *nl; 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate nl = *namepp; 7750Sstevel@tonic-gate while (nl != (name_entry *)NULL) { 7760Sstevel@tonic-gate if (str_cmp_address_specifier(nl->name, name) == 0 || 7770Sstevel@tonic-gate str_cmp_unqual_hostname(nl->name, name) == 0) { 7780Sstevel@tonic-gate if (nl->prev != (name_entry *)NULL) 7790Sstevel@tonic-gate nl->prev->nxt = nl->nxt; 7800Sstevel@tonic-gate else 7810Sstevel@tonic-gate *namepp = nl->nxt; 7820Sstevel@tonic-gate if (nl->nxt != (name_entry *)NULL) 7830Sstevel@tonic-gate nl->nxt->prev = nl->prev; 7840Sstevel@tonic-gate free(nl->name); 7850Sstevel@tonic-gate free(nl); 7860Sstevel@tonic-gate return; 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate nl = nl->nxt; 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* 7930Sstevel@tonic-gate * Finds name from specified list (namep). 7940Sstevel@tonic-gate */ 7950Sstevel@tonic-gate static name_entry * 7960Sstevel@tonic-gate find_name(namep, name) 7970Sstevel@tonic-gate name_entry **namep; 7980Sstevel@tonic-gate char *name; 7990Sstevel@tonic-gate { 8000Sstevel@tonic-gate name_entry *nl; 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate nl = *namep; 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate while (nl != (name_entry *)NULL) { 8050Sstevel@tonic-gate if (str_cmp_unqual_hostname(nl->name, name) == 0) { 8060Sstevel@tonic-gate return (nl); 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate nl = nl->nxt; 8090Sstevel@tonic-gate } 8100Sstevel@tonic-gate return ((name_entry *)NULL); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate /* 8140Sstevel@tonic-gate * Creates a file. 8150Sstevel@tonic-gate */ 8160Sstevel@tonic-gate 8170Sstevel@tonic-gate int 8180Sstevel@tonic-gate create_file(name) 8190Sstevel@tonic-gate char *name; 8200Sstevel@tonic-gate { 8210Sstevel@tonic-gate int fd; 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate /* 8240Sstevel@tonic-gate * The file might already exist. If it does, we ask for only write 8250Sstevel@tonic-gate * permission, since that's all the file was created with. 8260Sstevel@tonic-gate */ 8270Sstevel@tonic-gate if ((fd = open(name, O_CREAT | O_WRONLY, S_IWUSR)) == -1) { 8280Sstevel@tonic-gate if (errno != EEXIST) { 8290Sstevel@tonic-gate syslog(LOG_ERR, "can't open %s: %m", name); 8300Sstevel@tonic-gate return (1); 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate if (debug >= 2) 8350Sstevel@tonic-gate (void) printf("%s is created\n", name); 8360Sstevel@tonic-gate if (close(fd)) { 8370Sstevel@tonic-gate syslog(LOG_ERR, "statd: close, error %m\n"); 8380Sstevel@tonic-gate return (1); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate return (0); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * Deletes the file specified by name. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate void 8480Sstevel@tonic-gate delete_file(name) 8490Sstevel@tonic-gate char *name; 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate if (debug >= 2) 8520Sstevel@tonic-gate (void) printf("Remove monitor entry %s\n", name); 8530Sstevel@tonic-gate if (unlink(name) == -1) { 8540Sstevel@tonic-gate if (errno != ENOENT) 8550Sstevel@tonic-gate syslog(LOG_ERR, "statd: unlink of %s, error %m", name); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * Return 1 if file is a symlink, else 0. 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate int 8630Sstevel@tonic-gate is_symlink(file) 8640Sstevel@tonic-gate char *file; 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate int error; 8670Sstevel@tonic-gate struct stat lbuf; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate do { 8700Sstevel@tonic-gate bzero((caddr_t)&lbuf, sizeof (lbuf)); 8710Sstevel@tonic-gate error = lstat(file, &lbuf); 8720Sstevel@tonic-gate } while (error == EINTR); 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate if (error == 0) { 8750Sstevel@tonic-gate return ((lbuf.st_mode & S_IFMT) == S_IFLNK); 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate return (0); 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate /* 8820Sstevel@tonic-gate * Moves the file specified by `from' to `to' only if the 8830Sstevel@tonic-gate * new file is guaranteed to be created (which is presumably 8840Sstevel@tonic-gate * why we don't just do a rename(2)). If `from' is a 8850Sstevel@tonic-gate * symlink, the destination file will be a similar symlink 8860Sstevel@tonic-gate * in the directory of `to'. 8870Sstevel@tonic-gate * 8880Sstevel@tonic-gate * Returns 0 for success, 1 for failure. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate static int 8910Sstevel@tonic-gate move_file(fromdir, file, todir) 8920Sstevel@tonic-gate char *fromdir; 8930Sstevel@tonic-gate char *file; 8940Sstevel@tonic-gate char *todir; 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate int n; 8970Sstevel@tonic-gate char rname[MAXNAMELEN + 1]; /* +1 for the terminating NULL */ 8980Sstevel@tonic-gate char from[MAXPATHLEN]; 8990Sstevel@tonic-gate char to[MAXPATHLEN]; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate (void) strcpy(from, fromdir); 9020Sstevel@tonic-gate (void) strcat(from, "/"); 9030Sstevel@tonic-gate (void) strcat(from, file); 9040Sstevel@tonic-gate if (is_symlink(from)) { 9050Sstevel@tonic-gate /* 9060Sstevel@tonic-gate * Dig out the name of the regular file the link points to. 9070Sstevel@tonic-gate */ 9080Sstevel@tonic-gate n = readlink(from, rname, MAXNAMELEN); 9090Sstevel@tonic-gate if (n <= 0) { 9100Sstevel@tonic-gate if (debug >= 2) { 9110Sstevel@tonic-gate (void) printf("move_file: can't read link %s\n", 9120Sstevel@tonic-gate from); 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate return (1); 9150Sstevel@tonic-gate } 9160Sstevel@tonic-gate rname[n] = '\0'; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate /* 9190Sstevel@tonic-gate * Create the link. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate if (create_symlink(todir, rname, file) != 0) { 9220Sstevel@tonic-gate return (1); 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate } else { 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * Do what we've always done to move regular files. 9270Sstevel@tonic-gate */ 9280Sstevel@tonic-gate (void) strcpy(to, todir); 9290Sstevel@tonic-gate (void) strcat(to, "/"); 9300Sstevel@tonic-gate (void) strcat(to, file); 9310Sstevel@tonic-gate if (create_file(to) != 0) { 9320Sstevel@tonic-gate return (1); 9330Sstevel@tonic-gate } 9340Sstevel@tonic-gate } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * Remove the old file if we've created the new one. 9380Sstevel@tonic-gate */ 9390Sstevel@tonic-gate if (unlink(from) < 0) { 9400Sstevel@tonic-gate syslog(LOG_ERR, "move_file: unlink of %s, error %m", from); 9410Sstevel@tonic-gate return (1); 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate return (0); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate /* 9480Sstevel@tonic-gate * Create a symbolic link named `lname' to regular file `rname'. 9490Sstevel@tonic-gate * Both files should be in directory `todir'. 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate int 9520Sstevel@tonic-gate create_symlink(todir, rname, lname) 9530Sstevel@tonic-gate char *todir; 9540Sstevel@tonic-gate char *rname; 9550Sstevel@tonic-gate char *lname; 9560Sstevel@tonic-gate { 9570Sstevel@tonic-gate int error; 9580Sstevel@tonic-gate char lpath[MAXPATHLEN]; 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate /* 9610Sstevel@tonic-gate * Form the full pathname of the link. 9620Sstevel@tonic-gate */ 9630Sstevel@tonic-gate (void) strcpy(lpath, todir); 9640Sstevel@tonic-gate (void) strcat(lpath, "/"); 9650Sstevel@tonic-gate (void) strcat(lpath, lname); 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate /* 9680Sstevel@tonic-gate * Now make the new symlink ... 9690Sstevel@tonic-gate */ 9700Sstevel@tonic-gate if (symlink(rname, lpath) < 0) { 9710Sstevel@tonic-gate error = errno; 9720Sstevel@tonic-gate if (error != 0 && error != EEXIST) { 9730Sstevel@tonic-gate if (debug >= 2) { 9740Sstevel@tonic-gate (void) printf( 9750Sstevel@tonic-gate "create_symlink: can't link %s/%s -> %s\n", 9760Sstevel@tonic-gate todir, lname, rname); 9770Sstevel@tonic-gate } 9780Sstevel@tonic-gate return (1); 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate } 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate if (debug) { 9830Sstevel@tonic-gate if (error == EEXIST) { 9840Sstevel@tonic-gate (void) printf("link %s/%s -> %s already exists\n", 9850Sstevel@tonic-gate todir, lname, rname); 9860Sstevel@tonic-gate } else { 9870Sstevel@tonic-gate (void) printf("created link %s/%s -> %s\n", 9880Sstevel@tonic-gate todir, lname, rname); 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate return (0); 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate /* 9960Sstevel@tonic-gate * remove the name from the specified directory 9970Sstevel@tonic-gate * op = 0: CURRENT 9980Sstevel@tonic-gate * op = 1: BACKUP 9990Sstevel@tonic-gate */ 10000Sstevel@tonic-gate static void 10010Sstevel@tonic-gate remove_name(char *name, int op, int startup) 10020Sstevel@tonic-gate { 10030Sstevel@tonic-gate int i; 10040Sstevel@tonic-gate char *alt_dir; 10050Sstevel@tonic-gate char *queue; 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (op == 0) { 10080Sstevel@tonic-gate alt_dir = "statmon/sm"; 10090Sstevel@tonic-gate queue = CURRENT; 10100Sstevel@tonic-gate } else { 10110Sstevel@tonic-gate alt_dir = "statmon/sm.bak"; 10120Sstevel@tonic-gate queue = BACKUP; 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate remove_single_name(name, queue, NULL); 10160Sstevel@tonic-gate /* 10170Sstevel@tonic-gate * At startup, entries have not yet been copied to alternate 10180Sstevel@tonic-gate * directories and thus do not need to be removed. 10190Sstevel@tonic-gate */ 10200Sstevel@tonic-gate if (startup == 0) { 10210Sstevel@tonic-gate for (i = 0; i < pathix; i++) { 10220Sstevel@tonic-gate remove_single_name(name, path_name[i], alt_dir); 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /* 10280Sstevel@tonic-gate * Remove the name from the specified directory, which is dir1/dir2 or 10290Sstevel@tonic-gate * dir1, depending on whether dir2 is NULL. 10300Sstevel@tonic-gate */ 10310Sstevel@tonic-gate static void 10320Sstevel@tonic-gate remove_single_name(char *name, char *dir1, char *dir2) 10330Sstevel@tonic-gate { 10340Sstevel@tonic-gate int n, error; 10350Sstevel@tonic-gate char path[MAXPATHLEN+MAXNAMELEN+SM_MAXPATHLEN]; /* why > MAXPATHLEN? */ 10360Sstevel@tonic-gate char dirpath[MAXPATHLEN]; 10370Sstevel@tonic-gate char rname[MAXNAMELEN + 1]; /* +1 for NULL term */ 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (strlen(name) + strlen(dir1) + (dir2 != NULL ? strlen(dir2) : 0) 10400Sstevel@tonic-gate + 3 > MAXPATHLEN) { 10410Sstevel@tonic-gate if (dir2 != NULL) 10420Sstevel@tonic-gate syslog(LOG_ERR, 10430Sstevel@tonic-gate "statd: pathname too long: %s/%s/%s\n", 10440Sstevel@tonic-gate dir1, dir2, name); 10450Sstevel@tonic-gate else 10460Sstevel@tonic-gate syslog(LOG_ERR, 10470Sstevel@tonic-gate "statd: pathname too long: %s/%s\n", 10480Sstevel@tonic-gate dir1, name); 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate return; 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate (void) strcpy(path, dir1); 10540Sstevel@tonic-gate (void) strcat(path, "/"); 10550Sstevel@tonic-gate if (dir2 != NULL) { 10560Sstevel@tonic-gate (void) strcat(path, dir2); 10570Sstevel@tonic-gate (void) strcat(path, "/"); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate (void) strcpy(dirpath, path); /* save here - we may need it shortly */ 10600Sstevel@tonic-gate (void) strcat(path, name); 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate /* 10630Sstevel@tonic-gate * Despite the name of this routine :-@), `path' may be a symlink 10640Sstevel@tonic-gate * to a regular file. If it is, and if that file has no other 10650Sstevel@tonic-gate * links to it, we must remove it now as well. 10660Sstevel@tonic-gate */ 10670Sstevel@tonic-gate if (is_symlink(path)) { 10680Sstevel@tonic-gate n = readlink(path, rname, MAXNAMELEN); 10690Sstevel@tonic-gate if (n > 0) { 10700Sstevel@tonic-gate rname[n] = '\0'; 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate if (count_symlinks(dirpath, rname, &n) < 0) { 10730Sstevel@tonic-gate return; 10740Sstevel@tonic-gate } 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate if (n == 1) { 10770Sstevel@tonic-gate (void) strcat(dirpath, rname); 10780Sstevel@tonic-gate error = unlink(dirpath); 10790Sstevel@tonic-gate if (debug >= 2) { 10800Sstevel@tonic-gate if (error < 0) { 10810Sstevel@tonic-gate (void) printf( 10820Sstevel@tonic-gate "remove_name: can't unlink %s\n", 10830Sstevel@tonic-gate dirpath); 10840Sstevel@tonic-gate } else { 10850Sstevel@tonic-gate (void) printf( 10860Sstevel@tonic-gate "remove_name: unlinked %s\n", 10870Sstevel@tonic-gate dirpath); 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate } 10900Sstevel@tonic-gate } 10910Sstevel@tonic-gate } else { 10920Sstevel@tonic-gate /* 10930Sstevel@tonic-gate * Policy: if we can't read the symlink, leave it 10940Sstevel@tonic-gate * here for analysis by the system administrator. 10950Sstevel@tonic-gate */ 10960Sstevel@tonic-gate syslog(LOG_ERR, 10970Sstevel@tonic-gate "statd: can't read link %s: %m\n", path); 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate /* 11020Sstevel@tonic-gate * If it's a regular file, we can assume all symlinks and the 11030Sstevel@tonic-gate * files to which they refer have been processed already - just 11040Sstevel@tonic-gate * fall through to here to remove it. 11050Sstevel@tonic-gate */ 11060Sstevel@tonic-gate delete_file(path); 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate /* 11100Sstevel@tonic-gate * Count the number of symlinks in `dir' which point to `name' (also in dir). 11110Sstevel@tonic-gate * Passes back symlink count in `count'. 11120Sstevel@tonic-gate * Returns 0 for success, < 0 for failure. 11130Sstevel@tonic-gate */ 11140Sstevel@tonic-gate static int 11150Sstevel@tonic-gate count_symlinks(char *dir, char *name, int *count) 11160Sstevel@tonic-gate { 11170Sstevel@tonic-gate int cnt = 0; 11180Sstevel@tonic-gate int n; 11190Sstevel@tonic-gate DIR *dp; 1120*871Scasper struct dirent *dirp; 11210Sstevel@tonic-gate char lpath[MAXPATHLEN]; 11220Sstevel@tonic-gate char rname[MAXNAMELEN + 1]; /* +1 for term NULL */ 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate if ((dp = opendir(dir)) == (DIR *)NULL) { 11250Sstevel@tonic-gate syslog(LOG_ERR, "count_symlinks: open %s dir, error %m\n", 11260Sstevel@tonic-gate dir); 11270Sstevel@tonic-gate return (-1); 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate 1130*871Scasper while ((dirp = readdir(dp)) != NULL) { 11310Sstevel@tonic-gate if (strcmp(dirp->d_name, ".") == 0 || 11320Sstevel@tonic-gate strcmp(dirp->d_name, "..") == 0) { 11330Sstevel@tonic-gate continue; 11340Sstevel@tonic-gate } 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate (void) sprintf(lpath, "%s%s", dir, dirp->d_name); 11370Sstevel@tonic-gate if (is_symlink(lpath)) { 11380Sstevel@tonic-gate /* 11390Sstevel@tonic-gate * Fetch the name of the file the symlink refers to. 11400Sstevel@tonic-gate */ 11410Sstevel@tonic-gate n = readlink(lpath, rname, MAXNAMELEN); 11420Sstevel@tonic-gate if (n <= 0) { 11430Sstevel@tonic-gate if (debug >= 2) { 11440Sstevel@tonic-gate (void) printf( 11450Sstevel@tonic-gate "count_symlinks: can't read link %s\n", 11460Sstevel@tonic-gate lpath); 11470Sstevel@tonic-gate } 11480Sstevel@tonic-gate continue; 11490Sstevel@tonic-gate } 11500Sstevel@tonic-gate rname[n] = '\0'; 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate /* 11530Sstevel@tonic-gate * If `rname' matches `name', bump the count. There 11540Sstevel@tonic-gate * may well be multiple symlinks to the same name, so 11550Sstevel@tonic-gate * we must continue to process the entire directory. 11560Sstevel@tonic-gate */ 11570Sstevel@tonic-gate if (strcmp(rname, name) == 0) { 11580Sstevel@tonic-gate cnt++; 11590Sstevel@tonic-gate } 11600Sstevel@tonic-gate } 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate (void) closedir(dp); 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate if (debug) { 11660Sstevel@tonic-gate (void) printf("count_symlinks: found %d symlinks\n", cnt); 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate *count = cnt; 11690Sstevel@tonic-gate return (0); 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate /* 11730Sstevel@tonic-gate * Manage the cache of hostnames. An entry for each host that has recently 11740Sstevel@tonic-gate * locked a file is kept. There is an in-ram table (rec_table) and an empty 11750Sstevel@tonic-gate * file in the file system name space (/var/statmon/sm/<name>). This 11760Sstevel@tonic-gate * routine adds (deletes) the name to (from) the in-ram table and the entry 11770Sstevel@tonic-gate * to (from) the file system name space. 11780Sstevel@tonic-gate * 11790Sstevel@tonic-gate * If op == 1 then the name is added to the queue otherwise the name is 11800Sstevel@tonic-gate * deleted. 11810Sstevel@tonic-gate */ 11820Sstevel@tonic-gate void 11830Sstevel@tonic-gate record_name(name, op) 11840Sstevel@tonic-gate char *name; 11850Sstevel@tonic-gate int op; 11860Sstevel@tonic-gate { 11870Sstevel@tonic-gate name_entry *nl; 11880Sstevel@tonic-gate int i; 11890Sstevel@tonic-gate char path[MAXPATHLEN+MAXNAMELEN+SM_MAXPATHLEN]; 11900Sstevel@tonic-gate name_entry **record_q; 11910Sstevel@tonic-gate unsigned int hash; 11920Sstevel@tonic-gate 11930Sstevel@tonic-gate /* 11940Sstevel@tonic-gate * These names are supposed to be just host names, not paths or 11950Sstevel@tonic-gate * other arbitrary files. 11960Sstevel@tonic-gate * manipulating the empty pathname unlinks CURRENT, 11970Sstevel@tonic-gate * manipulating files with '/' would allow you to create and unlink 11980Sstevel@tonic-gate * files all over the system; LOG_AUTH, it's a security thing. 11990Sstevel@tonic-gate * Don't remove the directories . and .. 12000Sstevel@tonic-gate */ 12010Sstevel@tonic-gate if (name == NULL) 12020Sstevel@tonic-gate return; 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate if (name[0] == '\0' || strchr(name, '/') != NULL || 12050Sstevel@tonic-gate strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { 12060Sstevel@tonic-gate syslog(LOG_ERR|LOG_AUTH, "statd: attempt to %s \"%s/%s\"", 12070Sstevel@tonic-gate op == 1 ? "create" : "remove", CURRENT, name); 12080Sstevel@tonic-gate return; 12090Sstevel@tonic-gate } 12100Sstevel@tonic-gate 12110Sstevel@tonic-gate SMHASH(name, hash); 12120Sstevel@tonic-gate if (debug) { 12130Sstevel@tonic-gate if (op == 1) 12140Sstevel@tonic-gate (void) printf("inserting %s at hash %d,\n", 12150Sstevel@tonic-gate name, hash); 12160Sstevel@tonic-gate else 12170Sstevel@tonic-gate (void) printf("deleting %s at hash %d\n", name, hash); 12180Sstevel@tonic-gate pr_name(name, 1); 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate if (op == 1) { /* insert */ 12230Sstevel@tonic-gate mutex_lock(&record_table[hash].lock); 12240Sstevel@tonic-gate record_q = &record_table[hash].sm_rechdp; 12250Sstevel@tonic-gate if ((nl = find_name(record_q, name)) == (name_entry *)NULL) { 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate int path_len; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate if ((nl = insert_name(record_q, name, 1)) != 12300Sstevel@tonic-gate (name_entry *) NULL) 12310Sstevel@tonic-gate nl->count++; 12320Sstevel@tonic-gate mutex_unlock(&record_table[hash].lock); 12330Sstevel@tonic-gate /* make an entry in current directory */ 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate path_len = strlen(CURRENT) + strlen(name) + 2; 12360Sstevel@tonic-gate if (path_len > MAXPATHLEN) { 12370Sstevel@tonic-gate syslog(LOG_ERR, 12380Sstevel@tonic-gate "statd: pathname too long: %s/%s\n", 12390Sstevel@tonic-gate CURRENT, name); 12400Sstevel@tonic-gate return; 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate (void) strcpy(path, CURRENT); 12430Sstevel@tonic-gate (void) strcat(path, "/"); 12440Sstevel@tonic-gate (void) strcat(path, name); 12450Sstevel@tonic-gate (void) create_file(path); 12460Sstevel@tonic-gate if (debug) { 12470Sstevel@tonic-gate (void) printf("After insert_name\n"); 12480Sstevel@tonic-gate pr_name(name, 1); 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate /* make an entry in alternate paths */ 12510Sstevel@tonic-gate for (i = 0; i < pathix; i++) { 12520Sstevel@tonic-gate path_len = strlen(path_name[i]) + 12530Sstevel@tonic-gate strlen("/statmon/sm/") + 12540Sstevel@tonic-gate strlen(name) + 1; 12550Sstevel@tonic-gate 12560Sstevel@tonic-gate if (path_len > MAXPATHLEN) { 12570Sstevel@tonic-gate syslog(LOG_ERR, 12580Sstevel@tonic-gate "statd: pathname too long: %s/statmon/sm/%s\n", 12590Sstevel@tonic-gate path_name[i], name); 12600Sstevel@tonic-gate continue; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate (void) strcpy(path, path_name[i]); 12630Sstevel@tonic-gate (void) strcat(path, "/statmon/sm/"); 12640Sstevel@tonic-gate (void) strcat(path, name); 12650Sstevel@tonic-gate (void) create_file(path); 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate return; 12680Sstevel@tonic-gate } 12690Sstevel@tonic-gate nl->count++; 12700Sstevel@tonic-gate mutex_unlock(&record_table[hash].lock); 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate } else { /* delete */ 12730Sstevel@tonic-gate mutex_lock(&record_table[hash].lock); 12740Sstevel@tonic-gate record_q = &record_table[hash].sm_rechdp; 12750Sstevel@tonic-gate if ((nl = find_name(record_q, name)) == (name_entry *)NULL) { 12760Sstevel@tonic-gate mutex_unlock(&record_table[hash].lock); 12770Sstevel@tonic-gate return; 12780Sstevel@tonic-gate } 12790Sstevel@tonic-gate nl->count--; 12800Sstevel@tonic-gate if (nl->count == 0) { 12810Sstevel@tonic-gate delete_name(record_q, name); 12820Sstevel@tonic-gate mutex_unlock(&record_table[hash].lock); 12830Sstevel@tonic-gate /* remove this entry from current directory */ 12840Sstevel@tonic-gate remove_name(name, 0, 0); 12850Sstevel@tonic-gate } else 12860Sstevel@tonic-gate mutex_unlock(&record_table[hash].lock); 12870Sstevel@tonic-gate if (debug) { 12880Sstevel@tonic-gate (void) printf("After delete_name \n"); 12890Sstevel@tonic-gate pr_name(name, 1); 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate } 12920Sstevel@tonic-gate } 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate /* 12950Sstevel@tonic-gate * This routine adds a symlink in the form of an ASCII dotted quad 12960Sstevel@tonic-gate * IP address that is linked to the name already recorded in the 12970Sstevel@tonic-gate * filesystem name space by record_name(). Enough information is 12980Sstevel@tonic-gate * (hopefully) provided to support other address types in the future. 12990Sstevel@tonic-gate * The purpose of this is to cache enough information to contact 13000Sstevel@tonic-gate * hosts in other domains during server crash recovery (see bugid 13010Sstevel@tonic-gate * 1184192). 13020Sstevel@tonic-gate * 13030Sstevel@tonic-gate * The worst failure mode here is that the symlink is not made, and 13040Sstevel@tonic-gate * statd falls back to the old buggy behavior. 13050Sstevel@tonic-gate */ 13060Sstevel@tonic-gate void 13070Sstevel@tonic-gate record_addr(char *name, sa_family_t family, struct netobj *ah) 13080Sstevel@tonic-gate { 13090Sstevel@tonic-gate int i; 13100Sstevel@tonic-gate int path_len; 13110Sstevel@tonic-gate char *famstr; 13120Sstevel@tonic-gate struct in_addr addr; 13130Sstevel@tonic-gate char *addr6; 13140Sstevel@tonic-gate char ascii_addr[MAXNAMELEN]; 13150Sstevel@tonic-gate char path[MAXPATHLEN]; 13160Sstevel@tonic-gate 13170Sstevel@tonic-gate if (family == AF_INET) { 13180Sstevel@tonic-gate if (ah->n_len != sizeof (struct in_addr)) 13190Sstevel@tonic-gate return; 13200Sstevel@tonic-gate addr = *(struct in_addr *)ah->n_bytes; 13210Sstevel@tonic-gate } else if (family == AF_INET6) { 13220Sstevel@tonic-gate if (ah->n_len != sizeof (struct in6_addr)) 13230Sstevel@tonic-gate return; 13240Sstevel@tonic-gate addr6 = (char *)ah->n_bytes; 13250Sstevel@tonic-gate } else 13260Sstevel@tonic-gate return; 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate if (debug) { 13290Sstevel@tonic-gate if (family == AF_INET) 13300Sstevel@tonic-gate (void) printf("record_addr: addr= %x\n", addr.s_addr); 13310Sstevel@tonic-gate else if (family == AF_INET6) 13320Sstevel@tonic-gate (void) printf("record_addr: addr= %x\n", \ 13330Sstevel@tonic-gate ((struct in6_addr *)addr6)->s6_addr); 13340Sstevel@tonic-gate } 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate if (family == AF_INET) { 13370Sstevel@tonic-gate if (addr.s_addr == INADDR_ANY || 13380Sstevel@tonic-gate ((addr.s_addr && 0xff000000) == 0) || 13390Sstevel@tonic-gate IN_BADCLASS(addr.s_addr)) { 13400Sstevel@tonic-gate syslog(LOG_DEBUG, 13410Sstevel@tonic-gate "record_addr: illegal IP address %x\n", 13420Sstevel@tonic-gate addr.s_addr); 13430Sstevel@tonic-gate return; 13440Sstevel@tonic-gate } 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate /* convert address to ASCII */ 13480Sstevel@tonic-gate famstr = family2string(family); 13490Sstevel@tonic-gate if (famstr == NULL) { 13500Sstevel@tonic-gate syslog(LOG_DEBUG, 13510Sstevel@tonic-gate "record_addr: unsupported address family %d\n", 13520Sstevel@tonic-gate family); 13530Sstevel@tonic-gate return; 13540Sstevel@tonic-gate } 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate switch (family) { 13570Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 13580Sstevel@tonic-gate case AF_INET: 13590Sstevel@tonic-gate (void) sprintf(ascii_addr, "%s.%s", famstr, inet_ntoa(addr)); 13600Sstevel@tonic-gate break; 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate case AF_INET6: 13630Sstevel@tonic-gate (void) sprintf(ascii_addr, "%s.%s", famstr,\ 13640Sstevel@tonic-gate inet_ntop(family, addr6, abuf, sizeof (abuf))); 13650Sstevel@tonic-gate break; 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate default: 13680Sstevel@tonic-gate if (debug) { 13690Sstevel@tonic-gate (void) printf( 13700Sstevel@tonic-gate "record_addr: family2string supports unknown family %d (%s)\n", 13710Sstevel@tonic-gate family, 13720Sstevel@tonic-gate famstr); 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate free(famstr); 13750Sstevel@tonic-gate return; 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate if (debug) { 13790Sstevel@tonic-gate (void) printf("record_addr: ascii_addr= %s\n", ascii_addr); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate free(famstr); 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate /* 13840Sstevel@tonic-gate * Make the symlink in CURRENT. The `name' file should have 13850Sstevel@tonic-gate * been created previously by record_name(). 13860Sstevel@tonic-gate */ 13870Sstevel@tonic-gate (void) create_symlink(CURRENT, name, ascii_addr); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate /* 13900Sstevel@tonic-gate * Similarly for alternate paths. 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate for (i = 0; i < pathix; i++) { 13930Sstevel@tonic-gate path_len = strlen(path_name[i]) + 13940Sstevel@tonic-gate strlen("/statmon/sm/") + 13950Sstevel@tonic-gate strlen(name) + 1; 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate if (path_len > MAXPATHLEN) { 13980Sstevel@tonic-gate syslog(LOG_ERR, 13990Sstevel@tonic-gate "statd: pathname too long: %s/statmon/sm/%s\n", 14000Sstevel@tonic-gate path_name[i], name); 14010Sstevel@tonic-gate continue; 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate (void) strcpy(path, path_name[i]); 14040Sstevel@tonic-gate (void) strcat(path, "/statmon/sm"); 14050Sstevel@tonic-gate (void) create_symlink(path, name, ascii_addr); 14060Sstevel@tonic-gate } 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate /* 14100Sstevel@tonic-gate * SM_CRASH - simulate a crash of statd. 14110Sstevel@tonic-gate */ 14120Sstevel@tonic-gate void 14130Sstevel@tonic-gate sm_crash() 14140Sstevel@tonic-gate { 14150Sstevel@tonic-gate name_entry *nl, *next; 14160Sstevel@tonic-gate mon_entry *nl_monp, *mon_next; 14170Sstevel@tonic-gate int k; 14180Sstevel@tonic-gate my_id *nl_idp; 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate for (k = 0; k < MAX_HASHSIZE; k++) { 14210Sstevel@tonic-gate mutex_lock(&mon_table[k].lock); 14220Sstevel@tonic-gate if ((mon_next = mon_table[k].sm_monhdp) == 14230Sstevel@tonic-gate (mon_entry *) NULL) { 14240Sstevel@tonic-gate mutex_unlock(&mon_table[k].lock); 14250Sstevel@tonic-gate continue; 14260Sstevel@tonic-gate } else { 14270Sstevel@tonic-gate while ((nl_monp = mon_next) != (mon_entry *)NULL) { 14280Sstevel@tonic-gate mon_next = mon_next->nxt; 14290Sstevel@tonic-gate nl_idp = &nl_monp->id.mon_id.my_id; 14300Sstevel@tonic-gate free(nl_monp->id.mon_id.mon_name); 14310Sstevel@tonic-gate free(nl_idp->my_name); 14320Sstevel@tonic-gate free(nl_monp); 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate mon_table[k].sm_monhdp = (mon_entry *)NULL; 14350Sstevel@tonic-gate } 14360Sstevel@tonic-gate mutex_unlock(&mon_table[k].lock); 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate /* Clean up entries in record table */ 14400Sstevel@tonic-gate for (k = 0; k < MAX_HASHSIZE; k++) { 14410Sstevel@tonic-gate mutex_lock(&record_table[k].lock); 14420Sstevel@tonic-gate if ((next = record_table[k].sm_rechdp) == 14430Sstevel@tonic-gate (name_entry *) NULL) { 14440Sstevel@tonic-gate mutex_unlock(&record_table[k].lock); 14450Sstevel@tonic-gate continue; 14460Sstevel@tonic-gate } else { 14470Sstevel@tonic-gate while ((nl = next) != (name_entry *)NULL) { 14480Sstevel@tonic-gate next = next->nxt; 14490Sstevel@tonic-gate free(nl->name); 14500Sstevel@tonic-gate free(nl); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate record_table[k].sm_rechdp = (name_entry *)NULL; 14530Sstevel@tonic-gate } 14540Sstevel@tonic-gate mutex_unlock(&record_table[k].lock); 14550Sstevel@tonic-gate } 14560Sstevel@tonic-gate 14570Sstevel@tonic-gate /* Clean up entries in recovery table */ 14580Sstevel@tonic-gate mutex_lock(&recov_q.lock); 14590Sstevel@tonic-gate if ((next = recov_q.sm_recovhdp) != (name_entry *)NULL) { 14600Sstevel@tonic-gate while ((nl = next) != (name_entry *)NULL) { 14610Sstevel@tonic-gate next = next->nxt; 14620Sstevel@tonic-gate free(nl->name); 14630Sstevel@tonic-gate free(nl); 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate recov_q.sm_recovhdp = (name_entry *)NULL; 14660Sstevel@tonic-gate } 14670Sstevel@tonic-gate mutex_unlock(&recov_q.lock); 14680Sstevel@tonic-gate statd_init(); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate /* 14720Sstevel@tonic-gate * Initialize the hash tables: mon_table, record_table, recov_q and 14730Sstevel@tonic-gate * locks. 14740Sstevel@tonic-gate */ 14750Sstevel@tonic-gate void 14760Sstevel@tonic-gate sm_inithash() 14770Sstevel@tonic-gate { 14780Sstevel@tonic-gate int k; 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate if (debug) 14810Sstevel@tonic-gate (void) printf("Initializing hash tables\n"); 14820Sstevel@tonic-gate for (k = 0; k < MAX_HASHSIZE; k++) { 14830Sstevel@tonic-gate mon_table[k].sm_monhdp = (mon_entry *)NULL; 14840Sstevel@tonic-gate record_table[k].sm_rechdp = (name_entry *)NULL; 14850Sstevel@tonic-gate mutex_init(&mon_table[k].lock, USYNC_THREAD, NULL); 14860Sstevel@tonic-gate mutex_init(&record_table[k].lock, USYNC_THREAD, NULL); 14870Sstevel@tonic-gate } 14880Sstevel@tonic-gate mutex_init(&recov_q.lock, USYNC_THREAD, NULL); 14890Sstevel@tonic-gate recov_q.sm_recovhdp = (name_entry *)NULL; 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate /* 14940Sstevel@tonic-gate * Maps a socket address family to a name string, or NULL if the family 14950Sstevel@tonic-gate * is not supported by statd. 14960Sstevel@tonic-gate * Caller is responsible for freeing storage used by result string, if any. 14970Sstevel@tonic-gate */ 14980Sstevel@tonic-gate static char * 14990Sstevel@tonic-gate family2string(sa_family_t family) 15000Sstevel@tonic-gate { 15010Sstevel@tonic-gate char *rc; 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate switch (family) { 15040Sstevel@tonic-gate case AF_INET: 15050Sstevel@tonic-gate rc = strdup(SM_ADDR_IPV4); 15060Sstevel@tonic-gate break; 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate case AF_INET6: 15090Sstevel@tonic-gate rc = strdup(SM_ADDR_IPV6); 15100Sstevel@tonic-gate break; 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate default: 15130Sstevel@tonic-gate rc = NULL; 15140Sstevel@tonic-gate break; 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate return (rc); 15180Sstevel@tonic-gate } 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate /* 15210Sstevel@tonic-gate * Prints out list in record_table if flag is 1 otherwise 15220Sstevel@tonic-gate * prints out each list in recov_q specified by name. 15230Sstevel@tonic-gate */ 15240Sstevel@tonic-gate static void 15250Sstevel@tonic-gate pr_name(name, flag) 15260Sstevel@tonic-gate char *name; 15270Sstevel@tonic-gate int flag; 15280Sstevel@tonic-gate { 15290Sstevel@tonic-gate name_entry *nl; 15300Sstevel@tonic-gate unsigned int hash; 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate if (!debug) 15330Sstevel@tonic-gate return; 15340Sstevel@tonic-gate if (flag) { 15350Sstevel@tonic-gate SMHASH(name, hash); 15360Sstevel@tonic-gate (void) printf("*****record_q: "); 15370Sstevel@tonic-gate mutex_lock(&record_table[hash].lock); 15380Sstevel@tonic-gate nl = record_table[hash].sm_rechdp; 15390Sstevel@tonic-gate while (nl != (name_entry *)NULL) { 15400Sstevel@tonic-gate (void) printf("(%x), ", (int)nl); 15410Sstevel@tonic-gate nl = nl->nxt; 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate mutex_unlock(&record_table[hash].lock); 15440Sstevel@tonic-gate } else { 15450Sstevel@tonic-gate (void) printf("*****recovery_q: "); 15460Sstevel@tonic-gate mutex_lock(&recov_q.lock); 15470Sstevel@tonic-gate nl = recov_q.sm_recovhdp; 15480Sstevel@tonic-gate while (nl != (name_entry *)NULL) { 15490Sstevel@tonic-gate (void) printf("(%x), ", (int)nl); 15500Sstevel@tonic-gate nl = nl->nxt; 15510Sstevel@tonic-gate } 15520Sstevel@tonic-gate mutex_unlock(&recov_q.lock); 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate } 15550Sstevel@tonic-gate (void) printf("\n"); 15560Sstevel@tonic-gate } 1557