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