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