From 98573c1925f3692d1e8ea9eb018cb915fc0becc5 Mon Sep 17 00:00:00 2001 From: Marcel Telka Date: Fri, 27 Mar 2015 15:18:01 +0100 Subject: [PATCH] 4518 lockd: Cannot establish NLM service over Reviewed by: Gordon Ross Reviewed by: Dan McDonald Approved by: Dan McDonald --- usr/src/cmd/fs.d/nfs/statd/Makefile | 16 ++- usr/src/cmd/fs.d/nfs/statd/sm_proc.c | 220 ++++++++++++++++------------------ usr/src/cmd/fs.d/nfs/statd/sm_statd.c | 217 ++++++++++++++++----------------- usr/src/cmd/fs.d/nfs/statd/sm_statd.h | 20 +++- usr/src/cmd/fs.d/nfs/statd/sm_svc.c | 135 ++++++++++++--------- 5 files changed, 308 insertions(+), 300 deletions(-) diff --git a/usr/src/cmd/fs.d/nfs/statd/Makefile b/usr/src/cmd/fs.d/nfs/statd/Makefile index 1e55770f83..8a74b8860b 100644 --- a/usr/src/cmd/fs.d/nfs/statd/Makefile +++ b/usr/src/cmd/fs.d/nfs/statd/Makefile @@ -19,6 +19,11 @@ # # CDDL HEADER END # + +# +# Copyright 2015 Nexenta Systems, Inc. All rights reserved. +# + # # Copyright 1990-2003 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. @@ -35,9 +40,10 @@ CERRWARN += -_gcc=-Wno-unused-variable CERRWARN += -_gcc=-Wno-parentheses CERRWARN += -_gcc=-Wno-uninitialized -OBJS= sm_svc.o sm_proc.o sm_statd.o selfcheck.o +LOCAL= sm_svc.o sm_proc.o sm_statd.o +OBJS= $(LOCAL) selfcheck.o daemon.o -SRCS= sm_svc.c sm_proc.c sm_statd.c ../lib/selfcheck.c +SRCS= $(LOCAL:%.o=%.c) ../lib/selfcheck.c ../lib/daemon.c LDLIBS += -lsocket -lrpcsvc -lnsl @@ -49,10 +55,10 @@ $(TYPEPROG): $(OBJS) selfcheck.o: ../lib/selfcheck.c $(COMPILE.c) ../lib/selfcheck.c +daemon.o: ../lib/daemon.c + $(COMPILE.c) ../lib/daemon.c + lint: lint_SRCS clean: $(RM) $(OBJS) $(TYPEPROG) - - - diff --git a/usr/src/cmd/fs.d/nfs/statd/sm_proc.c b/usr/src/cmd/fs.d/nfs/statd/sm_proc.c index 22592eb95d..00d8d59f89 100644 --- a/usr/src/cmd/fs.d/nfs/statd/sm_proc.c +++ b/usr/src/cmd/fs.d/nfs/statd/sm_proc.c @@ -19,12 +19,16 @@ * * CDDL HEADER END */ + +/* + * Copyright 2015 Nexenta Systems, Inc. All rights reserved. + */ + /* * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ /* - * Copyright 2013 Nexenta Systems, Inc. All rights reserved. * Copyright (c) 2012 by Delphix. All rights reserved. */ @@ -113,7 +117,7 @@ sm_mon_svc(mon *monp, sm_stat_res *resp) rw_rdlock(&thr_rwlock); if (debug) { (void) printf("proc sm_mon: mon_name = %s, id = %d\n", - monidp->mon_name, * ((int *)monp->priv)); + monidp->mon_name, *((int *)monp->priv)); pr_mon(monp->mon_id.mon_name); } @@ -159,7 +163,7 @@ sm_unmon_all_svc(my_id *myidp, sm_stat *resp) myidp->my_name, myidp->my_prog, myidp->my_vers, myidp->my_proc); - delete_mon((char *)NULL, myidp); + delete_mon(NULL, myidp); pr_mon(NULL); resp->state = local_state; rw_unlock(&thr_rwlock); @@ -187,23 +191,22 @@ sm_simu_crash_svc(void *myidp) struct mon_entry *monitor_q; int found = 0; - /* Only one crash should be running at a time. */ - mutex_lock(&crash_lock); if (debug) (void) printf("proc sm_simu_crash\n"); - if (in_crash) { - cond_wait(&crash_finish, &crash_lock); + + /* Only one crash should be running at a time. */ + mutex_lock(&crash_lock); + if (in_crash != 0) { mutex_unlock(&crash_lock); return; - } else { - in_crash = 1; } + in_crash = 1; mutex_unlock(&crash_lock); for (i = 0; i < MAX_HASHSIZE; i++) { mutex_lock(&mon_table[i].lock); monitor_q = mon_table[i].sm_monhdp; - if (monitor_q != (struct mon_entry *)NULL) { + if (monitor_q != NULL) { mutex_unlock(&mon_table[i].lock); found = 1; break; @@ -217,7 +220,7 @@ sm_simu_crash_svc(void *myidp) if (found) { mutex_lock(&crash_lock); die = 1; - /* Signal sm_retry() thread if sleeping. */ + /* Signal sm_try() thread if sleeping. */ cond_signal(&retrywait); mutex_unlock(&crash_lock); rw_wrlock(&thr_rwlock); @@ -232,9 +235,7 @@ sm_simu_crash_svc(void *myidp) /* ARGSUSED */ void -nsmaddrproc1_reg(regargs, regresp) - reg1args *regargs; - reg1res *regresp; +nsmaddrproc1_reg(reg1args *regargs, reg1res *regresp) { nsm_addr_res status; name_addr_entry_t *entry; @@ -246,13 +247,11 @@ nsmaddrproc1_reg(regargs, regresp) int i; (void) printf("nap1_reg: fam= %d, name= %s, len= %d\n", - regargs->family, - regargs->name, - regargs->address.n_len); + regargs->family, regargs->name, regargs->address.n_len); (void) printf("address is: "); for (i = 0; i < regargs->address.n_len; i++) { (void) printf("%d.", - (unsigned char)regargs->address.n_bytes[i]); + (unsigned char)regargs->address.n_bytes[i]); } (void) printf("\n"); } @@ -267,7 +266,7 @@ nsmaddrproc1_reg(regargs, regresp) if (strcmp(regargs->name, entry->name) == 0) { if (debug) { (void) printf("nap1_reg: matched name %s\n", - entry->name); + entry->name); } break; } @@ -315,18 +314,17 @@ nsmaddrproc1_reg(regargs, regresp) if (addr->family == (sa_family_t)regargs->family && addr->ah.n_len == regargs->address.n_len && memcmp(addr->ah.n_bytes, regargs->address.n_bytes, - addr->ah.n_len) == 0) { + addr->ah.n_len) == 0) { if (debug) { int i; (void) printf("nap1_reg: matched addr "); for (i = 0; i < addr->ah.n_len; i++) { (void) printf("%d.", - (unsigned char)addr->ah.n_bytes[i]); + (unsigned char)addr->ah.n_bytes[i]); } (void) printf(" family %d for name %s\n", - addr->family, - entry->name); + addr->family, entry->name); } break; } @@ -337,8 +335,7 @@ nsmaddrproc1_reg(regargs, regresp) tmp_n_bytes = (char *)malloc(regargs->address.n_len); if (addr == NULL || tmp_n_bytes == NULL) { if (debug) { - (void) printf( - "nap1_reg: no memory for addr\n"); + (void) printf("nap1_reg: no memory for addr\n"); } /* @@ -371,15 +368,15 @@ nsmaddrproc1_reg(regargs, regresp) addr->ah.n_bytes = tmp_n_bytes; addr->family = regargs->family; if (debug) { - if ((addr->family != AF_INET) && \ - (addr->family != AF_INET6)) { + if ((addr->family != AF_INET) && + (addr->family != AF_INET6)) { (void) printf( - "nap1_reg: unknown addr family %d\n", - addr->family); + "nap1_reg: unknown addr family %d\n", + addr->family); } } (void) memcpy(addr->ah.n_bytes, regargs->address.n_bytes, - addr->ah.n_len); + addr->ah.n_len); addr->next = entry->addresses; entry->addresses = addr; @@ -401,8 +398,7 @@ done: * here. It is then filled in from the information passed in. */ static void -insert_mon(monp) - mon *monp; +insert_mon(mon *monp) { mon_entry *new, *found; my_id *my_idp, *nl_idp; @@ -414,8 +410,8 @@ insert_mon(monp) /* Allocate entry for new */ if ((new = (mon_entry *) malloc(sizeof (mon_entry))) == 0) { syslog(LOG_ERR, - "statd: insert_mon: malloc error on mon %s (id=%d)\n", - monp->mon_id.mon_name, * ((int *)monp->priv)); + "statd: insert_mon: malloc error on mon %s (id=%d)\n", + monp->mon_id.mon_name, *((int *)monp->priv)); return; } @@ -426,8 +422,8 @@ insert_mon(monp) /* Allocate entry for new mon_name */ if ((new->id.mon_id.mon_name = strdup(monp->mon_id.mon_name)) == 0) { syslog(LOG_ERR, - "statd: insert_mon: malloc error on mon %s (id=%d)\n", - monp->mon_id.mon_name, * ((int *)monp->priv)); + "statd: insert_mon: malloc error on mon %s (id=%d)\n", + monp->mon_id.mon_name, *((int *)monp->priv)); free(new); return; } @@ -435,10 +431,10 @@ insert_mon(monp) /* Allocate entry for new my_name */ if ((new->id.mon_id.my_id.my_name = - strdup(monp->mon_id.my_id.my_name)) == 0) { + strdup(monp->mon_id.my_id.my_name)) == 0) { syslog(LOG_ERR, - "statd: insert_mon: malloc error on mon %s (id=%d)\n", - monp->mon_id.mon_name, * ((int *)monp->priv)); + "statd: insert_mon: malloc error on mon %s (id=%d)\n", + monp->mon_id.mon_name, *((int *)monp->priv)); free(new->id.mon_id.mon_name); free(new); return; @@ -446,7 +442,7 @@ insert_mon(monp) if (debug) (void) printf("add_mon(%x) %s (id=%d)\n", - (int)new, new->id.mon_id.mon_name, * ((int *)new->id.priv)); + (int)new, new->id.mon_id.mon_name, *((int *)new->id.priv)); /* * Record the name, and all addresses which have been registered @@ -462,7 +458,7 @@ insert_mon(monp) for (addr = entry->addresses; addr; addr = addr->next) { record_addr(new->id.mon_id.mon_name, - addr->family, &addr->ah); + addr->family, &addr->ah); } break; } @@ -474,17 +470,17 @@ insert_mon(monp) monitor_q = mon_table[hash].sm_monhdp; /* If mon_table hash list is empty. */ - if (monitor_q == (struct mon_entry *)NULL) { + if (monitor_q == NULL) { if (debug) (void) printf("\nAdding to monitor_q hash %d\n", hash); - new->nxt = new->prev = (mon_entry *)NULL; + new->nxt = new->prev = NULL; mon_table[hash].sm_monhdp = new; mutex_unlock(&mon_table[hash].lock); return; } else { found = 0; my_idp = &new->id.mon_id.my_id; - while (monitor_q != (mon_entry *)NULL) { + while (monitor_q != NULL) { /* * This list is searched sequentially for the * tuple (hostname, prog, vers, proc). The tuples @@ -499,15 +495,15 @@ insert_mon(monp) */ if (str_cmp_unqual_hostname( - monitor_q->id.mon_id.mon_name, - new->id.mon_id.mon_name) == 0) { + monitor_q->id.mon_id.mon_name, + new->id.mon_id.mon_name) == 0) { /* found */ nl_idp = &monitor_q->id.mon_id.my_id; if ((str_cmp_unqual_hostname(my_idp->my_name, - nl_idp->my_name) == 0) && - my_idp->my_prog == nl_idp->my_prog && - my_idp->my_vers == nl_idp->my_vers && - my_idp->my_proc == nl_idp->my_proc) { + nl_idp->my_name) == 0) && + my_idp->my_prog == nl_idp->my_prog && + my_idp->my_vers == nl_idp->my_vers && + my_idp->my_proc == nl_idp->my_proc) { /* * already exists an identical one, * release the space allocated for the @@ -536,7 +532,7 @@ insert_mon(monp) */ new->nxt = found->nxt; new->prev = found; - if (found->nxt != (mon_entry *)NULL) + if (found->nxt != NULL) found->nxt->prev = new; found->nxt = new; } else { @@ -559,13 +555,11 @@ insert_mon(monp) * in hash table. */ static void -delete_mon(mon_name, my_idp) - char *mon_name; - my_id *my_idp; +delete_mon(char *mon_name, my_id *my_idp) { unsigned int hash; - if (mon_name != (char *)NULL) { + if (mon_name != NULL) { record_name(mon_name, 0); SMHASH(mon_name, hash); mutex_lock(&mon_table[hash].lock); @@ -575,7 +569,7 @@ delete_mon(mon_name, my_idp) for (hash = 0; hash < MAX_HASHSIZE; hash++) { mutex_lock(&mon_table[hash].lock); delete_onemon(mon_name, my_idp, - &mon_table[hash].sm_monhdp); + &mon_table[hash].sm_monhdp); mutex_unlock(&mon_table[hash].lock); } } @@ -587,44 +581,41 @@ delete_mon(mon_name, my_idp) * else delete specific monitor. */ void -delete_onemon(mon_name, my_idp, monitor_q) - char *mon_name; - my_id *my_idp; - mon_entry **monitor_q; +delete_onemon(char *mon_name, my_id *my_idp, mon_entry **monitor_q) { mon_entry *next, *nl; my_id *nl_idp; next = *monitor_q; - while ((nl = next) != (struct mon_entry *)NULL) { + while ((nl = next) != NULL) { next = next->nxt; - if (mon_name == (char *)NULL || (mon_name != (char *)NULL && - str_cmp_unqual_hostname(nl->id.mon_id.mon_name, - mon_name) == 0)) { + if (mon_name == NULL || (mon_name != NULL && + str_cmp_unqual_hostname(nl->id.mon_id.mon_name, + mon_name) == 0)) { nl_idp = &nl->id.mon_id.my_id; if ((str_cmp_unqual_hostname(my_idp->my_name, - nl_idp->my_name) == 0) && - my_idp->my_prog == nl_idp->my_prog && - my_idp->my_vers == nl_idp->my_vers && - my_idp->my_proc == nl_idp->my_proc) { + nl_idp->my_name) == 0) && + my_idp->my_prog == nl_idp->my_prog && + my_idp->my_vers == nl_idp->my_vers && + my_idp->my_proc == nl_idp->my_proc) { /* found */ if (debug) (void) printf("delete_mon(%x): %s\n", - (int)nl, mon_name ? - mon_name : ""); + (int)nl, mon_name ? + mon_name : ""); /* * Remove the monitor name from the * record_q, if id matches. */ record_name(nl->id.mon_id.mon_name, 0); /* if nl is not the first entry on list */ - if (nl->prev != (struct mon_entry *)NULL) + if (nl->prev != NULL) nl->prev->nxt = nl->nxt; else { *monitor_q = nl->nxt; } - if (nl->nxt != (struct mon_entry *)NULL) + if (nl->nxt != NULL) nl->nxt->prev = nl->prev; free(nl->id.mon_id.mon_name); free(nl_idp->my_name); @@ -639,9 +630,7 @@ delete_onemon(mon_name, my_idp, monitor_q) * has changed. */ static void -send_notice(mon_name, state) - char *mon_name; - int state; +send_notice(char *mon_name, int state) { struct mon_entry *next; mon_entry *monitor_q; @@ -654,7 +643,7 @@ send_notice(mon_name, state) monitor_q = mon_table[hash].sm_monhdp; next = monitor_q; - while (next != (struct mon_entry *)NULL) { + while (next != NULL) { if (hostname_eq(next->id.mon_id.mon_name, mon_name)) { monp = &next->id; /* @@ -663,26 +652,25 @@ send_notice(mon_name, state) * mon info and state. */ if ((minfop = - (moninfo_t *)xmalloc(sizeof (moninfo_t))) != - (moninfo_t *)NULL) { + (moninfo_t *)xmalloc(sizeof (moninfo_t))) != NULL) { (void) memcpy(&minfop->id, monp, sizeof (mon)); /* Allocate entry for mon_name */ if ((minfop->id.mon_id.mon_name = - strdup(monp->mon_id.mon_name)) == 0) { - syslog(LOG_ERR, - "statd: send_notice: malloc error on mon %s (id=%d)\n", - monp->mon_id.mon_name, - * ((int *)monp->priv)); + strdup(monp->mon_id.mon_name)) == 0) { + syslog(LOG_ERR, "statd: send_notice: " + "malloc error on mon %s (id=%d)\n", + monp->mon_id.mon_name, + *((int *)monp->priv)); free(minfop); continue; } /* Allocate entry for my_name */ if ((minfop->id.mon_id.my_id.my_name = - strdup(monp->mon_id.my_id.my_name)) == 0) { - syslog(LOG_ERR, - "statd: send_notice: malloc error on mon %s (id=%d)\n", - monp->mon_id.mon_name, - * ((int *)monp->priv)); + strdup(monp->mon_id.my_id.my_name)) == 0) { + syslog(LOG_ERR, "statd: send_notice: " + "malloc error on mon %s (id=%d)\n", + monp->mon_id.mon_name, + *((int *)monp->priv)); free(minfop->id.mon_id.mon_name); free(minfop); continue; @@ -694,15 +682,14 @@ send_notice(mon_name, state) * resources and continue. */ if (thr_create(NULL, NULL, thr_send_notice, - (void *)minfop, THR_DETACHED, - NULL)) { - syslog(LOG_ERR, - "statd: unable to create thread to send_notice to %s.\n", - mon_name); - free(minfop->id.mon_id.mon_name); - free(minfop->id.mon_id.my_id.my_name); - free(minfop); - continue; + minfop, THR_DETACHED, NULL)) { + syslog(LOG_ERR, "statd: unable to " + "create thread to send_notice to " + "%s.\n", mon_name); + free(minfop->id.mon_id.mon_name); + free(minfop->id.mon_id.my_id.my_name); + free(minfop); + continue; } } } @@ -745,9 +732,7 @@ thr_send_notice(void *arg) * Contact lockd specified by monp. */ static int -statd_call_lockd(monp, state) - mon *monp; - int state; +statd_call_lockd(mon *monp, int state) { enum clnt_stat clnt_stat; struct timeval tottimeout; @@ -768,7 +753,7 @@ statd_call_lockd(monp, state) } if (debug) (void) printf("statd_call_lockd: %s state = %d\n", - stat.mon_name, stat.state); + stat.mon_name, stat.state); tottimeout.tv_sec = SM_RPC_TIMEOUT; tottimeout.tv_usec = 0; @@ -779,17 +764,16 @@ statd_call_lockd(monp, state) return (-1); } - clnt_stat = clnt_call(clnt, my_idp->my_proc, - xdr_sm_status, (char *)&stat, - xdr_void, NULL, tottimeout); + clnt_stat = clnt_call(clnt, my_idp->my_proc, xdr_sm_status, + (char *)&stat, xdr_void, NULL, tottimeout); if (debug) { (void) printf("clnt_stat=%s(%d)\n", - clnt_sperrno(clnt_stat), clnt_stat); + clnt_sperrno(clnt_stat), clnt_stat); } if (clnt_stat != (int)RPC_SUCCESS) { syslog(LOG_WARNING, - "statd: cannot talk to lockd at %s, %s(%d)\n", - my_idp->my_name, clnt_sperrno(clnt_stat), clnt_stat); + "statd: cannot talk to lockd at %s, %s(%d)\n", + my_idp->my_name, clnt_sperrno(clnt_stat), clnt_stat); rc = -1; } @@ -855,8 +839,7 @@ create_client(char *host, int prognum, int versnum, char *netid, * to name, otherwise print out the entire monitor table. */ static void -pr_mon(name) - char *name; +pr_mon(char *name) { mon_entry *nl; int hash; @@ -869,17 +852,16 @@ pr_mon(name) for (hash = 0; hash < MAX_HASHSIZE; hash++) { mutex_lock(&mon_table[hash].lock); nl = mon_table[hash].sm_monhdp; - if (nl == (struct mon_entry *)NULL) { + if (nl == NULL) { (void) printf( - "*****monitor_q = NULL hash %d\n", - hash); + "*****monitor_q = NULL hash %d\n", hash); mutex_unlock(&mon_table[hash].lock); continue; } (void) printf("*****monitor_q:\n "); - while (nl != (mon_entry *)NULL) { + while (nl != NULL) { (void) printf("%s:(%x), ", - nl->id.mon_id.mon_name, (int)nl); + nl->id.mon_id.mon_name, (int)nl); nl = nl->nxt; } mutex_unlock(&mon_table[hash].lock); @@ -889,13 +871,13 @@ pr_mon(name) SMHASH(name, hash); mutex_lock(&mon_table[hash].lock); nl = mon_table[hash].sm_monhdp; - if (nl == (struct mon_entry *)NULL) { + if (nl == NULL) { (void) printf("*****monitor_q = NULL hash %d\n", hash); } else { (void) printf("*****monitor_q:\n "); - while (nl != (mon_entry *)NULL) { + while (nl != NULL) { (void) printf("%s:(%x), ", - nl->id.mon_id.mon_name, (int)nl); + nl->id.mon_id.mon_name, (int)nl); nl = nl->nxt; } (void) printf("\n"); @@ -1010,7 +992,7 @@ get_system_id(char *hostname) if (hp == (void *) NULL) { return (NULL); } - while ((ncp = getnetconfig(hp)) != (struct netconfig *)NULL) { + while ((ncp = getnetconfig(hp)) != NULL) { if ((strcmp(ncp->nc_protofmly, NC_INET) == 0) || (strcmp(ncp->nc_protofmly, NC_INET6) == 0)) { addrs = NULL; @@ -1067,7 +1049,7 @@ merge_hosts(void) * those of the receiver. */ lifc = getmyaddrs(); - if (lifc == (struct lifconf *)NULL) { + if (lifc == NULL) { goto finish; } lifrp = lifc->lifc_req; diff --git a/usr/src/cmd/fs.d/nfs/statd/sm_statd.c b/usr/src/cmd/fs.d/nfs/statd/sm_statd.c index 420dd68620..e7805e7747 100644 --- a/usr/src/cmd/fs.d/nfs/statd/sm_statd.c +++ b/usr/src/cmd/fs.d/nfs/statd/sm_statd.c @@ -18,6 +18,11 @@ * * CDDL HEADER END */ + +/* + * Copyright 2015 Nexenta Systems, Inc. All rights reserved. + */ + /* * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. @@ -86,8 +91,8 @@ static void delete_name(name_entry **namepp, char *name); static void remove_name(char *name, int op, int startup); static int statd_call_statd(char *name); static void pr_name(char *name, int flag); -static void *thr_statd_init(); -static void *sm_try(); +static void *thr_statd_init(void); +static void *sm_try(void); static void *thr_call_statd(void *); static void remove_single_name(char *name, char *dir1, char *dir2); static int move_file(char *fromdir, char *file, char *todir); @@ -99,10 +104,10 @@ static char *family2string(sa_family_t family); * all entries to notify its own failure */ void -statd_init() +statd_init(void) { struct dirent *dirp; - DIR *dp; + DIR *dp; FILE *fp, *fp_tmp; int i, tmp_state; char state_file[MAXPATHLEN+SM_MAXPATHLEN]; @@ -114,12 +119,13 @@ statd_init() * First try to open the file. If that fails, try to create it. * If that fails, give up. */ - if ((fp = fopen(STATE, "r+")) == (FILE *)NULL) - if ((fp = fopen(STATE, "w+")) == (FILE *)NULL) { + if ((fp = fopen(STATE, "r+")) == NULL) { + if ((fp = fopen(STATE, "w+")) == NULL) { syslog(LOG_ERR, "can't open %s: %m", STATE); exit(1); } else (void) chmod(STATE, 0644); + } if ((fscanf(fp, "%d", &LOCAL_STATE)) == EOF) { if (debug >= 2) (void) printf("empty file\n"); @@ -131,9 +137,8 @@ statd_init() */ for (i = 0; i < pathix; i++) { (void) sprintf(state_file, "%s/statmon/state", path_name[i]); - if ((fp_tmp = fopen(state_file, "r+")) == (FILE *)NULL) { - if ((fp_tmp = fopen(state_file, "w+")) == - (FILE *)NULL) { + if ((fp_tmp = fopen(state_file, "r+")) == NULL) { + if ((fp_tmp = fopen(state_file, "w+")) == NULL) { if (debug) syslog(LOG_ERR, "can't open %s: %m", @@ -181,9 +186,8 @@ statd_init() for (i = 0; i < pathix; i++) { (void) sprintf(state_file, "%s/statmon/state", path_name[i]); - if ((fp_tmp = fopen(state_file, "r+")) == (FILE *)NULL) { - if ((fp_tmp = fopen(state_file, "w+")) == - (FILE *)NULL) { + if ((fp_tmp = fopen(state_file, "r+")) == NULL) { + if ((fp_tmp = fopen(state_file, "w+")) == NULL) { syslog(LOG_ERR, "can't open %s: %m", state_file); continue; @@ -218,7 +222,7 @@ statd_init() } /* get all entries in CURRENT into BACKUP */ - if ((dp = opendir(CURRENT)) == (DIR *)NULL) { + if ((dp = opendir(CURRENT)) == NULL) { syslog(LOG_ERR, "statd: open current directory, error %m\n"); exit(1); } @@ -234,9 +238,10 @@ statd_init() (void) closedir(dp); /* Contact hosts' statd */ - if (thr_create(NULL, NULL, thr_statd_init, NULL, THR_DETACHED, 0)) { + if (thr_create(NULL, 0, (void *(*)(void *))thr_statd_init, NULL, + THR_DETACHED, NULL)) { syslog(LOG_ERR, - "statd: unable to create thread for thr_statd_init\n"); + "statd: unable to create thread for thr_statd_init\n"); exit(1); } } @@ -244,8 +249,8 @@ statd_init() /* * Work thread which contacts hosts' statd. */ -void * -thr_statd_init() +static void * +thr_statd_init(void) { struct dirent *dirp; DIR *dp; @@ -256,7 +261,7 @@ thr_statd_init() char buf[MAXPATHLEN+SM_MAXPATHLEN]; /* Go thru backup directory and contact hosts */ - if ((dp = opendir(BACKUP)) == (DIR *)NULL) { + if ((dp = opendir(BACKUP)) == NULL) { syslog(LOG_ERR, "statd: open backup directory, error %m\n"); exit(1); } @@ -297,7 +302,7 @@ thr_statd_init() * continue to next item on list. */ name = strdup(dirp->d_name); - if (name == (char *)NULL) { + if (name == NULL) { syslog(LOG_ERR, "statd: unable to allocate space for name %s\n", dirp->d_name); @@ -305,8 +310,7 @@ thr_statd_init() } /* Create a thread to do a statd_call_statd for name */ - if (thr_create(NULL, NULL, thr_call_statd, - (void *) name, 0, 0)) { + if (thr_create(NULL, 0, thr_call_statd, name, 0, NULL)) { syslog(LOG_ERR, "statd: unable to create thr_call_statd() " "for name %s.\n", dirp->d_name); @@ -372,7 +376,7 @@ thr_statd_init() * continue to next item on list. */ name = strdup(dirp->d_name); - if (name == (char *)NULL) { + if (name == NULL) { syslog(LOG_ERR, "statd: unable to allocate space for name %s\n", dirp->d_name); @@ -380,8 +384,7 @@ thr_statd_init() } /* Create a thread to do a statd_call_statd for name */ - if (thr_create(NULL, NULL, thr_call_statd, - (void *) name, 0, 0)) { + if (thr_create(NULL, 0, thr_call_statd, name, 0, NULL)) { syslog(LOG_ERR, "statd: unable to create thr_call_statd() " "for name %s.\n", dirp->d_name); @@ -421,20 +424,19 @@ thr_statd_init() /* - * Reset the die and in_crash variable and signal other threads - * that have issued an sm_crash and are waiting. + * Reset the die and in_crash variables. */ mutex_lock(&crash_lock); die = 0; in_crash = 0; mutex_unlock(&crash_lock); - cond_broadcast(&crash_finish); if (debug) (void) printf("Creating thread for sm_try\n"); /* Continue to notify statd on hosts that were unreachable. */ - if (thr_create(NULL, NULL, sm_try, NULL, THR_DETACHED, 0)) + if (thr_create(NULL, 0, (void *(*)(void *))sm_try, NULL, THR_DETACHED, + NULL)) syslog(LOG_ERR, "statd: unable to create thread for sm_try().\n"); thr_exit((void *) 0); @@ -536,8 +538,7 @@ thr_call_statd(void *namep) * state has changed for this server. */ static int -statd_call_statd(name) - char *name; +statd_call_statd(char *name) { enum clnt_stat clnt_stat; struct timeval tottimeout; @@ -564,7 +565,7 @@ statd_call_statd(name) unq_len = strcspn(name, "."); if ((strncmp(name, SM_ADDR_IPV4, unq_len) == 0) || - (strncmp(name, SM_ADDR_IPV6, unq_len) == 0)) { + (strncmp(name, SM_ADDR_IPV6, unq_len) == 0)) { name_or_addr = strchr(name, '.') + 1; } else { name_or_addr = name; @@ -577,7 +578,7 @@ statd_call_statd(name) */ if (debug) { (void) printf("statd_call_statd: calling create_client(%s)\n", - name_or_addr); + name_or_addr); } tottimeout.tv_sec = SM_RPC_TIMEOUT; @@ -594,15 +595,23 @@ statd_call_statd(name) xdr_void, NULL, tottimeout); if (debug) { (void) printf("clnt_stat=%s(%d)\n", - clnt_sperrno(clnt_stat), clnt_stat); + clnt_sperrno(clnt_stat), clnt_stat); } if (clnt_stat != (int)RPC_SUCCESS) { syslog(LOG_WARNING, - "statd: cannot talk to statd at %s, %s(%d)\n", - name_or_addr, clnt_sperrno(clnt_stat), clnt_stat); + "statd: cannot talk to statd at %s, %s(%d)\n", + name_or_addr, clnt_sperrno(clnt_stat), clnt_stat); rc = -1; } + /* + * Wait until the host_name is populated. + */ + (void) mutex_lock(&merges_lock); + while (in_merges) + (void) cond_wait(&merges_cond, &merges_lock); + (void) mutex_unlock(&merges_lock); + /* For HA systems and multi-homed hosts */ ntf.state = LOCAL_STATE; for (i = 0; i < addrix; i++) { @@ -610,8 +619,7 @@ statd_call_statd(name) if (debug) (void) printf("statd_call_statd at %s\n", name_or_addr); clnt_stat = clnt_call(clnt, SM_NOTIFY, xdr_stat_chge, - (char *)&ntf, xdr_void, NULL, - tottimeout); + (char *)&ntf, xdr_void, NULL, tottimeout); if (clnt_stat != (int)RPC_SUCCESS) { syslog(LOG_WARNING, "statd: cannot talk to statd at %s, %s(%d)\n", @@ -632,7 +640,7 @@ statd_call_statd(name) * variable will signal it. */ void * -sm_try() +sm_try(void) { name_entry *nl, *next; timestruc_t wtime; @@ -661,7 +669,7 @@ sm_try() mutex_unlock(&crash_lock); - while (((nl = next) != (name_entry *)NULL) && (!die)) { + while (((nl = next) != NULL) && (!die)) { next = next->nxt; if (statd_call_statd(nl->name) == 0) { /* remove name from BACKUP */ @@ -706,14 +714,13 @@ out: * Malloc's space and returns the ptr to malloc'ed space. NULL if unsuccessful. */ char * -xmalloc(len) - unsigned len; +xmalloc(unsigned len) { char *new; if ((new = malloc(len)) == 0) { syslog(LOG_ERR, "statd: malloc, error %m\n"); - return ((char *)NULL); + return (NULL); } else { (void) memset(new, 0, len); return (new); @@ -726,10 +733,7 @@ xmalloc(len) * is different */ static name_entry * -insert_name(namepp, name, need_alloc) - name_entry **namepp; - char *name; - int need_alloc; +insert_name(name_entry **namepp, char *name, int need_alloc) { name_entry *new; @@ -739,7 +743,7 @@ insert_name(namepp, name, need_alloc) /* Allocate name when needed which is only when adding to record_t */ if (need_alloc) { - if ((new->name = strdup(name)) == (char *)NULL) { + if ((new->name = strdup(name)) == NULL) { syslog(LOG_ERR, "statd: strdup, error %m\n"); free(new); return (NULL); @@ -748,7 +752,7 @@ insert_name(namepp, name, need_alloc) new->name = name; new->nxt = *namepp; - if (new->nxt != (name_entry *)NULL) + if (new->nxt != NULL) new->nxt->prev = new; new->prev = (name_entry *) NULL; @@ -756,7 +760,7 @@ insert_name(namepp, name, need_alloc) *namepp = new; if (debug) { (void) printf("insert_name: inserted %s at %p\n", - name, (void *)namepp); + name, (void *)namepp); } return (new); @@ -766,21 +770,19 @@ insert_name(namepp, name, need_alloc) * Deletes name from specified list (namepp). */ static void -delete_name(namepp, name) - name_entry **namepp; - char *name; +delete_name(name_entry **namepp, char *name) { name_entry *nl; nl = *namepp; - while (nl != (name_entry *)NULL) { + while (nl != NULL) { if (str_cmp_address_specifier(nl->name, name) == 0 || str_cmp_unqual_hostname(nl->name, name) == 0) { - if (nl->prev != (name_entry *)NULL) + if (nl->prev != NULL) nl->prev->nxt = nl->nxt; else *namepp = nl->nxt; - if (nl->nxt != (name_entry *)NULL) + if (nl->nxt != NULL) nl->nxt->prev = nl->prev; free(nl->name); free(nl); @@ -794,21 +796,19 @@ delete_name(namepp, name) * Finds name from specified list (namep). */ static name_entry * -find_name(namep, name) - name_entry **namep; - char *name; +find_name(name_entry **namep, char *name) { name_entry *nl; nl = *namep; - while (nl != (name_entry *)NULL) { + while (nl != NULL) { if (str_cmp_unqual_hostname(nl->name, name) == 0) { return (nl); } nl = nl->nxt; } - return ((name_entry *)NULL); + return (NULL); } /* @@ -816,8 +816,7 @@ find_name(namep, name) */ int -create_file(name) - char *name; +create_file(char *name) { int fd; @@ -846,8 +845,7 @@ create_file(name) * Deletes the file specified by name. */ void -delete_file(name) - char *name; +delete_file(char *name) { if (debug >= 2) (void) printf("Remove monitor entry %s\n", name); @@ -861,8 +859,7 @@ delete_file(name) * Return 1 if file is a symlink, else 0. */ int -is_symlink(file) - char *file; +is_symlink(char *file) { int error; struct stat lbuf; @@ -889,10 +886,7 @@ is_symlink(file) * Returns 0 for success, 1 for failure. */ static int -move_file(fromdir, file, todir) - char *fromdir; - char *file; - char *todir; +move_file(char *fromdir, char *file, char *todir) { int n; char rname[MAXNAMELEN + 1]; /* +1 for the terminating NULL */ @@ -910,7 +904,7 @@ move_file(fromdir, file, todir) if (n <= 0) { if (debug >= 2) { (void) printf("move_file: can't read link %s\n", - from); + from); } return (1); } @@ -950,10 +944,7 @@ move_file(fromdir, file, todir) * Both files should be in directory `todir'. */ int -create_symlink(todir, rname, lname) - char *todir; - char *rname; - char *lname; +create_symlink(char *todir, char *rname, char *lname) { int error; char lpath[MAXPATHLEN]; @@ -972,9 +963,8 @@ create_symlink(todir, rname, lname) error = errno; if (error != 0 && error != EEXIST) { if (debug >= 2) { - (void) printf( - "create_symlink: can't link %s/%s -> %s\n", - todir, lname, rname); + (void) printf("create_symlink: can't link " + "%s/%s -> %s\n", todir, lname, rname); } return (1); } @@ -983,10 +973,10 @@ create_symlink(todir, rname, lname) if (debug) { if (error == EEXIST) { (void) printf("link %s/%s -> %s already exists\n", - todir, lname, rname); + todir, lname, rname); } else { (void) printf("created link %s/%s -> %s\n", - todir, lname, rname); + todir, lname, rname); } } @@ -1123,7 +1113,7 @@ count_symlinks(char *dir, char *name, int *count) char lpath[MAXPATHLEN]; char rname[MAXNAMELEN + 1]; /* +1 for term NULL */ - if ((dp = opendir(dir)) == (DIR *)NULL) { + if ((dp = opendir(dir)) == NULL) { syslog(LOG_ERR, "count_symlinks: open %s dir, error %m\n", dir); return (-1); @@ -1173,7 +1163,7 @@ count_symlinks(char *dir, char *name, int *count) /* * Manage the cache of hostnames. An entry for each host that has recently - * locked a file is kept. There is an in-ram table (rec_table) and an empty + * locked a file is kept. There is an in-ram table (record_table) and an empty * file in the file system name space (/var/statmon/sm/). This * routine adds (deletes) the name to (from) the in-ram table and the entry * to (from) the file system name space. @@ -1182,9 +1172,7 @@ count_symlinks(char *dir, char *name, int *count) * deleted. */ void -record_name(name, op) - char *name; - int op; +record_name(char *name, int op) { name_entry *nl; int i; @@ -1204,9 +1192,9 @@ record_name(name, op) return; if (name[0] == '\0' || strchr(name, '/') != NULL || - strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { + strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { syslog(LOG_ERR|LOG_AUTH, "statd: attempt to %s \"%s/%s\"", - op == 1 ? "create" : "remove", CURRENT, name); + op == 1 ? "create" : "remove", CURRENT, name); return; } @@ -1214,7 +1202,7 @@ record_name(name, op) if (debug) { if (op == 1) (void) printf("inserting %s at hash %d,\n", - name, hash); + name, hash); else (void) printf("deleting %s at hash %d\n", name, hash); pr_name(name, 1); @@ -1224,7 +1212,7 @@ record_name(name, op) if (op == 1) { /* insert */ mutex_lock(&record_table[hash].lock); record_q = &record_table[hash].sm_rechdp; - if ((nl = find_name(record_q, name)) == (name_entry *)NULL) { + if ((nl = find_name(record_q, name)) == NULL) { int path_len; @@ -1237,8 +1225,8 @@ record_name(name, op) path_len = strlen(CURRENT) + strlen(name) + 2; if (path_len > MAXPATHLEN) { syslog(LOG_ERR, - "statd: pathname too long: %s/%s\n", - CURRENT, name); + "statd: pathname too long: %s/%s\n", + CURRENT, name); return; } (void) strcpy(path, CURRENT); @@ -1252,13 +1240,12 @@ record_name(name, op) /* make an entry in alternate paths */ for (i = 0; i < pathix; i++) { path_len = strlen(path_name[i]) + - strlen("/statmon/sm/") + - strlen(name) + 1; + strlen("/statmon/sm/") + strlen(name) + 1; if (path_len > MAXPATHLEN) { - syslog(LOG_ERR, - "statd: pathname too long: %s/statmon/sm/%s\n", - path_name[i], name); + syslog(LOG_ERR, "statd: pathname too " + "long: %s/statmon/sm/%s\n", + path_name[i], name); continue; } (void) strcpy(path, path_name[i]); @@ -1274,7 +1261,7 @@ record_name(name, op) } else { /* delete */ mutex_lock(&record_table[hash].lock); record_q = &record_table[hash].sm_rechdp; - if ((nl = find_name(record_q, name)) == (name_entry *)NULL) { + if ((nl = find_name(record_q, name)) == NULL) { mutex_unlock(&record_table[hash].lock); return; } @@ -1331,7 +1318,7 @@ record_addr(char *name, sa_family_t family, struct netobj *ah) if (family == AF_INET) (void) printf("record_addr: addr= %x\n", addr.s_addr); else if (family == AF_INET6) - (void) printf("record_addr: addr= %x\n", \ + (void) printf("record_addr: addr= %x\n", ((struct in6_addr *)addr6)->s6_addr); } @@ -1361,7 +1348,7 @@ record_addr(char *name, sa_family_t family, struct netobj *ah) break; case AF_INET6: - (void) sprintf(ascii_addr, "%s.%s", famstr,\ + (void) sprintf(ascii_addr, "%s.%s", famstr, inet_ntop(family, addr6, abuf, sizeof (abuf))); break; @@ -1410,7 +1397,7 @@ record_addr(char *name, sa_family_t family, struct netobj *ah) * SM_CRASH - simulate a crash of statd. */ void -sm_crash() +sm_crash(void) { name_entry *nl, *next; mon_entry *nl_monp, *mon_next; @@ -1424,14 +1411,14 @@ sm_crash() mutex_unlock(&mon_table[k].lock); continue; } else { - while ((nl_monp = mon_next) != (mon_entry *)NULL) { + while ((nl_monp = mon_next) != NULL) { mon_next = mon_next->nxt; nl_idp = &nl_monp->id.mon_id.my_id; free(nl_monp->id.mon_id.mon_name); free(nl_idp->my_name); free(nl_monp); } - mon_table[k].sm_monhdp = (mon_entry *)NULL; + mon_table[k].sm_monhdp = NULL; } mutex_unlock(&mon_table[k].lock); } @@ -1444,25 +1431,25 @@ sm_crash() mutex_unlock(&record_table[k].lock); continue; } else { - while ((nl = next) != (name_entry *)NULL) { + while ((nl = next) != NULL) { next = next->nxt; free(nl->name); free(nl); } - record_table[k].sm_rechdp = (name_entry *)NULL; + record_table[k].sm_rechdp = NULL; } mutex_unlock(&record_table[k].lock); } /* Clean up entries in recovery table */ mutex_lock(&recov_q.lock); - if ((next = recov_q.sm_recovhdp) != (name_entry *)NULL) { - while ((nl = next) != (name_entry *)NULL) { + if ((next = recov_q.sm_recovhdp) != NULL) { + while ((nl = next) != NULL) { next = next->nxt; free(nl->name); free(nl); } - recov_q.sm_recovhdp = (name_entry *)NULL; + recov_q.sm_recovhdp = NULL; } mutex_unlock(&recov_q.lock); statd_init(); @@ -1473,20 +1460,20 @@ sm_crash() * locks. */ void -sm_inithash() +sm_inithash(void) { int k; if (debug) (void) printf("Initializing hash tables\n"); for (k = 0; k < MAX_HASHSIZE; k++) { - mon_table[k].sm_monhdp = (mon_entry *)NULL; - record_table[k].sm_rechdp = (name_entry *)NULL; + mon_table[k].sm_monhdp = NULL; + record_table[k].sm_rechdp = NULL; mutex_init(&mon_table[k].lock, USYNC_THREAD, NULL); mutex_init(&record_table[k].lock, USYNC_THREAD, NULL); } mutex_init(&recov_q.lock, USYNC_THREAD, NULL); - recov_q.sm_recovhdp = (name_entry *)NULL; + recov_q.sm_recovhdp = NULL; } @@ -1522,9 +1509,7 @@ family2string(sa_family_t family) * prints out each list in recov_q specified by name. */ static void -pr_name(name, flag) - char *name; - int flag; +pr_name(char *name, int flag) { name_entry *nl; unsigned int hash; @@ -1536,7 +1521,7 @@ pr_name(name, flag) (void) printf("*****record_q: "); mutex_lock(&record_table[hash].lock); nl = record_table[hash].sm_rechdp; - while (nl != (name_entry *)NULL) { + while (nl != NULL) { (void) printf("(%x), ", (int)nl); nl = nl->nxt; } @@ -1545,7 +1530,7 @@ pr_name(name, flag) (void) printf("*****recovery_q: "); mutex_lock(&recov_q.lock); nl = recov_q.sm_recovhdp; - while (nl != (name_entry *)NULL) { + while (nl != NULL) { (void) printf("(%x), ", (int)nl); nl = nl->nxt; } diff --git a/usr/src/cmd/fs.d/nfs/statd/sm_statd.h b/usr/src/cmd/fs.d/nfs/statd/sm_statd.h index e1a5974678..aa1368b461 100644 --- a/usr/src/cmd/fs.d/nfs/statd/sm_statd.h +++ b/usr/src/cmd/fs.d/nfs/statd/sm_statd.h @@ -19,6 +19,11 @@ * * CDDL HEADER END */ + +/* + * Copyright 2015 Nexenta Systems, Inc. All rights reserved. + */ + /* * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. @@ -38,7 +43,6 @@ */ /* - * Copyright 2011 Nexenta Systems, Inc. All rights reserved. * Copyright (c) 2012 by Delphix. All rights reserved. */ @@ -79,7 +83,7 @@ struct mon_entry { }; typedef struct mon_entry mon_entry; -/* Structure entry for record (rec_table) and recovery (recov_q) tables */ +/* Structure entry for record (record_table) and recovery (recov_q) tables */ struct name_entry { char *name; /* name of host */ int count; /* count of entries */ @@ -98,7 +102,7 @@ typedef struct moninfo { typedef struct sm_hash { union { struct mon_entry *mon_hdptr; /* Head ptr for mon_table */ - name_entry *rec_hdptr; /* Head ptr for rec_table */ + name_entry *rec_hdptr; /* Head ptr for record_table */ name_entry *recov_hdptr; /* Head ptr for recov_q */ } smhd_t; mutex_t lock; /* Lock to protect each list head */ @@ -131,7 +135,6 @@ extern int die; /* Flag to indicate that an SM_CRASH */ /* request came in & to stop threads cleanly */ extern int in_crash; /* Flag to single thread sm_crash requests. */ extern int regfiles_only; /* Flag to indicate symlink use in statmon */ -extern cond_t crash_finish; /* Condition to wait until crash is finished */ extern mutex_t sm_trylock; /* Lock to single thread sm_try */ /* * The only established lock precedence here is: @@ -142,6 +145,11 @@ extern mutex_t name_addrlock; /* Locks all entries of name-to-addr table */ extern rwlock_t thr_rwlock; /* Reader/writer lock for requests coming in */ extern cond_t retrywait; /* Condition to wait before starting retry */ +extern boolean_t in_merges; /* Flag to indicate the host_name is not */ + /* populated yet */ +extern mutex_t merges_lock; /* Lock for in_merges variable */ +extern cond_t merges_cond; /* Condition variable for in_merges */ + extern char STATE[MAXPATHLEN], CURRENT[MAXPATHLEN]; extern char BACKUP[MAXPATHLEN]; extern int LOCAL_STATE; @@ -185,7 +193,7 @@ extern int create_file(char *name); extern void delete_file(char *name); extern void record_name(char *name, int op); extern void sm_crash(void); -extern void statd_init(); +extern void statd_init(void); extern void merge_hosts(void); extern void merge_ips(void); extern CLIENT *create_client(char *, int, int, char *, struct timeval *); @@ -203,7 +211,7 @@ extern void sm_unmon_all_svc(my_id *myidp, sm_stat *resp); extern void sm_simu_crash_svc(void *myidp); extern void sm_notify_svc(stat_chge *ntfp); -extern void sm_inithash(); +extern void sm_inithash(void); extern void copydir_from_to(char *from_dir, char *to_dir); extern int str_cmp_unqual_hostname(char *, char *); extern void record_addr(char *name, sa_family_t family, struct netobj *ah); diff --git a/usr/src/cmd/fs.d/nfs/statd/sm_svc.c b/usr/src/cmd/fs.d/nfs/statd/sm_svc.c index 1f657a0a48..37ef788f50 100644 --- a/usr/src/cmd/fs.d/nfs/statd/sm_svc.c +++ b/usr/src/cmd/fs.d/nfs/statd/sm_svc.c @@ -18,9 +18,10 @@ * * CDDL HEADER END */ + /* + * Copyright 2015 Nexenta Systems, Inc. All rights reserved. * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. - * Copyright 2011 Nexenta Systems, Inc. All rights reserved. */ /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ @@ -81,6 +82,9 @@ #define backup1 "statmon/sm.bak/" #define state1 "statmon/state" +extern int daemonize_init(void); +extern void daemonize_fini(int fd); + /* * User and group IDs to run as. These are hardwired, rather than looked * up at runtime, because they are very unlikely to change and because they @@ -120,12 +124,15 @@ int pathix = 0; /* # of -p entries */ mutex_t crash_lock; int die; int in_crash; -cond_t crash_finish; mutex_t sm_trylock; rwlock_t thr_rwlock; cond_t retrywait; mutex_t name_addrlock; +mutex_t merges_lock; +cond_t merges_cond; +boolean_t in_merges; + /* forward references */ static void set_statmon_owner(void); static void copy_client_names(void); @@ -138,7 +145,7 @@ static int nftw_owner(const char *, const struct stat *, int, struct FTW *); * SM_STAT * returns stat_fail to caller * SM_MON - * adds an entry to the monitor_q and the record_q + * adds an entry to the monitor_q and the record_q. * This message is sent by the server lockd to the server * statd, to indicate that a new client is to be monitored. * It is also sent by the server lockd to the client statd @@ -150,7 +157,7 @@ static int nftw_owner(const char *, const struct stat *, int, struct FTW *); * monitor_q and the record_q. Our statd has this * disabled. * SM_SIMU_CRASH - * simulate a crash. removes everything from the + * simulate a crash. Removes everything from the * record_q and the recovery_q, then calls statd_init() * to restart things. This message is sent by the server * lockd to the server statd to have all clients notified @@ -184,9 +191,7 @@ static int nftw_owner(const char *, const struct stat *, int, struct FTW *); */ static void -sm_prog_1(rqstp, transp) - struct svc_req *rqstp; - SVCXPRT *transp; +sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp) { union { struct sm_name sm_stat_1_arg; @@ -299,29 +304,29 @@ sm_prog_1(rqstp, transp) * Remove all files under directory path_dir. */ static int -remove_dir(path_dir) -char *path_dir; +remove_dir(char *path_dir) { DIR *dp; struct dirent *dirp; char tmp_path[MAXPATHLEN]; - if ((dp = opendir(path_dir)) == (DIR *)NULL) { + if ((dp = opendir(path_dir)) == NULL) { if (debug) - syslog(LOG_ERR, - "warning: open directory %s failed: %m\n", path_dir); + syslog(LOG_ERR, + "warning: open directory %s failed: %m\n", + path_dir); return (1); } while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") != 0 && - strcmp(dirp->d_name, "..") != 0) { + strcmp(dirp->d_name, "..") != 0) { if (strlen(path_dir) + strlen(dirp->d_name) +2 > - MAXPATHLEN) { + MAXPATHLEN) { - syslog(LOG_ERR, - "statd: remove dir %s/%s failed. Pathname too long.\n", - path_dir, dirp->d_name); + syslog(LOG_ERR, "statd: remove dir %s/%s " + "failed. Pathname too long.\n", path_dir, + dirp->d_name); continue; } @@ -341,9 +346,7 @@ char *path_dir; * Symlinks, if any, are preserved. */ void -copydir_from_to(from_dir, to_dir) -char *from_dir; -char *to_dir; +copydir_from_to(char *from_dir, char *to_dir) { int n; DIR *dp; @@ -351,16 +354,17 @@ char *to_dir; char rname[MAXNAMELEN + 1]; char path[MAXPATHLEN+MAXNAMELEN+2]; - if ((dp = opendir(from_dir)) == (DIR *)NULL) { + if ((dp = opendir(from_dir)) == NULL) { if (debug) - syslog(LOG_ERR, - "warning: open directory %s failed: %m\n", from_dir); + syslog(LOG_ERR, + "warning: open directory %s failed: %m\n", + from_dir); return; } while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || - strcmp(dirp->d_name, "..") == 0) { + strcmp(dirp->d_name, "..") == 0) { continue; } @@ -376,9 +380,8 @@ char *to_dir; n = readlink(path, rname, MAXNAMELEN); if (n <= 0) { if (debug >= 2) { - (void) printf( - "copydir_from_to: can't read link %s\n", - path); + (void) printf("copydir_from_to: can't " + "read link %s\n", path); } continue; } @@ -422,7 +425,7 @@ init_hostname(void) host_name_count = lifn.lifn_count; - host_name = (char **)malloc(host_name_count * sizeof (char *)); + host_name = malloc(host_name_count * sizeof (char *)); if (host_name == NULL) { perror("statd -a can't get ip configuration\n"); close(sock); @@ -432,6 +435,28 @@ init_hostname(void) return (0); } +static void +thr_statd_merges(void) +{ + /* + * Get other aliases from each interface. + */ + merge_hosts(); + + /* + * Get all of the configured IP addresses. + */ + merge_ips(); + + /* + * Notify the waiters. + */ + (void) mutex_lock(&merges_lock); + in_merges = B_FALSE; + (void) cond_broadcast(&merges_cond); + (void) mutex_unlock(&merges_lock); +} + int main(int argc, char *argv[]) { @@ -442,6 +467,7 @@ main(int argc, char *argv[]) struct rlimit rl; int mode; int sz; + int pipe_fd = -1; int connmaxrec = RPC_MAXDATASIZE; addrix = 0; @@ -552,20 +578,8 @@ main(int argc, char *argv[]) (void) enable_extended_FILE_stdio(-1, -1); if (!debug) { - ppid = fork(); - if (ppid == -1) { - (void) fprintf(stderr, "statd: fork failure\n"); - (void) fflush(stderr); - abort(); - } - if (ppid != 0) { - exit(0); - } - closefrom(0); - (void) open("/dev/null", O_RDONLY); - (void) open("/dev/null", O_WRONLY); - (void) dup(1); - (void) setsid(); + pipe_fd = daemonize_init(); + openlog("statd", LOG_PID, LOG_DAEMON); } @@ -588,11 +602,20 @@ main(int argc, char *argv[]) exit(0); } - /* Get other aliases from each interface. */ - merge_hosts(); + mutex_init(&merges_lock, USYNC_THREAD, NULL); + cond_init(&merges_cond, USYNC_THREAD, NULL); + in_merges = B_TRUE; - /* Get all of the configured IP addresses. */ - merge_ips(); + /* + * Create thr_statd_merges() thread to populate the host_name list + * asynchronously. + */ + if (thr_create(NULL, 0, (void *(*)(void *))thr_statd_merges, NULL, + THR_DETACHED, NULL) != 0) { + syslog(LOG_ERR, "statd: unable to create thread for " + "thr_statd_merges()."); + exit(1); + } /* * Set to automatic mode such that threads are automatically @@ -614,14 +637,14 @@ main(int argc, char *argv[]) } if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "netpath")) { - syslog(LOG_ERR, - "statd: unable to create (SM_PROG, SM_VERS) for netpath."); + syslog(LOG_ERR, "statd: unable to create (SM_PROG, SM_VERS) " + "for netpath."); exit(1); } if (!svc_create(sm_prog_1, NSM_ADDR_PROGRAM, NSM_ADDR_V1, "netpath")) { - syslog(LOG_ERR, - "statd: unable to create (NSM_ADDR_PROGRAM, NSM_ADDR_V1) for netpath."); + syslog(LOG_ERR, "statd: unable to create (NSM_ADDR_PROGRAM, " + "NSM_ADDR_V1) for netpath."); } /* @@ -655,7 +678,6 @@ main(int argc, char *argv[]) rwlock_init(&thr_rwlock, USYNC_THREAD, NULL); mutex_init(&crash_lock, USYNC_THREAD, NULL); mutex_init(&name_addrlock, USYNC_THREAD, NULL); - cond_init(&crash_finish, USYNC_THREAD, NULL); cond_init(&retrywait, USYNC_THREAD, NULL); sm_inithash(); die = 0; @@ -668,12 +690,17 @@ main(int argc, char *argv[]) in_crash = 1; statd_init(); + /* + * statd is up and running as far as we are concerned. + */ + daemonize_fini(pipe_fd); + if (debug) (void) printf("Starting svc_run\n"); svc_run(); syslog(LOG_ERR, "statd: svc_run returned\n"); /* NOTREACHED */ - thr_exit((void *) 1); + thr_exit((void *)1); return (0); } @@ -714,7 +741,7 @@ set_statmon_owner(void) } __fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION, - PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL); + PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL); } /* @@ -724,7 +751,7 @@ set_statmon_owner(void) */ static void -copy_client_names() +copy_client_names(void) { int i; char buf[MAXPATHLEN+SM_MAXPATHLEN]; -- 2.11.4.GIT