17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*98573c19SMarcel Telka
227c478bd9Sstevel@tonic-gate /*
23*98573c19SMarcel Telka * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
249ff75adeSSurya Prakki * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
40bbaa8b60SDan Kruchinin /*
41bbaa8b60SDan Kruchinin * Copyright (c) 2012 by Delphix. All rights reserved.
42bbaa8b60SDan Kruchinin */
43bbaa8b60SDan Kruchinin
447c478bd9Sstevel@tonic-gate #include <stdio.h>
45004388ebScasper #include <stdio_ext.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <ftw.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <syslog.h>
517c478bd9Sstevel@tonic-gate #include <netconfig.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <netdb.h>
547c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
557c478bd9Sstevel@tonic-gate #include <netinet/in.h>
567c478bd9Sstevel@tonic-gate #include <sys/param.h>
577c478bd9Sstevel@tonic-gate #include <sys/resource.h>
587c478bd9Sstevel@tonic-gate #include <sys/file.h>
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <sys/stat.h>
617c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
627c478bd9Sstevel@tonic-gate #include <dirent.h>
637c478bd9Sstevel@tonic-gate #include <errno.h>
647c478bd9Sstevel@tonic-gate #include <rpcsvc/sm_inter.h>
657c478bd9Sstevel@tonic-gate #include <rpcsvc/nsm_addr.h>
667c478bd9Sstevel@tonic-gate #include <thread.h>
677c478bd9Sstevel@tonic-gate #include <synch.h>
687c478bd9Sstevel@tonic-gate #include <net/if.h>
697c478bd9Sstevel@tonic-gate #include <limits.h>
707c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
717c478bd9Sstevel@tonic-gate #include <priv_utils.h>
727c478bd9Sstevel@tonic-gate #include "sm_statd.h"
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate #define home0 "/var/statmon"
767c478bd9Sstevel@tonic-gate #define current0 "/var/statmon/sm"
777c478bd9Sstevel@tonic-gate #define backup0 "/var/statmon/sm.bak"
787c478bd9Sstevel@tonic-gate #define state0 "/var/statmon/state"
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate #define home1 "statmon"
817c478bd9Sstevel@tonic-gate #define current1 "statmon/sm/"
827c478bd9Sstevel@tonic-gate #define backup1 "statmon/sm.bak/"
837c478bd9Sstevel@tonic-gate #define state1 "statmon/state"
847c478bd9Sstevel@tonic-gate
85*98573c19SMarcel Telka extern int daemonize_init(void);
86*98573c19SMarcel Telka extern void daemonize_fini(int fd);
87*98573c19SMarcel Telka
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate * User and group IDs to run as. These are hardwired, rather than looked
907c478bd9Sstevel@tonic-gate * up at runtime, because they are very unlikely to change and because they
917c478bd9Sstevel@tonic-gate * provide some protection against bogus changes to the passwd and group
927c478bd9Sstevel@tonic-gate * files.
937c478bd9Sstevel@tonic-gate */
949acbbeafSnn35248 uid_t daemon_uid = DAEMON_UID;
959acbbeafSnn35248 gid_t daemon_gid = DAEMON_GID;
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate char STATE[MAXPATHLEN], CURRENT[MAXPATHLEN], BACKUP[MAXPATHLEN];
987c478bd9Sstevel@tonic-gate static char statd_home[MAXPATHLEN];
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate int debug;
1017c478bd9Sstevel@tonic-gate int regfiles_only = 0; /* 1 => use symlinks in statmon, 0 => don't */
1027c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * These variables will be used to store all the
1067c478bd9Sstevel@tonic-gate * alias names for the host, as well as the -a
1077c478bd9Sstevel@tonic-gate * command line hostnames.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate int host_name_count;
1107c478bd9Sstevel@tonic-gate char **host_name; /* store -a opts */
1117c478bd9Sstevel@tonic-gate int addrix; /* # of -a entries */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * The following 2 variables are meaningful
1167c478bd9Sstevel@tonic-gate * only under a HA configuration.
1177c478bd9Sstevel@tonic-gate * The path_name array is dynamically allocated in main() during
1187c478bd9Sstevel@tonic-gate * command line argument processing for the -p options.
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate char **path_name = NULL; /* store -p opts */
1217c478bd9Sstevel@tonic-gate int pathix = 0; /* # of -p entries */
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /* Global variables. Refer to sm_statd.h for description */
1247c478bd9Sstevel@tonic-gate mutex_t crash_lock;
1257c478bd9Sstevel@tonic-gate int die;
1267c478bd9Sstevel@tonic-gate int in_crash;
1277c478bd9Sstevel@tonic-gate mutex_t sm_trylock;
1287c478bd9Sstevel@tonic-gate rwlock_t thr_rwlock;
1297c478bd9Sstevel@tonic-gate cond_t retrywait;
1307c478bd9Sstevel@tonic-gate mutex_t name_addrlock;
1317c478bd9Sstevel@tonic-gate
132*98573c19SMarcel Telka mutex_t merges_lock;
133*98573c19SMarcel Telka cond_t merges_cond;
134*98573c19SMarcel Telka boolean_t in_merges;
135*98573c19SMarcel Telka
1367c478bd9Sstevel@tonic-gate /* forward references */
1377c478bd9Sstevel@tonic-gate static void set_statmon_owner(void);
1387c478bd9Sstevel@tonic-gate static void copy_client_names(void);
1397c478bd9Sstevel@tonic-gate static void one_statmon_owner(const char *);
1407c478bd9Sstevel@tonic-gate static int nftw_owner(const char *, const struct stat *, int, struct FTW *);
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * statd protocol
1447c478bd9Sstevel@tonic-gate * commands:
1457c478bd9Sstevel@tonic-gate * SM_STAT
1467c478bd9Sstevel@tonic-gate * returns stat_fail to caller
1477c478bd9Sstevel@tonic-gate * SM_MON
148*98573c19SMarcel Telka * adds an entry to the monitor_q and the record_q.
1497c478bd9Sstevel@tonic-gate * This message is sent by the server lockd to the server
1507c478bd9Sstevel@tonic-gate * statd, to indicate that a new client is to be monitored.
1517c478bd9Sstevel@tonic-gate * It is also sent by the server lockd to the client statd
1527c478bd9Sstevel@tonic-gate * to indicate that a new server is to be monitored.
1537c478bd9Sstevel@tonic-gate * SM_UNMON
1547c478bd9Sstevel@tonic-gate * removes an entry from the monitor_q and the record_q
1557c478bd9Sstevel@tonic-gate * SM_UNMON_ALL
1567c478bd9Sstevel@tonic-gate * removes all entries from a particular host from the
1577c478bd9Sstevel@tonic-gate * monitor_q and the record_q. Our statd has this
1587c478bd9Sstevel@tonic-gate * disabled.
1597c478bd9Sstevel@tonic-gate * SM_SIMU_CRASH
160*98573c19SMarcel Telka * simulate a crash. Removes everything from the
1617c478bd9Sstevel@tonic-gate * record_q and the recovery_q, then calls statd_init()
1627c478bd9Sstevel@tonic-gate * to restart things. This message is sent by the server
1637c478bd9Sstevel@tonic-gate * lockd to the server statd to have all clients notified
1647c478bd9Sstevel@tonic-gate * that they should reclaim locks.
1657c478bd9Sstevel@tonic-gate * SM_NOTIFY
1667c478bd9Sstevel@tonic-gate * Sent by statd on server to statd on client during
1677c478bd9Sstevel@tonic-gate * crash recovery. The client statd passes the info
1687c478bd9Sstevel@tonic-gate * to its lockd so it can attempt to reclaim the locks
1697c478bd9Sstevel@tonic-gate * held on the server.
1707c478bd9Sstevel@tonic-gate *
1717c478bd9Sstevel@tonic-gate * There are three main hash tables used to keep track of things.
1727c478bd9Sstevel@tonic-gate * mon_table
1737c478bd9Sstevel@tonic-gate * table that keeps track hosts statd must watch. If one of
1747c478bd9Sstevel@tonic-gate * these hosts crashes, then any locks held by that host must
1757c478bd9Sstevel@tonic-gate * be released.
1767c478bd9Sstevel@tonic-gate * record_table
1777c478bd9Sstevel@tonic-gate * used to keep track of all the hostname files stored in
1787c478bd9Sstevel@tonic-gate * the directory /var/statmon/sm. These are client hosts who
1797c478bd9Sstevel@tonic-gate * are holding or have held a lock at some point. Needed
1807c478bd9Sstevel@tonic-gate * to determine if a file needs to be created for host in
1817c478bd9Sstevel@tonic-gate * /var/statmon/sm.
1827c478bd9Sstevel@tonic-gate * recov_q
1837c478bd9Sstevel@tonic-gate * used to keep track hostnames during a recovery
1847c478bd9Sstevel@tonic-gate *
1857c478bd9Sstevel@tonic-gate * The entries are hashed based upon the name.
1867c478bd9Sstevel@tonic-gate *
1877c478bd9Sstevel@tonic-gate * There is a directory /var/statmon/sm which holds a file named
1887c478bd9Sstevel@tonic-gate * for each host that is holding (or has held) a lock. This is
1897c478bd9Sstevel@tonic-gate * used during initialization on startup, or after a simulated
1907c478bd9Sstevel@tonic-gate * crash.
1917c478bd9Sstevel@tonic-gate */
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate static void
sm_prog_1(struct svc_req * rqstp,SVCXPRT * transp)194*98573c19SMarcel Telka sm_prog_1(struct svc_req *rqstp, SVCXPRT *transp)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate union {
1977c478bd9Sstevel@tonic-gate struct sm_name sm_stat_1_arg;
1987c478bd9Sstevel@tonic-gate struct mon sm_mon_1_arg;
1997c478bd9Sstevel@tonic-gate struct mon_id sm_unmon_1_arg;
2007c478bd9Sstevel@tonic-gate struct my_id sm_unmon_all_1_arg;
2017c478bd9Sstevel@tonic-gate struct stat_chge ntf_arg;
2027c478bd9Sstevel@tonic-gate struct reg1args reg1_arg;
2037c478bd9Sstevel@tonic-gate } argument;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate union {
2067c478bd9Sstevel@tonic-gate sm_stat_res stat_resp;
2077c478bd9Sstevel@tonic-gate sm_stat mon_resp;
2087c478bd9Sstevel@tonic-gate struct reg1res reg1_resp;
2097c478bd9Sstevel@tonic-gate } result;
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate bool_t (*xdr_argument)(), (*xdr_result)();
2127c478bd9Sstevel@tonic-gate char *(*local)();
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate * Dispatch according to which protocol is being used:
2167c478bd9Sstevel@tonic-gate * NSM_ADDR_PROGRAM is the private lockd address
2177c478bd9Sstevel@tonic-gate * registration protocol.
2187c478bd9Sstevel@tonic-gate * SM_PROG is the normal statd (NSM) protocol.
2197c478bd9Sstevel@tonic-gate */
2207c478bd9Sstevel@tonic-gate if (rqstp->rq_prog == NSM_ADDR_PROGRAM) {
2217c478bd9Sstevel@tonic-gate switch (rqstp->rq_proc) {
2227c478bd9Sstevel@tonic-gate case NULLPROC:
2237c478bd9Sstevel@tonic-gate svc_sendreply(transp, xdr_void, (caddr_t)NULL);
2247c478bd9Sstevel@tonic-gate return;
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate case NSMADDRPROC1_REG:
2277c478bd9Sstevel@tonic-gate xdr_argument = xdr_reg1args;
2287c478bd9Sstevel@tonic-gate xdr_result = xdr_reg1res;
2297c478bd9Sstevel@tonic-gate local = (char *(*)()) nsmaddrproc1_reg;
2307c478bd9Sstevel@tonic-gate break;
2317c478bd9Sstevel@tonic-gate
232bbaa8b60SDan Kruchinin case NSMADDRPROC1_UNREG: /* Not impl. */
2337c478bd9Sstevel@tonic-gate default:
2347c478bd9Sstevel@tonic-gate svcerr_noproc(transp);
2357c478bd9Sstevel@tonic-gate return;
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate } else {
238bbaa8b60SDan Kruchinin /* Must be SM_PROG */
2397c478bd9Sstevel@tonic-gate switch (rqstp->rq_proc) {
2407c478bd9Sstevel@tonic-gate case NULLPROC:
2417c478bd9Sstevel@tonic-gate svc_sendreply(transp, xdr_void, (caddr_t)NULL);
2427c478bd9Sstevel@tonic-gate return;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate case SM_STAT:
2457c478bd9Sstevel@tonic-gate xdr_argument = xdr_sm_name;
2467c478bd9Sstevel@tonic-gate xdr_result = xdr_sm_stat_res;
247bbaa8b60SDan Kruchinin local = (char *(*)()) sm_stat_svc;
2487c478bd9Sstevel@tonic-gate break;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate case SM_MON:
2517c478bd9Sstevel@tonic-gate xdr_argument = xdr_mon;
2527c478bd9Sstevel@tonic-gate xdr_result = xdr_sm_stat_res;
253bbaa8b60SDan Kruchinin local = (char *(*)()) sm_mon_svc;
2547c478bd9Sstevel@tonic-gate break;
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate case SM_UNMON:
2577c478bd9Sstevel@tonic-gate xdr_argument = xdr_mon_id;
2587c478bd9Sstevel@tonic-gate xdr_result = xdr_sm_stat;
259bbaa8b60SDan Kruchinin local = (char *(*)()) sm_unmon_svc;
2607c478bd9Sstevel@tonic-gate break;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate case SM_UNMON_ALL:
2637c478bd9Sstevel@tonic-gate xdr_argument = xdr_my_id;
2647c478bd9Sstevel@tonic-gate xdr_result = xdr_sm_stat;
265bbaa8b60SDan Kruchinin local = (char *(*)()) sm_unmon_all_svc;
2667c478bd9Sstevel@tonic-gate break;
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate case SM_SIMU_CRASH:
2697c478bd9Sstevel@tonic-gate xdr_argument = xdr_void;
2707c478bd9Sstevel@tonic-gate xdr_result = xdr_void;
271bbaa8b60SDan Kruchinin local = (char *(*)()) sm_simu_crash_svc;
2727c478bd9Sstevel@tonic-gate break;
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate case SM_NOTIFY:
2757c478bd9Sstevel@tonic-gate xdr_argument = xdr_stat_chge;
2767c478bd9Sstevel@tonic-gate xdr_result = xdr_void;
277bbaa8b60SDan Kruchinin local = (char *(*)()) sm_notify_svc;
2787c478bd9Sstevel@tonic-gate break;
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate default:
2817c478bd9Sstevel@tonic-gate svcerr_noproc(transp);
2827c478bd9Sstevel@tonic-gate return;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate (void) memset(&argument, 0, sizeof (argument));
2877c478bd9Sstevel@tonic-gate if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
2887c478bd9Sstevel@tonic-gate svcerr_decode(transp);
2897c478bd9Sstevel@tonic-gate return;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate (void) memset(&result, 0, sizeof (result));
2937c478bd9Sstevel@tonic-gate (*local)(&argument, &result);
2947c478bd9Sstevel@tonic-gate if (!svc_sendreply(transp, xdr_result, (caddr_t)&result)) {
2957c478bd9Sstevel@tonic-gate svcerr_systemerr(transp);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
2997c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "statd: unable to free arguments\n");
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate * Remove all files under directory path_dir.
3057c478bd9Sstevel@tonic-gate */
3067c478bd9Sstevel@tonic-gate static int
remove_dir(char * path_dir)307*98573c19SMarcel Telka remove_dir(char *path_dir)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate DIR *dp;
3104bc0a2efScasper struct dirent *dirp;
3117c478bd9Sstevel@tonic-gate char tmp_path[MAXPATHLEN];
3127c478bd9Sstevel@tonic-gate
313*98573c19SMarcel Telka if ((dp = opendir(path_dir)) == NULL) {
3147c478bd9Sstevel@tonic-gate if (debug)
3157c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
316*98573c19SMarcel Telka "warning: open directory %s failed: %m\n",
317*98573c19SMarcel Telka path_dir);
3187c478bd9Sstevel@tonic-gate return (1);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate
3214bc0a2efScasper while ((dirp = readdir(dp)) != NULL) {
3227c478bd9Sstevel@tonic-gate if (strcmp(dirp->d_name, ".") != 0 &&
3237c478bd9Sstevel@tonic-gate strcmp(dirp->d_name, "..") != 0) {
3247c478bd9Sstevel@tonic-gate if (strlen(path_dir) + strlen(dirp->d_name) +2 >
3257c478bd9Sstevel@tonic-gate MAXPATHLEN) {
3267c478bd9Sstevel@tonic-gate
327*98573c19SMarcel Telka syslog(LOG_ERR, "statd: remove dir %s/%s "
328*98573c19SMarcel Telka "failed. Pathname too long.\n", path_dir,
329*98573c19SMarcel Telka dirp->d_name);
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate continue;
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate (void) strcpy(tmp_path, path_dir);
3347c478bd9Sstevel@tonic-gate (void) strcat(tmp_path, "/");
3357c478bd9Sstevel@tonic-gate (void) strcat(tmp_path, dirp->d_name);
3367c478bd9Sstevel@tonic-gate delete_file(tmp_path);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate (void) closedir(dp);
3417c478bd9Sstevel@tonic-gate return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate * Copy all files from directory `from_dir' to directory `to_dir'.
3467c478bd9Sstevel@tonic-gate * Symlinks, if any, are preserved.
3477c478bd9Sstevel@tonic-gate */
3487c478bd9Sstevel@tonic-gate void
copydir_from_to(char * from_dir,char * to_dir)349*98573c19SMarcel Telka copydir_from_to(char *from_dir, char *to_dir)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate int n;
3527c478bd9Sstevel@tonic-gate DIR *dp;
3534bc0a2efScasper struct dirent *dirp;
3547c478bd9Sstevel@tonic-gate char rname[MAXNAMELEN + 1];
3557c478bd9Sstevel@tonic-gate char path[MAXPATHLEN+MAXNAMELEN+2];
3567c478bd9Sstevel@tonic-gate
357*98573c19SMarcel Telka if ((dp = opendir(from_dir)) == NULL) {
3587c478bd9Sstevel@tonic-gate if (debug)
3597c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
360*98573c19SMarcel Telka "warning: open directory %s failed: %m\n",
361*98573c19SMarcel Telka from_dir);
3627c478bd9Sstevel@tonic-gate return;
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3654bc0a2efScasper while ((dirp = readdir(dp)) != NULL) {
3667c478bd9Sstevel@tonic-gate if (strcmp(dirp->d_name, ".") == 0 ||
3677c478bd9Sstevel@tonic-gate strcmp(dirp->d_name, "..") == 0) {
3687c478bd9Sstevel@tonic-gate continue;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate (void) strcpy(path, from_dir);
3727c478bd9Sstevel@tonic-gate (void) strcat(path, "/");
3737c478bd9Sstevel@tonic-gate (void) strcat(path, dirp->d_name);
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate if (is_symlink(path)) {
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate * Follow the link to get the referenced file name
3787c478bd9Sstevel@tonic-gate * and make a new link for that file in to_dir.
3797c478bd9Sstevel@tonic-gate */
3807c478bd9Sstevel@tonic-gate n = readlink(path, rname, MAXNAMELEN);
3817c478bd9Sstevel@tonic-gate if (n <= 0) {
3827c478bd9Sstevel@tonic-gate if (debug >= 2) {
383*98573c19SMarcel Telka (void) printf("copydir_from_to: can't "
384*98573c19SMarcel Telka "read link %s\n", path);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate continue;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate rname[n] = '\0';
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate (void) create_symlink(to_dir, rname, dirp->d_name);
3917c478bd9Sstevel@tonic-gate } else {
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate * Simply copy regular files to to_dir.
3947c478bd9Sstevel@tonic-gate */
3957c478bd9Sstevel@tonic-gate (void) strcpy(path, to_dir);
3967c478bd9Sstevel@tonic-gate (void) strcat(path, "/");
3977c478bd9Sstevel@tonic-gate (void) strcat(path, dirp->d_name);
3987c478bd9Sstevel@tonic-gate (void) create_file(path);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate (void) closedir(dp);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate static int
init_hostname(void)4067c478bd9Sstevel@tonic-gate init_hostname(void)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate struct lifnum lifn;
4097c478bd9Sstevel@tonic-gate int sock;
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
4127c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "statd:init_hostname, socket: %m");
4137c478bd9Sstevel@tonic-gate return (-1);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC;
4177c478bd9Sstevel@tonic-gate lifn.lifn_flags = 0;
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
4207c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
4217c478bd9Sstevel@tonic-gate "statd:init_hostname, get number of interfaces, error: %m");
4227c478bd9Sstevel@tonic-gate close(sock);
4237c478bd9Sstevel@tonic-gate return (-1);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate host_name_count = lifn.lifn_count;
4277c478bd9Sstevel@tonic-gate
428*98573c19SMarcel Telka host_name = malloc(host_name_count * sizeof (char *));
4297c478bd9Sstevel@tonic-gate if (host_name == NULL) {
4307c478bd9Sstevel@tonic-gate perror("statd -a can't get ip configuration\n");
4317c478bd9Sstevel@tonic-gate close(sock);
4327c478bd9Sstevel@tonic-gate return (-1);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate close(sock);
4357c478bd9Sstevel@tonic-gate return (0);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
438*98573c19SMarcel Telka static void
thr_statd_merges(void)439*98573c19SMarcel Telka thr_statd_merges(void)
440*98573c19SMarcel Telka {
441*98573c19SMarcel Telka /*
442*98573c19SMarcel Telka * Get other aliases from each interface.
443*98573c19SMarcel Telka */
444*98573c19SMarcel Telka merge_hosts();
445*98573c19SMarcel Telka
446*98573c19SMarcel Telka /*
447*98573c19SMarcel Telka * Get all of the configured IP addresses.
448*98573c19SMarcel Telka */
449*98573c19SMarcel Telka merge_ips();
450*98573c19SMarcel Telka
451*98573c19SMarcel Telka /*
452*98573c19SMarcel Telka * Notify the waiters.
453*98573c19SMarcel Telka */
454*98573c19SMarcel Telka (void) mutex_lock(&merges_lock);
455*98573c19SMarcel Telka in_merges = B_FALSE;
456*98573c19SMarcel Telka (void) cond_broadcast(&merges_cond);
457*98573c19SMarcel Telka (void) mutex_unlock(&merges_lock);
458*98573c19SMarcel Telka }
459*98573c19SMarcel Telka
46011606941Sjwahlig int
main(int argc,char * argv[])46111606941Sjwahlig main(int argc, char *argv[])
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate int c;
4647c478bd9Sstevel@tonic-gate int ppid;
4657c478bd9Sstevel@tonic-gate extern char *optarg;
4667c478bd9Sstevel@tonic-gate int choice = 0;
4677c478bd9Sstevel@tonic-gate struct rlimit rl;
4687c478bd9Sstevel@tonic-gate int mode;
4697c478bd9Sstevel@tonic-gate int sz;
470*98573c19SMarcel Telka int pipe_fd = -1;
4717c478bd9Sstevel@tonic-gate int connmaxrec = RPC_MAXDATASIZE;
4727c478bd9Sstevel@tonic-gate
4737c478bd9Sstevel@tonic-gate addrix = 0;
4747c478bd9Sstevel@tonic-gate pathix = 0;
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate (void) gethostname(hostname, MAXHOSTNAMELEN);
4777c478bd9Sstevel@tonic-gate if (init_hostname() < 0)
4787c478bd9Sstevel@tonic-gate exit(1);
4797c478bd9Sstevel@tonic-gate
4809ff75adeSSurya Prakki while ((c = getopt(argc, argv, "Dd:a:G:p:rU:")) != EOF)
4817c478bd9Sstevel@tonic-gate switch (c) {
4827c478bd9Sstevel@tonic-gate case 'd':
4837c478bd9Sstevel@tonic-gate (void) sscanf(optarg, "%d", &debug);
4847c478bd9Sstevel@tonic-gate break;
4857c478bd9Sstevel@tonic-gate case 'D':
4867c478bd9Sstevel@tonic-gate choice = 1;
4877c478bd9Sstevel@tonic-gate break;
4887c478bd9Sstevel@tonic-gate case 'a':
4897c478bd9Sstevel@tonic-gate if (addrix < host_name_count) {
4907c478bd9Sstevel@tonic-gate if (strcmp(hostname, optarg) != 0) {
4917c478bd9Sstevel@tonic-gate sz = strlen(optarg);
4927c478bd9Sstevel@tonic-gate if (sz < MAXHOSTNAMELEN) {
4937c478bd9Sstevel@tonic-gate host_name[addrix] =
4947c478bd9Sstevel@tonic-gate (char *)xmalloc(sz+1);
4957c478bd9Sstevel@tonic-gate if (host_name[addrix] !=
4967c478bd9Sstevel@tonic-gate NULL) {
4977c478bd9Sstevel@tonic-gate (void) sscanf(optarg, "%s",
4987c478bd9Sstevel@tonic-gate host_name[addrix]);
4997c478bd9Sstevel@tonic-gate addrix++;
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate } else
5027c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5037c478bd9Sstevel@tonic-gate "statd: -a name of host is too long.\n");
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate } else
5067c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5077c478bd9Sstevel@tonic-gate "statd: -a exceeding maximum hostnames\n");
5087c478bd9Sstevel@tonic-gate break;
5099acbbeafSnn35248 case 'U':
5109acbbeafSnn35248 (void) sscanf(optarg, "%d", &daemon_uid);
5119acbbeafSnn35248 break;
5129acbbeafSnn35248 case 'G':
5139acbbeafSnn35248 (void) sscanf(optarg, "%d", &daemon_gid);
5149acbbeafSnn35248 break;
5157c478bd9Sstevel@tonic-gate case 'p':
5167c478bd9Sstevel@tonic-gate if (strlen(optarg) < MAXPATHLEN) {
5177c478bd9Sstevel@tonic-gate /* If the path_name array has not yet */
5187c478bd9Sstevel@tonic-gate /* been malloc'ed, do that. The array */
5197c478bd9Sstevel@tonic-gate /* should be big enough to hold all of the */
5207c478bd9Sstevel@tonic-gate /* -p options we might have. An upper */
5217c478bd9Sstevel@tonic-gate /* bound on the number of -p options is */
5227c478bd9Sstevel@tonic-gate /* argc/2, because each -p option consumes */
5237c478bd9Sstevel@tonic-gate /* two arguments. Here the upper bound */
5247c478bd9Sstevel@tonic-gate /* is supposing that all the command line */
5257c478bd9Sstevel@tonic-gate /* arguments are -p options, which would */
5267c478bd9Sstevel@tonic-gate /* actually never be the case. */
5277c478bd9Sstevel@tonic-gate if (path_name == NULL) {
5287c478bd9Sstevel@tonic-gate size_t sz = (argc/2) * sizeof (char *);
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate path_name = (char **)malloc(sz);
5317c478bd9Sstevel@tonic-gate if (path_name == NULL) {
5327c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5337c478bd9Sstevel@tonic-gate "statd: malloc failed\n");
5347c478bd9Sstevel@tonic-gate exit(1);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate (void) memset(path_name, 0, sz);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate path_name[pathix] = optarg;
5397c478bd9Sstevel@tonic-gate pathix++;
5407c478bd9Sstevel@tonic-gate } else {
5417c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5427c478bd9Sstevel@tonic-gate "statd: -p pathname is too long.\n");
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate break;
5457c478bd9Sstevel@tonic-gate case 'r':
5467c478bd9Sstevel@tonic-gate regfiles_only = 1;
5477c478bd9Sstevel@tonic-gate break;
5487c478bd9Sstevel@tonic-gate default:
5497c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
5507c478bd9Sstevel@tonic-gate "statd [-d level] [-D]\n");
5517c478bd9Sstevel@tonic-gate return (1);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate if (choice == 0) {
5557c478bd9Sstevel@tonic-gate (void) strcpy(statd_home, home0);
5567c478bd9Sstevel@tonic-gate (void) strcpy(CURRENT, current0);
5577c478bd9Sstevel@tonic-gate (void) strcpy(BACKUP, backup0);
5587c478bd9Sstevel@tonic-gate (void) strcpy(STATE, state0);
5597c478bd9Sstevel@tonic-gate } else {
5607c478bd9Sstevel@tonic-gate (void) strcpy(statd_home, home1);
5617c478bd9Sstevel@tonic-gate (void) strcpy(CURRENT, current1);
5627c478bd9Sstevel@tonic-gate (void) strcpy(BACKUP, backup1);
5637c478bd9Sstevel@tonic-gate (void) strcpy(STATE, state1);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate if (debug)
5667c478bd9Sstevel@tonic-gate (void) printf("debug is on, create entry: %s, %s, %s\n",
5677c478bd9Sstevel@tonic-gate CURRENT, BACKUP, STATE);
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl))
5707c478bd9Sstevel@tonic-gate (void) printf("statd: getrlimit failed. \n");
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate /* Set maxfdlimit current soft limit */
5735c6b0397SPavel Filipensky rl.rlim_cur = rl.rlim_max;
5747c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
5757c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "statd: unable to set RLIMIT_NOFILE to %d\n",
5765c6b0397SPavel Filipensky rl.rlim_cur);
5777c478bd9Sstevel@tonic-gate
578004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
579004388ebScasper
5807c478bd9Sstevel@tonic-gate if (!debug) {
581*98573c19SMarcel Telka pipe_fd = daemonize_init();
582*98573c19SMarcel Telka
5837c478bd9Sstevel@tonic-gate openlog("statd", LOG_PID, LOG_DAEMON);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate
5869acbbeafSnn35248 (void) _create_daemon_lock(STATD, daemon_uid, daemon_gid);
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * establish our lock on the lock file and write our pid to it.
5897c478bd9Sstevel@tonic-gate * exit if some other process holds the lock, or if there's any
5907c478bd9Sstevel@tonic-gate * error in writing/locking the file.
5917c478bd9Sstevel@tonic-gate */
5927c478bd9Sstevel@tonic-gate ppid = _enter_daemon_lock(STATD);
5937c478bd9Sstevel@tonic-gate switch (ppid) {
5947c478bd9Sstevel@tonic-gate case 0:
5957c478bd9Sstevel@tonic-gate break;
5967c478bd9Sstevel@tonic-gate case -1:
5977c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "error locking for %s: %s", STATD,
5987c478bd9Sstevel@tonic-gate strerror(errno));
5997c478bd9Sstevel@tonic-gate exit(2);
6007c478bd9Sstevel@tonic-gate default:
6017c478bd9Sstevel@tonic-gate /* daemon was already running */
6027c478bd9Sstevel@tonic-gate exit(0);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate
605*98573c19SMarcel Telka mutex_init(&merges_lock, USYNC_THREAD, NULL);
606*98573c19SMarcel Telka cond_init(&merges_cond, USYNC_THREAD, NULL);
607*98573c19SMarcel Telka in_merges = B_TRUE;
6087c478bd9Sstevel@tonic-gate
609*98573c19SMarcel Telka /*
610*98573c19SMarcel Telka * Create thr_statd_merges() thread to populate the host_name list
611*98573c19SMarcel Telka * asynchronously.
612*98573c19SMarcel Telka */
613*98573c19SMarcel Telka if (thr_create(NULL, 0, (void *(*)(void *))thr_statd_merges, NULL,
614*98573c19SMarcel Telka THR_DETACHED, NULL) != 0) {
615*98573c19SMarcel Telka syslog(LOG_ERR, "statd: unable to create thread for "
616*98573c19SMarcel Telka "thr_statd_merges().");
617*98573c19SMarcel Telka exit(1);
618*98573c19SMarcel Telka }
619bbaa8b60SDan Kruchinin
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate * Set to automatic mode such that threads are automatically
6227c478bd9Sstevel@tonic-gate * created
6237c478bd9Sstevel@tonic-gate */
6247c478bd9Sstevel@tonic-gate mode = RPC_SVC_MT_AUTO;
6257c478bd9Sstevel@tonic-gate if (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {
6267c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
6277c478bd9Sstevel@tonic-gate "statd:unable to set automatic MT mode.");
6287c478bd9Sstevel@tonic-gate exit(1);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate * Set non-blocking mode and maximum record size for
6337c478bd9Sstevel@tonic-gate * connection oriented RPC transports.
6347c478bd9Sstevel@tonic-gate */
6357c478bd9Sstevel@tonic-gate if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
6367c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "unable to set maximum RPC record size");
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate
6397c478bd9Sstevel@tonic-gate if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "netpath")) {
640*98573c19SMarcel Telka syslog(LOG_ERR, "statd: unable to create (SM_PROG, SM_VERS) "
641*98573c19SMarcel Telka "for netpath.");
6427c478bd9Sstevel@tonic-gate exit(1);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate if (!svc_create(sm_prog_1, NSM_ADDR_PROGRAM, NSM_ADDR_V1, "netpath")) {
646*98573c19SMarcel Telka syslog(LOG_ERR, "statd: unable to create (NSM_ADDR_PROGRAM, "
647*98573c19SMarcel Telka "NSM_ADDR_V1) for netpath.");
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate * Make sure /var/statmon and any alternate (-p) statmon
6527c478bd9Sstevel@tonic-gate * directories exist and are owned by daemon. Then change our uid
6537c478bd9Sstevel@tonic-gate * to daemon. The uid change is to prevent attacks against local
6547c478bd9Sstevel@tonic-gate * daemons that trust any call from a local root process.
6557c478bd9Sstevel@tonic-gate */
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate set_statmon_owner();
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate *
6617c478bd9Sstevel@tonic-gate * statd now runs as a daemon rather than root and can not
6627c478bd9Sstevel@tonic-gate * dump core under / because of the permission. It is
6637c478bd9Sstevel@tonic-gate * important that current working directory of statd be
6647c478bd9Sstevel@tonic-gate * changed to writable directory /var/statmon so that it
6657c478bd9Sstevel@tonic-gate * can dump the core upon the receipt of the signal.
6667c478bd9Sstevel@tonic-gate * One still need to set allow_setid_core to non-zero in
6677c478bd9Sstevel@tonic-gate * /etc/system to get the core dump.
6687c478bd9Sstevel@tonic-gate *
6697c478bd9Sstevel@tonic-gate */
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate if (chdir(statd_home) < 0) {
6727c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "can't chdir %s: %m", statd_home);
6737c478bd9Sstevel@tonic-gate exit(1);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate copy_client_names();
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate rwlock_init(&thr_rwlock, USYNC_THREAD, NULL);
6797c478bd9Sstevel@tonic-gate mutex_init(&crash_lock, USYNC_THREAD, NULL);
6807c478bd9Sstevel@tonic-gate mutex_init(&name_addrlock, USYNC_THREAD, NULL);
6817c478bd9Sstevel@tonic-gate cond_init(&retrywait, USYNC_THREAD, NULL);
6827c478bd9Sstevel@tonic-gate sm_inithash();
6837c478bd9Sstevel@tonic-gate die = 0;
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate * This variable is set to ensure that an sm_crash
6867c478bd9Sstevel@tonic-gate * request will not be done at the same time
6877c478bd9Sstevel@tonic-gate * when a statd_init is being done, since sm_crash
6887c478bd9Sstevel@tonic-gate * can reset some variables that statd_init will be using.
6897c478bd9Sstevel@tonic-gate */
6907c478bd9Sstevel@tonic-gate in_crash = 1;
6917c478bd9Sstevel@tonic-gate statd_init();
6927c478bd9Sstevel@tonic-gate
693*98573c19SMarcel Telka /*
694*98573c19SMarcel Telka * statd is up and running as far as we are concerned.
695*98573c19SMarcel Telka */
696*98573c19SMarcel Telka daemonize_fini(pipe_fd);
697*98573c19SMarcel Telka
6987c478bd9Sstevel@tonic-gate if (debug)
6997c478bd9Sstevel@tonic-gate (void) printf("Starting svc_run\n");
7007c478bd9Sstevel@tonic-gate svc_run();
7017c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "statd: svc_run returned\n");
7027c478bd9Sstevel@tonic-gate /* NOTREACHED */
7037c478bd9Sstevel@tonic-gate thr_exit((void *)1);
70411606941Sjwahlig return (0);
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate }
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate * Make sure the ownership of the statmon directories is correct, then
7107c478bd9Sstevel@tonic-gate * change our uid to match. If the top-level directories (/var/statmon, -p
7117c478bd9Sstevel@tonic-gate * arguments) don't exist, they are created first. The sm and sm.bak
7127c478bd9Sstevel@tonic-gate * directories are not created here, but if they already exist, they are
7137c478bd9Sstevel@tonic-gate * chowned to the correct uid, along with anything else in the
7147c478bd9Sstevel@tonic-gate * directories.
7157c478bd9Sstevel@tonic-gate */
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate static void
set_statmon_owner(void)71845916cd2Sjpk set_statmon_owner(void)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate int i;
72145916cd2Sjpk boolean_t can_do_mlp;
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate * Recursively chown/chgrp /var/statmon and the alternate paths,
7257c478bd9Sstevel@tonic-gate * creating them if necessary.
7267c478bd9Sstevel@tonic-gate */
7277c478bd9Sstevel@tonic-gate one_statmon_owner(statd_home);
7287c478bd9Sstevel@tonic-gate for (i = 0; i < pathix; i++) {
7297c478bd9Sstevel@tonic-gate char alt_path[MAXPATHLEN];
7307c478bd9Sstevel@tonic-gate
7317c478bd9Sstevel@tonic-gate snprintf(alt_path, MAXPATHLEN, "%s/statmon", path_name[i]);
7327c478bd9Sstevel@tonic-gate one_statmon_owner(alt_path);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate
73545916cd2Sjpk can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
7367c478bd9Sstevel@tonic-gate if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
7379acbbeafSnn35248 daemon_uid, daemon_gid, can_do_mlp ? PRIV_NET_BINDMLP : NULL,
73845916cd2Sjpk NULL) == -1) {
7397c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "can't run unprivileged: %m");
7407c478bd9Sstevel@tonic-gate exit(1);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate __fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
744*98573c19SMarcel Telka PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, NULL);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate * Copy client names from the alternate statmon directories into
7497c478bd9Sstevel@tonic-gate * /var/statmon. The top-level (statmon) directories should already
7507c478bd9Sstevel@tonic-gate * exist, though the sm and sm.bak directories might not.
7517c478bd9Sstevel@tonic-gate */
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate static void
copy_client_names(void)754*98573c19SMarcel Telka copy_client_names(void)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate int i;
7577c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN+SM_MAXPATHLEN];
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate /*
7607c478bd9Sstevel@tonic-gate * Copy all clients from alternate paths to /var/statmon/sm
7617c478bd9Sstevel@tonic-gate * Remove the files in alternate directory when copying is done.
7627c478bd9Sstevel@tonic-gate */
7637c478bd9Sstevel@tonic-gate for (i = 0; i < pathix; i++) {
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate * If the alternate directories do not exist, create it.
7667c478bd9Sstevel@tonic-gate * If they do exist, just do the copy.
7677c478bd9Sstevel@tonic-gate */
7687c478bd9Sstevel@tonic-gate snprintf(buf, sizeof (buf), "%s/statmon/sm", path_name[i]);
7697c478bd9Sstevel@tonic-gate if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
7707c478bd9Sstevel@tonic-gate if (errno != EEXIST) {
7717c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
7727c478bd9Sstevel@tonic-gate "can't mkdir %s: %m\n", buf);
7737c478bd9Sstevel@tonic-gate continue;
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate copydir_from_to(buf, CURRENT);
7767c478bd9Sstevel@tonic-gate (void) remove_dir(buf);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s/statmon/sm.bak",
7807c478bd9Sstevel@tonic-gate path_name[i]);
7817c478bd9Sstevel@tonic-gate if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
7827c478bd9Sstevel@tonic-gate if (errno != EEXIST) {
7837c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
7847c478bd9Sstevel@tonic-gate "can't mkdir %s: %m\n", buf);
7857c478bd9Sstevel@tonic-gate continue;
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate copydir_from_to(buf, BACKUP);
7887c478bd9Sstevel@tonic-gate (void) remove_dir(buf);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate }
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate * Create the given directory if it doesn't already exist. Set the user
7957c478bd9Sstevel@tonic-gate * and group to daemon for the directory and anything under it.
7967c478bd9Sstevel@tonic-gate */
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate static void
one_statmon_owner(const char * dir)7997c478bd9Sstevel@tonic-gate one_statmon_owner(const char *dir)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate if ((mkdir(dir, SM_DIRECTORY_MODE)) == -1) {
8027c478bd9Sstevel@tonic-gate if (errno != EEXIST) {
8037c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "can't mkdir %s: %m",
8047c478bd9Sstevel@tonic-gate dir);
8057c478bd9Sstevel@tonic-gate return;
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate if (debug)
8107c478bd9Sstevel@tonic-gate printf("Setting owner for %s\n", dir);
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate if (nftw(dir, nftw_owner, MAX_FDS, FTW_PHYS) != 0) {
8137c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "error setting owner for %s: %m",
8147c478bd9Sstevel@tonic-gate dir);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate * Set the user and group to daemon for the given file or directory. If
8207c478bd9Sstevel@tonic-gate * it's a directory, also makes sure that it is mode 755.
8217c478bd9Sstevel@tonic-gate * Generates a syslog message but does not return an error if there were
8227c478bd9Sstevel@tonic-gate * problems.
8237c478bd9Sstevel@tonic-gate */
8247c478bd9Sstevel@tonic-gate
8257c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
8267c478bd9Sstevel@tonic-gate static int
nftw_owner(const char * path,const struct stat * statp,int info,struct FTW * ftw)8277c478bd9Sstevel@tonic-gate nftw_owner(const char *path, const struct stat *statp, int info,
8287c478bd9Sstevel@tonic-gate struct FTW *ftw)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate if (!(info == FTW_F || info == FTW_D))
8317c478bd9Sstevel@tonic-gate return (0);
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate * Some older systems might have mode 777 directories. Fix that.
8357c478bd9Sstevel@tonic-gate */
8367c478bd9Sstevel@tonic-gate
8377c478bd9Sstevel@tonic-gate if (info == FTW_D && (statp->st_mode & (S_IWGRP | S_IWOTH)) != 0) {
8387c478bd9Sstevel@tonic-gate mode_t newmode = (statp->st_mode & ~(S_IWGRP | S_IWOTH)) &
8397c478bd9Sstevel@tonic-gate S_IAMB;
8407c478bd9Sstevel@tonic-gate
8417c478bd9Sstevel@tonic-gate if (debug)
8427c478bd9Sstevel@tonic-gate printf("chmod %03o %s\n", newmode, path);
8437c478bd9Sstevel@tonic-gate if (chmod(path, newmode) < 0) {
8447c478bd9Sstevel@tonic-gate int error = errno;
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "can't chmod %s to %03o: %m",
8477c478bd9Sstevel@tonic-gate path, newmode);
8487c478bd9Sstevel@tonic-gate if (debug)
8497c478bd9Sstevel@tonic-gate printf(" FAILED: %s\n", strerror(error));
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate
8537c478bd9Sstevel@tonic-gate /* If already owned by daemon, don't bother changing. */
8549acbbeafSnn35248 if (statp->st_uid == daemon_uid &&
8559acbbeafSnn35248 statp->st_gid == daemon_gid)
8567c478bd9Sstevel@tonic-gate return (0);
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate if (debug)
8597c478bd9Sstevel@tonic-gate printf("lchown %s daemon:daemon\n", path);
8609acbbeafSnn35248 if (lchown(path, daemon_uid, daemon_gid) < 0) {
8617c478bd9Sstevel@tonic-gate int error = errno;
8627c478bd9Sstevel@tonic-gate
8637c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "can't chown %s to daemon: %m",
8647c478bd9Sstevel@tonic-gate path);
8657c478bd9Sstevel@tonic-gate if (debug)
8667c478bd9Sstevel@tonic-gate printf(" FAILED: %s\n", strerror(error));
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate return (0);
8707c478bd9Sstevel@tonic-gate }
871